5050
5151import com .simsilica .lemur .Action ;
5252import com .simsilica .lemur .Button ;
53+ import com .simsilica .lemur .OptionPanel ;
5354import com .simsilica .lemur .OptionPanelState ;
5455
5556import example .net .client .GameClient ;
@@ -71,15 +72,21 @@ public class ConnectState extends BaseAppState {
7172 private Connector connector ;
7273 private Thread renderThread ;
7374
74- private boolean closing ;
75+ private OptionPanel connectingPanel ;
76+
77+ private volatile boolean closing ;
7578
7679 public ConnectState ( String host , int port ) {
77- this .host = host ;
80+ this .host = host ;
7881 this .port = port ;
7982 }
8083
8184 @ Override
8285 protected void initialize ( Application app ) {
86+
87+ connectingPanel = new OptionPanel ("Connecting..." , new ExitAction ("Cancel" , true ));
88+ getState (OptionPanelState .class ).show (connectingPanel );
89+
8390 this .renderThread = Thread .currentThread ();
8491 connector = new Connector ();
8592 connector .start ();
@@ -91,10 +98,19 @@ protected void cleanup( Application app ) {
9198 if ( client != null ) {
9299 client .close ();
93100 }
94-
101+
102+ // Close the connecting panel if it's still open
103+ closeConnectingPanel ();
104+
95105 // And re-enable the main menu
96106 getState (MainMenuState .class ).setEnabled (true );
97107 }
108+
109+ protected void closeConnectingPanel () {
110+ if ( getState (OptionPanelState .class ).getCurrent () == connectingPanel ) {
111+ getState (OptionPanelState .class ).close ();
112+ }
113+ }
98114
99115 @ Override
100116 protected void onEnable () {
@@ -135,6 +151,7 @@ public Object call() {
135151 }
136152
137153 protected void setClient ( final GameClient client ) {
154+ log .info ("Connection established:" + client );
138155 if ( isRenderThread () ) {
139156 this .client = client ;
140157 } else {
@@ -149,32 +166,38 @@ public Object call() {
149166
150167 protected void connected () {
151168 log .info ("connected()" );
169+ closeConnectingPanel ();
152170 }
153171
154172 protected void disconnected ( DisconnectInfo info ) {
155173 log .info ("disconnected(" + info + ")" );
174+ closeConnectingPanel ();
156175 if ( closing ) {
157176 return ;
158177 }
159178 if ( info != null ) {
160179 showError ("Disconnect" , info .reason , info .error , true );
161180 } else {
162181 showError ("Disconnected" , "Unknown error" , null , true );
163- }
164-
182+ }
165183 }
166184
167185 private class ExitAction extends Action {
168186 private boolean close ;
169187
170188 public ExitAction ( boolean close ) {
171- super ("Ok" );
189+ this ("Ok" , close );
190+ }
191+
192+ public ExitAction ( String name , boolean close ) {
193+ super (name );
172194 this .close = close ;
173195 }
174196
175197 public void execute ( Button source ) {
176198 if ( close ) {
177199 log .info ("Detaching ConnectionState" );
200+ closing = true ;
178201 getStateManager ().detach (ConnectState .this );
179202 }
180203 }
@@ -217,14 +240,23 @@ public void run() {
217240 try {
218241 log .info ("Creating game client for:" + host + " " + port );
219242 GameClient client = new GameClient (host , port );
243+ if ( closing ) {
244+ return ;
245+ }
220246 setClient (client );
221247 client .getClient ().addClientStateListener (connectionObserver );
222248 client .getClient ().addErrorListener (connectionObserver );
249+ if ( closing ) {
250+ return ;
251+ }
223252
224253 log .info ("Starting client..." );
225254 client .start ();
226255 log .info ("Client started." );
227256 } catch ( IOException e ) {
257+ if ( closing ) {
258+ return ;
259+ }
228260 showError ("Error Connecting" , e , true );
229261 }
230262 }
0 commit comments