make Toolbox act on multiple selections (and light up accordingly)
[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="46" width="219" 
6         headerHeight="6" headerColors="[black, gray]" 
7         borderThicknessRight="0" borderThicknessLeft="0" borderThicknessBottom="0" 
8         paddingLeft="4" paddingTop="4" layout="absolute" >
9
10                 <mx:Image data="@Embed('../../../embedded/close_small.png')"
11                         includeInLayout="false" id="toolboxClose" click="toggle();" 
12                         y="-6" x="205" />
13
14                 <mx:Button icon="@Embed('../../../embedded/delete.svg')"
15                         click='doDelete();' 
16                         enabled="{canDo('delete')}" 
17                         alpha="{getAlpha('delete')}" 
18                         toolTip="{deleteToolTipText()}" 
19                         width="28" height="28" textAlign="left" y="4" x="6" paddingLeft="6" paddingRight="0" />
20                 <mx:Button icon="@Embed('../../../embedded/direction.svg')" 
21                         click='doReverseDirection();'
22                         enabled="{canDo('reverseDirection')}" 
23                         alpha="{getAlpha('reverseDirection')}" 
24                         toolTip="Reverse direction (V)" 
25                         width="28" height="28" textAlign="left" y="4" x="36" paddingLeft="8" paddingRight="0" />
26                 <mx:Button icon="@Embed('../../../embedded/cut.svg')" 
27                            click='doSplit();'
28                         enabled="{canDo('split')}" 
29                         alpha="{getAlpha('split')}" 
30                         toolTip="Split way (X)" 
31                         width="28" height="28" textAlign="left" y="4" x="66" paddingLeft="8" paddingRight="0" />
32                 <mx:Button icon="@Embed('../../../embedded/straighten.svg')" 
33                         click='doStraighten();' 
34                         enabled="{canDo('straighten')}" 
35                         alpha="{getAlpha('straighten')}" 
36                         toolTip="Straighten way" 
37                         width="28" height="28" textAlign="left" y="4" x="96" paddingLeft="5" paddingRight="0" />
38                 <mx:Button icon="@Embed('../../../embedded/circle.svg')" 
39                         click='doCircularise();' 
40                         enabled="{canDo('circularise')}" 
41                         alpha="{getAlpha('circularise')}" 
42                         toolTip="Make circular" 
43                         width="28" height="28" textAlign="left" y="4" x="126" paddingLeft="4" paddingRight="0" />
44                 <mx:Button icon="@Embed('../../../embedded/quadrilateralise.svg')" 
45                         click='doQuadrilateralise();' 
46                         enabled="{canDo('quadrilateralise')}" 
47                         alpha="{getAlpha('quadrilateralise')}" 
48                         toolTip="Make right-angled" 
49                         width="28" height="28" textAlign="left" y="4" x="156" paddingLeft="6" paddingRight="0" />
50                 <mx:Button icon="@Embed('../../../embedded/parallel.svg')" 
51                         click='doParallelise();' 
52                         enabled="{canDo('parallelise')}" 
53                         alpha="{getAlpha('parallelise')}" 
54                         toolTip="Create parallel way (P)" 
55                         width="28" height="28" textAlign="left" y="4" x="186" paddingLeft="8" paddingRight="0" />
56
57         <mx:Script><![CDATA[
58
59                 import flash.events.Event;
60                 import flash.events.MouseEvent;
61                 import net.systemeD.halcyon.connection.*;
62                 import net.systemeD.potlatch2.controller.*;
63                 import net.systemeD.potlatch2.tools.*;
64
65                 private var controller:EditController;
66
67                 public function init(controller:EditController):void {
68                         this.controller=controller;
69                         /* check if the toolbox was explictly turned off in a previous session */
70                         if( SharedObject.getLocal("user_state").data['toolbox_visible'] == false) {
71                           this.visible = false;
72                         }
73                 }
74
75                 override protected function createChildren():void {
76                         super.createChildren();
77                         super.titleBar.addEventListener(MouseEvent.MOUSE_DOWN,handleDown);
78                         super.titleBar.addEventListener(MouseEvent.MOUSE_UP,handleUp);
79                 }
80
81                 public function updateSelectionUI():void {
82                         dispatchEvent(new Event("updateSkin"));
83                         dispatchEvent(new Event("updateAlpha"));
84                 }
85
86                 private function handleDown(e:Event):void {
87                         this.startDrag();
88                 }
89
90                 private function handleUp(e:Event):void {
91                         this.stopDrag();
92                 }
93                 
94                 public function toggle():void {
95                         this.visible=!this.visible;
96                         var obj:SharedObject = SharedObject.getLocal("user_state");
97                         obj.setProperty("toolbox_visible",this.visible);
98                         obj.flush();
99                 }
100                 
101                 // --------------------------------------------------------------------------------
102                 // Enable/disable toolbox buttons
103                 // (ideally we'd use CSS to set alpha in disabled state, but Flex's CSS
104                 //      capabilities aren't up to it)
105                 
106                 [Bindable(event="updateSkin")]
107                 public function canDo(op:String):Boolean {
108                         if (controller.state.selectCount==0) return false;
109
110                         switch (op) {
111                                 case 'delete':                          return true;
112                                 case 'reverseDirection':        return controller.state.hasSelectedWays();
113                                 case 'quadrilateralise':        return controller.state.hasSelectedAreas();
114                                 case 'straighten':                      return controller.state.hasSelectedUnclosedWays();
115                                 case 'circularise':                     return controller.state.hasSelectedAreas();
116                                 case 'split':                           return (controller.state is SelectedWayNode);
117                                 case 'parallelise':                     return (controller.state is SelectedWay);
118                         }
119                         return false;
120                 }
121
122                 [Bindable(event="updateAlpha")]
123                 public function getAlpha(op:String):Number {
124                         if (canDo(op)) { return 1; }
125                         return 0.5;
126                 }
127
128                 [Bindable(event="updateSkin")]
129                 private function deleteToolTipText():String {
130                         var entity:Entity=controller.state.firstSelected;
131                         if (entity is Node) { return "Delete Node (Delete)"; }
132                         if (entity is Way && Way(entity).isArea()) { return "Delete Area (Shift+Delete)"; }
133                         if (entity is Way) { return "Delete Way (Shift+Delete)"; }
134                         return "Delete Item"; // When nothing is selected
135                 }
136
137                 // --------------------------------------------------------------------------------
138                 // Individual toolbox actions
139
140                 public function doDelete():void {
141                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Delete objects");
142                         for each (var entity:Entity in controller.state.selection) {
143                                 if (entity is Node) { controller.connection.unregisterPOI(Node(entity)); }
144                                 entity.remove(undo.push);
145                         }
146                         MainUndoStack.getGlobalStack().addAction(undo);
147
148                         if (controller.state is SelectedWayNode) {
149                                 controller.setState(new SelectedWay(SelectedWayNode(controller.state).selectedWay));
150                         } else {
151                                 controller.setState(new NoSelection());
152                         }
153                 }
154
155                 public function doReverseDirection():void {
156                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Reverse direction of objects");
157                         for each (var way:Way in controller.state.selectedWays) {
158                                 way.reverseNodes(undo.push);
159                         }
160                         MainUndoStack.getGlobalStack().addAction(undo);
161                 }
162
163                 public function doQuadrilateralise():void {
164                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Make objects right-angled");
165                         for each (var way:Way in controller.state.selectedWays) {
166                                 Quadrilateralise.quadrilateralise(way, undo.push);
167                         }
168                         MainUndoStack.getGlobalStack().addAction(undo);
169                 }
170
171                 public function doStraighten():void {
172                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Straighten objects");
173                         for each (var way:Way in controller.state.selectedWays) {
174                                 Straighten.straighten(way, controller.map, undo.push);
175                         }
176                         MainUndoStack.getGlobalStack().addAction(undo);
177                 }
178
179                 public function doCircularise():void {
180                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Make objects circular ");
181                         for each (var way:Way in controller.state.selectedWays) {
182                                 Circularise.circularise(way, controller.map, undo.push);
183                         }
184                         MainUndoStack.getGlobalStack().addAction(undo);
185                 }
186
187                 public function doSplit():void {
188                         if (controller.state is SelectedWayNode) {
189                                 controller.setState(SelectedWayNode(controller.state).splitWay());
190                         }
191                 }
192                 
193                 public function doParallelise():void {
194                         if (controller.state is SelectedWay) {
195                                 controller.setState(new SelectedParallelWay(Way(controller.state.firstSelected)));
196                         }
197                 }
198
199
200         ]]>
201 </mx:Script>    
202 </mx:Panel>