My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/**
* ...
* @author Troy Gardner
* @version 0.1
*/

package com.troyworks.ui {
import flash.geom.Rectangle;
import flash.display.Stage;
import flash.system.ApplicationDomain;
import flash.media.Sound;
import flash.display.Sprite;
import flash.display.InteractiveObject;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.MovieClip;

public class UIUtil {
public var clipsToWatch : Array;
private var hidx : Object = new Object();
private var isHidden : Boolean;

public function UIUtil(clipsToEffect : Array) {
this.clipsToWatch = clipsToEffect;
}

public function makeVisible() : void {

var hide2 : Array = clipsToWatch.concat();
var o : Object;
while(hide2.length > 0) {
o = hide2.pop();
if(o != null) {
trace("showing " + o.name);
o.visible = true;
hidx[o.name] = o.visible;
}
}
isHidden = false;
}

///// these are useful for printing, switching from edit to preview mode etc. ///////
public function hide() : void {
if(!isHidden) {
var hide2 : Array = clipsToWatch.concat();
var o : Object;
while(hide2.length > 0) {
o = hide2.pop();
if(o != null) {
trace("hiding " + o.name);
hidx[o.name] = o.visible;
o.visible = false;
}
}
isHidden = true;
}
}

/*********************************************
* Stack based visibility management, as an alternative
* to adding/removing from displayList or frame based management
*
*/
public static function hideExcept(disObjCon : DisplayObjectContainer, except : Array = null, dontProcess : Array = null, remove : Boolean = false) : void {
var i : int = 0;
var n : int = disObjCon.numChildren;
var ii : int = 0;
var nn : int = (except == null) ? 0 : except.length;
var iii : int = 0;
var nnn : int = (dontProcess == null) ? 0 : dontProcess.length;
var dO : DisplayObject;
var ignore : Boolean;
var dontChange : Boolean;
for(;i < n;++i) {
dO = disObjCon.getChildAt(i);
ignore = false;
dontChange = false;
inner:
{
iii = 0;
for(;iii < nnn;++iii) {
//trace("checking to ignore " + dO.name + " " +except[ii].name );

if(dO == dontProcess[iii]) {

// trace("found something to not ignore");
dontChange = true;
break inner;
}
}
ii = 0;
for(;ii < nn;++ii) {
//trace("checking to ignore " + dO.name + " " +except[ii].name );

if(dO == except[ii]) {

// trace("found something to not ignore");
ignore = true;
break inner;
}
}
}
if(dO != null && !dontChange) {

if(!ignore) {
dO.visible = false;
// dO.alpha = .3;
/* if(remove){
disObjCon.removeChild(dO);
}
if(dO is DisplayObjectContainer) {
DisplayObjectContainer(dO).mouseEnabled = false;
DisplayObjectContainer(dO).mouseChildren = false;
DisplayObjectContainer(dO).tabEnabled = false;
}*/
} else {
// dO.alpha = .8;
dO.visible = true;
/* if(dO is DisplayObjectContainer) {
DisplayObjectContainer(dO).mouseEnabled = true;
DisplayObjectContainer(dO).mouseChildren = true;
DisplayObjectContainer(dO).tabEnabled = true;
}*/
}
}
}
}

public function resetVisibility() : void {
isHidden = false;
var hide2 : Array = clipsToWatch.concat();
var o : Object;
while(hide2.length > 0) {
o = hide2.pop();
if(o != null) {
trace("resettinging " + o.name + " " + hidx[o.name]);

o.visible = hidx[o.name];
}
}
}

public function release() : void {
while(clipsToWatch.length > 0) {
clipsToWatch.pop();
}
}

public function moveTo(x : int, y : int) : void {
for(var i : int = 0;i < clipsToWatch.length; i++) {
clipsToWatch[i].x = x;
clipsToWatch[i].y = y;
}
}

/* get a Sound object by the linkageID from the UI.fla */
public static function getSoundByLinkageID(soundLinkageId : String) : Sound {
var cls : Class = ApplicationDomain.currentDomain.getDefinition(soundLinkageId) as Class;
var snd : Sound = new cls() as Sound;
return snd;
}

/* get a DisplayObject object by the linkageID from the UI.fla */
public static function getDisplayObjectByLinkageID(soundLinkageId : String) : DisplayObject {
var cls : Class = ApplicationDomain.currentDomain.getDefinition(soundLinkageId) as Class;
var dO : DisplayObject = new cls() as DisplayObject;;
return dO;
}

/* returns the zero based frame label to be used in conjunctin with addFrameScript */

public static function getNumberOfFrameLabel(mc : MovieClip, lblName : String) : int {
var nm : String;
var fn : int;
for (var i : int = 0;i < mc.currentLabels.length; i++) {
nm = mc.currentLabels[i].name;
fn = mc.currentLabels[i].frame;
if(nm == lblName) {
trace(" " + nm + " " + ( fn - 1));
return fn - 1;
}
}
return -1;
}

public static function addFrameScriptForLabel(mc : MovieClip, lblName : String, fn : Function ) : int {
var frameNum : int = UIUtil.getNumberOfFrameLabel(mc, lblName);
mc.addFrameScript(frameNum, fn);
return frameNum;
}

public static function setInputShieldBehavior(sprite : Sprite, ON : Boolean = true) : void {
if(ON) {
sprite.buttonMode = true;
sprite.mouseChildren = false;
sprite.useHandCursor = false;
} else {
sprite.buttonMode = false;
sprite.mouseChildren = true;
sprite.useHandCursor = true;
}
}

public static function getMouseAngle(disObjCon : DisplayObjectContainer) : Number {
return Math.atan2(disObjCon.parent.mouseY - disObjCon.y, disObjCon.parent.mouseX - disObjCon.x) * 180 / Math.PI;
};


public static function match(changeClip : DisplayObject, toClip : DisplayObject, withProps : Array = null) : void {

if (withProps == null) {
withProps = [null, "alpha", "visible", "rotation", "scaleX", "scaleY", "x", "y"];
}
var L : Number = withProps.length;
while (--L) {
changeClip[withProps[L]] = toClip[withProps[L]];
}
}

/***********************************
* Used in sort operations
* **********************************/
public static function order_X(a : DisplayObject, b : DisplayObject) : Number {
if (a.x < b.x) {
return -1;
} else if (a.x > b.x) {
return 1;
} else {
return 0;
}
}

public static function order_Y(a : DisplayObject, b : DisplayObject) : Number {
if (a.y < b.y) {
return -1;
} else if (a.y > b.y) {
return 1;
} else {
return 0;
}
}

/********************************************************
* This traverses the parent.parent chain of a displaylist
* appending the name of a clip
* it's useful for absolute pathing a given leave
* for debugging and for associating dynamic data with it
* */
public static function getFullPath(child : DisplayObject, showRoot : Boolean = false) : String {
var cur : DisplayObject = child;
var pathA : Array = new Array();
var res : String;
while(cur.parent != null) {
//trace("parent" + cur.parent);
if(cur.parent != null) {
pathA.push(cur.name);
if(!showRoot && cur.parent == cur.stage) {
break;
} else {
cur = cur.parent;
}
}
}
pathA.reverse();
trace("res " + res);
res = pathA.join(".");
return res;
}

/*
* Returns a path with frame labels, really a deeplink pointer in the displaylist (minus depth)
* e.g. Some.swf#[2].outside_mc[1].middle_mc[1].inner_mc
*
*/
public static function getAnchorPath(child : DisplayObject, showRoot : Boolean = false) : String {
var cur : DisplayObject = child;
var pathA : Array = new Array();
var res : String;
var isLeaf : Boolean = true;

while (cur.parent != null) {
trace("parent" + cur.parent + " == " + cur.parent.name + " " + (cur.parent is Stage));
if (cur.parent != null) {
//trace("url " + cur.loaderInfo.url);

if ( !isLeaf ) {
if ( cur is MovieClip) {
pathA.push("[" + (cur as MovieClip).currentFrame + "].");
} else {
pathA.push(".");
}
}
if ( cur == child.root) {
break;
} else {
trace("moving up");
pathA.push(cur.name);
cur = cur.parent;
isLeaf = false;
}
} else {
trace("no path above ");
}
}
cur = cur.parent;
if ( cur is Stage) {
trace("found stage111111111111111");
//break;
var st : String = cur.loaderInfo.url;
var la : int = st.lastIndexOf("\\", st.length);
var lb : int = st.lastIndexOf("/", st.length);
var ci : int = Math.max(la, lb);
var st2 : String = st.substring(ci + 1, st.length);
//st2 = st2.replace(".swf", "SWF");
trace("swf name " + st2);
pathA.push(st2 + "#");
}
pathA.reverse();
trace("res " + res);
//res = pathA.join(".");
res = pathA.join("");
return res;
}
/* returns the collective rotational transform of the target clip to the stage */
public static function getCollectiveTransformsToStage(child : DisplayObject) : Object {
var cur : DisplayObject = child;
var sp : Sprite = new Sprite();
var b:Rectangle = child.getBounds(child);
sp.graphics.drawRect(b.x,b.y,b.width, b.height);
//sp.rotation = child.rotation;
//trace("start child " + child.stage);
var hitRoot:Boolean = false;
while (cur.parent != null) {
if (cur.parent != null) {
sp.x +=cur.x;
sp.y +=cur.y;
sp.rotation += cur.rotation;
sp.scaleX *= cur.scaleX;
sp.scaleY *= cur.scaleY;
sp.visible = (!sp.visible)?false:cur.visible;
// trace("start " + cur.name + " visible " + cur.visible + " " + cur.stage);
//trace(cur + "'s parent is " + cur.parent);
if (cur.parent == cur.stage) {

hitRoot = true;
break;
} else {
cur = cur.parent;
}
}
}
if(!hitRoot || child.stage == null){
// trace("removed from stage ");
sp.visible =false;
}
return sp;
}
/*
* will return if the passed in clip distance from the local root
* note in the case of multiple swfs, they have their own independent roots.
* nominally should be 1, as
* by the maintimeline/root> passed in clip
*/

public static function getDepthFromRoot(child : DisplayObject) : int {
var cur : DisplayObject = child;
var res : int = 1;
while(cur.parent != null) {
if(cur.parent != null) {
res++;
//trace(cur + "'s parent is " + cur.parent);
if(cur.parent == cur.root) {
break;
} else {

cur = cur.parent;
}
}
}
return res;
}

/*
* will return if the passed in clip is a top level clip as defined
* by the stage>maintimeline/root> passed inclip
* this is useful to avoid library collisions/contentions
*/
public static function isTopLevel(child : DisplayObject) : Boolean {
return getDepthFromStage(child) == 2;
}

/*
* will return if the passed in clip distance from the stage
* nominally should be 2, as
* by the stage>maintimeline/root> passed in clip
*/

public static function getDepthFromStage(child : DisplayObject) : int {
var cur : DisplayObject = child;
var res : int = 1;
while(cur.parent != null) {
if(cur.parent != null) {
res++;
trace(cur + "'s parent is " + cur.parent);
if(cur.parent == cur.stage) {
break;
} else {

cur = cur.parent;
}
}
}
return res;
}
}
}
Show details Hide details

Change log

r70 by TroyWorks on Aug 17, 2009   Diff
Lots of bug fixes and minor enhancements.
Go to: 
Project members, sign in to write a code review

Older revisions

r67 by TroyWorks on Feb 11, 2009   Diff
[No log message]
r52 by TroyWorks on Nov 09, 2008   Diff
[No log message]
r32 by TroyWorks on Aug 05, 2008   Diff
LayoutUtil.as had an erroneous check
against Num== NaN, should be
isNaN(num);   UIUtil.as adding a
utility to addAFrameScript to a
labelled frame
All revisions of this file

File info

Size: 12015 bytes, 418 lines
Hosted by Google Code