From 35174a9af664d5eedc8d89a82be120c4ac6119fa Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Wed, 15 Apr 2020 16:06:32 +0300 Subject: Add Environment annotations to interaction methods Should fix #41. --- .../io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java | 7 +++++++ .../java/io/github/cottonmc/cotton/gui/widget/WButton.java | 5 ++++- src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java | 1 + src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java | 9 ++++++--- .../java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java | 8 ++++++-- src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java | 1 + .../java/io/github/cottonmc/cotton/gui/widget/WTextField.java | 9 ++++++--- .../java/io/github/cottonmc/cotton/gui/widget/WWidget.java | 10 ++++++++++ 8 files changed, 41 insertions(+), 9 deletions(-) (limited to 'src/main') diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java index 7d19288..b5e1344 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java @@ -105,6 +105,7 @@ public abstract class WAbstractSlider extends WWidget { return true; } + @Environment(EnvType.CLIENT) @Override public WWidget onMouseDown(int x, int y, int button) { // Check if cursor is inside or <=2px away from track @@ -114,6 +115,7 @@ public abstract class WAbstractSlider extends WWidget { return super.onMouseDown(x, y, button); } + @Environment(EnvType.CLIENT) @Override public void onMouseDrag(int x, int y, int button) { if (isFocused()) { @@ -122,6 +124,7 @@ public abstract class WAbstractSlider extends WWidget { } } + @Environment(EnvType.CLIENT) @Override public void onClick(int x, int y, int button) { moveSlider(x, y); @@ -136,6 +139,7 @@ public abstract class WAbstractSlider extends WWidget { if (value != previousValue) onValueChanged(value); } + @Environment(EnvType.CLIENT) @Override public WWidget onMouseUp(int x, int y, int button) { dragging = false; @@ -143,6 +147,7 @@ public abstract class WAbstractSlider extends WWidget { return super.onMouseUp(x, y, button); } + @Environment(EnvType.CLIENT) @Override public void onMouseScroll(int x, int y, double amount) { int previous = value; @@ -245,6 +250,7 @@ public abstract class WAbstractSlider extends WWidget { if (valueChangeListener != null) valueChangeListener.accept(value); } + @Environment(EnvType.CLIENT) @Override public void onKeyPressed(int ch, int key, int modifiers) { boolean valueChanged = false; @@ -272,6 +278,7 @@ public abstract class WAbstractSlider extends WWidget { } } + @Environment(EnvType.CLIENT) @Override public void onKeyReleased(int ch, int key, int modifiers) { if (pendingDraggingFinishedFromKeyboard && (isDecreasingKey(ch) || isIncreasingKey(ch))) { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java index fbf664d..a03bd2a 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java @@ -2,6 +2,8 @@ package io.github.cottonmc.cotton.gui.widget; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.widget.data.Alignment; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.widget.AbstractButtonWidget; import net.minecraft.client.sound.PositionedSoundInstance; @@ -66,7 +68,8 @@ public class WButton extends WWidget { public void setSize(int x, int y) { super.setSize(x, 20); } - + + @Environment(EnvType.CLIENT) @Override public void onClick(int x, int y, int button) { super.onClick(x, y, button); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java index 75b102f..9163d4f 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java @@ -88,6 +88,7 @@ public class WLabel extends WWidget { } } + @Environment(EnvType.CLIENT) @Override public void onClick(int x, int y, int button) { Text hoveredText = getTextAt(x, y); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java index 373479b..e7bdeab 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java @@ -86,7 +86,8 @@ public abstract class WPanel extends WWidget { int pushDown = w.getY()+w.getHeight(); this.setSize(Math.max(this.getWidth(), pushRight), Math.max(this.getHeight(), pushDown)); } - + + @Environment(EnvType.CLIENT) @Override public WWidget onMouseUp(int x, int y, int button) { if (children.isEmpty()) return super.onMouseUp(x, y, button); @@ -101,7 +102,8 @@ public abstract class WPanel extends WWidget { } return super.onMouseUp(x, y, button); } - + + @Environment(EnvType.CLIENT) @Override public WWidget onMouseDown(int x, int y, int button) { if (children.isEmpty()) return super.onMouseDown(x, y, button); @@ -116,7 +118,8 @@ public abstract class WPanel extends WWidget { } return super.onMouseDown(x, y, button); } - + + @Environment(EnvType.CLIENT) @Override public void onMouseDrag(int x, int y, int button) { if (children.isEmpty()) return; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java index 0d67d00..152bf2f 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java @@ -3,6 +3,8 @@ package io.github.cottonmc.cotton.gui.widget; import io.github.cottonmc.cotton.gui.client.LibGuiClient; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.widget.data.Axis; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; public class WScrollBar extends WWidget { protected Axis axis = Axis.HORIZONTAL; @@ -150,12 +152,14 @@ public class WScrollBar extends WWidget { sliding = true; return this; } - + + @Environment(EnvType.CLIENT) @Override public void onMouseDrag(int x, int y, int button) { adjustSlider(x, y); } - + + @Environment(EnvType.CLIENT) @Override public WWidget onMouseUp(int x, int y, int button) { //TODO: Clicking before or after the handle should jump instead of scrolling diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java index 9761a6d..ab9333b 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java @@ -95,6 +95,7 @@ public class WText extends WWidget { } } + @Environment(EnvType.CLIENT) @Override public void onClick(int x, int y, int button) { if (button != 0) return; // only left clicks diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java index 97023aa..8f5c5c1 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java @@ -529,13 +529,15 @@ public class WTextField extends WWidget { renderButton(x, y); } - + + @Environment(EnvType.CLIENT) @Override public void onClick(int x, int y, int button) { requestFocus(); cursor = getCaretPos(this.text, x-OFFSET_X_TEXT); } - + + @Environment(EnvType.CLIENT) @Override public void onCharTyped(char ch) { if (this.text.length()