2021-06-02 15:16:05 +00:00
|
|
|
#include "button_menu.h"
|
2023-02-07 16:33:05 +00:00
|
|
|
|
|
|
|
#include <gui/canvas.h>
|
|
|
|
#include <gui/elements.h>
|
|
|
|
#include <input/input.h>
|
|
|
|
|
2021-06-02 15:16:05 +00:00
|
|
|
#include <furi.h>
|
2022-10-28 14:08:50 +00:00
|
|
|
#include <assets_icons.h>
|
2021-06-02 15:16:05 +00:00
|
|
|
|
2023-02-07 16:33:05 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <m-array.h>
|
|
|
|
|
2021-06-02 15:16:05 +00:00
|
|
|
#define ITEM_FIRST_OFFSET 17
|
|
|
|
#define ITEM_NEXT_OFFSET 4
|
|
|
|
#define ITEM_HEIGHT 14
|
|
|
|
#define ITEM_WIDTH 64
|
|
|
|
#define BUTTONS_PER_SCREEN 6
|
|
|
|
|
|
|
|
struct ButtonMenuItem {
|
|
|
|
const char* label;
|
|
|
|
int32_t index;
|
|
|
|
ButtonMenuItemCallback callback;
|
|
|
|
ButtonMenuItemType type;
|
|
|
|
void* callback_context;
|
|
|
|
};
|
|
|
|
|
|
|
|
ARRAY_DEF(ButtonMenuItemArray, ButtonMenuItem, M_POD_OPLIST);
|
|
|
|
|
|
|
|
struct ButtonMenu {
|
|
|
|
View* view;
|
2021-08-11 17:51:06 +00:00
|
|
|
bool freeze_input;
|
2021-06-02 15:16:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
ButtonMenuItemArray_t items;
|
2022-12-10 18:05:52 +00:00
|
|
|
size_t position;
|
2021-06-02 15:16:05 +00:00
|
|
|
const char* header;
|
|
|
|
} ButtonMenuModel;
|
|
|
|
|
|
|
|
static void button_menu_draw_control_button(
|
|
|
|
Canvas* canvas,
|
|
|
|
uint8_t item_position,
|
|
|
|
const char* text,
|
|
|
|
bool selected) {
|
|
|
|
furi_assert(canvas);
|
|
|
|
furi_assert(text);
|
|
|
|
|
|
|
|
uint8_t item_x = 0;
|
|
|
|
uint8_t item_y = ITEM_FIRST_OFFSET + (item_position * (ITEM_HEIGHT + ITEM_NEXT_OFFSET));
|
|
|
|
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
|
|
|
|
if(selected) {
|
|
|
|
elements_slightly_rounded_box(canvas, item_x, item_y, ITEM_WIDTH, ITEM_HEIGHT);
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
}
|
|
|
|
canvas_draw_str_aligned(
|
|
|
|
canvas,
|
|
|
|
item_x + (ITEM_WIDTH / 2),
|
|
|
|
item_y + (ITEM_HEIGHT / 2),
|
|
|
|
AlignCenter,
|
|
|
|
AlignCenter,
|
|
|
|
text);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void button_menu_draw_common_button(
|
|
|
|
Canvas* canvas,
|
|
|
|
uint8_t item_position,
|
|
|
|
const char* text,
|
|
|
|
bool selected) {
|
|
|
|
furi_assert(canvas);
|
|
|
|
furi_assert(text);
|
|
|
|
|
|
|
|
uint8_t item_x = 0;
|
|
|
|
uint8_t item_y = ITEM_FIRST_OFFSET + (item_position * (ITEM_HEIGHT + ITEM_NEXT_OFFSET));
|
|
|
|
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
|
|
|
|
if(selected) {
|
|
|
|
canvas_draw_rbox(canvas, item_x, item_y, ITEM_WIDTH, ITEM_HEIGHT, 5);
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
} else {
|
|
|
|
canvas_draw_rframe(canvas, item_x, item_y, ITEM_WIDTH, ITEM_HEIGHT, 5);
|
|
|
|
}
|
2021-09-02 09:02:34 +00:00
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* disp_str;
|
|
|
|
disp_str = furi_string_alloc_set(text);
|
2021-09-02 09:02:34 +00:00
|
|
|
elements_string_fit_width(canvas, disp_str, ITEM_WIDTH - 6);
|
|
|
|
|
2021-06-02 15:16:05 +00:00
|
|
|
canvas_draw_str_aligned(
|
|
|
|
canvas,
|
|
|
|
item_x + (ITEM_WIDTH / 2),
|
|
|
|
item_y + (ITEM_HEIGHT / 2),
|
|
|
|
AlignCenter,
|
|
|
|
AlignCenter,
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_get_cstr(disp_str));
|
2021-09-02 09:02:34 +00:00
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(disp_str);
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void button_menu_view_draw_callback(Canvas* canvas, void* _model) {
|
|
|
|
furi_assert(canvas);
|
|
|
|
furi_assert(_model);
|
|
|
|
|
|
|
|
ButtonMenuModel* model = (ButtonMenuModel*)_model;
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
|
|
|
|
2022-12-10 18:05:52 +00:00
|
|
|
const size_t active_screen = model->position / BUTTONS_PER_SCREEN;
|
|
|
|
const size_t items_size = ButtonMenuItemArray_size(model->items);
|
|
|
|
const size_t max_screen = items_size ? (items_size - 1) / BUTTONS_PER_SCREEN : 0;
|
2021-06-02 15:16:05 +00:00
|
|
|
|
|
|
|
if(active_screen > 0) {
|
2022-02-25 15:22:58 +00:00
|
|
|
canvas_draw_icon(canvas, 28, 1, &I_InfraredArrowUp_4x8);
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(max_screen > active_screen) {
|
2022-02-25 15:22:58 +00:00
|
|
|
canvas_draw_icon(canvas, 28, 123, &I_InfraredArrowDown_4x8);
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 16:09:43 +00:00
|
|
|
if(model->header) {
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* disp_str;
|
|
|
|
disp_str = furi_string_alloc_set(model->header);
|
2022-02-16 16:09:43 +00:00
|
|
|
elements_string_fit_width(canvas, disp_str, ITEM_WIDTH - 6);
|
|
|
|
canvas_draw_str_aligned(
|
2022-10-05 15:15:23 +00:00
|
|
|
canvas, 32, 10, AlignCenter, AlignCenter, furi_string_get_cstr(disp_str));
|
|
|
|
furi_string_free(disp_str);
|
2022-02-16 16:09:43 +00:00
|
|
|
}
|
2021-06-02 15:16:05 +00:00
|
|
|
|
2022-12-10 18:05:52 +00:00
|
|
|
size_t item_position = 0;
|
|
|
|
ButtonMenuItemArray_it_t it;
|
|
|
|
|
2021-06-02 15:16:05 +00:00
|
|
|
for(ButtonMenuItemArray_it(it, model->items); !ButtonMenuItemArray_end_p(it);
|
|
|
|
ButtonMenuItemArray_next(it), ++item_position) {
|
|
|
|
if(active_screen == (item_position / BUTTONS_PER_SCREEN)) {
|
|
|
|
if(ButtonMenuItemArray_cref(it)->type == ButtonMenuItemTypeControl) {
|
|
|
|
button_menu_draw_control_button(
|
|
|
|
canvas,
|
|
|
|
item_position % BUTTONS_PER_SCREEN,
|
|
|
|
ButtonMenuItemArray_cref(it)->label,
|
|
|
|
(item_position == model->position));
|
|
|
|
} else if(ButtonMenuItemArray_cref(it)->type == ButtonMenuItemTypeCommon) {
|
|
|
|
button_menu_draw_common_button(
|
|
|
|
canvas,
|
|
|
|
item_position % BUTTONS_PER_SCREEN,
|
|
|
|
ButtonMenuItemArray_cref(it)->label,
|
|
|
|
(item_position == model->position));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void button_menu_process_up(ButtonMenu* button_menu) {
|
|
|
|
furi_assert(button_menu);
|
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
button_menu->view,
|
|
|
|
ButtonMenuModel * model,
|
|
|
|
{
|
2021-06-02 15:16:05 +00:00
|
|
|
if(model->position > 0) {
|
|
|
|
model->position--;
|
|
|
|
} else {
|
|
|
|
model->position = ButtonMenuItemArray_size(model->items) - 1;
|
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void button_menu_process_down(ButtonMenu* button_menu) {
|
|
|
|
furi_assert(button_menu);
|
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
button_menu->view,
|
|
|
|
ButtonMenuModel * model,
|
|
|
|
{
|
2021-06-02 15:16:05 +00:00
|
|
|
if(model->position < (ButtonMenuItemArray_size(model->items) - 1)) {
|
|
|
|
model->position++;
|
|
|
|
} else {
|
|
|
|
model->position = 0;
|
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 14:11:12 +00:00
|
|
|
static void button_menu_process_right(ButtonMenu* button_menu) {
|
|
|
|
furi_assert(button_menu);
|
|
|
|
|
|
|
|
with_view_model(
|
|
|
|
button_menu->view,
|
|
|
|
ButtonMenuModel * model,
|
|
|
|
{
|
|
|
|
if(ButtonMenuItemArray_size(model->items) > BUTTONS_PER_SCREEN) {
|
|
|
|
size_t position_candidate = model->position + BUTTONS_PER_SCREEN;
|
|
|
|
position_candidate -= position_candidate % BUTTONS_PER_SCREEN;
|
|
|
|
if(position_candidate < (ButtonMenuItemArray_size(model->items))) {
|
|
|
|
model->position = position_candidate;
|
|
|
|
} else {
|
|
|
|
model->position = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void button_menu_process_left(ButtonMenu* button_menu) {
|
|
|
|
furi_assert(button_menu);
|
|
|
|
|
|
|
|
with_view_model(
|
|
|
|
button_menu->view,
|
|
|
|
ButtonMenuModel * model,
|
|
|
|
{
|
|
|
|
if(ButtonMenuItemArray_size(model->items) > BUTTONS_PER_SCREEN) {
|
|
|
|
size_t position_candidate;
|
|
|
|
if(model->position < BUTTONS_PER_SCREEN) {
|
|
|
|
position_candidate = (ButtonMenuItemArray_size(model->items) - 1);
|
|
|
|
} else {
|
|
|
|
position_candidate = model->position - BUTTONS_PER_SCREEN;
|
|
|
|
};
|
|
|
|
position_candidate -= position_candidate % BUTTONS_PER_SCREEN;
|
|
|
|
model->position = position_candidate;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
|
2021-08-11 17:51:06 +00:00
|
|
|
static void button_menu_process_ok(ButtonMenu* button_menu, InputType type) {
|
2021-06-02 15:16:05 +00:00
|
|
|
furi_assert(button_menu);
|
|
|
|
|
|
|
|
ButtonMenuItem* item = NULL;
|
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
button_menu->view,
|
|
|
|
ButtonMenuModel * model,
|
|
|
|
{
|
2021-06-02 15:16:05 +00:00
|
|
|
if(model->position < (ButtonMenuItemArray_size(model->items))) {
|
|
|
|
item = ButtonMenuItemArray_get(model->items, model->position);
|
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
false);
|
2021-06-02 15:16:05 +00:00
|
|
|
|
2022-08-03 15:47:10 +00:00
|
|
|
if(item) {
|
|
|
|
if(item->type == ButtonMenuItemTypeControl) {
|
|
|
|
if(type == InputTypeShort) {
|
2022-12-10 18:05:52 +00:00
|
|
|
if(item->callback) {
|
2022-08-03 15:47:10 +00:00
|
|
|
item->callback(item->callback_context, item->index, type);
|
|
|
|
}
|
2021-08-11 17:51:06 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-03 15:47:10 +00:00
|
|
|
if(item->type == ButtonMenuItemTypeCommon) {
|
|
|
|
if((type == InputTypePress) || (type == InputTypeRelease)) {
|
2022-12-10 18:05:52 +00:00
|
|
|
if(item->callback) {
|
2022-08-03 15:47:10 +00:00
|
|
|
item->callback(item->callback_context, item->index, type);
|
|
|
|
}
|
2021-08-11 17:51:06 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool button_menu_view_input_callback(InputEvent* event, void* context) {
|
|
|
|
furi_assert(event);
|
|
|
|
|
|
|
|
ButtonMenu* button_menu = context;
|
|
|
|
bool consumed = false;
|
|
|
|
|
2021-08-11 17:51:06 +00:00
|
|
|
if(event->key == InputKeyOk) {
|
|
|
|
if((event->type == InputTypeRelease) || (event->type == InputTypePress)) {
|
|
|
|
consumed = true;
|
|
|
|
button_menu->freeze_input = (event->type == InputTypePress);
|
|
|
|
button_menu_process_ok(button_menu, event->type);
|
|
|
|
} else if(event->type == InputTypeShort) {
|
|
|
|
consumed = true;
|
|
|
|
button_menu_process_ok(button_menu, event->type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!button_menu->freeze_input &&
|
|
|
|
((event->type == InputTypeRepeat) || (event->type == InputTypeShort))) {
|
2021-06-02 15:16:05 +00:00
|
|
|
switch(event->key) {
|
|
|
|
case InputKeyUp:
|
|
|
|
consumed = true;
|
|
|
|
button_menu_process_up(button_menu);
|
|
|
|
break;
|
|
|
|
case InputKeyDown:
|
|
|
|
consumed = true;
|
|
|
|
button_menu_process_down(button_menu);
|
|
|
|
break;
|
2023-01-13 14:11:12 +00:00
|
|
|
case InputKeyRight:
|
|
|
|
consumed = true;
|
|
|
|
button_menu_process_right(button_menu);
|
|
|
|
break;
|
|
|
|
case InputKeyLeft:
|
|
|
|
consumed = true;
|
|
|
|
button_menu_process_left(button_menu);
|
|
|
|
break;
|
2021-06-02 15:16:05 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
|
|
|
View* button_menu_get_view(ButtonMenu* button_menu) {
|
|
|
|
furi_assert(button_menu);
|
|
|
|
return button_menu->view;
|
|
|
|
}
|
|
|
|
|
2022-01-21 17:32:03 +00:00
|
|
|
void button_menu_reset(ButtonMenu* button_menu) {
|
2021-06-02 15:16:05 +00:00
|
|
|
furi_assert(button_menu);
|
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
button_menu->view,
|
|
|
|
ButtonMenuModel * model,
|
|
|
|
{
|
2021-11-15 17:09:40 +00:00
|
|
|
ButtonMenuItemArray_reset(model->items);
|
2021-06-02 15:16:05 +00:00
|
|
|
model->position = 0;
|
2022-02-16 16:09:43 +00:00
|
|
|
model->header = NULL;
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void button_menu_set_header(ButtonMenu* button_menu, const char* header) {
|
|
|
|
furi_assert(button_menu);
|
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
button_menu->view, ButtonMenuModel * model, { model->header = header; }, true);
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ButtonMenuItem* button_menu_add_item(
|
|
|
|
ButtonMenu* button_menu,
|
|
|
|
const char* label,
|
|
|
|
int32_t index,
|
|
|
|
ButtonMenuItemCallback callback,
|
|
|
|
ButtonMenuItemType type,
|
|
|
|
void* callback_context) {
|
|
|
|
ButtonMenuItem* item = NULL;
|
|
|
|
furi_assert(label);
|
|
|
|
furi_assert(button_menu);
|
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
button_menu->view,
|
|
|
|
ButtonMenuModel * model,
|
|
|
|
{
|
2021-06-02 15:16:05 +00:00
|
|
|
item = ButtonMenuItemArray_push_new(model->items);
|
|
|
|
item->label = label;
|
|
|
|
item->index = index;
|
|
|
|
item->type = type;
|
|
|
|
item->callback = callback;
|
|
|
|
item->callback_context = callback_context;
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2021-06-02 15:16:05 +00:00
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
ButtonMenu* button_menu_alloc(void) {
|
2022-02-18 19:53:46 +00:00
|
|
|
ButtonMenu* button_menu = malloc(sizeof(ButtonMenu));
|
2021-06-02 15:16:05 +00:00
|
|
|
button_menu->view = view_alloc();
|
|
|
|
view_set_orientation(button_menu->view, ViewOrientationVertical);
|
|
|
|
view_set_context(button_menu->view, button_menu);
|
|
|
|
view_allocate_model(button_menu->view, ViewModelTypeLocking, sizeof(ButtonMenuModel));
|
|
|
|
view_set_draw_callback(button_menu->view, button_menu_view_draw_callback);
|
|
|
|
view_set_input_callback(button_menu->view, button_menu_view_input_callback);
|
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
button_menu->view,
|
|
|
|
ButtonMenuModel * model,
|
|
|
|
{
|
2021-06-02 15:16:05 +00:00
|
|
|
ButtonMenuItemArray_init(model->items);
|
|
|
|
model->position = 0;
|
|
|
|
model->header = NULL;
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2021-06-02 15:16:05 +00:00
|
|
|
|
2021-08-11 17:51:06 +00:00
|
|
|
button_menu->freeze_input = false;
|
2021-06-02 15:16:05 +00:00
|
|
|
return button_menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
void button_menu_free(ButtonMenu* button_menu) {
|
|
|
|
furi_assert(button_menu);
|
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
button_menu->view,
|
|
|
|
ButtonMenuModel * model,
|
|
|
|
{ ButtonMenuItemArray_clear(model->items); },
|
|
|
|
true);
|
2021-06-02 15:16:05 +00:00
|
|
|
view_free(button_menu->view);
|
|
|
|
free(button_menu);
|
|
|
|
}
|
2021-06-09 13:04:49 +00:00
|
|
|
|
|
|
|
void button_menu_set_selected_item(ButtonMenu* button_menu, uint32_t index) {
|
|
|
|
furi_assert(button_menu);
|
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
button_menu->view,
|
|
|
|
ButtonMenuModel * model,
|
|
|
|
{
|
2022-12-10 18:05:52 +00:00
|
|
|
size_t item_position = 0;
|
2021-06-09 13:04:49 +00:00
|
|
|
ButtonMenuItemArray_it_t it;
|
|
|
|
for(ButtonMenuItemArray_it(it, model->items); !ButtonMenuItemArray_end_p(it);
|
|
|
|
ButtonMenuItemArray_next(it), ++item_position) {
|
2022-05-06 13:37:10 +00:00
|
|
|
if((uint32_t)ButtonMenuItemArray_cref(it)->index == index) {
|
2021-06-09 13:04:49 +00:00
|
|
|
model->position = item_position;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|