Marked classes as client only
This commit is contained in:
parent
40af0b9ea8
commit
00509c03b2
15 changed files with 62 additions and 88 deletions
|
@ -10,6 +10,10 @@ import com.mojang.blaze3d.vertex.PoseStack;
|
|||
import net.minecraft.client.gui.screens.Screen;
|
||||
import net.minecraft.network.chat.Component;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class TestScreen extends Screen {
|
||||
public TestScreen(Component component) {
|
||||
super(component);
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package org.betterx.ui.layout.components;
|
||||
|
||||
import org.betterx.ui.layout.components.input.MouseEvent;
|
||||
import org.betterx.ui.layout.components.render.ComponentRenderer;
|
||||
import org.betterx.ui.layout.values.DynamicSize;
|
||||
import org.betterx.ui.layout.values.Rectangle;
|
||||
|
@ -9,6 +8,10 @@ import com.mojang.blaze3d.vertex.PoseStack;
|
|||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.Font;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
class ButtonRenderer implements ComponentRenderer {
|
||||
Button linkedButton;
|
||||
|
||||
|
@ -35,6 +38,7 @@ class ButtonRenderer implements ComponentRenderer {
|
|||
}
|
||||
}
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class Button extends Component<ButtonRenderer> {
|
||||
int mouseX, mouseY;
|
||||
final net.minecraft.network.chat.Component component;
|
||||
|
@ -57,18 +61,6 @@ public class Button extends Component<ButtonRenderer> {
|
|||
this.onTooltip = onTooltip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onMouseEvent(MouseEvent event, int x, int y) {
|
||||
mouseX = x;
|
||||
mouseY = y;
|
||||
if (vanillaButton != null && relativeBounds.contains(x, y)) {
|
||||
if (event == MouseEvent.DOWN) return vanillaButton.mouseClicked(x, y, 0);
|
||||
|
||||
return true;
|
||||
}
|
||||
return super.onMouseEvent(event, x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onBoundsChanged() {
|
||||
vanillaButton = new net.minecraft.client.gui.components.Button(
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package org.betterx.ui.layout.components;
|
||||
|
||||
import org.betterx.ui.layout.components.input.MouseEvent;
|
||||
import org.betterx.ui.layout.components.render.ComponentRenderer;
|
||||
import org.betterx.ui.layout.values.Alignment;
|
||||
import org.betterx.ui.layout.values.DynamicSize;
|
||||
|
@ -9,6 +8,10 @@ import org.betterx.ui.layout.values.Rectangle;
|
|||
import com.mojang.blaze3d.vertex.PoseStack;
|
||||
import net.minecraft.client.gui.components.events.GuiEventListener;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public abstract class Component<R extends ComponentRenderer> implements ComponentWithBounds, GuiEventListener {
|
||||
protected final R renderer;
|
||||
protected final DynamicSize width;
|
||||
|
@ -86,14 +89,6 @@ public abstract class Component<R extends ComponentRenderer> implements Componen
|
|||
}
|
||||
}
|
||||
|
||||
boolean mouseEvent(MouseEvent event, int x, int y) {
|
||||
return onMouseEvent(event, x, y);
|
||||
}
|
||||
|
||||
public boolean onMouseEvent(MouseEvent event, int x, int y) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString() + "(" + relativeBounds + ", " + width.calculatedSize() + "x" + height.calculatedSize() + ")";
|
||||
|
|
|
@ -2,6 +2,10 @@ package org.betterx.ui.layout.components;
|
|||
|
||||
import org.betterx.ui.layout.values.Rectangle;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public interface ComponentWithBounds {
|
||||
Rectangle getRelativeBounds();
|
||||
}
|
||||
|
|
|
@ -2,6 +2,10 @@ package org.betterx.ui.layout.components;
|
|||
|
||||
import org.betterx.ui.layout.values.DynamicSize;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class Empty extends Component {
|
||||
public Empty(
|
||||
DynamicSize width,
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package org.betterx.ui.layout.components;
|
||||
|
||||
import org.betterx.ui.layout.components.input.MouseEvent;
|
||||
import org.betterx.ui.layout.components.input.RelativeContainerEventHandler;
|
||||
import org.betterx.ui.layout.components.render.ComponentRenderer;
|
||||
import org.betterx.ui.layout.values.Alignment;
|
||||
|
@ -10,10 +9,14 @@ import org.betterx.ui.layout.values.Rectangle;
|
|||
import com.mojang.blaze3d.vertex.PoseStack;
|
||||
import net.minecraft.client.gui.components.events.GuiEventListener;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class HorizontalStack<R extends ComponentRenderer> extends Component<R> implements RelativeContainerEventHandler {
|
||||
protected final List<Component<?>> components = new LinkedList<>();
|
||||
|
||||
|
@ -114,17 +117,6 @@ public class HorizontalStack<R extends ComponentRenderer> extends Component<R> i
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean mouseEvent(MouseEvent event, int x, int y) {
|
||||
if (!onMouseEvent(event, x, y)) {
|
||||
for (Component<?> c : components) {
|
||||
if (c.mouseEvent(event, x - relativeBounds.left, y - relativeBounds.top))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static HorizontalStack<?> centered(Component<?> c) {
|
||||
return new HorizontalStack<>(DynamicSize.relative(1), DynamicSize.relative(1)).addFiller().add(c).addFiller();
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package org.betterx.ui.layout.components;
|
||||
|
||||
|
||||
import org.betterx.ui.layout.components.input.MouseEvent;
|
||||
import org.betterx.ui.layout.components.input.RelativeContainerEventHandler;
|
||||
import org.betterx.ui.layout.values.Rectangle;
|
||||
|
||||
|
@ -11,9 +10,13 @@ import net.minecraft.client.gui.components.events.GuiEventListener;
|
|||
import net.minecraft.client.gui.narration.NarratableEntry;
|
||||
import net.minecraft.client.gui.narration.NarrationElementOutput;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
|
||||
import java.util.List;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class Panel implements ComponentWithBounds, RelativeContainerEventHandler, NarratableEntry, Widget {
|
||||
protected Component<?> child;
|
||||
List<? extends GuiEventListener> listeners = List.of();
|
||||
|
@ -28,13 +31,6 @@ public class Panel implements ComponentWithBounds, RelativeContainerEventHandler
|
|||
listeners = List.of(c);
|
||||
}
|
||||
|
||||
public boolean mouseEvent(MouseEvent event, int x, int y) {
|
||||
if (child != null) {
|
||||
return child.mouseEvent(event, x - bounds.left, y - bounds.top);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void calculateLayout() {
|
||||
if (child != null) {
|
||||
child.updateContainerWidth(bounds.width);
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package org.betterx.ui.layout.components;
|
||||
|
||||
import org.betterx.ui.layout.components.input.MouseEvent;
|
||||
import org.betterx.ui.layout.components.render.ComponentRenderer;
|
||||
import org.betterx.ui.layout.components.render.ScrollerRenderer;
|
||||
import org.betterx.ui.layout.values.DynamicSize;
|
||||
|
@ -8,6 +7,10 @@ import org.betterx.ui.layout.values.Rectangle;
|
|||
|
||||
import com.mojang.blaze3d.vertex.PoseStack;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class VerticalScroll<R extends ComponentRenderer, RS extends ScrollerRenderer> extends Component<R> {
|
||||
protected Component<?> child;
|
||||
protected final RS scrollerRenderer;
|
||||
|
@ -71,17 +74,6 @@ public class VerticalScroll<R extends ComponentRenderer, RS extends ScrollerRend
|
|||
updateScrollViewMetrics();
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean mouseEvent(MouseEvent event, int x, int y) {
|
||||
if (!onMouseEvent(event, x, y)) {
|
||||
if (child != null) {
|
||||
if (child.mouseEvent(event, x - relativeBounds.left, y - relativeBounds.top - scrollerOffset()))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void renderInBounds(
|
||||
PoseStack poseStack,
|
||||
|
@ -113,27 +105,6 @@ public class VerticalScroll<R extends ComponentRenderer, RS extends ScrollerRend
|
|||
private int mouseDownY = 0;
|
||||
private int scrollerDownY = 0;
|
||||
|
||||
@Override
|
||||
public boolean onMouseEvent(MouseEvent event, int x, int y) {
|
||||
if (event == MouseEvent.DOWN) {
|
||||
Rectangle scroller = scrollerRenderer.getScrollerBounds(relativeBounds);
|
||||
Rectangle picker = scrollerRenderer.getPickerBounds(scroller, saveScrollerY(), scrollerHeight);
|
||||
if (picker.contains(x, y)) {
|
||||
mouseDown = true;
|
||||
mouseDownY = y;
|
||||
scrollerDownY = saveScrollerY();
|
||||
return true;
|
||||
}
|
||||
} else if (event == MouseEvent.UP) {
|
||||
mouseDown = false;
|
||||
} else if (event == MouseEvent.DRAG && mouseDown) {
|
||||
int delta = y - mouseDownY;
|
||||
scrollerY = scrollerDownY + delta;
|
||||
return true;
|
||||
}
|
||||
return mouseDown;
|
||||
}
|
||||
|
||||
protected void updateScrollViewMetrics() {
|
||||
final int view = relativeBounds.height;
|
||||
final int content = child.relativeBounds.height;
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package org.betterx.ui.layout.components;
|
||||
|
||||
|
||||
import org.betterx.ui.layout.components.input.MouseEvent;
|
||||
import org.betterx.ui.layout.components.input.RelativeContainerEventHandler;
|
||||
import org.betterx.ui.layout.components.render.ComponentRenderer;
|
||||
import org.betterx.ui.layout.values.Alignment;
|
||||
|
@ -11,10 +10,14 @@ import org.betterx.ui.layout.values.Rectangle;
|
|||
import com.mojang.blaze3d.vertex.PoseStack;
|
||||
import net.minecraft.client.gui.components.events.GuiEventListener;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class VerticalStack<R extends ComponentRenderer> extends Component<R> implements RelativeContainerEventHandler {
|
||||
protected final List<Component<?>> components = new LinkedList<>();
|
||||
|
||||
|
@ -113,17 +116,6 @@ public class VerticalStack<R extends ComponentRenderer> extends Component<R> imp
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean mouseEvent(MouseEvent event, int x, int y) {
|
||||
if (!onMouseEvent(event, x, y)) {
|
||||
for (Component<?> c : components) {
|
||||
if (c.mouseEvent(event, x - relativeBounds.left, y - relativeBounds.top))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static VerticalStack<?> centered(Component<?> c) {
|
||||
return new VerticalStack<>(DynamicSize.relative(1), DynamicSize.relative(1)).addFiller().add(c).addFiller();
|
||||
}
|
||||
|
|
|
@ -4,6 +4,10 @@ import org.betterx.ui.layout.values.Rectangle;
|
|||
|
||||
import com.mojang.blaze3d.vertex.PoseStack;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public interface ComponentRenderer {
|
||||
void renderInBounds(PoseStack stack, int x, int y, float a, Rectangle bounds, Rectangle clipRect);
|
||||
}
|
||||
|
|
|
@ -3,6 +3,10 @@ package org.betterx.ui.layout.components.render;
|
|||
|
||||
import org.betterx.ui.layout.values.Rectangle;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public interface ScrollerRenderer {
|
||||
default int scrollerHeight() {
|
||||
return 16;
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
package org.betterx.ui.layout.values;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public enum Alignment {
|
||||
MIN, MAX, CENTER
|
||||
}
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
package org.betterx.ui.layout.values;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class DynamicSize {
|
||||
private SizeType sizeType;
|
||||
private int calculatedSize;
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
package org.betterx.ui.layout.values;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class Rectangle {
|
||||
public static final Rectangle ZERO = new Rectangle(0, 0, 0, 0);
|
||||
public final int left;
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
package org.betterx.ui.layout.values;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public interface SizeType {
|
||||
FitContent FIT_CONTENT = new FitContent();
|
||||
FitContentOrFill FIT_CONTENT_OR_FILL = new FitContentOrFill();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue