1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.scenes.scene2d.ui; 18 19 import com.badlogic.gdx.graphics.Color; 20 import com.badlogic.gdx.graphics.g2d.Batch; 21 import com.badlogic.gdx.graphics.g2d.BitmapFont; 22 import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle; 23 import com.badlogic.gdx.scenes.scene2d.utils.Drawable; 24 import com.badlogic.gdx.utils.Align; 25 26 /** A button with a child {@link Label} to display text. 27 * @author Nathan Sweet */ 28 public class TextButton extends Button { 29 private final Label label; 30 private TextButtonStyle style; 31 32 public TextButton (String text, Skin skin) { 33 this(text, skin.get(TextButtonStyle.class)); 34 setSkin(skin); 35 } 36 37 public TextButton (String text, Skin skin, String styleName) { 38 this(text, skin.get(styleName, TextButtonStyle.class)); 39 setSkin(skin); 40 } 41 42 public TextButton (String text, TextButtonStyle style) { 43 super(); 44 setStyle(style); 45 this.style = style; 46 label = new Label(text, new LabelStyle(style.font, style.fontColor)); 47 label.setAlignment(Align.center); 48 add(label).expand().fill(); 49 setSize(getPrefWidth(), getPrefHeight()); 50 } 51 52 public void setStyle (ButtonStyle style) { 53 if (style == null) throw new NullPointerException("style cannot be null"); 54 if (!(style instanceof TextButtonStyle)) throw new IllegalArgumentException("style must be a TextButtonStyle."); 55 super.setStyle(style); 56 this.style = (TextButtonStyle)style; 57 if (label != null) { 58 TextButtonStyle textButtonStyle = (TextButtonStyle)style; 59 LabelStyle labelStyle = label.getStyle(); 60 labelStyle.font = textButtonStyle.font; 61 labelStyle.fontColor = textButtonStyle.fontColor; 62 label.setStyle(labelStyle); 63 } 64 } 65 66 public TextButtonStyle getStyle () { 67 return style; 68 } 69 70 public void draw (Batch batch, float parentAlpha) { 71 Color fontColor; 72 if (isDisabled() && style.disabledFontColor != null) 73 fontColor = style.disabledFontColor; 74 else if (isPressed() && style.downFontColor != null) 75 fontColor = style.downFontColor; 76 else if (isChecked && style.checkedFontColor != null) 77 fontColor = (isOver() && style.checkedOverFontColor != null) ? style.checkedOverFontColor : style.checkedFontColor; 78 else if (isOver() && style.overFontColor != null) 79 fontColor = style.overFontColor; 80 else 81 fontColor = style.fontColor; 82 if (fontColor != null) label.getStyle().fontColor = fontColor; 83 super.draw(batch, parentAlpha); 84 } 85 86 public Label getLabel () { 87 return label; 88 } 89 90 public Cell getLabelCell () { 91 return getCell(label); 92 } 93 94 public void setText (String text) { 95 label.setText(text); 96 } 97 98 public CharSequence getText () { 99 return label.getText(); 100 } 101 102 /** The style for a text button, see {@link TextButton}. 103 * @author Nathan Sweet */ 104 static public class TextButtonStyle extends ButtonStyle { 105 public BitmapFont font; 106 /** Optional. */ 107 public Color fontColor, downFontColor, overFontColor, checkedFontColor, checkedOverFontColor, disabledFontColor; 108 109 public TextButtonStyle () { 110 } 111 112 public TextButtonStyle (Drawable up, Drawable down, Drawable checked, BitmapFont font) { 113 super(up, down, checked); 114 this.font = font; 115 } 116 117 public TextButtonStyle (TextButtonStyle style) { 118 super(style); 119 this.font = style.font; 120 if (style.fontColor != null) this.fontColor = new Color(style.fontColor); 121 if (style.downFontColor != null) this.downFontColor = new Color(style.downFontColor); 122 if (style.overFontColor != null) this.overFontColor = new Color(style.overFontColor); 123 if (style.checkedFontColor != null) this.checkedFontColor = new Color(style.checkedFontColor); 124 if (style.checkedOverFontColor != null) this.checkedFontColor = new Color(style.checkedOverFontColor); 125 if (style.disabledFontColor != null) this.disabledFontColor = new Color(style.disabledFontColor); 126 } 127 } 128 } 129