Welcome to mirror list, hosted at ThFree Co, Russian Federation.

button_panel.c « modules « gui « applications - github.com/ClusterM/flipperzero-firmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 07bb6b62940e590223dcd184d61f0f7c85ccb0df (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include "button_panel.h"
#include "furi_hal_resources.h"
#include "gui/canvas.h"
#include <m-array.h>
#include <m-i-list.h>
#include <m-list.h>
#include <furi.h>
#include <gui/elements.h>
#include <stdint.h>

typedef struct {
    // uint16_t to support multi-screen, wide button panel
    uint16_t x;
    uint16_t y;
    Font font;
    const char* str;
} LabelElement;

LIST_DEF(LabelList, LabelElement, M_POD_OPLIST)
#define M_OPL_LabelList_t() LIST_OPLIST(LabelList)

typedef struct {
    uint16_t x;
    uint16_t y;
    const Icon* name;
    const Icon* name_selected;
} IconElement;

typedef struct ButtonItem {
    uint32_t index;
    ButtonItemCallback callback;
    IconElement icon;
    void* callback_context;
} ButtonItem;

ARRAY_DEF(ButtonArray, ButtonItem*, M_PTR_OPLIST);
#define M_OPL_ButtonArray_t() ARRAY_OPLIST(ButtonArray, M_PTR_OPLIST)
ARRAY_DEF(ButtonMatrix, ButtonArray_t);
#define M_OPL_ButtonMatrix_t() ARRAY_OPLIST(ButtonMatrix, M_OPL_ButtonArray_t())

struct ButtonPanel {
    View* view;
    ButtonPanelInputCallback input_callback;
    void* input_context;
};

typedef struct {
    ButtonMatrix_t button_matrix;
    LabelList_t labels;
    uint16_t reserve_x;
    uint16_t reserve_y;
    uint16_t selected_item_x;
    uint16_t selected_item_y;
    ButtonPanelDrawCallback draw_callback;
    void* draw_context;
} ButtonPanelModel;

static ButtonItem** button_panel_get_item(ButtonPanelModel* model, size_t x, size_t y);
static void button_panel_process_up(ButtonPanel* button_panel);
static void button_panel_process_down(ButtonPanel* button_panel);
static void button_panel_process_left(ButtonPanel* button_panel);
static void button_panel_process_right(ButtonPanel* button_panel);
static void button_panel_process_ok(ButtonPanel* button_panel);
static void button_panel_view_draw_callback(Canvas* canvas, void* _model);
static bool button_panel_view_input_callback(InputEvent* event, void* context);

ButtonPanel* button_panel_alloc() {
    ButtonPanel* button_panel = malloc(sizeof(ButtonPanel));
    button_panel->view = view_alloc();
    view_set_orientation(button_panel->view, ViewOrientationVertical);
    view_set_context(button_panel->view, button_panel);
    view_allocate_model(button_panel->view, ViewModelTypeLocking, sizeof(ButtonPanelModel));
    view_set_draw_callback(button_panel->view, button_panel_view_draw_callback);
    view_set_input_callback(button_panel->view, button_panel_view_input_callback);
    button_panel->input_callback = NULL;

    with_view_model(
        button_panel->view, (ButtonPanelModel * model) {
            model->reserve_x = 0;
            model->reserve_y = 0;
            model->selected_item_x = 0;
            model->selected_item_y = 0;
            model->draw_callback = NULL;
            ButtonMatrix_init(model->button_matrix);
            LabelList_init(model->labels);
            return true;
        });

    return button_panel;
}

void button_panel_reserve(ButtonPanel* button_panel, size_t reserve_x, size_t reserve_y) {
    furi_check(reserve_x > 0);
    furi_check(reserve_y > 0);

    with_view_model(
        button_panel->view, (ButtonPanelModel * model) {
            model->reserve_x = reserve_x;
            model->reserve_y = reserve_y;
            ButtonMatrix_reserve(model->button_matrix, model->reserve_y);
            for(size_t i = 0; i > model->reserve_y; ++i) {
                ButtonArray_t* array = ButtonMatrix_get(model->button_matrix, i);
                ButtonArray_init(*array);
                ButtonArray_reserve(*array, reserve_x);
                // TODO: do we need to clear allocated memory of ptr-s to ButtonItem ??
            }
            LabelList_init(model->labels);
            return true;
        });
}

void button_panel_free(ButtonPanel* button_panel) {
    furi_assert(button_panel);

    button_panel_reset(button_panel);

    with_view_model(
        button_panel->view, (ButtonPanelModel * model) {
            LabelList_clear(model->labels);
            ButtonMatrix_clear(model->button_matrix);
            return true;
        });

    view_free(button_panel->view);
    free(button_panel);
}

void button_panel_reset(ButtonPanel* button_panel) {
    furi_assert(button_panel);

    with_view_model(
        button_panel->view, (ButtonPanelModel * model) {
            for(size_t x = 0; x < model->reserve_x; ++x) {
                for(size_t y = 0; y < model->reserve_y; ++y) {
                    ButtonItem** button_item = button_panel_get_item(model, x, y);
                    free(*button_item);
                    *button_item = NULL;
                }
            }
            model->reserve_x = 0;
            model->reserve_y = 0;
            LabelList_reset(model->labels);
            ButtonMatrix_reset(model->button_matrix);
            return true;
        });
}

static ButtonItem** button_panel_get_item(ButtonPanelModel* model, size_t x, size_t y) {
    furi_assert(model);

    furi_check(x < model->reserve_x);
    furi_check(y < model->reserve_y);
    ButtonArray_t* button_array = ButtonMatrix_safe_get(model->button_matrix, x);
    ButtonItem** button_item = ButtonArray_safe_get(*button_array, y);
    return button_item;
}

void button_panel_add_item(
    ButtonPanel* button_panel,
    uint32_t index,
    uint16_t matrix_place_x,
    uint16_t matrix_place_y,
    uint16_t x,
    uint16_t y,
    const Icon* icon_name,
    const Icon* icon_name_selected,
    ButtonItemCallback callback,
    void* callback_context) {
    furi_assert(button_panel);

    with_view_model(
        button_panel->view, (ButtonPanelModel * model) {
            ButtonItem** button_item_ptr =
                button_panel_get_item(model, matrix_place_x, matrix_place_y);
            furi_check(*button_item_ptr == NULL);
            *button_item_ptr = malloc(sizeof(ButtonItem));
            ButtonItem* button_item = *button_item_ptr;
            button_item->callback = callback;
            button_item->callback_context = callback_context;
            button_item->icon.x = x;
            button_item->icon.y = y;
            button_item->icon.name = icon_name;
            button_item->icon.name_selected = icon_name_selected;
            button_item->index = index;
            return true;
        });
}

View* button_panel_get_view(ButtonPanel* button_panel) {
    furi_assert(button_panel);
    return button_panel->view;
}

static void button_panel_view_draw_callback(Canvas* canvas, void* _model) {
    furi_assert(canvas);
    furi_assert(_model);

    ButtonPanelModel* model = _model;

    canvas_clear(canvas);
    canvas_set_color(canvas, ColorBlack);

    for(size_t x = 0; x < model->reserve_x; ++x) {
        for(size_t y = 0; y < model->reserve_y; ++y) {
            ButtonItem* button_item = *button_panel_get_item(model, x, y);
            const Icon* icon_name = button_item->icon.name;
            if((model->selected_item_x == x) && (model->selected_item_y == y)) {
                icon_name = button_item->icon.name_selected;
            }
            canvas_draw_icon(canvas, button_item->icon.x, button_item->icon.y, icon_name);
        }
    }

    for
        M_EACH(label, model->labels, LabelList_t) {
            canvas_set_font(canvas, label->font);
            canvas_draw_str(canvas, label->x, label->y, label->str);
        }

    if(model->draw_callback) model->draw_callback(canvas, model->draw_context);
}

static void button_panel_process_down(ButtonPanel* button_panel) {
    with_view_model(
        button_panel->view, (ButtonPanelModel * model) {
            size_t new_selected_item_x = model->selected_item_x;
            size_t new_selected_item_y = model->selected_item_y;
            size_t i;

            if(new_selected_item_y >= (model->reserve_y - 1)) return false;

            ++new_selected_item_y;

            for(i = 0; i < model->reserve_x; ++i) {
                new_selected_item_x = (model->selected_item_x + i) % model->reserve_x;
                if(*button_panel_get_item(model, new_selected_item_x, new_selected_item_y)) {
                    break;
                }
            }
            if(i == model->reserve_x) return false;

            model->selected_item_x = new_selected_item_x;
            model->selected_item_y = new_selected_item_y;

            return true;
        });
}

static void button_panel_process_up(ButtonPanel* button_panel) {
    with_view_model(
        button_panel->view, (ButtonPanelModel * model) {
            size_t new_selected_item_x = model->selected_item_x;
            size_t new_selected_item_y = model->selected_item_y;
            size_t i;

            if(new_selected_item_y <= 0) return false;

            --new_selected_item_y;

            for(i = 0; i < model->reserve_x; ++i) {
                new_selected_item_x = (model->selected_item_x + i) % model->reserve_x;
                if(*button_panel_get_item(model, new_selected_item_x, new_selected_item_y)) {
                    break;
                }
            }
            if(i == model->reserve_x) return false;

            model->selected_item_x = new_selected_item_x;
            model->selected_item_y = new_selected_item_y;
            return true;
        });
}

static void button_panel_process_left(ButtonPanel* button_panel) {
    with_view_model(
        button_panel->view, (ButtonPanelModel * model) {
            size_t new_selected_item_x = model->selected_item_x;
            size_t new_selected_item_y = model->selected_item_y;
            size_t i;

            if(new_selected_item_x <= 0) return false;

            --new_selected_item_x;

            for(i = 0; i < model->reserve_y; ++i) {
                new_selected_item_y = (model->selected_item_y + i) % model->reserve_y;
                if(*button_panel_get_item(model, new_selected_item_x, new_selected_item_y)) {
                    break;
                }
            }
            if(i == model->reserve_y) return false;

            model->selected_item_x = new_selected_item_x;
            model->selected_item_y = new_selected_item_y;
            return true;
        });
}

static void button_panel_process_right(ButtonPanel* button_panel) {
    with_view_model(
        button_panel->view, (ButtonPanelModel * model) {
            size_t new_selected_item_x = model->selected_item_x;
            size_t new_selected_item_y = model->selected_item_y;
            size_t i;

            if(new_selected_item_x >= (model->reserve_x - 1)) return false;

            ++new_selected_item_x;

            for(i = 0; i < model->reserve_y; ++i) {
                new_selected_item_y = (model->selected_item_y + i) % model->reserve_y;
                if(*button_panel_get_item(model, new_selected_item_x, new_selected_item_y)) {
                    break;
                }
            }
            if(i == model->reserve_y) return false;

            model->selected_item_x = new_selected_item_x;
            model->selected_item_y = new_selected_item_y;
            return true;
        });
}

void button_panel_process_ok(ButtonPanel* button_panel) {
    ButtonItem* button_item = NULL;

    with_view_model(
        button_panel->view, (ButtonPanelModel * model) {
            button_item =
                *button_panel_get_item(model, model->selected_item_x, model->selected_item_y);
            return true;
        });

    if(button_item && button_item->callback) {
        button_item->callback(button_item->callback_context, button_item->index);
    }
}

static bool button_panel_view_input_callback(InputEvent* event, void* context) {
    ButtonPanel* button_panel = context;
    furi_assert(button_panel);
    bool consumed = false;

    if(button_panel->input_callback) {
        consumed = button_panel->input_callback(event, button_panel->input_context);
    } else if(event->type == InputTypeShort) {
        switch(event->key) {
        case InputKeyUp:
            consumed = true;
            button_panel_process_up(button_panel);
            break;
        case InputKeyDown:
            consumed = true;
            button_panel_process_down(button_panel);
            break;
        case InputKeyLeft:
            consumed = true;
            button_panel_process_left(button_panel);
            break;
        case InputKeyRight:
            consumed = true;
            button_panel_process_right(button_panel);
            break;
        case InputKeyOk:
            consumed = true;
            button_panel_process_ok(button_panel);
            break;
        default:
            break;
        }
    }

    return consumed;
}

void button_panel_add_label(
    ButtonPanel* button_panel,
    uint16_t x,
    uint16_t y,
    Font font,
    const char* label_str) {
    furi_assert(button_panel);

    with_view_model(
        button_panel->view, (ButtonPanelModel * model) {
            LabelElement* label = LabelList_push_raw(model->labels);
            label->x = x;
            label->y = y;
            label->font = font;
            label->str = label_str;
            return true;
        });
}

void button_panel_set_popup_draw_callback(
    ButtonPanel* button_panel,
    ButtonPanelDrawCallback callback,
    void* context) {
    with_view_model(
        button_panel->view, (ButtonPanelModel * model) {
            model->draw_callback = callback;
            model->draw_context = context;
            return true;
        });
}

void button_panel_set_popup_input_callback(
    ButtonPanel* button_panel,
    ButtonPanelInputCallback callback,
    void* context) {
    button_panel->input_context = context;
    button_panel->input_callback = callback;
}