8
8
import codechicken .lib .gui .modular .lib .geometry .Rectangle ;
9
9
import net .minecraft .client .Minecraft ;
10
10
import net .minecraft .client .gui .Font ;
11
- import net .minecraft .resources .ResourceLocation ;
12
11
import org .jetbrains .annotations .NotNull ;
13
12
import org .jetbrains .annotations .Nullable ;
14
13
14
+ import java .util .function .Consumer ;
15
+
15
16
import static codechicken .lib .gui .modular .lib .geometry .Constraint .literal ;
16
17
import static codechicken .lib .gui .modular .lib .geometry .Constraint .match ;
17
18
import static codechicken .lib .gui .modular .lib .geometry .GeoParam .*;
@@ -44,6 +45,7 @@ public class GuiManipulable extends GuiElement<GuiManipulable> implements Conten
44
45
private boolean dragBottom = false ;
45
46
private boolean dragRight = false ;
46
47
private boolean enableCursors = false ;
48
+ private boolean resetOnUiInit = true ;
47
49
48
50
//Made available for external position restraints
49
51
public int xMin = 0 ;
@@ -53,8 +55,8 @@ public class GuiManipulable extends GuiElement<GuiManipulable> implements Conten
53
55
54
56
protected Rectangle minSize = Rectangle .create (0 , 0 , 50 , 50 );
55
57
protected Rectangle maxSize = Rectangle .create (0 , 0 , 256 , 256 );
56
- protected Runnable onMovedCallback = null ;
57
- protected Runnable onResizedCallback = null ;
58
+ protected Consumer < Boolean > onMovedCallback = null ;
59
+ protected Consumer < Boolean > onResizedCallback = null ;
58
60
protected PositionRestraint positionRestraint = draggable -> {
59
61
if (xMin < 0 ) {
60
62
int move = -xMin ;
@@ -112,7 +114,15 @@ public GuiManipulable constrain(GeoParam param, @Nullable Constraint constraint)
112
114
@ Override
113
115
public void onScreenInit (Minecraft mc , Font font , int screenWidth , int screenHeight ) {
114
116
super .onScreenInit (mc , font , screenWidth , screenHeight );
115
- resetBounds ();
117
+ if (resetOnUiInit ) resetBounds ();
118
+ }
119
+
120
+ /**
121
+ * @param resetOnUiInit If true, element bounds will be reset on UI init. (Default: true)
122
+ */
123
+ public GuiManipulable setResetOnUiInit (boolean resetOnUiInit ) {
124
+ this .resetOnUiInit = resetOnUiInit ;
125
+ return this ;
116
126
}
117
127
118
128
@ Override
@@ -137,6 +147,11 @@ public GuiManipulable addTopHandle(int handleSize) {
137
147
return this ;
138
148
}
139
149
150
+ public GuiManipulable setTopHandle (GuiElement <?> topHandle ) {
151
+ this .topHandle = topHandle ;
152
+ return this ;
153
+ }
154
+
140
155
public GuiManipulable addBottomHandle (int handleSize ) {
141
156
this .bottomHandle
142
157
.constrain (BOTTOM , match (contentElement .get (BOTTOM )))
@@ -146,6 +161,11 @@ public GuiManipulable addBottomHandle(int handleSize) {
146
161
return this ;
147
162
}
148
163
164
+ public GuiManipulable setBottomHandle (GuiElement <?> bottomHandle ) {
165
+ this .bottomHandle = bottomHandle ;
166
+ return this ;
167
+ }
168
+
149
169
public GuiManipulable addLeftHandle (int handleSize ) {
150
170
this .leftHandle
151
171
.constrain (LEFT , match (contentElement .get (LEFT )))
@@ -155,6 +175,11 @@ public GuiManipulable addLeftHandle(int handleSize) {
155
175
return this ;
156
176
}
157
177
178
+ public GuiManipulable setLeftHandle (GuiElement <?> leftHandle ) {
179
+ this .leftHandle = leftHandle ;
180
+ return this ;
181
+ }
182
+
158
183
public GuiManipulable addRightHandle (int handleSize ) {
159
184
this .rightHandle
160
185
.constrain (RIGHT , match (contentElement .get (RIGHT )))
@@ -164,6 +189,11 @@ public GuiManipulable addRightHandle(int handleSize) {
164
189
return this ;
165
190
}
166
191
192
+ public GuiManipulable setRightHandle (GuiElement <?> rightHandle ) {
193
+ this .rightHandle = rightHandle ;
194
+ return this ;
195
+ }
196
+
167
197
public GuiManipulable addMoveHandle (int handleSize ) {
168
198
this .moveHandle
169
199
.constrain (TOP , match (contentElement .get (TOP )))
@@ -173,6 +203,11 @@ public GuiManipulable addMoveHandle(int handleSize) {
173
203
return this ;
174
204
}
175
205
206
+ public GuiManipulable setMoveHandle (GuiElement <?> moveHandle ) {
207
+ this .moveHandle = moveHandle ;
208
+ return this ;
209
+ }
210
+
176
211
/**
177
212
* You can use this to retrieve the current move handle.
178
213
* You are free to update the constraints on this handle, but it must be constrained relative to the content element.
@@ -222,11 +257,21 @@ public GuiManipulable enableCursors(boolean enableCursors) {
222
257
}
223
258
224
259
public GuiManipulable setOnMovedCallback (Runnable onMovedCallback ) {
260
+ this .onMovedCallback = finished -> onMovedCallback .run ();
261
+ return this ;
262
+ }
263
+
264
+ public GuiManipulable setOnMovedCallback (Consumer <Boolean > onMovedCallback ) {
225
265
this .onMovedCallback = onMovedCallback ;
226
266
return this ;
227
267
}
228
268
229
269
public GuiManipulable setOnResizedCallback (Runnable onResizedCallback ) {
270
+ this .onResizedCallback = finished -> onResizedCallback .run ();
271
+ return this ;
272
+ }
273
+
274
+ public GuiManipulable setOnResizedCallback (Consumer <Boolean > onResizedCallback ) {
230
275
this .onResizedCallback = onResizedCallback ;
231
276
return this ;
232
277
}
@@ -324,13 +369,12 @@ public void mouseMoved(double mouseX, double mouseY) {
324
369
int xMove = (int ) (mouseX - dragXOffset ) - xMin ;
325
370
int yMove = (int ) (mouseY - dragYOffset ) - yMin ;
326
371
if (dragPos ) {
327
- Rectangle previous = Rectangle .create (xMin , yMin , xMax - xMin , yMax - yMin );
328
372
xMin += xMove ;
329
373
xMax += xMove ;
330
374
yMin += yMove ;
331
375
yMax += yMove ;
332
- validatePosition ();
333
- onMoved ();
376
+ validatePosition (false );
377
+ onMoved (false );
334
378
} else {
335
379
Rectangle min = getMinSize ();
336
380
Rectangle max = getMaxSize ();
@@ -358,43 +402,54 @@ public void mouseMoved(double mouseX, double mouseY) {
358
402
if (xMax - xMin > max .width ()) xMax = xMin + (int ) max .width ();
359
403
if (xMax > scaledScreenWidth ()) xMax = scaledScreenWidth ();
360
404
}
361
- validatePosition ();
362
- onResized ();
405
+ validatePosition (false );
406
+ onResized (false );
363
407
}
364
408
}
365
409
super .mouseMoved (mouseX , mouseY );
366
410
}
367
411
368
412
@ Override
369
413
public boolean mouseReleased (double mouseX , double mouseY , int button , boolean consumed ) {
370
- if (isDragging ) {
371
- validatePosition ();
414
+ if (isMoving ()) {
415
+ validatePosition (true );
416
+ }
417
+ if (isResizing ()) {
418
+ onResized (true );
372
419
}
373
420
isDragging = dragPos = dragTop = dragLeft = dragBottom = dragRight = false ;
374
421
return super .mouseReleased (mouseX , mouseY , button , consumed );
375
422
}
376
423
377
- protected void validatePosition () {
424
+ protected void validatePosition (boolean finished ) {
378
425
double x = xMin ;
379
426
double y = yMin ;
380
427
positionRestraint .restrainPosition (this );
381
- if ((x != xMin || y != yMin ) && onMovedCallback != null ) {
382
- onMovedCallback . run ( );
428
+ if ((x != xMin || y != yMin )) {
429
+ onMoved ( finished );
383
430
}
384
431
}
385
432
386
- protected void onMoved () {
433
+ protected void onMoved (boolean finished ) {
387
434
if (onMovedCallback != null ) {
388
- onMovedCallback .run ( );
435
+ onMovedCallback .accept ( finished );
389
436
}
390
437
}
391
438
392
- protected void onResized () {
439
+ protected void onResized (boolean finished ) {
393
440
if (onResizedCallback != null ) {
394
- onResizedCallback . run ( );
441
+ onMovedCallback . accept ( finished );
395
442
}
396
443
}
397
444
445
+ public boolean isMoving () {
446
+ return dragPos ;
447
+ }
448
+
449
+ public boolean isResizing () {
450
+ return dragTop || dragLeft || dragBottom || dragRight ;
451
+ }
452
+
398
453
public interface PositionRestraint {
399
454
void restrainPosition (GuiManipulable draggable );
400
455
}
0 commit comments