fun with tooltips
[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 close and minimise boxes, and be able to be activated from the top bar
15                 ** Remove annoying Illustrator cruft from SVG icons!
16
17         */
18
19         public class Toolbox extends Panel {
20                 
21                 private var entity:Entity;
22                 private var controller:EditController;
23
24                 public function Toolbox(){
25                         super();
26                 }
27                 
28                 public function init(controller:EditController):void {
29                         this.controller=controller;
30                 }
31
32                 override protected function createChildren():void {
33                         super.createChildren();
34                         super.titleBar.addEventListener(MouseEvent.MOUSE_DOWN,handleDown);
35                         super.titleBar.addEventListener(MouseEvent.MOUSE_UP,handleUp);
36                 }
37
38                 public function setEntity(entity:Entity):void {
39                         this.entity=entity;
40                         dispatchEvent(new Event("updateSkin"));
41                         dispatchEvent(new Event("updateAlpha"));
42                 }
43
44                 private function handleDown(e:Event):void {
45                         this.startDrag();
46                 }
47
48                 private function handleUp(e:Event):void {
49                         this.stopDrag();
50                 }
51                 
52                 // --------------------------------------------------------------------------------
53                 // Enable/disable toolbox buttons
54                 // (ideally we'd use CSS to set alpha in disabled state, but Flex's CSS
55                 //  capabilities aren't up to it)
56                 
57         [Bindable(event="updateSkin")]
58                 public function canDo(op:String):Boolean {
59                         switch (op) {
60                                 case 'delete':                          return (entity is Way || entity is Node);
61                                 case 'reverseDirection':        return (entity is Way);
62                                 case 'quadrilateralise':        return (entity is Way && Way(entity).isArea());
63                                 case 'straighten':                      return (entity is Way && !Way(entity).isArea());
64                                 case 'circularise':                     return (entity is Way && Way(entity).isArea());
65                                 case 'split':                           return (entity is Node && controller.state is SelectedWayNode);
66                                 case 'parallelise':                     return (entity is Way);
67                         }
68                         return false;
69                 }
70
71         [Bindable(event="updateAlpha")]
72                 public function getAlpha(op:String):Number {
73                         if (canDo(op)) { return 1; }
74                         return 0.5;
75                 }
76         
77
78                 // --------------------------------------------------------------------------------
79                 // Individual toolbox actions
80
81                 public function doDelete():void {
82                         if (entity is Node) { controller.connection.unregisterPOI(Node(entity)); }
83                         entity.remove(MainUndoStack.getGlobalStack().addAction);
84
85                         if (controller.state is SelectedWayNode) {
86                                 controller.setState(new SelectedWay(SelectedWayNode(controller.state).selectedWay));
87                         } else {
88                                 controller.setState(new NoSelection());
89                         }
90                 }
91
92         public function doReverseDirection():void {
93             if (entity is Way) { 
94                 Way(entity).reverseNodes(MainUndoStack.getGlobalStack().addAction);
95             }
96         }
97
98                 public function doQuadrilateralise():void {
99                         if (entity is Way) {
100                                 Quadrilateralise.quadrilateralise(Way(entity));
101                         }
102                 }
103
104                 public function doStraighten():void {
105                         if (entity is Way) {
106                                 Straighten.straighten(Way(entity),controller.map);
107                         }
108                 }
109
110                 public function doCircularise():void {
111                         if (entity is Way) {
112                                 Circularise.circularise(Way(entity),controller.map);
113                         }
114                 }
115
116                 public function doSplit():void {
117                         if (entity is Node && controller.state is SelectedWayNode) {
118                                 controller.setState(SelectedWayNode(controller.state).splitWay());
119                         }
120                 }
121                 
122                 public function doParallelise():void {
123                         if (entity is Way) {
124                                 controller.setState(new SelectedParallelWay(Way(entity)));
125                         }
126                 }
127
128         }
129 }