Actionscript:
-
package{
-
-
import flash.display.*;
-
import flash.events.*;
-
import flash.geom.*;
-
-
public class QuickCheckBox extends Sprite{
-
-
private var _checked:Boolean;
-
private var _bitmap:Bitmap;
-
private var _canvas:BitmapData;
-
-
private static var checked:BitmapData;
-
private static var unchecked:BitmapData;
-
private static var bg:Shape;
-
private static var ex:Shape;
-
private static var pnt:Point;
-
-
// static init for BitmapData and drawing
-
{
-
trace("only render your graphics once");
-
pnt = new Point(0,0);
-
checked = new BitmapData(10,10, true, 0x00000000);
-
unchecked = new BitmapData(10, 10, true, 0x00000000);
-
bg = new Shape();
-
with(bg.graphics) lineStyle(0,0x000000), beginFill(0xEFEFEF), drawRect(0,0,9,9);
-
unchecked.draw(bg);
-
ex = new Shape();
-
with(ex.graphics) {
-
lineStyle(2,0x333333), moveTo(3, 3),
-
lineTo(7, 7), moveTo(7, 3), lineTo(3, 7);
-
}
-
checked.draw(bg);
-
checked.draw(ex);
-
}
-
-
public function QuickCheckBox(value:Boolean = false):void{
-
_checked = value;
-
_canvas = new BitmapData(10,10, true, 0x00000000);
-
_bitmap = new Bitmap(_canvas);
-
addChild(_bitmap);
-
buttonMode = true;
-
render();
-
addEventListener(MouseEvent.CLICK, onClick);
-
}
-
-
public function render():void{
-
if (_checked){
-
_canvas.copyPixels(QuickCheckBox.checked, _canvas.rect, pnt);
-
}else{
-
_canvas.copyPixels(unchecked, _canvas.rect, pnt);
-
}
-
}
-
-
private function onClick(evt:Event):void{
-
_checked = !_checked;
-
render();
-
this.dispatchEvent(new Event(Event.CHANGE, true));
-
}
-
-
public function get checked():Boolean{
-
return _checked;
-
}
-
-
public function set checked(val:Boolean):void{
-
_checked = val;
-
render();
-
}
-
}
-
}
This checkbox class uses a static initializer. Static initializers can come in handy to initialize some static variables for all class instances to make use of. In this case I'm using the static initializer to create checked and unchecked BitmapData objects - all instances of QuickCheckBox use these two static BitmapData objects rather than create their own unique ones.
Here is some client code if you want to test this class out:
Actionscript:
-
for (var i:int = 0; i<100; i++){
-
var checkBox:QuickCheckBox = new QuickCheckBox(true);
-
checkBox.x =100 + i % 10 * 12;
-
checkBox.y = 100 + int(i / 10) * 12;
-
// uncheck a few checkboxes
-
if (checkBox.x == checkBox.y){
-
checkBox.checked = false;
-
}
-
addChild( checkBox);
-
// checkBox dispatches a change event
-
checkBox.addEventListener(Event.CHANGE, onChange);
-
}
-
function onChange(evt:Event):void{
-
trace(evt.currentTarget.checked);
-
}
-
/*outputs
-
only render your graphics once
-
*/
This draws 100 checkboxes, - you'll also notice in the output window that the trace statement from QuickCheckBox only runs once.
Here are 100 instances of QuickCheckBox:
4 Comments
Is this mainly for imporved performance, or just handy? Looks really cool though. How do the curly braces static init relate to the compiler and when is it executed? Is it before the constuctor?
With many duplicate BitmapData objects it will save RAM…. The static initializer gets run only once either when the first you access a static property of the class or when the first instance of the class is created - not sure what it does on the compiler side of things…
Really handy!
Never heard about that before… Made my day, thanks!
no prob