// Written by Chloe Fan // Reorganized by Nick Winter // July 2008 package { import flash.display.MovieClip; import flash.display.Sprite; import flash.geom.ColorTransform; import flash.geom.Rectangle; import org.wiiflash.Wiimote; import org.wiiflash.IR; import org.wiiflash.events.ButtonEvent; import org.wiiflash.events.WiimoteEvent; import flash.display.Graphics; public class Brush extends MovieClip { public var brush:Dot = new Dot();// instance of dot movieclip private var size:int = 12; private var color:uint = 0x000000; private var isPainting:Boolean = false; private var colorLocked:Boolean = false; public var strokes:Array = new Array(); public var stroke:Sprite; private var lockedStroke:Sprite; private var points:Array; public var locked:Boolean = false; private var distx:Number; // x distance between lockedStroke and brush private var disty:Number; // y distance between lockedStroke and brush public function Brush() { addChild(brush); } /////////////////////////////////////////////////////////////////////// // onRefresh // /////////////////////////////////////////////////////////////////////// public function onRefresh(e:WiimoteEvent):void { // update brush brush.width = size; brush.height = size; // get IR position var wiix = 1 - e.target.ir.x1;// x goes from 1-0 var wiiy = e.target.ir.y1;// y goes from 0-1 // update brush position if (wiix >= 0 && wiix <= 800 && wiiy >= 0 && wiiy <= 600) { brush.x = wiix * 800; brush.y = wiiy * 600; } else { if (brush.x/2 < 0 && wiix < 0) { brush.x = 0; } if (brush.x/2 > 800) { brush.x = 800; } if (wiiy < 0) { brush.y = 0; } if (wiiy > 600) { brush.y = 600; } } // draw stroke if is painting if (isPainting) { stroke.graphics.lineTo(brush.x, brush.y); points.push([brush.x, brush.y]); } // move stroke if locked if (locked) { lockedStroke.x = brush.x - distx; lockedStroke.y = brush.y - disty; } // change color if color is not locked if (!colorLocked) { // create new color // convert from radians to degrees var r = Math.round(Math.abs(e.target.sensorX) * 255); var g = Math.round(Math.abs(e.target.sensorY) * 255); var b = Math.round(Math.abs(e.target.sensorZ) * 255); color = r << 16 | g << 8 | b; // update color var newColor:ColorTransform = new ColorTransform(); newColor.color = color; brush.transform.colorTransform = newColor; if (stroke) { stroke.graphics.lineStyle(size, color); } } } /////////////////////////////////////////////////////////////////////// // Lock/unlock or Draw/stopDraw // /////////////////////////////////////////////////////////////////////// public function lockOrDraw(e:ButtonEvent):void { // if brush is intersecting stroke/shape, then access that stroke and lock it to brush for each (stroke in strokes) { if (brush.hitTestObject(stroke)) { lockedStroke = stroke; distx = brush.x - lockedStroke.x; disty = brush.y - lockedStroke.y; locked = true; continue; } } // else if on empty space or different strokes, draw as usual if (!locked) startDraw(e); } public function unlockOrStop(e:ButtonEvent):void { if (locked) { locked = false; } else { stopDraw(e); } } /////////////////////////////////////////////////////////////////////// // startDraw // /////////////////////////////////////////////////////////////////////// public function startDraw(e:ButtonEvent):void { isPainting = true; stroke = new Sprite(); strokes.push(stroke); addChild(stroke); stroke.graphics.lineStyle(size, color); stroke.graphics.moveTo(brush.x, brush.y); stroke.graphics.lineTo(brush.x, brush.y); points = new Array(); } /////////////////////////////////////////////////////////////////////// // stopDraw // /////////////////////////////////////////////////////////////////////// public function stopDraw(e:ButtonEvent):void { isPainting = false; var res = Recognize(points); if (res[0] == 0) { return; }// unrecognized if (res[0] == 1) {// line var intervalPoints:Array = res[1]; trace('line'); var x1:int = points[0][0]; var y1:int = points[0][1]; var x2:int = points[points.length-1][0]; var y2:int = points[points.length-1][1]; stroke.graphics.clear(); stroke.graphics.lineStyle(size, color); stroke.graphics.moveTo(x1, y1); stroke.graphics.lineTo(x2, y2); /*for each(var p:Array in intervalPoints) { stroke.graphics.beginFill(0xF9A1AF); stroke.graphics.drawCircle(p[0], p[1], 2); }*/ } else if (res[0] == 4) {// square trace('square'); var segments:Array = res[1][0]; var bounds:Array = findBounds(segments); stroke.x = bounds[0]; stroke.y = bounds[1]; stroke.graphics.clear(); stroke.graphics.beginFill(color); stroke.graphics.drawRect(0, 0, bounds[2], bounds[3]);              stroke.graphics.endFill(); /*var turningPoints:Array = res[1][1]; stroke.graphics.clear(); stroke.graphics.moveTo(points[0][0], points[0][1]); stroke.graphics.lineStyle(size, color); //trace(segments); //trace(turningPoints); for each (var p:Array in turningPoints) { stroke.graphics.lineTo(p[0], p[1]); //stroke.graphics.beginFill(0xFF91AF); //stroke.graphics.drawCircle(p[0], p[1], 15); //stroke.graphics.endFill(); } stroke.graphics.lineTo(points[0][0], points[0][1]);*/ } else if (res[0] == 3) {// triangle man trace('triangle'); var segments:Array = res[1][0]; var turningPoints:Array = res[1][1]; stroke.graphics.clear(); stroke.graphics.moveTo(points[0][0], points[0][1]); stroke.graphics.lineStyle(size, color); //trace(segments); //trace(turningPoints); for each (var p:Array in turningPoints) { stroke.graphics.lineTo(p[0], p[1]); //stroke.graphics.beginFill(0xFF91AF); //stroke.graphics.drawCircle(p[0], p[1], 15); //stroke.graphics.endFill(); } stroke.graphics.lineTo(points[0][0], points[0][1]); } } /////////////////////////////////////////////////////////////////////// // clearDraw // /////////////////////////////////////////////////////////////////////// public function clearDraw(e:ButtonEvent):void { for each (stroke in strokes) { removeChild(stroke); } strokes = new Array(); } /////////////////////////////////////////////////////////////////////// // Change brush size // /////////////////////////////////////////////////////////////////////// public function biggerBrush(e:ButtonEvent):void { if (size <= 77) { size += 5; } } public function smallerBrush(e:ButtonEvent):void { if (size >= 2) { size -= 5; } } /////////////////////////////////////////////////////////////////////// // Smaller functions // /////////////////////////////////////////////////////////////////////// public function togLockColor(e:ButtonEvent):void { colorLocked = !colorLocked; } public function undo(e:ButtonEvent):void { if (strokes.length > 0) { removeChild(strokes.pop()); } } private function findBounds(segments:Array):Array {   var x_min:Number = 9001;   var x_max:Number = -9001;   var y_min:Number = 9001;   var y_max:Number = -9001;   for each (var seg:Array in segments) {       for each (var p:Array in seg) {        if (p[0] < x_min) x_min = p[0];        if (p[0] > x_max) x_max = p[0];        if (p[1] < y_min) y_min = p[1];        if (p[1] > y_max) y_max = p[1];      }     }   // return x, y, width, height of new rectangle to be created   return [x_min, y_min, x_max - x_min, y_max - y_min]; } } }