update toolbox icons depending on what's selected (and add the useful W keypress)
[potlatch2.git] / net / systemeD / potlatch2 / Toolbox.as
index f684dab9d174d143112b61c3dbdde62196a21150..5f9a2aa94e330c20f10ff58afad758756c04d2e9 100644 (file)
@@ -14,9 +14,8 @@ package net.systemeD.potlatch2 {
                ** Should have a close box, and be able to be activated from the top bar
                ** Should be automatically positioned at bottom-right of canvas on init
                ** Should float above tagViewer, not beneath it
-               ** Icons should be disabled depending on what's selected (setEntity can do this)
-               ** Straighten, circularise, reverse way direction, parallelise
                ** Remove annoying Illustrator cruft from SVG icons!
+               ** Tooltips
 
        */
 
@@ -41,6 +40,8 @@ package net.systemeD.potlatch2 {
 
                public function setEntity(entity:Entity):void {
                        this.entity=entity;
+                       dispatchEvent(new Event("updateSkin"));
+                       dispatchEvent(new Event("updateAlpha"));
                }
 
                private function handleDown(e:Event):void {
@@ -50,13 +51,39 @@ package net.systemeD.potlatch2 {
                private function handleUp(e:Event):void {
                        this.stopDrag();
                }
+               
+               // --------------------------------------------------------------------------------
+               // Enable/disable toolbox buttons
+               // (ideally we'd use CSS to set alpha in disabled state, but Flex's CSS
+               //  capabilities aren't up to it)
+               
+        [Bindable(event="updateSkin")]
+               public function canDo(op:String):Boolean {
+                       switch (op) {
+                               case 'delete':                          return (entity is Way || entity is Node);
+                               case 'reverseDirection':        return (entity is Way);
+                               case 'quadrilateralise':        return (entity is Way && Way(entity).isArea());
+                               case 'straighten':                      return (entity is Way && !Way(entity).isArea());
+                               case 'circularise':                     return (entity is Way && Way(entity).isArea());
+                               case 'split':                           return (entity is Node && controller.state is SelectedWayNode);
+                               case 'parallelise':                     return (entity is Way);
+                       }
+                       return false;
+               }
+
+        [Bindable(event="updateAlpha")]
+               public function getAlpha(op:String):Number {
+                       if (canDo(op)) { return 1; }
+                       return 0.5;
+               }
+        
 
                // --------------------------------------------------------------------------------
                // Individual toolbox actions
 
                public function doDelete():void {
                        if (entity is Node) { controller.connection.unregisterPOI(Node(entity)); }
-                       entity.remove();
+                       entity.remove(MainUndoStack.getGlobalStack().addAction);
 
                        if (controller.state is SelectedWayNode) {
                                controller.setState(new SelectedWay(SelectedWayNode(controller.state).selectedWay));
@@ -65,6 +92,12 @@ package net.systemeD.potlatch2 {
                        }
                }
 
+        public function doReverseDirection():void {
+            if (entity is Way) { 
+                Way(entity).reverseNodes(MainUndoStack.getGlobalStack().addAction);
+            }
+        }
+
                public function doQuadrilateralise():void {
                        if (entity is Way) {
                                Quadrilateralise.quadrilateralise(Way(entity));
@@ -83,5 +116,17 @@ package net.systemeD.potlatch2 {
                        }
                }
 
+               public function doSplit():void {
+                       if (entity is Node && controller.state is SelectedWayNode) {
+                               controller.setState(SelectedWayNode(controller.state).splitWay());
+                       }
+               }
+               
+               public function doParallelise():void {
+                       if (entity is Way) {
+                               controller.setState(new SelectedParallelWay(Way(entity)));
+                       }
+               }
+
        }
-}
\ No newline at end of file
+}