From 79849944e83d9ce7f44a87f4878ca3e016c34101 Mon Sep 17 00:00:00 2001 From: Juuz <6596629+Juuxel@users.noreply.github.com> Date: Sun, 19 Mar 2023 12:45:45 +0200 Subject: Rename FocusHandler -> FocusModel, improve docs Closes #186. --- .../cotton/gui/impl/client/FocusElements.java | 16 +++---- .../cottonmc/cotton/gui/widget/WItemSlot.java | 8 ++-- .../github/cottonmc/cotton/gui/widget/WWidget.java | 18 +++++--- .../cotton/gui/widget/focus/FocusHandler.java | 51 ---------------------- .../cotton/gui/widget/focus/FocusModel.java | 51 ++++++++++++++++++++++ .../gui/widget/focus/SimpleFocusHandler.java | 23 ---------- .../cotton/gui/widget/focus/SimpleFocusModel.java | 23 ++++++++++ 7 files changed, 97 insertions(+), 93 deletions(-) delete mode 100644 src/main/java/io/github/cottonmc/cotton/gui/widget/focus/FocusHandler.java create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/widget/focus/FocusModel.java delete mode 100644 src/main/java/io/github/cottonmc/cotton/gui/widget/focus/SimpleFocusHandler.java create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/widget/focus/SimpleFocusModel.java diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/FocusElements.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/FocusElements.java index fc2fa42..0f6a5e3 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/FocusElements.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/FocusElements.java @@ -10,7 +10,7 @@ import io.github.cottonmc.cotton.gui.widget.WPanel; import io.github.cottonmc.cotton.gui.widget.WWidget; import io.github.cottonmc.cotton.gui.widget.data.Rect2i; import io.github.cottonmc.cotton.gui.widget.focus.Focus; -import io.github.cottonmc.cotton.gui.widget.focus.FocusHandler; +import io.github.cottonmc.cotton.gui.widget.focus.FocusModel; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; @@ -33,10 +33,10 @@ public final class FocusElements { } private static Stream> fromFoci(WWidget widget) { - @Nullable FocusHandler focusHandler = widget.getFocusHandler(); - if (focusHandler == null) return Stream.empty(); + @Nullable FocusModel focusModel = widget.getFocusModel(); + if (focusModel == null) return Stream.empty(); - return focusHandler.foci().map(focus -> new LeafFocusElement(widget, focus)); + return focusModel.foci().map(focus -> new LeafFocusElement(widget, focus)); } public sealed interface FocusElement extends Element { @@ -52,7 +52,7 @@ public final class FocusElements { if (focus != null) { widget.requestFocus(); - ((FocusHandler) widget.getFocusHandler()).setFocused((Focus) focus); + ((FocusModel) widget.getFocusModel()).setFocused((Focus) focus); } } else { widget.releaseFocus(); @@ -63,9 +63,9 @@ public final class FocusElements { @Override public boolean isFocused() { if (widget.isFocused()) { - FocusHandler focusHandler = (FocusHandler) widget.getFocusHandler(); - if (focusHandler != null) { - return focusHandler.isFocused((Focus) focus); + FocusModel focusModel = (FocusModel) widget.getFocusModel(); + if (focusModel != null) { + return focusModel.isFocused((Focus) focus); } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java index ff7548c..972dbd0 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java @@ -23,7 +23,7 @@ import io.github.cottonmc.cotton.gui.impl.client.NarrationMessages; import io.github.cottonmc.cotton.gui.widget.data.InputResult; import io.github.cottonmc.cotton.gui.widget.data.Rect2i; import io.github.cottonmc.cotton.gui.widget.focus.Focus; -import io.github.cottonmc.cotton.gui.widget.focus.FocusHandler; +import io.github.cottonmc.cotton.gui.widget.focus.FocusModel; import io.github.cottonmc.cotton.gui.widget.icon.Icon; import org.jetbrains.annotations.Nullable; @@ -96,7 +96,7 @@ public class WItemSlot extends WWidget { private int hoveredSlot = -1; private Predicate filter = ValidatedSlot.DEFAULT_ITEM_FILTER; private final Set listeners = new HashSet<>(); - private final FocusHandler focusHandler = new FocusHandler<>() { + private final FocusModel focusModel = new FocusModel<>() { @Override public boolean isFocused(Focus focus) { return focusedSlot == focus.key(); @@ -439,8 +439,8 @@ public class WItemSlot extends WWidget { @Nullable @Override - public FocusHandler getFocusHandler() { - return focusHandler; + public FocusModel getFocusModel() { + return focusModel; } @Override diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java index 25c9767..a240cd6 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java @@ -11,7 +11,7 @@ import io.github.cottonmc.cotton.gui.GuiDescription; import io.github.cottonmc.cotton.gui.impl.VisualLogger; import io.github.cottonmc.cotton.gui.widget.data.InputResult; import io.github.cottonmc.cotton.gui.widget.data.ObservableProperty; -import io.github.cottonmc.cotton.gui.widget.focus.FocusHandler; +import io.github.cottonmc.cotton.gui.widget.focus.FocusModel; import org.jetbrains.annotations.Nullable; import org.lwjgl.glfw.GLFW; @@ -429,15 +429,19 @@ public class WWidget { public void tick() {} /** - * Returns the focus handler of this widget. The focus - * handler provides the focusable areas of this widget, - * and applies cycling through them. + * Returns the focus model of this widget. The focus + * model provides the focusable areas of this widget, + * and handles switching through them. * - * @return the focus handler, or {@code null} if not available + *

If this widget {@linkplain #canFocus() can focus}, it should return + * a nonnull focus model. The default implementation returns + * {@link FocusModel#simple FocusModel.simple(this)} when the widget can be focused. + * + * @return the focus model, or {@code null} if not available * @since 7.0.0 */ - public @Nullable FocusHandler getFocusHandler() { - return canFocus() ? FocusHandler.simple(this) : null; + public @Nullable FocusModel getFocusModel() { + return canFocus() ? FocusModel.simple(this) : null; } /** diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/focus/FocusHandler.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/focus/FocusHandler.java deleted file mode 100644 index f3b569e..0000000 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/focus/FocusHandler.java +++ /dev/null @@ -1,51 +0,0 @@ -package io.github.cottonmc.cotton.gui.widget.focus; - -import io.github.cottonmc.cotton.gui.widget.WWidget; -import io.github.cottonmc.cotton.gui.widget.data.Rect2i; - -import java.util.stream.Stream; - -/** - * Manages the state of individual {@linkplain Focus foci} in a widget. - * Each instance should be specific to one widget. - * - * @param the focus key type - * @since 7.0.0 - */ -public interface FocusHandler { - /** - * Checks if a focus is focused in the target widget. - * If the target widget is not focused itself, none of its foci should have focus. - * - * @param focus the focus to check - * @return {@code true} if the focus is focused, {@code false} otherwise - */ - boolean isFocused(Focus focus); - - /** - * Applies a focus to the target widget. - * - *

This method does not need to {@linkplain WWidget#requestFocus request the GUI's focus} - * for the widget; that is the responsibility of the caller. - * - * @param focus the focus - */ - void setFocused(Focus focus); - - /** - * {@return a stream of all foci in the target widget} - */ - Stream> foci(); - - /** - * Creates a simple focus handler for a focusable widget. - * The focus handler provides the whole widget area as its only focus area. - * - * @param widget the widget - * @return the focus handler - */ - static FocusHandler simple(WWidget widget) { - Rect2i widgetArea = new Rect2i(0, 0, widget.getWidth(), widget.getHeight()); - return new SimpleFocusHandler(widget, widgetArea); - } -} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/focus/FocusModel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/focus/FocusModel.java new file mode 100644 index 0000000..615f686 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/focus/FocusModel.java @@ -0,0 +1,51 @@ +package io.github.cottonmc.cotton.gui.widget.focus; + +import io.github.cottonmc.cotton.gui.widget.WWidget; +import io.github.cottonmc.cotton.gui.widget.data.Rect2i; + +import java.util.stream.Stream; + +/** + * Manages the state of individual {@linkplain Focus foci} in a widget. + * Each instance should be specific to one widget. + * + * @param the focus key type + * @since 7.0.0 + */ +public interface FocusModel { + /** + * Checks if a focus is focused in the target widget. + * If the target widget is not focused itself, none of its foci should have focus. + * + * @param focus the focus to check + * @return {@code true} if the focus is focused, {@code false} otherwise + */ + boolean isFocused(Focus focus); + + /** + * Applies a focus to the target widget. + * + *

This method does not need to {@linkplain WWidget#requestFocus request the GUI's focus} + * for the widget; that is the responsibility of the caller. + * + * @param focus the focus + */ + void setFocused(Focus focus); + + /** + * {@return a stream of all foci in the target widget} + */ + Stream> foci(); + + /** + * Creates a simple focus model for a focusable widget. + * The focus model provides the whole widget area as its only focus area. + * + * @param widget the widget + * @return the focus model + */ + static FocusModel simple(WWidget widget) { + Rect2i widgetArea = new Rect2i(0, 0, widget.getWidth(), widget.getHeight()); + return new SimpleFocusModel(widget, widgetArea); + } +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/focus/SimpleFocusHandler.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/focus/SimpleFocusHandler.java deleted file mode 100644 index 76b0cc1..0000000 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/focus/SimpleFocusHandler.java +++ /dev/null @@ -1,23 +0,0 @@ -package io.github.cottonmc.cotton.gui.widget.focus; - -import io.github.cottonmc.cotton.gui.widget.WWidget; -import io.github.cottonmc.cotton.gui.widget.data.Rect2i; -import org.jetbrains.annotations.Nullable; - -import java.util.stream.Stream; - -record SimpleFocusHandler(WWidget widget, Rect2i area) implements FocusHandler<@Nullable Void> { - @Override - public boolean isFocused(Focus<@Nullable Void> focus) { - return widget.isFocused(); - } - - @Override - public void setFocused(Focus<@Nullable Void> focusArea) { - } - - @Override - public Stream> foci() { - return Stream.of(Focus.of(area)); - } -} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/focus/SimpleFocusModel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/focus/SimpleFocusModel.java new file mode 100644 index 0000000..4144f0a --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/focus/SimpleFocusModel.java @@ -0,0 +1,23 @@ +package io.github.cottonmc.cotton.gui.widget.focus; + +import io.github.cottonmc.cotton.gui.widget.WWidget; +import io.github.cottonmc.cotton.gui.widget.data.Rect2i; +import org.jetbrains.annotations.Nullable; + +import java.util.stream.Stream; + +record SimpleFocusModel(WWidget widget, Rect2i area) implements FocusModel<@Nullable Void> { + @Override + public boolean isFocused(Focus<@Nullable Void> focus) { + return widget.isFocused(); + } + + @Override + public void setFocused(Focus<@Nullable Void> focusArea) { + } + + @Override + public Stream> foci() { + return Stream.of(Focus.of(area)); + } +} -- cgit