aboutsummaryrefslogtreecommitdiff
path: root/archived/ui_selection.c
blob: 3e45ee2110e3ec9c91f414491c95372d3420a52a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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;
}