diff options
Diffstat (limited to 'core.c')
-rw-r--r-- | core.c | 34 |
1 files changed, 28 insertions, 6 deletions
@@ -36,10 +36,9 @@ static void add_scale(double amount) { pos_y -= mouse_y / scale; } -static uintptr_t current_addr() { - return to_addr( - pos_x + screen_width / 2 * scale, - pos_y + screen_width / 2 * scale); +static void world_pos(double *x, double *y) { + *x = *x / scale + pos_x - 0.5; + *y = *y / scale + pos_y - 0.5; } static void goto_addr(uintptr_t addr) { @@ -100,6 +99,11 @@ static void deinit_sdl() { SDL_Quit(); } +static bool held(SDL_Keycode k) { + const uint8_t *state = SDL_GetKeyboardState(NULL); + return state[SDL_GetScancodeFromKey(k)]; +} + static void handle_events() { bool refresh = false; SDL_Event e; @@ -122,15 +126,33 @@ static void handle_events() { break; } break; - case SDL_MOUSEMOTION: - if (e.motion.state & SDL_BUTTON_RMASK) { + case SDL_MOUSEMOTION:; + uint32_t state = e.motion.state; + if (state & SDL_BUTTON_MMASK || + (state & SDL_BUTTON_RMASK && held(SDLK_LSHIFT))) { pos_x -= (double) e.motion.xrel / scale; pos_y -= (double) e.motion.yrel / scale; refresh = true; + } else if (state & (SDL_BUTTON_LMASK | SDL_BUTTON_RMASK)) { + double x1 = e.motion.x - e.motion.xrel; + double y1 = e.motion.y - e.motion.yrel; + double x2 = e.motion.x; double y2 = e.motion.y; + world_pos(&x1, &y1); world_pos(&x2, &y2); + pencil(viewer, + x1, y1, x2, y2, !(state & SDL_BUTTON_RMASK)); + refresh = true; } mouse_x = e.motion.x; mouse_y = e.motion.y; break; + case SDL_MOUSEBUTTONDOWN:; + double x = e.button.x; double y = e.button.y; + world_pos(&x, &y); + if (e.button.button == SDL_BUTTON_LEFT) + pencil(viewer, x, y, x, y, true); + else if (e.button.button == SDL_BUTTON_RIGHT) + pencil(viewer, x, y, x, y, false); + break; case SDL_MOUSEWHEEL: if (e.wheel.y == 0) break; add_scale((double) e.wheel.y / 2); |