Skip to content

Commit db95f96

Browse files
committed
Added an in-game menu... this required removing the default
escape mapping and setting up the InputMapper functions for the new InGameMenuState to toggle open/close. Also hooked up the paths so that the disconnect menu would work to take the player back to the main menu. ...still need to hook up the InGameMenuState's exit button.
1 parent 78a2d32 commit db95f96

File tree

8 files changed

+236
-4
lines changed

8 files changed

+236
-4
lines changed

sim-eth-basic/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ dependencies {
3737
// We definitely want a UI
3838
//compile "com.simsilica:lemur:[1.8,)"
3939
compile "com.simsilica:lemur:1.8.2-SNAPSHOT"
40-
compile "com.simsilica:lemur-proto:[1.6,)"
40+
//compile "com.simsilica:lemur-proto:[1.6,)"
41+
compile "com.simsilica:lemur-proto:1.6.2-SNAPSHOT"
4142

4243
// And our base code for sim-etheral, SiO2, etc.
4344
// (many of these need to be built locally and 'gradle install'ed as of this writing)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import com.simsilica.lemur.Button;
5656
import com.simsilica.lemur.OptionPanel;
5757
import com.simsilica.lemur.OptionPanelState;
58+
import com.simsilica.state.CompositeAppState;
5859

5960
import example.net.client.GameClient;
6061

@@ -63,7 +64,7 @@
6364
*
6465
* @author Paul Speed
6566
*/
66-
public class ConnectionState extends BaseAppState {
67+
public class ConnectionState extends CompositeAppState {
6768

6869
static Logger log = LoggerFactory.getLogger(ConnectionState.class);
6970

@@ -115,7 +116,7 @@ public boolean join( String userName ) {
115116
}
116117

117118
protected void onLoggedOn() {
118-
getStateManager().attach(new GameSessionState());
119+
addChild(new GameSessionState(), true);
119120
}
120121

121122
@Override

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import com.jme3.app.Application;
4040

4141
import com.simsilica.event.EventBus;
42+
import com.simsilica.lemur.GuiGlobals;
43+
import com.simsilica.lemur.input.InputMapper;
4244
import com.simsilica.state.CompositeAppState;
4345

4446
/**
@@ -50,6 +52,16 @@
5052
public class GameSessionState extends CompositeAppState {
5153

5254
public GameSessionState() {
55+
// super(); add normal states on the super-constructor
56+
57+
// Add states that need to support enable/disable independent of
58+
// the outer state using addChild().
59+
addChild(new InGameMenuState(false), true);
60+
}
61+
62+
public void disconnect() {
63+
// Remove ourselves
64+
getStateManager().detach(this);
5365
}
5466

5567
@Override
@@ -58,12 +70,17 @@ protected void initialize( Application app ) {
5870

5971
EventBus.publish(GameSessionEvent.sessionStarted, new GameSessionEvent());
6072

73+
InputMapper inputMapper = GuiGlobals.getInstance().getInputMapper();
74+
inputMapper.activateGroup(MainGameFunctions.IN_GAME);
6175
}
6276

6377
@Override
6478
protected void cleanup( Application app ) {
6579
super.cleanup(app);
6680

81+
InputMapper inputMapper = GuiGlobals.getInstance().getInputMapper();
82+
inputMapper.deactivateGroup(MainGameFunctions.IN_GAME);
83+
6784
EventBus.publish(GameSessionEvent.sessionEnded, new GameSessionEvent());
6885
}
6986
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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 java.util.*;
40+
41+
import org.slf4j.*;
42+
43+
import com.jme3.app.Application;
44+
import com.jme3.app.state.BaseAppState;
45+
import com.jme3.math.*;
46+
import com.jme3.scene.*;
47+
48+
import com.simsilica.lemur.*;
49+
import com.simsilica.lemur.component.SpringGridLayout;
50+
import com.simsilica.lemur.input.InputMapper;
51+
import com.simsilica.lemur.style.ElementId;
52+
53+
/**
54+
*
55+
*
56+
* @author Paul Speed
57+
*/
58+
public class InGameMenuState extends BaseAppState {
59+
60+
static Logger log = LoggerFactory.getLogger(InGameMenuState.class);
61+
62+
private Container mainWindow;
63+
private TabbedPanel tabs;
64+
65+
private List<Action> sessionActions = new ArrayList<>();
66+
private Container sessionButtons;
67+
68+
public InGameMenuState( boolean enabled ) {
69+
setEnabled(enabled);
70+
sessionActions.add(new CallMethodAction("Disconnect", this, "disconnect"));
71+
sessionActions.add(new CallMethodAction("Exit", this, "exitGame"));
72+
}
73+
74+
75+
protected void disconnect() {
76+
log.info("disconnect()");
77+
getState(OptionPanelState.class).show("Disconnect?", "Really disconnect?",
78+
new CallMethodAction("Yes",
79+
getState(ConnectionState.class),
80+
"disconnect"),
81+
new EmptyAction("No"),
82+
new EmptyAction("Cancel"));
83+
}
84+
85+
protected void exitGame() {
86+
log.info("exitGame()");
87+
}
88+
89+
public List<Action> getSessionActions() {
90+
return sessionActions;
91+
}
92+
93+
public void toggleEnabled() {
94+
setEnabled(!isEnabled());
95+
}
96+
97+
@Override
98+
protected void initialize( Application app ) {
99+
mainWindow = new Container();
100+
101+
tabs = mainWindow.addChild(new TabbedPanel());
102+
103+
sessionButtons = tabs.addTab("Game Session", new Container());
104+
for( Action a : getSessionActions() ) {
105+
sessionButtons.addChild(new ActionButton(a));
106+
}
107+
108+
// Calculate a standard scale and position from the app's camera
109+
// height
110+
int height = app.getCamera().getHeight();
111+
Vector3f pref = mainWindow.getPreferredSize().clone();
112+
113+
float standardScale = getState(MainMenuState.class).getStandardScale();
114+
pref.multLocal(1.5f * standardScale);
115+
116+
// With a slight bias toward the top
117+
float y = height * 0.6f + pref.y * 0.5f;
118+
119+
mainWindow.setLocalTranslation(100 * standardScale, y, 0);
120+
mainWindow.setLocalScale(1.5f * standardScale);
121+
122+
InputMapper inputMapper = GuiGlobals.getInstance().getInputMapper();
123+
inputMapper.addDelegate(MainGameFunctions.F_IN_GAME_MENU, this, "toggleEnabled");
124+
}
125+
126+
@Override
127+
protected void cleanup( Application app ) {
128+
129+
InputMapper inputMapper = GuiGlobals.getInstance().getInputMapper();
130+
inputMapper.removeDelegate(MainGameFunctions.F_IN_GAME_MENU, this, "toggleEnabled");
131+
}
132+
133+
@Override
134+
protected void onEnable() {
135+
Node gui = ((Main)getApplication()).getGuiNode();
136+
gui.attachChild(mainWindow);
137+
}
138+
139+
@Override
140+
protected void onDisable() {
141+
mainWindow.removeFromParent();
142+
}
143+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,18 @@ public void simpleInitApp() {
9696
BaseStyles.loadGlassStyle();
9797
globals.getStyles().setDefaultStyle("glass");
9898

99+
MainGameFunctions.initializeDefaultMappings(globals.getInputMapper());
100+
99101
// Since we've added the background spinning widget here, we'll
100102
// also register events to enable/disable it.
101103
EventBus.addListener(new GameListener(),
102104
GameSessionEvent.sessionStarted,
103105
GameSessionEvent.sessionEnded);
106+
107+
// Get rid of the default close mapping in InputManager
108+
if( inputManager.hasMapping(INPUT_MAPPING_EXIT) ) {
109+
inputManager.deleteMapping(INPUT_MAPPING_EXIT);
110+
}
104111

105112
}
106113

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.input.KeyInput;
40+
import com.simsilica.lemur.input.FunctionId;
41+
import com.simsilica.lemur.input.InputMapper;
42+
43+
44+
/**
45+
* Defines a set of global game functions and some default key/control
46+
* mappings.
47+
*
48+
* @author Paul Speed
49+
*/
50+
public class MainGameFunctions {
51+
52+
public static final String IN_GAME = "In Game";
53+
public static final FunctionId F_IN_GAME_MENU = new FunctionId(IN_GAME, "Menu");
54+
55+
public static void initializeDefaultMappings( InputMapper inputMapper ) {
56+
57+
inputMapper.map(F_IN_GAME_MENU, KeyInput.KEY_ESCAPE);
58+
}
59+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ protected void initialize( Application app ) {
174174
hostDescription = hostPanel.addChild(new TextField("This a game server.\nThere are many like it\nbut this one is mine."));
175175
hostDescription.setSingleLine(false);
176176
hostPanel.addChild(new ActionButton(new CallMethodAction("Begin Hosting", this, "host")));
177-
177+
178+
179+
ActionButton exit = mainWindow.addChild(new ActionButton(new CallMethodAction("Exit Game", app, "stop")));
180+
exit.setInsets(new Insets3f(10, 10, 10, 10));
178181

179182
// Calculate a standard scale and position from the app's camera
180183
// height

sim-eth-basic/src/main/resources/log4j2.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<Loggers>
1717
<Logger name="example" level="DEBUG"/>
1818
<Logger name="com.simsilica.sim" level="TRACE"/>
19+
<Logger name="com.simsilica.lemur.CallMethodAction" level="TRACE"/>
1920
<Root level="INFO">
2021
<AppenderRef ref="RollingFile"/>
2122
<AppenderRef ref="STDOUT"/>

0 commit comments

Comments
 (0)