Radio Buttons

Created RadioButton and RadioManager.
They work together well I guess.
😄
This commit is contained in:
opfromthestart 2021-04-18 19:44:50 -04:00
parent 4a1f1b7545
commit a984333c3a
3 changed files with 146 additions and 0 deletions

View File

@ -0,0 +1,87 @@
package ru.windcorp.progressia.client.graphics.gui;
import java.util.function.Consumer;
import org.lwjgl.glfw.GLFW;
import glm.mat._4.Mat4;
import glm.vec._2.i.Vec2i;
import ru.windcorp.progressia.client.graphics.Colors;
import ru.windcorp.progressia.client.graphics.flat.RenderTarget;
import ru.windcorp.progressia.client.graphics.input.KeyEvent;
import ru.windcorp.progressia.client.graphics.input.bus.InputListener;
public class RadioButton extends Interactable {
private RadioManager manager;
private boolean isSelected;
public RadioButton(String name, Label textLabel, Consumer<RadioButton> onSelect, RadioManager myManager)
{
super(name, textLabel);
setPreferredSize(textLabel.getPreferredSize().x+23,textLabel.getPreferredSize().y);
manager = myManager;
manager.addOption(this);
addListener((Class<KeyEvent>) KeyEvent.class, (InputListener<KeyEvent>) e -> {if ((e.isLeftMouseButton() && containsCursor()) || (e.getKey()==GLFW.GLFW_KEY_ENTER && isFocused()) )
{
isClicked = e.isPress();
if (!isDisabled() && !isClicked)
{
onSelect.accept(this);
manager.selectSelf(this);
}
requestReassembly();
return true;
}
return false;});
}
public boolean isSelected()
{
return isSelected;
}
public void setSelected(boolean selected)
{
isSelected = selected;
}
protected void assembleSelf(RenderTarget target) {
if (isDisabled())
{
target.fill(getX()+getWidth()-getHeight(), getY(), getHeight(), getHeight(), 0xFFE5E5E5);
}
else if (isClicked() || isHovered() || isFocused())
{
target.fill(getX()+getWidth()-getHeight(), getY(), getHeight(), getHeight(), 0xFF37A2E6);
}
else
{
target.fill(getX()+getWidth()-getHeight(), getY(), getHeight(), getHeight(), 0xFFCBCBD0);
}
//Inside area
if (!isClicked() && isHovered() && !isDisabled())
{
target.fill(getX()+getWidth()-getHeight()+2, getY()+2, getHeight()-4, getHeight()-4, 0xFFC3E4F7);
}
else if (!isClicked() || isDisabled())
{
target.fill(getX()+getWidth()-getHeight()+2, getY()+2, getHeight()-4, getHeight()-4, Colors.WHITE);
}
if (isSelected())
{
if (!isDisabled())
{
target.fill(getX()+getWidth()-getHeight()+4, getY()+4, getHeight()-8, getHeight()-8, 0xFF37A2E6);
}
else
{
target.fill(getX()+getWidth()-getHeight()+4, getY()+4, getHeight()-8, getHeight()-8, 0xFFC3E4F7);
}
}
target.pushTransform(new Mat4().identity().translate( getX(), getY(), 0));
label.assembleSelf(target);
target.popTransform();
}
}

View File

@ -0,0 +1,37 @@
package ru.windcorp.progressia.client.graphics.gui;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class RadioManager {
private List<RadioButton> options;
private int selectedOption;
public RadioManager()
{
options = Collections.synchronizedList(new CopyOnWriteArrayList<>());
}
public void addOption(RadioButton option)
{
options.add(option);
}
public int getSelected()
{
return selectedOption;
}
public void selectSelf(RadioButton option)
{
if (!options.contains(option))
{
return;
}
options.get(selectedOption).setSelected(false);
selectedOption = options.indexOf(option);
option.takeFocus();
option.setSelected(true);
}
}

View File

@ -33,6 +33,8 @@ import ru.windcorp.progressia.client.graphics.gui.GUILayer;
import ru.windcorp.progressia.client.graphics.gui.Button;
import ru.windcorp.progressia.client.graphics.gui.Label;
import ru.windcorp.progressia.client.graphics.gui.Panel;
import ru.windcorp.progressia.client.graphics.gui.RadioButton;
import ru.windcorp.progressia.client.graphics.gui.RadioManager;
import ru.windcorp.progressia.client.graphics.gui.layout.LayoutAlign;
import ru.windcorp.progressia.client.graphics.gui.layout.LayoutVertical;
import ru.windcorp.progressia.client.localization.Localizer;
@ -88,6 +90,26 @@ public class LayerTestGUI extends GUILayer {
)
);
RadioManager manager = new RadioManager();
panel.addChild(
new RadioButton(
"Radio1,1",
new Label("RadioLabel1,1",font,"Option 1"),
rb -> {},
manager
)
);
panel.addChild(
new RadioButton(
"Radio1,2",
new Label("RadioLabel1,2",font,"Option 2"),
rb -> {},
manager
)
);
panel.addChild(
new Label(
"IsFlyingDisplay",