topical media & game development

talk show tell print

actionscript-wii-painter-ActionPaint.ax

actionscript-wii-painter-ActionPaint.ax [swf] flex


  package {
  
          
Wiimote @ax-actionscript-wii-painter-ActionPaint by Timen Olthof, 2008 Bachelor Project, VU Amsterdam The code for the paint function was ported and adapted from: "Splatter", by Stamen Design http://stamen.com/projects/splatter *

  
  
          import com.adobe.images.JPGEncoder;
          
          import flash.display.BitmapData;
          import flash.display.Graphics;
          import flash.display.MovieClip;
          import flash.display.Sprite;
          import flash.display.StageAlign;
          import flash.display.StageScaleMode;
          import flash.events.Event;
          import flash.events.MouseEvent;
          import flash.net.*;
          import flash.text.TextField;
          import flash.utils.ByteArray;
          
          import org.wiiflash.Wiimote;
          import org.wiiflash.events.ButtonEvent;
          import org.wiiflash.events.WiimoteEvent;
          
          [SWF(backgroundColor=0xffffff)]
          public class @ax-actionscript-wii-painter-ActionPaint extends Sprite
          {
                  //private var new_size_influence:Number = (Math.floor(Math.random() * 20) / 10) - 0.5;
                  //private var mid_point_push:Number = (Math.floor(Math.random() * 8) / 4) - 1;
                  private var new_size_influence:Number = 0.5;
                  private var mid_point_push:Number = .75;
                  private var max_line_width:Number = (Math.random() * 50) + 50;
                  //private var info.visible:Boolean = false;
                  private var curves:Array;
                  private var start_x:Number;
                  private var start_y:Number;
                  private var mid_x:Number;
                  private var mid_y:Number;
                  private var end_x:Number;
                  private var end_y:Number;
                  
                  private var distance:Number;
                  private var new_size:Number;
                  private var parity:Boolean;
                  private var size:Number = 0;
                  private var color:uint = 0x000000;
                  private var aggression:Number = 12.4;//3.4;
                  
                  private var vpX:Number;
                  private var vpY:Number;
                  
                  private var cursorX:Number;
                  private var cursorY:Number;
                  private var myWiimote:Wiimote;
                  private var cursor:Cursor;
                  
                  private var txt:TextField;
                  private var cursorVisible:Boolean = true;
                  private var counter:Number = 0;
                  
                  private var savePanel:MovieClip;
                  
                  
                  public function @ax-actionscript-wii-painter-ActionPaint() {
                          
                          stage.displayState = "fullScreen";
                          
                          //Security.allowDomain("http://localhost/");
                          
                          myWiimote = new Wiimote();
                          myWiimote.connect();
                          
                          stage.align = StageAlign.TOP_LEFT;
                          stage.scaleMode = StageScaleMode.NO_SCALE;
                          
                          cursor = new Cursor();
                          cursor.x = cursorX = vpX = stage.stageWidth / 2;
                          cursor.y = cursorY = vpY = stage.stageHeight / 2;
                          //cursor.visible = true;
                          addChild(cursor);
                          
                          txt = new TextField();
                          txt.x = 10;
                          txt.y = 10;
                          txt.width = 200;
                          txt.selectable = false;
                          txt.htmlText = "<font size='12px' color='#c3c3c3'>Wiimote @ax-actionscript-wii-painter-ActionPaint\nTimen Olthof 2008</font>";//max_line_width.toString();//.counter.toString();
                          //txt.text = Security.sandboxType;
                          addChild(txt);
                          
                          start_x = mid_x = end_x = stage.stageWidth / 2;
                          start_y = mid_y = end_y = stage.stageHeight / 2;
                          addEventListener(Event.ENTER_FRAME, onEnterFrame);
                          stage.addEventListener(MouseEvent.CLICK, onMouseClick); 
                          myWiimote.addEventListener(WiimoteEvent.UPDATE, onUpdated);
                          myWiimote.addEventListener(ButtonEvent.A_PRESS, onAPressed);
                          myWiimote.addEventListener(ButtonEvent.A_RELEASE, onAReleased);
                          myWiimote.addEventListener(ButtonEvent.B_PRESS, onBPressed);
                          myWiimote.addEventListener(ButtonEvent.B_RELEASE, onBReleased);
                          myWiimote.addEventListener(ButtonEvent.HOME_PRESS, onHomePressed);
                          myWiimote.addEventListener(ButtonEvent.ONE_PRESS, onOnePressed);
                          myWiimote.addEventListener(ButtonEvent.TWO_PRESS, onTwoPressed);
                  }
                  
                  public function onEnterFrame(event:Event):void {
                          //counter++;
                          //txt.text = counter.toString();
                          
                          
The code for the paint function was ported and adapted from: "Splatter", by Stamen Design http://stamen.com/projects/splatter *

  
                          
                          mid_x = ((end_x - start_x) * (1 + mid_point_push)) + start_x;
                          mid_y = ((end_y - start_y) * (1 + mid_point_push)) + start_y;
                          start_x = end_x;
                          start_y = end_y;
                          end_x = vpX;// = mouseX;
                          end_y = vpY;// = mouseY;
                          distance = Math.sqrt(Math.pow((end_x - start_x), 2) + Math.pow((end_y - start_y), 2));
                          new_size = max_line_width / distance;
                          size = aggression*new_size;//(new_size_influence * new_size) + ((1 - new_size_influence) * size);
                          splat(graphics, start_x, start_y, end_x, end_y, mid_x, mid_y, size);
                          parity = false;
                          /*
                          _parent.new_size_influence += ((Math.random() * 0.1) - 0.05);
                          _parent.mid_point_push += ((Math.random() * 0.1) - 0.05);
                          _parent.max_line_width += ((Math.random() * 4) - 2);
  			*/
                          new_size_influence += ((Math.random() * 0.1) - 0.05);
                          mid_point_push += ((Math.random() * 0.1) - 0.05);
                          max_line_width += ((Math.random() * 4) - 2);
                  }
                  private function splat(obj:Graphics, x1:Number, y1:Number, x2:Number, y2:Number, x3:Number, y3:Number, d:Number):void        {
                          obj.lineStyle(d, color, 100);
                          //txt.text = d.toString();
                          obj.moveTo(x1, y1);
                          obj.curveTo(x3, y3, x2, y2);
                          //curves.push([x1, y1, x3, y3, x2, y2, d]);
                          
                          // splotch
                          var dd:Number = Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));
                  
                           for (var i:uint = 0; i<Math.floor(5*Math.pow(Math.random(), 4)); i++) {
                          // positioning of splotch varies between ±4dd, tending towards 0
                                  var splat_range:Number = 1;
                                  var x4:Number = dd * 1 * (Math.pow(Math.random(), splat_range) - (splat_range/2));
                                  var y4:Number = dd * 1 * (Math.pow(Math.random(), splat_range) - (splat_range/2));
                                  // direction of splotch varies between ±0.5
                                  var x5:Number = Math.random() - 0.5;
                                  var y5:Number = Math.random() - 0.5;
                                  var d_:Number = d*(0.5+Math.random());
                                  obj.lineStyle(d_, color, 100);
                                  obj.moveTo((x1+x4), (y1+y4));
                                  obj.lineTo((x1+x4+x5), (y1+y4+y5));
                                  //curves.push([(x1+x4), (y1+y4), (x1+x4+x5), (y1+y4+y5), (x1+x4+x5), (y1+y4+y5), d_]);
                          
                           }
                          
                  }
                  private function onMouseClick(event:MouseEvent):void{
                          color = Math.random() * 0xFFFFFF;
                          //txt.text = "Color: 0x" + color.toString();
                  }
                  
                  private function onUpdated ( pEvt:WiimoteEvent ):void 
                  {
                          if (myWiimote.ir.p1==true) {// && myWiimote.ir.p2==true) {
                                   //statusText.text = "Status:" + ((1-myWiimote.ir.x1) * stage.stageWidth) + (myWiimote.ir.y1 * stage.stageHeight);
                                   cursor.x = cursorX = (1-myWiimote.ir.x1) * stage.stageWidth;
                                  cursor.y = cursorY = myWiimote.ir.y1 * stage.stageHeight;
  
                                  /*//force feedback
                                  if (cursorX<10 || cursorX>stage.stageWidth-10) {
                                          myWiimote.rumbleTimeout = 300;        
                                  }
                                  if (cursorY<10 || cursorY>stage.stageHeight-10) {
                                          myWiimote.rumbleTimeout = 300;        
                                  }*/
                          }
                                                  
                          if (myWiimote.b) {
                                  vpX = cursorX;
                                  vpY = cursorY;
                          }
                          
                          /*//zooming
                          if (myWiimote.minus) {
                                  fl += 1;
                          } else if (myWiimote.home) {
                                  fl = limit;
                          } else if (myWiimote.plus) {
                                  fl -= 1;
                          }*/
                  }
                  
                  private function onAPressed ( pEvt:ButtonEvent ):void
                  {        
                          color = Math.random() * 0xFFFFFF;
                  }
                  
                  private function onAReleased ( pEvt:ButtonEvent ):void
                  {        
                          //cursor.visible = false;
                  }
                  
                  private function onBPressed ( pEvt:ButtonEvent ):void
                  {        
                          cursor.visible = false;
                          if (myWiimote.ir.p1) {
                                  start_x = mid_x = end_x = cursor.x = cursorX = (1-myWiimote.ir.x1) * stage.stageWidth;
                                  start_y = mid_y = end_y = cursor.y = cursorY = myWiimote.ir.y1 * stage.stageHeight;
                          } else {
                                  start_x = mid_x = end_x = stage.stageWidth / 2;
                                  start_y = mid_y = end_y = stage.stageHeight / 2;
                          }
  
                  }
                  
                  private function onBReleased ( pEvt:ButtonEvent ):void
                  {        
                          cursor.visible = cursorVisible;
                  }
                  
                  private function onHomePressed ( pEvt:ButtonEvent ):void
                  {        
                          //saveImage();
                  }
                  
                  private function onOnePressed ( pEvt:ButtonEvent ):void
                  {        
                          graphics.clear();
                  }
                  
                  private function onTwoPressed ( pEvt:ButtonEvent ):void
                  {        
                          cursor.visible = txt.visible = cursorVisible = !cursor.visible;
                  }
                  
                     private function saveImage():void {
                             /*var jpgSource:BitmapData = new BitmapData (this.width, this.height);
                          jpgSource.draw(this);
                          var jpgEncoder:JPGEncoder = new JPGEncoder(85);
                          var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);
  			*/
                          //var jpgEncoded:String = Base64.encodeByteArray(jpgStream);
                          
                          /*var urlVars:URLVariables = new URLVariables();
                          urlVars.test = "test";
                          urlVars.jpgEncoded = jpgEncoded;
                          
                          txt.text = jpgEncoded;
                          var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
                          var jpgURLRequest:URLRequest = new URLRequest("http://localhost/actionpaintsave.php");
                          jpgURLRequest.requestHeaders.push(header);
                          jpgURLRequest.method = URLRequestMethod.POST;
                          jpgURLRequest.data = urlVars;
                          //sendToURL(jpgURLRequest);
                          navigateToURL(jpgURLRequest, "_blank");*/
                          
                                  /*//Get a reference to the desktop
                                  var desktop:File = File.desktopDirectory;
                                  
                                  //create a reference to the file on the desktop in which we will save html
                                  var saveFile:File = desktop.resolve("test.xml");
                                  
                                  //create a FileStream instance to write to the file
                                  var fs:FileStream = new FileStream();
                                  
                                  //open file in WRITE mode
                                  fs.open(saveFile, FileMode.WRITE);
                                  
                                  //write the string to the file
                                  fs.writeUTFBytes(XML(data.users) + " ..<");
                                  
                                  //close the file / file stream
                                  fs.close();*/
                          
                          //var httpRequest:HTTP
                          
                          /*//var jpgByteArray:ByteArray = encodeToJPEG(myBitmapData);
                          // base-64 encoded jpeg data in string
                          var jpgEncoded:String = Base64.encodeByteArray(jpgStream); 
                  
                          //
                          //file uploadef php
                          var urlRequest:URLRequest = new URLRequest("http://localhost/actionpaintsave.php?name=sketch.jpg");
                          urlRequest.method = URLRequestMethod.POST;
                          //var urlLoader:URLLoader = new URLLoader();
                          var urlVariables:URLVariables = new URLVariables();
                          urlVariables.file = jpgEncoded;
                          urlRequest.data = urlVariables;
                          //urlLoader.data = urlVariables;
                          //urlLoader.load(urlRequest);
                          navigateToURL(urlRequest, "_blank");*/
                  
  
                     }
                     
          }
  }
  


(C) Æliens 27/08/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.