2021-09-28 13:10:13 +00:00
|
|
|
#include "menu.h"
|
|
|
|
|
|
|
|
#include <gui/elements.h>
|
2022-10-28 14:08:50 +00:00
|
|
|
#include <assets_icons.h>
|
2021-09-28 13:10:13 +00:00
|
|
|
#include <furi.h>
|
2023-02-07 16:33:05 +00:00
|
|
|
#include <m-array.h>
|
2021-09-28 13:10:13 +00:00
|
|
|
|
|
|
|
struct Menu {
|
|
|
|
View* view;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const char* label;
|
|
|
|
IconAnimation* icon;
|
|
|
|
uint32_t index;
|
|
|
|
MenuItemCallback callback;
|
|
|
|
void* callback_context;
|
|
|
|
} MenuItem;
|
|
|
|
|
|
|
|
ARRAY_DEF(MenuItemArray, MenuItem, M_POD_OPLIST);
|
|
|
|
|
2021-12-15 12:23:16 +00:00
|
|
|
#define M_OPL_MenuItemArray_t() ARRAY_OPLIST(MenuItemArray, M_POD_OPLIST)
|
|
|
|
|
2021-09-28 13:10:13 +00:00
|
|
|
typedef struct {
|
|
|
|
MenuItemArray_t items;
|
2021-10-02 17:00:56 +00:00
|
|
|
size_t position;
|
2021-09-28 13:10:13 +00:00
|
|
|
} MenuModel;
|
|
|
|
|
|
|
|
static void menu_process_up(Menu* menu);
|
|
|
|
static void menu_process_down(Menu* menu);
|
|
|
|
static void menu_process_ok(Menu* menu);
|
|
|
|
|
|
|
|
static void menu_draw_callback(Canvas* canvas, void* _model) {
|
|
|
|
MenuModel* model = _model;
|
|
|
|
|
|
|
|
canvas_clear(canvas);
|
|
|
|
|
2021-10-02 17:00:56 +00:00
|
|
|
size_t position = model->position;
|
2021-09-28 13:10:13 +00:00
|
|
|
size_t items_count = MenuItemArray_size(model->items);
|
|
|
|
if(items_count) {
|
|
|
|
MenuItem* item;
|
|
|
|
size_t shift_position;
|
|
|
|
// First line
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
|
|
|
shift_position = (0 + position + items_count - 1) % items_count;
|
|
|
|
item = MenuItemArray_get(model->items, shift_position);
|
|
|
|
if(item->icon) {
|
|
|
|
canvas_draw_icon_animation(canvas, 4, 3, item->icon);
|
|
|
|
}
|
|
|
|
canvas_draw_str(canvas, 22, 14, item->label);
|
|
|
|
// Second line main
|
|
|
|
canvas_set_font(canvas, FontPrimary);
|
|
|
|
shift_position = (1 + position + items_count - 1) % items_count;
|
|
|
|
item = MenuItemArray_get(model->items, shift_position);
|
|
|
|
if(item->icon) {
|
|
|
|
canvas_draw_icon_animation(canvas, 4, 25, item->icon);
|
|
|
|
}
|
|
|
|
canvas_draw_str(canvas, 22, 36, item->label);
|
|
|
|
// Third line
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
|
|
|
shift_position = (2 + position + items_count - 1) % items_count;
|
|
|
|
item = MenuItemArray_get(model->items, shift_position);
|
|
|
|
if(item->icon) {
|
|
|
|
canvas_draw_icon_animation(canvas, 4, 47, item->icon);
|
|
|
|
}
|
|
|
|
canvas_draw_str(canvas, 22, 58, item->label);
|
|
|
|
// Frame and scrollbar
|
|
|
|
elements_frame(canvas, 0, 21, 128 - 5, 21);
|
|
|
|
elements_scrollbar(canvas, position, items_count);
|
|
|
|
} else {
|
|
|
|
canvas_draw_str(canvas, 2, 32, "Empty");
|
|
|
|
elements_scrollbar(canvas, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool menu_input_callback(InputEvent* event, void* context) {
|
|
|
|
Menu* menu = context;
|
|
|
|
bool consumed = false;
|
|
|
|
|
|
|
|
if(event->type == InputTypeShort) {
|
|
|
|
if(event->key == InputKeyUp) {
|
|
|
|
consumed = true;
|
|
|
|
menu_process_up(menu);
|
|
|
|
} else if(event->key == InputKeyDown) {
|
|
|
|
consumed = true;
|
|
|
|
menu_process_down(menu);
|
|
|
|
} else if(event->key == InputKeyOk) {
|
|
|
|
consumed = true;
|
|
|
|
menu_process_ok(menu);
|
|
|
|
}
|
2022-01-21 16:55:44 +00:00
|
|
|
} else if(event->type == InputTypeRepeat) {
|
|
|
|
if(event->key == InputKeyUp) {
|
|
|
|
consumed = true;
|
|
|
|
menu_process_up(menu);
|
|
|
|
} else if(event->key == InputKeyDown) {
|
|
|
|
consumed = true;
|
|
|
|
menu_process_down(menu);
|
|
|
|
}
|
2021-09-28 13:10:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
2021-10-02 17:00:56 +00:00
|
|
|
static void menu_enter(void* context) {
|
|
|
|
Menu* menu = context;
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
menu->view,
|
|
|
|
MenuModel * model,
|
|
|
|
{
|
2021-10-02 17:00:56 +00:00
|
|
|
MenuItem* item = MenuItemArray_get(model->items, model->position);
|
|
|
|
if(item && item->icon) {
|
|
|
|
icon_animation_start(item->icon);
|
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
false);
|
2021-10-02 17:00:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void menu_exit(void* context) {
|
|
|
|
Menu* menu = context;
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
menu->view,
|
|
|
|
MenuModel * model,
|
|
|
|
{
|
2021-10-02 17:00:56 +00:00
|
|
|
MenuItem* item = MenuItemArray_get(model->items, model->position);
|
|
|
|
if(item && item->icon) {
|
|
|
|
icon_animation_stop(item->icon);
|
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
false);
|
2021-10-02 17:00:56 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 13:10:13 +00:00
|
|
|
Menu* menu_alloc() {
|
2022-02-18 19:53:46 +00:00
|
|
|
Menu* menu = malloc(sizeof(Menu));
|
2021-09-28 13:10:13 +00:00
|
|
|
menu->view = view_alloc(menu->view);
|
|
|
|
view_set_context(menu->view, menu);
|
|
|
|
view_allocate_model(menu->view, ViewModelTypeLocking, sizeof(MenuModel));
|
|
|
|
view_set_draw_callback(menu->view, menu_draw_callback);
|
|
|
|
view_set_input_callback(menu->view, menu_input_callback);
|
2021-10-02 17:00:56 +00:00
|
|
|
view_set_enter_callback(menu->view, menu_enter);
|
|
|
|
view_set_exit_callback(menu->view, menu_exit);
|
2021-09-28 13:10:13 +00:00
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
menu->view,
|
|
|
|
MenuModel * model,
|
|
|
|
{
|
2021-09-28 13:10:13 +00:00
|
|
|
MenuItemArray_init(model->items);
|
|
|
|
model->position = 0;
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2021-09-28 13:10:13 +00:00
|
|
|
|
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
void menu_free(Menu* menu) {
|
|
|
|
furi_assert(menu);
|
2022-01-21 17:32:03 +00:00
|
|
|
menu_reset(menu);
|
2021-09-28 13:10:13 +00:00
|
|
|
view_free(menu->view);
|
|
|
|
free(menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
View* menu_get_view(Menu* menu) {
|
|
|
|
furi_assert(menu);
|
|
|
|
return (menu->view);
|
|
|
|
}
|
|
|
|
|
|
|
|
void menu_add_item(
|
|
|
|
Menu* menu,
|
|
|
|
const char* label,
|
2021-12-15 12:23:16 +00:00
|
|
|
const Icon* icon,
|
2021-09-28 13:10:13 +00:00
|
|
|
uint32_t index,
|
|
|
|
MenuItemCallback callback,
|
|
|
|
void* context) {
|
|
|
|
furi_assert(menu);
|
|
|
|
furi_assert(label);
|
|
|
|
|
|
|
|
MenuItem* item = NULL;
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
menu->view,
|
|
|
|
MenuModel * model,
|
|
|
|
{
|
2021-09-28 13:10:13 +00:00
|
|
|
item = MenuItemArray_push_new(model->items);
|
|
|
|
item->label = label;
|
2021-12-15 12:23:16 +00:00
|
|
|
item->icon = icon ? icon_animation_alloc(icon) : icon_animation_alloc(&A_Plugins_14);
|
2021-10-02 17:00:56 +00:00
|
|
|
view_tie_icon_animation(menu->view, item->icon);
|
2021-09-28 13:10:13 +00:00
|
|
|
item->index = index;
|
|
|
|
item->callback = callback;
|
|
|
|
item->callback_context = context;
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2021-09-28 13:10:13 +00:00
|
|
|
}
|
|
|
|
|
2022-01-21 17:32:03 +00:00
|
|
|
void menu_reset(Menu* menu) {
|
2021-09-28 13:10:13 +00:00
|
|
|
furi_assert(menu);
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
menu->view,
|
|
|
|
MenuModel * model,
|
|
|
|
{
|
2021-12-15 12:23:16 +00:00
|
|
|
for
|
|
|
|
M_EACH(item, model->items, MenuItemArray_t) {
|
|
|
|
icon_animation_stop(item->icon);
|
|
|
|
icon_animation_free(item->icon);
|
|
|
|
}
|
|
|
|
|
2021-11-15 17:09:40 +00:00
|
|
|
MenuItemArray_reset(model->items);
|
2021-09-28 13:10:13 +00:00
|
|
|
model->position = 0;
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2021-09-28 13:10:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 04:18:28 +00:00
|
|
|
void menu_set_selected_item(Menu* menu, uint32_t index) {
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
menu->view,
|
|
|
|
MenuModel * model,
|
|
|
|
{
|
|
|
|
if(index < MenuItemArray_size(model->items)) {
|
|
|
|
model->position = index;
|
2021-09-29 04:18:28 +00:00
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2021-09-29 04:18:28 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 13:10:13 +00:00
|
|
|
static void menu_process_up(Menu* menu) {
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
menu->view,
|
|
|
|
MenuModel * model,
|
|
|
|
{
|
2021-10-02 17:00:56 +00:00
|
|
|
MenuItem* item = MenuItemArray_get(model->items, model->position);
|
|
|
|
if(item && item->icon) {
|
|
|
|
icon_animation_stop(item->icon);
|
|
|
|
}
|
|
|
|
|
2021-09-28 13:10:13 +00:00
|
|
|
if(model->position > 0) {
|
|
|
|
model->position--;
|
|
|
|
} else {
|
|
|
|
model->position = MenuItemArray_size(model->items) - 1;
|
|
|
|
}
|
2021-10-02 17:00:56 +00:00
|
|
|
|
|
|
|
item = MenuItemArray_get(model->items, model->position);
|
|
|
|
if(item && item->icon) {
|
|
|
|
icon_animation_start(item->icon);
|
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2021-09-28 13:10:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void menu_process_down(Menu* menu) {
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
menu->view,
|
|
|
|
MenuModel * model,
|
|
|
|
{
|
2021-10-02 17:00:56 +00:00
|
|
|
MenuItem* item = MenuItemArray_get(model->items, model->position);
|
|
|
|
if(item && item->icon) {
|
|
|
|
icon_animation_stop(item->icon);
|
|
|
|
}
|
|
|
|
|
2021-09-28 13:10:13 +00:00
|
|
|
if(model->position < MenuItemArray_size(model->items) - 1) {
|
|
|
|
model->position++;
|
|
|
|
} else {
|
|
|
|
model->position = 0;
|
|
|
|
}
|
2021-10-02 17:00:56 +00:00
|
|
|
|
|
|
|
item = MenuItemArray_get(model->items, model->position);
|
|
|
|
if(item && item->icon) {
|
|
|
|
icon_animation_start(item->icon);
|
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2021-09-28 13:10:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void menu_process_ok(Menu* menu) {
|
|
|
|
MenuItem* item = NULL;
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
menu->view,
|
|
|
|
MenuModel * model,
|
|
|
|
{
|
2021-09-28 13:10:13 +00:00
|
|
|
if(model->position < MenuItemArray_size(model->items)) {
|
|
|
|
item = MenuItemArray_get(model->items, model->position);
|
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2021-09-28 13:10:13 +00:00
|
|
|
if(item && item->callback) {
|
|
|
|
item->callback(item->callback_context, item->index);
|
|
|
|
}
|
|
|
|
}
|