Laya 自制框架之窗口层级管理器
@author ixenos 2021年5月26日
1 package framework.manager
2 {
3 import laya.display.Sprite;
4 import laya.events.Event;
5 import laya.ui.Box;
6 import laya.utils.ClassUtils;
7 import laya.utils.Dictionary;
8 import laya.utils.Ease;
9 import laya.utils.Handler;
10 import laya.utils.Tween;
11
12 public class LayerManager
13 {
14 private static var _ins:LayerManager;
15
16 private var _layerBasic:Box;
17
18 private var _layerPop:Box;
19
20 private var _shadowPop:Box;
21 private var _shadowAlpha:Number = 0.3;
22
23 private var _curPop:String;
24 private var _curPopCloseByShadow:Boolean = false;
25
26 public static var container:Box;
27
28 private var _winMap:Dictionary = new Dictionary();
29
30 private var _diaMap:Dictionary = new Dictionary();
31
32 public function LayerManager()
33 {
34 container = new Box();
35 Laya.stage.addChild(container);
36
37 _layerBasic = new Box();
38 container.addChild(_layerBasic);
39
40 _layerPop = new Box();
41 _layerPop.mouseThrough = true;
42 container.addChild(_layerPop);
43
44 _shadowPop = new Box();
45 _shadowPop.on(Event.CLICK,this,onClickShadowPop);
46 _layerPop.addChild(_shadowPop);
47
48 }
49
50 public function resizeControl():void{
51 var container:Box = LayerManager.container;
52 if(container){
53 container.size(Laya.stage.width,Laya.stage.height);
54 _layerBasic.size(Laya.stage.width,Laya.stage.height);
55 _layerPop.size(Laya.stage.width,Laya.stage.height);
56 _shadowPop.size(Laya.stage.width,Laya.stage.height);
57 _shadowPop.graphics.drawRect(0,0,_shadowPop.width,_shadowPop.height,"0x000000");
58 _shadowPop.visible = false;
59 }
60 }
61
62 private function onClickShadowPop():void{
63 if(_curPopCloseByShadow && _curPop){
64 LayerManager.ins.closeDialog(_curPop);
65 _curPopCloseByShadow = false;
66 }
67 }
68
69 public static function get ins():LayerManager{
70 if(!_ins){
71 _ins = new LayerManager();
72 }
73 return _ins;
74 }
75
76 public static function clean():void{
77 _ins = null;
78 }
79
80 public function openWindow(id:String):void{
81 if(id){
82 if(_winMap.get(id)){
83
84 }else{
85 var sp:Box = ClassUtils.getInstance(id);
86 if(sp){
87 _winMap.set(id,sp);
88 _layerBasic.addChild(sp);
89 }
90 }
91 }
92 }
93
94 public function openDialog(id:String,closeByShadow:Boolean=true):void{
95 if(id){
96 if(_diaMap.get(id)){
97
98 }else{
99 var sp:Box = ClassUtils.getInstance(id);
100 if(sp){
101 _curPopCloseByShadow = closeByShadow;
102 _curPop = id;
103 _diaMap.set(id,sp);
104 _layerPop.addChild(sp);
105 sp.centerX = 0;
106 sp.centerY = 0;
107 sp.anchorX = 0.5;
108 sp.anchorY = 0.5;
109 sp.scaleX = 0.5;
110 sp.scaleY = 0.5;
111 sp.alpha = 0.3;
112 enterAni(sp);
113 showShadowPop(true);
114 }
115 }
116 }
117 }
118
119 private var _twSha:Tween;
120 private function showShadowPop(show:Boolean):void{
121 if(_twSha){
122 _twSha.clear();
123 _twSha = null;
124 }
125
126 if(show){
127 _shadowPop.alpha = _shadowAlpha;
128 _shadowPop.visible = true;
129 }else{
130 _shadowPop.alpha = 0;
131 _shadowPop.visible = false;
132 }
133 }
134
135 private var _twEnter:Tween;
136 private var _twExit:Tween;
137 private function enterAni(sp:Sprite, complete:Handler=null):void{
138 if(!sp)return;
139 if(_twEnter){
140 _twEnter.clear();
141 _twEnter = null;
142 }
143 _twEnter = Tween.to(sp,{scaleX:1,scaleY:1,alpha:1},300,Ease.backOut,complete);
144 }
145
146 private function exitAni(sp:Sprite, complete:Handler=null):void{
147 if(!sp)return;
148 if(_twExit){
149 _twExit.clear();
150 _twExit = null;
151 }
152
153 Laya.stage.mouseEnabled = false;
154 _twExit = Tween.to(sp,{scaleX:0.5,scaleY:0.5,alpha:0.3},100,null,new Handler(this,function():void{
155 Laya.stage.mouseEnabled = true;
156 complete && complete.run();
157 }));
158 }
159
160 public function closeWindow(id:String):void{
161 var sp:Box = _winMap.get(id) as Box;
162 if(sp){
163 sp.removeSelf();
164 sp.destroy();
165 _winMap.remove(id);
166 }
167 }
168
169 public function closeDialog(id:String):void{
170 var sp:Box = _diaMap.get(id) as Box;
171 if(sp){
172 showShadowPop(false);
173 exitAni(sp,new Handler(this,function():void{
174 sp.removeSelf();
175 sp.destroy();
176 _diaMap.remove(id);
177 }));
178 }
179 }
180
181 public function getWindowInstance(id:String):*{
182 var ret:*;
183 if(_winMap){
184 ret = _winMap.get(id);
185 }
186 return ret;
187 }
188
189 public function getDiaInstance(id:String):*{
190 var ret:*;
191 if(_diaMap){
192 ret = _diaMap.get(id);
193 }
194 return ret;
195 }
196
197 }
198 }