package com.freddufau.wii { import com.senocular.ui.VirtualMouse; import flash.display.DisplayObject; import flash.display.MovieClip; import flash.display.Stage; import org.wiiflash.Wiimote; import flash.events.Event; import flash.events.MouseEvent; import flash.events.IOErrorEvent; import org.wiiflash.events.ButtonEvent; import org.wiiflash.events.WiimoteEvent; import com.carlcalderon.arthropod.Debug; /** * ... * @author Fred DUFAU 2008 */ public class WiiMouse extends MovieClip { private var _virtualMouse:VirtualMouse private var _wiimote:Wiimote; private var _status:String; private var _pointer:MovieClip; private var pointX:Number; private var pointY:Number; private var _buttonEvents:Array = [ ButtonEvent.A_PRESS, ButtonEvent.A_RELEASE, ButtonEvent.B_PRESS, ButtonEvent.B_RELEASE, ButtonEvent.UP_PRESS, ButtonEvent.UP_RELEASE, ButtonEvent.DOWN_PRESS, ButtonEvent.DOWN_RELEASE, ButtonEvent.LEFT_PRESS, ButtonEvent.LEFT_RELEASE, ButtonEvent.RIGHT_PRESS, ButtonEvent.RIGHT_RELEASE, ButtonEvent.HOME_RELEASE, ButtonEvent.MINUS_PRESS, ButtonEvent.MINUS_RELEASE, ButtonEvent.PLUS_PRESS, ButtonEvent.PLUS_RELEASE, ButtonEvent.ONE_PRESS, ButtonEvent.ONE_RELEASE, ButtonEvent.TWO_PRESS, ButtonEvent.TWO_RELEASE ]; public function WiiMouse( pWiimoteInstance:Wiimote, pPointer:MovieClip ) { _wiimote = pWiimoteInstance; _pointer = pPointer; addEventListener( Event.ADDED_TO_STAGE, setup ); } private function setup(pEvt:Event):void { removeEventListener( Event.ADDED_TO_STAGE, setup ); this.stage.addEventListener( Event.ADDED, onAddedObjectToStage ); _pointer.mouseEnabled = false; _pointer.mouseChildren = false; this.stage.addChild( _pointer ); _virtualMouse = new VirtualMouse(this.stage, _pointer.x, _pointer.y); _virtualMouse.addEventListener(MouseEvent.MOUSE_MOVE, OnMouse_MOVE); _virtualMouse.ignore( _pointer ); initWiimote(); } /* * onAddedObjectToStage : * pour etre sur que le pointer sera au dessus de tout le monde, notamment les composants * genre combobox qui attache leur liste deroulante sur le stage * */ private function onAddedObjectToStage( pEvt:Event ):void { this.stage.addChild( _pointer ); } override public function set visible( pFlag:Boolean ):void { _pointer.visible = pFlag; } override public function get visible():Boolean { return _pointer.visible; } public function get pointer():MovieClip { return _pointer; } private function OnMouse_MOVE(evt:MouseEvent):void { _pointer.x = _virtualMouse.x; _pointer.y = _virtualMouse.y; } private function initWiimote():void { _wiimote.addEventListener( WiimoteEvent.UPDATE, onUpdated ); addMouseEvent(); } private function addMouseEvent():void { for (var i:int = 0; i < _buttonEvents.length; i++) { var eventType:String = _buttonEvents[i]; _wiimote.addEventListener(eventType, onWiiButtonRelease); } } // hum hum peut etre un héritage de virtual mouse à voir override public function get x():Number { return _virtualMouse.x; } override public function get y():Number { return _virtualMouse.y; } private function onUpdated(evt:Event):void { pointX = 1 - _wiimote.ir["x" + (_wiimote.id + 1)]; pointY = _wiimote.ir["y" + (_wiimote.id + 1)]; if( ! isNaN( pointY ) && ! isNaN( pointX ) ) { _virtualMouse.setLocation( pointX * (this.stage.stageWidth), pointY * (this.stage.stageHeight) ) ; } _pointer.rotation = ( _wiimote.roll * 180 / Math.PI ) * 2; //Debug.log( _wiimote.sensorX ); // on pourrait envoyer la vitesse des sensors pour calibrer les shapes... // !!!!!!!!!!!!!!!!!!!! } private function onWiiButtonRelease(evt:ButtonEvent):void { switch(evt.type) { case ButtonEvent.A_PRESS: _virtualMouse.press(); break; case ButtonEvent.A_RELEASE: _virtualMouse.release(); break; default: dispatchEvent( evt ); break; } } } }