fix backspacing issue on self-intersecting ways
[potlatch2.git] / net / systemeD / halcyon / MapPaint.as
1 package net.systemeD.halcyon {
2
3         import flash.display.Sprite;
4         import flash.display.DisplayObject;
5         import net.systemeD.halcyon.NodeUI;
6         import net.systemeD.halcyon.WayUI;
7         import net.systemeD.halcyon.connection.Node;
8         import net.systemeD.halcyon.connection.Way;
9         import net.systemeD.halcyon.styleparser.RuleSet;
10         import net.systemeD.halcyon.vectorlayers.VectorLayer;
11         import net.systemeD.halcyon.Globals;
12
13     public class MapPaint extends Sprite {
14
15                 public var map:Map;
16                 public var minlayer:int;
17                 public var maxlayer:int;
18                 public var ruleset:RuleSet;                                             // rules
19                 public var wayuis:Object=new Object();                  // sprites for ways and (POI/tagged) nodes
20                 public var nodeuis:Object=new Object();                 //  |
21                 public var isBackground:Boolean = true;                 // is it a background layer or the core paint object?
22                 public var sublayerIndex:Object={};                             // hash of index->position
23
24                 private const VERYBIG:Number=Math.pow(2,16);
25
26                 // Set up layering
27                 // [layer][3]                   - names
28                 // [layer][2][sublayer] - stroke
29                 // [layer][1]                   - casing
30                 // [layer][0]                   - fill
31
32                 public function MapPaint(map:Map,minlayer:int,maxlayer:int) {
33                         this.map=map;
34                         this.minlayer=minlayer;
35                         this.maxlayer=maxlayer;
36                         sublayerIndex[1]=0;
37
38                         for (var l:int=minlayer; l<=maxlayer; l++) {    // each layer (10 is +5, 0 is -5)
39                                 var s:Sprite = getHitSprite();                  //  |
40                                 s.addChild(getPaintSprite());                           //      | 0 fill
41                                 s.addChild(getPaintSprite());                           //      | 1 casing
42                                 var t:Sprite = getPaintSprite();                        //  | 2 stroke
43                                 t.addChild(getPaintSprite());                           //  |  | sublayer
44                                 s.addChild(t);                                                          //  |  |
45                                 s.addChild(getPaintSprite());                           //      | 3 names
46                                 s.addChild(getHitSprite());                                     //      | 4 way hit tests
47                                 s.addChild(getHitSprite());                                 //  | 5 node hit tests
48                                 addChild(s);                                                            //  |
49                         }
50                         addChild(getPaintSprite());                                     // name sprite
51                 }
52                 
53                 public function get ready():Boolean {
54                         if (!ruleset) { return false; }
55                         if (!ruleset.loaded) { return false; }
56                         return true;
57                 }
58
59                 public function sublayer(layer:int,sublayer:Number):Sprite {
60                         var l:DisplayObject;
61                         var o:DisplayObject;
62                         var index:String, ix:Number;
63                         if (!sublayerIndex.hasOwnProperty(sublayer)) {
64                                 // work out which position to add at
65                                 var lowestAbove:Number=VERYBIG;
66                                 var lowestAbovePos:int=-1;
67                                 var indexLength:uint=0;
68                                 for (index in sublayerIndex) {
69                                         ix=Number(index);
70                                         if (ix>sublayer && ix<lowestAbove) {
71                                                 lowestAbove=ix;
72                                                 lowestAbovePos=sublayerIndex[index];
73                                         }
74                                         indexLength++;
75                                 }
76                                 if (lowestAbovePos==-1) { lowestAbovePos=indexLength; }
77                         
78                                 // add sprites
79                                 for (var i:int=minlayer; i<=maxlayer; i++) {
80                                         l=getChildAt(i-minlayer);
81                                         o=(l as Sprite).getChildAt(2);
82                                         (o as Sprite).addChildAt(getPaintSprite(),lowestAbovePos);
83                                 }
84                         
85                                 // update index
86                                 // (we do it in this rather indirect way because if you alter sublayerIndex directly
87                                 //  within the loop, it confuses the iterator)
88                                 var toUpdate:Array=[];
89                                 for (index in sublayerIndex) {
90                                         ix=Number(index);
91                                         if (ix>sublayer) { toUpdate.push(index); }
92                                 }
93                                 for each (index in toUpdate) { sublayerIndex[index]++; }
94                                 sublayerIndex[sublayer]=lowestAbovePos;
95                         }
96
97                         l=getChildAt(layer-minlayer);
98                         o=(l as Sprite).getChildAt(2);
99                         return ((o as Sprite).getChildAt(sublayerIndex[sublayer]) as Sprite);
100                 }
101
102                 public function updateEntityUIs(o:Object, redraw:Boolean, remove:Boolean):void {
103                         var way:Way, node:Node;
104
105                         for each (way in o.waysInside) {
106                                 if (!wayuis[way.id]) { createWayUI(way); }
107                                 else if (redraw) { wayuis[way.id].recalculate(); wayuis[way.id].redraw(); }
108                         }
109                         if (remove) {
110                                 for each (way in o.waysOutside) { deleteWayUI(way); }
111                         }
112
113                         for each (node in o.poisInside) {
114                                 if (!nodeuis[node.id]) { createNodeUI(node); }
115                                 else if (redraw) { nodeuis[node.id].redraw(); }
116                         }
117                         if (remove) {
118                                 for each (node in o.poisOutside) { deleteNodeUI(node); }
119                         }
120                 }
121
122                 public function createWayUI(way:Way):WayUI {
123                         if (!wayuis[way.id]) { wayuis[way.id]=new WayUI(way,this); }
124                         return wayuis[way.id];
125                 }
126
127                 public function createNodeUI(node:Node):NodeUI {
128                         if (!nodeuis[node.id]) { nodeuis[node.id]=new NodeUI(node,this,0); }
129                         return nodeuis[node.id];
130                 }
131
132                 public function deleteWayUI(way:Way):void {
133                         if (!wayuis[way.id]) { return; }
134                         wayuis[way.id].removeSprites();
135                         delete wayuis[way.id];
136                 }
137
138                 public function deleteNodeUI(node:Node):void {
139                         if (!nodeuis[node.id]) { return; }
140                         nodeuis[node.id].removeSprites();
141                         delete nodeuis[node.id];
142                 }
143                 
144                 public function renumberWayUI(way:Way,oldID:Number):void {
145                         if (!wayuis[oldID]) { return; }
146                         wayuis[way.id]=wayuis[oldID];
147                         delete wayuis[oldID];
148                 }
149
150                 public function renumberNodeUI(node:Node,oldID:Number):void {
151                         if (!nodeuis[oldID]) { return; }
152                         nodeuis[node.id]=nodeuis[oldID];
153                         delete nodeuis[oldID];
154                 }
155
156         private function getPaintSprite():Sprite {
157             var s:Sprite = new Sprite();
158             s.mouseEnabled = false;
159             s.mouseChildren = false;
160             return s;
161         }
162
163         private function getHitSprite():Sprite {
164             var s:Sprite = new Sprite();
165             return s;
166         }
167
168                 public function redraw():void {
169                         for each (var w:WayUI in wayuis) { w.recalculate(); w.redraw(); }
170                         for each (var p:NodeUI in nodeuis) { p.redraw(); }
171                 }
172
173                 public function redrawPOIs():void {
174                         for each (var p:NodeUI in nodeuis) { p.redraw(); }
175                 }
176                 
177                 public function findSource():VectorLayer {
178                         var v:VectorLayer;
179                         for each (v in map.vectorlayers) {
180                                 if (v.paint==this) { return v; }
181                         }
182                         return null;
183                 }
184         }
185 }