Skip to content

Instantly share code, notes, and snippets.

@rafaelrinaldi
Created March 1, 2011 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaelrinaldi/848412 to your computer and use it in GitHub Desktop.
Save rafaelrinaldi/848412 to your computer and use it in GitHub Desktop.
Easy way to create a visible rectangle with basic parameters.
package tea.display
{
import flash.display.Sprite;
import flash.geom.Rectangle;
import tea.util.isUnassigned; // https://gist.github.com/848476
/**
*
* Easy way to create a visible rectangle with basic parameters.
*
* @param p_properties Rectangle properties.
* @return A Sprite instance with all the selected properties applied.
*
* */
public function drawRect( p_properties : Object ) : Sprite
{
var container : Sprite;
var bounds : Rectangle;
var x : Number, y : Number, width : Number, height : Number;
var color : Number, alpha : Number, lineThickness : Number, lineColor : Number; // "Special" parameters.
/** Default values. **/
const defaults : Object =
{
bounds: null,
x: 0,
y: 0,
width: 50,
height: 50,
color: 0xCC0000,
alpha: 1,
lineThickness: 0,
lineColor: 0x0
}
/**
* @param p_name Property name.
* @return Property value.
*/
function _get( p_name : String ) : * {
const value : * = p_properties[p_name];
if(isUnassigned(value)) return _default(p_name);
return value;
}
/**
* @param p_name Property name.
* @return Default property value.
*/
function _default( p_name : String ) : * {
return defaults[p_name];
}
bounds = _get("bounds");
if(bounds == null) {
x = _get("x");
y = _get("y");
width = _get("width");
height = _get("height");
}
color = _get("color");
alpha = _get("alpha");
lineThickness = _get("lineThickness");
lineColor = _get("lineColor");
container = new Sprite;
if(lineThickness > 0) container.graphics.lineStyle(lineThickness, lineColor);
container.graphics.beginFill(color, alpha);
if(bounds == null)
container.graphics.drawRect(x, y, width, height);
else
container.graphics.drawRect(bounds.x, bounds.y, bounds.width, bounds.height);
container.graphics.endFill();
return container;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment