Created
April 2, 2020 16:59
-
-
Save peanutbother/9564c6f9303e3e62c769fb9fc823e4eb to your computer and use it in GitHub Desktop.
UiContainer with xml asset loading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mygame.util; | |
import org.mini2Dx.core.Graphics; | |
import org.mini2Dx.core.assets.AssetManager; | |
import org.mini2Dx.core.files.*; | |
import org.mini2Dx.core.game.BasicGame; | |
import org.mini2Dx.core.graphics.viewport.FitViewport; | |
import org.mini2Dx.core.graphics.viewport.Viewport; | |
import org.mini2Dx.ui.UiThemeLoader; | |
import org.mini2Dx.ui.element.Container; | |
import org.mini2Dx.ui.style.UiTheme; | |
public class GameContainer extends BasicGame { | |
private static String DEFAULT_UI_FILENAME = "ui/default.xml"; | |
private AssetManager assetManager; | |
private UiContainer uiContainer; | |
public GameContainer() { | |
this(DEFAULT_UI_FILENAME); | |
} | |
public GameContainer(String uiFile) { | |
super(); | |
DEFAULT_UI_FILENAME = uiFile; | |
} | |
@Override | |
public void initialise() { | |
//Create fallback file resolver so we can use the default mini2Dx-ui theme | |
FileHandleResolver fileHandleResolver = new FallbackFileHandleResolver( | |
new InternalFileHandleResolver(), | |
new LocalFileHandleResolver(), | |
new ExternalFileHandleResolver() | |
); | |
Viewport viewport = new FitViewport(true, getWidth(), getHeight()); | |
//Create asset manager for loading resources | |
assetManager = new AssetManager(fileHandleResolver); | |
//Add mini2Dx-ui theme loader | |
assetManager.setAssetLoader(UiTheme.class, new UiThemeLoader(fileHandleResolver)); | |
assetManager.setAssetLoader(Container.class, new UiContainerLoader(fileHandleResolver)); | |
//Load default theme | |
assetManager.load(UiTheme.DEFAULT_THEME_FILENAME, UiTheme.class); | |
assetManager.load(DEFAULT_UI_FILENAME, Container.class); | |
uiContainer = new UiContainer(this, assetManager); | |
uiContainer.setViewport(viewport); | |
} | |
@Override | |
public void update(float delta) { | |
if (!assetManager.update()) { | |
//Wait for asset manager to finish loading assets | |
return; | |
} | |
if (!UiContainer.isThemeApplied()) { | |
UiContainer.setTheme(assetManager.get(UiTheme.DEFAULT_THEME_FILENAME, UiTheme.class)); | |
} | |
if (!UiContainer.isUiApplied()) { | |
Container ui = assetManager.get(DEFAULT_UI_FILENAME, Container.class); | |
ui.setWidthToWidthOf(uiContainer); | |
ui.setHeightToHeightOf(uiContainer); | |
UiContainer.setUi(ui); | |
} | |
uiContainer.update(delta); | |
} | |
@Override | |
public void interpolate(float alpha) { | |
// TODO check interpolation of uiContainer? | |
// uiContainer.interpolate(alpha); | |
} | |
@Override | |
public void render(Graphics g) { | |
uiContainer.render(g); | |
} | |
@Override | |
public void resize(int width, int height) { | |
super.resize(width, height); | |
uiContainer.resize(width, height); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mygame.util; | |
import org.mini2Dx.core.Graphics; | |
import org.mini2Dx.core.Mdx; | |
import org.mini2Dx.core.assets.AssetManager; | |
import org.mini2Dx.core.game.GameContainer; | |
import org.mini2Dx.core.graphics.viewport.Viewport; | |
import org.mini2Dx.ui.element.Container; | |
public class UiContainer extends org.mini2Dx.ui.UiContainer { | |
private static Container UI_CONTENT; | |
private String lastUiId; | |
private Viewport viewport; | |
public UiContainer(int width, int height, AssetManager assetManager) { | |
super(width, height, assetManager); | |
} | |
public UiContainer(GameContainer gc, AssetManager assetManager) { | |
super(gc, assetManager); | |
} | |
@Override | |
public void render(Graphics g) { | |
if (!isUiApplied()) return; | |
if (viewport != null) { | |
viewport.apply(g); | |
} | |
super.render(g); | |
} | |
@Override | |
public void update(float delta) { | |
if (!isUiApplied()) return; | |
if (lastUiId == null || !lastUiId.equals(UI_CONTENT.getId())) { | |
this.add(UI_CONTENT); | |
} | |
lastUiId = UI_CONTENT.getId(); | |
super.update(delta); | |
} | |
@Override | |
public void setViewport(Viewport viewport) { | |
super.setViewport(viewport); | |
this.viewport = viewport; | |
} | |
public void resize(int width, int height) { | |
set(width, height); | |
} | |
public static boolean isUiApplied() { | |
return UI_CONTENT != null; | |
} | |
public static void setUi(Container ui) { | |
if (ui == null) { | |
return; | |
} | |
if (UI_CONTENT != null && UI_CONTENT.getId().equals(ui.getId())) { | |
return; | |
} | |
UI_CONTENT = ui; | |
UI_CONTENT.set(0, 0, Mdx.graphicsContext.getWindowWidth(), Mdx.graphicsContext.getWindowHeight()); | |
} | |
public static Container getUi() { | |
return UI_CONTENT; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mygame.util; | |
import org.mini2Dx.core.assets.*; | |
import org.mini2Dx.core.exception.MdxException; | |
import org.mini2Dx.core.files.FileHandleResolver; | |
import org.mini2Dx.gdx.utils.Array; | |
import org.mini2Dx.ui.element.Container; | |
import org.mini2Dx.ui.xml.UiXmlLoader; | |
public class UiContainerLoader extends UiXmlLoader implements AsyncAssetLoader<Container> { | |
private static final String CACHE_CONTAINER_KEY = "uicontainer"; | |
/** | |
* Constructor | |
* | |
* @param resolver The {@link FileHandleResolver} to use | |
*/ | |
public UiContainerLoader(FileHandleResolver resolver) { | |
super(resolver); | |
} | |
@Override | |
public void loadOnAsyncThread(AssetDescriptor assetDescriptor, AsyncLoadingCache asyncLoadingCache) { | |
this.load(assetDescriptor.getFilePath()); | |
} | |
@Override | |
public boolean loadOnGameThread(AssetManager assetManager, AssetDescriptor<Container> assetDescriptor, AsyncLoadingCache asyncLoadingCache, AssetLoaderResult<Container> resultHolder) { | |
final Container container = asyncLoadingCache.getCache(CACHE_CONTAINER_KEY, Container.class); | |
resultHolder.setResult(container); | |
return true; | |
} | |
@Override | |
public Array<AssetDescriptor> getDependencies(AssetDescriptor assetDescriptor, AsyncLoadingCache asyncLoadingCache) { | |
if (!asyncLoadingCache.containsCache(CACHE_CONTAINER_KEY)) { | |
try { | |
asyncLoadingCache.setCache(CACHE_CONTAINER_KEY, this.load(assetDescriptor.getFilePath())); | |
} catch (Exception e) { | |
throw new MdxException(e.getMessage(), e); | |
} | |
} | |
return new Array<>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment