aboutsummaryrefslogtreecommitdiff
path: root/archived/ui_selection.c
diff options
context:
space:
mode:
authorRaymaekers Luca <raymaekers.luca@gmail.com>2024-12-07 19:17:33 +0100
committerRaymaekers Luca <luca@spacehb.net>2025-01-07 18:51:21 +0100
commit0574f5a7c5159a2ae1d7d2182cec982509947db9 (patch)
tree2a9678fd39bcf8ead03c7ee979caf668f2f17892 /archived/ui_selection.c
parent4aec2e1331650cabb966bbf24ed83f76cbbe9912 (diff)
checkpoint
Diffstat (limited to 'archived/ui_selection.c')
-rw-r--r--archived/ui_selection.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/archived/ui_selection.c b/archived/ui_selection.c
new file mode 100644
index 0000000..3e45ee2
--- /dev/null
+++ b/archived/ui_selection.c
@@ -0,0 +1,71 @@
+#define TB_IMPL
+#include "../chatty/external/termbox2.h"
+
+#include <signal.h>
+
+#define Assert(expr) if (!(expr)) raise(SIGTRAP)
+
+typedef struct {
+ int X, Y;
+} Position;
+
+int
+main(void)
+{
+ int Selected = 0;
+ int NumChoices = 3;
+ int Y = 0;
+
+ Position Positions[NumChoices];
+ Positions[0] = (Position){1, Y};
+ Positions[1] = (Position){5, Y};
+ Positions[2] = (Position){9, Y};
+ Y += 2;
+
+ struct tb_event ev = {0};
+ int Color = TB_GREEN;
+
+ tb_init();
+
+ int Quit = 0;
+ while (!Quit)
+ {
+ tb_clear();
+
+ int BaseY = Y;
+ tb_printf(0, BaseY, TB_BOLD, 0, "j"); tb_printf(2, BaseY++, 0, 0, "select next");
+ tb_printf(0, BaseY, TB_BOLD, 0, "k"); tb_printf(2, BaseY++, 0, 0, "select previous");
+ tb_printf(0, BaseY, TB_BOLD, 0, "s"); tb_printf(2, BaseY++, 0, 0, "change color");
+ tb_printf(0, BaseY, TB_BOLD, 0, "q"); tb_printf(2, BaseY++, 0, 0, "quit");
+
+ // Draw a box at position
+ for (int PositionsIndex = 0; PositionsIndex < NumChoices; PositionsIndex++)
+ {
+ Assert(Positions[PositionsIndex].X > 0);
+ tb_printf(Positions[PositionsIndex].X - 1, Positions[PositionsIndex].Y, Color, 0, "[ ]");
+ }
+ // Draw mark in selected box
+ tb_printf(Positions[Selected].X, Positions[Selected].Y, Color, 0, "x");
+
+ tb_present();
+ tb_poll_event(&ev);
+
+ if (ev.ch == 'q') Quit = 1;
+ else if (ev.ch == 'j' && Selected < NumChoices - 1) Selected++;
+ else if (ev.ch == 'k' && Selected) Selected--;
+ else if (ev.ch == 's')
+ {
+ switch (Selected)
+ {
+ case 0: Color = TB_GREEN; break;
+ case 1: Color = TB_BLUE; break;
+ case 2: Color = TB_RED; break;
+ default: Assert(0); break;
+ }
+ }
+ }
+
+ tb_shutdown();
+
+ return 0;
+}