UI and Options

BLaTTiX - Thu, 27 May 2021 10:30:30 GMT

Finally implemented a basic UI with persistent game options. This took longer than expected as I went down a rabbit-hole of starting to wire up Nuklear before realising that I could simply create a new Selectable component to complement the existing Text component.

typedef struct Selectable
  bool focused;
  bool toggle;
  bool value;
  Rectangle region;
  void (*callback)(GameOptions *options, bool value);
} Selectable;

The system that renders the Text component updates the region of the Selectable component (if it exists), and this is used to detect whether the cursor is hovering over the text. A new UI system simply iterates over Selectable components to decide which is active, and the relevant callback is triggered when a selection is made.

I also created a simple Options component, which is a singleton (similar to Input), and which is passed to relevant systems (for example, updating the player requires Options to determine whether the y-axis should be inverted or not). This component is serialised to allow it to persist between game sessions.

Altogether not much fun, but it is functional, and didn’t take too much time (thanks to Flecs). Now on to polishing the controls and fixing a few niggly bugs before moving on to greater things!