Skip to content

Commit

Permalink
Merge pull request MovingBlocks#631 from BenjaminAmos/nui-main-menu
Browse files Browse the repository at this point in the history
Adapted Main Menu screens to use NUI
  • Loading branch information
NicholasBatesNZ authored Jan 11, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 30700e9 + a8f97d9 commit 6c6f1b2
Showing 46 changed files with 1,788 additions and 1,286 deletions.
16 changes: 10 additions & 6 deletions engine/src/main/java/org/destinationsol/SolApplication.java
Original file line number Diff line number Diff line change
@@ -165,13 +165,14 @@ public void create() {

musicManager.playMusic(OggMusicManager.MENU_MUSIC_SET, options);

menuBackgroundManager = new MenuBackgroundManager(displayDimensions);
menuScreens = new MenuScreens(layouts, isMobile(), options);

inputManager.setScreen(this, menuScreens.main);
parameterAdapterManager = ParameterAdapterManager.createCore(this);

nuiManager = new NUIManager(this, context, commonDrawer, options);
nuiManager = new NUIManager(this, context, commonDrawer, options, uiDrawer);

menuBackgroundManager = new MenuBackgroundManager(displayDimensions);
menuScreens = new MenuScreens(layouts, isMobile(), options, nuiManager);

nuiManager.pushScreen(menuScreens.main);
}

@Override
@@ -359,6 +360,7 @@ public void play(boolean tut, String shipName, boolean isNewGame, WorldConfig wo
}

factionDisplay = new FactionDisplay(context.get(SolCam.class));
nuiManager.removeScreen(menuScreens.loading);
inputManager.setScreen(this, solGame.getScreens().mainGameScreen);
}

@@ -401,7 +403,9 @@ public SolLayouts getLayouts() {
public void finishGame() {
solGame.onGameEnd(context);
solGame = null;
inputManager.setScreen(this, menuScreens.main);
// TODO: remove the following line when all screens have been ported to use NUI
inputManager.setScreen(this, null);
nuiManager.pushScreen(menuScreens.main);
}

public boolean isMobile() {
Original file line number Diff line number Diff line change
@@ -128,4 +128,8 @@ public void dispose() {
logger.error("Error closing assetTypeManager", e);
}
}

public boolean isAssetLoaded(ResourceUrn urn, Class<? extends Asset<?>> type) {
return assetTypeManager.getAssetManager().isLoaded(urn, type);
}
}
29 changes: 29 additions & 0 deletions engine/src/main/java/org/destinationsol/assets/Assets.java
Original file line number Diff line number Diff line change
@@ -29,8 +29,10 @@
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.gestalt.assets.Asset;
import org.terasology.gestalt.assets.ResourceUrn;
import org.terasology.gestalt.entitysystem.prefab.Prefab;
import org.terasology.nui.asset.UIElement;
import org.terasology.nui.skin.UISkin;
import org.terasology.nui.skin.UISkinAsset;

@@ -267,6 +269,33 @@ public static Animation<TextureAtlas.AtlasRegion> getAnimation(String texturePat
return animation;
}

/**
* Retrieves the specified UIElement asset, if it exists. Otherwise, throws a RuntimeException.
* NOTE: It is the caller's responsibility for initialising the retrieved element, if needed.
* @see org.destinationsol.ui.nui.NUIManager#createScreen
* @param path the asset path, in the {@link ResourceUrn} format.
* @return the retrieved asset
*/
public static UIElement getUIElement(String path) {
Optional<UIElement> optionalUIElement = assetHelper.get(new ResourceUrn(path), UIElement.class);

if (optionalUIElement.isPresent()) {
return optionalUIElement.get();
}

throw new RuntimeException("UIElement " + path + " not found!");
}

/**
* Returns true if the asset specified has already been loaded.
* @param path the asset urn
* @param type the asset type
* @return true, if the asset has been loaded yet, otherwise false
*/
public static boolean isLoaded(String path, Class<? extends Asset<?>> type) {
return assetHelper.isAssetLoaded(new ResourceUrn(path), type);
}

public static void cacheLists() {
textureList = assetHelper.list(DSTexture.class);
}
Original file line number Diff line number Diff line change
@@ -38,4 +38,8 @@ protected void doReload(FontData data) {
public BitmapFont getBitmapFont() {
return fontData.getBitmapFont();
}

public FontData getFontData() {
return fontData;
}
}
Original file line number Diff line number Diff line change
@@ -70,16 +70,15 @@ public Optional<FontData> getAssetData(ResourceUrn urn) throws IOException {
return Optional.empty();
}

// The 1/scale value is due to the rendering scaling performed in UIFont.java, which uses the existing scale.
// Without this, large values would be displayed as smaller and smaller values as larger.
float scale = 1.0f / Float.parseFloat(urn.getFragmentName().toString());
float scale = Float.parseFloat(urn.getFragmentName().toString());
Optional<Font> fontAsset = assetManager.getAsset(urn.getRootUrn(), Font.class);
if (!fontAsset.isPresent()) {
return Optional.empty();
}

BitmapFont bitmapFont = fontAsset.get().getBitmapFont();
BitmapFont.BitmapFontData fontData = new BitmapFont.BitmapFontData(bitmapFont.getData().getFontFile(), bitmapFont.getData().flipped);
// Invert the flip value because the original font may be already flipped.
BitmapFont.BitmapFontData fontData = new BitmapFont.BitmapFontData(bitmapFont.getData().getFontFile(), !bitmapFont.getData().flipped);
fontData.setScale(scale);

return Optional.of(new FontData(new BitmapFont(fontData, bitmapFont.getRegions(), bitmapFont.isFlipped())));
114 changes: 0 additions & 114 deletions engine/src/main/java/org/destinationsol/assets/fonts/UIFont.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -12,7 +12,6 @@
import com.google.gson.JsonSyntaxException;
import com.google.gson.stream.JsonReader;
import org.destinationsol.assets.Assets;
import org.destinationsol.assets.fonts.UIFont;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.gestalt.assets.ResourceUrn;
@@ -210,7 +209,7 @@ public Font deserialize(JsonElement json, Type typeOfT, JsonDeserializationConte
if (!name.contains(":")) {
name = "engine:" + name;
}
return new UIFont(Assets.getFont(name));
return Assets.getFont(name).getFontData();
}
}

4 changes: 2 additions & 2 deletions engine/src/main/java/org/destinationsol/game/SolGame.java
Original file line number Diff line number Diff line change
@@ -164,9 +164,9 @@ public SolGame(String shipName, boolean isTutorial, boolean isNewGame, CommonDra

boolean isMobile = solApplication.isMobile();
if (!isMobile) {
mainGameScreen = (MainGameScreen) Assets.getAssetHelper().get(new ResourceUrn(NUI_MAIN_GAME_SCREEN_DESKTOP_URI), UIElement.class).get().getRootWidget();
mainGameScreen = (MainGameScreen) solApplication.getNuiManager().createScreen(NUI_MAIN_GAME_SCREEN_DESKTOP_URI);
} else {
mainGameScreen = (MainGameScreen) Assets.getAssetHelper().get(new ResourceUrn(NUI_MAIN_GAME_SCREEN_MOBILE_URI), UIElement.class).get().getRootWidget();
mainGameScreen = (MainGameScreen) solApplication.getNuiManager().createScreen(NUI_MAIN_GAME_SCREEN_MOBILE_URI);
}

if (isTutorial) {
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ public class ShowNUIScreenCommandHandler {
@Command(shortDescription = "Displays a NUI screen")
public String showNUIScreen(@Game SolGame game, @CommandParam(value = "screen", suggester = NUIScreenSuggester.class) String screen) {
NUIManager nuiManager = game.getSolApplication().getNuiManager();
nuiManager.pushScreen((NUIScreenLayer) Assets.getAssetHelper().get(new ResourceUrn(screen), UIElement.class).get().getRootWidget());
nuiManager.pushScreen(nuiManager.createScreen(screen));
return "Screen displayed.";
}
}
Original file line number Diff line number Diff line change
@@ -107,9 +107,7 @@ public class MainGameScreen extends SolUiBaseScreen {
switch (gameOptions.controlType) {
case KEYBOARD:
UIShipControlsScreen shipControlsScreen =
(UIShipControlsScreen) Assets.getAssetHelper().get(
new ResourceUrn("engine:uiShipControlsScreen"), UIElement.class).get().getRootWidget();
solApplication.getNuiManager().pushScreen(shipControlsScreen);
(UIShipControlsScreen) solApplication.getNuiManager().createScreen("engine:uiShipControlsScreen");
shipControl = shipControlsScreen;
break;
case MOUSE:
@@ -162,6 +160,17 @@ public class MainGameScreen extends SolUiBaseScreen {
myMoneyExcessTp = new TextPlace(SolColor.WHITE);
}

@Override
public void onAdd(SolApplication solApplication) {
super.onAdd(solApplication);
if (solApplication.getOptions().controlType == GameOptions.ControlType.KEYBOARD) {
UIShipControlsScreen uiControls = (UIShipControlsScreen) shipControl;
if (!solApplication.getNuiManager().hasScreen(uiControls)) {
solApplication.getNuiManager().pushScreen(uiControls);
}
}
}

public static Rectangle btn(float x, float y, boolean halfHeight) {
float gap = .01f;
float cellH = CELL_SZ;
Original file line number Diff line number Diff line change
@@ -248,8 +248,9 @@ public void setEnterNewKey(boolean newKey) {
Gdx.input.setInputProcessor(new InputAdapter() {
@Override
public boolean keyUp(int keycode) {
// Don't capture the escape key
// Don't capture the escape key - cancel the key input instead
if (keycode == Input.Keys.ESCAPE) {
setEnterNewKey(false);
return true;
}

@@ -359,6 +360,11 @@ public List<InputConfigItem> getItems(GameOptions gameOptions) {
return itemsList;
}

@Override
public int getSelectedIndex() {
return selectedIndex;
}

@Override
public void setSelectedIndex(int index) {
selectedIndex = index;
Original file line number Diff line number Diff line change
@@ -223,8 +223,9 @@ public void setEnterNewKey(boolean newKey) {
Gdx.input.setInputProcessor(new InputAdapter() {
@Override
public boolean keyUp(int keycode) {
// Don't capture the escape key
// Don't capture the escape key - cancel the key input instead
if (keycode == Input.Keys.ESCAPE) {
setEnterNewKey(false);
return true;
}

@@ -255,6 +256,11 @@ public List<InputConfigItem> getItems(GameOptions gameOptions) {
return itemsList;
}

@Override
public int getSelectedIndex() {
return selectedIndex;
}

@Override
public void setSelectedIndex(int index) {
selectedIndex = index;
Original file line number Diff line number Diff line change
@@ -195,8 +195,9 @@ public void setEnterNewKey(boolean newKey) {
Gdx.input.setInputProcessor(new InputAdapter() {
@Override
public boolean keyUp(int keycode) {
// Don't capture the escape key
// Don't capture the escape key - cancel the key input instead
if (keycode == Input.Keys.ESCAPE) {
setEnterNewKey(false);
return true;
}

@@ -218,6 +219,11 @@ public List<InputConfigItem> getItems(GameOptions gameOptions) {
return itemsList;
}

@Override
public int getSelectedIndex() {
return selectedIndex;
}

@Override
public void setSelectedIndex(int index) {
selectedIndex = index;
Loading

0 comments on commit 6c6f1b2

Please sign in to comment.