/* Copyright (c) 2007 Joa Ebert and Thibault Imbert Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package org.wiiflash { import flash.events.Event; import flash.events.EventDispatcher; import flash.events.IEventDispatcher; import flash.events.IOErrorEvent; import flash.geom.Point; import flash.utils.ByteArray; import org.wiiflash.events.*; /** * Dispatched when a Wiimote object has successfully connected to the WiiFlash server. * * @eventType flash.events.Event.CONNECT */ [Event(name='connect', type='flash.events.Event')] /** * Dispatched when a Wiimote object could not establish a connection. * * @eventType flash.events.IOErrorEvent.IO_ERROR */ [Event(name='ioError', type='flash.events.IOErrorEvent')] /** * Dispatched when Wiimote data has been updated. * * @eventType org.wiiflash.events.WiimoteEvent.UPDATE */ [Event(name='update', type='org.wiiflash.events.WiimoteEvent')] /** * Dispatched when Nunchuk has been connected to Wiimote. * * @eventType org.wiiflash.events.WiimoteEvent.NUNCHUK_CONNECT */ [Event(name='nunchukConnect', type='org.wiiflash.events.WiimoteEvent')] /** * Dispatched when Nunchuk has been disconnected from Wiimote. * * @eventType org.wiiflash.events.WiimoteEvent.NUNCHUK_DISCONNECT */ [Event(name='nunchukDisconnect', type='org.wiiflash.events.WiimoteEvent')] /** * Dispatched when Point 1 of the IR sensor bar has been found. * * @eventType org.wiiflash.events.WiimoteEvent.IR1_FOUND */ [Event(name='ir1Found', type='org.wiiflash.events.WiimoteEvent')] /** * Dispatched when Point 2 of the IR sensor bar has been found. * * @eventType org.wiiflash.events.WiimoteEvent.IR2_FOUND */ [Event(name='ir2Found', type='org.wiiflash.events.WiimoteEvent')] /** * Dispatched when Point 1 of the IR sensor bar has been lost. * * @eventType org.wiiflash.events.WiimoteEvent.IR1_LOST */ [Event(name='ir1Lost', type='org.wiiflash.events.WiimoteEvent')] /** * Dispatched when Point 2 of the IR sensor bar has been lost. * * @eventType org.wiiflash.events.WiimoteEvent.IR2_LOST */ [Event(name='ir2Lost', type='org.wiiflash.events.WiimoteEvent')] //----------------------------------------------------------------------------------- // Press events //----------------------------------------------------------------------------------- /** * Dispatched when button 1 has been pressed. * * @eventType org.wiiflash.events.ButtonEvent.ONE_PRESS */ [Event(name='onePress', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when button 2 has been pressed. * * @eventType org.wiiflash.events.ButtonEvent.TWO_PRESS */ [Event(name='twoPress', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when button A has been pressed. * * @eventType org.wiiflash.events.ButtonEvent.A_PRESS */ [Event(name='aPress', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when button B has been pressed. * * @eventType org.wiiflash.events.ButtonEvent.B_PRESS */ [Event(name='bPress', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when button + has been pressed. * * @eventType org.wiiflash.events.ButtonEvent.PLUS_PRESS */ [Event(name='plusPress', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when button - has been pressed. * * @eventType org.wiiflash.events.ButtonEvent.MINUS_PRESS */ [Event(name='minusPress', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when button Home has been pressed. * * @eventType org.wiiflash.events.ButtonEvent.HOME_PRESS */ [Event(name='homePress', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when button Up has been pressed. * * @eventType org.wiiflash.events.ButtonEvent.UP_PRESS */ [Event(name='upPress', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when button Down has been pressed. * * @eventType org.wiiflash.events.ButtonEvent.DOWN_PRESS */ [Event(name='downPress', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when button Left has been pressed. * * @eventType org.wiiflash.events.ButtonEvent.LEFT_PRESS */ [Event(name='leftPress', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when button Right has been pressed. * * @eventType org.wiiflash.events.ButtonEvent.RIGHT_PRESS */ [Event(name='rightPress', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when Nunchuk button C has been pressed. * * @eventType org.wiiflash.events.ButtonEvent.C_PRESS */ [Event(name='cPress', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when Nunchuk button Z has been pressed. * * @eventType org.wiiflash.events.ButtonEvent.Z_PRESS */ [Event(name='zPress', type='org.wiiflash.events.ButtonEvent')] //----------------------------------------------------------------------------------- // Release events //----------------------------------------------------------------------------------- /** * Dispatched when button 1 has been released. * * @eventType org.wiiflash.events.ButtonEvent.ONE_RELEASE */ [Event(name='oneRelease', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when button 2 has been released. * * @eventType org.wiiflash.events.ButtonEvent.TWO_RELEASE */ [Event(name='twoRelease', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when button A has been released. * * @eventType org.wiiflash.events.ButtonEvent.A_RELEASE */ [Event(name='aRelease', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when button B has been released. * * @eventType org.wiiflash.events.ButtonEvent.B_RELEASE */ [Event(name='bRelease', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when button + has been released. * * @eventType org.wiiflash.events.ButtonEvent.PLUS_RELEASE */ [Event(name='plusRelease', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when button - has been released. * * @eventType org.wiiflash.events.ButtonEvent.MINUS_RELEASE */ [Event(name='minusRelease', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when button Home has been released. * * @eventType org.wiiflash.events.ButtonEvent.HOME_RELEASE */ [Event(name='homeRelease', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when button Up has been released. * * @eventType org.wiiflash.events.ButtonEvent.UP_RELEASE */ [Event(name='upRelease', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when button Down has been released. * * @eventType org.wiiflash.events.ButtonEvent.DOWN_RELEASE */ [Event(name='downRelease', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when button Left has been released. * * @eventType org.wiiflash.events.ButtonEvent.LEFT_RELEASE */ [Event(name='leftRelease', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when button Right has been released. * * @eventType org.wiiflash.events.ButtonEvent.RIGHT_RELEASE */ [Event(name='rightRelease', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when Nunchuk button C has been released. * * @eventType org.wiiflash.events.ButtonEvent.C_RELEASE */ [Event(name='cRelease', type='org.wiiflash.events.ButtonEvent')] /** * Dispatched when Nunchuk button Z has been released. * * @eventType org.wiiflash.events.ButtonEvent.Z_RELEASE */ [Event(name='zRelease', type='org.wiiflash.events.ButtonEvent')] //----------------------------------------------------------------------------------- /** * The Wiimote class represents a Wiimote. * * A Wiimote object has to be connected to the WiiFlash server. The WiiFlash server * gathers available information from a Wiimote through Bluetooth and sends it back. * *
It is important to remember the following information regarding the motion sensors: *
* * var wiimote: Wiimote = new Wiimote(); * wiimote.addEventListener( Event.CONNECT, onWiimoteConnect ); * wiimote.connect(); **
* * wiimote.addEventListener( ButtonEvent.A_PRESS, onWiimoteAPress ); * wiimote.addEventListener( ButtonEvent.A_RELEASE, onWiimoteARelease ); **
wiimote.rumble = true;
wiimote.rumble = false;
wiimote.mouseControl = true;
wiimote.mouseControl = false;
wiimote.rumbleTimeout = 1000;
var battery:Number = wiimote.batteryLevel;
wiimote.leds = Wiimote.LED1 | Wiimote.LED4;
A sensor measures only acceleration. The default acceleration a sensor can measure * is the gravity vector that is pointing downwards. This has no affect to the yaw angle * and is the reason whil there wont be much changes in value. Using an IR sensor bar * can solve this issue.
*/ public function get yaw(): Number { return calcAngle( sensorZ ); } /** * @private */ internal static function calcAngle( value: Number ): Number { var clamp: Number = value; if ( clamp > 1 ) clamp = 1; if ( clamp < -1 ) clamp = -1; return Math.asin( clamp ); } //----------------------------------------------------------------------------------- // Parsing //----------------------------------------------------------------------------------- internal function update( pack: ByteArray ): void { _batteryLevel = pack.readUnsignedByte() / 0xC8; var buttonState: int = pack.readUnsignedShort(); var button: Button; for ( var i: int = 0; i < I11; i++ ) { button = buttons[ i ]; button.update( buttonState ); if ( button.state && !button.lastState ) { eventDispatcher.dispatchEvent( new ButtonEvent( ButtonType.getEventFromType( button.type, true ), true ) ); } else if ( !button.state && button.lastState ) { eventDispatcher.dispatchEvent( new ButtonEvent( ButtonType.getEventFromType( button.type, false ), false ) ); } } _x = pack.readFloat(); _y = pack.readFloat(); _z = pack.readFloat(); _extensionType = pack.readUnsignedByte(); _hasNunchuk = _extensionType == 1; _hasClassicController = _extensionType == 2; if ( _hasClassicController && !_hadClassicController ) { eventDispatcher.dispatchEvent( new WiimoteEvent( WiimoteEvent.CONTROLLER_CONNECT ) ); } else if ( !_hasClassicController && _hadClassicController ) { eventDispatcher.dispatchEvent( new WiimoteEvent( WiimoteEvent.CONTROLLER_DISCONNECT ) ); } if ( _hasNunchuk && !_hadNunchuk ) { eventDispatcher.dispatchEvent( new WiimoteEvent( WiimoteEvent.NUNCHUK_CONNECT ) ); } else if ( !_hasNunchuk && _hadNunchuk ) { eventDispatcher.dispatchEvent( new WiimoteEvent( WiimoteEvent.NUNCHUK_DISCONNECT ) ); } _hadNunchuk = _hasNunchuk; _hadClassicController = _hasClassicController; if ( _hasNunchuk ) { _nunchuk.update( pack ); } else if ( _hasClassicController ) { _classicController.update( pack ); } else { pack.position = 37; } ir.update( pack ); eventDispatcher.dispatchEvent( new WiimoteEvent( WiimoteEvent.UPDATE ) ); } /** * Dispatched when the Wiimote is successfully connected to the WiiFlash Server. * * @eventType flash.events.Event.CONNECT */ private function onConnect( event:Event ): void { eventDispatcher.dispatchEvent( event ); } /** * Dispatched when the Wiimote is disconnected from to the WiiFlash Server. * * @eventType flash.events.Event.CLOSE */ private function onError( event:Event ): void { eventDispatcher.dispatchEvent( event ); } //----------------------------------------------------------------------------------- // IEventDispatcher //----------------------------------------------------------------------------------- /** * Registers an event listener object with a Wiimote object so that the listener receives notification of an event. * * @param type The type of event. * @param listener The listener function that processes the event. * @param useCapture Determines whether the listener works in the capture phase or the target and bubbling phases. * @param priority The priority level of the event listener. * @param useWeakReference Determines whether the reference to the listener is strong or weak. * * @see http://livedocs.adobe.com/flex/2/langref/flash/events/IEventDispatcher.html#addEventListener() flash.events.IEventDispatcher.addEventListener() */ public function addEventListener( type: String, listener: Function, useCapture: Boolean = false, priority: int = 0, useWeakReference: Boolean = false ): void { eventDispatcher.addEventListener( type, listener, useCapture, priority, useWeakReference ); } /** * Dispatches an event into the event flow. * * @param event The Event object dispatched into the event flow. * * @see http://livedocs.adobe.com/flex/2/langref/flash/events/IEventDispatcher.html#dispatchEvent() flash.events.IEventDispatcher.dispatchEvent() */ public function dispatchEvent( event: Event ): Boolean { return eventDispatcher.dispatchEvent( event ); } /** * Checks whether the Wiimote object has any listeners registered for a specific type of event. * * @param type The type of event. * @return A value oftrue
if a listener of the specified type is registered; false
otherwise.
*
* @see http://livedocs.adobe.com/flex/2/langref/flash/events/IEventDispatcher.html#hasEventListener() flash.events.IEventDispatcher.hasEventListener()
*/
public function hasEventListener( type: String ): Boolean
{
return eventDispatcher.hasEventListener( type );
}
/**
* Removes a listener from the Wiimote object.
*
* @param type The type of event.
* @param listener The listener object to remove.
* @param useCapture Specifies whether the listener was registered for the capture phase or the target and bubbling phases.
*
* @see http://livedocs.adobe.com/flex/2/langref/flash/events/IEventDispatcher.html#removeEventListener() flash.events.IEventDispatcher.removeEventListener()
*/
public function removeEventListener( type: String, listener: Function, useCapture: Boolean = false ): void
{
eventDispatcher.removeEventListener( type, listener, useCapture );
}
/**
* Checks whether an event listener is registered with this Wiimote object or any of its ancestors for the specified event type.
*
* @param type The type of event.
* @return A value of true
if a listener of the specified type will be triggered; false
otherwise.
*
* @see http://livedocs.adobe.com/flex/2/langref/flash/events/IEventDispatcher.html#willTrigger() flash.events.IEventDispatcher.willTrigger()
*/
public function willTrigger( type: String ): Boolean
{
return eventDispatcher.willTrigger( type );
}
}
}