Skip to content

Instantly share code, notes, and snippets.

@peanutbother
Created May 9, 2020 21:39
Show Gist options
  • Save peanutbother/7d74272e214c9b8ebd3730c9d639ba71 to your computer and use it in GitHub Desktop.
Save peanutbother/7d74272e214c9b8ebd3730c9d639ba71 to your computer and use it in GitHub Desktop.
package com.example.util;
import com.example.ui.GameMenuModel;
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.FitViewport;
import org.mini2Dx.core.graphics.viewport.Viewport;
import org.mini2Dx.core.screen.GameScreen;
import org.mini2Dx.core.screen.ScreenManager;
import org.mini2Dx.core.screen.transition.NullTransition;
import org.mini2Dx.ui.UiContainer;
import org.mini2Dx.ui.style.UiTheme;
public abstract class BasicGameScreen extends org.mini2Dx.core.screen.BasicGameScreen {
protected final AssetManager assetManager;
protected Viewport viewport;
protected UiContainer uiContainer;
protected int screenToLoad = -1;
private UiModel view;
protected BasicGameScreen(AssetManager assetManager) {
this.assetManager = assetManager;
}
public <T extends UiModel> void initialise(GameContainer gc, Models model) {
uiContainer = new UiContainer(gc, assetManager);
InputMultiplexer
.getInstance()
.addProcessor(uiContainer);
viewport = new FitViewport(true, gc.getWidth(), gc.getHeight());
view = ScreenFactory.create(model, uiContainer);
if (view != null) {
uiContainer.add(view.getContainer());
}
}
@Override
public void update(GameContainer gc, ScreenManager<? extends GameScreen> screenManager, float delta) {
if (!assetManager.update()) return;
if (!UiContainer.isThemeApplied()) {
UiContainer.setTheme(assetManager.get("ui/" + UiTheme.DEFAULT_THEME_FILENAME, UiTheme.class));
}
uiContainer.update(delta);
getView().update(delta);
if (screenToLoad != -1) {
screenManager.enterGameScreen(screenToLoad, new NullTransition(), new NullTransition());
// view.getContainer().setVisibility(Visibility.HIDDEN);
screenToLoad = -1;
}
Mdx.input.setInputProcessor(uiContainer);
}
@Override
public void render(GameContainer gc, Graphics g) {
viewport.apply(g);
uiContainer.render(g);
}
@Override
public void onResize(int width, int height) {
uiContainer.set(width, height);
}
protected <T extends UiModel> T getModel() {
return (T) view;
}
}
package com.example.ui;
import com.example.util.UiModel;
import org.mini2Dx.ui.UiContainer;
import org.mini2Dx.ui.annotation.UiElement;
import org.mini2Dx.ui.element.Container;
public class GameMenuModel extends UiModel {
@UiElement(id = "GameMenu")
Container container;
@Override
public Container getContainer() {
return container;
}
@Override
public void initialize(UiContainer uiContainer) {
super.initialize(uiContainer);
}
}
package com.example.ui;
import com.example.util.BasicGameScreen;
import org.mini2Dx.core.assets.AssetManager;
import org.mini2Dx.core.game.GameContainer;
public class GameMenuScreen extends BasicGameScreen {
public static final GameMenuModel model = GameMenuModel;
public static final int ID = 1;
public GameMenuScreen(AssetManager assetManager) {
super(assetManager);
}
@Override
public void initialise(GameContainer gc) {
super.initialise(gc, model);
}
@Override
public int getId() {
return ID;
}
}

Using the above pattern, you can implement a ScreenFactory using implemented api:

public static <T extends UiModel> T create(Models model, UiContainer uiContainer) {
        if (!isInitialized(uiContainer)) throw new MdxException("ScreenFactory not initialized!");

        if (isCached(model)) return (T) uiCache.get(model);

        if (model != null) {
            T container = (T) uiLoader.load(model.getLayout(), model.getInstance(), uiContainer);
            uiCache.put(model, container);

            Container modelContainer = container.getContainer();
            modelContainer.setWidthToWidthOf(uiContainer);
            modelContainer.setHeightToHeightOf(uiContainer);
            return container;
        }

        return null;
    }
package com.example.util;
import org.mini2Dx.ui.UiContainer;
import org.mini2Dx.ui.element.Container;
public abstract class UiModel {
public abstract Container getContainer();
public void initialize(UiContainer uiContainer) {
Container container = getContainer();
container.setWidthToWidthOf(uiContainer);
container.setHeightToHeightOf(uiContainer);
}
public void update(float delta) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment