forked from arpit/openpyro
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathViewStack.as
More file actions
128 lines (112 loc) · 3.3 KB
/
ViewStack.as
File metadata and controls
128 lines (112 loc) · 3.3 KB
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
package org.openPyro.containers
{
import flash.display.DisplayObject;
import org.openPyro.controls.scrollBarClasses.ScrollPolicy;
import org.openPyro.core.UIContainer;
import org.openPyro.layout.ILayout;
import org.openPyro.utils.ArrayUtil;
public class ViewStack extends UIContainer{
[ArrayElementType("org.openPyro.core.UIContainer")]
protected var viewChildren:Array;
/**
* The viewstack manages multiple the visibility of
* multiple UIContainers that are added to it.
*/
public function ViewStack(){
super();
this._horizontalScrollPolicy = ScrollPolicy.OFF
this._verticalScrollPolicy = ScrollPolicy.OFF;
viewChildren = new Array();
}
private var viewsChanged:Boolean = true;
protected var _selectedIndex:int = -1;
public function set selectedIndex(idx:int):void
{
if(_selectedIndex == idx) return;
_selectedIndex = idx;
this._selectedChild = viewChildren[_selectedIndex];
viewsChanged = true;
if(this.initialized){
forceInvalidateDisplayList=true;
invalidateDisplayList();
}
}
public function get selectedIndex():int
{
return _selectedIndex;
}
override public function addChild(child:DisplayObject):DisplayObject
{
if(!(child is UIContainer))
{
throw new Error("ViewStacks can only hold UIContainers");
return;
}
return addChildAt(child, this.contentPane.numChildren);
}
override public function addChildAt(child:DisplayObject, index:int):DisplayObject
{
if(!(child is UIContainer))
{
throw new Error("ViewStacks can only hold UIContainers");
return;
}
ArrayUtil.insertAt(viewChildren, index, child);
_selectedChild = viewChildren[viewChildren.length-1];
_selectedIndex = viewChildren.length -1;
viewsChanged = true;
for(var i:int=0; i<viewChildren.length; i++){
viewChildren[i].visible = false;
}
_selectedChild.visible = true;
return super.addChildAt(child, index);
}
protected var _selectedChild:UIContainer = null;
public function set selectedChild(child:UIContainer):void
{
if(_selectedChild == child) return;
for(var i:uint=0; i<viewChildren.length; i++)
{
var container:UIContainer = viewChildren[i];
if(container == child)
{
this.selectedIndex = i;
}
}
}
override public function removeChild(child:DisplayObject):DisplayObject
{
if(viewChildren.indexOf(child) != -1){
ArrayUtil.remove(viewChildren, child);
}
if(_selectedChild == child){
// set the topmost child as the visible child
selectedIndex = viewChildren.length-1;
}
return super.removeChild(child);
}
public function get selectedChild():UIContainer
{
return _selectedChild;
}
override public function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth-padding.left- padding.right, unscaledHeight-padding.top - padding.bottom);
for each(var ch:DisplayObject in this.layoutChildren){
ch.y = padding.top;
ch.x = padding.right;
}
if(viewsChanged){
viewsChanged = false;
showSelectedChild()
}
}
protected function showSelectedChild():void
{
for(var i:uint=0; i<viewChildren.length; i++){
var child:DisplayObject = DisplayObject(viewChildren[i]);
child.visible = (i == _selectedIndex)?true:false;
}
}
}
}