Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 31 additions & 22 deletions src/main/java/com/cleanroommc/modularui/widgets/DropDownMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,7 @@ public class DropDownMenu extends SingleChildWidget<DropDownMenu> implements Int
private IDrawable arrowClosed;
private IDrawable arrowOpened;
private static int offsetArrow = 5;
private static int offsetLeft = 5;

public DropDownMenu() {
menu.setEnabled(false);
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -151,7 +151,7 @@ private static class DropDownWrapper extends ScrollWidget<DropDownWrapper> {

private DropDownDirection direction = DropDownDirection.DOWN;
private int maxItemsOnDisplay = 10;
private final List<IWidget> children = new ArrayList<>();
private final List<DropDownItem> children = new ArrayList<>();
private boolean open;
private int count = 0;
private int currentIndex = -1;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -226,9 +226,7 @@ public void onResized() {
List<IWidget> 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);
}
}
Expand All @@ -240,6 +238,17 @@ private void rebuild() {

public static class DropDownItem extends ButtonWidget<DropDownItem> {

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;
Expand Down