OSDN Git Service

Added checkedOver drawable to buttons.
authorNathanSweet <nathan.sweet@gmail.com>
Tue, 25 Sep 2012 05:58:42 +0000 (22:58 -0700)
committerNathanSweet <nathan.sweet@gmail.com>
Tue, 25 Sep 2012 05:58:42 +0000 (22:58 -0700)
Added ImageTextButton.

gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Button.java
gdx/src/com/badlogic/gdx/scenes/scene2d/ui/ImageButton.java
gdx/src/com/badlogic/gdx/scenes/scene2d/ui/ImageTextButton.java [new file with mode: 0644]
gdx/src/com/badlogic/gdx/scenes/scene2d/ui/TextButton.java

index 3676686..91f42a4 100644 (file)
@@ -170,7 +170,7 @@ public class Button extends Table {
 \r
                Drawable background = null;\r
                float offsetX = 0, offsetY = 0;\r
-               if (clickListener.isPressed() && !isDisabled) {\r
+               if (isPressed() && !isDisabled) {\r
                        background = style.down == null ? style.up : style.down;\r
                        offsetX = style.pressedOffsetX;\r
                        offsetY = style.pressedOffsetY;\r
@@ -178,8 +178,8 @@ public class Button extends Table {
                        if (isDisabled && style.disabled != null)\r
                                background = style.disabled;\r
                        else if (isChecked && style.checked != null)\r
-                               background = style.checked;\r
-                       else if (clickListener.isOver() && style.over != null)\r
+                               background = (isOver() && style.checkedOver != null) ? style.checkedOver : style.checked;\r
+                       else if (isOver() && style.over != null)\r
                                background = style.over;\r
                        else\r
                                background = style.up;\r
@@ -232,7 +232,7 @@ public class Button extends Table {
         * @author mzechner */\r
        static public class ButtonStyle {\r
                /** Optional. */\r
-               public Drawable up, down, checked, over, disabled;\r
+               public Drawable up, down, over, checked, checkedOver, disabled;\r
                /** Optional. */\r
                public float pressedOffsetX, pressedOffsetY;\r
                /** Optional. */\r
@@ -242,16 +242,17 @@ public class Button extends Table {
                }\r
 \r
                public ButtonStyle (Drawable up, Drawable down, Drawable checked) {\r
-                       this.down = down;\r
                        this.up = up;\r
+                       this.down = down;\r
                        this.checked = checked;\r
                }\r
 \r
                public ButtonStyle (ButtonStyle style) {\r
-                       this.down = style.down;\r
                        this.up = style.up;\r
-                       this.checked = style.checked;\r
+                       this.down = style.down;\r
                        this.over = style.over;\r
+                       this.checked = style.checked;\r
+                       this.checkedOver = style.checkedOver;\r
                        this.disabled = style.disabled;\r
                        this.pressedOffsetX = style.pressedOffsetX;\r
                        this.pressedOffsetY = style.pressedOffsetY;\r
index cd0000f..545ec9a 100644 (file)
@@ -78,7 +78,9 @@ public class ImageButton extends Button {
                else if (isPressed && style.imageDown != null)\r
                        image.setDrawable(style.imageDown);\r
                else if (isChecked && style.imageChecked != null)\r
-                       image.setDrawable(style.imageChecked);\r
+                       image.setDrawable((style.imageCheckedOver != null && isOver()) ? style.imageCheckedOver : style.imageChecked);\r
+               else if (isOver() && style.imageOver != null)\r
+                       image.setDrawable(style.imageOver);\r
                else if (style.imageUp != null) //\r
                        image.setDrawable(style.imageUp);\r
        }\r
@@ -100,7 +102,7 @@ public class ImageButton extends Button {
         * @author Nathan Sweet */\r
        static public class ImageButtonStyle extends ButtonStyle {\r
                /** Optional. */\r
-               public Drawable imageUp, imageDown, imageChecked, imageDisabled;\r
+               public Drawable imageUp, imageDown, imageOver, imageChecked, imageCheckedOver, imageDisabled;\r
 \r
                public ImageButtonStyle () {\r
                }\r
@@ -117,7 +119,10 @@ public class ImageButton extends Button {
                        super(style);\r
                        this.imageUp = style.imageUp;\r
                        this.imageDown = style.imageDown;\r
+                       this.imageOver = style.imageOver;\r
                        this.imageChecked = style.imageChecked;\r
+                       this.imageCheckedOver = style.imageCheckedOver;\r
+                       this.imageDisabled = style.imageDisabled;\r
                }\r
 \r
                public ImageButtonStyle (ButtonStyle style) {\r
diff --git a/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/ImageTextButton.java b/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/ImageTextButton.java
new file mode 100644 (file)
index 0000000..51f6015
--- /dev/null
@@ -0,0 +1,156 @@
+
+package com.badlogic.gdx.scenes.scene2d.ui;
+
+import com.esotericsoftware.tablelayout.Cell;
+
+import com.badlogic.gdx.graphics.Color;
+import com.badlogic.gdx.graphics.g2d.BitmapFont;
+import com.badlogic.gdx.graphics.g2d.SpriteBatch;
+import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
+import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
+import com.badlogic.gdx.scenes.scene2d.utils.Align;
+import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
+import com.badlogic.gdx.utils.Scaling;
+
+/** A button with a child {@link Image} and {@link Label}.
+ * @see ImageButton
+ * @see TextButton
+ * @see Button
+ * @author Nathan Sweet */
+public class ImageTextButton extends Button {
+       private final Image image;
+       private final Label label;
+       private ImageTextButtonStyle style;
+
+       public ImageTextButton (String text, Skin skin) {
+               this(text, skin.get(ImageTextButtonStyle.class));
+               setSkin(skin);
+       }
+
+       public ImageTextButton (String text, Skin skin, String styleName) {
+               this(text, skin.get(styleName, ImageTextButtonStyle.class));
+               setSkin(skin);
+       }
+
+       public ImageTextButton (String text, ImageTextButtonStyle style) {
+               super(style);
+               this.style = style;
+
+               defaults().space(3);
+
+               image = new Image();
+               image.setScaling(Scaling.fit);
+               add(image);
+
+               label = new Label(text, new LabelStyle(style.font, style.fontColor));
+               label.setAlignment(Align.center);
+               add(label);
+
+               setWidth(getPrefWidth());
+               setHeight(getPrefHeight());
+       }
+
+       public void setStyle (ButtonStyle style) {
+               if (!(style instanceof ImageTextButtonStyle)) throw new IllegalArgumentException("style must be a ImageTextButtonStyle.");
+               super.setStyle(style);
+               this.style = (ImageTextButtonStyle)style;
+               if (image != null) updateImage();
+               if (label != null) {
+                       ImageTextButtonStyle textButtonStyle = (ImageTextButtonStyle)style;
+                       LabelStyle labelStyle = label.getStyle();
+                       labelStyle.font = textButtonStyle.font;
+                       labelStyle.fontColor = textButtonStyle.fontColor;
+                       label.setStyle(labelStyle);
+               }
+       }
+
+       public ImageTextButtonStyle getStyle () {
+               return style;
+       }
+
+       private void updateImage () {
+               boolean isPressed = isPressed();
+               if (isDisabled && style.imageDisabled != null)
+                       image.setDrawable(style.imageDisabled);
+               else if (isPressed && style.imageDown != null)
+                       image.setDrawable(style.imageDown);
+               else if (isChecked && style.imageChecked != null)
+                       image.setDrawable((style.imageCheckedOver != null && isOver()) ? style.imageCheckedOver : style.imageChecked);
+               else if (isOver() && style.imageOver != null)
+                       image.setDrawable(style.imageOver);
+               else if (style.imageUp != null) //
+                       image.setDrawable(style.imageUp);
+       }
+
+       public void draw (SpriteBatch batch, float parentAlpha) {
+               updateImage();
+               Color fontColor;
+               if (isDisabled && style.disabledFontColor != null)
+                       fontColor = style.disabledFontColor;
+               else if (isPressed() && style.downFontColor != null)
+                       fontColor = style.downFontColor;
+               else if (isChecked && style.checkedFontColor != null)
+                       fontColor = (isOver() && style.checkedOverFontColor != null) ? style.checkedOverFontColor : style.checkedFontColor;
+               else if (isOver() && style.overFontColor != null)
+                       fontColor = style.overFontColor;
+               else
+                       fontColor = style.fontColor;
+               if (fontColor != null) label.getStyle().fontColor = fontColor;
+               super.draw(batch, parentAlpha);
+       }
+
+       public Image getImage () {
+               return image;
+       }
+
+       public Cell getImageCell () {
+               return getCell(image);
+       }
+
+       public Label getLabel () {
+               return label;
+       }
+
+       public Cell getLabelCell () {
+               return getCell(label);
+       }
+
+       public void setText (String text) {
+               label.setText(text);
+       }
+
+       public CharSequence getText () {
+               return label.getText();
+       }
+
+       /** The style for an image text button, see {@link ImageTextButton}.
+        * @author Nathan Sweet */
+       static public class ImageTextButtonStyle extends TextButtonStyle {
+               /** Optional. */
+               public Drawable imageUp, imageDown, imageOver, imageChecked, imageCheckedOver, imageDisabled;
+
+               public ImageTextButtonStyle () {
+               }
+
+               public ImageTextButtonStyle (Drawable up, Drawable down, Drawable checked) {
+                       super(up, down, checked);
+               }
+
+               public ImageTextButtonStyle (ImageTextButtonStyle style) {
+                       super(style);
+                       this.font = style.font;
+                       if (style.fontColor != null) this.fontColor = new Color(style.fontColor);
+                       if (style.downFontColor != null) this.downFontColor = new Color(style.downFontColor);
+                       if (style.overFontColor != null) this.overFontColor = new Color(style.overFontColor);
+                       if (style.checkedFontColor != null) this.checkedFontColor = new Color(style.checkedFontColor);
+                       if (style.checkedOverFontColor != null) this.checkedOverFontColor = new Color(style.checkedOverFontColor);
+                       if (style.disabledFontColor != null) this.disabledFontColor = new Color(style.disabledFontColor);
+                       if (style.imageUp != null) this.imageUp = style.imageUp;
+                       if (style.imageDown != null) this.imageDown = style.imageDown;
+                       if (style.imageOver != null) this.imageOver = style.imageOver;
+                       if (style.imageChecked != null) this.imageChecked = style.imageChecked;
+                       if (style.imageCheckedOver != null) this.imageCheckedOver = style.imageCheckedOver;
+                       if (style.imageDisabled != null) this.imageDisabled = style.imageDisabled;
+               }
+       }
+}
index 253f1b6..238295a 100644 (file)
@@ -74,7 +74,7 @@ public class TextButton extends Button {
                else if (isPressed() && style.downFontColor != null)\r
                        fontColor = style.downFontColor;\r
                else if (isChecked && style.checkedFontColor != null)\r
-                       fontColor = style.checkedFontColor;\r
+                       fontColor = (isOver() && style.checkedOverFontColor != null) ? style.checkedOverFontColor : style.checkedFontColor;\r
                else if (isOver() && style.overFontColor != null)\r
                        fontColor = style.overFontColor;\r
                else\r
@@ -104,7 +104,7 @@ public class TextButton extends Button {
        static public class TextButtonStyle extends ButtonStyle {\r
                public BitmapFont font;\r
                /** Optional. */\r
-               public Color downFontColor, fontColor, checkedFontColor, overFontColor, disabledFontColor;\r
+               public Color fontColor, downFontColor, overFontColor, checkedFontColor, checkedOverFontColor, disabledFontColor;\r
 \r
                public TextButtonStyle () {\r
                }\r
@@ -116,10 +116,11 @@ public class TextButton extends Button {
                public TextButtonStyle (TextButtonStyle style) {\r
                        super(style);\r
                        this.font = style.font;\r
-                       if (style.downFontColor != null) this.downFontColor = new Color(style.downFontColor);\r
                        if (style.fontColor != null) this.fontColor = new Color(style.fontColor);\r
-                       if (style.checkedFontColor != null) this.checkedFontColor = new Color(style.checkedFontColor);\r
+                       if (style.downFontColor != null) this.downFontColor = new Color(style.downFontColor);\r
                        if (style.overFontColor != null) this.overFontColor = new Color(style.overFontColor);\r
+                       if (style.checkedFontColor != null) this.checkedFontColor = new Color(style.checkedFontColor);\r
+                       if (style.checkedOverFontColor != null) this.checkedFontColor = new Color(style.checkedOverFontColor);\r
                        if (style.disabledFontColor != null) this.disabledFontColor = new Color(style.disabledFontColor);\r
                }\r
        }\r