Skip to content

Commit 83a7c90

Browse files
committed
feat(nui): convert InventoryScreen and InventoryOperationsScreen classes to NUI
1 parent 106b0e4 commit 83a7c90

19 files changed

+1301
-710
lines changed

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

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,49 @@
1616

1717
package org.destinationsol.game.screens;
1818

19-
import org.destinationsol.GameOptions;
2019
import org.destinationsol.SolApplication;
2120
import org.destinationsol.game.FactionInfo;
2221
import org.destinationsol.game.Hero;
2322
import org.destinationsol.game.SolGame;
2423
import org.destinationsol.game.item.ItemContainer;
2524
import org.destinationsol.game.item.SolItem;
2625
import org.destinationsol.game.ship.SolShip;
27-
import org.destinationsol.ui.SolInputManager;
28-
import org.destinationsol.ui.SolUiControl;
26+
import org.destinationsol.ui.nui.screens.InventoryScreen;
2927
import org.destinationsol.ui.nui.screens.TalkScreen;
28+
import org.destinationsol.ui.nui.widgets.UIWarnButton;
29+
import org.terasology.nui.backends.libgdx.GDXInputUtil;
30+
import org.terasology.nui.widgets.UIButton;
3031

32+
/**
33+
* This screen allows you to purchase items for the hero's ship.
34+
* The purchased items are moved to the hero's inventory and the cost deducted from the hero's money.
35+
*/
3136
public class BuyItemsScreen extends InventoryOperationsScreen {
32-
public final SolUiControl buyControl;
37+
private final UIButton[] actionButtons = new UIButton[1];
38+
39+
@Override
40+
public void initialise(SolApplication solApplication, InventoryScreen inventoryScreen) {
41+
UIWarnButton buyButton = new UIWarnButton();
42+
buyButton.setText("Buy");
43+
buyButton.setKey(GDXInputUtil.GDXToNuiKey(solApplication.getOptions().getKeyBuyItem()));
44+
buyButton.subscribe(button -> {
45+
SolGame game = solApplication.getGame();
46+
Hero hero = game.getHero();
47+
TalkScreen talkScreen = game.getScreens().talkScreen;
48+
SolShip target = talkScreen.getTarget();
49+
SolItem selectedItem = inventoryScreen.getSelectedItem();
50+
if (selectedItem == null) {
51+
return;
52+
}
3353

34-
BuyItemsScreen(InventoryScreen inventoryScreen, GameOptions gameOptions) {
35-
buyControl = new SolUiControl(inventoryScreen.itemCtrl(0), true, gameOptions.getKeyBuyItem());
36-
buyControl.setDisplayName("Buy");
37-
controls.add(buyControl);
54+
target.getTradeContainer().getItems().remove(selectedItem);
55+
hero.getItemContainer().add(selectedItem);
56+
hero.setMoney(hero.getMoney() - selectedItem.getPrice());
57+
FactionInfo.setDisposition(target.getFactionID(), 1);
58+
59+
inventoryScreen.updateItemRows();
60+
});
61+
actionButtons[0] = buyButton;
3862
}
3963

4064
@Override
@@ -48,28 +72,27 @@ public String getHeader() {
4872
}
4973

5074
@Override
51-
public void updateCustom(SolApplication solApplication, SolInputManager.InputPointer[] inputPointers, boolean clickedOutside) {
75+
public UIButton[] getActionButtons() {
76+
return actionButtons;
77+
}
78+
79+
public UIWarnButton getBuyControl() {
80+
return (UIWarnButton) actionButtons[0];
81+
}
82+
83+
@Override
84+
public void update(SolApplication solApplication, InventoryScreen inventoryScreen) {
5285
SolGame game = solApplication.getGame();
53-
InventoryScreen is = game.getScreens().inventoryScreen;
5486
Hero hero = game.getHero();
5587
TalkScreen talkScreen = game.getScreens().talkScreen;
56-
SolShip target = talkScreen.getTarget();
5788
if (talkScreen.isTargetFar(hero)) {
58-
solApplication.getInputManager().setScreen(solApplication, game.getScreens().oldMainGameScreen);
89+
solApplication.getNuiManager().removeScreen(inventoryScreen);
5990
return;
6091
}
61-
SolItem selItem = is.getSelectedItem();
92+
SolItem selItem = inventoryScreen.getSelectedItem();
6293
boolean enabled = selItem != null && hero.getMoney() >= selItem.getPrice() && hero.getItemContainer().canAdd(selItem);
63-
buyControl.setDisplayName(enabled ? "Buy" : "---");
64-
buyControl.setEnabled(enabled);
65-
if (!enabled) {
66-
return;
67-
}
68-
if (buyControl.isJustOff()) {
69-
target.getTradeContainer().getItems().remove(selItem);
70-
hero.getItemContainer().add(selItem);
71-
hero.setMoney(hero.getMoney() - selItem.getPrice());
72-
FactionInfo.setDisposition(target.getFactionID(), 1);
73-
}
94+
UIButton buyButton = actionButtons[0];
95+
buyButton.setText(enabled ? "Buy" : "---");
96+
buyButton.setEnabled(enabled);
7497
}
7598
}

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

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.destinationsol.game.screens;
1717

1818
import com.badlogic.gdx.math.Vector2;
19-
import org.destinationsol.GameOptions;
2019
import org.destinationsol.SolApplication;
2120
import org.destinationsol.game.Hero;
2221
import org.destinationsol.game.SolGame;
@@ -29,17 +28,33 @@
2928
import org.destinationsol.game.ship.SolShip;
3029
import org.destinationsol.game.ship.hulls.Hull;
3130
import org.destinationsol.game.ship.hulls.HullConfig;
32-
import org.destinationsol.ui.SolInputManager;
33-
import org.destinationsol.ui.SolUiControl;
31+
import org.destinationsol.ui.nui.screens.InventoryScreen;
3432
import org.destinationsol.ui.nui.screens.TalkScreen;
33+
import org.destinationsol.ui.nui.widgets.KeyActivatedButton;
34+
import org.terasology.nui.backends.libgdx.GDXInputUtil;
35+
import org.terasology.nui.widgets.UIButton;
3536

37+
/**
38+
* This screen allows you to purchase a new ship for the hero.
39+
* The hero's previous ship will be replaced with the purchased ship and the cost deducted from the hero's money.
40+
*/
3641
public class ChangeShipScreen extends InventoryOperationsScreen {
37-
private final SolUiControl changeControl;
42+
private final UIButton[] actionButtons = new UIButton[1];
43+
44+
@Override
45+
public void initialise(SolApplication solApplication, InventoryScreen inventoryScreen) {
46+
KeyActivatedButton changeButton = new KeyActivatedButton();
47+
changeButton.setText("Change");
48+
changeButton.setKey(GDXInputUtil.GDXToNuiKey(solApplication.getOptions().getKeyChangeShip()));
49+
changeButton.subscribe(button -> {
50+
SolGame game = solApplication.getGame();
51+
Hero hero = game.getHero();
52+
SolItem selectedItem = inventoryScreen.getSelectedItem();
3853

39-
ChangeShipScreen(InventoryScreen inventoryScreen, GameOptions gameOptions) {
40-
changeControl = new SolUiControl(inventoryScreen.itemCtrl(0), true, gameOptions.getKeyChangeShip());
41-
changeControl.setDisplayName("Change");
42-
controls.add(changeControl);
54+
hero.setMoney(hero.getMoney() - selectedItem.getPrice());
55+
changeShip(game, hero, (ShipItem) selectedItem);
56+
});
57+
actionButtons[0] = changeButton;
4358
}
4459

4560
@Override
@@ -53,38 +68,39 @@ public String getHeader() {
5368
}
5469

5570
@Override
56-
public void updateCustom(SolApplication solApplication, SolInputManager.InputPointer[] inputPointers, boolean clickedOutside) {
71+
public UIButton[] getActionButtons() {
72+
return actionButtons;
73+
}
74+
75+
@Override
76+
public void update(SolApplication solApplication, InventoryScreen inventoryScreen) {
5777
SolGame game = solApplication.getGame();
58-
InventoryScreen is = game.getScreens().inventoryScreen;
5978
Hero hero = game.getHero();
6079
TalkScreen talkScreen = game.getScreens().talkScreen;
6180
if (talkScreen.isTargetFar(hero)) {
6281
solApplication.getInputManager().setScreen(solApplication, game.getScreens().oldMainGameScreen);
6382
return;
6483
}
65-
SolItem selItem = is.getSelectedItem();
84+
85+
UIButton changeButton = actionButtons[0];
86+
87+
SolItem selItem = inventoryScreen.getSelectedItem();
6688
if (selItem == null) {
67-
changeControl.setDisplayName("---");
68-
changeControl.setEnabled(false);
89+
changeButton.setText("---");
90+
changeButton.setEnabled(false);
6991
return;
7092
}
7193
boolean enabled = hasMoneyToBuyShip(hero, selItem);
7294
boolean sameShip = isSameShip(hero, selItem);
7395
if (enabled && !sameShip) {
74-
changeControl.setDisplayName("Change");
75-
changeControl.setEnabled(true);
96+
changeButton.setText("Change");
97+
changeButton.setEnabled(true);
7698
} else if (enabled && sameShip) {
77-
changeControl.setDisplayName("Have it");
78-
changeControl.setEnabled(false);
79-
return;
99+
changeButton.setText("Have it");
100+
changeButton.setEnabled(false);
80101
} else {
81-
changeControl.setDisplayName("---");
82-
changeControl.setEnabled(false);
83-
return;
84-
}
85-
if (changeControl.isJustOff()) {
86-
hero.setMoney(hero.getMoney() - selItem.getPrice());
87-
changeShip(game, hero, (ShipItem) selItem);
102+
changeButton.setText("---");
103+
changeButton.setEnabled(false);
88104
}
89105
}
90106

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

Lines changed: 76 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,68 +15,95 @@
1515
*/
1616
package org.destinationsol.game.screens;
1717

18-
import org.destinationsol.GameOptions;
1918
import org.destinationsol.SolApplication;
2019
import org.destinationsol.game.SolGame;
2120
import org.destinationsol.game.item.ItemContainer;
2221
import org.destinationsol.game.item.MercItem;
2322
import org.destinationsol.game.item.SolItem;
2423
import org.destinationsol.game.ship.SolShip;
2524
import org.destinationsol.ui.SolInputManager;
26-
import org.destinationsol.ui.SolUiControl;
25+
import org.destinationsol.ui.nui.NUIManager;
26+
import org.destinationsol.ui.nui.screens.InventoryScreen;
27+
import org.destinationsol.ui.nui.widgets.KeyActivatedButton;
28+
import org.terasology.nui.backends.libgdx.GDXInputUtil;
29+
import org.terasology.nui.widgets.UIButton;
2730

31+
/**
32+
* This screen shows an overview of all the mercenaries that the hero has hired.
33+
* You can manage each mercenary's independent inventory from here, as well as selecting their equipped items.
34+
*/
2835
public class ChooseMercenaryScreen extends InventoryOperationsScreen {
29-
private final SolUiControl giveControl;
30-
private final SolUiControl takeControl;
31-
private final SolUiControl equipControl;
36+
private final UIButton[] actionButtons = new UIButton[3];
3237
private final ItemContainer EMPTY_ITEM_CONTAINER = new ItemContainer();
3338

34-
ChooseMercenaryScreen(InventoryScreen inventoryScreen, GameOptions gameOptions) {
35-
giveControl = new SolUiControl(inventoryScreen.itemCtrl(0), true, gameOptions.getKeyShoot());
36-
giveControl.setDisplayName("Give Items");
37-
controls.add(giveControl);
39+
public ChooseMercenaryScreen() {
40+
}
41+
42+
@Override
43+
public void initialise(SolApplication solApplication, InventoryScreen inventoryScreen) {
44+
KeyActivatedButton giveButton = new KeyActivatedButton();
45+
giveButton.setText("Give Items");
46+
giveButton.setKey(GDXInputUtil.GDXToNuiKey(solApplication.getOptions().getKeyShoot()));
47+
giveButton.subscribe(button -> {
48+
SolItem selectedItem = inventoryScreen.getSelectedItem();
49+
SolInputManager inputManager = solApplication.getInputManager();
50+
NUIManager nuiManager = solApplication.getNuiManager();
51+
52+
SolShip solship = ((MercItem) selectedItem).getSolShip();
53+
inputManager.setScreen(solApplication, solApplication.getGame().getScreens().oldMainGameScreen);
54+
nuiManager.removeScreen(inventoryScreen);
55+
inventoryScreen.getGiveItems().setTarget(solship);
56+
inventoryScreen.setOperations(inventoryScreen.getGiveItems());
57+
nuiManager.pushScreen(inventoryScreen);
58+
});
59+
actionButtons[0] = giveButton;
60+
61+
KeyActivatedButton takeButton = new KeyActivatedButton();
62+
takeButton.setText("Take Items");
63+
takeButton.setKey(GDXInputUtil.GDXToNuiKey(solApplication.getOptions().getKeyShoot2()));
64+
takeButton.subscribe(button -> {
65+
SolItem selectedItem = inventoryScreen.getSelectedItem();
66+
SolInputManager inputManager = solApplication.getInputManager();
67+
NUIManager nuiManager = solApplication.getNuiManager();
3868

39-
takeControl = new SolUiControl(inventoryScreen.itemCtrl(1), true, gameOptions.getKeyShoot2());
40-
takeControl.setDisplayName("Take Items");
41-
controls.add(takeControl);
42-
43-
equipControl = new SolUiControl(inventoryScreen.itemCtrl(2), true, gameOptions.getKeyDrop());
44-
equipControl.setDisplayName("Equip Items");
45-
controls.add(equipControl);
69+
SolShip solship = ((MercItem) selectedItem).getSolShip();
70+
inputManager.setScreen(solApplication, solApplication.getGame().getScreens().oldMainGameScreen);
71+
inventoryScreen.getTakeItems().setTarget(solship);
72+
nuiManager.removeScreen(inventoryScreen);
73+
inventoryScreen.setOperations(inventoryScreen.getTakeItems());
74+
nuiManager.pushScreen(inventoryScreen);
75+
});
76+
actionButtons[1] = takeButton;
77+
78+
KeyActivatedButton equipButton = new KeyActivatedButton();
79+
equipButton.setText("Equip Items");
80+
equipButton.setKey(GDXInputUtil.GDXToNuiKey(solApplication.getOptions().getKeyDrop()));
81+
equipButton.subscribe(button -> {
82+
SolItem selectedItem = inventoryScreen.getSelectedItem();
83+
SolInputManager inputManager = solApplication.getInputManager();
84+
NUIManager nuiManager = solApplication.getNuiManager();
85+
86+
SolShip solship = ((MercItem) selectedItem).getSolShip();
87+
inputManager.setScreen(solApplication, solApplication.getGame().getScreens().oldMainGameScreen);
88+
nuiManager.removeScreen(inventoryScreen);
89+
inventoryScreen.getShowInventory().setTarget(solship);
90+
inventoryScreen.setOperations(inventoryScreen.getShowInventory());
91+
nuiManager.pushScreen(inventoryScreen);
92+
});
93+
actionButtons[2] = equipButton;
4694
}
4795

4896
@Override
49-
public void updateCustom(SolApplication solApplication, SolInputManager.InputPointer[] inputPointers, boolean clickedOutside) {
50-
SolGame game = solApplication.getGame();
51-
InventoryScreen is = game.getScreens().inventoryScreen;
52-
SolInputManager inputMan = solApplication.getInputManager();
53-
GameScreens screens = game.getScreens();
54-
SolItem selItem = is.getSelectedItem();
55-
boolean selNull = selItem != null;
97+
public void update(SolApplication solApplication, InventoryScreen inventoryScreen) {
98+
boolean selNull = inventoryScreen.getSelectedItem() != null;
5699

57-
giveControl.setEnabled(selNull);
58-
takeControl.setEnabled(selNull);
59-
equipControl.setEnabled(selNull);
100+
UIButton giveButton = actionButtons[0];
101+
UIButton takeButton = actionButtons[1];
102+
UIButton equipButton = actionButtons[2];
60103

61-
if (giveControl.isJustOff() && selNull) {
62-
SolShip solship = ((MercItem) selItem).getSolShip();
63-
inputMan.setScreen(solApplication, screens.oldMainGameScreen);
64-
is.giveItemsScreen.setTarget(solship);
65-
is.setOperations(is.giveItemsScreen);
66-
inputMan.addScreen(solApplication, is);
67-
} else if (takeControl.isJustOff() && selNull) {
68-
SolShip solship = ((MercItem) selItem).getSolShip();
69-
inputMan.setScreen(solApplication, screens.oldMainGameScreen);
70-
is.takeItems.setTarget(solship);
71-
is.setOperations(is.takeItems);
72-
inputMan.addScreen(solApplication, is);
73-
} else if (equipControl.isJustOff() && selNull) {
74-
SolShip solship = ((MercItem) selItem).getSolShip();
75-
inputMan.setScreen(solApplication, screens.oldMainGameScreen);
76-
is.showInventory.setTarget(solship);
77-
is.setOperations(is.showInventory);
78-
inputMan.addScreen(solApplication, is);
79-
}
104+
giveButton.setEnabled(selNull);
105+
takeButton.setEnabled(selNull);
106+
equipButton.setEnabled(selNull);
80107
}
81108

82109
@Override
@@ -95,4 +122,8 @@ public String getHeader() {
95122
return "Mercenaries:";
96123
}
97124

125+
@Override
126+
public UIButton[] getActionButtons() {
127+
return actionButtons;
128+
}
98129
}

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.InventoryScreen;
2223
import org.destinationsol.ui.nui.screens.TalkScreen;
2324

2425
import javax.inject.Inject;
@@ -48,7 +49,7 @@ public GameScreens(SolApplication cmp, Context context) {
4849
}
4950
mapScreen = new MapScreen(rightPaneLayout, cmp.isMobile(), cmp.getOptions());
5051
menuScreen = new MenuScreen(layouts.menuLayout, cmp.getOptions());
51-
inventoryScreen = new InventoryScreen(cmp.getOptions());
52+
inventoryScreen = (InventoryScreen) cmp.getNuiManager().createScreen("engine:inventoryScreen");
5253
talkScreen = (TalkScreen) cmp.getNuiManager().createScreen("engine:talkScreen");
5354
waypointCreationScreen = new WaypointCreationScreen(layouts.menuLayout, cmp.getOptions(), mapScreen);
5455
consoleScreen = new ConsoleScreen(context.get(Console.class));

0 commit comments

Comments
 (0)