My favorites | Sign in
Project Home Downloads Issues Source
Search
for
CamoDisplayAndBoxModelDisplay  
* This is out of date and refers to version 2.0 of the framework. A update is coming soon. *
Featured, Phase-Implementation
Updated Feb 4, 2010 by jessefre...@gmail.com

Introduction

The CamoDisplay is the final building block of framework and a powerful alternative to the native Sprite class. Before getting into the CamoDisplay we should take some time to talk about the AbstractDisplay and the BoxModelDisplay.

Details

AbstractDisplay

The AbstractDisplay adds two methods, move and resize to the Sprite Class. The most important feature of the AbstractDisplay however is what happens when we call addChild and removeChild. The AbstractDisplay automatically creates a container called “display” that gets added as the first child to the display list. Every time another DisplayObject is added to an AbstractDisplay, it gets attached onto the class’s display instance. All the children methods are mapped over to the display Sprite. Here is a chart to illustrate the inheritance of the Camo’s display classes:

BoxModelDisplay

The BoxModelDisplay allows us to apply Margin, Padding, Border and a Background (Color/Image). The BoxModelDisplay manages the offset of the display sprite based on those properties. Lets take a look at how the Box Model gets rendered:

Camo’s Box Model consists of a Margin, Border and Padding wrapped around a display. To set the BoxModel’s main values (Margin, Padding, Border, and Background) we will need a style Object. We can get these from a CamoStyleSheet and apply it by placing it inside of a CamoDisplayStyle then calling the BoxModelDisplay’s applyStyle method. The CamoDisplayStyle class will parse the Object, convert the values to the correct type, and then call refresh to render the Box Model. Here is a list of supported properties of BoxModel:

CSS Property Values Example (px is stripped out by the parser)
background-alpha "number range 0 - 1" Example: background-alpha: .8;
background-color valid hex color background-color: 0xff0000; sets background red.
background-color: #ff0000; sets background red.
background-image type filename
jpg, png, or gif background-image: url ("/imgs/sample_image.jpg"); Will load in sample_image.jpg and redraw Box Model once loaded.
background-position value for x y background-position: 10px 30px; This only works if background-repeat is set to no-repeat.
background-repeat "repeat no-repeat repeat-x repeat-y" background-repeat: no-repeat; does not repeat the background image, can be used with background-position to move the image’s x,y position.
background-repeat: repeat-x: Repeats the background image along its x value based on the height of the background image.
background-scale9 value for
x, y, width, height background-scale9: 1px 1px 98px 48px; applies a scale9Grid to the background image.
border1 value for 
size style color alpha border: 1px solid #ff0000 .5; solid is the only style supported currently.
border-alpha "value range 0 - 1" border-alpha: .5; height value height: 50px;
margin2 value for
top, right, bottom, left margin: 5px 2px 2px 5px; applies values to top, right, bottom,left
margin: 5px 2px; applies 5 to top & bottom, 2 to right & left
margin: 10px; applies 10 to top, right, bottom, left
padding3 value for 
top, right, bottom, left padding: 5px 2px 2px 5px; applies values to top, right, bottom,left
padding: 5px 2px; applies 5 to top & bottom, 2 to right & left
padding: 10px; applies 10 to top, right, bottom, left
width value width: 100px;

BoxModel Background

A BoxModelDisplay’s background-color and background-image work exactly to how it would in HTML. The background-color is the bottom most color behind the display. It is important to realize that the CSS width and height affect how the background fill will take place. If you do not define the width and height of the BoxModel, it will use the values from the display. The background-image gets composited on top of the background-color. The background-image property lets the box model know how to load in a source image4. Background-images also have repeat rules: repeat (is default), no-repeat, repeat-x, repeat-y. When using no-repeat you can specify a background image’s position (x, y) to offset the image. This is helpful when you only want a background-image to be displayed at a certain location behind the display.

When the BoxModelDisplay does a refresh5, all of its values are reviewed and drawn to the graphic’s layer of the BoxModelDisplay. This insures that the border, background-color, and background-image are a single Bitmap to save on memory. The display Sprite (where all children get added to) is offset by the top and left Padding, and Border6. Once the refresh is complete the display will be correctly positioned. Padding is invisible unless you have set a background color or image. In that case the background will show through in the area offset by the padding. When you request the width or height of a BoxModelDisplay the dimensions are returned taking into account the padding, border and display. The BoxModelDisplay can be used on its own but when combined with the CamoDisplay, you get additional css style support.

CamoDisplay

The CamoDisplay inherits some powerful layout logic from the BoxModelDisplay and extends the applyStyle method by supporting additional CSS properties. Here is a list of the properties supported by the CamoDisplay:

CSS Property Values Example (px is stripped out by the parser)
align "left (default) center right" align: left; the logic for this should be in another Class that extends the CamoDisplay or add’s the CamoDisplay to its display list.
alpha "value range 0 - 1" alpha: .5; sets the alpha to 50%;
cursor "hand pointer none" cursor: pointer; this is a read only property. The actual logic to manage the cursor should be done in a class that extends the CamoDisplay.
overflow "visible(default) hidden" overflow: visible; any graphics outside of the display’s css width and height will be displayed.
overflow: hidden; the display is masked off based on the style’s width and height. If no width and height is set in the style the mask not work correctly.
rotation value rotation:180; sets the rotation to 180.
scale9Grid "values for x y width height" scale9Grid: 1px 1px 98px 48px; applies a rectangle to the CamoDisplay’s scale9Grid property.
vertical-align "top (default) middle bottom" vertical-align: top; the logic for this should be in another Class that extends the CamoDisplay or adds the CamoDisplay to its display list.
visibility "visible(default) hidden" visibility: hidden; sets visible to false;
x value x: 10px; sets the x position to 10;
y value y: 10px; sets the y position to 10;
z-index value z-index: 5; This is supported in the CamoDisplay addChild is called.

The most important functionality added by the CamoDisplay is the rasterize method. Just like the BoxModelDisplay which draws its border, background-color, and background-image to the graphics layer to save memory, the CamoDisplay can do the same thing with it’s display Sprite. When you call rasterize, a Bitmap snapshot of the display is take, the old display is removed and a new one is put in its place with the previous display's BitmapData drawn to it’s graphics layer. All the children that were attached to the display are removed (so the Garbage Collector can release them from memory) and you are left with a flattened display.

It is important to note that rasterize can only be called once since all children in the display are removed. The speed and memory benefits can greatly increase the performance of your application. This also allows you to layer images in the display Sprite and then flatten them into a single image. Rasterize should be used on any CamoDisplay where its children will never be changed or used outside of the initial setup of the class.

1 unsupported
border-top
border-right
border-bottom
border-left

2 unsupported
margin-top
margin-right
margin-bottom
margin-left

3 unsupported
padding-top
padding-right
padding-bottom
padding-left

4 The BoxModelDisplay supports jpg, png, or gif background images.

5 Calling the refresh method on a BoxModelDisplay is considered expensive and should only be used when needed.

6 Margin is not accounted for in offset of the display sprite unlike Padding. Instead Margin is a value accessible through the margin property and should be used by classes that layout or align BoxModelDeisplays.


Sign in to add a comment
Powered by Google Project Hosting