Skip to content

Instantly share code, notes, and snippets.

@rafaelrinaldi
Created March 3, 2011 01:11
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/852120 to your computer and use it in GitHub Desktop.
Save rafaelrinaldi/852120 to your computer and use it in GitHub Desktop.
Easiest way to create a rectangle!
package tea.display
{
import flash.display.GradientType;
import flash.display.InterpolationMethod;
import flash.display.SpreadMethod;
import flash.display.Sprite;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.display.LineScaleMode;
import flash.display.CapsStyle;
import flash.display.JointStyle;
/**
*
* Easiest way to create a rectangle!
* The gradient fill option was based on GradientBox, created by my friend Zeh Fernando (https://github.com/zeh).
*
* Available properties:
*
* • Basic properties:
*
* • bounds: a Rectangle instance.
* • x: x position.
* • y: y position.
* • width: shape width.
* • height: shape height.
* • color: shape fill color.
* • alpha: shape opacity.
*
* • Line properties:
*
* • lineThickness: line thickness.
* • lineColor: line color.
*
* • Rounded properties:
*
* • ellipseWidth: ellipse width.
* • ellipseHeight: ellipse height.
*
* • Gradient properties:
*
* • type: gradient type (GradientType).
* • angle: gradient angle.
* • colors: an Array with gradient colors.
* • alphas: an Array with gradient alpha values.
* • ratios: an Array with gradient ratio values.
*
*
* @param p_properties Rectangle properties.
* @return A Sprite instance with all the selected properties applied.
* @example
* <pre>
* import tea.display.drawRect;
*
* const basicRect : Obect = {
* width: 50,
* height: 50,
* color: 0xCC0000
* };
*
* const lineRect : Object = {
* width: 100,
* height: 70,
* lineThickness: 2,
* lineColor: 0xCCCCCC
* }
*
* const gradientRect : Object = {
* width: 200,
* height: 200,
* gradient: {
* angle: 180,
* colors: [0x333333, 0xFFFFFF]
* }
* }
*
* addChild(drawRect(basicRect)).y = 0;
* addChild(drawRect(lineRect)).y = 60;
* addChild(drawRect(gradientRect)).y = 140;
* </pre>
*
* @date 01/03/2011
* @author Rafael Rinaldi (rafaelrinaldi.com)
*
* */
public function drawRect( p_properties : Object = null ) : Sprite
{
var container : Sprite; // Sprite instance that will be returned by this function.
var bounds : Rectangle; // Rectangle dimension.
var x : Number, y : Number, width : Number, height : Number, color : Number, alpha : Number; // Basic properties.
var lineThickness : Number, lineColor : Number; // Special properties.
var ellipseWidth : Number, ellipseHeight : Number; // Rounded properties.
var type : String, angle : Number, colors : Array, alphas : Array, ratios : Array, count : Number; // Gradient properties.
var matrix : Matrix;
/** Default values. **/
const defaults : Object =
{
bounds: null,
x: 0,
y: 0,
width: 50,
height: 50,
color: 0xCC0000,
alpha: 1,
lineThickness: 0,
lineColor: 0x0,
ellipseWidth: 0,
ellipseHeight: 0,
gradient: {
type: GradientType.LINEAR,
angle: 90,
colors: [0x0, 0xFFFFFF],
alphas: null,
ratios: null
}
}
/**
* @param p_name Property name.
* @param p_root Root to search for.
* @return Property value.
*/
function _get( p_name : String, p_root : String = null ) : * {
const value : * = p_root == null ? p_properties[p_name] : p_properties[p_root][p_name];
if(value == undefined) return _default(p_name, p_root);
return value;
}
/**
* @param p_name Property name.
* @param p_root Root to search for.
* @return Default property value.
*/
function _default( p_name : String, p_root : String = null ) : * {
return p_root == null ? defaults[p_name] : defaults[p_root][p_name];
}
/**
* @param p_name Property name.
* @return True if the property exists, false otherwise.
*/
function _has( p_name : String ) : Boolean {
return Boolean(p_properties.hasOwnProperty(p_name));
}
/**
* @return Default gradient alpha values.
*/
function _getDefaultAlphas() : Array {
alphas = [];
for (count = 0; count < colors.length; count++) alphas.push(1 + (1 * (count / (colors.length - 1))));
return alphas;
}
/**
* @return Default gradient ratio values.
*/
function _getDefaultRatios() : Array {
ratios = [];
for (count = 0; count < colors.length; count++) ratios.push(0 + (255 * (count / (colors.length - 1))));
return ratios;
}
/** User can pass a Rectangle instance instead of "x", "y", "width" and "height". **/
bounds = _get("bounds");
/** If a Rectangle instance isn't passed, get all the dimension properties individually. **/
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");
ellipseWidth = _get("ellipseWidth");
ellipseHeight = _get("ellipseHeight");
/** Just get gradient values if gradient settings was passed. **/
if(_has("gradient")) {
type = _get("type", "gradient");
angle = _get("angle", "gradient");
colors = _get("colors", "gradient");
alphas = _get("alphas", "gradient") || _getDefaultAlphas();
ratios = _get("ratios", "gradient") || _getDefaultRatios();
/** Creating gradient Matrix instance. **/
matrix = new Matrix;
matrix.createGradientBox(width, height, (angle / 180) * Math.PI, 0, 0);
}
/** Creating the sprite to return. **/
container = new Sprite;
if(lineThickness > 0) container.graphics.lineStyle(lineThickness, lineColor, 1, true, LineScaleMode.NORMAL, CapsStyle.ROUND, JointStyle.ROUND);
/** Applying fill. **/
if(_has("gradient"))
container.graphics.beginGradientFill(type, colors, alphas, ratios, matrix, SpreadMethod.PAD, InterpolationMethod.RGB);
else
container.graphics.beginFill(color, alpha);
/** Drawing the shape. **/
if(bounds == null)
container.graphics.drawRoundRect(x, y, width, height, ellipseWidth, ellipseHeight);
else
container.graphics.drawRoundRect(bounds.x, bounds.y, bounds.width, bounds.height, ellipseWidth, ellipseHeight);
container.graphics.endFill();
return container;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment