diff --git a/src/main/java/com/cleanroommc/modularui/widgets/DropDownMenu.java b/src/main/java/com/cleanroommc/modularui/widgets/DropDownMenu.java index c7ab27230..ca3f65dba 100644 --- a/src/main/java/com/cleanroommc/modularui/widgets/DropDownMenu.java +++ b/src/main/java/com/cleanroommc/modularui/widgets/DropDownMenu.java @@ -9,6 +9,7 @@ import com.cleanroommc.modularui.screen.viewport.ModularGuiContext; import com.cleanroommc.modularui.theme.WidgetTheme; import com.cleanroommc.modularui.theme.WidgetThemeEntry; +import com.cleanroommc.modularui.utils.Alignment; import com.cleanroommc.modularui.widget.ScrollWidget; import com.cleanroommc.modularui.widget.SingleChildWidget; import com.cleanroommc.modularui.widget.Widget; @@ -29,6 +30,7 @@ public class DropDownMenu extends SingleChildWidget implements Int private IDrawable arrowClosed; private IDrawable arrowOpened; private static int offsetArrow = 5; + private static int offsetLeft = 5; public DropDownMenu() { menu.setEnabled(false); @@ -64,19 +66,19 @@ public DropDownMenu setMaxItemsToDisplay(int maxItems) { } public DropDownMenu addChoice(ItemSelected onSelect, IDrawable... drawable) { - DropDownItem item = new DropDownItem(); + DropDownItem item = new DropDownItem(drawable); return addChoice(index -> item.onMouseReleased(m -> { - menu.setOpened(false); - menu.setCurrentIndex(index); - onSelect.selected(this); - return true; - }) - .overlay(drawable)); + menu.setOpened(false); + menu.setCurrentIndex(index); + onSelect.selected(this); + return true; + })); } public DropDownMenu addChoice(ItemSelected onSelect, String text) { - return addChoice(onSelect, IKey.str(text)); + IKey key = IKey.str(text).alignment(Alignment.CenterLeft); + return addChoice(onSelect, key); } public DropDownMenu setDropDownDirection(DropDownDirection direction) { @@ -102,17 +104,15 @@ public void draw(ModularGuiContext context, WidgetThemeEntry widgetTheme) { Area area = getArea(); int smallerSide = Math.min(area.width, area.height); WidgetTheme wt = getActiveWidgetTheme(widgetTheme, isHovering()); - if (menu.getSelectedItem() != null) { - menu.getSelectedItem().setEnabled(true); - menu.getSelectedItem().drawBackground(context, widgetTheme); - menu.getSelectedItem().draw(context, widgetTheme); - menu.getSelectedItem().drawForeground(context); - menu.getSelectedItem().drawOverlay(context, widgetTheme); + DropDownItem selectedItem = menu.getSelectedItem(); + int arrowSize = smallerSide / 2; + if (selectedItem != null) { + selectedItem.setEnabled(true); + selectedItem.getDrawable().draw(context, offsetLeft, 0, area.width - arrowSize - offsetArrow, area.height, wt); } else { - NONE.draw(context, 0, 0, area.width, area.height, wt); + NONE.draw(context, offsetLeft, 0, area.width, area.height, wt); } - int arrowSize = smallerSide / 2; if (menu.isOpen()) { arrowOpened.draw(context, area.width - arrowSize - offsetArrow, arrowSize / 2, arrowSize, arrowSize, wt); } else { @@ -151,7 +151,7 @@ private static class DropDownWrapper extends ScrollWidget { private DropDownDirection direction = DropDownDirection.DOWN; private int maxItemsOnDisplay = 10; - private final List children = new ArrayList<>(); + private final List children = new ArrayList<>(); private boolean open; private int count = 0; private int currentIndex = -1; @@ -194,11 +194,11 @@ public void setCurrentIndex(int currentIndex) { return Collections.unmodifiableList(children); } - public IWidget getSelectedItem() { + public DropDownItem getSelectedItem() { if (currentIndex < 0 || currentIndex >= count) { return null; } - return getChildren().get(currentIndex); + return children.get(currentIndex); } @Override @@ -226,9 +226,7 @@ public void onResized() { List children = getChildren(); for (int i = 0; i < children.size(); i++) { IWidget child = children.get(i); - child.getArea().setSize(parentArea.width, parentArea.height); - child.getArea().setPos(0, parentArea.height * i); - child.getFlex().left(0).top(parentArea.height * i); + child.getFlex().left(offsetLeft).top(parentArea.height * i).width(parentArea.width); child.setEnabled(open); } } @@ -240,6 +238,17 @@ private void rebuild() { public static class DropDownItem extends ButtonWidget { + private final IDrawable drawable; + + public DropDownItem(IDrawable[] drawable) { + this.drawable = IDrawable.of(drawable); + overlay(drawable); + } + + public IDrawable getDrawable() { + return drawable; + } + @Override public boolean canClickThrough() { return false;