Package com.drakulo.games.ais.ui.component.button

Source Code of com.drakulo.games.ais.ui.component.button.TextButton

package com.drakulo.games.ais.ui.component.button;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import com.drakulo.games.ais.ui.FontHelper;
import com.drakulo.games.ais.ui.component.Bindable;
import com.drakulo.games.ais.ui.twlbridge.RootPane;

import de.matthiasmann.twl.Label;
import de.matthiasmann.twl.Widget;

public class TextButton extends Button implements Bindable {
  private static final String THEME_ENABLED = "centeredButton";
  private static final String THEME_DISABLED = "disabledButtonLabel";
  private static final String THEME_CLICKED = "clickedButtonLabel";

  /** The text label */
  private Label label;

  public TextButton(String text) {
    this(text, 0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT);
  }

  /**
   * Complete constructor
   *
   * @param text
   *            the message
   * @param x
   *            the X coordinate
   * @param y
   *            the Y coordinate
   * @param width
   *            the button width
   * @param height
   *            the button height
   */
  public TextButton(String text, int x, int y, int width, int height) {
    createLabel();
    label.setText(FontHelper.firstToUpper(text));

    label.setSize(width, height);
    setWidth(width);
    setHeight(height);

    setPosition(x, y);
    label.setPosition(x, y);
  }

  /**
   * Simple constructor
   *
   * @param text
   *            the message
   * @param x
   *            the X coordinate
   * @param y
   *            the Y coordinate
   */
  public TextButton(String text, int x, int y) {
    this(text, x, y, DEFAULT_WIDTH, DEFAULT_HEIGHT);
  }

  @Override
  public void render(Graphics g) throws SlickException {
    // Render the button background
    super.render(g);
  }

  @Override
  public void update(Input input) throws SlickException {
    super.update(input);
    label.setVisible(isShown());
    final String lastTheme = label.getTheme();
    if (state == PRESSED) {
      label.setTheme(THEME_CLICKED);
    } else if (disabled) {
      label.setTheme(THEME_DISABLED);
    } else {
      label.setTheme(THEME_ENABLED);
    }
    if (!lastTheme.equals(label.getTheme())) {
      label.reapplyTheme();
    }
  }

  @Override
  public void setPosition(int x, int y) {
    super.setPosition(x, y);
    if (label == null) {
      createLabel();
    }
    label.setPosition(x, y);
  }

  public void setText(String text) {
    label.setText(FontHelper.firstToUpper(text));
  }

  /**
   * Bind the button label to the TWL root pane
   *
   * @param pane
   *            - the TWL root pane
   */
  public void bindTo(RootPane pane) {
    pane.add(label);
  }

  /**
   * Initialize the label with a centered style for text rendering
   */
  private void createLabel() {
    label = new Label();
    label.setTheme(THEME_ENABLED);
  }

  @Override
  public void setSize(int width, int height) {
    super.setSize(width, height);
    label.setSize(width, height);
  }

  @Override
  public void enable() {
    super.enable();
    label.setTheme(THEME_ENABLED);
    label.reapplyTheme();
  }

  @Override
  public void disable() {
    super.disable();
    label.setTheme(THEME_CLICKED);
    label.validateLayout();
    label.reapplyTheme();
  }

  @Override
  public Widget getBindable() {
    return label;
  }

}
TOP

Related Classes of com.drakulo.games.ais.ui.component.button.TextButton

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.