topical media & game development
actionscript-phidget-flex-PhidgetManagerExample.mx
actionscript-phidget-flex-PhidgetManagerExample.mx
[swf]
flex
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="370" height="312">
<mx:Script>
<![CDATA[
import com.phidgets.Phidget;
import com.phidgets.PhidgetManager;
import com.phidgets.events.*;
import mx.collections.*;
public var phid:com.phidgets.PhidgetManager;
[Bindable]
public var phidgetsList:ArrayCollection;
private var phidgetsArray:Array;
public function connect():void{
phid = new PhidgetManager();
phidgetsArray = new Array();
phid.addEventListener(PhidgetManagerEvent.CONNECT, onConnect);
phid.addEventListener(PhidgetManagerEvent.DETACH, onDetach);
phid.addEventListener(PhidgetManagerEvent.ATTACH, onAttach);
phid.addEventListener(PhidgetManagerEvent.DISCONNECT, onDisconnect);
phid.open(addressText.text, int(portText.text), passwordText.text);
}
private function onAttach(evt:PhidgetManagerEvent):void{
phidgetsArray[phidgetsArray.length] = {name:evt.Device.Name,
version:evt.Device.Version, serial:evt.Device.serialNumber};
phidgetsList = new ArrayCollection(phidgetsArray);
}
private function onDetach(evt:PhidgetManagerEvent):void{
var i:int;
var j:int = 0;
var newArray:Array = new Array();
for(i=0;i<phidgetsArray.length;i++){
if(phidgetsArray[i] != null)
{
if(phidgetsArray[i]['name'] == evt.Device.Name &&
phidgetsArray[i]['serial'] == evt.Device.serialNumber)
phidgetsArray[i] = null;
else
newArray[j++] = phidgetsArray[i];
}
}
phidgetsArray = newArray;
phidgetsList = new ArrayCollection(phidgetsArray);
}
private function onDisconnect(evt:PhidgetManagerEvent):void{
connectedText.text = "Disconnected";
}
private function onConnect(evt:PhidgetManagerEvent):void{
connectedText.text = "Connected";
}
]]>
</mx:Script>
<mx:Button x="240" y="279" label="Connect" click="connect()"/>
<mx:TextInput x="105" y="251" id="addressText" text="localhost" width="107"/>
<mx:TextInput x="260" y="251" id="portText" width="60" text="5001"/>
<mx:TextInput x="125" y="279" id="passwordText" width="107"/>
<mx:Text x="43" y="253" text="Address:"/>
<mx:Text x="220" y="253" text="Port:"/>
<mx:Text x="56" y="281" text="Password:"/>
<mx:Text x="10" y="10" text="Server Status:"/>
<mx:Text x="124" y="10" text="Unknown" id="connectedText"/>
<mx:DataGrid x="10" y="36" height="207" id="managerList" dataProvider="{phidgetsList}" width="350">
<mx:columns>
<mx:DataGridColumn headerText="Name" dataField="name"/>
<mx:DataGridColumn headerText="Version" dataField="version" width="55"/>
<mx:DataGridColumn headerText="Serial Number" dataField="serial" width="95"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>
(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.