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:
@@ -16,14 +16,10 @@ import java.util.UUID;
|
||||
|
||||
public class CustomImageObject implements GuiEntryListEntry {
|
||||
|
||||
public CustomImageObject(String name, UUID assetUUID) {
|
||||
public CustomImageObject(String name, UUID assetUUID) throws IOException {
|
||||
this.name = name;
|
||||
|
||||
try {
|
||||
setLinkedAsset(assetUUID);
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
setLinkedAsset(assetUUID);
|
||||
}
|
||||
|
||||
@Getter @Setter private String name;
|
||||
@@ -43,7 +39,7 @@ public class CustomImageObject implements GuiEntryListEntry {
|
||||
|
||||
if(asset instanceof ReplayImageAsset) {
|
||||
setImage(((ReplayImageAsset)asset).getObject());
|
||||
} else {
|
||||
} else if(asset != null) {
|
||||
throw new UnsupportedOperationException("A CustomImageObject requires a ReplayImageAsset");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,8 +141,12 @@ public class GuiObjectManager extends GuiScreen {
|
||||
addButton = new GuiAdvancedButton(0, 0, 0, 20, I18n.format("replaymod.gui.add"), new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
CustomImageObject customImageObject = new CustomImageObject(I18n.format("replaymod.gui.objects.defaultname"), null);
|
||||
objectList.addElement(customImageObject);
|
||||
try {
|
||||
CustomImageObject customImageObject = new CustomImageObject(I18n.format("replaymod.gui.objects.defaultname"), null);
|
||||
objectList.addElement(customImageObject);
|
||||
} catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}, null);
|
||||
|
||||
@@ -165,11 +169,44 @@ public class GuiObjectManager extends GuiScreen {
|
||||
CustomImageObject selectedObject = objectList.getElement(selectionIndex);
|
||||
if(selectedObject != null) {
|
||||
disableElements.setEnabled(true);
|
||||
|
||||
nameInput.setText(selectedObject.getName());
|
||||
|
||||
//setting the dropdown value
|
||||
int sel = 0;
|
||||
if(selectedObject.getLinkedAsset() != null) {
|
||||
int i = 0;
|
||||
for(GuiEntryListValueEntry<UUID> entry : assetDropdown.getAllElements()) {
|
||||
if(selectedObject.getLinkedAsset().equals(entry.getValue())) {
|
||||
sel = i;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
assetDropdown.setSelectionIndexQuietly(sel);
|
||||
|
||||
} else {
|
||||
disableElements.setEnabled(false);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
assetDropdown.addSelectionListener(new SelectionListener() {
|
||||
@Override
|
||||
public void onSelectionChanged(int selectionIndex) {
|
||||
CustomImageObject selectedObject = objectList.getElement(objectList.getSelectionIndex());
|
||||
GuiEntryListValueEntry<UUID> entry = assetDropdown.getElement(selectionIndex);
|
||||
if(selectedObject != null && entry != null) {
|
||||
try {
|
||||
selectedObject.setLinkedAsset(entry.getValue());
|
||||
} catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
String[] labelStrings = new String[] {
|
||||
@@ -200,12 +237,12 @@ public class GuiObjectManager extends GuiScreen {
|
||||
opacityInputs = new ComposedElement(opacityInput);
|
||||
numberInputs = new ComposedElement(anchorInputs, positionInputs, scaleInputs, orientationInputs, opacityInputs);
|
||||
|
||||
for(int i = numberInputs.getParts().length-1; i >= 0; i--) {
|
||||
int yPos = this.height-5-10-(25*(numberInputs.getParts().length-i));
|
||||
for(int i = numberInputs.getParts().size()-1; i >= 0; i--) {
|
||||
int yPos = this.height-5-10-(25*(numberInputs.getParts().size()-i));
|
||||
DelegatingElement button = keyframeButton(10, yPos, i);
|
||||
GuiString label = new GuiString(35, yPos + 6, Color.WHITE, labelStrings[i]);
|
||||
|
||||
ComposedElement child = (ComposedElement)numberInputs.getParts()[i];
|
||||
ComposedElement child = (ComposedElement)numberInputs.getParts().get(i);
|
||||
int x = 0;
|
||||
for(GuiElement el : child.getParts()) {
|
||||
GuiDraggingNumberInput dni = (GuiDraggingNumberInput)el;
|
||||
@@ -318,6 +355,11 @@ public class GuiObjectManager extends GuiScreen {
|
||||
Point mousePos = MouseUtils.getMousePos();
|
||||
allElements.buttonPressed(mc, mousePos.getX(), mousePos.getY(), typedChar, keyCode);
|
||||
|
||||
CustomImageObject selectedObject = objectList.getElement(objectList.getSelectionIndex());
|
||||
if(selectedObject != null) {
|
||||
selectedObject.setName(nameInput.getText().trim());
|
||||
}
|
||||
|
||||
super.keyTyped(typedChar, keyCode);
|
||||
}
|
||||
|
||||
@@ -364,14 +406,17 @@ public class GuiObjectManager extends GuiScreen {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
if(!enabled) return;
|
||||
public boolean mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
if(!enabled) return false;
|
||||
super.mouseClick(mc, mouseX, mouseY, button);
|
||||
int time = (int) getTimeAt(mouseX, mouseY);
|
||||
if(time != -1) {
|
||||
cursorPosition = time;
|
||||
dragging = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -179,8 +179,8 @@ public class GuiReplaySpeedSlider extends GuiButton implements GuiElement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
mousePressed(mc, mouseX, mouseY);
|
||||
public boolean mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
return mousePressed(mc, mouseX, mouseY);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,8 +33,8 @@ public abstract class DelegatingElement implements GuiElement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
delegate().mouseClick(mc, mouseX, mouseY, button);
|
||||
public boolean mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
return delegate().mouseClick(mc, mouseX, mouseY, button);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -52,10 +52,12 @@ public class GuiAdvancedButton extends GuiButton implements GuiElement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
public boolean mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
if (isHovering(mouseX, mouseY)) {
|
||||
performAction();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -27,8 +27,8 @@ public class GuiAdvancedCheckBox extends GuiCheckBox implements GuiElement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
mousePressed(mc, mouseX, mouseY);
|
||||
public boolean mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
return mousePressed(mc, mouseX, mouseY);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -67,8 +67,9 @@ public class GuiAdvancedTextField extends GuiTextField implements GuiElement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
public boolean mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
mouseClicked(mouseX, mouseY, button);
|
||||
return isHovering(mouseX, mouseY);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -23,12 +23,14 @@ public class GuiDraggingNumberInput extends GuiNumberInputWithText {
|
||||
private boolean clicked;
|
||||
|
||||
@Override
|
||||
public void mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
public boolean mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
if(MouseUtils.isMouseWithinBounds(xPosition, yPosition, width, height) && isEnabled) {
|
||||
dragging = false;
|
||||
clicked = true;
|
||||
prevMouseX = mouseX;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -133,11 +133,10 @@ public class GuiDropdown<T extends GuiEntryListEntry> extends GuiAdvancedTextFie
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClicked(int xPos, int yPos, int mouseButton) {
|
||||
mouseClickedResult(xPos, yPos);
|
||||
public boolean mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
return mouseClickedResult(mouseX, mouseY);
|
||||
}
|
||||
|
||||
|
||||
public boolean mouseClickedResult(int xPos, int yPos) {
|
||||
boolean success = false;
|
||||
if(!isEnabled) return success;
|
||||
@@ -202,9 +201,18 @@ public class GuiDropdown<T extends GuiEntryListEntry> extends GuiAdvancedTextFie
|
||||
}
|
||||
|
||||
public void setSelectionIndex(int index) {
|
||||
setSelectionIndexQuietly(index);
|
||||
fireSelectionChangeEvent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the selection index without notifying SelectionChangeListeners.
|
||||
* @param index The Selection Index
|
||||
*/
|
||||
public void setSelectionIndexQuietly(int index) {
|
||||
this.selectionIndex = index;
|
||||
if(selectionIndex < 0) selectionIndex = -1;
|
||||
fireSelectionChangeEvent();
|
||||
if(selectionIndex >= elements.size()) selectionIndex = elements.size()-1;
|
||||
}
|
||||
|
||||
private void fireSelectionChangeEvent() {
|
||||
|
||||
@@ -9,7 +9,7 @@ public interface GuiElement {
|
||||
|
||||
boolean isHovering(int mouseX, int mouseY);
|
||||
|
||||
void mouseClick(Minecraft mc, int mouseX, int mouseY, int button);
|
||||
boolean mouseClick(Minecraft mc, int mouseX, int mouseY, int button);
|
||||
void mouseDrag(Minecraft mc, int mouseX, int mouseY, int button);
|
||||
void mouseRelease(Minecraft mc, int mouseX, int mouseY, int button);
|
||||
|
||||
|
||||
@@ -61,12 +61,14 @@ public class GuiScrollbar extends Gui implements GuiElement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
if(!enabled) return;
|
||||
public boolean mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
if(!enabled) return false;
|
||||
if (isHovering(mouseX, mouseY)) {
|
||||
draggingStart = mouseX;
|
||||
draggingStartPosition = sliderPosition;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -50,8 +50,8 @@ public class GuiString extends Gui implements GuiElement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
|
||||
public boolean mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -470,7 +470,7 @@ public class GuiTextArea extends Gui implements GuiElement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
public boolean mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
boolean hovering = isHovering(mouseX, mouseY);
|
||||
|
||||
if (hovering && isFocused() && button == 0) {
|
||||
@@ -486,6 +486,7 @@ public class GuiTextArea extends Gui implements GuiElement {
|
||||
}
|
||||
|
||||
setFocused(hovering);
|
||||
return hovering;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -32,13 +32,13 @@ public class GuiKeyframeTimeline extends GuiTimeline {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
if(!enabled) return;
|
||||
public boolean mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
if(!enabled) return false;
|
||||
//left mouse button
|
||||
if(button == 0) {
|
||||
long time = getTimeAt(mouseX, mouseY);
|
||||
if(time == -1) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
int tolerance = (int) (2 * Math.round(zoom * timelineLength / width));
|
||||
@@ -71,6 +71,8 @@ public class GuiKeyframeTimeline extends GuiTimeline {
|
||||
this.clickTime = currentTime;
|
||||
|
||||
}
|
||||
|
||||
return isHovering(mouseX, mouseY);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -24,14 +24,14 @@ public class GuiMarkerTimeline extends GuiTimeline {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
if(!enabled) return;
|
||||
public boolean mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
if(!enabled) return false;
|
||||
|
||||
//left mouse button
|
||||
if(button == 0) {
|
||||
long time = getTimeAt(mouseX, mouseY);
|
||||
if(time == -1) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
int tolerance = (int) (2 * Math.round(zoom * timelineLength / width));
|
||||
@@ -66,7 +66,7 @@ public class GuiMarkerTimeline extends GuiTimeline {
|
||||
|
||||
long time = getTimeAt(mouseX, mouseY);
|
||||
if(time == -1) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
int tolerance = (int) (2 * Math.round(zoom * timelineLength / width));
|
||||
@@ -82,6 +82,8 @@ public class GuiMarkerTimeline extends GuiTimeline {
|
||||
ReplayMod.replaySender.jumpToTime(closest.getRealTimestamp());
|
||||
}
|
||||
}
|
||||
|
||||
return isHovering(mouseX, mouseY);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -222,8 +222,8 @@ public class GuiTimeline extends Gui implements GuiElement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
|
||||
public boolean mouseClick(Minecraft mc, int mouseX, int mouseY, int button) {
|
||||
return isHovering(mouseX, mouseY);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user