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