From 3ada204ef587dc93331904fdd30636bb46c225a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Sun, 20 Jun 2021 21:42:26 +0200 Subject: [PATCH] refactor: move text view commands to text viewer --- text-viewer/include/gui_component_text_view.h | 1 - text-viewer/src/gui_component_text_view.c | 59 ----------------- text-viewer/src/text_viewer.c | 64 ++++++++++++++++++- 3 files changed, 63 insertions(+), 61 deletions(-) diff --git a/text-viewer/include/gui_component_text_view.h b/text-viewer/include/gui_component_text_view.h index b74d06914cdfceee3a32133ce6cad73ee6048751..961bbf44693956c951f94f5225dcdda71dafd7fa 100644 --- a/text-viewer/include/gui_component_text_view.h +++ b/text-viewer/include/gui_component_text_view.h @@ -20,7 +20,6 @@ typedef struct { multiline_text_t *gui_multiline_text_create(font_t *font, display_pixel_t color, char *text); component_t gui_text_view_create(gui_t *gui, multiline_text_t *text, int16_t x, int16_t y); -void gui_text_view_register_commands(gui_t *gui, component_t *text_view); void gui_text_view_scroll(component_t *text_view, int16_t x, int16_t y); void gui_text_view_reset_scroll(component_t *text_view); diff --git a/text-viewer/src/gui_component_text_view.c b/text-viewer/src/gui_component_text_view.c index 25de1bec53de744cbd4f841570d96e2022aad96c..d3f8bbc2f7967a67e24b20638376aeedeb3ed762 100644 --- a/text-viewer/src/gui_component_text_view.c +++ b/text-viewer/src/gui_component_text_view.c @@ -65,65 +65,6 @@ multiline_text_t *gui_multiline_text_create(font_t *font, display_pixel_t color, return multiline_text; } -static void command_handler_move(void *state, direction_t direction, int amount) { - component_t *text_view = (component_t*)state; - if (text_view->focused) { - direction_move_xy(direction, (int32_t*)&text_view->x, (int32_t*)&text_view->y, -amount); - } -} - -static void command_handler_move_down(void *state, int amount) { - command_handler_move(state, DOWN, amount); -} - -static void command_handler_move_up(void *state, int amount) { - command_handler_move(state, UP, amount); -} - -static void command_handler_move_left(void *state, int amount) { - command_handler_move(state, LEFT, amount); -} -static void command_handler_move_right(void *state, int amount) { - command_handler_move(state, RIGHT, amount); -} -static void command_handler_reset(void *state, int amount) { - gui_text_view_reset_scroll((component_t*)state); -} - -component_t gui_text_view_create(gui_t *gui, multiline_text_t *text, int16_t x, int16_t y) { - component_t text_view = - gui_component_create(x, y, 1, 1, gui_text_view_render, gui_text_view_update); - text_view.state = text; - text_view.focusable = true; - - return text_view; -} - -void gui_text_view_register_commands(gui_t *gui, component_t *text_view) { - commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_LEFT, - command_handler_move_left, text_view); - commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_RIGHT, - command_handler_move_right, text_view); - commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_DOWN, - command_handler_move_down, text_view); - commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_UP, - command_handler_move_up, text_view); - commands_register(gui->commands, IN_KEYBOARD, 'r', - command_handler_reset, text_view); - - commands_register(gui->commands, IN_ENCODER_ROTATE, - ROTATION_ENCODER_HORIZONTAL, command_handler_move_right, - text_view); - - commands_register(gui->commands, IN_ENCODER_ROTATE, - ROTATION_ENCODER_VERTICAL, command_handler_move_down, - text_view); - - commands_register(gui->commands, IN_ENCODER_CLICK, - ROTATION_ENCODER_ZOOM, command_handler_reset, - text_view); -} - void gui_text_view_scroll(component_t *text_view, int16_t x, int16_t y) { text_view->x -= x; text_view->y -= y; diff --git a/text-viewer/src/text_viewer.c b/text-viewer/src/text_viewer.c index e60ec047591c3081c5e794248c8beaef843f275e..f6c093b1a2b1bbed10130fad17f16c18e7486794 100644 --- a/text-viewer/src/text_viewer.c +++ b/text-viewer/src/text_viewer.c @@ -9,6 +9,9 @@ #include #include "gui_component_text.h" #include "gui_component_text_view.h" +#include "direction.h" +#include "keyboard_const.h" +#include "rotation_const.h" #include #include @@ -132,6 +135,65 @@ static component_t *text_viewer_gui_add_text_view(text_viewer_t *text_viewer, co return gui_one_container_set_component(view_container, text_view); } +static void command_handler_move(void *state, direction_t direction, int amount) { + component_t *text_view = (component_t*)state; + if (text_view->focused) { + direction_move_xy(direction, (int32_t*)&text_view->x, (int32_t*)&text_view->y, -amount); + } +} + +static void command_handler_move_down(void *state, int amount) { + command_handler_move(state, DOWN, amount); +} + +static void command_handler_move_up(void *state, int amount) { + command_handler_move(state, UP, amount); +} + +static void command_handler_move_left(void *state, int amount) { + command_handler_move(state, LEFT, amount); +} +static void command_handler_move_right(void *state, int amount) { + command_handler_move(state, RIGHT, amount); +} +static void command_handler_reset(void *state, int amount) { + gui_text_view_reset_scroll((component_t*)state); +} + +component_t gui_text_view_create(gui_t *gui, multiline_text_t *text, int16_t x, int16_t y) { + component_t text_view = + gui_component_create(x, y, 1, 1, gui_text_view_render, gui_text_view_update); + text_view.state = text; + text_view.focusable = true; + + return text_view; +} + +void gui_text_view_register_commands(gui_t *gui, component_t *text_view) { + commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_LEFT, + command_handler_move_left, text_view); + commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_RIGHT, + command_handler_move_right, text_view); + commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_DOWN, + command_handler_move_down, text_view); + commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_UP, + command_handler_move_up, text_view); + commands_register(gui->commands, IN_KEYBOARD, 'r', + command_handler_reset, text_view); + + commands_register(gui->commands, IN_ENCODER_ROTATE, + ROTATION_ENCODER_HORIZONTAL, command_handler_move_right, + text_view); + + commands_register(gui->commands, IN_ENCODER_ROTATE, + ROTATION_ENCODER_VERTICAL, command_handler_move_down, + text_view); + + commands_register(gui->commands, IN_ENCODER_CLICK, + ROTATION_ENCODER_ZOOM, command_handler_reset, + text_view); +} + void text_viewer_start_loop(text_viewer_t *text_viewer) { command_t command_arr[20]; commands_t commands = @@ -161,7 +223,7 @@ void text_viewer_start_loop(text_viewer_t *text_viewer) { gui_set_active_window(&text_viewer->gui, &text_viewer_window); gui_text_view_register_commands(&text_viewer->gui, - text_view); // TODO: rethink this design + text_view); text_viewer_register_commands(text_viewer, &commands);