From 143a88ac1d453a171049c51c601330020216ea8c Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Thu, 4 Jul 2024 10:02:51 +0200 Subject: [PATCH] String coloring --- src/text_editor/layout.cpp | 7 ++++- src/text_editor/main.cpp | 52 +++++++++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/text_editor/layout.cpp b/src/text_editor/layout.cpp index 47caa04..285c195 100644 --- a/src/text_editor/layout.cpp +++ b/src/text_editor/layout.cpp @@ -30,7 +30,12 @@ struct History { struct ColoredString { Range range; - Color color; + Color text_color; + Color highlight_background_color; + + uint8_t use_text_color; + uint8_t use_highlight_background_color; + uint8_t use_underline; }; struct Window { diff --git a/src/text_editor/main.cpp b/src/text_editor/main.cpp index cc2ab6c..440375a 100644 --- a/src/text_editor/main.cpp +++ b/src/text_editor/main.cpp @@ -76,9 +76,9 @@ int main() { InitArena(&FrameArena); InitArena(&PermArena); - float font_size = 64; + float font_size = 14; float font_spacing = 1; - Font font = LoadFontEx("C:/Windows/Fonts/times.ttf", (int)font_size, NULL, 250); + Font font = LoadFontEx("C:/Windows/Fonts/consola.ttf", (int)font_size, NULL, 250); Array windows = {}; { @@ -434,7 +434,20 @@ int main() { String s = GetString(focused_window->buffer); while (Seek(s, seek, &index)) { Range range = {base_index + index, base_index + index + seek.len}; - focused_window->colored_strings.add({range, RED}); + + ColoredString colored = {}; + colored.range = range; + colored.use_text_color = true; + colored.use_underline = true; + colored.text_color = RED; + focused_window->colored_strings.add(colored); + + colored = {}; + colored.range = range; + colored.use_highlight_background_color = true; + colored.highlight_background_color = {0, 50, 150, 50}; + focused_window->colored_strings.add(colored); + base_index += index + seek.len; s = s.skip(index + seek.len); } @@ -649,20 +662,35 @@ int main() { ForItem(col, visible_columns) { Rect2 rect = {col.rect.min + window.rect.min - window.scroll, col.rect.max + window.rect.min - window.scroll}; - Color color = BLACK; - For(window.colored_strings) { - if (InRange(col.pos, it.range)) { - color = it.color; - break; - } - } - if (col.codepoint == '\n') { DrawTextEx(font, "\\n", rect.min, font_size, font_spacing, GRAY); } else if (col.codepoint == '\0') { DrawTextEx(font, "\\0", rect.min, font_size, font_spacing, {255, 0, 0, 150}); } else if ((col.codepoint != ' ') && (col.codepoint != '\t')) { - DrawTextCodepoint(font, col.codepoint, rect.min, font_size, color); + + bool underline = false; + bool highlight = false; + Color highlight_color = MAGENTA; + Color text_color = BLACK; + + For(window.colored_strings) { + if (InRange(col.pos, it.range)) { + if (it.use_underline) underline = true; + if (it.use_text_color) text_color = it.text_color; + if (it.use_highlight_background_color) { + highlight = true; + highlight_color = it.highlight_background_color; + } + } + } + + if (highlight) DrawRectangleRec(ToRectangle(rect), highlight_color); + DrawTextCodepoint(font, col.codepoint, rect.min, font_size, text_color); + if (underline) { + Rect2 rect_copy = rect; + Rect2 r = CutBottom(&rect_copy, 1) - Vec2{0, window.font_size * 0.1f}; + DrawRectangleRec(ToRectangle(r), text_color); + } } }