Save state of the toolbox in SharedObject
[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="Delete item" 
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" 
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" 
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" 
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 entity:Entity;
66                 private var controller:EditController;
67
68                 public function init(controller:EditController):void {
69                         this.controller=controller;
70             /* check if the toolbox was explictly turned off in a previous session */
71             if( SharedObject.getLocal("user_state").data['toolbox_visible'] == false) {
72               this.visible = false;
73             }
74                 }
75
76                 override protected function createChildren():void {
77                         super.createChildren();
78                         super.titleBar.addEventListener(MouseEvent.MOUSE_DOWN,handleDown);
79                         super.titleBar.addEventListener(MouseEvent.MOUSE_UP,handleUp);
80                 }
81
82                 public function setEntity(entity:Entity):void {
83                         this.entity=entity;
84                         dispatchEvent(new Event("updateSkin"));
85                         dispatchEvent(new Event("updateAlpha"));
86                 }
87
88                 private function handleDown(e:Event):void {
89                         this.startDrag();
90                 }
91
92                 private function handleUp(e:Event):void {
93                         this.stopDrag();
94                 }
95                 
96                 public function toggle():void {
97                         this.visible=!this.visible;
98             var obj:SharedObject = SharedObject.getLocal("user_state");
99             obj.setProperty("toolbox_visible",this.visible);
100             obj.flush();
101                 }
102                 
103                 // --------------------------------------------------------------------------------
104                 // Enable/disable toolbox buttons
105                 // (ideally we'd use CSS to set alpha in disabled state, but Flex's CSS
106                 //  capabilities aren't up to it)
107                 
108         [Bindable(event="updateSkin")]
109                 public function canDo(op:String):Boolean {
110                         switch (op) {
111                                 case 'delete':                          return (entity is Way || entity is Node);
112                                 case 'reverseDirection':        return (entity is Way);
113                                 case 'quadrilateralise':        return (entity is Way && Way(entity).isArea());
114                                 case 'straighten':                      return (entity is Way && !Way(entity).isArea());
115                                 case 'circularise':                     return (entity is Way && Way(entity).isArea());
116                                 case 'split':                           return (entity is Node && controller.state is SelectedWayNode);
117                                 case 'parallelise':                     return (entity is Way);
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
129                 // --------------------------------------------------------------------------------
130                 // Individual toolbox actions
131
132                 public function doDelete():void {
133                         if (entity is Node) { controller.connection.unregisterPOI(Node(entity)); }
134                         entity.remove(MainUndoStack.getGlobalStack().addAction);
135
136                         if (controller.state is SelectedWayNode) {
137                                 controller.setState(new SelectedWay(SelectedWayNode(controller.state).selectedWay));
138                         } else {
139                                 controller.setState(new NoSelection());
140                         }
141                 }
142
143         public function doReverseDirection():void {
144             if (entity is Way) { 
145                 Way(entity).reverseNodes(MainUndoStack.getGlobalStack().addAction);
146             }
147         }
148
149                 public function doQuadrilateralise():void {
150                         if (entity is Way) {
151                                 Quadrilateralise.quadrilateralise(Way(entity));
152                         }
153                 }
154
155                 public function doStraighten():void {
156                         if (entity is Way) {
157                                 Straighten.straighten(Way(entity),controller.map);
158                         }
159                 }
160
161                 public function doCircularise():void {
162                         if (entity is Way) {
163                                 Circularise.circularise(Way(entity),controller.map);
164                         }
165                 }
166
167                 public function doSplit():void {
168                         if (entity is Node && controller.state is SelectedWayNode) {
169                                 controller.setState(SelectedWayNode(controller.state).splitWay());
170                         }
171                 }
172                 
173                 public function doParallelise():void {
174                         if (entity is Way) {
175                                 controller.setState(new SelectedParallelWay(Way(entity)));
176                         }
177                 }
178
179
180     ]]>
181 </mx:Script>    
182 </mx:Panel>