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