]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/gui/IControlSet.java
Setup for 1.14.4-Fabric
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / gui / IControlSet.java
1 package com.irtimaled.bbor.client.gui;
2
3 import net.minecraft.client.gui.Element;
4
5 import java.util.List;
6
7 public interface IControlSet extends IFocusableControl, Element {
8     List<? extends IControl> controls();
9
10     IControl getFocused();
11
12     void setFocused(IControl control);
13
14     boolean isDragging();
15
16     void setDragging(boolean dragging);
17
18     default boolean mouseClicked(double mouseX, double mouseY, int button) {
19         for (IControl control : this.controls()) {
20             if (control.isVisible() && control.mouseClicked(mouseX, mouseY, button)) {
21                 IControl focused = getFocused();
22                 if (focused != null && focused != control) {
23                     focused.clearFocus();
24                 }
25                 this.setFocused(control);
26                 if (button == 0) this.setDragging(true);
27                 return true;
28             }
29         }
30         return false;
31     }
32
33     default boolean mouseReleased(double mouseX, double mouseY, int button) {
34         this.setDragging(false);
35         IControl focused = this.getFocused();
36         return focused != null && focused.mouseReleased(mouseX, mouseY, button);
37     }
38
39     default boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) {
40         IControl focused = this.getFocused();
41         return focused != null && this.isDragging() && button == 0 && focused.mouseDragged(mouseX, mouseY, button, deltaX, deltaY);
42     }
43
44     default boolean mouseScrolled(double mouseX, double mouseY, double scrollAmount) {
45         IControl focused = this.getFocused();
46         return focused != null && focused.mouseScrolled(mouseX, mouseY, scrollAmount);
47     }
48
49     default boolean keyPressed(int key, int scanCode, int modifiers) {
50         IControl focused = this.getFocused();
51         return focused != null && focused.keyPressed(key, scanCode, modifiers);
52     }
53
54     default boolean keyReleased(int key, int scanCode, int modifiers) {
55         IControl focused = this.getFocused();
56         return focused != null && focused.keyReleased(key, scanCode, modifiers);
57     }
58
59     default boolean charTyped(char character, int modifiers) {
60         IControl focused = this.getFocused();
61         return focused != null && focused.charTyped(character, modifiers);
62     }
63
64     default boolean changeFocus(boolean moveForward) {
65         IControl focused = this.getFocused();
66         if (focused != null && focused.changeFocus(moveForward)) {
67             return true;
68         }
69
70         List<? extends IControl> controls = this.controls();
71         int controlIndex = controls.indexOf(focused);
72         int newIndex;
73         if (focused != null && controlIndex >= 0) {
74             newIndex = controlIndex + (moveForward ? 1 : 0);
75         } else if (moveForward) {
76             newIndex = 0;
77         } else {
78             newIndex = controls.size();
79         }
80
81         if (ListHelper.findNextMatch(controls, newIndex, moveForward,
82                 c -> c.changeFocus(moveForward), this::setFocused)) return true;
83         this.setFocused(null);
84         return false;
85     }
86
87     default void clearFocus() {
88         IControl focused = getFocused();
89         if (focused != null) {
90             setFocused(null);
91             focused.clearFocus();
92         }
93     }
94 }