package com.lucasdnd.ags.ui;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import com.lucasdnd.ags.system.ResourceLoader;
public class RadioButtonGroup extends UIComponent {
String title;
RadioButton[] radioButtons;
final float lineHeight = 32f;
public RadioButtonGroup (String title, float xPos, float yPos, RadioButton[] radioButtons) {
this.title = title;
this.radioButtons = radioButtons;
this.xPos = xPos;
this.yPos = yPos;
// Position of the Radio Buttons
for (int i = 0; i < radioButtons.length; i++) {
radioButtons[i].setxPos(this.xPos);
radioButtons[i].setyPos(this.yPos + lineHeight * (i + 1));
}
}
public void update(boolean leftMouseClicked, int mouseX, int mouseY) throws SlickException {
// When clicked, unselect all of them, then select the one that was clicked
boolean anyRadioButtonsClicked = false;
for (RadioButton rb: radioButtons) {
if (rb.gotLeftClicked(leftMouseClicked, mouseX, mouseY) && rb.isEnabled()) {
anyRadioButtonsClicked = true;
}
}
if (anyRadioButtonsClicked) {
// Set a selected one
RadioButton selectedRadioButton = null;
for (RadioButton rb : radioButtons) {
if (rb.gotLeftClicked(leftMouseClicked, mouseX, mouseY) && rb.isEnabled()) {
rb.setSelected(true);
selectedRadioButton = rb;
}
}
// Disable the others
for (RadioButton rb : radioButtons) {
if (rb != selectedRadioButton) {
rb.setSelected(false);
}
}
}
}
public void render (GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
ResourceLoader.getInstance().tinyLightGrayFont.drawString(xPos + 1f, yPos + 1f, title);
ResourceLoader.getInstance().tinyBlackFont.drawString(xPos, yPos, title);
for (RadioButton rb : radioButtons) {
rb.render(container, game, g);
}
}
/**
* Returns the selected RadioButton
* @return
*/
public RadioButton getSelected() {
for (RadioButton rb : radioButtons) {
if (rb.isSelected()) {
return rb;
}
}
return null;
}
/**
* Returns the index of the selected RadioButton
* @return
*/
public int getSelectedIndex() {
for (int i = 0; i < radioButtons.length; i++) {
if (radioButtons[i].isSelected()) {
return i;
}
}
return -1;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public RadioButton[] getRadioButtons() {
return radioButtons;
}
public void setRadioButtons(RadioButton[] radioButtons) {
this.radioButtons = radioButtons;
}
}