Merge pull request #41 from stevage/pedestrian-crossings
[potlatch2.git] / net / systemeD / potlatch2 / Toolbox.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:Panel
3     xmlns:fx="http://ns.adobe.com/mxml/2009"
4         xmlns:mx="library://ns.adobe.com/flex/mx"
5         xmlns:s="library://ns.adobe.com/flex/spark"
6         xmlns:potlatch2="net.systemeD.potlatch2.*"
7         height="76" width="129" layout="absolute"
8         styleName="theToolBox">
9
10                 <mx:Image data="@Embed('../../../embedded/close_small.png')"
11                         includeInLayout="false" id="toolboxClose" click="toggle();" 
12                         y="-6" x="115" />
13
14                 <!-- Top row -->
15
16                 <s:Button icon="@Embed('../../../embedded/delete.svg')"
17                         click='doDelete(event.ctrlKey,event.shiftKey);' 
18                         enabled="{canDo('delete')}" 
19                         alpha="{getAlpha('delete')}" 
20                         toolTip="{deleteToolTipText()}" 
21                         width="28" height="28" y="4" x="6" />
22
23                 <mx:HBox id="reverseButton" width="28" height="28" y="4" x="36" borderStyle="none"
24                            horizontalAlign="center" verticalAlign="middle" toolTip="Reverse direction (V)">
25             <mx:ViewStack id="rotateButtonStack" creationPolicy="all">
26                 <mx:HBox id="arrowBoxWrapper">
27                                         <s:NavigatorContent width="28" height="28">
28                                                 <s:Button id="arrow" icon="{Arrow}" width="28" height="28" alpha="{getAlpha('reverseDirection')}" click="reverseClicked();"/>
29                                         </s:NavigatorContent>
30                 </mx:HBox>
31                 <mx:HBox id="clockwiseBox" horizontalAlign="center" verticalAlign="middle">
32                     <s:Button id="clockwise" icon="@Embed('../../../embedded/clockwise.svg')"
33                         alpha="{getAlpha('reverseDirection')}"
34                         width="28" height="28" click="reverseClicked();" />
35                 </mx:HBox>
36                 <mx:HBox id="antiClockwiseBox" horizontalAlign="center" verticalAlign="middle">
37                     <s:Button id="anticlockwise" icon="@Embed('../../../embedded/anti-clockwise.svg')"
38                         enabled="{canDo('reverseDirection')}"
39                         alpha="{getAlpha('reverseDirection')}"
40                         width="28" height="28" click="reverseClicked();" />
41                 </mx:HBox>
42             </mx:ViewStack>
43         </mx:HBox>
44                 <s:Button icon="@Embed('../../../embedded/cut.svg')"
45                         id="splitButton"
46                         click='doSplit();'
47                         enabled="{canDo('split')}" 
48                         alpha="{getAlpha('split')}" 
49                         toolTip="Split way (X)" 
50                         width="28" height="28" y="4" x="66" />
51                 <s:Button 
52                         id="mergeButton"
53                         click='doMerge();'
54                         toolTip="Merge ways" 
55                         width="28" height="28" y="4" x="96" />
56
57                 <!-- Second row -->
58
59                 <s:Button icon="@Embed('../../../embedded/straighten.svg')"
60                         id="straightenButton"
61                         click='doStraighten();' 
62                         enabled="{canDo('straighten')}" 
63                         alpha="{getAlpha('straighten')}" 
64                         toolTip="Straighten way" 
65                         width="28" height="28" y="34" x="6" />
66                 <s:Button icon="@Embed('../../../embedded/circle.svg')"
67                         id="circulariseButton"
68                         click='doCircularise();' 
69                         enabled="{canDo('circularise')}" 
70                         alpha="{getAlpha('circularise')}" 
71                         toolTip="Make circular" 
72                         width="28" height="28" y="34" x="36" />
73                 <s:Button icon="@Embed('../../../embedded/quadrilateralise.svg')"
74                         id="quadrilateraliseButton"
75                         click='doQuadrilateralise();' 
76                         enabled="{canDo('quadrilateralise')}" 
77                         alpha="{getAlpha('quadrilateralise')}" 
78                         toolTip="Make right-angled (Q)"
79                         width="28" height="28" y="34" x="66" />
80                 <s:Button icon="@Embed('../../../embedded/parallel.svg')"
81                         id="parralleliseButton"
82                         click='doParallelise();' 
83                         enabled="{canDo('parallelise')}" 
84                         alpha="{getAlpha('parallelise')}" 
85                         toolTip="Create parallel way (P)" 
86                         width="28" height="28" y="34" x="96" />
87
88         <fx:Script><![CDATA[
89
90                 import flash.events.Event;
91                 import flash.events.MouseEvent;
92                 import net.systemeD.halcyon.connection.*;
93                 import net.systemeD.halcyon.connection.actions.*;
94                 import net.systemeD.potlatch2.controller.*;
95                 import net.systemeD.potlatch2.tools.*;
96                 import embedded.*;      // for FXG icons
97
98                 private var controller:EditController;
99
100         [Bindable]
101         public var angle:int=0;
102
103         public var deleteNode:String = "Delete Node (Delete)";
104         public var deleteArea:String = "Delete Area (Shift+Delete)";
105         public var deleteWay:String = "Delete Way (Shift+Delete)";
106         public var deleteItem:String = "Delete Item"; // When nothing is selected
107
108         [Bindable] [Embed(source="../../../embedded/merge.svg"       )] private var mergeIcon:Class;
109         [Bindable] [Embed(source="../../../embedded/multipolygon.svg")] private var multipolygonIcon:Class;
110
111                 public function init(controller:EditController):void {
112                         this.controller=controller;
113                         /* check if the toolbox was explictly turned off in a previous session */
114                         if( SharedObject.getLocal("user_state").data['toolbox_visible'] == false) {
115                           this.visible = false;
116                         }
117                 }
118
119                 override protected function createChildren():void {
120                         super.createChildren();
121                         super.titleBar.addEventListener(MouseEvent.MOUSE_DOWN,handleDown);
122                 }
123
124                 public function updateSelectionUI():void {
125                         dispatchEvent(new Event("updateSkin"));
126                         dispatchEvent(new Event("updateAlpha"));
127                         updateDirectionArrow();
128                         updateMergeIcon();
129                 }
130
131                 private function handleDown(e:Event):void {
132                         this.startDrag();
133                         stage.addEventListener(MouseEvent.MOUSE_UP,handleUp);
134                 }
135
136                 private function handleUp(e:Event):void {
137                         this.stopDrag();
138                         stage.removeEventListener(MouseEvent.MOUSE_UP,handleUp);
139                 }
140                 
141                 public function toggle():void {
142                         this.visible=!this.visible;
143                         var obj:SharedObject = SharedObject.getLocal("user_state");
144                         obj.setProperty("toolbox_visible",this.visible);
145                         obj.flush();
146                 }
147                 
148                 // --------------------------------------------------------------------------------
149                 // Enable/disable toolbox buttons
150                 // (ideally we'd use CSS to set alpha in disabled state, but Flex's CSS
151                 //      capabilities aren't up to it)
152                 
153                 [Bindable(event="updateSkin")]
154                 public function canDo(op:String):Boolean {
155                         if (controller.state.selectCount==0) return false;
156
157                         switch (op) {
158                                 case 'delete':                          return true;
159                                 case 'reverseDirection':        return controller.state.hasSelectedWays();
160                                 case 'quadrilateralise':        return (controller.state.hasSelectedAreas() || controller.state.hasSelectedWayNodesInAreas());
161                                 case 'straighten':                      return controller.state.hasSelectedUnclosedWays();
162                                 case 'circularise':                     return controller.state.hasSelectedAreas();
163                                 case 'split':                           return (controller.state is SelectedWayNode);
164                                 case 'parallelise':                     return (controller.state is SelectedWay);
165                         }
166                         return false;
167                 }
168
169                 [Bindable(event="updateAlpha")]
170                 public function getAlpha(op:String):Number {
171                         if (canDo(op)) { return 1; }
172                         return 0.5;
173                 }
174
175                 [Bindable(event="updateSkin")]
176                 private function deleteToolTipText():String {
177                         var entity:Entity=controller.state.firstSelected;
178                         if (entity is Node) { return deleteNode; }
179                         if (entity is Way && Way(entity).isArea()) { return deleteArea; }
180                         if (entity is Way) { return deleteWay; }
181                         return deleteItem; // When nothing is selected
182                 }
183
184         private function updateDirectionArrow():void {
185             if (controller.state is SelectedWay) {
186                 var w:Way = Way(controller.state.firstSelected);
187                 if (w) { // not entirely sure why this protection is necessary, but it appears so
188                     if (w.isArea()) {
189                         // so Way.clockwise appears to give wrong results. Patches welcome, I guess, but for now...
190                         w.clockwise? rotateButtonStack.selectedChild = antiClockwiseBox : rotateButtonStack.selectedChild = clockwiseBox;
191                     } else {
192                         rotateButtonStack.selectedChild = arrowBoxWrapper;
193                         arrow.iconDisplay.rotation=w.angle;
194                                                 arrow.iconDisplay.smooth=true;
195                     }
196                 }
197             }
198         }
199
200                 private function updateMergeIcon():void {
201                         var multi:Object=controller.state.multipolygonMembers();
202                         if (multi.outer) {
203                                 mergeButton.setStyle("icon",multipolygonIcon);
204                                 mergeButton.alpha=1;
205                                 mergeButton.enabled=true;
206                         } else if (controller.state.hasAdjoiningWays()) {
207                                 mergeButton.setStyle("icon",mergeIcon);
208                                 mergeButton.alpha=1;
209                                 mergeButton.enabled=true;
210                         } else {
211                                 mergeButton.setStyle("icon",mergeIcon);
212                                 mergeButton.alpha=0.5;
213                                 mergeButton.enabled=false;
214                         }
215                 }
216
217         private function reverseClicked():void {
218             if(canDo('reverseDirection')) {
219                 doReverseDirection();
220             }
221         }
222
223                 // --------------------------------------------------------------------------------
224                 // Individual toolbox actions
225
226                 public function doDelete(controlKey:Boolean,shiftKey:Boolean):void {
227                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Delete objects");
228                         if (controlKey) {
229                                 // Temporary function to delete everything with non-accepting status
230                                 var conn:Connection=controller.state.selection[0].connection;
231                                 var o:Object=conn.getObjectsByBbox(controller.map.edge_l, controller.map.edge_r, controller.map.edge_t, controller.map.edge_b);
232                                 for each (var way:Way in o.waysInside) {
233                                         if (way.status=='no' || (shiftKey && way.status=='partial')) { way.remove(undo.push); }
234                                 }
235                                 for each (var poi:Node in o.poisInside) {
236                                         if (poi.status=='no' || (shiftKey && way.status=='partial')) { poi.connection.unregisterPOI(poi); poi.remove(undo.push); }
237                                 }
238                                 MainUndoStack.getGlobalStack().addAction(undo);
239                                 controller.setState(new NoSelection());
240                                 
241                         } else {
242                                 // Standard delete function
243                                 for each (var entity:Entity in controller.state.selection) {
244                                         if (entity is Node) { entity.connection.unregisterPOI(Node(entity)); }
245                                         entity.remove(undo.push);
246                                 }
247                                 MainUndoStack.getGlobalStack().addAction(undo);
248
249                                 if (controller.state is SelectedWayNode) {
250                                         controller.setState(new SelectedWay(SelectedWayNode(controller.state).selectedWay));
251                                 } else {
252                                         controller.setState(new NoSelection());
253                                 }
254                         }
255                 }
256                 
257                 public function doMerge():void {
258                         var multi:Object=controller.state.multipolygonMembers();
259                         if (multi.outer) {
260                                 controller.setState(SelectedMultiple(controller.state).createMultipolygon());
261                         } else {
262                                 controller.setState(SelectedMultiple(controller.state).mergeWays());
263                         }
264                 }
265
266                 public function doReverseDirection():void {
267                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Reverse direction of objects");
268                         for each (var way:Way in controller.state.selectedWays) {
269                                 way.reverseNodes(undo.push);
270                         }
271                         MainUndoStack.getGlobalStack().addAction(undo);
272                 }
273
274                 public function doQuadrilateralise():void {
275                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Make objects right-angled");
276                         for each (var way:Way in controller.state.selectedWays) {
277                                 Quadrilateralise.quadrilateralise(way, undo.push);
278                         }
279             for each (var node:Node in controller.state.selectedNodes) {
280                 for each (var parentWay:Way in node.parentWays) {
281                   Quadrilateralise.quadrilateralise(parentWay, undo.push);
282                 }
283             }
284                         MainUndoStack.getGlobalStack().addAction(undo);
285                 }
286
287                 public function doStraighten():void {
288                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Straighten objects");
289                         for each (var way:Way in controller.state.selectedWays) {
290                                 Straighten.straighten(way, controller.map, undo.push);
291                         }
292                         MainUndoStack.getGlobalStack().addAction(undo);
293                 }
294
295                 public function doCircularise():void {
296                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Make objects circular ");
297                         for each (var way:Way in controller.state.selectedWays) {
298                                 Circularise.circularise(way, controller.map, undo.push);
299                         }
300                         MainUndoStack.getGlobalStack().addAction(undo);
301                 }
302
303                 public function doSplit():void {
304                         if (controller.state is SelectedWayNode) {
305                                 controller.setState(SelectedWayNode(controller.state).splitWay());
306                         }
307                 }
308                 
309                 public function doParallelise():void {
310                         if (controller.state is SelectedWay) {
311                                 controller.setState(new SelectedParallelWay(Way(controller.state.firstSelected)));
312                         }
313                 }
314
315
316         ]]>
317 </fx:Script>
318 </mx:Panel>