~ruther/CTU-FEE-B0B35APO-Semestral-project

3ada204ef587dc93331904fdd30636bb46c225a9 — František Boháček 4 years ago 8e259f0
refactor: move text view commands to text viewer
M text-viewer/include/gui_component_text_view.h => text-viewer/include/gui_component_text_view.h +0 -1
@@ 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);

M text-viewer/src/gui_component_text_view.c => text-viewer/src/gui_component_text_view.c +0 -59
@@ 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;

M text-viewer/src/text_viewer.c => text-viewer/src/text_viewer.c +63 -1
@@ 9,6 9,9 @@
#include <stdlib.h>
#include "gui_component_text.h"
#include "gui_component_text_view.h"
#include "direction.h"
#include "keyboard_const.h"
#include "rotation_const.h"
#include <string.h>
#include <libgen.h>



@@ 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);