Add user_check config option for deletion warning dialogues
[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="56" width="129" layout="absolute" verticalScrollPolicy="off"
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                 <!-- Show more/less -->
58
59                 <s:Button 
60                         id="expandButton"
61                         click="expand();"
62                         label="Show more" baselineShift="1"
63                         width="118" height="12" y="34" x="6" />
64
65                 <!-- Second row -->
66
67                 <s:Button icon="@Embed('../../../embedded/straighten.svg')"
68                         id="straightenButton"
69                         click='doStraighten();' 
70                         enabled="{canDo('straighten')}" 
71                         alpha="{getAlpha('straighten')}" 
72                         toolTip="Straighten way" 
73                         width="28" height="28" y="48" x="6" />
74                 <s:Button icon="@Embed('../../../embedded/circle.svg')"
75                         id="circulariseButton"
76                         click='doCircularise();' 
77                         enabled="{canDo('circularise')}" 
78                         alpha="{getAlpha('circularise')}" 
79                         toolTip="Make circular" 
80                         width="28" height="28" y="48" x="36" />
81                 <s:Button icon="@Embed('../../../embedded/quadrilateralise.svg')"
82                         id="quadrilateraliseButton"
83                         click='doQuadrilateralise();' 
84                         enabled="{canDo('quadrilateralise')}" 
85                         alpha="{getAlpha('quadrilateralise')}" 
86                         toolTip="Make right-angled (Q)"
87                         width="28" height="28" y="48" x="66" />
88                 <s:Button icon="@Embed('../../../embedded/parallel.svg')"
89                         id="parralleliseButton"
90                         click='doParallelise();' 
91                         enabled="{canDo('parallelise')}" 
92                         alpha="{getAlpha('parallelise')}" 
93                         toolTip="Create parallel way (P)" 
94                         width="28" height="28" y="48" x="96" />
95
96         <fx:Script><![CDATA[
97
98                 import flash.events.Event;
99                 import flash.events.MouseEvent;
100                 import net.systemeD.halcyon.AttentionEvent;
101                 import net.systemeD.halcyon.connection.*;
102                 import net.systemeD.halcyon.connection.actions.*;
103                 import net.systemeD.potlatch2.controller.*;
104                 import net.systemeD.potlatch2.tools.*;
105                 import embedded.*;      // for FXG icons
106
107                 private var controller:EditController;
108
109         [Bindable]
110         public var angle:int=0;
111
112         public var deleteNode:String = "Delete Node (Delete)";
113         public var deleteArea:String = "Delete Area (Shift+Delete)";
114         public var deleteWay:String = "Delete Way (Shift+Delete)";
115         public var deleteItem:String = "Delete Item"; // When nothing is selected
116
117         [Bindable] [Embed(source="../../../embedded/merge.svg"       )] private var mergeIcon:Class;
118         [Bindable] [Embed(source="../../../embedded/multipolygon.svg")] private var multipolygonIcon:Class;
119
120                 public function init(controller:EditController):void {
121                         this.controller=controller;
122                         /* check if the toolbox was explictly turned off in a previous session */
123                         if( SharedObject.getLocal("user_state","/").data['toolbox_visible'] == false) {
124                           this.visible = false;
125                         }
126                 }
127
128                 override protected function createChildren():void {
129                         super.createChildren();
130                         super.titleBar.addEventListener(MouseEvent.MOUSE_DOWN,handleDown);
131                 }
132
133                 public function updateSelectionUI():void {
134                         dispatchEvent(new Event("updateSkin"));
135                         dispatchEvent(new Event("updateAlpha"));
136                         updateDirectionArrow();
137                         updateMergeIcon();
138                 }
139
140                 private function handleDown(e:Event):void {
141                         this.startDrag();
142                         stage.addEventListener(MouseEvent.MOUSE_UP,handleUp);
143                 }
144
145                 private function handleUp(e:Event):void {
146                         this.stopDrag();
147                         stage.removeEventListener(MouseEvent.MOUSE_UP,handleUp);
148                 }
149                 
150                 public function toggle():void {
151                         this.visible=!this.visible;
152                         var obj:SharedObject = SharedObject.getLocal("user_state","/");
153                         obj.setProperty("toolbox_visible",this.visible);
154                         try { obj.flush(); } catch (e:Error) {}
155                 }
156                 
157                 public function expand():void {
158                         if (height==56) {
159                                 height+=34;
160                                 y=Math.min(y,stage.stageHeight-height-10);
161                                 expandButton.label="Show less"; // ** FIXME: make translatable
162                         } else {
163                                 height-=34;
164                                 expandButton.label="Show more";
165                         }
166                 }
167                 
168                 // --------------------------------------------------------------------------------
169                 // Enable/disable toolbox buttons
170                 // (ideally we'd use CSS to set alpha in disabled state, but Flex's CSS
171                 //      capabilities aren't up to it)
172                 
173                 [Bindable(event="updateSkin")]
174                 public function canDo(op:String):Boolean {
175                         if (controller.state.selectCount==0) return false;
176
177                         switch (op) {
178                                 case 'delete':                          return true;
179                                 case 'reverseDirection':        return controller.state.hasSelectedWays();
180                                 case 'quadrilateralise':        return (controller.state.hasSelectedAreas() || controller.state.hasSelectedWayNodesInAreas());
181                                 case 'straighten':                      return controller.state.hasSelectedUnclosedWays();
182                                 case 'circularise':                     return controller.state.hasSelectedAreas();
183                                 case 'split':                           return (controller.state is SelectedWayNode);
184                                 case 'parallelise':                     return (controller.state is SelectedWay);
185                         }
186                         return false;
187                 }
188
189                 [Bindable(event="updateAlpha")]
190                 public function getAlpha(op:String):Number {
191                         if (canDo(op)) { return 1; }
192                         return 0.5;
193                 }
194
195                 [Bindable(event="updateSkin")]
196                 private function deleteToolTipText():String {
197                         var entity:Entity=controller.state.firstSelected;
198                         if (entity is Node) { return deleteNode; }
199                         if (entity is Way && Way(entity).isArea()) { return deleteArea; }
200                         if (entity is Way) { return deleteWay; }
201                         return deleteItem; // When nothing is selected
202                 }
203
204         private function updateDirectionArrow():void {
205             if (controller.state is SelectedWay) {
206                 var w:Way = Way(controller.state.firstSelected);
207                 if (w) { // not entirely sure why this protection is necessary, but it appears so
208                     if (w.isArea()) {
209                         // so Way.clockwise appears to give wrong results. Patches welcome, I guess, but for now...
210                         w.clockwise? rotateButtonStack.selectedChild = antiClockwiseBox : rotateButtonStack.selectedChild = clockwiseBox;
211                     } else {
212                         rotateButtonStack.selectedChild = arrowBoxWrapper;
213                         arrow.iconDisplay.rotation=w.angle;
214                                                 arrow.iconDisplay.smooth=true;
215                     }
216                 }
217             }
218         }
219
220                 private function updateMergeIcon():void {
221                         var multi:Object=controller.state.multipolygonMembers();
222                         if (multi.outer) {
223                                 mergeButton.setStyle("icon",multipolygonIcon);
224                                 mergeButton.alpha=1;
225                                 mergeButton.enabled=true;
226                         } else if (controller.state.hasAdjoiningWays()) {
227                                 mergeButton.setStyle("icon",mergeIcon);
228                                 mergeButton.alpha=1;
229                                 mergeButton.enabled=true;
230                         } else {
231                                 mergeButton.setStyle("icon",mergeIcon);
232                                 mergeButton.alpha=0.5;
233                                 mergeButton.enabled=false;
234                         }
235                 }
236
237         private function reverseClicked():void {
238             if(canDo('reverseDirection')) {
239                 doReverseDirection();
240             }
241         }
242
243                 // --------------------------------------------------------------------------------
244                 // Individual toolbox actions
245
246                 public function doDelete(controlKey:Boolean,shiftKey:Boolean):void {
247                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Delete objects");
248                         if (controlKey) {
249                                 // Temporary function to delete everything with non-accepting status
250                                 var conn:Connection=controller.state.selection[0].connection;
251                                 var o:Object=conn.getObjectsByBbox(controller.map.edge_l, controller.map.edge_r, controller.map.edge_t, controller.map.edge_b);
252                                 for each (var way:Way in o.waysInside) {
253                                         if (way.status=='no' || (shiftKey && way.status=='partial')) { way.remove(undo.push); }
254                                 }
255                                 for each (var poi:Node in o.poisInside) {
256                                         if (poi.status=='no' || (shiftKey && way.status=='partial')) { poi.connection.unregisterPOI(poi); poi.remove(undo.push); }
257                                 }
258                                 MainUndoStack.getGlobalStack().addAction(undo);
259                                 controller.setState(new NoSelection());
260                                 
261                         } else {
262                                 // Standard delete function
263                                 for each (var entity:Entity in controller.state.selection) {
264                                         if (entity is Node) { entity.connection.unregisterPOI(Node(entity)); }
265                                         entity.remove(undo.push);
266                                 }
267                                 MainUndoStack.getGlobalStack().addAction(undo);
268
269                                 if (controller.state is SelectedWayNode) {
270                                         controller.setState(new SelectedWay(SelectedWayNode(controller.state).selectedWay));
271                                 } else {
272                                         controller.setState(new NoSelection());
273                                 }
274                         }
275                 }
276                 
277                 public function doMerge():void {
278                         var multi:Object=controller.state.multipolygonMembers();
279                         if (multi.outer) {
280                                 controller.setState(SelectedMultiple(controller.state).createMultipolygon());
281                         } else {
282                                 controller.setState(SelectedMultiple(controller.state).mergeWays());
283                         }
284                 }
285
286                 public function doReverseDirection():void {
287                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Reverse direction of objects");
288                         for each (var way:Way in controller.state.selectedWays) {
289                                 way.reverseNodes(undo.push);
290                         }
291                         MainUndoStack.getGlobalStack().addAction(undo);
292                 }
293
294                 public function doQuadrilateralise():void {
295                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Make objects right-angled");
296                         for each (var way:Way in controller.state.selectedWays) {
297                                 Quadrilateralise.quadrilateralise(way, undo.push);
298                         }
299             for each (var node:Node in controller.state.selectedNodes) {
300                 for each (var parentWay:Way in node.parentWays) {
301                   Quadrilateralise.quadrilateralise(parentWay, undo.push);
302                 }
303             }
304                         MainUndoStack.getGlobalStack().addAction(undo);
305                 }
306
307                 public function doStraighten():void {
308                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Straighten objects");
309                         var success:Boolean=true;
310                         for each (var way:Way in controller.state.selectedWays) {
311                                 success &&= Straighten.straighten(way, controller.map, undo.push);
312                         }
313                         if (!success) controller.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, "Too bendy to straighten"));
314                         if (!undo.empty) MainUndoStack.getGlobalStack().addAction(undo);
315                 }
316
317                 public function doCircularise():void {
318                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Make objects circular ");
319                         for each (var way:Way in controller.state.selectedWays) {
320                                 Circularise.circularise(way, controller.map, undo.push);
321                         }
322                         MainUndoStack.getGlobalStack().addAction(undo);
323                 }
324
325                 public function doSplit():void {
326                         if (controller.state is SelectedWayNode) {
327                                 controller.setState(SelectedWayNode(controller.state).splitWay());
328                         }
329                 }
330                 
331                 public function doParallelise():void {
332                         if (controller.state is SelectedWay) {
333                                 controller.setState(new SelectedParallelWay(Way(controller.state.firstSelected)));
334                         }
335                 }
336
337
338         ]]>
339 </fx:Script>
340 </mx:Panel>