move mouse-up listener to the stage, should fix #3410
[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                 }
119
120                 public function updateSelectionUI():void {
121                         dispatchEvent(new Event("updateSkin"));
122                         dispatchEvent(new Event("updateAlpha"));
123                         updateDirectionArrow();
124                 }
125
126                 private function handleDown(e:Event):void {
127                         this.startDrag();
128                         stage.addEventListener(MouseEvent.MOUSE_UP,handleUp);
129                 }
130
131                 private function handleUp(e:Event):void {
132                         this.stopDrag();
133                         stage.removeEventListener(MouseEvent.MOUSE_UP,handleUp);
134                 }
135                 
136                 public function toggle():void {
137                         this.visible=!this.visible;
138                         var obj:SharedObject = SharedObject.getLocal("user_state");
139                         obj.setProperty("toolbox_visible",this.visible);
140                         obj.flush();
141                 }
142                 
143                 // --------------------------------------------------------------------------------
144                 // Enable/disable toolbox buttons
145                 // (ideally we'd use CSS to set alpha in disabled state, but Flex's CSS
146                 //      capabilities aren't up to it)
147                 
148                 [Bindable(event="updateSkin")]
149                 public function canDo(op:String):Boolean {
150                         if (controller.state.selectCount==0) return false;
151
152                         switch (op) {
153                                 case 'delete':                          return true;
154                                 case 'reverseDirection':        return controller.state.hasSelectedWays();
155                                 case 'quadrilateralise':        return (controller.state.hasSelectedAreas() || controller.state.hasSelectedWayNodesInAreas());
156                                 case 'straighten':                      return controller.state.hasSelectedUnclosedWays();
157                                 case 'circularise':                     return controller.state.hasSelectedAreas();
158                                 case 'split':                           return (controller.state is SelectedWayNode);
159                                 case 'parallelise':                     return (controller.state is SelectedWay);
160                                 case 'merge':                           return controller.state.hasAdjoiningWays();
161                         }
162                         return false;
163                 }
164
165                 [Bindable(event="updateAlpha")]
166                 public function getAlpha(op:String):Number {
167                         if (canDo(op)) { return 1; }
168                         return 0.5;
169                 }
170
171                 [Bindable(event="updateSkin")]
172                 private function deleteToolTipText():String {
173                         var entity:Entity=controller.state.firstSelected;
174                         if (entity is Node) { return "Delete Node (Delete)"; }
175                         if (entity is Way && Way(entity).isArea()) { return "Delete Area (Shift+Delete)"; }
176                         if (entity is Way) { return "Delete Way (Shift+Delete)"; }
177                         return "Delete Item"; // When nothing is selected
178                 }
179
180         private function updateDirectionArrow():void {
181             if (controller.state is SelectedWay) {
182                 var w:Way = Way(controller.state.firstSelected);
183                 if (w) { // not entirely sure why this protection is necessary, but it appears so
184                     if (w.isArea()) {
185                         // so Way.clockwise appears to give wrong results. Patches welcome, I guess, but for now...
186                         w.clockwise? rotateButtonStack.selectedChild = antiClockwiseBox : rotateButtonStack.selectedChild = clockwiseBox;
187                     } else {
188                         rotateButtonStack.selectedChild = arrowBoxWrapper;
189                         // reset and reposition back to the starting point relative to its parent
190                         rotate.end();
191                         angle = 0;
192                         rotate.play();
193                         arrowBox.x = 0;
194                         arrowBox.y = 0;
195
196                         // move
197                         rotate.end();
198                         angle = w.angle;
199                         rotate.play();
200                     }
201                 }
202             }
203         }
204
205         private function reverseClicked():void {
206             if(canDo('reverseDirection')) {
207                 doReverseDirection();
208             }
209         }
210
211                 // --------------------------------------------------------------------------------
212                 // Individual toolbox actions
213
214                 public function doDelete():void {
215                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Delete objects");
216                         for each (var entity:Entity in controller.state.selection) {
217                                 if (entity is Node) { controller.connection.unregisterPOI(Node(entity)); }
218                                 entity.remove(undo.push);
219                         }
220                         MainUndoStack.getGlobalStack().addAction(undo);
221
222                         if (controller.state is SelectedWayNode) {
223                                 controller.setState(new SelectedWay(SelectedWayNode(controller.state).selectedWay));
224                         } else {
225                                 controller.setState(new NoSelection());
226                         }
227                 }
228                 
229                 public function doMerge():void {
230                         controller.setState(SelectedMultiple(controller.state).mergeWays());
231                 }
232
233                 public function doReverseDirection():void {
234                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Reverse direction of objects");
235                         for each (var way:Way in controller.state.selectedWays) {
236                                 way.reverseNodes(undo.push);
237                         }
238                         MainUndoStack.getGlobalStack().addAction(undo);
239                 }
240
241                 public function doQuadrilateralise():void {
242                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Make objects right-angled");
243                         for each (var way:Way in controller.state.selectedWays) {
244                                 Quadrilateralise.quadrilateralise(way, undo.push);
245                         }
246             for each (var node:Node in controller.state.selectedNodes) {
247                 for each (var parentWay:Way in node.parentWays) {
248                   Quadrilateralise.quadrilateralise(parentWay, undo.push);
249                 }
250             }
251                         MainUndoStack.getGlobalStack().addAction(undo);
252                 }
253
254                 public function doStraighten():void {
255                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Straighten objects");
256                         for each (var way:Way in controller.state.selectedWays) {
257                                 Straighten.straighten(way, controller.map, undo.push);
258                         }
259                         MainUndoStack.getGlobalStack().addAction(undo);
260                 }
261
262                 public function doCircularise():void {
263                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Make objects circular ");
264                         for each (var way:Way in controller.state.selectedWays) {
265                                 Circularise.circularise(way, controller.map, undo.push);
266                         }
267                         MainUndoStack.getGlobalStack().addAction(undo);
268                 }
269
270                 public function doSplit():void {
271                         if (controller.state is SelectedWayNode) {
272                                 controller.setState(SelectedWayNode(controller.state).splitWay());
273                         }
274                 }
275                 
276                 public function doParallelise():void {
277                         if (controller.state is SelectedWay) {
278                                 controller.setState(new SelectedParallelWay(Way(controller.state.firstSelected)));
279                         }
280                 }
281
282
283         ]]>
284 </mx:Script>    
285 </mx:Panel>