Move reverse tooltip to outer hbox, so that the whole hbox becomes the target for...
[potlatch2.git] / net / systemeD / potlatch2 / Toolbox.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:Panel
3         xmlns:mx="http://www.adobe.com/2006/mxml"
4         xmlns:potlatch2="net.systemeD.potlatch2.*"
5         height="76" width="129" layout="absolute"
6         styleName="theToolBox">
7
8         <!-- the animation effect that controls the rotation of the reverse arrow.
9               We could get more fancy by using previous angle in angleFrom, and a longer duration, to give a nice animated effect -->
10         <mx:Rotate id="rotate" angleFrom="{angle-1}" angleTo="{angle}" target="{arrowBox}" originX="{arrowBox.width/2}" originY="{arrowBox.height/2}" duration="4"/>
11
12
13                 <mx:Image data="@Embed('../../../embedded/close_small.png')"
14                         includeInLayout="false" id="toolboxClose" click="toggle();" 
15                         y="-6" x="115" />
16
17                 <!-- Top row -->
18
19                 <mx:Button icon="@Embed('../../../embedded/delete.svg')"
20                         click='doDelete();' 
21                         enabled="{canDo('delete')}" 
22                         alpha="{getAlpha('delete')}" 
23                         toolTip="{deleteToolTipText()}" 
24                         width="28" height="28" textAlign="left" y="4" x="6" paddingLeft="6" paddingRight="0" />
25
26         <mx:HBox id="reverseButton" width="28" height="28" y="4" x="36" borderStyle="solid" cornerRadius="4"
27                   click="reverseClicked();" horizontalAlign="center" verticalAlign="middle" toolTip="Reverse direction (V)">
28             <mx:ViewStack id="rotateButtonStack" creationPolicy="all">
29                 <mx:HBox id="arrowBoxWrapper"><!-- changing the viewstack back onto a rotated hbox causes positioning glitches, hence this wrapper -->
30                     <!-- I can totally recommend adding borderStyle="solid" to arrowBox when debugging -->
31                     <mx:HBox id="arrowBox" horizontalAlign="center" verticalAlign="middle" width="24" height="24">
32                         <mx:Image id="arrow" source="@Embed('../../../embedded/arrow.svg')"
33                             alpha="{getAlpha('reverseDirection')}"
34                             width="22" height="22"/>
35                     </mx:HBox>
36                 </mx:HBox>
37                 <mx:HBox id="clockwiseBox" horizontalAlign="center" verticalAlign="middle">
38                     <mx:Image id="clockwise" source="@Embed('../../../embedded/clockwise.svg')"
39                         alpha="{getAlpha('reverseDirection')}"
40                         width="22" height="22" x="2" y="2"/>
41                 </mx:HBox>
42                 <mx:HBox id="antiClockwiseBox" horizontalAlign="center" verticalAlign="middle">
43                     <mx:Image id="anticlockwise" source="@Embed('../../../embedded/anti-clockwise.svg')"
44                         click='doReverseDirection();'
45                         enabled="{canDo('reverseDirection')}"
46                         alpha="{getAlpha('reverseDirection')}"
47                         width="22" height="22" x="2" y="2"/>
48                 </mx:HBox>
49             </mx:ViewStack>
50         </mx:HBox>
51                 <mx:Button icon="@Embed('../../../embedded/cut.svg')" 
52                         click='doSplit();'
53                         enabled="{canDo('split')}" 
54                         alpha="{getAlpha('split')}" 
55                         toolTip="Split way (X)" 
56                         width="28" height="28" textAlign="left" y="4" x="66" paddingLeft="8" paddingRight="0" />
57                 <mx:Button icon="@Embed('../../../embedded/merge.svg')" 
58                         click='doMerge();'
59                         enabled="{canDo('merge')}" 
60                         alpha="{getAlpha('merge')}" 
61                         toolTip="Merge ways" 
62                         width="28" height="28" textAlign="left" y="4" x="96" paddingLeft="3" paddingRight="0" />
63
64                 <!-- Second row -->
65
66                 <mx:Button icon="@Embed('../../../embedded/straighten.svg')" 
67                         click='doStraighten();' 
68                         enabled="{canDo('straighten')}" 
69                         alpha="{getAlpha('straighten')}" 
70                         toolTip="Straighten way" 
71                         width="28" height="28" textAlign="left" y="34" x="6" paddingLeft="5" paddingRight="0" />
72                 <mx:Button icon="@Embed('../../../embedded/circle.svg')" 
73                         click='doCircularise();' 
74                         enabled="{canDo('circularise')}" 
75                         alpha="{getAlpha('circularise')}" 
76                         toolTip="Make circular" 
77                         width="28" height="28" textAlign="left" y="34" x="36" paddingLeft="4" paddingRight="0" />
78                 <mx:Button icon="@Embed('../../../embedded/quadrilateralise.svg')" 
79                         click='doQuadrilateralise();' 
80                         enabled="{canDo('quadrilateralise')}" 
81                         alpha="{getAlpha('quadrilateralise')}" 
82                         toolTip="Make right-angled (Q)"
83                         width="28" height="28" textAlign="left" y="34" x="66" paddingLeft="6" paddingRight="0" />
84                 <mx:Button icon="@Embed('../../../embedded/parallel.svg')" 
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         <mx: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
100                 private var controller:EditController;
101
102         [Bindable]
103         public var angle:int=0;
104
105         public var deleteNode:String = "Delete Node (Delete)";
106         public var deleteArea:String = "Delete Area (Shift+Delete)";
107         public var deleteWay:String = "Delete Way (Shift+Delete)";
108         public var deleteItem:String = "Delete Item"; // When nothing is selected
109
110                 public function init(controller:EditController):void {
111                         this.controller=controller;
112                         /* check if the toolbox was explictly turned off in a previous session */
113                         if( SharedObject.getLocal("user_state").data['toolbox_visible'] == false) {
114                           this.visible = false;
115                         }
116                 }
117
118                 override protected function createChildren():void {
119                         super.createChildren();
120                         super.titleBar.addEventListener(MouseEvent.MOUSE_DOWN,handleDown);
121                         super.titleBar.addEventListener(MouseEvent.MOUSE_UP,handleUp);
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                 }
133
134                 private function handleUp(e:Event):void {
135                         this.stopDrag();
136                 }
137                 
138                 public function toggle():void {
139                         this.visible=!this.visible;
140                         var obj:SharedObject = SharedObject.getLocal("user_state");
141                         obj.setProperty("toolbox_visible",this.visible);
142                         obj.flush();
143                 }
144                 
145                 // --------------------------------------------------------------------------------
146                 // Enable/disable toolbox buttons
147                 // (ideally we'd use CSS to set alpha in disabled state, but Flex's CSS
148                 //      capabilities aren't up to it)
149                 
150                 [Bindable(event="updateSkin")]
151                 public function canDo(op:String):Boolean {
152                         if (controller.state.selectCount==0) return false;
153
154                         switch (op) {
155                                 case 'delete':                          return true;
156                                 case 'reverseDirection':        return controller.state.hasSelectedWays();
157                                 case 'quadrilateralise':        return (controller.state.hasSelectedAreas() || controller.state.hasSelectedWayNodesInAreas());
158                                 case 'straighten':                      return controller.state.hasSelectedUnclosedWays();
159                                 case 'circularise':                     return controller.state.hasSelectedAreas();
160                                 case 'split':                           return (controller.state is SelectedWayNode);
161                                 case 'parallelise':                     return (controller.state is SelectedWay);
162                                 case 'merge':                           return controller.state.hasAdjoiningWays();
163                         }
164                         return false;
165                 }
166
167                 [Bindable(event="updateAlpha")]
168                 public function getAlpha(op:String):Number {
169                         if (canDo(op)) { return 1; }
170                         return 0.5;
171                 }
172
173                 [Bindable(event="updateSkin")]
174                 private function deleteToolTipText():String {
175                         var entity:Entity=controller.state.firstSelected;
176                         if (entity is Node) { return deleteNode; }
177                         if (entity is Way && Way(entity).isArea()) { return deleteArea; }
178                         if (entity is Way) { return deleteWay; }
179                         return deleteItem; // When nothing is selected
180                 }
181
182         private function updateDirectionArrow():void {
183             if (controller.state is SelectedWay) {
184                 var w:Way = Way(controller.state.firstSelected);
185                 if (w) { // not entirely sure why this protection is necessary, but it appears so
186                     if (w.isArea()) {
187                         // so Way.clockwise appears to give wrong results. Patches welcome, I guess, but for now...
188                         w.clockwise? rotateButtonStack.selectedChild = antiClockwiseBox : rotateButtonStack.selectedChild = clockwiseBox;
189                     } else {
190                         rotateButtonStack.selectedChild = arrowBoxWrapper;
191                         // reset and reposition back to the starting point relative to its parent
192                         rotate.end();
193                         angle = 0;
194                         rotate.play();
195                         arrowBox.x = 0;
196                         arrowBox.y = 0;
197
198                         // move
199                         rotate.end();
200                         angle = w.angle;
201                         rotate.play();
202                     }
203                 }
204             }
205         }
206
207         private function reverseClicked():void {
208             if(canDo('reverseDirection')) {
209                 doReverseDirection();
210             }
211         }
212
213                 // --------------------------------------------------------------------------------
214                 // Individual toolbox actions
215
216                 public function doDelete():void {
217                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Delete objects");
218                         for each (var entity:Entity in controller.state.selection) {
219                                 if (entity is Node) { controller.connection.unregisterPOI(Node(entity)); }
220                                 entity.remove(undo.push);
221                         }
222                         MainUndoStack.getGlobalStack().addAction(undo);
223
224                         if (controller.state is SelectedWayNode) {
225                                 controller.setState(new SelectedWay(SelectedWayNode(controller.state).selectedWay));
226                         } else {
227                                 controller.setState(new NoSelection());
228                         }
229                 }
230                 
231                 public function doMerge():void {
232                         controller.setState(SelectedMultiple(controller.state).mergeWays());
233                 }
234
235                 public function doReverseDirection():void {
236                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Reverse direction of objects");
237                         for each (var way:Way in controller.state.selectedWays) {
238                                 way.reverseNodes(undo.push);
239                         }
240                         MainUndoStack.getGlobalStack().addAction(undo);
241                 }
242
243                 public function doQuadrilateralise():void {
244                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Make objects right-angled");
245                         for each (var way:Way in controller.state.selectedWays) {
246                                 Quadrilateralise.quadrilateralise(way, undo.push);
247                         }
248             for each (var node:Node in controller.state.selectedNodes) {
249                 for each (var parentWay:Way in node.parentWays) {
250                   Quadrilateralise.quadrilateralise(parentWay, undo.push);
251                 }
252             }
253                         MainUndoStack.getGlobalStack().addAction(undo);
254                 }
255
256                 public function doStraighten():void {
257                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Straighten objects");
258                         for each (var way:Way in controller.state.selectedWays) {
259                                 Straighten.straighten(way, controller.map, undo.push);
260                         }
261                         MainUndoStack.getGlobalStack().addAction(undo);
262                 }
263
264                 public function doCircularise():void {
265                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Make objects circular ");
266                         for each (var way:Way in controller.state.selectedWays) {
267                                 Circularise.circularise(way, controller.map, undo.push);
268                         }
269                         MainUndoStack.getGlobalStack().addAction(undo);
270                 }
271
272                 public function doSplit():void {
273                         if (controller.state is SelectedWayNode) {
274                                 controller.setState(SelectedWayNode(controller.state).splitWay());
275                         }
276                 }
277                 
278                 public function doParallelise():void {
279                         if (controller.state is SelectedWay) {
280                                 controller.setState(new SelectedParallelWay(Way(controller.state.firstSelected)));
281                         }
282                 }
283
284
285         ]]>
286 </mx:Script>    
287 </mx:Panel>