diff options
author | syeyoung <cyong06@naver.com> | 2021-02-04 21:55:02 +0900 |
---|---|---|
committer | syeyoung <cyong06@naver.com> | 2021-02-04 21:55:02 +0900 |
commit | c37bda238c5072fa50dfa2d87aa137e1bdf5c4ac (patch) | |
tree | 5a276b3db53c9275cb0da74cf8aed0cfa0e79491 /src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MStringSelectionButton.java | |
parent | 809f6b70870ec2a0fd4e4abfd24539e30d8db11b (diff) | |
download | Skyblock-Dungeons-Guide-c37bda238c5072fa50dfa2d87aa137e1bdf5c4ac.tar.gz Skyblock-Dungeons-Guide-c37bda238c5072fa50dfa2d87aa137e1bdf5c4ac.tar.bz2 Skyblock-Dungeons-Guide-c37bda238c5072fa50dfa2d87aa137e1bdf5c4ac.zip |
move guis
Diffstat (limited to 'src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MStringSelectionButton.java')
-rwxr-xr-x | src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MStringSelectionButton.java | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MStringSelectionButton.java b/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MStringSelectionButton.java new file mode 100755 index 00000000..b3f78fa3 --- /dev/null +++ b/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MStringSelectionButton.java @@ -0,0 +1,74 @@ +package kr.syeyoung.dungeonsguide.gui.elements; + +import kr.syeyoung.dungeonsguide.gui.MPanel; +import lombok.Getter; +import lombok.Setter; + +import java.awt.*; +import java.util.List; + +@Getter +@Setter +public class MStringSelectionButton extends MPanel { + + private List<String> possible; + private int selectedIndex; + + private MButton dec; + private MButton inc; + private MLabel selected; + + @Getter + @Setter + private Runnable onUpdate; + + public MStringSelectionButton(final List<String> possible, String defaultValue) { + this.possible = possible; + selectedIndex = possible.indexOf(defaultValue); + if (selectedIndex == -1) selectedIndex = 0; + + dec = new MButton(); dec.setText("<"); add(dec); + inc = new MButton(); inc.setText(">"); add(inc); + selected = new MLabel(); updateSelected(); add(selected); + + dec.setOnActionPerformed(new Runnable() { + @Override + public void run() { + selectedIndex++; + if (selectedIndex >= possible.size()) selectedIndex = 0; + updateSelected(); + onUpdate.run(); + } + }); + inc.setOnActionPerformed(new Runnable() { + @Override + public void run() { + selectedIndex --; + if (selectedIndex < 0) selectedIndex = possible.size() - 1; + updateSelected(); + onUpdate.run(); + } + }); + } + + public String selectionToDisplay(String selection) { + return selection; + } + + public String getSelected() { + if (possible.size() == 0) return null; + return possible.get(selectedIndex); + } + + private void updateSelected() { + if (possible.size() == 0) selected.setText("-Empty-"); + else selected.setText(selectionToDisplay(possible.get(selectedIndex))); + } + + @Override + public void onBoundsUpdate() { + dec.setBounds(new Rectangle(0,0,getBounds().height, getBounds().height)); + inc.setBounds(new Rectangle(getBounds().width - getBounds().height, 0, getBounds().height, getBounds().height)); + selected.setBounds(new Rectangle(getBounds().height, 0, getBounds().width - getBounds().height - getBounds().height, getBounds().height)); + } +} |