summaryrefslogtreecommitdiff
path: root/layers.ha
diff options
context:
space:
mode:
authorLassi Pulkkinen <lassi@pulk.fi>2024-10-31 03:11:21 +0200
committerLassi Pulkkinen <lassi@pulk.fi>2024-10-31 03:51:35 +0200
commitae44478b30d890fe0fb04022f44d474dcdcc3f9d (patch)
tree5f462459ae4b47d22114eed717d1382d08cf4dfe /layers.ha
Initial commit (import old repo)HEADmain
Diffstat (limited to 'layers.ha')
-rw-r--r--layers.ha73
1 files changed, 73 insertions, 0 deletions
diff --git a/layers.ha b/layers.ha
new file mode 100644
index 0000000..ebe9ac2
--- /dev/null
+++ b/layers.ha
@@ -0,0 +1,73 @@
+type Layer = struct {
+ blocks_input: *fn() bool,
+ input: *fn(InputEvent) bool,
+ is_opaque: *fn() bool,
+ render: *fn() void,
+};
+
+type InputEvent = (CancelEvent | KeyEvent |
+ MouseButtonEvent | MouseOverEvent);
+
+type CancelEvent = void;
+
+type KeyEvent = struct {
+ status: ButtonStatus,
+ key: Key,
+};
+
+type Key = uint;
+
+type MouseButtonEvent = struct {
+ status: ButtonStatus,
+ button: MouseButton,
+};
+
+type MouseButton = enum u8 {
+ LEFT,
+ MIDDLE,
+ RIGHT,
+ X1,
+ X2,
+};
+
+type ButtonStatus = enum u8 {
+ UP,
+ HELD,
+ DOWN,
+};
+
+type MouseOverEvent = struct {
+ covered: bool,
+};
+
+const LAYERS = [
+ &LAYER_GAME,
+ &LAYER_HUD,
+ &LAYER_DEATH,
+ &LAYER_GAME_WAITING,
+ &LAYER_CLIENT_WAITING,
+];
+
+fn layers_input(event: InputEvent, top: size, all: bool) size = {
+ for (top > 0) {
+ top -= 1;
+ if (LAYERS[top].input(event) && !all) {
+ break;
+ };
+ };
+ return top;
+};
+
+fn layers_render() void = {
+ let bottom = len(LAYERS);
+ for (bottom != 0) {
+ bottom -= 1;
+ if (LAYERS[bottom].is_opaque()) {
+ break;
+ };
+ };
+
+ for (let i = bottom; i < len(LAYERS); i += 1) {
+ LAYERS[i].render();
+ };
+};