aboutsummaryrefslogtreecommitdiff
path: root/src/Java/binnie/craftgui/controls/button/ControlButton.java
blob: b41cb65116e80d511362eda5386481ccdd3c7f31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package binnie.craftgui.controls.button;

import binnie.craftgui.controls.ControlText;
import binnie.craftgui.controls.core.Control;
import binnie.craftgui.core.Attribute;
import binnie.craftgui.core.CraftGUI;
import binnie.craftgui.core.IWidget;
import binnie.craftgui.core.geometry.TextJustification;
import binnie.craftgui.core.renderer.Renderer;
import binnie.craftgui.events.EventButtonClicked;
import binnie.craftgui.events.EventHandler.Origin;
import binnie.craftgui.events.EventMouse.Down;
import binnie.craftgui.events.EventMouse.Down.Handler;
import binnie.craftgui.resource.minecraft.CraftGUITexture;

public class ControlButton
  extends Control
{
  private ControlText textWidget;
  private String text;
  
  public ControlButton(IWidget parent, float x, float y, float width, float height)
  {
    super(parent, x, y, width, height);
    
    addAttribute(Attribute.MouseOver);
    
    addEventHandler(new EventMouse.Down.Handler()
    {
      public void onEvent(EventMouse.Down event)
      {
        ControlButton.this.callEvent(new EventButtonClicked(ControlButton.this.getWidget()));
        ControlButton.this.onMouseClick(event);
      }
    }.setOrigin(EventHandler.Origin.Self, this));
  }
  
  protected void onMouseClick(EventMouse.Down event) {}
  
  public ControlButton(IWidget parent, float x, float y, float width, float height, String text)
  {
    this(parent, x, y, width, height);
    
    this.text = text;
    this.textWidget = new ControlText(this, getArea(), text, TextJustification.MiddleCenter);
  }
  
  public void onUpdateClient()
  {
    if (this.textWidget != null) {
      this.textWidget.setValue(getText());
    }
  }
  
  public String getText()
  {
    return this.text;
  }
  
  public void onRenderBackground()
  {
    Object texture = CraftGUITexture.ButtonDisabled;
    if (isMouseOver()) {
      texture = CraftGUITexture.ButtonHighlighted;
    } else if (isEnabled()) {
      texture = CraftGUITexture.Button;
    }
    CraftGUI.Render.texture(texture, getArea());
  }
}