Skip to content

Commit

Permalink
Add ModRadioButtonGroup + ModToggleButton.addConsumer() (daviscook477…
Browse files Browse the repository at this point in the history
…#394)

* Add ModRadioButtonGroup + ModToggleButton.addConsumer()

* Remove unused import

---------

Co-authored-by: Kiooeht <[email protected]>
  • Loading branch information
Mindbomber2 and kiooeht authored Apr 18, 2023
1 parent c9493ee commit 7078d79
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
43 changes: 43 additions & 0 deletions mod/src/main/java/basemod/ModRadioButtonGroup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package basemod;

import java.util.ArrayList;
import java.util.List;

public class ModRadioButtonGroup {
private List<ModToggleButton> buttons;
private ModToggleButton selected;

public ModRadioButtonGroup(){
this.buttons = new ArrayList<ModToggleButton>();
}

public ModRadioButtonGroup(ModToggleButton... buttons){
for(ModToggleButton b : buttons){
addButton(b);
}
}

public boolean addButton(ModToggleButton b){
b.addConsumer(
button ->
{
selectButton(button);
});
return buttons.add(b);
}

public boolean removeButton(ModToggleButton b){
return buttons.remove(b);
}

public void selectButton(ModToggleButton b){
this.selected=b;
updateButtons();
}

public void updateButtons(){
for(ModToggleButton b : buttons){
b.enabled = selected.equals(b);
}
}
}
12 changes: 11 additions & 1 deletion mod/src/main/java/basemod/ModToggleButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
import com.megacrit.cardcrawl.helpers.ImageMaster;
import com.megacrit.cardcrawl.helpers.input.InputHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class ModToggleButton implements IUIElement {
private static final float TOGGLE_Y_DELTA = 0f;
private static final float TOGGLE_X_EXTEND = 12.0f;
private static final float HB_WIDTH_EXTENDED = 200.0f;


private List<Consumer<ModToggleButton>> consumers = new ArrayList<Consumer<ModToggleButton>>();
private Consumer<ModToggleButton> toggle;
Hitbox hb;
private float x;
Expand Down Expand Up @@ -97,6 +100,9 @@ public void update() {
private void onToggle() {
this.enabled = !enabled;
toggle.accept(this);
for(Consumer<ModToggleButton> c : consumers){
c.accept(this);
}
}

public void toggle() {
Expand Down Expand Up @@ -144,4 +150,8 @@ public float getX() {
public float getY() {
return y/Settings.scale;
}

public void addConsumer(Consumer<ModToggleButton> c){
consumers.add(c);
}
}

0 comments on commit 7078d79

Please sign in to comment.