update toolbox icons depending on what's selected (and add the useful W keypress)
[potlatch2.git] / net / systemeD / potlatch2 / Toolbox.as
1 package net.systemeD.potlatch2 {
2
3         import flash.events.Event;
4         import flash.events.MouseEvent;
5         import mx.containers.Panel;
6         import net.systemeD.halcyon.connection.*;
7     import net.systemeD.potlatch2.controller.*;
8     import net.systemeD.potlatch2.tools.*;
9
10         /*
11                 Floating toolbox palette
12
13                 Still to do:
14                 ** Should have a close box, and be able to be activated from the top bar
15                 ** Should be automatically positioned at bottom-right of canvas on init
16                 ** Should float above tagViewer, not beneath it
17                 ** Remove annoying Illustrator cruft from SVG icons!
18                 ** Tooltips
19
20         */
21
22         public class Toolbox extends Panel{
23                 
24                 private var entity:Entity;
25                 private var controller:EditController;
26
27                 public function Toolbox(){
28                         super();
29                 }
30                 
31                 public function init(controller:EditController):void {
32                         this.controller=controller;
33                 }
34
35                 override protected function createChildren():void {
36                         super.createChildren();
37                         super.titleBar.addEventListener(MouseEvent.MOUSE_DOWN,handleDown);
38                         super.titleBar.addEventListener(MouseEvent.MOUSE_UP,handleUp);
39                 }
40
41                 public function setEntity(entity:Entity):void {
42                         this.entity=entity;
43                         dispatchEvent(new Event("updateSkin"));
44                         dispatchEvent(new Event("updateAlpha"));
45                 }
46
47                 private function handleDown(e:Event):void {
48                         this.startDrag();
49                 }
50
51                 private function handleUp(e:Event):void {
52                         this.stopDrag();
53                 }
54                 
55                 // --------------------------------------------------------------------------------
56                 // Enable/disable toolbox buttons
57                 // (ideally we'd use CSS to set alpha in disabled state, but Flex's CSS
58                 //  capabilities aren't up to it)
59                 
60         [Bindable(event="updateSkin")]
61                 public function canDo(op:String):Boolean {
62                         switch (op) {
63                                 case 'delete':                          return (entity is Way || entity is Node);
64                                 case 'reverseDirection':        return (entity is Way);
65                                 case 'quadrilateralise':        return (entity is Way && Way(entity).isArea());
66                                 case 'straighten':                      return (entity is Way && !Way(entity).isArea());
67                                 case 'circularise':                     return (entity is Way && Way(entity).isArea());
68                                 case 'split':                           return (entity is Node && controller.state is SelectedWayNode);
69                                 case 'parallelise':                     return (entity is Way);
70                         }
71                         return false;
72                 }
73
74         [Bindable(event="updateAlpha")]
75                 public function getAlpha(op:String):Number {
76                         if (canDo(op)) { return 1; }
77                         return 0.5;
78                 }
79         
80
81                 // --------------------------------------------------------------------------------
82                 // Individual toolbox actions
83
84                 public function doDelete():void {
85                         if (entity is Node) { controller.connection.unregisterPOI(Node(entity)); }
86                         entity.remove(MainUndoStack.getGlobalStack().addAction);
87
88                         if (controller.state is SelectedWayNode) {
89                                 controller.setState(new SelectedWay(SelectedWayNode(controller.state).selectedWay));
90                         } else {
91                                 controller.setState(new NoSelection());
92                         }
93                 }
94
95         public function doReverseDirection():void {
96             if (entity is Way) { 
97                 Way(entity).reverseNodes(MainUndoStack.getGlobalStack().addAction);
98             }
99         }
100
101                 public function doQuadrilateralise():void {
102                         if (entity is Way) {
103                                 Quadrilateralise.quadrilateralise(Way(entity));
104                         }
105                 }
106
107                 public function doStraighten():void {
108                         if (entity is Way) {
109                                 Straighten.straighten(Way(entity),controller.map);
110                         }
111                 }
112
113                 public function doCircularise():void {
114                         if (entity is Way) {
115                                 Circularise.circularise(Way(entity),controller.map);
116                         }
117                 }
118
119                 public function doSplit():void {
120                         if (entity is Node && controller.state is SelectedWayNode) {
121                                 controller.setState(SelectedWayNode(controller.state).splitWay());
122                         }
123                 }
124                 
125                 public function doParallelise():void {
126                         if (entity is Way) {
127                                 controller.setState(new SelectedParallelWay(Way(entity)));
128                         }
129                 }
130
131         }
132 }