Skip to content

Commit b806847

Browse files
committed
Added a state for managing the user input for logging in...
right now just a player name with no authentication or anything.
1 parent 71214a8 commit b806847

File tree

4 files changed

+159
-15
lines changed

4 files changed

+159
-15
lines changed

sim-eth-basic/src/main/java/example/ConnectState.java

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.slf4j.*;
4343

4444
import com.jme3.app.Application;
45+
import com.jme3.app.state.AppState;
4546
import com.jme3.app.state.BaseAppState;
4647
import com.jme3.network.Client;
4748
import com.jme3.network.ClientStateListener;
@@ -64,6 +65,8 @@ public class ConnectState extends BaseAppState {
6465

6566
static Logger log = LoggerFactory.getLogger(ConnectState.class);
6667

68+
private AppState parent;
69+
6770
private String host;
6871
private int port;
6972

@@ -76,11 +79,25 @@ public class ConnectState extends BaseAppState {
7679

7780
private volatile boolean closing;
7881

79-
public ConnectState( String host, int port ) {
82+
public ConnectState( AppState parent, String host, int port ) {
83+
this.parent = parent;
8084
this.host = host;
8185
this.port = port;
8286
}
8387

88+
public void disconnect() {
89+
log.info("disconnect()");
90+
closing = true;
91+
log.info("Detaching ConnectionState");
92+
getStateManager().detach(this);
93+
}
94+
95+
public boolean join( String userName ) {
96+
log.info("join(" + userName + ")");
97+
98+
return true;
99+
}
100+
84101
@Override
85102
protected void initialize( Application app ) {
86103

@@ -102,8 +119,8 @@ protected void cleanup( Application app ) {
102119
// Close the connecting panel if it's still open
103120
closeConnectingPanel();
104121

105-
// And re-enable the main menu
106-
getState(MainMenuState.class).setEnabled(true);
122+
// And re-enable the parent
123+
parent.setEnabled(true);
107124
}
108125

109126
protected void closeConnectingPanel() {
@@ -164,13 +181,15 @@ public Object call() {
164181
}
165182
}
166183

167-
protected void connected() {
168-
log.info("connected()");
184+
protected void onConnected() {
185+
log.info("onConnected()");
169186
closeConnectingPanel();
187+
188+
getStateManager().attach(new LoginState());
170189
}
171190

172-
protected void disconnected( DisconnectInfo info ) {
173-
log.info("disconnected(" + info + ")");
191+
protected void onDisconnected( DisconnectInfo info ) {
192+
log.info("onDisconnected(" + info + ")");
174193
closeConnectingPanel();
175194
if( closing ) {
176195
return;
@@ -196,9 +215,7 @@ public ExitAction( String name, boolean close ) {
196215

197216
public void execute( Button source ) {
198217
if( close ) {
199-
log.info("Detaching ConnectionState");
200-
closing = true;
201-
getStateManager().detach(ConnectState.this);
218+
disconnect();
202219
}
203220
}
204221
}
@@ -208,7 +225,7 @@ public void clientConnected( final Client c ) {
208225
log.info("clientConnected(" + c + ")");
209226
getApplication().enqueue(new Callable() {
210227
public Object call() {
211-
connected();
228+
onConnected();
212229
return null;
213230
}
214231
});
@@ -218,7 +235,7 @@ public void clientDisconnected( final Client c, final DisconnectInfo info ) {
218235
log.info("clientDisconnected(" + c + ", " + info + ")");
219236
getApplication().enqueue(new Callable() {
220237
public Object call() {
221-
disconnected(info);
238+
onDisconnected(info);
222239
return null;
223240
}
224241
});

sim-eth-basic/src/main/java/example/HostState.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public HostState( int port, String description ) {
8686

8787
protected void joinGame() {
8888
log.info("joinGame()");
89-
getStateManager().attach(new ConnectState("127.0.0.1", port));
89+
getStateManager().attach(new ConnectState(this, "127.0.0.1", port));
9090
setEnabled(false); // hide our window
9191
}
9292

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* $Id$
3+
*
4+
* Copyright (c) 2016, Simsilica, LLC
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
*
14+
* 2. Redistributions in binary form must reproduce the above copyright
15+
* notice, this list of conditions and the following disclaimer in
16+
* the documentation and/or other materials provided with the
17+
* distribution.
18+
*
19+
* 3. Neither the name of the copyright holder nor the names of its
20+
* contributors may be used to endorse or promote products derived
21+
* from this software without specific prior written permission.
22+
*
23+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34+
* OF THE POSSIBILITY OF SUCH DAMAGE.
35+
*/
36+
37+
package example;
38+
39+
import com.jme3.app.Application;
40+
import com.jme3.app.state.BaseAppState;
41+
import com.jme3.math.*;
42+
import com.jme3.scene.*;
43+
44+
import com.simsilica.lemur.*;
45+
import com.simsilica.lemur.component.SpringGridLayout;
46+
import com.simsilica.lemur.style.ElementId;
47+
48+
/**
49+
* A basic "login" state that provides a simple UI for logging in
50+
* once a connection has been established. This just manages the UI
51+
* and calls back to the ConnectState to do the actual 'work'.
52+
*
53+
* @author Paul Speed
54+
*/
55+
public class LoginState extends BaseAppState {
56+
57+
private Container loginPanel;
58+
private TextField nameField;
59+
60+
public LoginState() {
61+
}
62+
63+
protected void join() {
64+
65+
String name = nameField.getText().trim();
66+
if( getState(ConnectState.class).join(nameField.getText()) ) {
67+
getStateManager().detach(this);
68+
}
69+
}
70+
71+
protected void cancel() {
72+
getState(ConnectState.class).disconnect();
73+
getStateManager().detach(this);
74+
}
75+
76+
@Override
77+
protected void initialize( Application app ) {
78+
loginPanel = new Container();
79+
loginPanel.addChild(new Label("Login", new ElementId("title")));
80+
81+
Container props = loginPanel.addChild(new Container(new SpringGridLayout(Axis.Y, Axis.X, FillMode.None, FillMode.Last)));
82+
props.setBackground(null);
83+
props.addChild(new Label("Name:"));
84+
nameField = props.addChild(new TextField(System.getProperty("user.name")), 1);
85+
86+
Container buttons = loginPanel.addChild(new Container(new SpringGridLayout(Axis.X, Axis.Y)));
87+
buttons.setBackground(null);
88+
buttons.setLayout(new SpringGridLayout(Axis.X, Axis.Y));
89+
buttons.addChild(new ActionButton(new CallMethodAction("Join", this, "join")));
90+
buttons.addChild(new ActionButton(new CallMethodAction("Cancel", this, "cancel")));
91+
92+
float scale = 1.5f * getState(MainMenuState.class).getStandardScale();
93+
loginPanel.setLocalScale(scale);
94+
95+
Vector3f prefs = loginPanel.getPreferredSize().clone();
96+
prefs.x = Math.max(300, prefs.x);
97+
loginPanel.setPreferredSize(prefs.clone());
98+
99+
// Now account for scaling
100+
prefs.multLocal(scale);
101+
102+
int width = app.getCamera().getWidth();
103+
int height = app.getCamera().getHeight();
104+
105+
loginPanel.setLocalTranslation(width * 0.5f - prefs.x * 0.5f, height * 0.5f + prefs.y * 0.5f, 10);
106+
}
107+
108+
@Override
109+
protected void cleanup( Application app ) {
110+
}
111+
112+
@Override
113+
protected void onEnable() {
114+
Node root = ((Main)getApplication()).getGuiNode();
115+
root.attachChild(loginPanel);
116+
}
117+
118+
@Override
119+
protected void onDisable() {
120+
loginPanel.removeFromParent();
121+
}
122+
}

sim-eth-basic/src/main/java/example/MainMenuState.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public class MainMenuState extends BaseAppState {
6969
public MainMenuState() {
7070
}
7171

72+
public float getStandardScale() {
73+
int height = getApplication().getCamera().getHeight();
74+
return height / 720f;
75+
}
76+
7277
protected void showError( String title, String error ) {
7378
getState(OptionPanelState.class).show(title, error);
7479
}
@@ -102,7 +107,7 @@ protected void connect() {
102107

103108
// Add the state that will manage our remote connection and create
104109
// the game states and so on.
105-
getStateManager().attach(new ConnectState(host, port));
110+
getStateManager().attach(new ConnectState(this, host, port));
106111

107112
// Disable ourselves
108113
setEnabled(false);
@@ -176,7 +181,7 @@ protected void initialize( Application app ) {
176181
int height = app.getCamera().getHeight();
177182
Vector3f pref = mainWindow.getPreferredSize().clone();
178183

179-
float standardScale = (height / 720f);
184+
float standardScale = getStandardScale();
180185
pref.multLocal(1.5f * standardScale);
181186

182187
// With a slight bias toward the top

0 commit comments

Comments
 (0)