GLOBALS {ignore} 
// 
// This routine computes the bounding box of a card deck, by looping through
// all cards in the deck, and grabbing the coordinates of each, while keeping
// track of the minimum and maximum values for X and Y. It uses those values
// to compute the maximum width and height of the bounding box, as well.
// 
ROUTINE computeBoundingBox {} {card x1 y1 w h x2 y2 bx1 by1 bx2 by2 bw bh} 
	// initialize computation variables:
	// lower-left (minimum) X/Y values
	SET bx1 9999 
	SET by1 9999 
	// upper-right (maximum) X/Y values
	SET bx2 -9999 
	SET by2 -9999 
	// maximum width/height values
	SET bw 0 
	SET bh 0 
	// clear results text prior to calculations
	SET dbbc_co_clear 1 
	// loop through all cards in the specified deck
	SET card 0 
	LOOP card < dbbc_oi_max_num_cards 
		// select a card
		SET @dbbc_cn_text card 
		// get the X/Y coordinates of the deck (i.e., of the card)
		SET altiaGetObjXY dbbc_oi_integer 
		SET x1 altiaObjX 
		SET y1 altiaObjY 
		// get the width/height of the deck (i.e., of the card)
		SET altiaGetObjWH dbbc_oi_integer 
		SET w altiaObjW 
		SET h altiaObjH 
		// compute the upper-right limit of the card
		MATH x2 x1 + w 
		MATH y2 y1 + h 
		// determine the lower-left limit of the deck
		IF x1 < bx1 
			SET bx1 x1 
		END  
		IF y1 < by1 
			SET by1 y1 
		END  
		// determine the upper-right limit of the deck
		IF x2 > bx2 
			SET bx2 x2 
		END  
		IF y2 > by2 
			SET by2 y2 
		END  
		MATH card card + 1 
	END  
	// finally, select card #0
	SET @dbbc_cn_text 0 
	// determine the maximum size of the deck
	MATH bw bx2 - bx1 
	MATH bh by2 - by1 
	// display the results
	EXPR dbbc_co_text {[$format "(%d,%d)-(%d,%d) %d x %d" bx1 by1 bx2 by2 bw bh]} 
END  
// 
// This code is executed when the 'Bounding Box Compute Parameter' is set to 1,
// in the Custom Properties of the 'Deck Bounding Box Calculator' group.
// 
WHEN dbbc_co_compute == 1 
	// perform some sanity checks
	IF dbbc_oi_integer <= 0 
		SET dbbc_co_text "Please enter Deck Object ID first!" 
		RETURN 0 
	END  
	IF dbbc_cn_text < "0" 
		SET dbbc_co_text "Please enter Deck Card Name first!" 
		RETURN 0 
	END  
	IF dbbc_oi_max_num_cards <= 0 
		SET dbbc_co_text "Maximum Number of Cards must be at least 1!" 
		RETURN 0 
	END  
	// once all sanity checks pass, perform the calculations
	CALL computeBoundingBox {} ignore 
END