Skip to content

Instantly share code, notes, and snippets.

@schosin
Last active September 6, 2024 15:44
Show Gist options
  • Select an option

  • Save schosin/f936a1fc6f54dbe5131beb274338e2e0 to your computer and use it in GitHub Desktop.

Select an option

Save schosin/f936a1fc6f54dbe5131beb274338e2e0 to your computer and use it in GitHub Desktop.
Sample systems for using scene2d with artemis-odb
public abstract class PassiveIteratingSystem extends BaseEntitySystem {
public PassiveIteratingSystem() {
}
public PassiveIteratingSystem(Builder aspect) {
super(aspect);
}
@Override
protected void initialize() {
setEnabled(false);
}
@Override
protected final void processSystem() {
setEnabled(false);
}
}
public class UiManager extends BaseSystem implements Profiled {
// Singletons
private Ui ui; // provides access to Stage and Skin
private Input input; // provides access to InputMultiplexer
private UiCamera uiCamera; // provides access to OrthographicCamera for the UI
@Wire(name = G.Injectables.BATCH)
private PolygonSpriteBatch batch;
@Wire(name = G.Injectables.SETTINGS)
private Settings settings;
@Override
protected void initialize() {
ui.skin = new Skin(Gdx.files.classpath("scene2d/skin/uiskin.json"));
ui.stage = new Stage(new ScreenViewport(uiCamera.camera()), batch);
var settings = this.settings.debug() != null ? this.settings.debug().stage() : null;
if (settings != null) {
ui.stage.setDebugUnderMouse(settings.debugUnderMouse());
ui.stage.setDebugParentUnderMouse(settings.debugParentUnderMouse());
ui.stage.setDebugInvisible(settings.debugInvisible());
ui.stage.setDebugAll(settings.debugAll());
}
input.addProcessor(0, ui.stage);
}
@Override
protected void processSystem() {
ui.stage.act(world.delta);
ui.stage.draw();
}
@Override
protected void dispose() {
input.removeProcessor(ui.stage);
ui.stage.dispose();
ui.skin.dispose();
}
@Getter
@Singleton
public static final class Ui extends Component {
private Skin skin;
private Stage stage;
@Setter
private TooltipContainer tooltip;
}
}
@All(Unit.class)
public class UnitListSystem extends PassiveIteratingSystem {
// Singletons
private Ui ui;
// State
private final VerticalGroup list = new VerticalGroup();
private final IntMap<Label> labels = new IntMap<>();
@Override
protected void initialize() {
ui.stage().addActor(this.list);
list.setPosition(32, 32, Align.bottomLeft);
}
@Override
protected void inserted(int entityId) {
var label = entityLabel(entityId);
this.labels.put(entityId, label);
this.list.addActor(label);
this.list.pack();
}
@Override
protected void removed(int entityId) {
var label = labels.remove(entityId);
if (label != null) {
this.list.removeActor(label);
this.list.pack();
}
}
private Label entityLabel(int entityId) {
return new Label(Integer.toString(entityId), ui.skin());
}
}
public class TooltipUiSystem extends PassiveSystem {
// Singletons
private Ui ui;
// State
private Label tooltipTitle, tooltipText;
private VerticalGroup tooltipWindow;
private TooltipContainer tooltipContainer;
public void hideTooltip() {
tooltipWindow.clear();
tooltipContainer.setVisible(false);
}
public void showTooltip(String title, String text) {
tooltipText.setText(text);
showTooltip(title, tooltipText);
}
private void showTooltip(String title, Actor tooltip) {
tooltipTitle.setText(title);
tooltipWindow.clear();
tooltipWindow.addActor(tooltipTitle);
tooltipWindow.addActor(tooltip);
tooltipContainer.pack();
tooltipContainer.setPosition(Gdx.graphics.getWidth() - 32, 32, Align.bottomRight);
tooltipContainer.setVisible(true);
}
@Override
protected void initialize() {
super.initialize();
// Create actor
this.tooltipText = new Label("", ui.skin());
this.tooltipTitle = new Label("Tooltip", ui.skin());
this.tooltipWindow = new VerticalGroup();
this.tooltipContainer = new TooltipContainer(tooltipWindow, tooltipTitle);
tooltipContainer.setBackground(ui.skin().getDrawable("window-border-bg"));
tooltipContainer.pad(4f);
tooltipContainer.setVisible(false);
ui.tooltip(tooltipContainer);
ui.stage().addActor(tooltipContainer);
hideTooltip();
}
@Getter
public static final class TooltipContainer extends Container<VerticalGroup> {
private final VerticalGroup window;
private final Label title;
public TooltipContainer(VerticalGroup window, Label title) {
super(window);
this.window = window;
this.title = title;
}
}
}
@schosin
Copy link
Author

schosin commented Nov 29, 2020

Example of how I am currently using Libgdx scene2d with artemis-odb. This also makes use of the singleton plugin of artemis-odb-contrib.
The basic idea is having UiManager create and act/draw the Stage and have other systems for the actual UI elements.

In the first example UnitListSystem I create a list of entityIds having the Unit component to the bottom left of the screen:

  1. Initialization of the actors and adding to the scene happens in BaseSystem#initialize()
  2. Labels for the entities are added to the list in BaseEntitySystem#inserted(int). To easily remove them, I also keep them in a IntMap
  3. Labels for the entities are removed from the list in BaseEntitySystem#removed(int)

The second example is TooltipUiSystem. This system creates a simple tooltip window that can be called by other systems. An example would be hovering over non-scene2d entities and showing a tooltip (e.g. iff the entity has a Tooltip component).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment