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