aboutsummaryrefslogtreecommitdiff
path: root/ui.h
blob: 21a4a8085876e02ce8631fee845ac2e5e34c7024 (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Format option at a position in raw text, used when iterating to know when to toggle a color
// option.
typedef struct {
    u32 Position;
    u32 Color;
} format_option;

// Array of format options and length of said array
typedef struct {
    format_option* Options;
    u32 Len;
} markdown_formatoptions;

typedef struct {
    u32* Text;
    u32 Len;
} raw_result;

// Return True if ch is whitespace
Bool
is_whitespace(u32 ch)
{
    if (ch == L' ')
        return True;
    return False;
}

// Return True if ch is a supported markdown markup character
// TODO: tilde
Bool
is_markdown(u32 ch)
{
    if (ch == L'_' ||
        ch == L'*')
        return True;
    return False;
}

// Print `Text`, `Len` characters long with markdown
// NOTE: This function has no wrapping support
void
tb_print_markdown(u32 X, u32 Y, u32 fg, u32 bg, u32* Text, u32 Len)
{
    for (u32 ch = 0; ch < Len; ch++)
    {
        if (Text[ch] == L'_')
        {
            if (ch < Len - 1 && Text[ch + 1] == L'_')
            {
                fg ^= TB_UNDERLINE;
                ch++;
            }
            else
            {
                fg ^= TB_ITALIC;
            }
        }
        else if (Text[ch] == L'*')
        {
            if (ch < Len - 1 && Text[ch + 1] == L'*')
            {
                fg ^= TB_BOLD;
                ch++;
            }
            else
            {
                fg ^= TB_ITALIC;
            }
        }
        else
        {
            tb_printf(X, Y, fg, bg, "%lc", Text[ch]);
#ifdef DEBUG
            tb_present();
#endif
            X++;
        }
    }
}

// Print `Text`, `Len` characters long as a string wrapped at `XLimit` width and `YLimit` height.
void
tb_print_wrapped(u32 X, u32 Y, u32 XLimit, u32 YLimit, u32* Text, u32 Len)
{
    // Iterator in text
    assert(XLimit > 0);
    assert(YLimit > 0);
    u32 i = XLimit;

    u32 PrevI = 0;

    // For printing
    u32 t = 0;

    while(i < Len)
    {
        // Search backwards for whitespace
        while (!is_whitespace(Text[i]))
        {
            i--;

            // Failed to find whitespace, break on limit at character
            if (i == PrevI)
            {
                i += XLimit;
                break;
            }
        }

        t = Text[i];
        Text[i] = 0;
        tb_printf(X, Y++, 0, 0, "%ls", Text + PrevI);
#ifdef DEBUG
        tb_present();
#endif

        Text[i] = t;

        if (is_whitespace(Text[i])) i++;

        PrevI = i;
        i += XLimit;

        if (Y >= YLimit - 1)
        {
            break;
        }
    }
    tb_printf(X, Y++, 0, 0, "%ls", Text + PrevI);
}

// Print raw string with markdown format options in `MDFormat`, wrapped at
// `XLimit` and `YLimit`.  The string is offset by `XOffset` and `YOffset`.
// `fg` and `bg` are passed to `tb_printf`.
// `Len` is the length of the string not including a null terminator
// The wrapping algorithm searches for a whitespace backwards and if none are found it wraps at
// `XLimit`.
// This function first builds an array of positions where to wrap and then prints `Text` by
// character using the array in `MDFormat.Options` and `WrapPositions` to know when to act.
// Returns how many times wrapped
u32
tb_print_wrapped_with_markdown(u32 XOffset, u32 YOffset, u32 fg, u32 bg,
                               u32* Text, u32 Len,
                               u32 XLimit, u32 YLimit,
                               markdown_formatoptions MDFormat)
{
    XLimit -= XOffset;
    YLimit -= YOffset;
    assert(YLimit > 0);
    assert(XLimit > 0);

    u32 TextIndex = XLimit;
    u32 PrevTextIndex = 0;

    u32 WrapPositions[Len/XLimit + 1];
    u32 WrapPositionsLen = 0;

    // Get wrap positions
    while (TextIndex < Len)
    {
        while (!is_whitespace(Text[TextIndex]))
        {
            TextIndex--;

            if (TextIndex == PrevTextIndex)
            {
                TextIndex += XLimit;
                break;
            }
        }

        WrapPositions[WrapPositionsLen] = TextIndex;
        WrapPositionsLen++;

        PrevTextIndex = TextIndex;
        TextIndex += XLimit;
    }

    u32 MDFormatOptionsIndex = 0;
    u32 WrapPositionsIndex = 0;
    u32 X = XOffset, Y = YOffset;

    for (u32 TextIndex = 0; TextIndex < Len; TextIndex++)
    {
        if (MDFormatOptionsIndex < MDFormat.Len &&
            TextIndex == MDFormat.Options[MDFormatOptionsIndex].Position)
        {
            fg ^= MDFormat.Options[MDFormatOptionsIndex].Color;
            MDFormatOptionsIndex++;
        }
        if (WrapPositionsIndex < WrapPositionsLen &&
            TextIndex == WrapPositions[WrapPositionsIndex])
        {
            Y++;
            if (Y == YLimit) return WrapPositionsIndex + 1;
            WrapPositionsIndex++;
            X = XOffset;
            if (is_whitespace(Text[TextIndex])) continue;
        }
        tb_printf(X++, Y, fg, bg, "%lc", Text[TextIndex]);
    }
    assert(WrapPositionsIndex == WrapPositionsLen);
    assert(MDFormat.Len == MDFormatOptionsIndex);

    return WrapPositionsLen + 1;
}

// Return string without markdown markup characters using `is_markdown()`
// ScratchArena is used to allocate space for the raw text
// If ScratchArena is null then it will only return then length of the raw string
// Len should be characters + null terminator
// Copies the null terminator as well
raw_result
markdown_to_raw(Arena* ScratchArena, wchar_t* Text, u32 Len)
{
    raw_result Result = {0};
    if (ScratchArena)
    {
        Result.Text = ScratchArena->addr;
    }

    for (u32 i = 0; i < Len; i++)
    {
        if (!is_markdown(Text[i]))
        {
            if (ScratchArena)
            {
                u32* ch = ArenaPush(ScratchArena, sizeof(*ch));
                *ch = Text[i];
            }
            Result.Len++;
        }
    }

    return Result;
}

// Get a string with markdown in it and fill array in makrdown_formtoptions with position and colors
// Use Scratcharena to make allocations on that buffer, The Maximimum space needed is Len, eg. when
// the string is only markup characters.
markdown_formatoptions
preprocess_markdown(Arena* ScratchArena, wchar_t* Text, u32 Len)
{
    markdown_formatoptions Result = {0};
    Result.Options = (format_option*)((u8*)ScratchArena->addr + ScratchArena->pos);

    format_option* FormatOpt;

    // raw char iterator
    u32 rawch = 0;

    for (u32 i = 0; i < Len; i++)
    {
        switch (Text[i])
        {
        case L'_':
        {
            FormatOpt = ArenaPush(ScratchArena, sizeof(*FormatOpt));
            Result.Len++;

            FormatOpt->Position = rawch;
            if (i < Len - 1 && Text[i + 1] == '_')
            {
                FormatOpt->Color = TB_UNDERLINE;
                i++;
            }
            else
            {
                FormatOpt->Color = TB_ITALIC;
            }
        } break;
        case L'*':
        {
            FormatOpt = ArenaPush(ScratchArena, sizeof(*FormatOpt));
            Result.Len++;

            FormatOpt->Position = rawch;
            if (i < Len - 1 && Text[i + 1] == '*')
            {
                FormatOpt->Color = TB_BOLD;
                i++;
            }
            else
            {
                FormatOpt->Color = TB_ITALIC;
            }
        } break;
        default:
        {
            rawch++;
        } break;
        }
    }

    return Result;
}

u32 InputBoxMarginX = 1;
u32 InputBoxPaddingX = 1;
#define INPUT_BOX_BORDER_WIDTH 1
#define INPUT_BOX_MIN_TEXT_WIDTH 1
#define INPUT_BOX_MIN_TEXT_HEIGHT 1

u32
GetInputBoxMinimumWidth()
{
    return InputBoxPaddingX * 2 +
           InputBoxMarginX * 2 +
           INPUT_BOX_BORDER_WIDTH * 2 +
           INPUT_BOX_MIN_TEXT_WIDTH;
}

u32
GetInputBoxMinimumHeight()
{
    return INPUT_BOX_BORDER_WIDTH * 2 +
           INPUT_BOX_MIN_TEXT_HEIGHT;
}

void
InputBox(u32 BoxX, u32 BoxY, u32 BoxWidth, u32 BoxHeight,
              wchar_t *Text, u32 TextLen,
              Bool Focused)
{
    //  ╭───────╮
    // M│P....█P│M
    //  ╰───────╯
    // . -> text
    // █ -> cursor
    // P -> padding (symmetric)
    // M -> margin (symmetric)

    u32 MarginX = InputBoxMarginX;
    u32 PaddingX = InputBoxPaddingX;
    u32 BorderWidth = INPUT_BOX_BORDER_WIDTH;

    // Get 0-based coordinate
    BoxWidth -= 2* MarginX;
    BoxHeight--;

    wchar_t ur = L'╭';
    wchar_t ru = L'╯';
    wchar_t rd = L'╮';
    wchar_t dr = L'╰';
    wchar_t lr = L'─';
    wchar_t ud = L'│';

    // Draw Borders
    {
        // Draw the top bar
        tb_printf(BoxX + MarginX, BoxY, 0, 0, "%lc", ur);
        for (u32 X = 1;
             X < BoxWidth;
             X++)
        {
            tb_printf(BoxX + X + MarginX, BoxY, 0, 0, "%lc", lr);
        }
        tb_printf(BoxX + BoxWidth + MarginX, BoxY, 0, 0, "%lc", rd);

        // Draw vertical bars
        for (u32 Y = 1;
             Y < BoxHeight;
             Y++)
        {
            tb_printf(BoxX + MarginX, BoxY + Y, 0, 0, "%lc", ud);
            tb_printf(BoxX + BoxWidth + MarginX, BoxY + Y, 0, 0, "%lc", ud);
        }

        // Draw the bottom bar
        tb_printf(BoxWidth + MarginX, BoxY + BoxHeight, 0, 0, "%lc", ru);
        for (u32 X = 1;
             X < BoxWidth;
             X++)
        {
            tb_printf(BoxX + X + MarginX, BoxY + BoxHeight, 0, 0, "%lc", lr);
        }
        tb_printf(BoxX + MarginX, BoxY + BoxHeight, 0, 0, "%lc", dr);
    }

    // Draw the text
    u32 TextX = BoxX + MarginX + PaddingX + 1;
    u32 TextY = BoxY + 1;
    u32 TextWidth = BoxWidth - TextX - MarginX + 1;
    u32 TextHeight = BoxHeight - BorderWidth * 2 + 1;
    u32 TextDisplaySize = TextWidth * TextHeight;

    // XOffset and YOffset are needed for setting the cursor position
    u32 XOffset = 0, YOffset = 0;
    u32 TextOffset = 0;

    // If there is more than one line, implement vertical wrapping otherwise scroll the text
    // horizontally.
    if (TextHeight > 1)
    {
        // If there is not enough space to fit the text scroll one line by advancing by textwidth.
        if (TextLen >= TextDisplaySize)
        {
            // TextHeight - 1 : scroll by one line
            TextOffset = (TextLen / TextWidth - (TextHeight - 1))  * TextWidth;
        }

        // Print the text
        while (TextOffset < TextLen)
        {
            for (YOffset = 0;
                 YOffset < TextHeight && TextOffset < TextLen;
                 YOffset++)
            {
                for (XOffset = 0;
                     XOffset < TextWidth && TextOffset < TextLen;
                     XOffset++)
                {
                    tb_printf(TextX + XOffset, TextY + YOffset, 0, 0, "%lc", Text[TextOffset]);
                    TextOffset++;
                }
            }
        }
    }
    else
    {
        // Scrooll the text horizontally
        if (TextLen >= TextDisplaySize)
        {
            TextOffset = TextLen - TextWidth;
            XOffset = TextWidth;
        }
        else
        {
            XOffset = TextLen;
        }
        YOffset = 1;
        tb_printf(TextX, TextY, 0, 0, "%ls", Text + TextOffset);
    }

#ifdef DEBUG
    tb_printf(BoxX + 1, BoxY, 0, 0, "%d/%d [%dx%d] %dx%d %d (%d,%d)+(%d,%d)",
            TextLen, MAX_INPUT_LEN,
            BoxWidth, BoxHeight,
            TextWidth, TextHeight,
            TextOffset,
            TextX, TextY,
            XOffset, YOffset);
#endif

    // Set the cursor
    if (Focused)
    {
        if (TextLen == 0)
        {
            // When there is no text
            global.cursor_x = TextX;
            global.cursor_y = TextY;
        }
        else if (TextLen % TextWidth == 0 && TextHeight > 1)
        {
            // When at the end of width put the cursor on the next line
            global.cursor_x = TextX;
            global.cursor_y = TextY + YOffset;
        }
        else
        {
            // Put cursor after the text
            // Minus one because of the for loop
            global.cursor_x = (TextX-1) + XOffset + 1;
            global.cursor_y = (TextY-1) + YOffset;
        }
    }
}