Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Integration of NUI (Relates to #455) #536

Merged
merged 21 commits into from
Nov 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
38832e1
Initial import of NUI from Terasology.
BenjaminAmos Feb 8, 2020
1e3add6
Overlays are now correctly initialised with NUI.
BenjaminAmos Feb 9, 2020
2cbd4d9
Removed commented NUI code used for testing.
BenjaminAmos Feb 10, 2020
4b12e0b
Ported Terasology's console UI (NUI) to Destination Sol.
BenjaminAmos Feb 14, 2020
dbf9808
Added a new showNUIScreen command.
BenjaminAmos Feb 18, 2020
7c21af3
Moved UICommandEntry to the org.terasology.nui.widgets package.
BenjaminAmos Mar 20, 2020
a9a8e54
Added a note about some files being borrowed from Terasology.
BenjaminAmos Jun 4, 2020
ed6ee34
Fixed compilation and runtime issues after rebase against current dev…
BenjaminAmos Jun 4, 2020
1931fdb
Fixed NUI on Android and updated to a JOML-based NUI.
BenjaminAmos Jul 11, 2020
d802803
Fixed resize issues and Android compatiblity fixes.
BenjaminAmos Jul 17, 2020
2d32f63
Added support for NUI UI scaling.
BenjaminAmos Jul 17, 2020
d285cd7
Updated NUI to use 2.0.0-SNAPSHOT and added JavaDoc to NUIManager.
BenjaminAmos Jul 20, 2020
2fb8edb
Added JavaDoc for NUIScreenLayer.
BenjaminAmos Jul 20, 2020
7d0f9aa
Added minor JavaDoc for the UI screens.
BenjaminAmos Jul 20, 2020
24c2345
Updated NUI module version in ModuleManager and minor explanatory com…
BenjaminAmos Jul 20, 2020
6f0f9e7
Updated NUI to version 2.0.0.
BenjaminAmos Aug 15, 2020
fa519b4
Removed dependency on TeraMath.
BenjaminAmos Aug 16, 2020
f362af5
Changed "Map Pan Speed" text to "NUI UI scale" to accurately describe…
BenjaminAmos Aug 16, 2020
d13df9a
Changed UI scale to increase visible size with larger values.
BenjaminAmos Aug 16, 2020
1dafc8d
Removed excess menuControl.update calls from MainGameScreen.
BenjaminAmos Aug 16, 2020
9484f2f
Merge branch 'develop' into nui-intergration
NicholasBatesNZ Aug 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions engine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,29 @@ import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.ClasspathHelper;

// Dependencies needed for what our Gradle scripts themselves use. It cannot be included via an external Gradle file :-(
buildscript {
repositories {
// External libs - jcenter is Bintray and is supposed to be a superset of Maven Central, but do both just in case
jcenter()
mavenCentral()

// HACK: Needed for NUI reflections
mavenLocal()
google()
maven { url "http://artifactory.terasology.org/artifactory/virtual-repo-live" }
}

dependencies {
// Needed for caching reflected data during builds
classpath 'org.reflections:reflections:0.9.10'
classpath 'dom4j:dom4j:1.6.1'

// HACK: Needed for NUI reflections
classpath group: 'org.terasology.nui', name: 'nui', version: '2.0.0-SNAPSHOT'
classpath group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
}
}

Expand All @@ -31,6 +41,8 @@ dependencies {
compile(group: 'org.terasology.gestalt', name: 'gestalt-entity-system', version: '7.0.6-SNAPSHOT')
compile(group: 'org.terasology.gestalt', name: 'gestalt-module', version: '7.0.6-SNAPSHOT')
compile(group: 'org.terasology.gestalt', name: 'gestalt-util', version: '7.0.6-SNAPSHOT')
compile group: 'org.terasology.nui', name: 'nui', version: '2.0.0'
compile group: 'org.terasology.nui', name: 'nui-libgdx', version: '2.0.0'

compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.4.0'

Expand Down Expand Up @@ -64,6 +76,7 @@ task cacheReflections {
dirToReflect.mkdirs()
Reflections reflections = new Reflections(new ConfigurationBuilder()
.addUrls(dirToReflect.toURI().toURL())
.addUrls(ClasspathHelper.forPackage("org.terasology.nui.widgets"))
.setScanners(new TypeAnnotationsScanner(), new SubTypesScanner()))
reflections.save(sourceSets.main.output.classesDir.toString() + "/reflections.cache")
}
Expand Down
20 changes: 19 additions & 1 deletion engine/src/main/java/org/destinationsol/GameOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public Volume advance() {
public static final int DEFAULT_BUTTON_RIGHT = -1;
public static final int DEFAULT_MAP_SCROLL_SPEED = 10;
public static final int DEFAULT_MOBILE_MAP_SCROLL_SPEED = 5;
private static final float DEFAULT_NUI_UI_SCALE = 1.0f;

public int x;
public int y;
Expand Down Expand Up @@ -177,6 +178,7 @@ public Volume advance() {
private int controllerButtonUp;
private int controllerButtonDown;
private int mapScrollSpeed;
public float nuiUiScale;

private ResolutionProvider resolutionProvider;

Expand Down Expand Up @@ -226,6 +228,7 @@ public GameOptions(boolean mobile, SolFileReader solFileReader) {
controllerButtonDown = reader.getInt("controllerButtonDown", DEFAULT_BUTTON_DOWN);
canSellEquippedItems = reader.getBoolean("canSellEquippedItems", false);
mapScrollSpeed = reader.getInt("mapScrollSpeed", mobile ? DEFAULT_MOBILE_MAP_SCROLL_SPEED : DEFAULT_MAP_SCROLL_SPEED);
nuiUiScale = reader.getFloat("nuiUiScale", DEFAULT_NUI_UI_SCALE);
}

public void advanceResolution() {
Expand Down Expand Up @@ -268,6 +271,13 @@ public void advanceMapScrollSpeed() {
}
}

public void advanceNuiUiScale() {
nuiUiScale += 0.25f;
if (nuiUiScale > 2.0f) {
nuiUiScale = 0.25f;
}
}

/**
* Save the configuration settings to file.
*/
Expand All @@ -286,7 +296,7 @@ public void save() {
"controllerButtonShoot2", getControllerButtonShoot2(), "controllerButtonAbility", getControllerButtonAbility(),
"controllerButtonLeft", getControllerButtonLeft(), "controllerButtonRight", getControllerButtonRight(),
"controllerButtonUp", getControllerButtonUp(), "controllerButtonDown", getControllerButtonDown(),
"mapScrollSpeed", getMapScrollSpeed());
"mapScrollSpeed", getMapScrollSpeed(), "nuiUiScale", getNuiUiScale());
}

/**
Expand Down Expand Up @@ -1005,4 +1015,12 @@ public int getMapScrollSpeed() {
public void setMapScrollSpeed(int mapScrollSpeed) {
this.mapScrollSpeed = mapScrollSpeed;
}

public float getNuiUiScale() {
return nuiUiScale;
}

public void setNuiUiScale(float nuiUiScale) {
this.nuiUiScale = nuiUiScale;
}
}
11 changes: 11 additions & 0 deletions engine/src/main/java/org/destinationsol/SolApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.destinationsol.ui.SolInputManager;
import org.destinationsol.ui.SolLayouts;
import org.destinationsol.ui.UiDrawer;
import org.destinationsol.ui.nui.NUIManager;
import org.destinationsol.util.FramerateLimiter;
import org.destinationsol.util.InjectionHelper;
import org.slf4j.Logger;
Expand Down Expand Up @@ -106,6 +107,7 @@ public class SolApplication implements ApplicationListener {
private String fatalErrorTrace;
private SolGame solGame;
private ParameterAdapterManager parameterAdapterManager;
private NUIManager nuiManager;
private Context context;
// TODO: Make this non-static.
public static DisplayDimensions displayDimensions;
Expand Down Expand Up @@ -162,6 +164,8 @@ public void create() {

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

nuiManager = new NUIManager(this, commonDrawer, options);
}

@Override
Expand Down Expand Up @@ -236,6 +240,7 @@ private void update() {
}

inputManager.update(this);
nuiManager.update(this);

if (solGame != null) {
solGame.update();
Expand Down Expand Up @@ -288,6 +293,8 @@ private void draw() {
}
uiDrawer.updateMtx();
inputManager.draw(uiDrawer, this);
nuiManager.draw(commonDrawer);
inputManager.drawCursor(uiDrawer);

if (solGame != null) {
solGame.drawDebugUi(uiDrawer);
Expand Down Expand Up @@ -400,6 +407,10 @@ public MenuBackgroundManager getMenuBackgroundManager() {
return menuBackgroundManager;
}

public NUIManager getNuiManager() {
return nuiManager;
}

// TODO: Make this non-static.
public static void addResizeSubscriber(ResizeSubscriber resizeSubscriber) {
resizeSubscribers.add(resizeSubscriber);
Expand Down
30 changes: 27 additions & 3 deletions engine/src/main/java/org/destinationsol/assets/AssetHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
package org.destinationsol.assets;

import org.destinationsol.assets.music.AndroidOggMusicFileFormat;
import org.destinationsol.assets.music.OggMusic;
import org.destinationsol.assets.music.OggMusicData;
import org.destinationsol.assets.sound.AndroidOggSoundFileFormat;
import org.destinationsol.assets.sound.OggSound;
import org.destinationsol.assets.sound.OggSoundData;
import org.destinationsol.assets.music.OggMusicData;
import org.destinationsol.assets.music.OggMusic;
import org.destinationsol.assets.sound.OggSound;
import org.destinationsol.assets.ui.UIFormat;
import org.destinationsol.assets.ui.UISkinFormat;
import org.terasology.gestalt.assets.Asset;
import org.terasology.gestalt.assets.AssetData;
import org.terasology.gestalt.assets.AssetType;
import org.terasology.gestalt.assets.ResourceUrn;
import org.terasology.gestalt.assets.format.producer.AssetFileDataProducer;
import org.terasology.gestalt.assets.module.ModuleAwareAssetTypeManager;
import org.terasology.gestalt.assets.module.ModuleAwareAssetTypeManagerImpl;
import org.terasology.gestalt.assets.module.ModuleDependencyResolutionStrategy;
Expand All @@ -35,6 +38,13 @@
import org.terasology.gestalt.entitysystem.prefab.PrefabJsonFormat;
import org.terasology.gestalt.module.ModuleEnvironment;
import org.terasology.gestalt.naming.Name;
import org.terasology.nui.UIWidget;
import org.terasology.nui.asset.UIElement;
import org.terasology.nui.reflection.WidgetLibrary;
import org.terasology.nui.skin.UISkin;
import org.terasology.reflection.copy.CopyStrategyLibrary;
import org.terasology.reflection.reflect.ReflectFactory;
import org.terasology.reflection.reflect.ReflectionReflectFactory;

import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -63,6 +73,20 @@ public void init(ModuleEnvironment environment, ComponentManager componentManage
new ModuleEnvironmentDependencyProvider(environment))),
componentManager, assetTypeManager.getAssetManager()).create());

// The NUI widgets are loaded here so that they can be found when reading the UI JSON files (in UIFormat.UIWidgetTypeAdapter)
ReflectFactory reflectFactory = new ReflectionReflectFactory();
WidgetLibrary widgetLibrary = new WidgetLibrary(environment, reflectFactory, new CopyStrategyLibrary(reflectFactory));
for (Class<? extends UIWidget> widgetClass : environment.getSubtypesOf(UIWidget.class)) {
Name moduleName = environment.getModuleProviding(widgetClass);
widgetLibrary.register(new ResourceUrn(moduleName, new Name(widgetClass.getSimpleName())), widgetClass);
}

assetTypeManager.createAssetType(UISkin.class, UISkin::new, "skins");
((AssetFileDataProducer)assetTypeManager.getAssetType(UISkin.class).get().getProducers().get(0)).addAssetFormat(new UISkinFormat(widgetLibrary));

assetTypeManager.createAssetType(UIElement.class, UIElement::new, "ui");
((AssetFileDataProducer)assetTypeManager.getAssetType(UIElement.class).get().getProducers().get(0)).addAssetFormat(new UIFormat(widgetLibrary));

assetTypeManager.switchEnvironment(environment);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@

import com.badlogic.gdx.graphics.g2d.BitmapFont;
import org.terasology.gestalt.assets.AssetData;
import org.terasology.nui.backends.libgdx.LibGDXFont;

public class FontData implements AssetData {
public class FontData extends LibGDXFont implements AssetData {
private BitmapFont bitmapFont;

public FontData(BitmapFont bitmapFont) {
super(bitmapFont);
this.bitmapFont = bitmapFont;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/
package org.destinationsol.assets.fonts;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.Array;
import org.destinationsol.assets.AssetDataFileHandle;
import org.destinationsol.assets.Assets;
import org.terasology.gestalt.assets.ResourceUrn;
Expand All @@ -38,12 +40,17 @@ public FontData load(ResourceUrn urn, List<AssetDataFile> inputs) throws IOExcep
AssetDataFileHandle fontDataHandle = new AssetDataFileHandle(inputs.get(0));
BitmapFont.BitmapFontData fontData = new BitmapFont.BitmapFontData(fontDataHandle, true);

String[] fontTexturePath = fontData.imagePaths[0].split("/");
String fontTextureName = fontTexturePath[fontTexturePath.length - 1];
fontTextureName = fontTextureName.substring(0, fontTextureName.lastIndexOf('.'));
TextureRegion fontTexture = new TextureRegion(Assets.getDSTexture(urn.getModuleName() + ":" + fontTextureName).getTexture());
TextureRegion[] fontTextures = new TextureRegion[fontData.imagePaths.length];
for (int textureNo = 0; textureNo < fontData.imagePaths.length; textureNo++){
String[] pathSegments = fontData.imagePaths[textureNo].split("/");
String fontTextureName = pathSegments[pathSegments.length - 1];
fontTextureName = fontTextureName.substring(0, fontTextureName.lastIndexOf('.'));
Texture texture = Assets.getDSTexture(urn.getModuleName() + ":" + fontTextureName).getTexture();
TextureRegion fontTexture = new TextureRegion(texture);
fontTextures[textureNo] = fontTexture;
}

BitmapFont bitmapFont = new BitmapFont(fontData, fontTexture, false);
BitmapFont bitmapFont = new BitmapFont(fontData, Array.with(fontTextures), false);
bitmapFont.setUseIntegerPositions(false);
return new FontData(bitmapFont);
}
Expand Down
114 changes: 114 additions & 0 deletions engine/src/main/java/org/destinationsol/assets/fonts/UIFont.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2020 The Terasology Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.destinationsol.assets.fonts;

import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import org.joml.Vector2i;
import org.terasology.nui.backends.libgdx.LibGDXFont;

import java.util.List;

/**
* This class wraps a {@link Font} asset in order to adapt it to NUI's LibGDX back-end. It handles font scaling,
* so that NUI renders correctly.
*/
public class UIFont extends LibGDXFont {
private float fontScale;
private float previousFontScaleX;
private float previousFontScaleY;
private static final float FONT_SCALE = 8.0f;

public UIFont(Font font) {
super(font.getBitmapFont());

fontScale = FONT_SCALE / super.getGdxFont().getData().xHeight;
}

private void scale() {
scale(fontScale, fontScale);
}

private void scale(float scaleX, float scaleY) {
previousFontScaleX = super.getGdxFont().getScaleX();
previousFontScaleY = super.getGdxFont().getScaleY();
super.getGdxFont().getData().setScale(scaleX, scaleY);
}

private void reset() {
super.getGdxFont().getData().setScale(previousFontScaleX, previousFontScaleY);
}

@Override
public Vector2i getSize(List<String> lines) {
scale();
Vector2i result = super.getSize(lines);
reset();
return result;
}

@Override
public int getWidth(Character c) {
scale();
int result = super.getWidth(c);
reset();
return result;
}

@Override
public int getLineHeight() {
scale();
int result = super.getLineHeight();
reset();
return result;
}

@Override
public int getHeight(String text) {
scale();
int result = super.getHeight(text);
reset();
return result;
}

@Override
public int getBaseHeight() {
scale();
int result = super.getBaseHeight();
reset();
return result;
}

@Override
public int getWidth(String text) {
scale();
int result = super.getWidth(text);
reset();
return result;
}

@Override
public BitmapFont getGdxFont() {
super.getGdxFont().getData().setScale(fontScale, -fontScale);
return super.getGdxFont();
}

@Override
public GlyphLayout getGlyphLayout() {
super.getGdxFont().getData().setScale(fontScale, -fontScale);
return super.getGlyphLayout();
}
}
Loading