From 0574f5a7c5159a2ae1d7d2182cec982509947db9 Mon Sep 17 00:00:00 2001 From: Raymaekers Luca Date: Sat, 7 Dec 2024 19:17:33 +0100 Subject: checkpoint --- ui.h | 730 +++++++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 534 insertions(+), 196 deletions(-) (limited to 'ui.h') diff --git a/ui.h b/ui.h index bdb4167..078a456 100644 --- a/ui.h +++ b/ui.h @@ -1,4 +1,14 @@ -#include "chatty.h" +#ifndef UI_H +#define UI_H + +/* Macro's */ + +#include +#include +#include "termbox2.h" +#include "arena.h" + +/* Types */ // Format option at a position in raw text, used when iterating to know when to toggle a color // option. @@ -18,24 +28,535 @@ typedef struct { u32 Len; } raw_result; +// Rectangle +typedef struct { + s32 X, Y, W, H; +} rect; + +// Characters to use for drawing a box +// See DrawBox() for an example +typedef struct { + wchar_t ur, ru, rd, dr, lr, ud; +} box_characters; + +/* Functions */ + +bool IsInRect(rect Rect, s32 X, s32 Y); +bool is_whitespace(u32 ch); +bool is_markdown(u32 ch); +void tb_print_wrapped(u32 X, u32 Y, u32 XLimit, u32 YLimit, u32* Text, u32 Len); +void tb_print_markdown(u32 X, u32 Y, u32 fg, u32 bg, u32* Text, u32 Len); +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); +raw_result markdown_to_raw(Arena* ScratchArena, wchar_t* Text, u32 Len); +markdown_formatoptions preprocess_markdown(Arena* ScratchArena, wchar_t* Text, u32 Len); + +/* Input Box UI */ + +// assumes TEXTBOX_MAX_INPUT to be set +// +// #define TEXTBOX_MAX_INPUT 128 + +#define TEXTBOX_PADDING_X 1 +#define TEXTBOX_BORDER_WIDTH 1 +#define TEXTBOX_MIN_WIDTH TEXTBOX_PADDING_X * 2 + TEXTBOX_BORDER_WIDTH * 2 + 1; +#define TEXTBOXFROMBOX(Box) \ + { \ + .X = Box.X + TEXTBOX_BORDER_WIDTH + TEXTBOX_PADDING_X, \ + .Y = Box.Y + TEXTBOX_BORDER_WIDTH, \ + .W = Box.W - TEXTBOX_BORDER_WIDTH * 2 - TEXTBOX_PADDING_X * 2, \ + .H = Box.H - TEXTBOX_BORDER_WIDTH * 2 \ + } + +void DrawBox(rect Rect, box_characters *Chars); +void DrawTextBox(rect TextR, wchar_t *Text, u32 TextLen); +void DrawTextBoxWrapped(rect TextR, wchar_t *Text, u32 TextLen); +void TextBoxScrollLeft(rect Text, u32 *TextOffset); +void TextBoxScrollRight(rect Text, u32 *TextOffset); +void TextBoxDelete(wchar_t* Text, u64 Pos); +void TextBoxInsert(wchar_t *Input, u32 InputPos, u32 InputLen, wchar_t ch); +u32 TextBoxKeypress(struct tb_event ev, + rect TextR, wchar_t *Text, u32 *TextLenPtr, u32 TextPos, u32 *TextOffsetPtr); + +// Draw box along boundaries in Rect with optional Chars. +void +DrawBox(rect Rect, box_characters *Chars) +{ + wchar_t ur, ru, rd, dr, lr, ud; + if (!Chars) + { + ur = L'╭'; + ru = L'╯'; + rd = L'╮'; + dr = L'╰'; + lr = L'─'; + ud = L'│'; + } + else + { + ur = Chars->ur; + ru = Chars->ru; + rd = Chars->rd; + dr = Chars->dr; + lr = Chars->lr; + ud = Chars->ud; + } + + Rect.H--; + Rect.W--; + + tb_printf(Rect.X, Rect.Y, 0, 0, "%lc", ur); + for (s32 X = 1; X < Rect.W; X++) + { + tb_printf(Rect.X + X, Rect.Y, 0, 0, "%lc", lr); + } + tb_printf(Rect.X + Rect.W, Rect.Y, 0, 0, "%lc", rd); + + // Draw vertical bars + for (s32 Y = 1; Y < Rect.H; Y++) + { + tb_printf(Rect.X, Rect.Y + Y, 0, 0, "%lc", ud); + tb_printf(Rect.X + Rect.W, Rect.Y + Y, 0, 0, "%lc", ud); + } + + tb_printf(Rect.X, Rect.Y + Rect.H, 0, 0, "%lc", dr); + for (s32 X = 1; X < Rect.W; X++) + { + tb_printf(Rect.X + X, Rect.Y + Rect.H, 0, 0, "%lc", lr); + } + tb_printf(Rect.X + Rect.W, Rect.Y + Rect.H, 0, 0, "%lc", ru); +} + +// SCROLLING +// ╭──────────╮ ╭──────────╮ Going Left on the first character scrolls up. +// │ █3 4 │ => │ 1 2█ │ Cursor on end of the top line. +// │ 5 6 │ │ 3 4 │ +// ╰──────────╯ ╰──────────╯ +// +// ╭──────────╮ ╭──────────╮ Going Right on the last character scrolls down. +// │ 1 3 │ => │ 2 4 │ Puts cursor on start of the bottom line. +// │ 2 4█ │ │ █ │ +// ╰──────────╯ ╰──────────╯ +// +// ╭──────────╮ ╭──────────╮ Going Down on bottom line scrolls down. +// │ 1 3 │ => │ 2 4 │ Cursor stays on bottom line. +// │ 2 █ 4 │ │ █ │ +// ╰──────────╯ ╰──────────╯ +// +// ╭──────────╮ ╭──────────╮ Going Up on top line scrolls up. +// │ 3 █ 4 │ => │ 1 █ 2 │ Cursor stays on top line. +// │ 5 6 │ │ 3 5 │ +// ╰──────────╯ ╰──────────╯ +// +// In code this translates to changing global.cursor_{x,y} and TextOffset accordingly. + +// Scroll one character to the left +void +TextBoxScrollLeft(rect Text, u32 *TextOffset) +{ + // If text is on the first character of the box scroll up + if (global.cursor_x == Text.X && + global.cursor_y == Text.Y) + { + global.cursor_x = Text.X + Text.W - 1; + global.cursor_y = Text.Y; + + *TextOffset -= Text.W; + } + else + { + if (global.cursor_x == Text.X) + { + // Got to previous line + global.cursor_x = Text.X + Text.W - 1; + global.cursor_y--; + } + else + { + global.cursor_x--; + } + } +} + +// Scroll one character to the right +void +TextBoxScrollRight(rect Text, u32 *TextOffset) +{ + // If cursor is on the last character scroll forwards + if (global.cursor_x == Text.X + Text.W - 1 && + global.cursor_y == Text.Y + Text.H - 1) + { + global.cursor_x = Text.X; + global.cursor_y = Text.Y + Text.H - 1; + + *TextOffset += Text.W; + } + else + { + global.cursor_x++; + if (global.cursor_x == Text.X + Text.W) + { + global.cursor_x = Text.X; + global.cursor_y++; + } + } +} + + +// Delete a character in Text at Pos +void +TextBoxDelete(wchar_t* Text, u64 Pos) +{ + memmove(Text + Pos, + Text + Pos + 1, + (TEXTBOX_MAX_INPUT - Pos - 1) * sizeof(*Text)); +} + +// Insert a ev.ch in Input at InputPos +void +TextBoxInsert(wchar_t *Input, u32 InputPos, u32 InputLen, wchar_t ch) +{ + if (InputPos < InputLen) + { + memmove(Input + InputPos, + Input + InputPos - 1, + (InputLen - InputPos + 1) * sizeof(*Input)); + } + Input[InputPos] = ch; +} + +// Handle the key event ev changing Text, TextLenPtr, and TextOffsetPtr accordingly. +// InputPos is the position in the Input relating to the cursor position. +// TextR is the bounding box for the text. +// +// Returns non-zero when a key event was handled. +// +// TODO: pass by value and return struct with updated values +u32 +TextBoxKeypress(struct tb_event ev, rect TextR, + wchar_t *Text, u32 *TextLenPtr, u32 TextPos, u32 *TextOffsetPtr) +{ + u32 Result = 1; + + u32 TextLen = *TextLenPtr; + u32 TextOffset = *TextOffsetPtr; + + switch (ev.key) + { + + // Delete character backwards + case TB_KEY_CTRL_8: + // case TB_KEY_BACKSPACE2: + { + if (TextPos == 0) break; + + TextBoxDelete(Text, TextPos - 1); + TextLen--; + + TextBoxScrollLeft(TextR, &TextOffset); + + } break; + + // Delete character forwards + case TB_KEY_CTRL_D: + { + if (TextPos == TextLen) break; + TextBoxDelete(Text, TextPos); + TextLen--; + // Delete(Text, Position) + } break; + + // Delete word backwards + case TB_KEY_CTRL_W: + { + u32 At = TextPos; + // Find character to stop on + while (At && is_whitespace(Text[At - 1])) At--; + while (At && !is_whitespace(Text[At - 1])) At--; + + s32 NDelete = TextPos - At; + memmove(Text + At, Text + TextPos, (TextLen - TextPos) * sizeof(Text[At])); + TextLen -= NDelete; +#ifdef DEBUG + Text[TextLen] = 0; +#endif + // NOTE: this could be calculated at once instead + while(NDelete--) TextBoxScrollLeft(TextR, &TextOffset); + + Assert(IsInRect(TextR, global.cursor_x, global.cursor_y)); + + } break; + + // Delete until start of Text + case TB_KEY_CTRL_U: + { + memmove(Text, Text + TextPos, (TextLen - TextPos) * sizeof(*Text)); + TextLen -= TextPos; +#ifdef DEBUG + Text[TextLen] = 0; +#endif + global.cursor_x = TextR.X; + global.cursor_y = TextR.Y; + TextOffset = 0; + } break; + + // Delete until end of Text + case TB_KEY_CTRL_K: + { + TextLen = TextPos; + Text[TextPos] = 0; + } break; + + // Move to start of line + case TB_KEY_CTRL_A: global.cursor_x = TextR.X; break; + + // Move to end of line + case TB_KEY_CTRL_E: + { + if (global.cursor_x == TextR.X + TextR.W - 1) break; + + if (TextPos + TextR.W > TextLen) + { + // Put the cursor on the last character + global.cursor_x = TextR.X + (TextLen - TextOffset) % TextR.W; + } + else + { + global.cursor_x = TextR.X + TextR.W - 1; + } + } break; + + // Move backwards + case TB_KEY_CTRL_B: + case TB_KEY_ARROW_LEFT: + { + // Move forward by word + if (ev.mod == TB_MOD_CTRL) + { + u32 At = TextPos; + while(At && is_whitespace(Text[At])) At--; + while(At && !is_whitespace(Text[At])) At--; + while(TextPos - At++) TextBoxScrollLeft(TextR, &TextOffset); + } + // Move forward by character + else + { + if (TextPos == 0) break; + TextBoxScrollLeft(TextR, &TextOffset); + } + } break; + + // Move forwards + case TB_KEY_CTRL_F: + case TB_KEY_ARROW_RIGHT: + { + // Move forward by word + if (ev.mod == TB_MOD_CTRL) + { + u32 At = TextPos; + while(At < TextLen && is_whitespace(Text[At])) At++; + while(At < TextLen && !is_whitespace(Text[At])) At++; + while(At-- - TextPos) TextBoxScrollRight(TextR, &TextOffset); + } + // Move forward by character + else + { + if (TextPos == TextLen) break; + TextBoxScrollRight(TextR, &TextOffset); + } + } break; + + // Move up + case TB_KEY_CTRL_P: + case TB_KEY_ARROW_UP: + { + if (global.cursor_y == TextR.Y) + { + if (TextOffset == 0) + { + global.cursor_x = TextR.X; + + break; + } + + TextOffset -= TextR.W; + global.cursor_y = TextR.Y; + } + else + { + global.cursor_y--; + } + } break; + + // Move down + case TB_KEY_CTRL_N: + case TB_KEY_ARROW_DOWN: + { + if (TextPos + TextR.W > TextLen) + { + // Put the cursor on the last character + global.cursor_x = TextR.X + (TextLen - TextOffset) % (TextR.W); + global.cursor_y = TextR.Y + (TextLen - TextOffset) / TextR.W; + + // If cursor ended 1 line under the bottom line this means that the text + // needs to be scrolled. + if (global.cursor_y == TextR.Y + TextR.H) + { + TextOffset += TextR.W; + global.cursor_y--; + } + + break; + } + + if (global.cursor_y == TextR.Y + TextR.H - 1) + { + TextOffset += TextR.W; + } + else + { + global.cursor_y++; + } + } break; + default: + { + Result = 0; + } + } + + *TextLenPtr = TextLen; + *TextOffsetPtr = TextOffset; + + return Result; +} + +// Draws characters from Text fitting in the TextR rectangle. +// InputLen is the amount of characters in Text. +// +// NOTE: TextR is always filled, when not enough characters in Input it will uses spaces instead. +// This makes it easy to update the textbox by recalling this function. +void +DrawTextBox(rect TextR, wchar_t *Text, u32 TextLen) +{ + // Draw the text right of the cursor + // NOTE: the cursor is assumed to be in the box + Assert(IsInRect(TextR, global.cursor_x, global.cursor_y)); + s32 AtX = TextR.X, AtY = TextR.Y; + u32 At = 0; + while (AtY < TextR.Y + TextR.H) + { + if (At < TextLen) + { + tb_printf(AtX++, AtY, 0, 0, "%lc", Text[At++]); + } + else + { + tb_printf(AtX++, AtY, 0, 0, " "); + } + + if (AtX == TextR.X + TextR.W) { AtY++; AtX = TextR.X; } + } +} + +// NOTE: To ensure that the text looks the same even when scrolling it you must provide the whole text, +// wrap the whole text and the only show the portion that can fit in the Text Rectangle. + +// When line will exceed width break the word on the next line. This is done by looking backwards +// for whitespace from TextR.W width. When a whitespace is found text is wrapped on the next line. +// TODO: this does not work yet. +void +DrawTextBoxWrapped(rect TextR, wchar_t *Text, u32 TextLen) +{ + if (TextLen <= TextR.W) + { + tb_printf(TextR.X, TextR.Y, 0, 0, "%ls", Text); + tb_present(); + global.cursor_x = TextR.X + TextLen; + global.cursor_y = TextR.Y; + return; + } + + u32 SearchIndex = TextR.W; + u32 PrevIndex = 0; + u32 Y = TextR.Y; + + while (SearchIndex < TextLen) + { + while (Text[SearchIndex] != ' ') + { + SearchIndex--; + if (SearchIndex == PrevIndex) + { + SearchIndex += TextR.W; + break; + } + } + + // Wrap + wchar_t BreakChar = Text[SearchIndex]; + Text[SearchIndex] = 0; + tb_printf(TextR.X, Y, 0, 0, "%ls", Text + PrevIndex); + tb_present(); + Text[SearchIndex] = BreakChar; + + if (Y + 1 == TextR.Y + TextR.H) + { + global.cursor_y = Y; + global.cursor_x = TextR.X + (SearchIndex - PrevIndex); + return; + } + Y++; + + if (BreakChar == L' ') + { + SearchIndex++; + } + + PrevIndex = SearchIndex; + SearchIndex += TextR.W; + } + + // This happens when SearchIndex exceeds TextLen but there is still some + // text left to print. We can assume that the text will fit because otherwise it would have + // been wrapped a second time and the loop would have returned. + tb_printf(TextR.X, Y, 0, 0, "%ls", Text + PrevIndex); + // NOTE: this sets the cursor position correctly + + global.cursor_y = Y; + global.cursor_x = TextR.X + TextLen - PrevIndex; +} + +#endif // UI_H + +// Check if coordinate (X,Y) is in rect boundaries +bool +IsInRect(rect Rect, s32 X, s32 Y) +{ + if ((X >= Rect.X && X <= Rect.X + Rect.W) && + (Y >= Rect.Y && Y <= Rect.Y + Rect.H)) return true; + return false; +} + // Return True if ch is whitespace -Bool +bool is_whitespace(u32 ch) { if (ch == L' ') - return True; - return False; + return true; + return false; } // Return True if ch is a supported markdown markup character // TODO: tilde -Bool +bool is_markdown(u32 ch) { if (ch == L'_' || ch == L'*') - return True; - return False; + return true; + return false; } // Print `Text`, `Len` characters long with markdown @@ -85,8 +606,8 @@ 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); + Assert(XLimit > 0); + Assert(YLimit > 0); u32 i = XLimit; u32 PrevI = 0; @@ -148,8 +669,8 @@ tb_print_wrapped_with_markdown(u32 XOffset, u32 YOffset, u32 fg, u32 bg, { XLimit -= XOffset; YLimit -= YOffset; - assert(YLimit > 0); - assert(XLimit > 0); + Assert(YLimit > 0); + Assert(XLimit > 0); u32 TextIndex = XLimit; u32 PrevTextIndex = 0; @@ -201,8 +722,8 @@ tb_print_wrapped_with_markdown(u32 XOffset, u32 YOffset, u32 fg, u32 bg, } tb_printf(X++, Y, fg, bg, "%lc", Text[TextIndex]); } - assert(WrapPositionsIndex == WrapPositionsLen); - assert(MDFormat.Len == MDFormatOptionsIndex); + Assert(WrapPositionsIndex == WrapPositionsLen); + Assert(MDFormat.Len == MDFormatOptionsIndex); return WrapPositionsLen + 1; } @@ -296,186 +817,3 @@ preprocess_markdown(Arena* ScratchArena, wchar_t* Text, u32 Len) 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 -#ifdef MAX_INPUT_LEN - 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); -#else - tb_printf(BoxX + 1, BoxY, 0, 0, "%d/%d [%dx%d] %dx%d %d (%d,%d)+(%d,%d)", - TextLen, TextLen, - BoxWidth, BoxHeight, - TextWidth, TextHeight, - TextOffset, - TextX, TextY, - XOffset, YOffset); - -#endif -#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; - } - } -} -- cgit v1.2.3