Advertisement
hnOsmium0001

draft TextDropdown 11/152019-11-15

Nov 15th, 2019
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. package powerlessri.harmonics.gui.widget;
  2.  
  3. import com.google.common.base.Preconditions;
  4. import powerlessri.harmonics.gui.ITexture;
  5. import powerlessri.harmonics.gui.Texture;
  6. import powerlessri.harmonics.gui.widget.button.ColoredTextButton;
  7. import powerlessri.harmonics.gui.widget.button.SimpleIconButton;
  8. import powerlessri.harmonics.gui.widget.button.TextButton;
  9. import powerlessri.harmonics.gui.widget.panel.Panel;
  10. import powerlessri.harmonics.gui.widget.panel.VerticalList;
  11.  
  12. public class TextDropdown extends Dropdown<IWidget, Panel<IWidget>, VerticalList<ColoredTextButton>> {
  13.  
  14.     public static final ITexture TOGGLE_TO_EXPAND = Texture.portion();
  15.     public static final ITexture TOGGLE_TO_EXPAND_HOVERED = Texture.portion();
  16.     public static final ITexture TOGGLE_TO_COLLAPSE = Texture.portion();
  17.     public static final ITexture TOGGLE_TO_COLLAPSE_HOVERED = Texture.portion();
  18.  
  19.     private TextField textField;
  20.  
  21.     public TextDropdown(int width, int height) {
  22.         this(width, height, height * 8);
  23.     }
  24.  
  25.     public TextDropdown(int width, int height, int listHeight) {
  26.         super(new Panel<IWidget>() {
  27.             {
  28.                 this.setDimensions(width, height);
  29.             }
  30.  
  31.             @Override
  32.             public void onInitialAttach() {
  33.                 TextDropdown parent = (TextDropdown) this.getParent();
  34.                 Preconditions.checkState(parent != null);
  35.  
  36.                 this.addChildren(new TextField(width, height));
  37.  
  38.                 SimpleIconButton toggle = new SimpleIconButton(TOGGLE_TO_EXPAND, TOGGLE_TO_EXPAND_HOVERED);
  39.                 toggle.setClickAction(b -> {
  40.                     parent.toggle();
  41.                     if (parent.isExpanded()) {
  42.                         toggle.setTextures(TOGGLE_TO_COLLAPSE, TOGGLE_TO_COLLAPSE_HOVERED);
  43.                     } else {
  44.                         toggle.setTextures(TOGGLE_TO_EXPAND, TOGGLE_TO_EXPAND_HOVERED);
  45.                     }
  46.                 });
  47.                 toggle.alignRight(getXRight());
  48.                 this.addChildren(toggle);
  49.             }
  50.         }, new VerticalList<>(width, listHeight));
  51.         this.setBackgroundRenderer(0, IBackgroundRenderer.NOTHING);
  52.         textField = (TextField) getLabel().getPanels().get(0);
  53.     }
  54.  
  55.     public void addOption(String option) {
  56.         getPanel().addChildren(ColoredTextButton.ofText(option, b -> {
  57.             textField.setText(option);
  58.             toggle();
  59.         }));
  60.     }
  61.  
  62.     public TextField getTextField() {
  63.         return textField;
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement