Buttony things

Centered button text
Label object
New dynamic text color
This commit is contained in:
opfromthestart 2021-04-17 11:01:03 -04:00
parent 7e852ff05f
commit 330ed1ab9b
2 changed files with 36 additions and 26 deletions

View File

@ -14,23 +14,24 @@ import ru.windcorp.progressia.client.graphics.font.Font;
import ru.windcorp.progressia.client.graphics.Colors; import ru.windcorp.progressia.client.graphics.Colors;
import ru.windcorp.progressia.client.graphics.gui.event.FocusEvent; import ru.windcorp.progressia.client.graphics.gui.event.FocusEvent;
import ru.windcorp.progressia.client.graphics.gui.event.HoverEvent; import ru.windcorp.progressia.client.graphics.gui.event.HoverEvent;
import ru.windcorp.progressia.client.graphics.gui.layout.LayoutAlign;
import ru.windcorp.progressia.client.graphics.input.bus.InputListener; import ru.windcorp.progressia.client.graphics.input.bus.InputListener;
import ru.windcorp.progressia.client.graphics.input.InputEvent; import ru.windcorp.progressia.client.graphics.input.InputEvent;
import ru.windcorp.progressia.client.graphics.input.KeyEvent; import ru.windcorp.progressia.client.graphics.input.KeyEvent;
public class Button extends Component { public class Button extends Component {
private Font font;
private String currentText;
private Vec2i currentSize; private Vec2i currentSize;
private String text;
private boolean isDisabled; private boolean isDisabled;
private boolean isClicked; private boolean isClicked;
private Label label;
private LayoutAlign align;
public <T extends InputEvent> Button(String name, Font font, String text, Consumer<Button> onClick) {//, InputListener<T> onClick, Class<? extends T> onClickClass) { public <T extends InputEvent> Button(String name, Label textLabel, Consumer<Button> onClick) {//, InputListener<T> onClick, Class<? extends T> onClickClass) {
super(name); super(name);
this.font = font; label = textLabel;
this.text = text; align = new LayoutAlign();
addChild(textLabel);
setPreferredSize(107,34); setPreferredSize(107,34);
Button inButton = (Button) setFocusable(true); Button inButton = (Button) setFocusable(true);
@ -44,16 +45,26 @@ public class Button extends Component {
addListener(new Object() { addListener(new Object() {
@Subscribe @Subscribe
public void onFocusChanged(FocusEvent e) { public void onFocusChanged(FocusEvent e) {
inButton.setText(e.getNewState() ? "Is Focused" : "Isn't focused"); inButton.setText(new Label("dummy",new Font().withColor(Colors.BLACK),e.getNewState() ? "Is Focused" : "Isn't focused"));
requestReassembly(); requestReassembly();
} }
}); });
addListener((Class<KeyEvent>) KeyEvent.class, (InputListener<KeyEvent>) e -> {isClicked = e.isPress(); addListener((Class<KeyEvent>) KeyEvent.class, (InputListener<KeyEvent>) e -> {if (e.isLeftMouseButton() && inButton.containsCursor())
if (!inButton.isDisabled()) {
onClick.accept(inButton); isClicked = e.isPress();
requestReassembly(); if (!inButton.isDisabled())
return true;}); {
onClick.accept(inButton);
}
requestReassembly();
}
else if (e.isLeftMouseButton())
{
setFocused(false);
}
return true;});
} }
public boolean isClicked() public boolean isClicked()
@ -72,9 +83,11 @@ public class Button extends Component {
return isDisabled; return isDisabled;
} }
public void setText(String newText) public void setText(Label newText)
{ {
text = newText; removeChild(label);
label = newText;
addChild(label);
requestReassembly(); requestReassembly();
} }
@ -102,22 +115,19 @@ public class Button extends Component {
{ {
target.fill(getX()+2, getY()+2, getWidth()-4, getHeight()-4, Colors.WHITE); target.fill(getX()+2, getY()+2, getWidth()-4, getHeight()-4, Colors.WHITE);
} }
//text Font tempFont = new Font().withColor(Colors.BLACK);
Font tempFont;
if (isDisabled()) if (isDisabled())
{ {
tempFont = font.withColor(Colors.GRAY_A); tempFont = tempFont.withColor(Colors.GRAY_A);
} }
else if (isClicked()) else if (isClicked())
{ {
tempFont = font.withColor(Colors.WHITE); tempFont = tempFont.withColor(Colors.WHITE);
} }
else
{ target.pushTransform(new Mat4().identity().translate( getX()+.5f*getWidth()-.5f*label.getPreferredSize().x, getY(), 0));
tempFont = font.withColor(Colors.BLACK); label = new Label(label.getName(), tempFont, label.getContentSupplier());
} label.assembleSelf(target);
target.pushTransform(new Mat4().identity().translate(getX(), getY(), -1000).scale(2));
target.addCustomRenderer(tempFont.assemble( (CharSequence) this.text, this.getWidth()));
target.popTransform(); target.popTransform();
} }
} }

View File

@ -59,11 +59,11 @@ public class LayerTestGUI extends GUILayer {
TestPlayerControls tpc = TestPlayerControls.getInstance(); TestPlayerControls tpc = TestPlayerControls.getInstance();
Button disableButton = new Button("TestButton", new Font().withColor(Colors.BLACK), "I'm in TestGUI", b -> {b.setDisable(!b.isDisabled());}); Button disableButton = new Button("TestButton", new Label("TestButtonLabel", new Font().withColor(Colors.BLACK), "I'm in TestGUI"), b -> {b.setDisable(!b.isDisabled());});
panel.addChild(disableButton); panel.addChild(disableButton);
panel.addChild(new Button("TestButton", new Font().withColor(Colors.BLACK), "I enable the above button", b -> {disableButton.setDisable(false);})); panel.addChild(new Button("TestButton2", new Label("TestButtonLabel2", new Font().withColor(Colors.BLACK), "I enable the above button"), b -> {disableButton.setDisable(false);b.takeFocus();}));
panel.addChild( panel.addChild(
new Label( new Label(