GuiElement interface's mouseClick method returns a boolean which is handled by ComposedElement to prevent mouse clicks on multiple elements at once
ComposedElement's entries are sorted so that GuiDropdown are always drawn on top Modified thrown UnsupportedOperationExceptions in CustomImageObject to not be thrown if asset is null
This commit is contained in:
@@ -3,23 +3,22 @@ package eu.crushedpixel.replaymod.gui.elements;
|
||||
import lombok.Getter;
|
||||
import net.minecraft.client.Minecraft;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class ComposedElement implements GuiElement {
|
||||
|
||||
private static final ComposedElementComparator COMPOSED_ELEMENT_COMPARATOR = new ComposedElementComparator();
|
||||
|
||||
@Getter
|
||||
private GuiElement[] parts;
|
||||
private List<GuiElement> parts = new ArrayList<GuiElement>();
|
||||
|
||||
public ComposedElement(GuiElement...parts) {
|
||||
this.parts = parts;
|
||||
this.parts = new ArrayList<GuiElement>(Arrays.asList(parts));
|
||||
}
|
||||
|
||||
public void addPart(GuiElement part) {
|
||||
GuiElement[] newParts = new GuiElement[parts.length+1];
|
||||
int i = 0;
|
||||
for(GuiElement e : parts) {
|
||||
newParts[i] = e;
|
||||
i++;
|
||||
}
|
||||
newParts[i] = part;
|
||||
this.parts = newParts;
|
||||
parts.add(part);
|
||||
Collections.sort(parts, COMPOSED_ELEMENT_COMPARATOR);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -47,10 +46,14 @@ public class ComposedElement implements GuiElement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
for (GuiElement part : parts) {
|
||||
part.mouseClick(mc, mouseX, mouseY, button);
|
||||
public boolean mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
boolean clicked = false;
|
||||
//iterate over elements in reverse order to first handle mouse clicks of elements that are drawn on top
|
||||
for (int i=0; i<parts.size(); i++) {
|
||||
clicked = parts.get(parts.size()-1-i).mouseClick(mc, mouseX, mouseY, button);
|
||||
if(clicked) break;
|
||||
}
|
||||
return clicked;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -87,4 +90,20 @@ public class ComposedElement implements GuiElement {
|
||||
part.setEnabled(enabled);
|
||||
}
|
||||
}
|
||||
|
||||
private static class ComposedElementComparator implements Comparator<GuiElement> {
|
||||
|
||||
@Override
|
||||
public int compare(GuiElement o1, GuiElement o2) {
|
||||
Boolean d1 = o1 instanceof GuiDropdown;
|
||||
Boolean d2 = o2 instanceof GuiDropdown;
|
||||
|
||||
return d1.compareTo(d2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user