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

f9cc39006acbc2a7ea5bac86e175d021de92daca — František Boháček 3 years ago a655e13
feat: add gui list commands for scrolling and clicking items
2 files changed, 77 insertions(+), 0 deletions(-)

A file-browser/include/gui_list_commands.h
A file-browser/src/gui_list_commands.c
A file-browser/include/gui_list_commands.h => file-browser/include/gui_list_commands.h +20 -0
@@ 0,0 1,20 @@
#ifndef __GUI_LIST_COMMANDS_H__
#define __GUI_LIST_COMMANDS_H__

#include <stdint.h>
#include "gui.h"

typedef void (*gui_list_item_clicked_fn)(container_t *list_container, void *state, uint32_t selected_index);

typedef struct {
  gui_list_item_clicked_fn clicked;
  void *state;
  container_t *container;

  gui_t *gui;
  window_t *window;
} gui_list_command_state_t;

void gui_list_commands_register(commands_t *commands, gui_list_command_state_t *state);

#endif // __GUI_LIST_COMMANDS_H__

A file-browser/src/gui_list_commands.c => file-browser/src/gui_list_commands.c +57 -0
@@ 0,0 1,57 @@
#include "gui_list_commands.h"
#include "direction.h"
#include "gui.h"
#include "input.h"
#include "keyboard_const.h"
#include "rotation_const.h"

static void command_handler_gui_list_clicked(void *state, int amount) {
  gui_list_command_state_t *click_state = (gui_list_command_state_t*)state;
  if (amount > 1) {
    click_state->clicked(click_state->container, click_state->state,
                         gui_list_get_selected_index(click_state->container));
  }
}

static void command_handler_move(void *state, direction_t direction, int amount) {
  gui_list_command_state_t *click_state = (gui_list_command_state_t *)state;
  if (click_state->window == click_state->gui->active_window) {
    int32_t x = 0, y = 0;
    direction_move_xy(direction, &x, &y, amount);
    gui_list_scroll(click_state->container, (int16_t)x, (int16_t)y);
  }
}

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_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);
}

void gui_list_commands_register(commands_t *commands, gui_list_command_state_t *state) {
  commands_register(commands, IN_KEYBOARD, 13, command_handler_gui_list_clicked, state);

  commands_register(commands, IN_KEYBOARD, KEYBOARD_DOWN,
                    command_handler_move_down, state);
  commands_register(commands, IN_KEYBOARD, KEYBOARD_UP, command_handler_move_up,
                    state);
  commands_register(commands, IN_KEYBOARD, KEYBOARD_LEFT,
                    command_handler_move_left, state);
  commands_register(commands, IN_KEYBOARD, KEYBOARD_RIGHT,
                    command_handler_move_right, state);

  commands_register(commands, IN_ENCODER_ROTATE, ROTATION_ENCODER_HORIZONTAL,
                    command_handler_move_right, state);
  commands_register(commands, IN_ENCODER_ROTATE, ROTATION_ENCODER_VERTICAL,
                    command_handler_move_down, state);
}

Do not follow this link