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

CTU-FEE-B0B35APO-Semestral-project/text-viewer/src/text_viewer_handlers.c -rw-r--r-- 5.3 KiB
7aa0ada9 — Rutherther chore: fix image in readme 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "text_viewer_handlers.h"
#include "direction.h"
#include "display_utils.h"
#include "gui_component_text_view.h"
#include "keyboard_const.h"
#include "rotation_const.h"

static void command_handler_move(void *state, direction_t direction,
                                 int amount) {
  component_t *text_view = (component_t *)state;
  if (text_view->focused) {
    int32_t x = 0;
    int32_t y = 0;
    direction_move_xy(direction, &x, &y, amount);
    gui_text_view_scroll(text_view, x, y);
  }
}

static void command_handler_jump(void *state, direction_t direction,
                                 int amount) {
  component_t *text_view = (component_t *)state;

  if (direction == LEFT || direction == RIGHT) {
    amount = DISPLAY_WIDTH * 0.6;
  } else {
    amount = DISPLAY_HEIGHT * 0.4;
  }

  int32_t x = 0;
  int32_t y = 0;
  direction_move_xy(direction, &x, &y, amount);
  gui_text_view_scroll(text_view, x, y);
}

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_jump_up(void *state, int amount) {
  command_handler_jump(state, UP, amount);
}

static void command_handler_jump_down(void *state, int amount) {
  command_handler_jump(state, DOWN, amount);
}

static void command_handler_jump_right(void *state, int amount) {
  command_handler_jump(state, RIGHT, amount);
}

static void command_handler_jump_left(void *state, int amount) {
  command_handler_jump(state, LEFT, amount);
}
static void command_handler_reset(void *state, int amount) {
  gui_text_view_reset_scroll((component_t *)state);
}

static void command_handler_full_scroll(void *state, int amount) {
  gui_text_view_full_scroll((component_t *)state);
}

static void command_handler_zoom_in(void *state, int amount) {
  component_t *component = (component_t *)state;
  multiline_text_t* text = (multiline_text_t*) (component)->state;
  uint16_t old_size = text->font->size;

  amount = amount > 1 ? 1 : -1;
  text->font->size += amount;
  if (text->font->size == 0) {
    text->font->size = 1;
  }

  component->y += amount * (component->y / (old_size + text->font->line_spacing));
}

static void command_handler_zoom_out(void *state, int amount) {
  command_handler_zoom_in(state, -amount);
}

static void command_handler_zoom_reset(void *state, int amount) {
  component_t *component = (component_t *)state;
  multiline_text_t *text = (multiline_text_t *)(component)->state;
  uint16_t old_size = text->font->size;
  text->font->size = text->font->font.height;


  amount = text->font->size - old_size;
  component->y +=
      amount * (component->y / (old_size + text->font->line_spacing));
}

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, KEYBOARD_JUMP_LEFT,
                    command_handler_jump_left, text_view);
  commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_JUMP_RIGHT,
                    command_handler_jump_right, text_view);
  commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_PAGE_DOWN,
                    command_handler_jump_down, text_view);
  commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_PAGE_UP,
                    command_handler_jump_up, text_view);

  commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_HOME, command_handler_reset,
                    text_view);

  commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_END,
                    command_handler_full_scroll, text_view);
  commands_register(gui->commands, IN_KEYBOARD, 'c',
                    command_handler_zoom_reset, text_view);

  commands_register(gui->commands, IN_ENCODER_ROTATE,
                    ROTATION_ENCODER_HORIZONTAL, command_handler_move_right,
                    text_view);

  commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_ZOOM_IN,
                    command_handler_zoom_in, text_view);
  commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_ZOOM_OUT,
                    command_handler_zoom_out, text_view);

  commands_register(gui->commands, IN_ENCODER_ROTATE, ROTATION_ENCODER_ZOOM,
                    command_handler_zoom_in, 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_VERTICAL,
                    command_handler_reset, text_view);
  commands_register(gui->commands, IN_ENCODER_CLICK, ROTATION_ENCODER_ZOOM,
                    command_handler_full_scroll, text_view);
}