@@ -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 /**
0 commit comments