Make rotation arrow Flex 4-compatible.
[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                 <mx:Button icon="@Embed('../../../embedded/delete.svg')"
17                         click='doDelete();' 
18                         enabled="{canDo('delete')}" 
19                         alpha="{getAlpha('delete')}" 
20                         toolTip="{deleteToolTipText()}" 
21                         width="28" height="28" textAlign="left" y="4" x="6" paddingLeft="6" paddingRight="0" />
22
23                 <mx:HBox id="reverseButton" width="28" height="28" y="4" x="36" borderStyle="solid" cornerRadius="4"
24                           click="reverseClicked();" horizontalAlign="center" verticalAlign="middle" toolTip="Reverse direction (V)">
25             <mx:ViewStack id="rotateButtonStack" creationPolicy="all">
26                 <mx:HBox id="arrowBoxWrapper">
27                                         <s:NavigatorContent width="22" height="22">
28                                                 <s:Button id="arrow" icon="{Arrow}" width="22" height="22" alpha="{getAlpha('reverseDirection')}" />
29                                         </s:NavigatorContent>
30                 </mx:HBox>
31                 <mx:HBox id="clockwiseBox" horizontalAlign="center" verticalAlign="middle">
32                     <mx:Image id="clockwise" source="@Embed('../../../embedded/clockwise.svg')"
33                         alpha="{getAlpha('reverseDirection')}"
34                         width="22" height="22" x="2" y="2"/>
35                 </mx:HBox>
36                 <mx:HBox id="antiClockwiseBox" horizontalAlign="center" verticalAlign="middle">
37                     <mx:Image id="anticlockwise" source="@Embed('../../../embedded/anti-clockwise.svg')"
38                         click='doReverseDirection();'
39                         enabled="{canDo('reverseDirection')}"
40                         alpha="{getAlpha('reverseDirection')}"
41                         width="22" height="22" x="2" y="2"/>
42                 </mx:HBox>
43             </mx:ViewStack>
44         </mx:HBox>
45                 <mx:Button icon="@Embed('../../../embedded/cut.svg')" 
46                         id="splitButton"
47                         click='doSplit();'
48                         enabled="{canDo('split')}" 
49                         alpha="{getAlpha('split')}" 
50                         toolTip="Split way (X)" 
51                         width="28" height="28" textAlign="left" y="4" x="66" paddingLeft="8" paddingRight="0" />
52                 <mx:Button icon="@Embed('../../../embedded/merge.svg')" 
53                         id="mergeButton"
54                         click='doMerge();'
55                         enabled="{canDo('merge')}" 
56                         alpha="{getAlpha('merge')}" 
57                         toolTip="Merge ways" 
58                         width="28" height="28" textAlign="left" y="4" x="96" paddingLeft="3" paddingRight="0" />
59
60                 <!-- Second row -->
61
62                 <mx:Button icon="@Embed('../../../embedded/straighten.svg')" 
63                         id="straightenButton"
64                         click='doStraighten();' 
65                         enabled="{canDo('straighten')}" 
66                         alpha="{getAlpha('straighten')}" 
67                         toolTip="Straighten way" 
68                         width="28" height="28" textAlign="left" y="34" x="6" paddingLeft="5" paddingRight="0" />
69                 <mx:Button icon="@Embed('../../../embedded/circle.svg')" 
70                         id="circulariseButton"
71                         click='doCircularise();' 
72                         enabled="{canDo('circularise')}" 
73                         alpha="{getAlpha('circularise')}" 
74                         toolTip="Make circular" 
75                         width="28" height="28" textAlign="left" y="34" x="36" paddingLeft="4" paddingRight="0" />
76                 <mx:Button icon="@Embed('../../../embedded/quadrilateralise.svg')" 
77                         id="quadrilateraliseButton"
78                         click='doQuadrilateralise();' 
79                         enabled="{canDo('quadrilateralise')}" 
80                         alpha="{getAlpha('quadrilateralise')}" 
81                         toolTip="Make right-angled (Q)"
82                         width="28" height="28" textAlign="left" y="34" x="66" paddingLeft="6" paddingRight="0" />
83                 <mx:Button icon="@Embed('../../../embedded/parallel.svg')" 
84                         id="parralleliseButton"
85                         click='doParallelise();' 
86                         enabled="{canDo('parallelise')}" 
87                         alpha="{getAlpha('parallelise')}" 
88                         toolTip="Create parallel way (P)" 
89                         width="28" height="28" textAlign="left" y="34" x="96" paddingLeft="8" paddingRight="0" />
90
91         <fx:Script><![CDATA[
92
93                 import flash.events.Event;
94                 import flash.events.MouseEvent;
95                 import net.systemeD.halcyon.connection.*;
96                 import net.systemeD.halcyon.connection.actions.*;
97                 import net.systemeD.potlatch2.controller.*;
98                 import net.systemeD.potlatch2.tools.*;
99                 import embedded.*;      // for FXG icons
100
101                 private var controller:EditController;
102
103         [Bindable]
104         public var angle:int=0;
105
106         public var deleteNode:String = "Delete Node (Delete)";
107         public var deleteArea:String = "Delete Area (Shift+Delete)";
108         public var deleteWay:String = "Delete Way (Shift+Delete)";
109         public var deleteItem:String = "Delete Item"; // When nothing is selected
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                 }
129
130                 private function handleDown(e:Event):void {
131                         this.startDrag();
132                         stage.addEventListener(MouseEvent.MOUSE_UP,handleUp);
133                 }
134
135                 private function handleUp(e:Event):void {
136                         this.stopDrag();
137                         stage.removeEventListener(MouseEvent.MOUSE_UP,handleUp);
138                 }
139                 
140                 public function toggle():void {
141                         this.visible=!this.visible;
142                         var obj:SharedObject = SharedObject.getLocal("user_state");
143                         obj.setProperty("toolbox_visible",this.visible);
144                         obj.flush();
145                 }
146                 
147                 // --------------------------------------------------------------------------------
148                 // Enable/disable toolbox buttons
149                 // (ideally we'd use CSS to set alpha in disabled state, but Flex's CSS
150                 //      capabilities aren't up to it)
151                 
152                 [Bindable(event="updateSkin")]
153                 public function canDo(op:String):Boolean {
154                         if (controller.state.selectCount==0) return false;
155
156                         switch (op) {
157                                 case 'delete':                          return true;
158                                 case 'reverseDirection':        return controller.state.hasSelectedWays();
159                                 case 'quadrilateralise':        return (controller.state.hasSelectedAreas() || controller.state.hasSelectedWayNodesInAreas());
160                                 case 'straighten':                      return controller.state.hasSelectedUnclosedWays();
161                                 case 'circularise':                     return controller.state.hasSelectedAreas();
162                                 case 'split':                           return (controller.state is SelectedWayNode);
163                                 case 'parallelise':                     return (controller.state is SelectedWay);
164                                 case 'merge':                           return controller.state.hasAdjoiningWays();
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                     }
195                 }
196             }
197         }
198
199         private function reverseClicked():void {
200             if(canDo('reverseDirection')) {
201                 doReverseDirection();
202             }
203         }
204
205                 // --------------------------------------------------------------------------------
206                 // Individual toolbox actions
207
208                 public function doDelete():void {
209                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Delete objects");
210                         for each (var entity:Entity in controller.state.selection) {
211                                 if (entity is Node) { entity.connection.unregisterPOI(Node(entity)); }
212                                 entity.remove(undo.push);
213                         }
214                         MainUndoStack.getGlobalStack().addAction(undo);
215
216                         if (controller.state is SelectedWayNode) {
217                                 controller.setState(new SelectedWay(SelectedWayNode(controller.state).selectedWay));
218                         } else {
219                                 controller.setState(new NoSelection());
220                         }
221                 }
222                 
223                 public function doMerge():void {
224                         controller.setState(SelectedMultiple(controller.state).mergeWays());
225                 }
226
227                 public function doReverseDirection():void {
228                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Reverse direction of objects");
229                         for each (var way:Way in controller.state.selectedWays) {
230                                 way.reverseNodes(undo.push);
231                         }
232                         MainUndoStack.getGlobalStack().addAction(undo);
233                 }
234
235                 public function doQuadrilateralise():void {
236                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Make objects right-angled");
237                         for each (var way:Way in controller.state.selectedWays) {
238                                 Quadrilateralise.quadrilateralise(way, undo.push);
239                         }
240             for each (var node:Node in controller.state.selectedNodes) {
241                 for each (var parentWay:Way in node.parentWays) {
242                   Quadrilateralise.quadrilateralise(parentWay, undo.push);
243                 }
244             }
245                         MainUndoStack.getGlobalStack().addAction(undo);
246                 }
247
248                 public function doStraighten():void {
249                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Straighten objects");
250                         for each (var way:Way in controller.state.selectedWays) {
251                                 Straighten.straighten(way, controller.map, undo.push);
252                         }
253                         MainUndoStack.getGlobalStack().addAction(undo);
254                 }
255
256                 public function doCircularise():void {
257                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Make objects circular ");
258                         for each (var way:Way in controller.state.selectedWays) {
259                                 Circularise.circularise(way, controller.map, undo.push);
260                         }
261                         MainUndoStack.getGlobalStack().addAction(undo);
262                 }
263
264                 public function doSplit():void {
265                         if (controller.state is SelectedWayNode) {
266                                 controller.setState(SelectedWayNode(controller.state).splitWay());
267                         }
268                 }
269                 
270                 public function doParallelise():void {
271                         if (controller.state is SelectedWay) {
272                                 controller.setState(new SelectedParallelWay(Way(controller.state.firstSelected)));
273                         }
274                 }
275
276
277         ]]>
278 </fx:Script>
279 </mx:Panel>