Skip to content

Commit 02a7d2a

Browse files
committed
feat(nui): convert the tutorial screen to NUI
1 parent b32b8dc commit 02a7d2a

8 files changed

Lines changed: 184 additions & 41 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.destinationsol.ui.nui.NUIManager;
3030
import org.destinationsol.ui.nui.NUIScreenLayer;
3131
import org.destinationsol.ui.nui.screens.ConsoleScreen;
32+
import org.destinationsol.ui.nui.screens.TutorialScreen;
3233
import org.destinationsol.ui.nui.screens.UIShipControlsScreen;
3334

3435
import java.util.ArrayList;
@@ -117,7 +118,8 @@ public void updateCustom(SolApplication solApplication, SolInputManager.InputPoi
117118
NUIScreenLayer topScreen = nuiManager.getTopScreen();
118119
boolean controlsEnabled = inputMan.getTopScreen() == this &&
119120
(topScreen instanceof org.destinationsol.ui.nui.screens.MainGameScreen ||
120-
topScreen instanceof UIShipControlsScreen);
121+
topScreen instanceof UIShipControlsScreen ||
122+
topScreen instanceof TutorialScreen);
121123
shipControl.update(solApplication, controlsEnabled);
122124

123125
if (solApplication.getNuiManager().hasScreenOfType(ConsoleScreen.class)) {

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,6 @@ public void draw(UiDrawer uiDrawer, SolApplication solApplication) {
364364
}
365365
}
366366
uiDrawer.setTextMode(null);
367-
368-
SolGame game = solApplication.getGame();
369-
TutorialManager tutorialManager = game == null ? null : game.getTutMan();
370-
if (tutorialManager != null && getTopScreen() != game.getScreens().menuScreen) {
371-
tutorialManager.draw(uiDrawer);
372-
}
373367
}
374368

375369
public void drawCursor(UiDrawer uiDrawer) {

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

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
package org.destinationsol.ui;
1717

1818
import com.badlogic.gdx.Gdx;
19-
import com.badlogic.gdx.math.Rectangle;
2019
import org.destinationsol.GameOptions;
21-
import org.destinationsol.common.SolColor;
2220
import org.destinationsol.game.SolGame;
2321
import org.destinationsol.game.UpdateAwareSystem;
2422
import org.destinationsol.game.item.SolItem;
2523
import org.destinationsol.game.screens.GameScreens;
2624
import org.destinationsol.game.screens.MainGameScreen;
2725
import org.destinationsol.game.screens.ShipMixedControl;
28-
import org.destinationsol.ui.nui.screens.InventoryScreen;
26+
import org.destinationsol.ui.nui.NUIManager;
27+
import org.destinationsol.ui.nui.NUIScreenLayer;
28+
import org.destinationsol.ui.nui.screens.TutorialScreen;
2929
import org.destinationsol.ui.nui.screens.UIShipControlsScreen;
3030
import org.destinationsol.ui.nui.widgets.UIWarnButton;
3131

@@ -35,25 +35,23 @@
3535
import java.util.List;
3636

3737
public class TutorialManager implements UpdateAwareSystem {
38-
private final Rectangle background;
38+
private final NUIManager nuiManager;
39+
private final TutorialScreen tutorialScreen;
3940
private final ArrayList<Step> steps;
4041
private final GameScreens screens;
4142
private final GameOptions gameOptions;
4243
private final Provider<SolGame> game;
43-
private final DisplayDimensions displayDimensions;
4444

4545
private int stepIndex;
4646

4747
@Inject
48-
public TutorialManager(GameScreens screens, GameOptions gameOptions, Provider<SolGame> game, DisplayDimensions displayDimensions) {
48+
public TutorialManager(GameScreens screens, GameOptions gameOptions, Provider<SolGame> game, NUIManager nuiManager) {
4949
this.screens = screens;
5050
this.gameOptions = gameOptions;
5151
this.game = game;
52-
this.displayDimensions = displayDimensions;
52+
this.nuiManager = nuiManager;
53+
this.tutorialScreen = (TutorialScreen) nuiManager.createScreen("engine:tutorialScreen");
5354

54-
float backgroundW = displayDimensions.getRatio() * .5f;
55-
float backgroundH = .2f;
56-
background = new Rectangle(displayDimensions.getRatio() / 2 - backgroundW / 2, 1 - backgroundH, backgroundW, backgroundH);
5755
steps = new ArrayList<>();
5856
stepIndex = 0;
5957
}
@@ -124,10 +122,10 @@ public void start() {
124122
}
125123

126124
if (mobile) {
127-
addStep("Close the map", screens.mapScreen.getCloseButton(), true);
125+
addScreenCloseStep("Close the map", screens.mapScreen.getCloseButton(), screens.mapScreen);
128126
} else {
129-
addStep("Close the map\n(" + gameOptions.getKeyMapName() + " or " + gameOptions.getKeyCloseName() + " keys)",
130-
screens.mapScreen.getCloseButton(), true);
127+
addScreenCloseStep("Close the map\n(" + gameOptions.getKeyMapName() + " or " + gameOptions.getKeyCloseName() + " keys)",
128+
screens.mapScreen.getCloseButton(), screens.mapScreen);
131129
}
132130

133131
UIWarnButton inventoryButton = nuiMain.getInventoryButton();
@@ -177,9 +175,9 @@ public void start() {
177175
}
178176

179177
if (mobile) {
180-
addStep("Close the inventory\n(Touch the screen outside inventory)", screens.inventoryScreen.getCloseButton(), true);
178+
addScreenCloseStep("Close the inventory\n(Touch the screen outside inventory)", screens.inventoryScreen.getCloseButton(), screens.inventoryScreen);
181179
} else {
182-
addStep("Close the inventory (" + gameOptions.getKeyCloseName() + " key)", screens.inventoryScreen.getCloseButton(), true);
180+
addScreenCloseStep("Close the inventory (" + gameOptions.getKeyCloseName() + " key)", screens.inventoryScreen.getCloseButton(), screens.inventoryScreen);
183181
}
184182

185183
if (mouseCtrl) {
@@ -210,9 +208,9 @@ public void start() {
210208
}
211209

212210
if (mobile) {
213-
addStep("Close the Buy screen\n(Touch the screen outside inventory)", screens.inventoryScreen.getCloseButton(), true);
211+
addScreenCloseStep("Close the Buy screen\n(Touch the screen outside inventory)", screens.inventoryScreen.getCloseButton(), screens.inventoryScreen);
214212
} else {
215-
addStep("Close the Buy screen\n(" + gameOptions.getKeyCloseName() + " key)", screens.inventoryScreen.getCloseButton(), true);
213+
addScreenCloseStep("Close the Buy screen\n(" + gameOptions.getKeyCloseName() + " key)", screens.inventoryScreen.getCloseButton(), screens.inventoryScreen);
216214
}
217215

218216
if (mouseCtrl) {
@@ -243,7 +241,9 @@ public void start() {
243241
addStep("Buy new ships, hire mercenaries\n" + shootKey2, nuiShootCtrl);
244242
addStep("Tutorial is complete and will exit now!\n" + shootKey2, nuiShootCtrl);
245243
}
244+
246245
steps.get(0).start();
246+
tutorialScreen.setTutorialText(steps.get(0).text);
247247
}
248248

249249
private void addStep(String text, SolUiControl ctrl) {
@@ -270,30 +270,31 @@ private void addStep(Step step) {
270270
steps.add(step);
271271
}
272272

273+
private void addScreenCloseStep(String text, UIWarnButton ctrl, NUIScreenLayer uiScreen) {
274+
steps.add(new NuiScreenCloseStep(text, ctrl, nuiManager, uiScreen));
275+
}
276+
273277
@Override
274278
public void update(SolGame game, float timeStep) {
279+
if (nuiManager.getTopScreen() != tutorialScreen) {
280+
if (nuiManager.hasScreen(tutorialScreen)) {
281+
tutorialScreen.moveToTop();
282+
} else {
283+
nuiManager.pushScreen(tutorialScreen);
284+
}
285+
}
286+
275287
Step step = steps.get(stepIndex);
276288
step.highlight();
277289
if (step.canProgressToNextStep()) {
278290
stepIndex++;
279291
if (stepIndex < steps.size()) {
280292
steps.get(stepIndex).start();
293+
tutorialScreen.setTutorialText(steps.get(stepIndex).text);
281294
}
282295
}
283296
}
284297

285-
public void draw(UiDrawer uiDrawer) {
286-
if (isFinished()) {
287-
return;
288-
}
289-
Step step = steps.get(stepIndex);
290-
uiDrawer.draw(background, SolColor.UI_BG_LIGHT);
291-
uiDrawer.drawLine(background.x, background.y, 0, background.width, SolColor.WHITE);
292-
uiDrawer.drawLine(background.x + background.width, background.y, 90, background.height, SolColor.WHITE);
293-
uiDrawer.drawLine(background.x, background.y, 90, background.height, SolColor.WHITE);
294-
uiDrawer.drawString(step.text, displayDimensions.getRatio() / 2, background.y + background.height / 2, FontSize.TUT, true, SolColor.WHITE);
295-
}
296-
297298
public boolean isFinished() {
298299
return stepIndex == steps.size();
299300
}
@@ -367,11 +368,30 @@ public boolean canProgressToNextStep() {
367368
}
368369
}
369370

371+
public static class NuiScreenCloseStep extends NuiStep {
372+
private final NUIManager nuiManager;
373+
private final NUIScreenLayer uiScreen;
374+
375+
public NuiScreenCloseStep(String text, UIWarnButton closeButton, NUIManager nuiManager, NUIScreenLayer uiScreen) {
376+
super(text, closeButton, true);
377+
this.nuiManager = nuiManager;
378+
this.uiScreen = uiScreen;
379+
}
380+
381+
@Override
382+
public boolean canProgressToNextStep() {
383+
if (super.canProgressToNextStep()) {
384+
return true;
385+
}
386+
return !nuiManager.hasScreen(uiScreen);
387+
}
388+
}
389+
370390
public static class SelectEquippedItemStep extends Step {
371-
InventoryScreen inventoryScreen;
391+
org.destinationsol.ui.nui.screens.InventoryScreen inventoryScreen;
372392
SolGame game;
373393

374-
public SelectEquippedItemStep(String text, InventoryScreen inventoryScreen, SolGame game) {
394+
public SelectEquippedItemStep(String text, org.destinationsol.ui.nui.screens.InventoryScreen inventoryScreen, SolGame game) {
375395
super(text, null, true);
376396
this.inventoryScreen = inventoryScreen;
377397
this.game = game;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ public void onDraw(Canvas canvas) {
535535
super.onDraw(canvas);
536536

537537
// Don't render the borders on-top of the map screen.
538-
if (!solApplication.getInputManager().isScreenOn(solApplication.getGame().getScreens().mapScreen)) {
538+
if (!nuiManager.hasScreen(solApplication.getGame().getScreens().mapScreen)) {
539539
try (NUIManager.LegacyUiDrawerWrapper wrapper = nuiManager.getLegacyUiDrawer()) {
540540
borderDrawer.draw(wrapper.getUiDrawer(), solApplication, solApplication.getGame().getContext());
541541
zoneNameAnnouncer.drawText(wrapper.getUiDrawer());

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,13 @@ public void onRemoved() {
237237

238238
SolGame solGame = solApplication.getGame();
239239
solGame.getMapDrawer().setToggled(false);
240-
solApplication.getInputManager().setScreen(solApplication, solGame.getScreens().mainGameScreen);
240+
solApplication.getInputManager().setScreen(solApplication, solGame.getScreens().oldMainGameScreen);
241241
}
242242

243243
@Override
244244
public void update(float delta) {
245245
super.update(delta);
246-
ShipUiControl shipControl = solApplication.getGame().getScreens().mainGameScreen.getShipControl();
246+
ShipUiControl shipControl = solApplication.getGame().getScreens().oldMainGameScreen.getShipControl();
247247
if (shipControl instanceof ShipMouseControl) {
248248
shipControl.update(solApplication, true);
249249
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2022 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.game.SolGame;
20+
import org.destinationsol.ui.nui.NUIScreenLayer;
21+
import org.terasology.nui.widgets.UILabel;
22+
23+
import javax.inject.Inject;
24+
25+
/**
26+
* This screen displays the message box shown during the tutorial to instruct the user.
27+
* It is unusual in that it should always be rendered on-top of all other UI screens.
28+
* See {@link #moveToTop()} and {@link org.destinationsol.ui.TutorialManager#update(SolGame, float)} for how this is done.
29+
*/
30+
public class TutorialScreen extends NUIScreenLayer {
31+
private UILabel tutorialText;
32+
private boolean isReplaceRemove;
33+
34+
@Inject
35+
public TutorialScreen() {
36+
}
37+
38+
@Override
39+
public void initialise() {
40+
tutorialText = find("tutorialText", UILabel.class);
41+
}
42+
43+
public String getTutorialText() {
44+
return tutorialText.getText();
45+
}
46+
47+
public void setTutorialText(String text) {
48+
tutorialText.setText(text);
49+
}
50+
51+
@Override
52+
public boolean isBlockingInput() {
53+
return false;
54+
}
55+
56+
@Override
57+
protected boolean escapeCloses() {
58+
return false;
59+
}
60+
61+
public void moveToTop() {
62+
isReplaceRemove = true;
63+
nuiManager.removeScreen(this);
64+
isReplaceRemove = false;
65+
nuiManager.pushScreen(this);
66+
}
67+
68+
@Override
69+
public void onRemoved() {
70+
if (isReplaceRemove) {
71+
return;
72+
}
73+
74+
// This screen is always on-top, so when other screens call popScreen,
75+
// we should remove the screen underneath us, since this was likely the intended behaviour.
76+
if (nuiManager.getScreens().size() > 1 &&
77+
!(nuiManager.getTopScreen() instanceof MainGameScreen) &&
78+
!(nuiManager.getTopScreen() instanceof UIShipControlsScreen)) {
79+
nuiManager.popScreen();
80+
}
81+
}
82+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"inherit": "engine:mainGameScreen",
3+
"families": {
4+
"tutorialBox": {
5+
"font": "engine:main#0.8",
6+
"max-width": 512,
7+
"min-height": 128,
8+
"elements": {
9+
"UIBox": {
10+
"background": "engine:background",
11+
"text-align-horizontal": "middle"
12+
}
13+
}
14+
}
15+
}
16+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"type": "TutorialScreen",
3+
"skin": "engine:tutorialScreen",
4+
"contents": {
5+
"type": "RelativeLayout",
6+
"contents": [
7+
{
8+
"type": "UIBox",
9+
"id": "tutorialBox",
10+
"family": "tutorialBox",
11+
"content": {
12+
"type": "UILabel",
13+
"id": "tutorialText",
14+
"text": "Tutorial content goes here..."
15+
},
16+
"layoutInfo": {
17+
"position-left": {
18+
"offset": 128
19+
},
20+
"position-right": {
21+
"offset": 128
22+
},
23+
"position-bottom": {},
24+
"use-content-height": true
25+
}
26+
}
27+
]
28+
}
29+
}

0 commit comments

Comments
 (0)