Add callback for selection in dropdown GUIs

This commit is contained in:
johni0702
2015-11-07 19:00:55 +01:00
parent a6f9e06c83
commit 30e85b7096
2 changed files with 19 additions and 0 deletions

View File

@@ -32,6 +32,7 @@ import de.johni0702.minecraft.gui.element.AbstractGuiClickable;
import de.johni0702.minecraft.gui.element.GuiElement;
import de.johni0702.minecraft.gui.function.Clickable;
import de.johni0702.minecraft.gui.layout.VerticalLayout;
import de.johni0702.minecraft.gui.utils.Consumer;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.minecraft.client.gui.FontRenderer;
@@ -53,6 +54,8 @@ public abstract class AbstractGuiDropdownMenu<V, T extends AbstractGuiDropdownMe
@Getter
private boolean opened;
private Consumer<Integer> onSelection;
private GuiPanel dropdown;
private ReadableDimension size;
@@ -136,6 +139,7 @@ public abstract class AbstractGuiDropdownMenu<V, T extends AbstractGuiDropdownMe
@Override
public T setSelected(int selected) {
this.selected = selected;
onSelection(selected);
return getThis();
}
@@ -165,6 +169,18 @@ public abstract class AbstractGuiDropdownMenu<V, T extends AbstractGuiDropdownMe
return opened ? Collections.<GuiElement>singletonList(dropdown) : Collections.<GuiElement>emptyList();
}
@Override
public T onSelection(Consumer<Integer> consumer) {
this.onSelection = consumer;
return getThis();
}
public void onSelection(Integer value) {
if (onSelection != null) {
onSelection.consume(value);
}
}
@Override
public boolean mouseClick(ReadablePoint position, int button) {
Point pos = new Point(position);

View File

@@ -23,6 +23,7 @@
package de.johni0702.minecraft.gui.element.advanced;
import de.johni0702.minecraft.gui.element.GuiElement;
import de.johni0702.minecraft.gui.utils.Consumer;
public interface IGuiDropdownMenu<V, T extends IGuiDropdownMenu<V, T>> extends GuiElement<T> {
T setValues(V... values);
@@ -40,4 +41,6 @@ public interface IGuiDropdownMenu<V, T extends IGuiDropdownMenu<V, T>> extends G
V[] getValues();
boolean isOpened();
T onSelection(Consumer<Integer> consumer);
}