Permalink
Browse files

ScrollableContainer can participate in layouts

All containers now keep track of their content height and width separately from the objects height and width.
VerticalPanel automatically matches height/width to content height/width
ScrollableContianer keeps them separate since the content may be far bigger (taller) than the visible clipped object
  • Loading branch information...
1 parent 7b24844 commit 95e2f6d869995f93829bd5d95d5f947561425c36 @nsxdavid nsxdavid committed Dec 30, 2011
@@ -18,16 +18,22 @@ void Start()
var optionsButton = UIZoomButton.create( "optionsUp.png", "optionsDown.png", 0, 0 );
optionsButton.animationDuration = 0.2f;
optionsButton.animationTargetScale = new Vector3( 1.4f, 1.4f, 1.4f );
+
+ // Scores button
+ var scores2 = UIContinuousButton.create("scoresUp.png", "scoresDown.png", 0, 0);
+
+
// Vertical panel
var vPanel = UIVerticalPanel.create( "vertPanelTop.png", "vertPanelMiddle.png", "vertPanelBottom.png" );
vPanel.beginUpdates();
vPanel.spacing = 20;
vPanel.edgeInsets = new UIEdgeInsets( 30, 10, 20, 10 );
- vPanel.addChild( playButton, scores, optionsButton );
+ vPanel.addChild( playButton, scores, optionsButton, scores2 );
vPanel.endUpdates();
+
vPanel.positionCenter();
}
@@ -29,6 +29,8 @@ public enum UILayoutType { Horizontal, Vertical, BackgroundLayout, AbsoluteLayou
private bool _suspendUpdates; // when true, layoutChildren will do nothing
+ protected float _contentWidth;
+ protected float _contentHeight;
/// <summary>
/// Hides the container and all of it's children
@@ -139,8 +141,8 @@ protected virtual void layoutChildren()
// rules for vertical and horizontal layouts
if (_layoutType == UIAbstractContainer.UILayoutType.Horizontal || _layoutType == UIAbstractContainer.UILayoutType.Vertical) {
// start with the insets, then add each object + spacing then end with insets
- _width = _edgeInsets.left;
- _height = _edgeInsets.top + _scrollPosition;
+ _contentWidth = _edgeInsets.left;
+ _contentHeight = _edgeInsets.top + _scrollPosition;
// create UIAnchorInfo to control positioning
var anchorInfo = UIAnchorInfo.DefaultAnchorInfo();
@@ -173,18 +175,18 @@ protected virtual void layoutChildren()
}
// we add spacing for all but the first and last
if (i != 0 && i != lastIndex)
- _width += _spacing;
+ _contentWidth += _spacing;
// Set anchor offset
- anchorInfo.OffsetX = _width * hdFactor;
+ anchorInfo.OffsetX = _contentWidth * hdFactor;
item.anchorInfo = anchorInfo;
// all items get their width added
- _width += item.width;
+ _contentWidth += item.width;
// height will just be the height of the tallest item
- if (_height < item.height)
- _height = item.height;
+ if (_contentHeight < item.height)
+ _contentHeight = item.height;
i++;
}
@@ -217,39 +219,39 @@ protected virtual void layoutChildren()
}
// we add spacing for all but the first and last
if (i != 0 && i != lastIndex)
- _height += _spacing;
+ _contentHeight += _spacing;
// Set anchor offset
- anchorInfo.OffsetY = _height * hdFactor;
+ anchorInfo.OffsetY = _contentHeight * hdFactor;
item.anchorInfo = anchorInfo;
// all items get their height added
- _height += item.height;
+ _contentHeight += item.height;
// width will just be the width of the widest item
- if (_width < item.width)
- _width = item.width;
+ if (_contentWidth < item.width)
+ _contentWidth = item.width;
i++;
}
}
// add the right and bottom edge inset to finish things off
- _width += _edgeInsets.right;
- _height += _edgeInsets.bottom;
+ _contentWidth += _edgeInsets.right;
+ _contentHeight += _edgeInsets.bottom;
}
else if (_layoutType == UIAbstractContainer.UILayoutType.AbsoluteLayout) {
foreach (var item in _children) {
if (!item.hidden) {
item.localPosition = new Vector3(item.position.x, item.position.y, item.position.z);
// find the width that contains the item with the largest offset/width
- if (_width < item.localPosition.x + item.width)
- _width = item.localPosition.x + item.width;
+ if (_contentWidth < item.localPosition.x + item.width)
+ _contentWidth = item.localPosition.x + item.width;
// find the height that contains the item with the largest offset/height
- if (_height < -item.localPosition.y + item.height)
- _height = -item.localPosition.y + item.height;
+ if (_contentHeight < -item.localPosition.y + item.height)
+ _contentHeight = -item.localPosition.y + item.height;
}
}
}
@@ -267,4 +269,10 @@ public override void transformChanged()
layoutChildren();
}
+ public void matchSizeToContentSize()
+ {
+ _width = _contentWidth;
+ _height = _contentHeight;
+ }
+
}
@@ -161,7 +161,19 @@ public void setSize( float width, float height )
{
_touchFrame = new Rect( position.x, -position.y, width, height );
calculateMinMaxInsets();
- }
+ }
+
+ public override float width
+ {
+ get { return _touchFrame.width; }
+ }
+
+
+ public override float height
+ {
+ get { return _touchFrame.height; }
+ }
+
/// <summary>
@@ -172,8 +184,8 @@ public override void addChild( params UISprite[] children )
base.addChild( children );
// after the children are added we can grab the width/height which are freshly calculated
- _contentSize.x = width;
- _contentSize.y = height;
+ _contentSize.x = _contentWidth;
+ _contentSize.y = _contentHeight;
calculateMinMaxInsets();
foreach( var child in children )
@@ -206,13 +206,13 @@ public virtual void transformChanged()
#region IPositionable implementation
// subclasses should implement these methods if they want to take part in positioning!
- public float width
+ public virtual float width
{
get { return _width; }
}
- public float height
+ public virtual float height
{
get { return _height; }
}
@@ -92,18 +92,19 @@ public new void addChild( params UISprite[] children )
protected override void layoutChildren()
{
base.layoutChildren();
+ matchSizeToContentSize();
// make sure we have enough height to work with. totalAvailableHeight will be calculated as the minimum required height for the panel
// if _height is greater than that, we will use it
var totalAvailableHeight = _topStripHeight + _bottomStripHeight + 1 + _edgeInsets.top + _edgeInsets.bottom;
- if( _height > totalAvailableHeight )
- totalAvailableHeight = (int)_height;
+ if( _contentHeight > totalAvailableHeight )
+ totalAvailableHeight = (int)_contentHeight;
// now we move our sprites into position. we have the proper height now so we can use that with a couple extra pixels to fill the gap
var leftoverHeight = totalAvailableHeight - _topStripHeight - _bottomStripHeight + 3;
_middleStrip.setSize( _middleStrip.width, leftoverHeight );
- _bottomStrip.localPosition = new Vector3( _bottomStrip.localPosition.x, -_height - _bottomStripHeight, _bottomStrip.localPosition.z );
+ _bottomStrip.localPosition = new Vector3( _bottomStrip.localPosition.x, -_contentHeight - _bottomStripHeight, _bottomStrip.localPosition.z );
}
}

0 comments on commit 95e2f6d

Please sign in to comment.