summaryrefslogtreecommitdiff
path: root/examples/ui.c
blob: 0847af3454c9f6636be5ea6a81bc5d52e4404097 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#if 0 /*
#/  ================================================================
#/
#/    ui.c
#/
#/  ================================================================
#/
#/    Self-compilation shell script
#/
SRC=${0##*./}
BIN=${SRC%.*}
gcc                                         \
  -Wall -Wextra -Werror -pedantic           \
  -Wno-old-style-declaration                \
  -Wno-missing-braces                       \
  -Wno-unused-variable                      \
  -Wno-unused-but-set-variable              \
  -Wno-unused-parameter                     \
  -Wno-overlength-strings                   \
  -O3                                       \
  -fsanitize=undefined,address,leak -mshstk \
  -lX11 -lm                                 \
  -o $BIN $SRC &&                           \
  ./$BIN $@ && rm $BIN
exit $? # */
#endif

#include "../graphics.c"

i32 main(i32 argc, c8 **argv) {
  (void) argc;
  (void) argv;

  platform = (Platform) {
    .title        = "UI",
    .frame_width  = 960,
    .frame_height = 720,
  };

  p_init();

  b8 button_0_down    = 0;
  b8 button_1_down    = 0;
  b8 button_1_checked = 0;

  i64        text_len  = 0;
  static c32 text[512] = {0};

  i64 cursor    = 0;
  i64 selection = 0;

  while (!platform.done) {
    p_wait_events();

    for (i32 j = 0; j < platform.frame_height; ++j)
      for (i32 i = 0; i < platform.frame_width; ++i)
        platform.pixels[j * platform.frame_width + i] = 0x302000;

    if (platform.cursor_x >= 40 && platform.cursor_x < 100 && platform.cursor_y >= 40 && platform.cursor_y < 100) {
      button_0_down = platform.key_down[BUTTON_LEFT];
      if (button_0_down)
        draw_panel(OP_SET, 0xffffff, 40, 40, 60, 60);
      else
        draw_panel(OP_SET, 0x00ff00, 40, 40, 60, 60);
    } else {
      button_0_down = 0;
      draw_panel(OP_SET, 0x208020, 40, 40, 60, 60);
    }

    if (platform.cursor_x >= 40 && platform.cursor_x < 100 && platform.cursor_y >= 120 && platform.cursor_y < 180) {
      button_1_down = platform.key_down[BUTTON_LEFT];
      if (platform.key_pressed[BUTTON_LEFT])
        button_1_checked = !button_1_checked;
      if (button_1_down)
        draw_panel(OP_SET, 0xffffff, 40, 120, 60, 60);
      else if (button_1_checked)
        draw_panel(OP_SET, 0xff8080, 40, 120, 60, 60);
      else
        draw_panel(OP_SET, 0x80ff80, 40, 120, 60, 60);
    } else {
      button_1_down = 0;
      if (button_1_checked)
        draw_panel(OP_SET, 0xff0000, 40, 120, 60, 60);
      else
        draw_panel(OP_SET, 0x00ff00, 40, 120, 60, 60);
    }

    i64 w  = platform.frame_width  / 2;
    i64 h  = platform.frame_height / 2;
    i64 x0 = w / 2;
    i64 y0 = h / 2;

    i64 color = 0xff7f7f;

    if (platform.cursor_x >= x0 && platform.cursor_x < x0 + w &&
        platform.cursor_y >= y0 && platform.cursor_y < y0 + h)
      color = 0xffffff;

    for (i64 i = 0; i < platform.input_size; ++i)
      if (platform.input[i].ctrl)
        switch (platform.input[i].key) {
          case 'v': {
            if (selection != 0) {
              i64 i0 = selection < 0 ? cursor + selection : cursor;
              i64 i1 = selection < 0 ? cursor : cursor + selection;
              for (i64 i = 0; i1 + i < text_len; ++i)
                text[i0 + i] = text[i1 + i];
              selection = 0;
              cursor    = i0;
              text_len -= i1 - i0;
            }

            for (i64 n = 0; n < platform.clipboard_size;) {
              c32 c = utf8_read(platform.clipboard_size - n, platform.clipboard + n);

              if (text_len < (i64) (sizeof text / sizeof *text)) {
                for (i64 j = text_len; j > cursor; --j)
                  text[j] = text[j - 1];
                text[cursor++] = c;
                ++text_len;
              }

              n += utf8_size(c);
            }
          } break;

          case 'x': {
            i64       len = 0;
            static c8 buf[1024];

            i64 i0 = selection < 0 ? cursor + selection : cursor;
            i64 i1 = selection < 0 ? cursor : cursor + selection;
            for (i64 i = 0; len + 4 <= (i64) sizeof buf && i < i1 - i0; ++i)
              len += utf8_write(text[i0 + i], buf + len);

            if (len > 0)
              p_clipboard_write(len, buf);

            for (i64 i = 0; i1 + i < text_len; ++i)
              text[i0 + i] = text[i1 + i];
            selection = 0;
            cursor    = i0;
            text_len -= i1 - i0;
          } break;

          case 'c': {
            i64       len = 0;
            static c8 buf[1024];

            i64 i0 = selection < 0 ? cursor + selection : cursor;
            i64 i1 = selection < 0 ? cursor : cursor + selection;
            for (i64 i = 0; len + 4 <= (i64) sizeof buf && i < i1 - i0; ++i)
              len += utf8_write(text[i0 + i], buf + len);

            if (len > 0)
              p_clipboard_write(len, buf);
          } break;

          default:;
        }
      else
        switch (platform.input[i].key) {
          case KEY_LEFT:
            if (platform.key_down[MOD_SHIFT]) {
              if (cursor > 0)
                ++selection;
            } else if (selection != 0) {
              cursor = selection < 0 ? cursor + selection + 1 : cursor + 1;
              selection = 0;
            }
            if (cursor > 0)
              --cursor;
            break;

          case KEY_RIGHT:
            if (platform.key_down[MOD_SHIFT]) {
              if (cursor < text_len)
                --selection;
            } else if (selection != 0) {
              cursor = selection < 0 ? cursor - 1 : cursor + selection - 1;
              selection = 0;
            }
            if (cursor < text_len)
              ++cursor;
            break;

          case '\b':
            if (selection != 0) {
              i64 i0 = selection < 0 ? cursor + selection : cursor;
              i64 i1 = selection < 0 ? cursor : cursor + selection;
              for (i64 i = 0; i1 + i < text_len; ++i)
                text[i0 + i] = text[i1 + i];
              selection = 0;
              cursor    = i0;
              text_len -= i1 - i0;
            } else if (cursor > 0 && text_len > 0) {
              for (i64 i = cursor; i < text_len; ++i)
                text[i - 1] = text[i];
              --cursor;
              --text_len;
            }
            break;

          case KEY_DELETE:
            if (selection != 0) {
              i64 i0 = selection < 0 ? cursor + selection : cursor;
              i64 i1 = selection < 0 ? cursor : cursor + selection;
              for (i64 i = 0; i1 + i < text_len; ++i)
                text[i0 + i] = text[i1 + i];
              selection = 0;
              cursor    = i0;
              text_len -= i1 - i0;
            } else if (cursor < text_len) {
              for (i64 i = cursor + 1; i < text_len; ++i)
                text[i - 1] = text[i];
              --text_len;
            }
            break;

          case '\n':
          case '\r':
          case '\t':
            platform.input[i].c = platform.input[i].key;
            //  fallthrough

          default:
            if (platform.input[i].c) {
              if (selection != 0) {
                i64 i0 = selection < 0 ? cursor + selection : cursor;
                i64 i1 = selection < 0 ? cursor : cursor + selection;
                for (i64 i = 0; i1 + i < text_len; ++i)
                  text[i0 + i] = text[i1 + i];
                selection = 0;
                cursor    = i0;
                text_len -= i1 - i0;
              }

              if (text_len < (i64) (sizeof text / sizeof *text)) {
                for (i64 i = text_len; i > cursor; --i)
                  text[i] = text[i - 1];
                text[cursor++] = platform.input[i].c;
                ++text_len;
              }
            }
        }

    draw_text_area(0, x0 + 8, y0 - 8, w, h, 10., 10., text_len, text);
    draw_text_area(color, x0, y0, w, h, 10., 10., text_len, text);
    draw_text_cursor(0xffffff, x0, y0, w, h, 10., 10., cursor, selection, text_len, text);

    p_render_frame();
  }

  p_cleanup();
  return 0;
}