From 28a93ad6931f03114ebfaf0c6635fab96ffea186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Mon, 27 Oct 2014 17:20:46 +0100 Subject: [PATCH] Refactor the input part And add support for moving the canvas with cursor keys. --- autistdraw.c | 99 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 65 insertions(+), 34 deletions(-) diff --git a/autistdraw.c b/autistdraw.c index 258f24b..05fdfd9 100644 --- a/autistdraw.c +++ b/autistdraw.c @@ -657,56 +657,39 @@ export_irc (app_context_t *app) // --- Event handlers ---------------------------------------------------------- -static bool -on_key (app_context_t *app, termo_key_t *key) +static void +move_canvas (app_context_t *app, int x, int y) { - if (key->type == TERMO_TYPE_KEYSYM && key->code.sym == TERMO_SYM_ESCAPE) - return false; + app->corner_x += x; + app->corner_y += y; - if (key->type == TERMO_TYPE_KEY - && (key->modifiers & TERMO_KEYMOD_CTRL) - && (key->code.codepoint == 'C' || key->code.codepoint == 'c')) - return false; + app->center_x += x; + app->center_y += y; - if (key->type == TERMO_TYPE_KEY && key->code.codepoint == 'e') - { - export_ansi (app); - return true; - } - - if (key->type == TERMO_TYPE_KEY && key->code.codepoint == 'E') - { - export_irc (app); - return true; - } - - if (key->type != TERMO_TYPE_MOUSE) - return true; + redraw_canvas (app); +} +static void +on_mouse (app_context_t *app, termo_key_t *key) +{ int screen_y, screen_x, button; termo_mouse_event_t event; termo_interpret_mouse (app->tk, key, &event, &button, &screen_y, &screen_x); if (event != TERMO_MOUSE_PRESS && event != TERMO_MOUSE_DRAG) - return true; + return; // Middle mouse button, or Ctrl + left mouse button, moves the canvas if (button == 2 || (button == 1 && key->modifiers == TERMO_KEYMOD_CTRL)) { if (event == TERMO_MOUSE_DRAG) - { - app->corner_x += app->move_saved_x - screen_x; - app->corner_y += app->move_saved_y - screen_y; - - app->center_x += app->move_saved_x - screen_x; - app->center_y += app->move_saved_y - screen_y; - - redraw_canvas (app); - } + move_canvas (app, + app->move_saved_x - screen_x, + app->move_saved_y - screen_y); app->move_saved_x = screen_x; app->move_saved_y = screen_y; - return true; + return; } uint8_t *color; @@ -715,7 +698,7 @@ on_key (app_context_t *app, termo_key_t *key) else if (button == 3) color = &app->current_color_right; else - return true; + return; int canvas_x = app->corner_x + screen_x; int canvas_y = app->corner_y + screen_y - TOP_BAR_CUTOFF; @@ -732,6 +715,54 @@ on_key (app_context_t *app, termo_key_t *key) int pair = (float) screen_x / COLS * PALETTE_WIDTH; *color = pair + (screen_y - 1) * PALETTE_WIDTH; } +} + +static bool +on_key (app_context_t *app, termo_key_t *key) +{ + if (key->type == TERMO_TYPE_KEYSYM) + { + if (key->code.sym == TERMO_SYM_ESCAPE) + return false; + + if (key->modifiers) + return true; + + switch (key->code.sym) + { + case TERMO_SYM_UP: + move_canvas (app, 0, -1); + break; + case TERMO_SYM_DOWN: + move_canvas (app, 0, 1); + break; + case TERMO_SYM_LEFT: + move_canvas (app, -1, 0); + break; + case TERMO_SYM_RIGHT: + move_canvas (app, 1, 0); + default: + break; + } + return true; + } + + if (key->type == TERMO_TYPE_KEY) + { + if ((key->modifiers & TERMO_KEYMOD_CTRL) + && (key->code.codepoint == 'C' || key->code.codepoint == 'c')) + return false; + + if (key->modifiers) + return true; + + if (key->code.codepoint == 'e') export_ansi (app); + if (key->code.codepoint == 'E') export_irc (app); + return true; + } + + if (key->type == TERMO_TYPE_MOUSE) + on_mouse (app, key); return true; }