Skip to content

Commit 4c5c06a

Browse files
committed
Move UI responsibilities to GameInterfaceControl
1 parent 08775a2 commit 4c5c06a

File tree

4 files changed

+63
-55
lines changed

4 files changed

+63
-55
lines changed

blastback_client/src/com/blastback/appstates/GUIAppState.java

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public class GUIAppState extends BaseAppState implements ScreenController
2525
{
2626

2727
private Nifty _niftyInstance;
28-
Element _scoreboardElement;
2928
private List<BaseAppState> _gameStates;
3029
private GameClient _application;
3130

@@ -81,22 +80,7 @@ public void joinButtonClicked()
8180
}
8281
}
8382

84-
public void displayScoreboard(boolean value)
85-
{
86-
if (_scoreboardElement == null)
87-
{
88-
_scoreboardElement = _niftyInstance.createPopup("popup_scoreboard");
89-
}
90-
91-
if (value)
92-
{
93-
_niftyInstance.showPopup(_niftyInstance.getCurrentScreen(), _scoreboardElement.getId(), null);
94-
} else
95-
{
96-
_niftyInstance.closePopup(_scoreboardElement.getId());
97-
98-
}
99-
}
83+
10084

10185
private void showMessage(String msg)
10286
{
@@ -117,36 +101,6 @@ public void exitButtonClicked()
117101
_application.stop();
118102
}
119103

120-
/**
121-
* Update ammo label (only in hud-screen).
122-
*
123-
* @param currentAmmo amount of currently owned ammo
124-
* @param maxAmmo max amount of ammo
125-
*/
126-
public void updateAmmo(int currentAmmo, int maxAmmo)
127-
{
128-
Element ammoLabel = _niftyInstance.getCurrentScreen().findElementById("text_ammo");
129-
if (ammoLabel != null)
130-
{
131-
ammoLabel.getRenderer(TextRenderer.class).setText("AMMO " + currentAmmo + " / " + maxAmmo);
132-
}
133-
}
134-
135-
/**
136-
* Update timer label (only in hud-screen).
137-
*
138-
* @param timeLeft time left in seconds
139-
*/
140-
public void updateTimer(long timeLeft)
141-
{
142-
Element timerLabel = _niftyInstance.getCurrentScreen().findElementById("text_timer");
143-
if (timerLabel != null)
144-
{
145-
int minutes = (int) (timeLeft / 60);
146-
int seconds = (int) (timeLeft - 60 * minutes);
147-
timerLabel.getRenderer(TextRenderer.class).setText("TIMER " + minutes + ":" + seconds);
148-
}
149-
}
150104

151105
/**
152106
* It is a way of verifying user's ip/port combination. I wasn´t able to
@@ -290,4 +244,11 @@ protected void onEnable()
290244
protected void onDisable()
291245
{
292246
}
247+
248+
public Nifty getNifty()
249+
{
250+
return _niftyInstance;
251+
}
252+
253+
293254
}

blastback_client/src/com/blastback/appstates/PlayerAppState.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private void initPlayer()
121121
// Add controls to spatials
122122
_player.addControl(_charControl);
123123
_player.addControl(new PlayerMovementControl());
124-
_player.addControl(new GameInterfaceControl(_guiAppState));
124+
_player.addControl(new GameInterfaceControl(_guiAppState.getNifty()));
125125
_player.addControl(new PlayerShootingControl(new Vector3f(0f, 0f, -1.5f))); //to adjust
126126
_healthControl = new CharacterHealthControl();
127127
_player.addControl(_healthControl);

blastback_client/src/com/blastback/controls/GameInterfaceControl.java

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,71 @@
44
import com.jme3.renderer.RenderManager;
55
import com.jme3.renderer.ViewPort;
66
import com.jme3.scene.control.AbstractControl;
7+
import de.lessvoid.nifty.Nifty;
8+
import de.lessvoid.nifty.elements.Element;
9+
import de.lessvoid.nifty.elements.render.TextRenderer;
710

811

912
public class GameInterfaceControl extends AbstractControl
1013
{
11-
private GUIAppState _guiAppState;
14+
private Nifty _niftyInstance;
15+
Element _scoreboardElement;
1216

13-
public GameInterfaceControl(GUIAppState guiAppState)
17+
public GameInterfaceControl( Nifty niftyInstance)
1418
{
15-
this._guiAppState = guiAppState;
19+
this._niftyInstance = niftyInstance;
1620
}
17-
18-
public GUIAppState getGui()
21+
22+
public void displayScoreboard(boolean value)
1923
{
20-
return _guiAppState;
24+
if (_scoreboardElement == null)
25+
{
26+
_scoreboardElement = _niftyInstance.createPopup("popup_scoreboard");
27+
}
28+
29+
if (value)
30+
{
31+
_niftyInstance.showPopup(_niftyInstance.getCurrentScreen(), _scoreboardElement.getId(), null);
32+
} else
33+
{
34+
_niftyInstance.closePopup(_scoreboardElement.getId());
35+
36+
}
2137
}
2238

2339

2440

41+
/**
42+
* Update ammo label (only in hud-screen).
43+
*
44+
* @param currentAmmo amount of currently owned ammo
45+
* @param maxAmmo max amount of ammo
46+
*/
47+
public void updateAmmo(int currentAmmo, int maxAmmo)
48+
{
49+
Element ammoLabel = _niftyInstance.getCurrentScreen().findElementById("text_ammo");
50+
if (ammoLabel != null)
51+
{
52+
ammoLabel.getRenderer(TextRenderer.class).setText("AMMO " + currentAmmo + " / " + maxAmmo);
53+
}
54+
}
55+
56+
/**
57+
* Update timer label (only in hud-screen).
58+
*
59+
* @param timeLeft time left in seconds
60+
*/
61+
public void updateTimer(long timeLeft)
62+
{
63+
Element timerLabel = _niftyInstance.getCurrentScreen().findElementById("text_timer");
64+
if (timerLabel != null)
65+
{
66+
int minutes = (int) (timeLeft / 60);
67+
int seconds = (int) (timeLeft - 60 * minutes);
68+
timerLabel.getRenderer(TextRenderer.class).setText("TIMER " + minutes + ":" + seconds);
69+
}
70+
}
71+
2572
@Override
2673
protected void controlUpdate(float tpf)
2774
{

blastback_client/src/com/blastback/controls/PlayerInputControl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void onAction(String name, boolean keyPressed, float tpf)
7272
}
7373
if (name.equals("Tab"))
7474
{
75-
_gameInterfaceControl.getGui().displayScoreboard(keyPressed);
75+
_gameInterfaceControl.displayScoreboard(keyPressed);
7676
}
7777

7878
if (name.equals("Shoot"))

0 commit comments

Comments
 (0)