\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
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
* @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
}\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
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
* @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
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
--- /dev/null
+
+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;
+ }
+ }
+}
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
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
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