Flash CS3: Missing "disabledState" for the SimpleButton class

Posted on August 22, 2007

The flash.display.SimpleButton class provides only four states for specifying display objects these are used as visual objects for the following states: upState, overState downState and hitTestState. But I've missed a disabled state for an inactive Button.

Anyway, overriding the setter method of its enabled property is a good way to solve this problem (thanks to Roman Wache aka mascha for his hint). Note the comments within the code located below for more information ;-) .

Example

To see this content latest Flash Player Plugin is required.

Source code

 1 /**
 2 * Flash CS3: Missing “disabledState” for the SimpleButton class
 3 *
 4 * @author   Jens Krause [www.websector.de]
 5 * @date     08/22/07
 6 * @see      http://www.websector.de/blog/2007/08/22/flash-cs3-missing-disabledstate-for-the-simplebutton-class/
 7 *
 8 */
 9 
10 package
11 {
12  import flash.display.DisplayObject;
13  import flash.display.SimpleButton;
14 
15  public class CustomSimpleButton extends SimpleButton
16  {
17 
18      protected var enabledState: DisplayObject;
19      protected var disabledState: DisplayObject;
20 
21      /**
22          * Constructor of CustomSimpleButton
23          * Declares all states including additional states called "enabledState" or "disabledState"
24          *
25          * @param  txt  String of its label
26          */
27      public function CustomSimpleButton(txt: String)
28      {
29          //
30          // states
31          enabledState = new ButtonDisplayState(txt, ButtonDisplayState.STATE_NORMAL);
32          disabledState = new ButtonDisplayState(txt, ButtonDisplayState.STATE_DISABLED);
33          overState = new ButtonDisplayState(txt, ButtonDisplayState.STATE_OVER);
34          downState = new ButtonDisplayState(txt, ButtonDisplayState.STATE_DOWN);
35          upState = enabledState;
36          hitTestState = upState;
37      }
38 
39      /**
40          * Overides the setter method of its enabled property
41          * @param  value    Boolean true or false
42          */
43      override public function set enabled(value: Boolean):void
44      {
45          super.enabled = value;
46          // hide or enable mouse events
47          this.mouseEnabled = enabled;
48          // With mouseEnabled = false you'll have only one state named "upState".
49          // Use this state for setting the new states called "enabledState" or "disabledState" ;-)
50          upState = (enabled) ? enabledState : disabledState;
51      }
52  }
53 }
 1 /**
 2 * Flash CS3: Missing “disabledState” for the SimpleButton class
 3 *
 4 * @author   Jens Krause [www.websector.de]
 5 * @date     08/22/07
 6 * @see      http://www.websector.de/blog/2007/08/22/flash-cs3-missing-disabledstate-for-the-simplebutton-class/
 7 *
 8 */
 9 package
10 {
11  import flash.display.Bitmap;
12  import flash.display.BitmapData;
13  import flash.display.Sprite;
14  import flash.text.TextField;
15  import flash.text.TextFieldAutoSize;
16  import flash.text.TextFormat;
17 
18  class ButtonDisplayState extends Sprite
19  {
20      public static var STATE_NORMAL: uint = 0;
21      public static var STATE_OVER: uint = 1;
22      public static var STATE_DOWN: uint = 2;
23      public static var STATE_DISABLED: uint = 3;
24 
25      /**
26          * Constructor of ButtonDisplayState
27          * Adds a background image and a label
28          *
29          * @param  txt      String of its label
30          * @param  stateID  Identifier of its state
31          */
32      public function ButtonDisplayState(txt: String, stateID:uint)
33      {
34          var bgImage: BitmapData;
35          //
36          // Using bitmaps located in the library for background.
37          // Note: Flash IDE creates associated classes for these bitmaps called "BGNormal", etc.
38          // Check the "Automatically declare stage instances" box in the publish settings ;-)
39          switch (stateID)
40          {
41              case STATE_NORMAL:
42                  bgImage = new BGNormal(108, 37);
43              break;
44              case STATE_OVER:
45              case STATE_DOWN:
46                  bgImage = new BGOver(108, 37);
47              break;
48              case STATE_DISABLED:
49                  bgImage = new BGDisabled(108, 37);
50              break;
51              default:
52                  bgImage = new BGNormal(108, 37);
53 
54          }
55 
56          //
57          // add bitmap
58          var bg: Bitmap = new Bitmap(bgImage);
59          this.addChild(bg);
60 
61          //
62          // and add a label as well
63          var format: TextFormat = new TextFormat();
64          format.font = "Arial";
65          format.color = 0xFFFFFF;
66          format.size = 15;
67 
68             var label: TextField = new TextField();
69             label.autoSize = TextFieldAutoSize.CENTER;
70          label.defaultTextFormat = format;
71             label.text = txt;
72             label.x = 0;
73             label.y = 5;
74             label.width = bg.width;
75 
76             this.addChild(label);
77 
78      }
79  }
80 }

Download full source

DisabledSimpleButtonExample.zip

Any feedback?

comments powered by Disqus