├── README
├── bin
└── VirtualControllers.swc
├── docs
└── IGNORE
└── src
├── .actionScriptProperties
├── .project
├── .settings
└── com.adobe.flexbuilder.flashbridge.prefs
├── VirtualControllers.fla
└── com
└── flashgen
└── ui
└── controls
├── ArcadeButton.as
└── ThumbStick.as
/README:
--------------------------------------------------------------------------------
1 | ########################################################################
2 | # This file is released under the Creative Commons Attribution-Share
3 | # Alike 3.0 License
4 | #
5 | # All enclosed files are copyright (C) 2011 Mike Jones & FlashGen.Com,
6 | # unless otherwise indicated.
7 | #
8 | # You can redistribute it and/or modify it under the terms of the
9 | # CREATIVE COMMONS ATTRIBUTION-SHARE ALIKE 3.0 License as published by
10 | # the Creative Commons.
11 | #
12 | # This program is distributed in the hope that it will be useful, but
13 | # WITHOUT ANY WARRANTY; without even the implied warranty of
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 | #
16 | # See the Creative Commons Attribution-Share Alike 3.0 License for
17 | # more details.
18 | #
19 | # For any queries not directly related to the CREATIVE COMMONS
20 | # ATTRIBUTION-SHARE ALIKE 3.0 LICENSE please use the following methods
21 | # of contact:
22 | #
23 | # web: http://blog.flashgen.com
24 | # email: fgdevelopment@flashgen.com
25 | #
26 | ########################################################################
27 |
28 | Thanks for downloading this code. I hope it will be useful for you. Before
29 | you open the file please take the time to read this document- it isn't
30 | that long but does contain valuable information.
31 |
32 | Directory Structure
33 | -------------------
34 | The repository is broken down in to three sections:
35 |
36 | src - This contains the source code
37 | bin - A binary SWC file that contains the compiled code in the src folder
38 | docs - Any supporting documents / API information
39 |
40 | Files
41 | -------------------
42 | Within the src folder are a set of nested folders (packages) for the
43 | ActionScript classes used by the controls. There is also a Flash
44 | Professional file called VirtualControllers.fla in the root of the src
45 | folder. This FLA will only open in Flash Professional CS5 or newer.
46 |
47 | To compile and test the controls, open VirtualControllers.fla in Flash
48 | Professional and test the movie. This FLA contains a simple example that
49 | hooks the thumb stick up to a text field to display the output (position)
50 | of the controller.
51 |
52 | Warranty
53 | --------------------
54 | As the Creative Commons license above indicates. This code is provided 'as is'
55 | and without warranty. Use it at your own risk.
56 |
57 | That said, if you do have a question please feel free to drop me an email
58 | at: fgdevelopment@flashgen.com
59 |
60 |
61 |
--------------------------------------------------------------------------------
/bin/VirtualControllers.swc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pixadecimal/Virtual_Controllers/82f8b761077d764c18e26bbcfa99812f42968baa/bin/VirtualControllers.swc
--------------------------------------------------------------------------------
/docs/IGNORE:
--------------------------------------------------------------------------------
1 | Intentionally left empty
--------------------------------------------------------------------------------
/src/.actionScriptProperties:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/src/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | Virtual Controller
4 |
5 |
6 |
7 |
8 | com.adobe.flexbuilder.flashbridge.flashprobuilder
9 |
10 |
11 |
12 |
13 | com.adobe.flexbuilder.flashbridge.flashpronature
14 | com.adobe.flexbuilder.project.actionscriptnature
15 |
16 |
17 |
--------------------------------------------------------------------------------
/src/.settings/com.adobe.flexbuilder.flashbridge.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | flashProFiles/defaultIndex=0
3 | flashProFiles/defaultPath=
4 | flashProFiles/paths=
5 | flashProFiles/flashProID=1303809847515
6 | flashProFiles/asVersion=0
7 | flashProFiles/boundTemplate=${FLASHPRO_USERCONFIG}/ActionScript 3.0/Templates/boundClass_as3.as
8 | flashProFiles/classTemplate=${FLASHPRO_USERCONFIG}/ActionScript 3.0/Templates/standardClass_as3.as
9 | flashProFiles/recentFiles=
10 | flashProFiles/locations=
11 |
--------------------------------------------------------------------------------
/src/VirtualControllers.fla:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pixadecimal/Virtual_Controllers/82f8b761077d764c18e26bbcfa99812f42968baa/src/VirtualControllers.fla
--------------------------------------------------------------------------------
/src/com/flashgen/ui/controls/ArcadeButton.as:
--------------------------------------------------------------------------------
1 | package com.flashgen.ui.controls
2 | {
3 | import flash.display.Sprite;
4 | import flash.ui.Multitouch;
5 | import flash.ui.MultitouchInputMode;
6 | import flash.events.TouchEvent;
7 | import flash.events.KeyboardEvent;
8 | import flash.events.MouseEvent;
9 | import flash.system.Capabilities;
10 | import flash.events.Event;
11 |
12 | public class ArcadeButton extends Sprite
13 | {
14 | public static const BUTTON_A :String = "buttonA";
15 | public static const BUTTON_B :String = "buttonB";
16 |
17 | private var _emulateKeyboard :Boolean = false;
18 | private var _keyCode :uint = 0;
19 | private var _charCode :uint = 0;
20 |
21 | private var _btn :Sprite;
22 | private var _btnA :Sprite;
23 | private var _btnB :Sprite;
24 |
25 | public function ArcadeButton()
26 | {
27 | _btn = boundary;
28 |
29 | _btnA = buttonA;
30 | _btnB = buttonB;
31 |
32 | if(Capabilities.cpuArchitecture == "ARM")
33 | {
34 | Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
35 | _btn.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchDown);
36 | _btn.addEventListener(TouchEvent.TOUCH_END, onTouchUp);
37 | }
38 | else
39 | {
40 | _btn.addEventListener(MouseEvent.MOUSE_DOWN, onTestTouchDown);
41 | _btn.addEventListener(MouseEvent.MOUSE_UP, onTestTouchUp);
42 | }
43 | }
44 |
45 | protected function dispatchKeyboardEvent(eventType:String, event:Event):void
46 | {
47 | if(_emulateKeyboard)
48 | dispatchEvent(new KeyboardEvent(eventType, true, false, _charCode, _keyCode));
49 | else
50 | dispatchEvent(event);
51 | }
52 |
53 | protected function onTouchDown(event:TouchEvent):void
54 | {
55 | dispatchKeyboardEvent(KeyboardEvent.KEY_DOWN, event);
56 | }
57 |
58 | protected function onTouchUp(event:TouchEvent):void
59 | {
60 | dispatchKeyboardEvent(KeyboardEvent.KEY_UP, event);
61 | }
62 |
63 | protected function onTestTouchDown(event:MouseEvent):void
64 | {
65 | dispatchKeyboardEvent(KeyboardEvent.KEY_DOWN, event);
66 | }
67 |
68 | protected function onTestTouchUp(event:MouseEvent):void
69 | {
70 | dispatchKeyboardEvent(KeyboardEvent.KEY_UP, event);
71 | }
72 |
73 | public function set emulateKeyboard(value:Boolean):void
74 | {
75 | _emulateKeyboard = value;
76 | }
77 |
78 | public function get emulateKeyboard():Boolean
79 | {
80 | return _emulateKeyboard;
81 | }
82 |
83 | public function set keyCodeValue(value:uint):void
84 | {
85 | _keyCode = value;
86 | }
87 |
88 | public function get keyCodeValue():uint
89 | {
90 | return _keyCode;
91 | }
92 |
93 | public function set charCodeValue(value:uint):void
94 | {
95 | _charCode = value;
96 | }
97 |
98 | public function get charCodeValue():uint
99 | {
100 | return _charCode;
101 | }
102 |
103 | public function set buttonVersion(value:String):void
104 | {
105 | if(value == BUTTON_A)
106 | {
107 | _btnA.visible = true;
108 | _btnB.visible = false;
109 | }
110 | else
111 | {
112 | _btnA.visible = false;
113 | _btnB.visible = true;
114 | }
115 | }
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/src/com/flashgen/ui/controls/ThumbStick.as:
--------------------------------------------------------------------------------
1 | package com.flashgen.ui.controls
2 | {
3 | import flash.display.Sprite;
4 | import flash.events.TouchEvent;
5 | import flash.system.Capabilities;
6 | import flash.events.MouseEvent;
7 | import flash.events.Event;
8 | import flash.ui.Multitouch;
9 | import flash.ui.MultitouchInputMode;
10 | import flash.events.KeyboardEvent;
11 | import flash.ui.Keyboard;
12 |
13 | public class ThumbStick extends Sprite
14 | {
15 | private var _thumb :Sprite;
16 | private var _surround :Sprite;
17 | private var _boundary :Sprite;
18 |
19 | private var _degrees :Number;
20 | private var _radius :Number = 25;
21 |
22 | private var _primaryKeyCode :int;
23 | private var _secondaryKeyCode :int;
24 |
25 | private var _previousPrimaryKeyCode :int;
26 | private var _previousSecondaryKeyCode :int = 0;
27 |
28 | public function ThumbStick()
29 | {
30 | _thumb = thumb;
31 | _surround = surround;
32 | _boundary = boundary;
33 | _boundary.width = 200;
34 | _boundary.height = 200;
35 | _boundary.alpha = 0;
36 |
37 | if(Capabilities.cpuArchitecture == "ARM")
38 | {
39 | Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
40 | _thumb.addEventListener(TouchEvent.TOUCH_BEGIN, onThumbDown);
41 | _thumb.addEventListener(TouchEvent.TOUCH_END, onThumbUp);
42 | _surround.addEventListener(TouchEvent.TOUCH_END, onThumbUp);
43 | _boundary.addEventListener(TouchEvent.TOUCH_END, onThumbUp);
44 | }
45 | else
46 | {
47 | _thumb.addEventListener(MouseEvent.MOUSE_DOWN, onTestThumbDown);
48 | _thumb.addEventListener(MouseEvent.MOUSE_UP, onTestThumbUp);
49 | _surround.addEventListener(MouseEvent.MOUSE_UP, onTestThumbUp);
50 | _boundary.addEventListener(MouseEvent.MOUSE_UP, onTestThumbUp);
51 | }
52 | }
53 |
54 | protected function initDrag():void
55 | {
56 | _thumb.startDrag();
57 | addEventListener(Event.ENTER_FRAME, onThumbDrag);
58 | }
59 |
60 | protected function resetThumb():void
61 | {
62 | removeEventListener(Event.ENTER_FRAME, onThumbDrag);
63 |
64 | killAllEvents();
65 |
66 | _thumb.stopDrag();
67 | _thumb.x = 0;
68 | _thumb.y = 0;
69 | }
70 |
71 | protected function onThumbDrag(e:Event):void
72 | {
73 | // Store the current x/y of the knob
74 | var _currentX :Number = _thumb.x;
75 | var _currentY :Number = _thumb.y;
76 |
77 | // Store the registration point of the surrounding 'joystick holder'
78 | var _registrationX :Number = _surround.x;
79 | var _registrationY :Number = _surround.y;
80 |
81 | // Subtract the two from each other to get the actual x/y
82 | var _actualX :Number = _currentX - _registrationX;
83 | var _actualY :Number = _currentY - _registrationY;
84 |
85 | // Calculate the degrees for use when creating the zones.
86 | _degrees = Math.round(Math.atan2(_actualY, _actualX) * 180/Math.PI);
87 |
88 | // Calculate the radian value of the knobs current position
89 | var _angle :Number = _degrees * (Math.PI / 180);
90 |
91 | // As we want to lock the orbit of the knob we need to calculate x/y at the maximum distance
92 | var _maxX :Number = Math.round((_radius * Math.cos(_angle)) + _registrationX);
93 | var _maxY :Number = Math.round((_radius * Math.sin(_angle)) + _registrationY);
94 |
95 | // Check to make sure that the value is positive or negative
96 | if(_currentX > 0 && _currentX > _maxX || _currentX < 0 && _currentX < _maxX)
97 | _thumb.x = _maxX;
98 |
99 | if(_currentY > 0 && _currentY > _maxY || _currentY < 0 && _currentY < _maxY)
100 | _thumb.y = _maxY;
101 |
102 | dispatchKeyCombo();
103 | }
104 |
105 | protected function dispatchKeyCombo():void
106 | {
107 | _secondaryKeyCode = 0;
108 |
109 | // Thumb stick position - Right
110 | if(_degrees >= -22 && _degrees <= 22)
111 | {
112 | _primaryKeyCode = Keyboard.RIGHT;
113 | }
114 | // Thumb stick position - Down
115 | else if (_degrees >= 68 && _degrees <= 112)
116 | {
117 | _primaryKeyCode = Keyboard.DOWN;
118 | }
119 | // Thumb stick position - Left
120 | else if((_degrees >= 158 && _degrees <= 180) || (_degrees >= -179 && _degrees <= -158))
121 | {
122 | _primaryKeyCode = Keyboard.LEFT;
123 | }
124 | // Thumb stick position - Up
125 | else if(_degrees >= -112 && _degrees <= -68)
126 | {
127 | _primaryKeyCode = Keyboard.UP;
128 | }
129 | // Thumb stick position - Up/Left
130 | else if(_degrees >= -157 && _degrees <= -113)
131 | {
132 | _primaryKeyCode = Keyboard.UP;
133 | _secondaryKeyCode = Keyboard.LEFT;
134 | }
135 | // Thumb stick position - Down/Left
136 | else if(_degrees <= 157 && _degrees >= 113)
137 | {
138 | _primaryKeyCode = Keyboard.DOWN;
139 | _secondaryKeyCode = Keyboard.LEFT;
140 | }
141 | // Thumb stick position - Up/Right
142 | else if(_degrees >=-67 && _degrees <= -21)
143 | {
144 | _primaryKeyCode = Keyboard.UP;
145 | _secondaryKeyCode = Keyboard.RIGHT;
146 | }
147 | // Thumb stick position - Down/Right
148 | else if(_degrees >= 23 && _degrees <= 67)
149 | {
150 | _primaryKeyCode = Keyboard.DOWN;
151 | _secondaryKeyCode = Keyboard.RIGHT;
152 | }
153 |
154 | if(_primaryKeyCode != _previousPrimaryKeyCode)
155 | {
156 | dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_UP, true, false, 0, _previousPrimaryKeyCode));
157 | _previousPrimaryKeyCode = _primaryKeyCode;
158 | }
159 |
160 | if(_previousSecondaryKeyCode != _secondaryKeyCode)
161 | {
162 | dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_UP, true, false, 0, _previousSecondaryKeyCode));
163 | _previousSecondaryKeyCode = _secondaryKeyCode;
164 | }
165 |
166 | if(_secondaryKeyCode > 0)
167 | dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_DOWN, true, false, 0, _secondaryKeyCode));
168 |
169 | dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_DOWN, true, false, 0, _primaryKeyCode));
170 | }
171 |
172 | protected function killAllEvents():void
173 | {
174 | if(_primaryKeyCode)
175 | dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_UP, true, false, 0, _previousPrimaryKeyCode));
176 |
177 | if(_secondaryKeyCode > 0)
178 | dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_UP, true, false, 0, _previousSecondaryKeyCode));
179 | }
180 |
181 | protected function onThumbDown(e:TouchEvent):void
182 | {
183 | initDrag();
184 | }
185 |
186 | protected function onThumbUp(e:TouchEvent):void
187 | {
188 | resetThumb();
189 | }
190 |
191 |
192 | protected function onTestThumbDown(e:MouseEvent):void
193 | {
194 | initDrag();
195 | }
196 |
197 | protected function onTestThumbUp(e:MouseEvent):void
198 | {
199 | resetThumb();
200 | }
201 | }
202 | }
203 |
--------------------------------------------------------------------------------