~zodiacsohma/armagetronad/vectron

« back to all changes in this revision

Viewing changes to src/Home_old.as

  • Committer: zodiacsohma1 at gmail
  • Date: 2014-01-22 02:46:23 UTC
  • Revision ID: zodiacsohma1@gmail.com-20140122024623-1dyhnemuhogi23gp
Converted all tab characters into spaces (4 gaps).

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
package
27
27
{
28
 
        import flash.display.MovieClip;
29
 
        import flash.display.Sprite;
30
 
 
31
 
        import flash.geom.Point;
32
 
        import flash.geom.Rectangle;
33
 
 
34
 
        import flash.events.Event;
35
 
        import flash.events.MouseEvent;
36
 
        import flash.events.ProgressEvent;
37
 
        import flash.events.KeyboardEvent;
38
 
        import flash.ui.Keyboard;
39
 
 
40
 
        import flash.net.FileReference;
41
 
 
42
 
        import orfaust.Debug;
43
 
        import orfaust.CustomEvent;
44
 
        import orfaust.containers.List;
45
 
        
46
 
 
47
 
        public class Home extends Base
48
 
        {
49
 
                private var _aamap:Aamap;
50
 
 
51
 
                private var _tool:ToolInterface;
52
 
                private static var _pointer:MovieClip;
53
 
                private static var _snapToGrid:Boolean = true;
54
 
 
55
 
                private const ZOOM_FACTOR = .95;
56
 
                private var _scale = 1;
57
 
                private static var _cursor:Point = new Point(0,0);
58
 
 
59
 
                private var _infoHandlerMouseDown:Boolean = false;
60
 
                private const INFO_MAX_WIDTH = 230;
61
 
 
62
 
                private var _draggingMap:Boolean = false;
63
 
                private var _keyDown:List = new List;
64
 
 
65
 
 
66
 
 
67
 
 
68
 
                override protected function init():void
69
 
                {
70
 
                        pointer.visible = false;
71
 
                        _pointer = pointer;
72
 
 
73
 
                        // testing urls to load external maps
74
 
 
75
 
                        //var xmlUrl = 'aamap.php?url=';
76
 
 
77
 
                        //xmlUrl += 'resource.armagetronad.net/resource/ZURD/race/Crossdeath-1.0.1.aamap.xml';
78
 
                        //xmlUrl += 'resource.armagetronad.net/resource/ZURD/race/ecdmazed-1.aamap.xml';
79
 
                        //xmlUrl += 'resource.armagetronad.net/resource/hoop/motorace/tester-0.1.aamap.xml';
80
 
                        //xmlUrl += 'resource.armagetronad.net/resource/hoop/race/alba-1.6.aamap.xml';
81
 
                        //xmlUrl += 
82
 
 
83
 
                        //var xmlUrl = 'aamap/default-1.0.1.aamap.xml';
84
 
                        var xmlUrl = 'aamap/vectron-1.0.aamap.xml';
85
 
 
86
 
                        Zone.init();
87
 
 
88
 
                        stage.addEventListener(Event.RESIZE,onStageResize,false,0,true);
89
 
                        onStageResize(null);
90
 
 
91
 
                        infoHandler.addEventListener(MouseEvent.MOUSE_DOWN,handleInfoSize,false,0,true);
92
 
                        infoHandler.addEventListener(MouseEvent.MOUSE_UP,handleInfoSize,false,0,true);
93
 
                        infoHandler.addEventListener(MouseEvent.ROLL_OVER,infoHandlerHover,false,0,true);
94
 
                        infoHandler.addEventListener(MouseEvent.ROLL_OUT,infoHandlerHover,false,0,true);
95
 
                        infoHandler.addEventListener(MouseEvent.MOUSE_MOVE,infoHandlerHover,false,0,true);
96
 
 
97
 
                        toolBar.tools.addEventListener(MouseEvent.CLICK,onToolClick,false,0,true);
98
 
                        toolBar.addEventListener(MouseEvent.ROLL_OVER,breakToolEvents,false,0,true);
99
 
                        toolBar.addEventListener(MouseEvent.ROLL_OUT,breakToolEvents,false,0,true);
100
 
 
101
 
                        info.addEventListener(MouseEvent.ROLL_OVER,breakToolEvents,false,0,true);
102
 
                        info.addEventListener(MouseEvent.ROLL_OUT,breakToolEvents,false,0,true);
103
 
                        infoHandler.addEventListener(MouseEvent.ROLL_OVER,breakToolEvents,false,0,true);
104
 
                        infoHandler.addEventListener(MouseEvent.ROLL_OUT,breakToolEvents,false,0,true);
105
 
 
106
 
                        _tool = toolBar.tools.select;
107
 
 
108
 
                        stage.addEventListener(KeyboardEvent.KEY_DOWN,handleKeyboard,false,0,true);
109
 
                        stage.addEventListener(KeyboardEvent.KEY_UP,handleKeyboard,false,0,true);
110
 
 
111
 
                        super.loadUrl(xmlUrl,initMap,loadingProgress);
112
 
                }
113
 
 
114
 
                private function handleInfoSize(e:MouseEvent):void
115
 
                {
116
 
                        if(e.type == MouseEvent.MOUSE_DOWN)
117
 
                                infoHandler.startDrag(false,new Rectangle(0,0,INFO_MAX_WIDTH,0));
118
 
                        else
119
 
                                infoHandler.stopDrag();
120
 
                }
121
 
                private function infoHandlerHover(e:MouseEvent):void
122
 
                {
123
 
                        pointer.visible = true;
124
 
                        pointer.gotoAndStop(2);
125
 
 
126
 
                        if(e.type == MouseEvent.ROLL_OVER || e.type == MouseEvent.MOUSE_MOVE)
127
 
                        {
128
 
                                infoMask.width = infoHandler.x;
129
 
                                pointer.x = stage.mouseX;
130
 
                                pointer.y = stage.mouseY;
131
 
                        }
132
 
                        else
133
 
                        {
134
 
                                pointer.visible = false;
135
 
                        }
136
 
                }
137
 
 
138
 
 
139
 
 
140
 
                public static function get mapCursor():Point
141
 
                {
142
 
                        return _cursor;
143
 
                }
144
 
                public static function set snapToGrid(val:Boolean)
145
 
                {
146
 
                        _snapToGrid = val;
147
 
                }
148
 
 
149
 
                public static function get pointer():MovieClip
150
 
                {
151
 
                        return _pointer;
152
 
                }
 
28
    import flash.display.MovieClip;
 
29
    import flash.display.Sprite;
 
30
 
 
31
    import flash.geom.Point;
 
32
    import flash.geom.Rectangle;
 
33
 
 
34
    import flash.events.Event;
 
35
    import flash.events.MouseEvent;
 
36
    import flash.events.ProgressEvent;
 
37
    import flash.events.KeyboardEvent;
 
38
    import flash.ui.Keyboard;
 
39
 
 
40
    import flash.net.FileReference;
 
41
 
 
42
    import orfaust.Debug;
 
43
    import orfaust.CustomEvent;
 
44
    import orfaust.containers.List;
 
45
 
 
46
 
 
47
    public class Home extends Base
 
48
    {
 
49
        private var _aamap:Aamap;
 
50
 
 
51
        private var _tool:ToolInterface;
 
52
        private static var _pointer:MovieClip;
 
53
        private static var _snapToGrid:Boolean = true;
 
54
 
 
55
        private const ZOOM_FACTOR = .95;
 
56
        private var _scale = 1;
 
57
        private static var _cursor:Point = new Point(0,0);
 
58
 
 
59
        private var _infoHandlerMouseDown:Boolean = false;
 
60
        private const INFO_MAX_WIDTH = 230;
 
61
 
 
62
        private var _draggingMap:Boolean = false;
 
63
        private var _keyDown:List = new List;
 
64
 
 
65
 
 
66
 
 
67
 
 
68
        override protected function init():void
 
69
        {
 
70
            pointer.visible = false;
 
71
            _pointer = pointer;
 
72
 
 
73
            // testing urls to load external maps
 
74
 
 
75
            //var xmlUrl = 'aamap.php?url=';
 
76
 
 
77
            //xmlUrl += 'resource.armagetronad.net/resource/ZURD/race/Crossdeath-1.0.1.aamap.xml';
 
78
            //xmlUrl += 'resource.armagetronad.net/resource/ZURD/race/ecdmazed-1.aamap.xml';
 
79
            //xmlUrl += 'resource.armagetronad.net/resource/hoop/motorace/tester-0.1.aamap.xml';
 
80
            //xmlUrl += 'resource.armagetronad.net/resource/hoop/race/alba-1.6.aamap.xml';
 
81
            //xmlUrl +=
 
82
 
 
83
            //var xmlUrl = 'aamap/default-1.0.1.aamap.xml';
 
84
            var xmlUrl = 'aamap/vectron-1.0.aamap.xml';
 
85
 
 
86
            Zone.init();
 
87
 
 
88
            stage.addEventListener(Event.RESIZE,onStageResize,false,0,true);
 
89
            onStageResize(null);
 
90
 
 
91
            infoHandler.addEventListener(MouseEvent.MOUSE_DOWN,handleInfoSize,false,0,true);
 
92
            infoHandler.addEventListener(MouseEvent.MOUSE_UP,handleInfoSize,false,0,true);
 
93
            infoHandler.addEventListener(MouseEvent.ROLL_OVER,infoHandlerHover,false,0,true);
 
94
            infoHandler.addEventListener(MouseEvent.ROLL_OUT,infoHandlerHover,false,0,true);
 
95
            infoHandler.addEventListener(MouseEvent.MOUSE_MOVE,infoHandlerHover,false,0,true);
 
96
 
 
97
            toolBar.tools.addEventListener(MouseEvent.CLICK,onToolClick,false,0,true);
 
98
            toolBar.addEventListener(MouseEvent.ROLL_OVER,breakToolEvents,false,0,true);
 
99
            toolBar.addEventListener(MouseEvent.ROLL_OUT,breakToolEvents,false,0,true);
 
100
 
 
101
            info.addEventListener(MouseEvent.ROLL_OVER,breakToolEvents,false,0,true);
 
102
            info.addEventListener(MouseEvent.ROLL_OUT,breakToolEvents,false,0,true);
 
103
            infoHandler.addEventListener(MouseEvent.ROLL_OVER,breakToolEvents,false,0,true);
 
104
            infoHandler.addEventListener(MouseEvent.ROLL_OUT,breakToolEvents,false,0,true);
 
105
 
 
106
            _tool = toolBar.tools.select;
 
107
 
 
108
            stage.addEventListener(KeyboardEvent.KEY_DOWN,handleKeyboard,false,0,true);
 
109
            stage.addEventListener(KeyboardEvent.KEY_UP,handleKeyboard,false,0,true);
 
110
 
 
111
            super.loadUrl(xmlUrl,initMap,loadingProgress);
 
112
        }
 
113
 
 
114
        private function handleInfoSize(e:MouseEvent):void
 
115
        {
 
116
            if(e.type == MouseEvent.MOUSE_DOWN)
 
117
                infoHandler.startDrag(false,new Rectangle(0,0,INFO_MAX_WIDTH,0));
 
118
            else
 
119
                infoHandler.stopDrag();
 
120
        }
 
121
        private function infoHandlerHover(e:MouseEvent):void
 
122
        {
 
123
            pointer.visible = true;
 
124
            pointer.gotoAndStop(2);
 
125
 
 
126
            if(e.type == MouseEvent.ROLL_OVER || e.type == MouseEvent.MOUSE_MOVE)
 
127
            {
 
128
                infoMask.width = infoHandler.x;
 
129
                pointer.x = stage.mouseX;
 
130
                pointer.y = stage.mouseY;
 
131
            }
 
132
            else
 
133
            {
 
134
                pointer.visible = false;
 
135
            }
 
136
        }
 
137
 
 
138
 
 
139
 
 
140
        public static function get mapCursor():Point
 
141
        {
 
142
            return _cursor;
 
143
        }
 
144
        public static function set snapToGrid(val:Boolean)
 
145
        {
 
146
            _snapToGrid = val;
 
147
        }
 
148
 
 
149
        public static function get pointer():MovieClip
 
150
        {
 
151
            return _pointer;
 
152
        }
153
153
 
154
154
 
155
155
 
156
156
/* zoom */
157
157
 
158
 
                private function zoom(e:MouseEvent):void
159
 
                {
160
 
                        if(e.delta > 0)
161
 
                                zoomIn();
162
 
                        else
163
 
                                zoomOut();
164
 
                }
165
 
 
166
 
                private function zoomIn():void
167
 
                {
168
 
                        if(_scale > 50)
169
 
                                return;
170
 
 
171
 
                        var factor = _scale;
172
 
                        _scale /= ZOOM_FACTOR;
173
 
 
174
 
                        setScale();
175
 
                        factor = _scale - factor;
176
 
 
177
 
                        _aamap.x -= _cursor.x * factor;
178
 
                        _aamap.y -= -_cursor.y * factor;
179
 
 
180
 
                        grid.render(_aamap);
181
 
                        setInfo();
182
 
                }
183
 
                private function zoomOut():void
184
 
                {
185
 
                        if(_scale < .05)
186
 
                                return;
187
 
 
188
 
                        var factor = _scale;
189
 
                        _scale *= ZOOM_FACTOR;
190
 
 
191
 
                        setScale();
192
 
                        factor -= _scale;
193
 
 
194
 
                        _aamap.x += _cursor.x * factor;
195
 
                        _aamap.y += -_cursor.y * factor;
196
 
 
197
 
                        grid.render(_aamap);
198
 
                        setInfo();
199
 
                }
200
 
 
201
 
                private function setScale():void
202
 
                {
203
 
                        _aamap.scaleX = _scale;
204
 
                        _aamap.scaleY = -_scale;
205
 
                }
206
 
 
207
 
                private function setInfo():void
208
 
                {
209
 
                        _cursor.x = (stage.mouseX - _aamap.x) / _scale;
210
 
                        _cursor.y = (-stage.mouseY + _aamap.y) / _scale;
211
 
 
212
 
                        snapPointer.visible = _snapToGrid;
213
 
                        if(_snapToGrid)
214
 
                        {
215
 
                                var gridSize = grid.size;
216
 
 
217
 
                                var xSnap = Math.floor((_cursor.x + gridSize.x / 2) / gridSize.x);
218
 
                                var ySnap = Math.floor((_cursor.y + gridSize.y / 2) / gridSize.y);
219
 
 
220
 
                                _cursor.x = xSnap * gridSize.x;
221
 
                                _cursor.y = ySnap * gridSize.y;
222
 
 
223
 
                                snapPointer.x = _aamap.x + _cursor.x * _aamap.scaleX;
224
 
                                snapPointer.y = _aamap.y + _cursor.y * _aamap.scaleY;
225
 
                        }
226
 
 
227
 
                        info.txt.text =
228
 
                                'x: ' + _cursor.x.toString() + '\n' + 
229
 
                                'y: ' + (_cursor.y).toString() + '\n' +
230
 
                                'zoom: ' + Math.floor(_scale * 100).toString() + '%';
231
 
                }
 
158
        private function zoom(e:MouseEvent):void
 
159
        {
 
160
            if(e.delta > 0)
 
161
                zoomIn();
 
162
            else
 
163
                zoomOut();
 
164
        }
 
165
 
 
166
        private function zoomIn():void
 
167
        {
 
168
            if(_scale > 50)
 
169
                return;
 
170
 
 
171
            var factor = _scale;
 
172
            _scale /= ZOOM_FACTOR;
 
173
 
 
174
            setScale();
 
175
            factor = _scale - factor;
 
176
 
 
177
            _aamap.x -= _cursor.x * factor;
 
178
            _aamap.y -= -_cursor.y * factor;
 
179
 
 
180
            grid.render(_aamap);
 
181
            setInfo();
 
182
        }
 
183
        private function zoomOut():void
 
184
        {
 
185
            if(_scale < .05)
 
186
                return;
 
187
 
 
188
            var factor = _scale;
 
189
            _scale *= ZOOM_FACTOR;
 
190
 
 
191
            setScale();
 
192
            factor -= _scale;
 
193
 
 
194
            _aamap.x += _cursor.x * factor;
 
195
            _aamap.y += -_cursor.y * factor;
 
196
 
 
197
            grid.render(_aamap);
 
198
            setInfo();
 
199
        }
 
200
 
 
201
        private function setScale():void
 
202
        {
 
203
            _aamap.scaleX = _scale;
 
204
            _aamap.scaleY = -_scale;
 
205
        }
 
206
 
 
207
        private function setInfo():void
 
208
        {
 
209
            _cursor.x = (stage.mouseX - _aamap.x) / _scale;
 
210
            _cursor.y = (-stage.mouseY + _aamap.y) / _scale;
 
211
 
 
212
            snapPointer.visible = _snapToGrid;
 
213
            if(_snapToGrid)
 
214
            {
 
215
                var gridSize = grid.size;
 
216
 
 
217
                var xSnap = Math.floor((_cursor.x + gridSize.x / 2) / gridSize.x);
 
218
                var ySnap = Math.floor((_cursor.y + gridSize.y / 2) / gridSize.y);
 
219
 
 
220
                _cursor.x = xSnap * gridSize.x;
 
221
                _cursor.y = ySnap * gridSize.y;
 
222
 
 
223
                snapPointer.x = _aamap.x + _cursor.x * _aamap.scaleX;
 
224
                snapPointer.y = _aamap.y + _cursor.y * _aamap.scaleY;
 
225
            }
 
226
 
 
227
            info.txt.text =
 
228
                'x: ' + _cursor.x.toString() + '\n' +
 
229
                'y: ' + (_cursor.y).toString() + '\n' +
 
230
                'zoom: ' + Math.floor(_scale * 100).toString() + '%';
 
231
        }
232
232
 
233
233
 
234
234
 
235
235
 
236
236
/* tools */
237
237
 
238
 
                private function breakToolEvents(e:MouseEvent):void
239
 
                {
240
 
                        if(e.type == 'rollOver')
241
 
                        {
242
 
                                //removeMapListeners();
243
 
                                _tool.close();
244
 
                                snapPointer.visible = false;
245
 
                        }
246
 
                        else
247
 
                        {
248
 
                                //addMapListeners();
249
 
                        }
250
 
                }
251
 
 
252
 
                private function onToolClick(e:MouseEvent):void
253
 
                {
254
 
                        switchTool(e.target);
255
 
                }
256
 
 
257
 
                private function switchTool(selected:*):void
258
 
                {
259
 
                        if(selected == _tool || selected == toolBar || selected == toolBar.overlay)
260
 
                                return;
261
 
 
262
 
                        pointer.visible = false;
263
 
 
264
 
                        _tool.close();
265
 
                        //removeToolListeners();
266
 
 
267
 
                        _tool = selected;
268
 
                        toolBar.overlay.x = toolBar.tools.x + selected.x;
269
 
                        //addToolListeners();
270
 
                }
271
 
 
272
 
                private function addToolListeners():void
273
 
                {
274
 
                        _tool.addEventListener('ADD_EDITING_OBJECT',addEditingObject,false,0,true);
275
 
                        _tool.addEventListener('EDITING_OBJECT_COMPLETE',editingObjectComplete,false,0,true);
276
 
                        _tool.addEventListener('REMOVE_EDITING_OBJECT',removeEditingObject,false,0,true);
277
 
 
278
 
                        if(_tool == toolBar.tools.select)
279
 
                        {
280
 
                                _aamap.addEventListener(MouseEvent.MOUSE_DOWN,handleObjectMouseEvent,false,0,true);
281
 
                                _aamap.addEventListener(MouseEvent.MOUSE_UP,handleObjectMouseEvent,false,0,true);
282
 
                                _aamap.addEventListener(MouseEvent.MOUSE_MOVE,handleObjectMouseEvent,false,0,true);
283
 
                                _aamap.addEventListener('OBJECT_ROLL_OVER',handleObjectHover,false,0,true);
284
 
                                _aamap.addEventListener('OBJECT_ROLL_OUT',handleObjectHover,false,0,true);
285
 
                        }
286
 
                }
287
 
 
288
 
                private function removeToolListeners():void
289
 
                {
290
 
                        _tool.removeEventListener('ADD_EDITING_OBJECT',addEditingObject);
291
 
                        _tool.removeEventListener('EDITING_OBJECT_COMPLETE',editingObjectComplete);
292
 
                        _tool.removeEventListener('REMOVE_EDITING_OBJECT',removeEditingObject);
293
 
 
294
 
                        if(_tool == toolBar.tools.select)
295
 
                        {
296
 
                                _aamap.removeEventListener(MouseEvent.MOUSE_DOWN,handleObjectMouseEvent);
297
 
                                _aamap.removeEventListener(MouseEvent.MOUSE_UP,handleObjectMouseEvent);
298
 
                                _aamap.removeEventListener(MouseEvent.MOUSE_MOVE,handleObjectMouseEvent);
299
 
                                _aamap.removeEventListener('OBJECT_ROLL_OVER',handleObjectHover);
300
 
                                _aamap.removeEventListener('OBJECT_ROLL_OUT',handleObjectHover);
301
 
                        }
302
 
                }
 
238
        private function breakToolEvents(e:MouseEvent):void
 
239
        {
 
240
            if(e.type == 'rollOver')
 
241
            {
 
242
                //removeMapListeners();
 
243
                _tool.close();
 
244
                snapPointer.visible = false;
 
245
            }
 
246
            else
 
247
            {
 
248
                //addMapListeners();
 
249
            }
 
250
        }
 
251
 
 
252
        private function onToolClick(e:MouseEvent):void
 
253
        {
 
254
            switchTool(e.target);
 
255
        }
 
256
 
 
257
        private function switchTool(selected:*):void
 
258
        {
 
259
            if(selected == _tool || selected == toolBar || selected == toolBar.overlay)
 
260
                return;
 
261
 
 
262
            pointer.visible = false;
 
263
 
 
264
            _tool.close();
 
265
            //removeToolListeners();
 
266
 
 
267
            _tool = selected;
 
268
            toolBar.overlay.x = toolBar.tools.x + selected.x;
 
269
            //addToolListeners();
 
270
        }
 
271
 
 
272
        private function addToolListeners():void
 
273
        {
 
274
            _tool.addEventListener('ADD_EDITING_OBJECT',addEditingObject,false,0,true);
 
275
            _tool.addEventListener('EDITING_OBJECT_COMPLETE',editingObjectComplete,false,0,true);
 
276
            _tool.addEventListener('REMOVE_EDITING_OBJECT',removeEditingObject,false,0,true);
 
277
 
 
278
            if(_tool == toolBar.tools.select)
 
279
            {
 
280
                _aamap.addEventListener(MouseEvent.MOUSE_DOWN,handleObjectMouseEvent,false,0,true);
 
281
                _aamap.addEventListener(MouseEvent.MOUSE_UP,handleObjectMouseEvent,false,0,true);
 
282
                _aamap.addEventListener(MouseEvent.MOUSE_MOVE,handleObjectMouseEvent,false,0,true);
 
283
                _aamap.addEventListener('OBJECT_ROLL_OVER',handleObjectHover,false,0,true);
 
284
                _aamap.addEventListener('OBJECT_ROLL_OUT',handleObjectHover,false,0,true);
 
285
            }
 
286
        }
 
287
 
 
288
        private function removeToolListeners():void
 
289
        {
 
290
            _tool.removeEventListener('ADD_EDITING_OBJECT',addEditingObject);
 
291
            _tool.removeEventListener('EDITING_OBJECT_COMPLETE',editingObjectComplete);
 
292
            _tool.removeEventListener('REMOVE_EDITING_OBJECT',removeEditingObject);
 
293
 
 
294
            if(_tool == toolBar.tools.select)
 
295
            {
 
296
                _aamap.removeEventListener(MouseEvent.MOUSE_DOWN,handleObjectMouseEvent);
 
297
                _aamap.removeEventListener(MouseEvent.MOUSE_UP,handleObjectMouseEvent);
 
298
                _aamap.removeEventListener(MouseEvent.MOUSE_MOVE,handleObjectMouseEvent);
 
299
                _aamap.removeEventListener('OBJECT_ROLL_OVER',handleObjectHover);
 
300
                _aamap.removeEventListener('OBJECT_ROLL_OUT',handleObjectHover);
 
301
            }
 
302
        }
303
303
 
304
304
 
305
305
 
308
308
 
309
309
/* map */
310
310
 
311
 
                private function initMap(e:Event):void
312
 
                {
313
 
                        progBar.visible = false;
314
 
 
315
 
                        try
316
 
                        {
317
 
                                _aamap = new Aamap(e.target.data);
318
 
                        }
319
 
                        catch(e)
320
 
                        {
321
 
                                Debug.log(e);
322
 
                                return;
323
 
                        }
324
 
 
325
 
                        mapContainer.addChild(_aamap);
326
 
                        _aamap.scaleY = -1;
327
 
 
328
 
                        _aamap.x = _sW / 2;
329
 
                        _aamap.y = _sH / 2;
330
 
 
331
 
                        //addToolListeners();
332
 
                        //addMapListeners();
333
 
                        stage.addEventListener(MouseEvent.MOUSE_WHEEL,zoom,false,0,true);
334
 
 
335
 
                        toolBar.save.addEventListener(MouseEvent.CLICK,saveXml,false,0,true);
336
 
                        toolBar.open.addEventListener(MouseEvent.CLICK,loadXml,false,0,true);
337
 
 
338
 
                        grid.size = new Point(10,10);
339
 
                        //grid.visible = false;
340
 
                        grid.render(_aamap);
341
 
                        setInfo();
342
 
                }
343
 
 
344
 
 
345
 
                private function removeSelected(e:MouseEvent):void
346
 
                {
347
 
                        toolBar.tools.select.removeSelected();
348
 
                }
349
 
 
350
 
                private function saveXml(e:MouseEvent):void
351
 
                {
352
 
                        Debug.clear();
353
 
                        Debug.log('\n' + _aamap.xml);
354
 
                }
355
 
 
356
 
                private function loadXml(e:MouseEvent):void
357
 
                {
358
 
                        
359
 
                }
360
 
 
361
 
 
362
 
 
363
 
                private function addMapListeners():void
364
 
                {
365
 
                        stage.addEventListener(MouseEvent.MOUSE_DOWN,handleStageMouseEvent,false,0,true);
366
 
                        stage.addEventListener(MouseEvent.MOUSE_UP,handleStageMouseEvent,false,0,true);
367
 
                        stage.addEventListener(MouseEvent.MOUSE_MOVE,handleStageMouseEvent,true,0,true);
368
 
                }
369
 
                private function removeMapListeners():void
370
 
                {
371
 
                        stage.removeEventListener(MouseEvent.MOUSE_DOWN,handleStageMouseEvent);
372
 
                        stage.removeEventListener(MouseEvent.MOUSE_UP,handleStageMouseEvent);
373
 
                        stage.removeEventListener(MouseEvent.MOUSE_MOVE,handleStageMouseEvent);
374
 
                }
 
311
        private function initMap(e:Event):void
 
312
        {
 
313
            progBar.visible = false;
 
314
 
 
315
            try
 
316
            {
 
317
                _aamap = new Aamap(e.target.data);
 
318
            }
 
319
            catch(e)
 
320
            {
 
321
                Debug.log(e);
 
322
                return;
 
323
            }
 
324
 
 
325
            mapContainer.addChild(_aamap);
 
326
            _aamap.scaleY = -1;
 
327
 
 
328
            _aamap.x = _sW / 2;
 
329
            _aamap.y = _sH / 2;
 
330
 
 
331
            //addToolListeners();
 
332
            //addMapListeners();
 
333
            stage.addEventListener(MouseEvent.MOUSE_WHEEL,zoom,false,0,true);
 
334
 
 
335
            toolBar.save.addEventListener(MouseEvent.CLICK,saveXml,false,0,true);
 
336
            toolBar.open.addEventListener(MouseEvent.CLICK,loadXml,false,0,true);
 
337
 
 
338
            grid.size = new Point(10,10);
 
339
            //grid.visible = false;
 
340
            grid.render(_aamap);
 
341
            setInfo();
 
342
        }
 
343
 
 
344
 
 
345
        private function removeSelected(e:MouseEvent):void
 
346
        {
 
347
            toolBar.tools.select.removeSelected();
 
348
        }
 
349
 
 
350
        private function saveXml(e:MouseEvent):void
 
351
        {
 
352
            Debug.clear();
 
353
            Debug.log('\n' + _aamap.xml);
 
354
        }
 
355
 
 
356
        private function loadXml(e:MouseEvent):void
 
357
        {
 
358
 
 
359
        }
 
360
 
 
361
 
 
362
 
 
363
        private function addMapListeners():void
 
364
        {
 
365
            stage.addEventListener(MouseEvent.MOUSE_DOWN,handleStageMouseEvent,false,0,true);
 
366
            stage.addEventListener(MouseEvent.MOUSE_UP,handleStageMouseEvent,false,0,true);
 
367
            stage.addEventListener(MouseEvent.MOUSE_MOVE,handleStageMouseEvent,true,0,true);
 
368
        }
 
369
        private function removeMapListeners():void
 
370
        {
 
371
            stage.removeEventListener(MouseEvent.MOUSE_DOWN,handleStageMouseEvent);
 
372
            stage.removeEventListener(MouseEvent.MOUSE_UP,handleStageMouseEvent);
 
373
            stage.removeEventListener(MouseEvent.MOUSE_MOVE,handleStageMouseEvent);
 
374
        }
375
375
 
376
376
 
377
377
 
378
378
 
379
379
/* editing */
380
380
 
381
 
                private function handleStageMouseEvent(e:MouseEvent):void
382
 
                {
383
 
                        Debug.log(e.target);
384
 
 
385
 
                        // drag map
386
 
                        if(e.ctrlKey && e.type != MouseEvent.MOUSE_MOVE && !_tool.objectDragging || _draggingMap)
387
 
                        {
388
 
                                switch(e.type)
389
 
                                {
390
 
                                        case MouseEvent.MOUSE_DOWN:
391
 
                                                _aamap.startDrag();
392
 
                                                _draggingMap = true;
393
 
                                                _pointer.visible = false;
394
 
                                                break;
395
 
 
396
 
                                        case MouseEvent.MOUSE_UP:
397
 
                                                _aamap.stopDrag();
398
 
                                                _draggingMap = false;
399
 
                                                break;
400
 
 
401
 
                                        case MouseEvent.MOUSE_MOVE:
402
 
                                                grid.render(_aamap);
403
 
                                                break;
404
 
                                }
405
 
                        }
406
 
 
407
 
                        else
408
 
                        {
409
 
                                if(e.type == MouseEvent.MOUSE_MOVE)
410
 
                                {
411
 
                                        pointer.x = stage.mouseX;
412
 
                                        pointer.y = stage.mouseY;
413
 
                                }
414
 
                                _tool.handleMouse(e,_aamap);
415
 
                        }
416
 
 
417
 
                        setInfo();
418
 
                }
419
 
 
420
 
                private function handleObjectMouseEvent(e:MouseEvent):void
421
 
                {
422
 
                        _tool.handleObjectMouseEvent(e,_cursor);
423
 
                }
424
 
 
425
 
                private function handleObjectHover(e:CustomEvent):void
426
 
                {
427
 
                        _tool.handleObjectMouseHover(e);
428
 
                }
429
 
 
430
 
 
431
 
                // KEYBOARD
432
 
                private function handleKeyboard(e:KeyboardEvent):void
433
 
                {
434
 
                        //Debug.log(e.keyCode);
435
 
                        var ch = e.keyCode;
436
 
 
437
 
                        switch(e.type)
438
 
                        {
439
 
                                case KeyboardEvent.KEY_DOWN:
440
 
                                        if(!_keyDown.find(ch))
441
 
                                                _keyDown.push(ch);
442
 
 
443
 
                                        if(!e.ctrlKey && !e.altKey && ! e.shiftKey)
444
 
                                                shortCuts(ch);
445
 
 
446
 
                                        break;
447
 
 
448
 
                                case KeyboardEvent.KEY_UP:
449
 
                                        if(_keyDown.find(ch))
450
 
                                                _keyDown.remove(ch);
451
 
 
452
 
                                        break;
453
 
                        }
454
 
                        _tool.handleKeyboard(_keyDown);
455
 
                }
456
 
 
457
 
                private function shortCuts(ch):void
458
 
                {
459
 
                        switch(ch)
460
 
                        {
461
 
                                case 65:        // A -> edit tool
462
 
                                        switchTool(toolBar.tools.edit);
463
 
                                        break;
464
 
 
465
 
                                case 83:        // S -> spawn tool
466
 
                                        switchTool(toolBar.tools.spawn);
467
 
                                        break;
468
 
 
469
 
                                case 86:        // V -> select tool
470
 
                                        switchTool(toolBar.tools.select);
471
 
                                        break;
472
 
 
473
 
                                case 87:        // W -> wall tool
474
 
                                        switchTool(toolBar.tools.wall);
475
 
                                        break;
476
 
 
477
 
                                case 90:        // Z -> zone tool
478
 
                                        switchTool(toolBar.tools.zone);
479
 
                                        break;
480
 
                        }
481
 
                }
 
381
        private function handleStageMouseEvent(e:MouseEvent):void
 
382
        {
 
383
            Debug.log(e.target);
 
384
 
 
385
            // drag map
 
386
            if(e.ctrlKey && e.type != MouseEvent.MOUSE_MOVE && !_tool.objectDragging || _draggingMap)
 
387
            {
 
388
                switch(e.type)
 
389
                {
 
390
                    case MouseEvent.MOUSE_DOWN:
 
391
                        _aamap.startDrag();
 
392
                        _draggingMap = true;
 
393
                        _pointer.visible = false;
 
394
                        break;
 
395
 
 
396
                    case MouseEvent.MOUSE_UP:
 
397
                        _aamap.stopDrag();
 
398
                        _draggingMap = false;
 
399
                        break;
 
400
 
 
401
                    case MouseEvent.MOUSE_MOVE:
 
402
                        grid.render(_aamap);
 
403
                        break;
 
404
                }
 
405
            }
 
406
 
 
407
            else
 
408
            {
 
409
                if(e.type == MouseEvent.MOUSE_MOVE)
 
410
                {
 
411
                    pointer.x = stage.mouseX;
 
412
                    pointer.y = stage.mouseY;
 
413
                }
 
414
                _tool.handleMouse(e,_aamap);
 
415
            }
 
416
 
 
417
            setInfo();
 
418
        }
 
419
 
 
420
        private function handleObjectMouseEvent(e:MouseEvent):void
 
421
        {
 
422
            _tool.handleObjectMouseEvent(e,_cursor);
 
423
        }
 
424
 
 
425
        private function handleObjectHover(e:CustomEvent):void
 
426
        {
 
427
            _tool.handleObjectMouseHover(e);
 
428
        }
 
429
 
 
430
 
 
431
        // KEYBOARD
 
432
        private function handleKeyboard(e:KeyboardEvent):void
 
433
        {
 
434
            //Debug.log(e.keyCode);
 
435
            var ch = e.keyCode;
 
436
 
 
437
            switch(e.type)
 
438
            {
 
439
                case KeyboardEvent.KEY_DOWN:
 
440
                    if(!_keyDown.find(ch))
 
441
                        _keyDown.push(ch);
 
442
 
 
443
                    if(!e.ctrlKey && !e.altKey && ! e.shiftKey)
 
444
                        shortCuts(ch);
 
445
 
 
446
                    break;
 
447
 
 
448
                case KeyboardEvent.KEY_UP:
 
449
                    if(_keyDown.find(ch))
 
450
                        _keyDown.remove(ch);
 
451
 
 
452
                    break;
 
453
            }
 
454
            _tool.handleKeyboard(_keyDown);
 
455
        }
 
456
 
 
457
        private function shortCuts(ch):void
 
458
        {
 
459
            switch(ch)
 
460
            {
 
461
                case 65:    // A -> edit tool
 
462
                    switchTool(toolBar.tools.edit);
 
463
                    break;
 
464
 
 
465
                case 83:    // S -> spawn tool
 
466
                    switchTool(toolBar.tools.spawn);
 
467
                    break;
 
468
 
 
469
                case 86:    // V -> select tool
 
470
                    switchTool(toolBar.tools.select);
 
471
                    break;
 
472
 
 
473
                case 87:    // W -> wall tool
 
474
                    switchTool(toolBar.tools.wall);
 
475
                    break;
 
476
 
 
477
                case 90:    // Z -> zone tool
 
478
                    switchTool(toolBar.tools.zone);
 
479
                    break;
 
480
            }
 
481
        }
482
482
 
483
483
 
484
484
 
485
485
/* tool listeners */
486
486
 
487
 
                private function addEditingObject(e:CustomEvent):void
488
 
                {
489
 
                        if(_aamap.editing.numChildren != 0)
490
 
                                error('editing is not empty');
491
 
                        else
492
 
                        {
493
 
                                _aamap.editing.addChild(e.data);
494
 
                        }
495
 
                }
496
 
                private function editingObjectComplete(e:Event):void
497
 
                {
498
 
                        if(_aamap.editing.numChildren == 0)
499
 
                                error('editing is empty');
500
 
 
501
 
                        else if(_aamap.editing.numChildren > 1)
502
 
                                error('editing contains more than 1 object');
503
 
 
504
 
                        else
505
 
                        {
506
 
                                var obj = _aamap.editing.getChildAt(0);
507
 
                                _aamap.addObject(obj);
508
 
                        }
509
 
                }
510
 
                private function removeEditingObject(e:Event):void
511
 
                {
512
 
                        if(_aamap.editing.numChildren == 0)
513
 
                                error('editing is empty');
514
 
 
515
 
                        else if(_aamap.editing.numChildren > 1)
516
 
                                error('editing contains more than 1 object');
517
 
 
518
 
                        else
519
 
                        {
520
 
                                var obj = _aamap.editing.getChildAt(0);
521
 
                                _aamap.editing.removeChild(obj);
522
 
                        }
523
 
                }
524
 
 
525
 
                private function objectDragStart(e:CustomEvent):void
526
 
                {
527
 
                        var obj = e.data as AamapObject;
528
 
                        obj.startDrag();
529
 
                }
530
 
 
531
 
                private function objectDragStop(e:CustomEvent):void
532
 
                {
533
 
                        var obj = e.data as AamapObject;
534
 
                        obj.stopDrag();
535
 
                }
 
487
        private function addEditingObject(e:CustomEvent):void
 
488
        {
 
489
            if(_aamap.editing.numChildren != 0)
 
490
                error('editing is not empty');
 
491
            else
 
492
            {
 
493
                _aamap.editing.addChild(e.data);
 
494
            }
 
495
        }
 
496
        private function editingObjectComplete(e:Event):void
 
497
        {
 
498
            if(_aamap.editing.numChildren == 0)
 
499
                error('editing is empty');
 
500
 
 
501
            else if(_aamap.editing.numChildren > 1)
 
502
                error('editing contains more than 1 object');
 
503
 
 
504
            else
 
505
            {
 
506
                var obj = _aamap.editing.getChildAt(0);
 
507
                _aamap.addObject(obj);
 
508
            }
 
509
        }
 
510
        private function removeEditingObject(e:Event):void
 
511
        {
 
512
            if(_aamap.editing.numChildren == 0)
 
513
                error('editing is empty');
 
514
 
 
515
            else if(_aamap.editing.numChildren > 1)
 
516
                error('editing contains more than 1 object');
 
517
 
 
518
            else
 
519
            {
 
520
                var obj = _aamap.editing.getChildAt(0);
 
521
                _aamap.editing.removeChild(obj);
 
522
            }
 
523
        }
 
524
 
 
525
        private function objectDragStart(e:CustomEvent):void
 
526
        {
 
527
            var obj = e.data as AamapObject;
 
528
            obj.startDrag();
 
529
        }
 
530
 
 
531
        private function objectDragStop(e:CustomEvent):void
 
532
        {
 
533
            var obj = e.data as AamapObject;
 
534
            obj.stopDrag();
 
535
        }
536
536
 
537
537
 
538
538
 
539
539
/* loading */
540
540
 
541
 
                // LOADING PROGRESS
542
 
                private function loadingProgress(e:ProgressEvent):void
543
 
                {
544
 
                        progBar.setProgress(e.bytesLoaded,e.bytesTotal);
545
 
                }
 
541
        // LOADING PROGRESS
 
542
        private function loadingProgress(e:ProgressEvent):void
 
543
        {
 
544
            progBar.setProgress(e.bytesLoaded,e.bytesTotal);
 
545
        }
546
546
 
547
547
 
548
548
/* resize */
549
549
 
550
 
                private function onStageResize(e:Event):void
551
 
                {
552
 
                        toolBar.x = _sW;
 
550
        private function onStageResize(e:Event):void
 
551
        {
 
552
            toolBar.x = _sW;
553
553
 
554
 
                        if(progBar.visible)
555
 
                        {
556
 
                                progBar.x = _sW / 2 - progBar.width / 2;
557
 
                                progBar.y = _sH / 2 - progBar.height / 2;
558
 
                        }
559
 
                }
560
 
        }
 
554
            if(progBar.visible)
 
555
            {
 
556
                progBar.x = _sW / 2 - progBar.width / 2;
 
557
                progBar.y = _sH / 2 - progBar.height / 2;
 
558
            }
 
559
        }
 
560
    }
561
561
}
 
 
b'\\ No newline at end of file'