mde
|
1361
|
1
|
/*
|
|
2
|
Copyright 2009, Matthew Eernisse (mde@fleegix.org) and Slide, Inc.
|
|
3
|
|
|
4
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
you may not use this file except in compliance with the License.
|
|
6
|
You may obtain a copy of the License at
|
|
7
|
|
|
8
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
|
|
10
|
Unless required by applicable law or agreed to in writing, software
|
|
11
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
See the License for the specific language governing permissions and
|
|
14
|
limitations under the License.
|
|
15
|
*/
|
|
16
|
|
|
17
|
package org.windmill {
|
|
18
|
import org.windmill.events.*;
|
|
19
|
import org.windmill.Locator;
|
|
20
|
import flash.events.*
|
|
21
|
import mx.events.*
|
mde
|
1367
|
22
|
import flash.utils.*;
|
mde
|
1361
|
23
|
import flash.external.ExternalInterface;
|
|
24
|
|
|
25
|
public class Controller {
|
|
26
|
private static function _log(msg:*):void {
|
|
27
|
ExternalInterface.call("logger", msg);
|
|
28
|
}
|
mde
|
1367
|
29
|
|
mde
|
1361
|
30
|
public function Controller():void {}
|
|
31
|
|
|
32
|
public static function click(params:Object):void {
|
|
33
|
var obj:* = Locator.lookupDisplayObject(params);
|
|
34
|
// Give it focus
|
|
35
|
Events.triggerFocusEvent(obj, FocusEvent.FOCUS_IN);
|
|
36
|
// Down, (TextEvent.LINK,) up, click
|
|
37
|
Events.triggerMouseEvent(obj, MouseEvent.MOUSE_DOWN, {
|
|
38
|
buttonDown: true });
|
|
39
|
// If this is a link, do the TextEvent hokey-pokey
|
|
40
|
// All events fire on the containing DisplayObject
|
|
41
|
if ('link' in params) {
|
|
42
|
var link:String = Locator.locateLinkHref(params.link,
|
|
43
|
obj.htmlText);
|
|
44
|
Events.triggerTextEvent(obj, TextEvent.LINK, {
|
|
45
|
text: link });
|
|
46
|
}
|
|
47
|
Events.triggerMouseEvent(obj, MouseEvent.MOUSE_UP);
|
|
48
|
Events.triggerMouseEvent(obj, MouseEvent.CLICK);
|
|
49
|
}
|
mde
|
1367
|
50
|
|
mde
|
1361
|
51
|
// Click alias functions
|
|
52
|
public static function check(params:Object):void {
|
|
53
|
return Controller.click(params);
|
|
54
|
}
|
|
55
|
public static function radio(params:Object):void {
|
|
56
|
return Controller.click(params);
|
|
57
|
}
|
|
58
|
|
mde
|
1367
|
59
|
public static function dragToCoords(params:Object):void {
|
|
60
|
Controller._log('started ...');
|
|
61
|
var obj:* = Locator.lookupDisplayObject(params);
|
|
62
|
var startCoords:Array = [obj.x, obj.y];
|
|
63
|
var endCoords:Array = Controller.parseCoords(params.coords);
|
|
64
|
// Move mouse over to the dragged obj
|
|
65
|
Events.triggerMouseEvent(obj.stage, MouseEvent.MOUSE_MOVE, {
|
|
66
|
stageX: startCoords[0],
|
|
67
|
stageY: startCoords[1]
|
|
68
|
});
|
|
69
|
// Give it focus
|
|
70
|
Events.triggerFocusEvent(obj, FocusEvent.FOCUS_IN);
|
|
71
|
// Down, (TextEvent.LINK,) up, click
|
|
72
|
Events.triggerMouseEvent(obj, MouseEvent.MOUSE_DOWN, {
|
|
73
|
buttonDown: true });
|
|
74
|
// Number of steps will be number of pixels in shorter delta
|
|
75
|
var deltaX:int = endCoords[0] - startCoords[0];
|
|
76
|
var deltaY:int = endCoords[1] - startCoords[1];
|
|
77
|
var stepCount:int = 10; // Just pick an arbitrary number of steps
|
|
78
|
// Number of pixels per step
|
|
79
|
var incrX:Number = deltaX / stepCount;
|
|
80
|
var incrY:Number = deltaY / stepCount;
|
|
81
|
// Current pos as the move happens
|
|
82
|
var currX:Number = startCoords[0];
|
|
83
|
var currY:Number = startCoords[1];
|
|
84
|
// Step number
|
|
85
|
var currStep:int = 0;
|
|
86
|
// Use a delay so we can see the move
|
|
87
|
var stepTimer:Timer = new Timer(5);
|
|
88
|
// Step function -- reposition per step
|
|
89
|
var doStep:Function = function ():void {
|
|
90
|
if (currStep <= stepCount) {
|
|
91
|
Events.triggerMouseEvent(obj.stage, MouseEvent.MOUSE_MOVE, {
|
|
92
|
stageX: currX,
|
|
93
|
stageY: currY
|
|
94
|
});
|
|
95
|
currX += incrX;
|
|
96
|
currY += incrY;
|
|
97
|
currStep++;
|
|
98
|
}
|
|
99
|
// Once it's finished, stop the timer and trigger
|
|
100
|
// the final mouse events
|
|
101
|
else {
|
|
102
|
stepTimer.stop();
|
|
103
|
Events.triggerMouseEvent(obj, MouseEvent.MOUSE_UP);
|
|
104
|
Events.triggerMouseEvent(obj, MouseEvent.CLICK);
|
|
105
|
}
|
|
106
|
}
|
|
107
|
// Start the timer loop
|
|
108
|
stepTimer.addEventListener(TimerEvent.TIMER, doStep);
|
|
109
|
stepTimer.start();
|
|
110
|
}
|
|
111
|
|
|
112
|
// Ensure coords are in the right format and are numbers
|
|
113
|
private static function parseCoords(coordsStr:String):Array {
|
|
114
|
var coords:Array = coordsStr.replace(
|
|
115
|
/\(|\)| /g, '').split(',');
|
|
116
|
if (isNaN(coords[0]) || isNaN(coords[1])) {
|
|
117
|
throw new Error('Coordinates must be in format "(x, y)"');
|
|
118
|
}
|
|
119
|
else {
|
|
120
|
coords[0] = parseInt(coords[0], 10);
|
|
121
|
coords[1] = parseInt(coords[1], 10);
|
|
122
|
}
|
|
123
|
return coords;
|
|
124
|
}
|
|
125
|
|
mde
|
1361
|
126
|
public static function doubleClick(params:Object):void {
|
|
127
|
var obj:* = Locator.lookupDisplayObject(params);
|
|
128
|
// Give it focus
|
|
129
|
Events.triggerFocusEvent(obj, FocusEvent.FOCUS_IN);
|
|
130
|
// First click
|
|
131
|
// Down, (TextEvent.LINK,) up, click
|
|
132
|
Events.triggerMouseEvent(obj, MouseEvent.MOUSE_DOWN, {
|
|
133
|
buttonDown: true });
|
|
134
|
// If this is a link, do the TextEvent hokey-pokey
|
|
135
|
// All events fire on the containing DisplayObject
|
|
136
|
if ('link' in params) {
|
|
137
|
var link:String = Locator.locateLinkHref(params.link,
|
|
138
|
obj.htmlText);
|
|
139
|
Events.triggerTextEvent(obj, TextEvent.LINK, {
|
|
140
|
text: link });
|
|
141
|
}
|
|
142
|
Events.triggerMouseEvent(obj, MouseEvent.MOUSE_UP);
|
|
143
|
Events.triggerMouseEvent(obj, MouseEvent.CLICK);
|
|
144
|
// Second click
|
|
145
|
// Down, (TextEvent.LINK,) up, double click
|
|
146
|
Events.triggerMouseEvent(obj, MouseEvent.MOUSE_DOWN, {
|
|
147
|
buttonDown: true });
|
|
148
|
// TextEvent hokey-pokey, reprise
|
|
149
|
if ('link' in params) {
|
|
150
|
Events.triggerTextEvent(obj, TextEvent.LINK, {
|
|
151
|
text: link });
|
|
152
|
}
|
|
153
|
Events.triggerMouseEvent(obj, MouseEvent.MOUSE_UP);
|
|
154
|
Events.triggerMouseEvent(obj, MouseEvent.DOUBLE_CLICK);
|
|
155
|
}
|
|
156
|
|
|
157
|
public static function type(params:Object):void {
|
|
158
|
// Look up the item to write to
|
|
159
|
var obj:* = Locator.lookupDisplayObject(params);
|
|
160
|
// Text to type out
|
|
161
|
var str:String = params.text;
|
|
162
|
// Char
|
|
163
|
var currChar:String;
|
|
164
|
// Char code
|
|
165
|
var currCode:int;
|
|
166
|
|
|
167
|
// Give the item focus
|
|
168
|
Events.triggerFocusEvent(obj, FocusEvent.FOCUS_IN);
|
|
169
|
// Clear out any value it previously had
|
|
170
|
obj.text = '';
|
|
171
|
|
|
172
|
// Write out the string, firing appropriate events as you go
|
|
173
|
for (var i:int = 0; i < str.length; i++) {
|
|
174
|
currChar = str.charAt(i);
|
|
175
|
currCode = str.charCodeAt(i);
|
|
176
|
// FIXME: In reality, capital letters / special chars
|
|
177
|
// would be firing shift key events around these
|
|
178
|
Events.triggerKeyboardEvent(obj, KeyboardEvent.KEY_DOWN, {
|
|
179
|
charCode: currCode });
|
|
180
|
// Append to the value
|
|
181
|
obj.text += str.charAt(i);
|
|
182
|
Events.triggerTextEvent(obj, TextEvent.TEXT_INPUT, {
|
|
183
|
text: currChar });
|
|
184
|
Events.triggerKeyboardEvent(obj, KeyboardEvent.KEY_UP, {
|
|
185
|
charCode: currCode });
|
|
186
|
}
|
|
187
|
}
|
|
188
|
|
|
189
|
public static function select(params:Object):void {
|
|
190
|
// Look up the item to write to
|
|
191
|
var obj:* = Locator.lookupDisplayObject(params);
|
|
192
|
var sel:* = obj.selectedItem;
|
|
193
|
var item:*;
|
|
194
|
// Give the item focus
|
|
195
|
Events.triggerFocusEvent(obj, FocusEvent.FOCUS_IN);
|
|
196
|
// Set by index
|
|
197
|
switch (true) {
|
|
198
|
case ('index' in params):
|
|
199
|
if (obj.selectedIndex != params.index) {
|
|
200
|
Events.triggerListEvent(obj, ListEvent.CHANGE);
|
|
201
|
obj.selectedIndex = params.index;
|
|
202
|
}
|
|
203
|
break;
|
|
204
|
case ('label' in params):
|
|
205
|
case ('text' in params):
|
|
206
|
var targetLabel:String = params.label || params.text;
|
|
207
|
// Can set a custom label field via labelField attr
|
|
208
|
var labelField:String = obj.labelField ?
|
|
209
|
obj.labelField : 'label';
|
|
210
|
if (sel[labelField] != targetLabel) {
|
|
211
|
Events.triggerListEvent(obj, ListEvent.CHANGE);
|
|
212
|
for each (item in obj.dataProvider) {
|
|
213
|
if (item[labelField] == targetLabel) {
|
|
214
|
obj.selectedItem = item;
|
|
215
|
}
|
|
216
|
}
|
|
217
|
}
|
|
218
|
break;
|
|
219
|
case ('data' in params):
|
|
220
|
case ('value' in params):
|
|
221
|
var targetData:String = params.data || params.value;
|
|
222
|
if (sel.data != targetData) {
|
|
223
|
Events.triggerListEvent(obj, ListEvent.CHANGE);
|
|
224
|
for each (item in obj.dataProvider) {
|
|
225
|
if (item.data == targetData) {
|
|
226
|
obj.selectedItem = item;
|
|
227
|
}
|
|
228
|
}
|
|
229
|
}
|
|
230
|
break;
|
|
231
|
default:
|
|
232
|
// Do nothing
|
|
233
|
}
|
|
234
|
}
|
|
235
|
}
|
|
236
|
}
|
|
237
|
|