Java Reference
In-Depth Information
preferredWidth += horizontalMargin;
//calculate total height for container
preferredHeight = verticalMargin;
for(i in [0..<rows]) {
if(i > 0) preferredHeight += verticalSpacing;
insert preferredHeight into rowOffsets;
preferredHeight += rowHeights[i];
}
preferredHeight += verticalMargin;
maximumWidth = minimumWidth = preferredWidth;
maximumHeight = minimumHeight = preferredHeight;
}
Next, we need a function to actually position each node within the Grid. The
layout() function is shown in Listing 5.21.
Listing 5.21
Grid Layout Functions - layout()
function layout() {
inCalc = true; //do not call layout from layout
calcDimensions();
var index = 0;
for(node in content) {
if(node.visible) { // only layout visible nodes
var w = getNodePreferredWidth(node);
var h = getNodePreferredHeight(node);
var row = index / cols;
var col = index mod cols;
var x = colOffsets[col];
var y = rowOffsets[row];
// adjust based on Alignment
x += alignX(node, col);
// adjust based on Alignment
y += alignY(node, row);
node.translateX = x;
node.translateY = y;
index++;
}
}
inCalc = false;
}
alignX and alignY adjust the nodes position depending on the alignment value
for that column and row, respectively. The Alignment class is just a Java Enum
type. These two functions are presented in Listing 5.22.
 
Search WWH ::




Custom Search