The HBox

Posted: April 27th, 2009 | Categories: AS3-Layout-Framework, Actionscript 3.0 | 4 Comments

An Hbox arranges its child components horizontally according to their desired size. If the size is is declared in percentage values, it will set the components bounds percentaged to the space left filled by fixed size components.

NOTE: percentaged values of child components are just handled if the desired size of the HBox was set explicitly.

For a detailed view on the component/container structure see AS3-Layout-Framework.

The following example demonstrates a basic usage of the HBox:

 
package
{
	import com.addicted2flash.layout.component.Application;
	import com.addicted2flash.layout.component.HBox;
	import com.addicted2flash.layout.core.UIContainer;	
 
	/**
	 * @author Tim Richter
	 */
	public class HBoxTest extends Application
	{
		private var _box: UIContainer;
 
		public function HBoxTest()
		{
			super( Application.ABSOLUTE );
 
			_box = new HBox();
			_box.add( new SimpleComponent( 100, 100 ) );
			_box.add( new SimpleComponent( 50, 100 ) );
			_box.add( new SimpleComponent( 100, 150 ) );
 
			//-- add hbox to the application container
			container.add( _box );
		}
	}
}
 

SimpleComponent.as

 
package layout
{
	import com.addicted2flash.layout.core.Component;
 
	import flash.display.Graphics;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;	
 
	/**
	 * @author Tim Richter
	 */
	public class SimpleComponent extends Component
	{
		private static var _id: int = 0;
		private var _tField: TextField;
 
		public function SimpleComponent( w: Number, h: Number )
		{
			desiredWidth = w;
			desiredHeight = h;
 
			_tField = new TextField( );
			_tField.autoSize = TextFieldAutoSize.CENTER;
			_tField.selectable = false;
			_tField.textColor = 0x999999;
			_tField.text = ( _id++ ).toString();
 
			display.addChild( _tField );
		}
 
		override public function arrange(): void
		{
			var g: Graphics = display.graphics;
			g.clear();
			g.beginFill( 0xccff00 );
			g.drawRect( 0, 0, width, height );
			g.endFill();
 
			_tField.x = ( width - _tField.textWidth ) * 0.5;
			_tField.y = ( height - _tField.textHeight ) * 0.5;
		}
	}
}
 

4 Comments on “The HBox”

  1. 1 TIMNHE said at 18:33 on August 5th, 2009:

    Hi Tim, first of all congratulations, I´m lost to much time thinking about to do something like that, great work.

    I´m trying your examples and playing a little. In the HBoxTest, I want to simulate a scalable html header where I have for example 3 Items, the first and the third are scalable to the stage, and the second has a fixed width.

    I add 3 SimpleComponents, and set its width as percent value:

    _box.add( new SimpleComponent( new Size( 1, 50 )));
    _box.add( new SimpleComponent( new Size( 250, 100 )) );
    _box.add( new SimpleComponent( new Size( 1, 100 )));

    Allways remain a white space between the tird object and the right side of the window.
    How can I fill the complete width of the stage with these components?

    Thanks!

  2. 2 tim said at 18:35 on August 6th, 2009:

    Hi TIMNHE,

    thank you for your comment. I had to refactor the sizes of the components for a better and faster workflow. So there are only explicit getter/setter for desired/maximum/minimum width and height right now. If you want to lay out a component in percentage values, you have to use desiredPercentageWidth/desiredPercentageHeight instead.

    Hope this could help you!

  3. 3 TIMNHE said at 23:28 on August 6th, 2009:

    I did´nt know this functions ‘desiredPercentageWidth / desiredPercentageHeight’, I will try it again.

    Another question … is there any class / property, to handle the component´s min/max position? I’m using the Canvas layout (sorry if it isn’t the place) to lay any components.

    http://www.aquar.net/canvasDemo.html

    To lay the components, the exmple uses the CanvasConstrain, but when the stageWidth is too narrow, I will to stop the position of any components, button2 for example.

    It´s possible to control a ‘global’ minSize?

    Thanks again!

  4. 4 TIMNHE said at 00:27 on August 7th, 2009:

    Just update the repository classes and I found the canvas minimumWidth and minimumHeight it´s perfect!

    thanks tim!



Leave a Reply