Skip to content

Commit c6dcfae

Browse files
committed
only close panel after click is fully processed
1 parent c3c35cb commit c6dcfae

3 files changed

Lines changed: 58 additions & 21 deletions

File tree

src/main/java/com/cleanroommc/modularui/screen/ModularPanel.java

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public static ModularPanel defaultPanel(@NotNull String name, int width, int hei
7272
private final String name;
7373
private ModularScreen screen;
7474
private IPanelHandler panelHandler;
75-
private State state = State.IDLE;
76-
private boolean cantDisposeNow = false;
75+
private State state = State.CLOSED;
76+
private boolean cantCloseNow = false;
7777
private final ObjectList<LocatedWidget> hovering = ObjectList.create();
7878
private final Input keyboard = new Input();
7979
private final Input mouse = new Input();
@@ -135,7 +135,7 @@ public void setPanelSyncHandler(PanelSyncHandler syncHandler) {
135135
* @return true if this panel is currently open on a screen
136136
*/
137137
public boolean isOpen() {
138-
return this.state == State.OPEN;
138+
return this.state.open;
139139
}
140140

141141
/**
@@ -144,6 +144,14 @@ public boolean isOpen() {
144144
*/
145145
public void closeIfOpen() {
146146
if (!isOpen()) return;
147+
if (this.cantCloseNow) {
148+
if (!this.state.disposing) {
149+
this.state = State.WAIT_CLOSING;
150+
} else {
151+
this.state = State.WAIT_CLOSING_AND_DISPOSING;
152+
}
153+
return;
154+
}
147155
closeSubPanels();
148156
if (isMainPanel()) {
149157
// close screen and let NEA handle animation
@@ -267,12 +275,16 @@ public void onClose() {
267275
@MustBeInvokedByOverriders
268276
@Override
269277
public void dispose() {
270-
if (this.state == State.DISPOSED) return;
271-
if (this.state != State.CLOSED && this.state != State.WAIT_DISPOSING) {
278+
if (this.state.disposing) return;
279+
if (this.state.open && !this.state.closing) {
272280
throw new IllegalStateException("Panel must be closed before disposing!");
273281
}
274-
if (this.cantDisposeNow) {
275-
this.state = State.WAIT_DISPOSING;
282+
if (this.cantCloseNow) {
283+
if (this.state.closing) {
284+
this.state = State.WAIT_CLOSING_AND_DISPOSING;
285+
} else {
286+
this.state = State.WAIT_DISPOSING;
287+
}
276288
return;
277289
}
278290
super.dispose();
@@ -293,12 +305,18 @@ public final <T> T doSafe(Supplier<T> runnable) {
293305
if (this.state == State.DISPOSED) return null;
294306
// make sure the screen is also not disposed
295307
return getScreen().getPanelManager().doSafe(() -> {
296-
this.cantDisposeNow = true;
308+
this.cantCloseNow = true;
297309
T t = runnable.get();
298-
this.cantDisposeNow = false;
299-
if (this.state == State.WAIT_DISPOSING) {
300-
this.state = State.CLOSED;
301-
dispose();
310+
this.cantCloseNow = false;
311+
if (this.state.open) {
312+
if (this.state.closing) {
313+
this.state = State.OPEN;
314+
closeIfOpen();
315+
}
316+
if (this.state.disposing) {
317+
this.state = State.CLOSED;
318+
dispose();
319+
}
302320
}
303321
return t;
304322
});
@@ -846,26 +864,39 @@ public State getState() {
846864
}
847865

848866
public enum State {
849-
/**
850-
* Initial state of any panel.
851-
*/
852-
IDLE,
867+
853868
/**
854869
* State after the panel opened.
855870
*/
856-
OPEN,
871+
OPEN(true, false, false),
857872
/**
858873
* State after panel closed. Panel can still be reopened in this state.
859874
*/
860-
CLOSED,
875+
CLOSED(false, true, false),
861876
/**
862877
* State after panel disposed. The panel is now lost and has to be rebuilt, when reopening it.
863878
*/
864-
DISPOSED,
879+
DISPOSED(false, true, true),
880+
/**
881+
* Panel is open and is waiting to be closed.
882+
*/
883+
WAIT_CLOSING(true, true, false),
865884
/**
866885
* Panel is closed and is waiting to be disposed.
867886
*/
868-
WAIT_DISPOSING
887+
WAIT_DISPOSING(true, false, true),
888+
/**
889+
* Panel is open abd is waiting to close and then to be disposed.
890+
*/
891+
WAIT_CLOSING_AND_DISPOSING(true, true, true);
892+
893+
public final boolean open, closing, disposing;
894+
895+
State(boolean open, boolean closing, boolean disposing) {
896+
this.open = open;
897+
this.closing = closing;
898+
this.disposing = disposing;
899+
}
869900
}
870901

871902
/**

src/main/java/com/cleanroommc/modularui/screen/PanelManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public void openPanel(@NotNull ModularPanel panel, @NotNull IPanelHandler panelH
196196
openPanel(panel, true);
197197
}
198198

199-
public void closePanel(@NotNull ModularPanel panel) {
199+
void closePanel(@NotNull ModularPanel panel) {
200200
if (!hasOpenPanel(panel)) {
201201
throw new IllegalArgumentException("Panel '" + panel.getName() + "' is open in this screen!");
202202
}

src/main/java/com/cleanroommc/modularui/test/TestTile.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.cleanroommc.modularui.value.sync.IntSyncValue;
2525
import com.cleanroommc.modularui.value.sync.ItemSlotSH;
2626
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
27+
import com.cleanroommc.modularui.value.sync.StringSyncValue;
2728
import com.cleanroommc.modularui.widget.EmptyWidget;
2829
import com.cleanroommc.modularui.widget.ParentWidget;
2930
import com.cleanroommc.modularui.widgets.ButtonWidget;
@@ -44,6 +45,7 @@
4445
import com.cleanroommc.modularui.widgets.slot.ModularSlot;
4546
import com.cleanroommc.modularui.widgets.slot.PhantomItemSlot;
4647
import com.cleanroommc.modularui.widgets.slot.SlotGroup;
48+
import com.cleanroommc.modularui.widgets.textfield.TextFieldWidget;
4749

4850
import net.minecraft.init.Blocks;
4951
import net.minecraft.init.Items;
@@ -85,6 +87,7 @@ public class TestTile extends TileEntity implements IGuiHolder<PosGuiData>, ITic
8587
private final int duration = 80;
8688
private int progress = 0;
8789
private int cycleState = 0;
90+
private String s = "";
8891
private List<Integer> serverInts = new ArrayList<>();
8992
private ItemStack displayItem = new ItemStack(Items.DIAMOND);
9093
private final ItemStackHandler storage = new ItemStackHandler(9);
@@ -341,6 +344,9 @@ public ModularPanel openSecondWindow(PanelSyncManager syncManager, IPanelHandler
341344
IPanelHandler panelSyncHandler = syncManager.syncedPanel("other_panel_2", true, (syncManager1, syncHandler1) ->
342345
openThirdWindow(syncManager1, syncHandler1, number));
343346
IntSyncValue num = syncManager.getHyperVisor().findSyncHandler("cycle_state", IntSyncValue.class);
347+
panel.child(new TextFieldWidget()
348+
.size(60, 14).pos(10, 80)
349+
.value(new StringSyncValue(() -> s, v -> s = v)));
344350
panel.child(ButtonWidget.panelCloseButton())
345351
.child(new ButtonWidget<>()
346352
.size(10).top(14).right(4)

0 commit comments

Comments
 (0)