| 1 | package nicotine.screens.clickgui;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.InputConstants;
|
| 4 | import net.minecraft.client.Minecraft;
|
| 5 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 6 | import net.minecraft.client.gui.render.TextureSetup;
|
| 7 | import net.minecraft.client.gui.screens.Screen;
|
| 8 | import net.minecraft.client.input.KeyEvent;
|
| 9 | import net.minecraft.client.input.MouseButtonEvent;
|
| 10 | import net.minecraft.client.renderer.RenderPipelines;
|
| 11 | import net.minecraft.client.renderer.blockentity.AbstractEndPortalRenderer;
|
| 12 | import net.minecraft.client.renderer.texture.AbstractTexture;
|
| 13 | import net.minecraft.client.renderer.texture.TextureManager;
|
| 14 | import net.minecraft.network.chat.Component;
|
| 15 | import nicotine.mod.mods.general.GUI;
|
| 16 | import nicotine.screens.clickgui.element.*;
|
| 17 | import nicotine.screens.clickgui.element.button.*;
|
| 18 | import nicotine.screens.clickgui.element.button.InputText;
|
| 19 | import nicotine.screens.clickgui.element.window.Window;
|
| 20 | import nicotine.util.Keybind;
|
| 21 | import nicotine.util.Settings;
|
| 22 | import org.jetbrains.annotations.NotNull;
|
| 23 | import org.joml.Vector2i;
|
| 24 |
|
| 25 | import java.util.ArrayList;
|
| 26 | import java.util.List;
|
| 27 |
|
| 28 | import static nicotine.util.Common.mc;
|
| 29 |
|
| 30 | public class BaseScreen extends Screen {
|
| 31 | public final Window window;
|
| 32 |
|
| 33 | protected final Vector2i dragOffset = new Vector2i(-1, -1);
|
| 34 | protected ClickableElement elementForDragging = null;
|
| 35 | private boolean draggingSlider = false;
|
| 36 |
|
| 37 | public double mouseX = 0;
|
| 38 | public double mouseY = 0;
|
| 39 |
|
| 40 | private final List<Window> subWindows = new ArrayList<>();
|
| 41 |
|
| 42 | public BaseScreen(String title, Window window) {
|
| 43 | super(Component.literal(title));
|
| 44 | this.window = window;
|
| 45 | }
|
| 46 |
|
| 47 | public void addSubWindow(Window subWindow) {
|
| 48 | this.subWindows.add(subWindow);
|
| 49 | }
|
| 50 |
|
| 51 | public void removeSubWindow(Window subWindow) {
|
| 52 | this.subWindows.remove(subWindow);
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | public boolean mouseReleased(MouseButtonEvent mouseButtonEvent) {
|
| 57 | draggingSlider = false;
|
| 58 |
|
| 59 | if (dragOffset.x != -1 && dragOffset.y != -1) {
|
| 60 | dragOffset.x = -1;
|
| 61 | dragOffset.y = -1;
|
| 62 |
|
| 63 | elementForDragging = null;
|
| 64 | }
|
| 65 |
|
| 66 | return true;
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public void mouseMoved(final double x, final double y) {
|
| 71 | this.mouseX = x;
|
| 72 | this.mouseY = y;
|
| 73 | }
|
| 74 |
|
| 75 | public static void dragElement(double mouseX, double mouseY, Vector2i dragOffset, Element element) {
|
| 76 | final int width = mc.getWindow().getGuiScaledWidth();
|
| 77 | final int height = mc.getWindow().getGuiScaledHeight();
|
| 78 |
|
| 79 | Vector2i pos = new Vector2i((int) mouseX + dragOffset.x, (int) mouseY + dragOffset.y);
|
| 80 |
|
| 81 | if (pos.x < 0) {
|
| 82 | pos.x = 0;
|
| 83 | } else if (pos.x + element.width >= width) {
|
| 84 | pos.x = width - element.width - 1;
|
| 85 | }
|
| 86 |
|
| 87 | if (pos.y < 0) {
|
| 88 | pos.y = 0;
|
| 89 | } else if (pos.y + element.height >= height) {
|
| 90 | pos.y = height - element.height - 1;
|
| 91 | }
|
| 92 |
|
| 93 | element.x = pos.x;
|
| 94 | element.y = pos.y;
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public boolean mouseScrolled(final double x, final double y, final double scrollX, final double scrollY) {
|
| 99 | for (Element element : this.window.getElements(true)) {
|
| 100 | if (element instanceof Window window && window.mouseOverElement(mouseX, mouseY)) {
|
| 101 | if (scrollY > 0) {
|
| 102 | if (window.scrollOffset > 0)
|
| 103 | window.scrollOffset--;
|
| 104 | } else {
|
| 105 | window.scrollOffset++;
|
| 106 | }
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | return true;
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public boolean mouseDragged(MouseButtonEvent mouseButtonEvent, double offsetX, double offsetY) {
|
| 115 | double mouseX = mouseButtonEvent.x();
|
| 116 | double mouseY = mouseButtonEvent.y();
|
| 117 |
|
| 118 | if (dragOffset.x != -1 && dragOffset.y != -1) {
|
| 119 | dragElement(mouseX, mouseY, dragOffset, elementForDragging);
|
| 120 | return true;
|
| 121 | } else {
|
| 122 | for (Element element : this.window.getElements()) {
|
| 123 | if (element instanceof SliderButton sliderButton && sliderButton.mouseOverElement(mouseX, mouseY)) {
|
| 124 | sliderButton.click(mouseX, mouseY, InputConstants.MOUSE_BUTTON_LEFT);
|
| 125 | draggingSlider = true;
|
| 126 | return true;
|
| 127 | }
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | if (draggingSlider)
|
| 132 | return true;
|
| 133 |
|
| 134 | for (Element element : this.window.getElements(true)) {
|
| 135 | if (!(element instanceof Draggable draggable))
|
| 136 | continue;
|
| 137 |
|
| 138 | if (draggable.isDraggableArea(mouseX, mouseY) && mouseButtonEvent.input() == InputConstants.MOUSE_BUTTON_LEFT) {
|
| 139 | elementForDragging = (ClickableElement) element;
|
| 140 |
|
| 141 | dragOffset.x = element.x - (int) mouseX;
|
| 142 | dragOffset.y = element.y - (int) mouseY;
|
| 143 | return true;
|
| 144 | }
|
| 145 | }
|
| 146 |
|
| 147 | return true;
|
| 148 | }
|
| 149 |
|
| 150 | public void closeScreen() {
|
| 151 | this.onClose();
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | public boolean keyPressed(KeyEvent keyEvent) {
|
| 156 | if (keyEvent.key() == InputConstants.KEY_ESCAPE) {
|
| 157 | if (InputText.selectedTextBox != null) {
|
| 158 | InputText.selectedTextBox = null;
|
| 159 | return true;
|
| 160 | }
|
| 161 |
|
| 162 | Settings.save();
|
| 163 | DropDownButton.selectedDropDownOption = null;
|
| 164 | closeScreen();
|
| 165 | }
|
| 166 |
|
| 167 | for (Window subWindow : this.subWindows) {
|
| 168 | if (subWindow.handleKeyPress(keyEvent)) {
|
| 169 | return true;
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | if (KeybindButton.selectedKeybindOption != null) {
|
| 174 | KeybindButton.selectedKeybindOption.keyCode = keyEvent.key();
|
| 175 | KeybindButton.selectedKeybindOption = null;
|
| 176 | }
|
| 177 |
|
| 178 | if (InputText.selectedTextBox != null) {
|
| 179 | if (keyEvent.key() == InputConstants.KEY_SPACE) {
|
| 180 | InputText.selectedTextBox.text += " ";
|
| 181 | } else if (keyEvent.key() == InputConstants.KEY_BACKSPACE && ! InputText.selectedTextBox.text.isEmpty()) {
|
| 182 | InputText.selectedTextBox.text = InputText.selectedTextBox.text.substring(0, InputText.selectedTextBox.text.length() - 1);
|
| 183 | } else {
|
| 184 | String input = InputConstants.getKey(new KeyEvent(keyEvent.key(), 0, 0)).getDisplayName().getString();
|
| 185 |
|
| 186 | if (input.length() < 2 &&
|
| 187 | (InputText.selectedTextBox.regexString.isEmpty() ||
|
| 188 | String.format("%s%s", InputText.selectedTextBox.text, input).matches(InputText.selectedTextBox.regexString))
|
| 189 | ) {
|
| 190 | if (keyEvent.modifiers() != 1)
|
| 191 | input = input.toLowerCase();
|
| 192 |
|
| 193 | InputText.selectedTextBox.text += input;
|
| 194 | }
|
| 195 | }
|
| 196 | }
|
| 197 |
|
| 198 | return true;
|
| 199 | }
|
| 200 |
|
| 201 | @Override
|
| 202 | public void init() {
|
| 203 | this.window.centerPosition();
|
| 204 | }
|
| 205 |
|
| 206 | @Override
|
| 207 | public boolean mouseClicked(MouseButtonEvent mouseButtonEvent, boolean doubled) {
|
| 208 |
|
| 209 | InputText.selectedTextBox = null;
|
| 210 |
|
| 211 | if (KeybindButton.selectedKeybindOption != null) {
|
| 212 | KeybindButton.selectedKeybindOption.keyCode = mouseButtonEvent.input() + 1000;
|
| 213 | KeybindButton.selectedKeybindOption = null;
|
| 214 |
|
| 215 | return true;
|
| 216 | }
|
| 217 |
|
| 218 | double mouseX = mouseButtonEvent.x();
|
| 219 | double mouseY = mouseButtonEvent.y();
|
| 220 |
|
| 221 | for (Element element : this.window.getElements().reversed()) {
|
| 222 | if (element instanceof ClickableElement clickableElement && clickableElement.mouseOverElement(mouseX, mouseY)) {
|
| 223 | clickableElement.click(mouseX, mouseY, mouseButtonEvent.input());
|
| 224 |
|
| 225 | if (!(clickableElement instanceof DropDownButton))
|
| 226 | DropDownButton.selectedDropDownOption = null;
|
| 227 |
|
| 228 | return true;
|
| 229 | }
|
| 230 | }
|
| 231 |
|
| 232 | return true;
|
| 233 | }
|
| 234 |
|
| 235 | @Override
|
| 236 | public boolean isPauseScreen() {
|
| 237 | return false;
|
| 238 | }
|
| 239 |
|
| 240 | @Override
|
| 241 | public boolean isAllowedInPortal() {
|
| 242 | return true;
|
| 243 | }
|
| 244 |
|
| 245 | @Override
|
| 246 | public void extractBackground(@NotNull GuiGraphicsExtractor context, int mouseX, int mouseY, float delta) {
|
| 247 | if (GUI.blur.enabled) {
|
| 248 | this.extractBlurredBackground(context);
|
| 249 | this.extractMenuBackground(context);
|
| 250 | }
|
| 251 |
|
| 252 | if (mc.level == null) {
|
| 253 | TextureManager textureManager = Minecraft.getInstance().getTextureManager();
|
| 254 | AbstractTexture endSkyTexture = textureManager.getTexture(AbstractEndPortalRenderer.END_SKY_LOCATION);
|
| 255 | AbstractTexture endPortalTexture = textureManager.getTexture(AbstractEndPortalRenderer.END_PORTAL_LOCATION);
|
| 256 | TextureSetup textureSetup = TextureSetup.doubleTexture(endSkyTexture.getTextureView(), endSkyTexture.getSampler(), endPortalTexture.getTextureView(), endPortalTexture.getSampler());
|
| 257 | context.fill(RenderPipelines.END_PORTAL, textureSetup, 0, 0, this.width, this.height);
|
| 258 | }
|
| 259 | }
|
| 260 |
|
| 261 | @Override
|
| 262 | public void extractRenderState(@NotNull GuiGraphicsExtractor context, int mouseX, int mouseY, float delta) {
|
| 263 | window.clear();
|
| 264 |
|
| 265 | addDrawables();
|
| 266 | if (DropDownButton.selectedDropDownOption != null) {
|
| 267 | window.add(DropDownButton.getDropDownButtons());
|
| 268 | }
|
| 269 |
|
| 270 | this.window.applyZIndex();
|
| 271 |
|
| 272 | window.draw(context, mouseX, mouseY);
|
| 273 | }
|
| 274 |
|
| 275 | protected void addDrawables() {
|
| 276 | this.window.addDrawables();
|
| 277 |
|
| 278 | for (Window subWindow : this.subWindows) {
|
| 279 | this.window.add(subWindow);
|
| 280 | subWindow.addDrawables();
|
| 281 | this.window.add(subWindow.getElements());
|
| 282 | subWindow.clear();
|
| 283 | }
|
| 284 | }
|
| 285 | }
|
| 286 |
|