1
4
5 import org.aswing.ASWingConstants;
6 import org.aswing.Container;
7 import org.aswing.geom.Dimension;
8 import org.aswing.geom.Rectangle;
9 import org.aswing.graphices.Graphics;
10 import org.aswing.graphices.SolidBrush;
11 import org.aswing.utils.MCUtils;
12
13
20 class org.aswing.FloorPane extends Container {
21
22
26 public static var PREFER_SIZE_BOTH:Number = 0;
27
30 public static var PREFER_SIZE_IMAGE:Number = 1;
31
34 public static var PREFER_SIZE_LAYOUT:Number = 2;
35
36
40 public static var CENTER:Number = ASWingConstants.CENTER;
41
45 public static var TOP:Number = ASWingConstants.TOP;
46
50 public static var LEFT:Number = ASWingConstants.LEFT;
51
55 public static var BOTTOM:Number = ASWingConstants.BOTTOM;
56
60 public static var RIGHT:Number = ASWingConstants.RIGHT;
61
62 private var path:String;
63 private var floorEnabled:Boolean;
64 private var floorMC:MovieClip;
65 private var floorMCDepth:Number;
66 private var floorMCMask:MovieClip;
67 private var floorMCMaskDepth:Number;
68 private var maskFloor:Boolean;
69 private var floorLoaded:Boolean;
70 private var prefferSizeStrategy:Number;
71 private var verticalAlignment:Number;
72 private var horizontalAlignment:Number;
73 private var scaleImage:Boolean;
74 private var floorOriginalSize:Dimension;
75 private var hadscaled:Boolean;
76 private var offsetX:Number;
77 private var offsetY:Number;
78
79
94 private function FloorPane(path:String, prefferSizeStrategy:Number) {
95 super();
96
97 this.path = path;
98 if(prefferSizeStrategy == undefined){
99 prefferSizeStrategy = PREFER_SIZE_BOTH;
100 }
101 this.prefferSizeStrategy = prefferSizeStrategy;
102
103 verticalAlignment = TOP;
104 horizontalAlignment = LEFT;
105 scaleImage = false;
106 hadscaled = false;
107 maskFloor = true;
108 floorOriginalSize = null;
109 floorEnabled = true;
110 floorLoaded = false;
111 offsetX = 0;
112 offsetY = 0;
113 }
114
115
116
123 public function setPath(path:String):Void{
124 if(path != this.path){
125 this.path = path;
126 reload();
127 }
128 }
129
130 public function getPath():String{
131 return path;
132 }
133
134
142 public function setPrefferSizeStrategy(p:Number):Void{
143 prefferSizeStrategy = p;
144 }
145
146
150 public function getPrefferSizeStrategy():Number{
151 return prefferSizeStrategy;
152 }
153
154
165 public function getVerticalAlignment():Number {
166 return verticalAlignment;
167 }
168
169
179 public function setVerticalAlignment(alignment:Number):Void {
180 if (alignment == verticalAlignment){
181 return;
182 }else{
183 verticalAlignment = alignment;
184 revalidate();
185 }
186 }
187
188
199 public function getHorizontalAlignment():Number{
200 return horizontalAlignment;
201 }
202
203
212 public function setHorizontalAlignment(alignment:Number):Void {
213 if (alignment == horizontalAlignment){
214 return;
215 }else{
216 horizontalAlignment = alignment;
217 revalidate();
218 }
219 }
220
221
227 public function setScaleImage(b:Boolean):Void{
228 if(scaleImage != b){
229 scaleImage = b;
230 revalidate();
231 }
232 }
233
234
238 public function isScaleImage():Boolean{
239 return scaleImage;
240 }
241
242
247 public function setOffsetX(offset:Number):Void{
248 if(offsetX != offset){
249 offsetX = offset;
250 revalidate();
251 }
252 }
253
254
259 public function setOffsetY(offset:Number):Void{
260 if(offsetY != offset){
261 offsetY = offset;
262 revalidate();
263 }
264 }
265
266
269 public function getOffsetX():Number{
270 return offsetX;
271 }
272
273
276 public function getOffsetY():Number{
277 return offsetY;
278 }
279
280
290 public function getFloorMC():MovieClip{
291 return floorMC;
292 }
293
294
302 public function disableFloor():Void{
303 if(floorEnabled){
304 floorEnabled = false;
305 setLoaded(false);
306 removeFloorMCs();
307 }
308 }
309
310
316 public function enableFloor():Void{
317 if(!floorEnabled){
318 floorEnabled = true;
319 reload();
320 }
321 }
322
323
328 public function isEnabledFloor():Boolean{
329 return floorEnabled;
330 }
331
332
336 public function isLoaded():Boolean{
337 return floorLoaded;
338 }
339
340
345 public function getFloorOriginalSize():Dimension{
346 if(isLoaded()){
347 return floorOriginalSize;
348 }else{
349 return null;
350 }
351 }
352
353
354
355 private function create():Void{
356 super.create();
357 if(MCUtils.isMovieClipExist(target_mc)){
358 floorMCDepth = target_mc.getNextHighestDepth();
359 floorMCMaskDepth = floorMCDepth + 1;
360 reload();
361 }
362 }
363
364
367 public function doLayout():Void{
368 super.doLayout();
369 fitImage();
370 }
371
372 private function fitImage():Void{
373 if(isLoaded()){
374 var b:Rectangle = getPaintBounds();
375 floorMCMask._x = b.x;
376 floorMCMask._y = b.y;
377 floorMCMask._width = b.width;
378 floorMCMask._height = b.height;
379 if(scaleImage){
380 floorMC._x = b.x - offsetX;
381 floorMC._y = b.y - offsetY;
382 floorMC._width = b.width - offsetX;
383 floorMC._height = b.height - offsetY;
384 hadscaled = true;
385 }else{
386 if(hadscaled){
387 if(floorMC._width != floorOriginalSize.width){
388 floorMC._width = floorOriginalSize.width;
389 }
390 if(floorMC._height != floorOriginalSize.height){
391 floorMC._height = floorOriginalSize.height;
392 }
393 hadscaled = false;
394 }
395 var mx:Number, my:Number;
396 if(horizontalAlignment == CENTER){
397 mx = b.x + (b.width - floorOriginalSize.width)/2;
398 }else if(horizontalAlignment == RIGHT){
399 mx = b.x + (b.width - floorOriginalSize.width);
400 }else{
401 mx = b.x;
402 }
403 if(verticalAlignment == CENTER){
404 my = b.y + (b.height - floorOriginalSize.height)/2;
405 }else if(verticalAlignment == BOTTOM){
406 my = b.y + (b.height - floorOriginalSize.height);
407 }else{
408 my = b.y;
409 }
410 floorMC._x = mx - offsetX;
411 floorMC._y = my - offsetY;
412 }
413 }
414 }
415
416
417
420 private function countPreferredSize():Dimension{
421 var size:Dimension = null;
422 var sizeByMC:Dimension;
423 if(isLoaded()){
424 sizeByMC = new Dimension(floorOriginalSize.width - offsetX, floorOriginalSize.height - offsetY);
425 sizeByMC = getInsets().roundsSize(sizeByMC);
426 }else{
427 sizeByMC = super.countPreferredSize();
428 }
429
430 size = super.countPreferredSize();
431
432 if(prefferSizeStrategy == PREFER_SIZE_IMAGE){
433 return sizeByMC;
434 }else if(prefferSizeStrategy == PREFER_SIZE_LAYOUT){
435 return size;
436 }else{
437 return new Dimension(
438 Math.max(sizeByMC.width, size.width),
439 Math.max(sizeByMC.height, size.height));
440 }
441 }
442
443
451 public function reload():Void{
452 if(isEnabledFloor()){
453 removeFloorMCs();
454 floorMC = createFloorMC();
455 floorMCMask = createFloorMaskMC(floorMC);
456 setMaskFloor(maskFloor);
457 setLoaded(false);
458 loadFloor();
459 }
460 }
461
462 public function isMaskFloor():Boolean{
463 return maskFloor;
464 }
465
466 public function setMaskFloor(m:Boolean):Void{
467 maskFloor = m;
468 if(m){
469 floorMC.setMask(floorMCMask);
470 floorMCMask._visible = true;
471 }else{
472 floorMCMask._visible = false;
473 floorMC.setMask(null);
474 }
475 }
476
477 private function removeFloorMCs():Void{
478 floorMC.setMask(null);
479 floorMC.unloadMovie();
480 floorMC.removeMovieClip();
481 floorMCMask.clear();
482 floorMC = floorMCMask = null;
483 }
484
485
488 private function createFloorMaskMC(floorMC:MovieClip):MovieClip{
489 if(MCUtils.isMovieClipExist(floorMC)){
490 var parentMC:MovieClip = MovieClip(floorMC._parent);
491 floorMCMask = creater.createMC(parentMC, "floorMask", getFloorMaskDepth());
492 var g:Graphics = new Graphics(floorMCMask);
493 g.fillRectangle(new SolidBrush(0), 0, 0, 1, 1);
494 return floorMCMask;
495 }else{
496 return null;
497 }
498 }
499
500 private function setLoaded(b:Boolean):Void{
501 floorLoaded = b;
502 }
503
504 private function setFloorOriginalSize(size:Dimension):Void{
505 floorOriginalSize = new Dimension(size.width, size.height);
506 }
507
508
511 private function getFloorDepth():Number{
512 return floorMCDepth;
513 }
514
515
518 private function getFloorMaskDepth():Number{
519 return floorMCMaskDepth;
520 }
521
522
523
528 private function loadFloor():Void{
529 }
530
531
536 private function createFloorMC():MovieClip{
537 return null;
538 }
539 }
540