From 833767d6e2fb12436e1fb2ed00caa2f6c7262885 Mon Sep 17 00:00:00 2001 From: martinfouilleul Date: Thu, 9 Feb 2023 09:50:53 +0100 Subject: [PATCH] move text with the mouse in perf_text example --- examples/perf_text/main.c | 35 +++++++++++++++++++++++++++++++++-- src/win32_app.c | 11 +++++++---- todo.txt | 1 + 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/examples/perf_text/main.c b/examples/perf_text/main.c index ab3722d..d2a7df2 100644 --- a/examples/perf_text/main.c +++ b/examples/perf_text/main.c @@ -112,6 +112,11 @@ int main() f64 frameTime = 0; + bool tracked = false; + vec2 trackPoint = {0}; + f32 startX = 10; + f32 startY = contentRect.h - lineHeight - 10; + while(!mp_should_quit()) { f64 startFrameTime = mp_get_time(MP_CLOCK_MONOTONIC); @@ -127,13 +132,39 @@ int main() mp_request_quit(); } break; + case MP_EVENT_MOUSE_BUTTON: + { + if(event.key.code == MP_MOUSE_LEFT) + { + if(event.key.action == MP_KEY_PRESS) + { + tracked = true; + vec2 mousePos = mp_input_mouse_position(); + trackPoint.x = mousePos.x - startX; + trackPoint.y = mousePos.y - startY; + } + else + { + tracked = false; + } + + } + } break; + default: break; } } - f32 textX = 10; - f32 textY = contentRect.h - lineHeight - 10; + if(tracked) + { + vec2 mousePos = mp_input_mouse_position(); + startX = mousePos.x - trackPoint.x; + startY = mousePos.y - trackPoint.y; + } + + f32 textX = startX; + f32 textY = startY; mg_surface_prepare(surface); mg_set_color_rgba(1, 1, 1, 1); diff --git a/src/win32_app.c b/src/win32_app.c index 1f1e960..27659e3 100644 --- a/src/win32_app.c +++ b/src/win32_app.c @@ -224,8 +224,8 @@ static void process_mouse_event(mp_window_data* window, mp_key_action action, mp mp_event event = {0}; event.window = mp_window_handle_from_ptr(window); event.type = MP_EVENT_MOUSE_BUTTON; - event.key.action = MP_KEY_PRESS; - event.key.code = MP_MOUSE_LEFT; + event.key.action = action; + event.key.code = button; event.key.mods = mp_get_mod_keys(); mp_queue_event(&event); @@ -378,11 +378,14 @@ LRESULT WinProc(HWND windowHandle, UINT message, WPARAM wParam, LPARAM lParam) RECT rect; GetClientRect(mpWindow->win32.hWnd, &rect); + u32 dpi = GetDpiForWindow(mpWindow->win32.hWnd); + f32 scaling = (f32)dpi/96.; + mp_event event = {0}; event.window = mp_window_handle_from_ptr(mpWindow); event.type = MP_EVENT_MOUSE_MOVE; - event.move.x = LOWORD(lParam); - event.move.y = rect.bottom - HIWORD(lParam); + event.move.x = LOWORD(lParam) / scaling; + event.move.y = (rect.bottom - HIWORD(lParam)) / scaling; if(__mpApp.inputState.mouse.posValid) { diff --git a/todo.txt b/todo.txt index ffd9320..2268a22 100644 --- a/todo.txt +++ b/todo.txt @@ -1,6 +1,7 @@ Overview -------- +[ ] Pan/Zoom on text example [ ] Clean+Fixes of canvas code and examples [ ] Make backend selection easier [ ] Investigate and fix artifact when seam is aligned to tile boundary?