-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAbstractMoveableScreen.java
More file actions
244 lines (219 loc) · 9.23 KB
/
AbstractMoveableScreen.java
File metadata and controls
244 lines (219 loc) · 9.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package com.tanishisherewith.dynamichud.huds;
import com.tanishisherewith.dynamichud.handlers.DefaultDragHandler;
import com.tanishisherewith.dynamichud.handlers.DefaultMouseHandler;
import com.tanishisherewith.dynamichud.handlers.DragHandler;
import com.tanishisherewith.dynamichud.handlers.MouseHandler;
import com.tanishisherewith.dynamichud.util.DynamicUtil;
import com.tanishisherewith.dynamichud.util.colorpicker.ColorGradientPicker;
import com.tanishisherewith.dynamichud.util.contextmenu.ContextMenu;
import com.tanishisherewith.dynamichud.widget.Widget;
import com.tanishisherewith.dynamichud.widget.slider.SliderWidget;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;
import java.util.ArrayList;
import java.util.List;
public abstract class AbstractMoveableScreen extends Screen {
protected final DynamicUtil dynamicutil; // The DynamicUtil instance used by this screen
protected MinecraftClient mc = MinecraftClient.getInstance();
protected Widget selectedWidget = null; // The currently selected widget
protected int dragStartX = 0, dragStartY = 0; // The starting position of a drag operation
protected List<ContextMenu> contextMenu = new ArrayList<>(); // The context menu that is currently displayed
protected ColorGradientPicker colorPicker = null; // The color picker that is currently displayed
protected Widget sliderWigdet = null; // The widget that is currently being edited by the slider
protected List<SliderWidget> Slider = new ArrayList<>(); // The List of sliders
protected MouseHandler mouseHandler;
protected DragHandler dragHandler;
protected int gridSize = 3; // The size of each grid cell in pixels
protected boolean ShouldPause = false; // To pause if the screen is opened or not
protected boolean ShouldBeAffectedByResize = false; // If the stuff drawn on screen to be affected by screen resize or not
protected int widgetX;
protected int widgetY;
/**
* Constructs a AbstractMoveableScreen object.
*
* @param dynamicutil The DynamicUtil instance used by this screen
*/
public AbstractMoveableScreen(Text title, DynamicUtil dynamicutil) {
super(title);
this.dynamicutil = dynamicutil;
updateMouseHandler(this.colorPicker, contextMenu, Slider);
dragHandler = new DefaultDragHandler();
}
/**
* Handles mouse dragging on this screen.
*
* @param mouseX - Current X position of mouse cursor.
* @param mouseY - Current Y position of mouse cursor.
* @param button - Mouse button being dragged.
* @param deltaX - Change in X position since last call to this method.
* @param deltaY - Change in Y position since last call to this method.
* @return true if mouse dragging was handled by this screen.
*/
@Override
public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) {
if (mouseHandler.mouseDragged(mouseX, mouseY, button, deltaX, deltaY)) {
return true;
}
if (selectedWidget != null && selectedWidget.isDraggable) {
// Update the position of the widget while dragging
int newX = (int) (mouseX - dragStartX);
int newY = (int) (mouseY - dragStartY);
// Snap the widget to the grid
newX = (newX / gridSize) * gridSize;
newY = (newY / gridSize) * gridSize;
selectedWidget.setX(newX);
selectedWidget.setY(newY);
return true;
}
return false;
}
/**
* Handles mouse clicks on this screen.
*
* @param mouseX - X position of mouse cursor.
* @param mouseY - Y position of mouse cursor.
* @param button - Mouse button that was clicked.
* @return true if mouse click was handled by this screen, false otherwise.
*/
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (mouseHandler.mouseClicked(mouseX, mouseY, button)) {
return true;
}
// Check if the context menu is visible and if the mouse click occurred outside its bounds
// Check if the Slider is visible and if the mouse click occurred outside its bounds
if (contextMenu != null) {
for (ContextMenu contextmenu : contextMenu) {
if (!contextmenu.contains(mouseX, mouseY)) {
// Close the context menu
contextMenu.clear();
Slider.clear();
return true;
}
}
}
if (Slider != null) {
for (SliderWidget sliderWidget : Slider) {
if (!sliderWidget.contains(mouseX, mouseY)) {
// Close the Slider
Slider.clear();
contextMenu.clear();
return true;
}
}
}
for (Widget widget : dynamicutil.getWidgetManager().getWidgets()) {
if (widget.getWidgetBox().contains(widget, mouseX, mouseY)) {
// Start dragging the widget
colorPicker = null;
contextMenu.clear();
Slider.clear();
if (button == 1) { // Right-click
handleRightClickOnWidget(widget);
} else if (button == 0) {
widget.enabled = !widget.enabled;
}
if (dragHandler.startDragging(widget, mouseX, mouseY) && button == 0 && widget.isDraggable) {
selectedWidget = widget;
if (contextMenu != null) {
for (ContextMenu contextmenu : contextMenu) {
contextmenu.updatePosition();
}
}
if (Slider != null) {
for (SliderWidget sliderWidget : Slider) {
sliderWidget.updatePosition();
}
}
return true;
}
}
}
return false;
}
/**
* Handles mouse release events on this screen.
*
* @param mouseX The current x position of the mouse cursor
* @param mouseY The current y position of the mouse cursor
* @param button The mouse button that was released
* @return True if the mouse release event was handled by this screen, false otherwise
*/
@Override
public boolean mouseReleased(double mouseX, double mouseY, int button) {
// Stop dragging or scaling the widget
if (mouseHandler.mouseReleased(mouseX, mouseY, button)) {
return true;
}
if (selectedWidget != null) {
selectedWidget = null;
return true;
}
return contextMenu != null;
}
/**
* Renders this screen and its widgets on the screen.
*
* @param drawContext The matrix stack used for rendering
* @param mouseX The current x position of the mouse cursor
* @param mouseY The current y position of the mouse cursor
* @param delta The time elapsed since the last frame in seconds
*/
@Override
public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) {
super.render(drawContext, mouseX, mouseY, delta);
// Draw each widget
for (Widget widget : dynamicutil.getWidgetManager().getWidgets()) {
widget.render(drawContext);
}
// Draw the slider and other stuff
if (Slider != null) {
for (SliderWidget sliderWidget : Slider) {
sliderWidget.render(drawContext);
}
}
if (contextMenu != null) {
for (ContextMenu contextMenu : contextMenu) {
contextMenu.render(drawContext);
}
}
if (colorPicker != null) {
colorPicker.render(drawContext);
}
if (selectedWidget != null) {
widgetX = selectedWidget.getX();
widgetY = selectedWidget.getY();
}
updateMouseHandler(colorPicker, contextMenu, Slider);
}
private void updateMouseHandler(ColorGradientPicker colorPicker, List<ContextMenu> contextMenu, List<SliderWidget> Slider) {
this.colorPicker = colorPicker;
this.contextMenu = contextMenu;
this.Slider = Slider;
mouseHandler = new DefaultMouseHandler(colorPicker, contextMenu, Slider);
}
public void setGridSize(int gridSize) {
this.gridSize = gridSize;
}
public void setShouldPause(boolean shouldpause) {
this.ShouldPause = shouldpause;
}
public void setShouldBeAffectedByResize(boolean shouldBeAffectedByResize) {
this.ShouldBeAffectedByResize = shouldBeAffectedByResize;
}
@Override
public void resize(MinecraftClient client, int width, int height) {
if (ShouldBeAffectedByResize)
super.resize(client, width, height);
else {
}
}
@Override
public boolean shouldPause() {
return ShouldPause;
}
protected abstract boolean handleRightClickOnWidget(Widget widget);
protected abstract void menu(Widget widget, int x, int y);
}