Skip to content

Commit a93f894

Browse files
committed
feat(nui): convert in-game menu to nui
1 parent e957061 commit a93f894

File tree

9 files changed

+199
-109
lines changed

9 files changed

+199
-109
lines changed

engine/src/main/java/org/destinationsol/SolApplication.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,11 @@ public SolLayouts getLayouts() {
402402

403403
public void finishGame() {
404404
solGame.onGameEnd(gameContext.getBean(Context.class));
405-
solGame = null;
406405
// TODO: remove the following line when all screens have been ported to use NUI
407406
inputManager.setScreen(this, null);
407+
inputManager.update(this); // Force an update to remove all the InputManager UI screens
408+
409+
solGame = null;
408410
nuiManager.pushScreen(menuScreens.main);
409411
entityCreated = false;
410412
}

engine/src/main/java/org/destinationsol/game/screens/GameScreens.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.destinationsol.game.console.Console;
2020
import org.destinationsol.game.context.Context;
2121
import org.destinationsol.ui.SolLayouts;
22+
import org.destinationsol.ui.nui.screens.MenuScreen;
2223

2324
import javax.inject.Inject;
2425

@@ -46,7 +47,7 @@ public GameScreens(SolApplication cmp, Context context) {
4647
mainGameScreen = (org.destinationsol.ui.nui.screens.MainGameScreen) cmp.getNuiManager().createScreen(NUI_MAIN_GAME_SCREEN_MOBILE_URI);
4748
}
4849
mapScreen = new MapScreen(rightPaneLayout, cmp.isMobile(), cmp.getOptions());
49-
menuScreen = new MenuScreen(layouts.menuLayout, cmp.getOptions());
50+
menuScreen = (MenuScreen) cmp.getNuiManager().createScreen("engine:menuScreen");
5051
inventoryScreen = new InventoryScreen(cmp.getOptions());
5152
talkScreen = new TalkScreen(layouts.menuLayout, cmp.getOptions());
5253
waypointCreationScreen = new WaypointCreationScreen(layouts.menuLayout, cmp.getOptions(), mapScreen);

engine/src/main/java/org/destinationsol/game/screens/MainGameScreen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void updateCustom(SolApplication solApplication, SolInputManager.InputPoi
122122

123123
if (solApplication.getNuiManager().hasScreenOfType(ConsoleScreen.class)) {
124124
controls.forEach(x -> x.setEnabled(false));
125-
} else if (!inputMan.isScreenOn(screens.menuScreen)) {
125+
} else if (!nuiManager.hasScreen(screens.menuScreen)) {
126126
game.setPaused(false);
127127
controls.forEach(x -> x.setEnabled(true));
128128
}

engine/src/main/java/org/destinationsol/game/screens/MenuScreen.java

Lines changed: 0 additions & 100 deletions
This file was deleted.

engine/src/main/java/org/destinationsol/ui/SolInputManager.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ public void update(SolApplication solApplication) {
198198

199199
// This keeps the mouse within the window, but only when playing the game with the mouse.
200200
// All other times the mouse can freely leave and return.
201-
if (!mobile && solApplication.getOptions().controlType == GameOptions.ControlType.MIXED && game != null && getTopScreen() != game.getScreens().menuScreen) {
201+
if (!mobile && solApplication.getOptions().controlType == GameOptions.ControlType.MIXED && game != null &&
202+
solApplication.getNuiManager().getTopScreen() != game.getScreens().menuScreen) {
202203
if (!Gdx.input.isCursorCatched() && !osIsLinux) {
203204
Gdx.input.setCursorCatched(true);
204205
}
@@ -306,7 +307,8 @@ private void updateCursor(SolApplication solApplication) {
306307
}
307308
return;
308309
}
309-
if (mousePrevPos.epsilonEquals(mousePos, 0) && game != null && getTopScreen() != game.getScreens().menuScreen) {
310+
if (mousePrevPos.epsilonEquals(mousePos, 0) && game != null &&
311+
solApplication.getNuiManager().getTopScreen() != game.getScreens().menuScreen) {
310312
mouseIdleTime += Const.REAL_TIME_STEP;
311313
currCursor = mouseIdleTime < CURSOR_SHOW_TIME ? uiCursor : null;
312314
} else {
@@ -365,7 +367,8 @@ public void draw(UiDrawer uiDrawer, SolApplication solApplication) {
365367

366368
SolGame game = solApplication.getGame();
367369
TutorialManager tutorialManager = game == null ? null : game.getTutMan();
368-
if (tutorialManager != null && getTopScreen() != game.getScreens().menuScreen) {
370+
if (tutorialManager != null &&
371+
solApplication.getNuiManager().getTopScreen() != game.getScreens().menuScreen) {
369372
tutorialManager.draw(uiDrawer);
370373
}
371374
}

engine/src/main/java/org/destinationsol/ui/nui/screens/MainGameScreen.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ public void update(float delta) {
427427
super.update(delta);
428428
SolInputManager solInputManager = solApplication.getInputManager();
429429
GameScreens gameScreens = solApplication.getGame().getScreens();
430-
if (!solInputManager.isScreenOn(gameScreens.menuScreen) &&
430+
if (!nuiManager.hasScreen(gameScreens.menuScreen) &&
431431
!solInputManager.isScreenOn(gameScreens.mapScreen)) {
432432
((AbstractWidget) contents).setVisible(true);
433433
} else {
@@ -901,10 +901,9 @@ public UITextureRegion get() {
901901
}
902902

903903
private void onMenuButtonClicked(UIWidget widget) {
904-
SolInputManager solInputManager = solApplication.getInputManager();
905904
GameScreens gameScreens = solApplication.getGame().getScreens();
906905

907-
solInputManager.setScreen(solApplication, gameScreens.menuScreen);
906+
nuiManager.pushScreen(gameScreens.menuScreen);
908907
}
909908

910909
private void onMapButtonClicked(UIWidget widget) {
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2021 The Terasology Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.destinationsol.ui.nui.screens;
18+
19+
import org.destinationsol.SolApplication;
20+
import org.destinationsol.ui.nui.NUIScreenLayer;
21+
import org.destinationsol.ui.nui.widgets.KeyActivatedButton;
22+
import org.terasology.nui.backends.libgdx.GDXInputUtil;
23+
import org.terasology.nui.widgets.UIButton;
24+
25+
import javax.inject.Inject;
26+
27+
/**
28+
* This is the game menu, accessible from anytime in-game by pressing the menu key (default "Escape").
29+
* The game menu allows you to configure volume options, trigger a respawn or exit to the main menu.
30+
*/
31+
public class MenuScreen extends NUIScreenLayer {
32+
private final SolApplication solApplication;
33+
34+
@Inject
35+
public MenuScreen(SolApplication solApplication) {
36+
this.solApplication = solApplication;
37+
}
38+
39+
@Override
40+
public void initialise() {
41+
UIButton canSellUsedItemsButton = find("canSellUsedItemsButton", UIButton.class);
42+
canSellUsedItemsButton.setText("Can sell used items: " + (solApplication.getOptions().canSellEquippedItems ? "Yes" : "No"));
43+
canSellUsedItemsButton.subscribe(button -> {
44+
solApplication.getOptions().canSellEquippedItems = !solApplication.getOptions().canSellEquippedItems;
45+
((UIButton)button).setText("Can sell used items: " + (solApplication.getOptions().canSellEquippedItems ? "Yes" : "No"));
46+
});
47+
48+
UIButton soundVolumeButton = find("soundVolumeButton", UIButton.class);
49+
soundVolumeButton.setText("Sound Volume: " + solApplication.getOptions().sfxVolume.getName());
50+
soundVolumeButton.subscribe(button -> {
51+
solApplication.getOptions().advanceSoundVolMul();
52+
((UIButton)button).setText("Sound Volume: " + solApplication.getOptions().sfxVolume.getName());
53+
});
54+
55+
UIButton musicVolumeButton = find("musicVolumeButton", UIButton.class);
56+
musicVolumeButton.setText("Music Volume: " + solApplication.getOptions().musicVolume.getName());
57+
musicVolumeButton.subscribe(button -> {
58+
solApplication.getOptions().advanceMusicVolMul();
59+
((UIButton)button).setText("Music Volume: " + solApplication.getOptions().musicVolume.getName());
60+
solApplication.getMusicManager().changeVolume(solApplication.getOptions());
61+
});
62+
63+
UIButton respawnButton = find("respawnButton", UIButton.class);
64+
respawnButton.subscribe(button -> {
65+
solApplication.getGame().respawn();
66+
nuiManager.removeScreen(this);
67+
});
68+
69+
UIButton exitButton = find("exitButton", UIButton.class);
70+
exitButton.subscribe(button -> {
71+
solApplication.finishGame();
72+
});
73+
74+
KeyActivatedButton resumeButton = find("resumeButton", KeyActivatedButton.class);
75+
resumeButton.setKey(GDXInputUtil.GDXToNuiKey(solApplication.getOptions().getKeyClose()));
76+
resumeButton.subscribe(button -> {
77+
nuiManager.removeScreen(this);
78+
});
79+
}
80+
81+
@Override
82+
public void onAdded() {
83+
solApplication.getGame().setPaused(true);
84+
}
85+
86+
@Override
87+
public void onRemoved() {
88+
solApplication.getGame().setPaused(false);
89+
}
90+
91+
@Override
92+
protected boolean escapeCloses() {
93+
return false;
94+
}
95+
}

engine/src/main/resources/org/destinationsol/assets/skins/mainGameScreen.skin

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@
2626
"controlsUIButton": {
2727
"font": "engine:main#0.95"
2828
},
29+
"unrestrictedMenuBox": {
30+
"elements": {
31+
"UIBox": {
32+
"background": "engine:background"
33+
}
34+
}
35+
},
36+
"menuUIButton": {
37+
"elements": {
38+
"UIButton": {
39+
"max-width": 640,
40+
"max-height": 128
41+
}
42+
},
43+
"font": "engine:main#1"
44+
},
2945
"warnDrawers": {
3046
"elements": {
3147
"UIWarnDrawer": {
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"type": "MenuScreen",
3+
"skin": "engine:mainGameScreen",
4+
"contents": {
5+
"type": "RelativeLayout",
6+
"contents": [
7+
{
8+
"type": "UIBackingBox",
9+
"family": "unrestrictedMenuBox",
10+
"layoutInfo": {
11+
"position-top": {},
12+
"position-bottom": {},
13+
"position-left": {},
14+
"position-right": {}
15+
}
16+
},
17+
{
18+
"type": "ColumnLayout",
19+
"id": "menuButtons",
20+
"family": "menuUIButton",
21+
"columns": 1,
22+
"verticalSpacing": 10,
23+
"contents": [
24+
{
25+
"type": "UIButton",
26+
"id": "canSellUsedItemsButton",
27+
"text": "Can sell used items"
28+
},
29+
{
30+
"type": "UIButton",
31+
"id": "soundVolumeButton",
32+
"text": "Sound Volume"
33+
},
34+
{
35+
"type": "UIButton",
36+
"id": "musicVolumeButton",
37+
"text": "Music Volume"
38+
},
39+
{
40+
"type": "UIButton",
41+
"id": "respawnButton",
42+
"text": "Respawn"
43+
},
44+
{
45+
"type": "UIButton",
46+
"id": "exitButton",
47+
"text": "Exit"
48+
},
49+
{
50+
"type": "KeyActivatedButton",
51+
"id": "resumeButton",
52+
"text": "Resume"
53+
}
54+
],
55+
"layoutInfo": {
56+
"position-horizontal-center": {},
57+
"position-bottom": {
58+
"offset": 32
59+
},
60+
"position-top": {
61+
"target": "MIDDLE",
62+
"offset": -64
63+
},
64+
"position-left": {
65+
"offset": 64
66+
},
67+
"position-right": {
68+
"offset": 64
69+
}
70+
}
71+
}
72+
]
73+
}
74+
}

0 commit comments

Comments
 (0)