Skip to content

Commit 042f715

Browse files
authored
feat: display ship preview on new ship screen (#671)
1 parent 2b1a796 commit 042f715

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

engine/src/main/java/org/destinationsol/ui/nui/screens/mainMenu/NewShipScreen.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,27 @@
2424
import org.destinationsol.ui.nui.NUIManager;
2525
import org.destinationsol.ui.nui.NUIScreenLayer;
2626
import org.destinationsol.ui.nui.widgets.KeyActivatedButton;
27+
import org.json.JSONObject;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
2730
import org.terasology.gestalt.assets.ResourceUrn;
2831
import org.terasology.nui.Canvas;
32+
import org.terasology.nui.UITextureRegion;
2933
import org.terasology.nui.backends.libgdx.GDXInputUtil;
3034
import org.terasology.nui.widgets.UIButton;
35+
import org.terasology.nui.widgets.UIImage;
3136

3237
import javax.inject.Inject;
3338
import java.util.ArrayList;
3439
import java.util.List;
3540

3641
public class NewShipScreen extends NUIScreenLayer {
37-
42+
private static final Logger logger = LoggerFactory.getLogger(NewShipScreen.class);
3843
private final SolApplication solApplication;
3944
private int numberOfSystems = SystemsBuilder.DEFAULT_SYSTEM_COUNT;
4045
private int playerSpawnConfigIndex = 0;
4146
private List<String> playerSpawnConfigNames = new ArrayList<>();
47+
private List<UITextureRegion> playerSpawnConfigTextures = new ArrayList<>();
4248

4349
@Inject
4450
public NewShipScreen(SolApplication solApplication) {
@@ -59,14 +65,29 @@ public void initialise() {
5965
});
6066

6167
for (ResourceUrn configUrn : Assets.getAssetHelper().listAssets(Json.class, "playerSpawnConfig")) {
62-
playerSpawnConfigNames.addAll(Validator.getValidatedJSON(configUrn.toString(), "engine:schemaPlayerSpawnConfig").keySet());
68+
JSONObject playerSpawnConfigs = Validator.getValidatedJSON(configUrn.toString(), "engine:schemaPlayerSpawnConfig");
69+
playerSpawnConfigNames.addAll(playerSpawnConfigs.keySet());
70+
for (String spawnConfigName : playerSpawnConfigs.keySet()) {
71+
JSONObject playerSpawnConfig = playerSpawnConfigs.getJSONObject(spawnConfigName);
72+
try {
73+
playerSpawnConfigTextures.add(Assets.getDSTexture(playerSpawnConfig.getString("hull")).getUiTexture());
74+
} catch (RuntimeException e) {
75+
logger.error("Failed to load ship texture!", e);
76+
// Null values will not render any texture.
77+
playerSpawnConfigTextures.add(null);
78+
}
79+
}
6380
}
6481

82+
UIImage shipPreviewImage = find("shipPreviewImage", UIImage.class);
83+
shipPreviewImage.setImage(playerSpawnConfigTextures.get(playerSpawnConfigIndex));
84+
6585
UIButton startingShipButton = find("startingShipButton", UIButton.class);
6686
startingShipButton.setText("Starting Ship: " + playerSpawnConfigNames.get(playerSpawnConfigIndex));
6787
startingShipButton.subscribe(button -> {
6888
playerSpawnConfigIndex = (playerSpawnConfigIndex + 1) % playerSpawnConfigNames.size();
6989
((UIButton)button).setText("Starting Ship: " + playerSpawnConfigNames.get(playerSpawnConfigIndex));
90+
shipPreviewImage.setImage(playerSpawnConfigTextures.get(playerSpawnConfigIndex));
7091
});
7192

7293
// NOTE: The original code used getKeyEscape() for both the "OK" and "Cancel" buttons. This was probably a mistake.

engine/src/main/resources/org/destinationsol/assets/ui/mainMenu/newShipScreen.ui

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,29 @@
1111
"layoutInfo": {
1212
"position-horizontal-center": {},
1313
"position-top": {
14-
"target": "MIDDLE",
14+
"widget": "shipPreviewImage",
15+
"target": "TOP",
1516
"offset": -64
17+
},
18+
"position-bottom": {
19+
"widget": "shipPreviewImage",
20+
"target": "TOP"
1621
}
1722
}
1823
},
24+
{
25+
"type": "UIImage",
26+
"id": "shipPreviewImage",
27+
"layoutInfo": {
28+
"position-horizontal-center": {},
29+
"position-top": {
30+
"target": "MIDDLE",
31+
"offset": -80
32+
},
33+
"use-content-width": true,
34+
"use-content-height": true
35+
}
36+
},
1937
{
2038
"type": "ColumnLayout",
2139
"id": "menuButtons",

0 commit comments

Comments
 (0)