Event Handling
This commit is contained in:
parent
4894f5602c
commit
20c19444f2
8 changed files with 484 additions and 16 deletions
|
@ -58,4 +58,46 @@ public class TestScreen extends Screen {
|
|||
main.render(poseStack);
|
||||
super.render(poseStack, i, j, f);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseClicked(double x, double y, int button) {
|
||||
boolean res = super.mouseClicked(x, y, button);
|
||||
if (!res) {
|
||||
res = main.mouseClicked(x, y, button);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseReleased(double d, double e, int i) {
|
||||
boolean res = super.mouseReleased(d, e, i);
|
||||
if (!res) {
|
||||
res = main.mouseReleased(d, e, i);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(double d, double e) {
|
||||
super.mouseMoved(d, e);
|
||||
main.mouseMoved(d, e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseDragged(double d, double e, int i, double f, double g) {
|
||||
boolean res = super.mouseDragged(d, e, i, f, g);
|
||||
if (!res) {
|
||||
res = main.mouseDragged(d, e, i, f, g);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseScrolled(double d, double e, double f) {
|
||||
boolean res = super.mouseScrolled(d, e, f);
|
||||
if (!res) {
|
||||
res = main.mouseScrolled(d, e, f);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -62,7 +62,7 @@ public class Button extends Component<ButtonRenderer> {
|
|||
mouseX = x;
|
||||
mouseY = y;
|
||||
if (vanillaButton != null && relativeBounds.contains(x, y)) {
|
||||
|
||||
if (event == MouseEvent.DOWN) return vanillaButton.mouseClicked(x, y, 0);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -91,4 +91,80 @@ public class Button extends Component<ButtonRenderer> {
|
|||
public int getContentHeight() {
|
||||
return renderer.getHeight(component);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(double x, double y) {
|
||||
if (vanillaButton != null)
|
||||
vanillaButton.mouseMoved(x - relativeBounds.left, y - relativeBounds.top);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseClicked(double x, double y, int button) {
|
||||
if (vanillaButton != null)
|
||||
return vanillaButton.mouseClicked(x - relativeBounds.left, y - relativeBounds.top, button);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseReleased(double x, double y, int button) {
|
||||
if (vanillaButton != null)
|
||||
return vanillaButton.mouseReleased(x - relativeBounds.left, y - relativeBounds.top, button);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseDragged(double x, double y, int button, double x2, double y2) {
|
||||
if (vanillaButton != null)
|
||||
return vanillaButton.mouseDragged(
|
||||
x - relativeBounds.left,
|
||||
y - relativeBounds.top,
|
||||
button,
|
||||
x2 - relativeBounds.left,
|
||||
y2 - relativeBounds.top
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseScrolled(double x, double y, double f) {
|
||||
if (vanillaButton != null)
|
||||
return vanillaButton.mouseScrolled(x - relativeBounds.left, y - relativeBounds.top, f);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyPressed(int i, int j, int k) {
|
||||
if (vanillaButton != null)
|
||||
return vanillaButton.keyPressed(i, j, k);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyReleased(int i, int j, int k) {
|
||||
if (vanillaButton != null)
|
||||
return vanillaButton.keyReleased(i, j, k);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean charTyped(char c, int i) {
|
||||
if (vanillaButton != null)
|
||||
return vanillaButton.charTyped(c, i);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean changeFocus(boolean bl) {
|
||||
if (vanillaButton != null)
|
||||
return vanillaButton.changeFocus(bl);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMouseOver(double x, double y) {
|
||||
if (vanillaButton != null)
|
||||
return vanillaButton.isMouseOver(x, y);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,8 +7,9 @@ import org.betterx.ui.layout.values.DynamicSize;
|
|||
import org.betterx.ui.layout.values.Rectangle;
|
||||
|
||||
import com.mojang.blaze3d.vertex.PoseStack;
|
||||
import net.minecraft.client.gui.components.events.GuiEventListener;
|
||||
|
||||
public abstract class Component<R extends ComponentRenderer> implements ComponentWithBounds {
|
||||
public abstract class Component<R extends ComponentRenderer> implements ComponentWithBounds, GuiEventListener {
|
||||
protected final R renderer;
|
||||
protected final DynamicSize width;
|
||||
protected final DynamicSize height;
|
||||
|
@ -78,8 +79,8 @@ public abstract class Component<R extends ComponentRenderer> implements Componen
|
|||
}
|
||||
}
|
||||
|
||||
void mouseEvent(MouseEvent event, int x, int y) {
|
||||
onMouseEvent(event, x, y);
|
||||
boolean mouseEvent(MouseEvent event, int x, int y) {
|
||||
return onMouseEvent(event, x, y);
|
||||
}
|
||||
|
||||
public boolean onMouseEvent(MouseEvent event, int x, int y) {
|
||||
|
|
|
@ -105,12 +105,14 @@ public class HorizontalStack<R extends ComponentRenderer> extends Component<R> {
|
|||
}
|
||||
|
||||
@Override
|
||||
void mouseEvent(MouseEvent event, int x, int y) {
|
||||
boolean mouseEvent(MouseEvent event, int x, int y) {
|
||||
if (!onMouseEvent(event, x, y)) {
|
||||
for (Component<?> c : components) {
|
||||
c.mouseEvent(event, x - relativeBounds.left, y - relativeBounds.top);
|
||||
if (c.mouseEvent(event, x - relativeBounds.left, y - relativeBounds.top))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static HorizontalStack<?> centered(Component<?> c) {
|
||||
|
@ -142,4 +144,98 @@ public class HorizontalStack<R extends ComponentRenderer> extends Component<R> {
|
|||
public HorizontalStack<R> addFiller() {
|
||||
return addEmpty(DynamicSize.fill());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(double x, double y) {
|
||||
for (Component<?> c : components) {
|
||||
c.mouseMoved(x - relativeBounds.left, y - relativeBounds.top);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseClicked(double x, double y, int button) {
|
||||
for (Component<?> c : components) {
|
||||
if (c.mouseClicked(x - relativeBounds.left, y - relativeBounds.top, button))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseReleased(double x, double y, int button) {
|
||||
for (Component<?> c : components) {
|
||||
if (c.mouseReleased(x - relativeBounds.left, y - relativeBounds.top, button))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseDragged(double x, double y, int button, double x2, double y2) {
|
||||
for (Component<?> c : components) {
|
||||
if (c.mouseDragged(
|
||||
x - relativeBounds.left,
|
||||
y - relativeBounds.top,
|
||||
button,
|
||||
x2 - relativeBounds.left,
|
||||
y2 - relativeBounds.top
|
||||
))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseScrolled(double x, double y, double f) {
|
||||
for (Component<?> c : components) {
|
||||
if (c.mouseScrolled(x - relativeBounds.left, y - relativeBounds.top, f))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyPressed(int i, int j, int k) {
|
||||
for (Component<?> c : components) {
|
||||
if (c.keyPressed(i, j, k))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyReleased(int i, int j, int k) {
|
||||
for (Component<?> c : components) {
|
||||
if (c.keyReleased(i, j, k))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean charTyped(char cc, int i) {
|
||||
for (Component<?> c : components) {
|
||||
if (c.charTyped(cc, i))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean changeFocus(boolean bl) {
|
||||
for (Component<?> c : components) {
|
||||
if (c.changeFocus(bl))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMouseOver(double x, double y) {
|
||||
for (Component<?> c : components) {
|
||||
if (c.isMouseOver(x, y))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,8 +5,9 @@ import org.betterx.ui.layout.components.input.MouseEvent;
|
|||
import org.betterx.ui.layout.values.Rectangle;
|
||||
|
||||
import com.mojang.blaze3d.vertex.PoseStack;
|
||||
import net.minecraft.client.gui.components.events.GuiEventListener;
|
||||
|
||||
public class Panel implements ComponentWithBounds {
|
||||
public class Panel implements ComponentWithBounds, GuiEventListener {
|
||||
protected Component<?> child;
|
||||
public final Rectangle bounds;
|
||||
|
||||
|
@ -18,10 +19,11 @@ public class Panel implements ComponentWithBounds {
|
|||
this.child = c;
|
||||
}
|
||||
|
||||
public void mouseEvent(MouseEvent event, int x, int y) {
|
||||
public boolean mouseEvent(MouseEvent event, int x, int y) {
|
||||
if (child != null) {
|
||||
child.mouseEvent(event, x - bounds.left, y - bounds.top);
|
||||
return child.mouseEvent(event, x - bounds.left, y - bounds.top);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void calculateLayout() {
|
||||
|
@ -42,4 +44,73 @@ public class Panel implements ComponentWithBounds {
|
|||
public Rectangle getRelativeBounds() {
|
||||
return bounds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(double x, double y) {
|
||||
if (child != null)
|
||||
child.mouseMoved(x - bounds.left, y - bounds.top);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseClicked(double x, double y, int button) {
|
||||
if (child != null)
|
||||
return child.mouseClicked(x - bounds.left, y - bounds.top, button);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseReleased(double x, double y, int button) {
|
||||
if (child != null)
|
||||
return child.mouseReleased(x - bounds.left, y - bounds.top, button);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseDragged(double x, double y, int button, double x2, double y2) {
|
||||
if (child != null)
|
||||
return child.mouseDragged(x - bounds.left, y - bounds.top, button, x2 - bounds.left, y2 - bounds.top);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseScrolled(double x, double y, double f) {
|
||||
if (child != null)
|
||||
return child.mouseScrolled(x - bounds.left, y - bounds.top, f);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyPressed(int i, int j, int k) {
|
||||
if (child != null)
|
||||
return child.keyPressed(i, j, k);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyReleased(int i, int j, int k) {
|
||||
if (child != null)
|
||||
return child.keyReleased(i, j, k);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean charTyped(char c, int i) {
|
||||
if (child != null)
|
||||
return child.charTyped(c, i);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean changeFocus(boolean bl) {
|
||||
if (child != null)
|
||||
return child.changeFocus(bl);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMouseOver(double x, double y) {
|
||||
if (child != null)
|
||||
return child.isMouseOver(x, y);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -72,12 +72,14 @@ public class VerticalScroll<R extends ComponentRenderer, RS extends ScrollerRend
|
|||
}
|
||||
|
||||
@Override
|
||||
void mouseEvent(MouseEvent event, int x, int y) {
|
||||
boolean mouseEvent(MouseEvent event, int x, int y) {
|
||||
if (!onMouseEvent(event, x, y)) {
|
||||
if (child != null) {
|
||||
child.mouseEvent(event, x - relativeBounds.left, y - relativeBounds.top - scrollerOffset());
|
||||
if (child.mouseEvent(event, x - relativeBounds.left, y - relativeBounds.top - scrollerOffset()))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -108,7 +110,7 @@ public class VerticalScroll<R extends ComponentRenderer, RS extends ScrollerRend
|
|||
public boolean onMouseEvent(MouseEvent event, int x, int y) {
|
||||
if (event == MouseEvent.DOWN) {
|
||||
Rectangle scroller = scrollerRenderer.getScrollerBounds(relativeBounds);
|
||||
Rectangle picker = scrollerRenderer.getPickerBounds(relativeBounds, saveScrollerY(), scrollerHeight);
|
||||
Rectangle picker = scrollerRenderer.getPickerBounds(scroller, saveScrollerY(), scrollerHeight);
|
||||
if (picker.contains(x, y)) {
|
||||
mouseDown = true;
|
||||
mouseDownY = y;
|
||||
|
@ -117,7 +119,7 @@ public class VerticalScroll<R extends ComponentRenderer, RS extends ScrollerRend
|
|||
}
|
||||
} else if (event == MouseEvent.UP) {
|
||||
mouseDown = false;
|
||||
} else if (event == MouseEvent.MOVE && mouseDown) {
|
||||
} else if (event == MouseEvent.DRAG && mouseDown) {
|
||||
int delta = y - mouseDownY;
|
||||
scrollerY = scrollerDownY + delta;
|
||||
return true;
|
||||
|
@ -147,4 +149,88 @@ public class VerticalScroll<R extends ComponentRenderer, RS extends ScrollerRend
|
|||
public boolean showScrollBar() {
|
||||
return child.relativeBounds.height > relativeBounds.height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(double x, double y) {
|
||||
if (child != null)
|
||||
child.mouseMoved(x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseClicked(double x, double y, int button) {
|
||||
Rectangle scroller = scrollerRenderer.getScrollerBounds(relativeBounds);
|
||||
Rectangle picker = scrollerRenderer.getPickerBounds(scroller, saveScrollerY(), scrollerHeight);
|
||||
if (picker.contains((int) x, (int) y)) {
|
||||
mouseDown = true;
|
||||
mouseDownY = (int) y;
|
||||
scrollerDownY = saveScrollerY();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (child != null)
|
||||
return child.mouseClicked(x, y, button);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseReleased(double x, double y, int button) {
|
||||
mouseDown = false;
|
||||
if (child != null)
|
||||
return child.mouseReleased(x - relativeBounds.left, y - relativeBounds.top, button);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseDragged(double x, double y, int button, double x2, double y2) {
|
||||
if (mouseDown) {
|
||||
int delta = (int) y - mouseDownY;
|
||||
scrollerY = scrollerDownY + delta;
|
||||
return true;
|
||||
}
|
||||
if (child != null)
|
||||
return child.mouseDragged(x, y, button, x2, y2);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseScrolled(double x, double y, double f) {
|
||||
if (child != null)
|
||||
return child.mouseScrolled(x, y, f);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyPressed(int i, int j, int k) {
|
||||
if (child != null)
|
||||
return child.keyPressed(i, j, k);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyReleased(int i, int j, int k) {
|
||||
if (child != null)
|
||||
return child.keyReleased(i, j, k);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean charTyped(char c, int i) {
|
||||
if (child != null)
|
||||
return child.charTyped(c, i);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean changeFocus(boolean bl) {
|
||||
if (child != null)
|
||||
return child.changeFocus(bl);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMouseOver(double x, double y) {
|
||||
if (child != null)
|
||||
return child.isMouseOver(x, y);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -104,12 +104,14 @@ public class VerticalStack<R extends ComponentRenderer> extends Component<R> {
|
|||
}
|
||||
|
||||
@Override
|
||||
void mouseEvent(MouseEvent event, int x, int y) {
|
||||
boolean mouseEvent(MouseEvent event, int x, int y) {
|
||||
if (!onMouseEvent(event, x, y)) {
|
||||
for (Component<?> c : components) {
|
||||
c.mouseEvent(event, x - relativeBounds.left, y - relativeBounds.top);
|
||||
if (c.mouseEvent(event, x - relativeBounds.left, y - relativeBounds.top))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static VerticalStack<?> centered(Component<?> c) {
|
||||
|
@ -141,4 +143,98 @@ public class VerticalStack<R extends ComponentRenderer> extends Component<R> {
|
|||
public VerticalStack<R> addFiller() {
|
||||
return addEmpty(DynamicSize.fill());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(double x, double y) {
|
||||
for (Component<?> c : components) {
|
||||
c.mouseMoved(x - relativeBounds.left, y - relativeBounds.top);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseClicked(double x, double y, int button) {
|
||||
for (Component<?> c : components) {
|
||||
if (c.mouseClicked(x - relativeBounds.left, y - relativeBounds.top, button))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseReleased(double x, double y, int button) {
|
||||
for (Component<?> c : components) {
|
||||
if (c.mouseReleased(x - relativeBounds.left, y - relativeBounds.top, button))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseDragged(double x, double y, int button, double x2, double y2) {
|
||||
for (Component<?> c : components) {
|
||||
if (c.mouseDragged(
|
||||
x - relativeBounds.left,
|
||||
y - relativeBounds.top,
|
||||
button,
|
||||
x2 - relativeBounds.left,
|
||||
y2 - relativeBounds.top
|
||||
))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseScrolled(double x, double y, double f) {
|
||||
for (Component<?> c : components) {
|
||||
if (c.mouseScrolled(x - relativeBounds.left, y - relativeBounds.top, f))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyPressed(int i, int j, int k) {
|
||||
for (Component<?> c : components) {
|
||||
if (c.keyPressed(i, j, k))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyReleased(int i, int j, int k) {
|
||||
for (Component<?> c : components) {
|
||||
if (c.keyReleased(i, j, k))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean charTyped(char cc, int i) {
|
||||
for (Component<?> c : components) {
|
||||
if (c.charTyped(cc, i))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean changeFocus(boolean bl) {
|
||||
for (Component<?> c : components) {
|
||||
if (c.changeFocus(bl))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMouseOver(double x, double y) {
|
||||
for (Component<?> c : components) {
|
||||
if (c.isMouseOver(x, y))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
package org.betterx.ui.layout.components.input;
|
||||
|
||||
public enum MouseEvent {
|
||||
DOWN, UP, MOVE
|
||||
DOWN, UP, MOVE, DRAG, SCROLL
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue