From b08c7c9ce6960c5fc80e1eebe6ff39522909b419 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sat, 8 Jun 2024 15:58:18 +0200 Subject: [PATCH] Small mouse fix --- examples/text_editor/main.lc | 6 ++--- examples/text_editor/prototype.lc | 2 ++ examples/text_editor/text_editor.lc | 38 +++++++++++++++++------------ 3 files changed, 27 insertions(+), 19 deletions(-) create mode 100644 examples/text_editor/prototype.lc diff --git a/examples/text_editor/main.lc b/examples/text_editor/main.lc index 738e528..6691b1e 100644 --- a/examples/text_editor/main.lc +++ b/examples/text_editor/main.lc @@ -81,11 +81,9 @@ main :: proc(): int { ClearBackground(RAYWHITE); if sandbox_chosen == SANDBOX_TEXT_EDITOR { - if !TeInited InitTextEditor(font, font_size, font_spacing); - ComputeWindowRects(screen_rect); - UpdateAndDrawWindows(font, font_size); + UpdateTextEditor(screen_rect, font, font_size, font_spacing); } else if sandbox_chosen == SANDBOX_PROTOTYPE { - + UpdatePrototype(screen_rect); } DrawRect(top_bar_original, LIGHTGRAY); diff --git a/examples/text_editor/prototype.lc b/examples/text_editor/prototype.lc new file mode 100644 index 0000000..b5fe0aa --- /dev/null +++ b/examples/text_editor/prototype.lc @@ -0,0 +1,2 @@ +UpdatePrototype :: proc(rect: Rect2P) { +} \ No newline at end of file diff --git a/examples/text_editor/text_editor.lc b/examples/text_editor/text_editor.lc index a66199e..0d6dff8 100644 --- a/examples/text_editor/text_editor.lc +++ b/examples/text_editor/text_editor.lc @@ -8,23 +8,27 @@ FocusedWindow: *Window; WindowStack: [8]Window; WindowStackCount: int; -InitTextEditor :: proc(font: Font, font_size: float, font_spacing: float) { - TeInited = true; - TeFont = font; - TeFontSpacing = font_spacing; - TeFontSize = font_size; +UpdateTextEditor :: proc(rect: Rect2P, font: Font, font_size: float, font_spacing: float) { + if !TeInited { + TeInited = true; + TeFont = font; + TeFontSpacing = font_spacing; + TeFontSize = font_size; - glyph_info: GlyphInfo = GetGlyphInfo(TeFont, 'A'); - size := MeasureTextEx(TeFont, "A", TeFontSize, TeFontSpacing); - Monosize = {:float(glyph_info.image.width), size.y}; + glyph_info: GlyphInfo = GetGlyphInfo(TeFont, 'A'); + size := MeasureTextEx(TeFont, "A", TeFontSize, TeFontSpacing); + Monosize = {:float(glyph_info.image.width), size.y}; - file_content := LoadFileText("C:/Work/language/examples/text_editor/main.lc"); - AddText(&TeBuffer, {file_content, :int(strlen(file_content))}); - UnloadFileText(file_content); + file_content := LoadFileText("C:/Work/language/examples/text_editor/main.lc"); + AddText(&TeBuffer, {file_content, :int(strlen(file_content))}); + UnloadFileText(file_content); - AddWindow({buffer = &TeBuffer}); - AddWindow({buffer = &TeBuffer}); - FocusedWindow = &WindowStack[0]; + AddWindow({buffer = &TeBuffer}); + AddWindow({buffer = &TeBuffer}); + FocusedWindow = &WindowStack[0]; + } + ComputeWindowRects(rect); + UpdateAndDrawWindows(font, font_size); } Window :: struct { @@ -32,6 +36,7 @@ Window :: struct { cursor: Selection; scroll: Vector2; mouse_scrolling: bool; + mouse_selecting: bool; rect: Rect2P; } @@ -330,6 +335,7 @@ UpdateAndDrawWindow :: proc(w: *Window, font: Font, font_size: float) { if CheckCollisionPointRec(mouse_p, Rect2PToRectangle(w.rect)) { if CheckCollisionPointRec(mouse_p, Rect2PToRectangle(text_window_rect)) && !w.mouse_scrolling { if IsMouseButtonPressed(MOUSE_BUTTON_LEFT) { + w.mouse_selecting = true; p := Vector2Add(mouse_p, w.scroll); p = Vector2Subtract(p, text_window_rect.min); p = Vector2Divide(p, Monosize); @@ -348,7 +354,7 @@ UpdateAndDrawWindow :: proc(w: *Window, font: Font, font_size: float) { } } - if IsMouseButtonDown(MOUSE_BUTTON_LEFT) { + if w.mouse_selecting && IsMouseButtonDown(MOUSE_BUTTON_LEFT) { SetMouseCursor(MOUSE_CURSOR_DEFAULT); p := Vector2Add(mouse_p, w.scroll); p = Vector2Subtract(p, text_window_rect.min); @@ -356,6 +362,8 @@ UpdateAndDrawWindow :: proc(w: *Window, font: Font, font_size: float) { x := :int(floorf(p.x)); y := :int(floorf(p.y)); w.cursor.b = CalculatePosFromVisualPos(w.buffer, {x, y}); + } else { + w.mouse_selecting = false; } if w.mouse_scrolling {