Skip to content

Commit 2a5be23

Browse files
authored
Merge pull request #12 from tixlegeek/tixlegeek-check_bmp_size_editor
- Rework BMP Editor events - Add width check for editing bitmaps on flipper
2 parents 80ba0cc + cf74053 commit 2a5be23

File tree

3 files changed

+81
-26
lines changed

3 files changed

+81
-26
lines changed

401lightMessengerApp/401LightMsg_bmp_editor.c

+75-21
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,24 @@ static uint8_t bmp_editor_move(AppBmpEditor* BmpEditor, int8_t x, int8_t y) {
3232
}
3333

3434
static uint8_t bmp_editor_resize(AppBmpEditor* BmpEditor, int8_t w, int8_t h) {
35+
// Compute the new dimensions given increments/decrements in size. Usually only height
3536
int dh = BmpEditor->model_data->bmp_h + h;
3637
int dw = BmpEditor->model_data->bmp_w + w;
37-
if(dh < PARAM_BMP_EDITOR_MIN_RES_H)
38+
if(dh < PARAM_BMP_EDITOR_MIN_RES_H) {
3839
BmpEditor->model_data->bmp_h = PARAM_BMP_EDITOR_MIN_RES_H;
39-
else if(dh > PARAM_BMP_EDITOR_MAX_RES_H)
40+
} else if(dh > PARAM_BMP_EDITOR_MAX_RES_H) {
4041
BmpEditor->model_data->bmp_h = PARAM_BMP_EDITOR_MAX_RES_H;
41-
else
42+
} else {
4243
BmpEditor->model_data->bmp_h = dh;
44+
}
4345

44-
if(dw < PARAM_BMP_EDITOR_MIN_RES_W)
46+
if(dw < PARAM_BMP_EDITOR_MIN_RES_W) {
4547
BmpEditor->model_data->bmp_w = PARAM_BMP_EDITOR_MIN_RES_W;
46-
else if(dw > PARAM_BMP_EDITOR_MAX_RES_W)
48+
} else if(dw > PARAM_BMP_EDITOR_MAX_RES_W) {
4749
BmpEditor->model_data->bmp_w = PARAM_BMP_EDITOR_MAX_RES_W;
48-
else
50+
} else {
4951
BmpEditor->model_data->bmp_w = dw;
52+
}
5053

5154
BmpEditor->model_data->cursor.x = (BmpEditor->model_data->bmp_w / 2);
5255
BmpEditor->model_data->cursor.y = (BmpEditor->model_data->bmp_h / 2);
@@ -84,7 +87,7 @@ static void bmp_editor_text_input_callback(void* ctx) {
8487
LIGHTMSGCONF_SAVE_FOLDER,
8588
BmpEditor->bitmapName,
8689
".bmp");
87-
view_dispatcher_send_custom_event(app->view_dispatcher, SetTextInputSaveEvent);
90+
view_dispatcher_send_custom_event(app->view_dispatcher, AppBmpEditorEventSaveText);
8891
}
8992

9093
static void bmp_editor_select_name(void* ctx) {
@@ -114,6 +117,7 @@ static void bmp_editor_select_file(void* ctx) {
114117
AppContext* app = (AppContext*)ctx; // Main app struct
115118
AppData* appData = (AppData*)app->data;
116119
AppBmpEditor* BmpEditor = app->sceneBmpEditor;
120+
117121
bmpEditorData* BmpEditorData = BmpEditor->model_data;
118122
Configuration* light_msg_data = (Configuration*)appData->config;
119123
DialogsFileBrowserOptions browser_options;
@@ -129,16 +133,24 @@ static void bmp_editor_select_file(void* ctx) {
129133
if(dialog_file_browser_show(BmpEditor->dialogs, bitmapPath, bitmapPath, &browser_options)) {
130134
if(BmpEditorData->bitmap) bitmapMatrix_free(BmpEditorData->bitmap);
131135
BmpEditorData->bitmap = bmp_to_bitmapMatrix(furi_string_get_cstr(bitmapPath));
132-
BmpEditorData->bmp_w = BmpEditorData->bitmap->width;
133-
BmpEditorData->bmp_h = BmpEditorData->bitmap->height;
134-
BmpEditorData->state = BmpEditorStateDrawing;
135-
136-
memcpy(
137-
BmpEditor->bitmapPath,
138-
furi_string_get_cstr(bitmapPath),
139-
strlen(furi_string_get_cstr(bitmapPath)));
140-
bmp_compute_model(BmpEditor, BmpEditor->model_data);
141-
view_dispatcher_switch_to_view(app->view_dispatcher, AppViewBmpEditor);
136+
if(BmpEditorData->bitmap->width > PARAM_BMP_EDITOR_MAX_RES_W) {
137+
//if(BmpEditorData->bitmap) bitmapMatrix_free(BmpEditorData->bitmap);
138+
//furi_string_free(bitmapPath);
139+
BmpEditorData->state = BmpEditorStateSizeError;
140+
BmpEditorData->error = L401_ERR_WIDTH;
141+
view_dispatcher_switch_to_view(app->view_dispatcher, AppViewBmpEditor);
142+
return;
143+
} else {
144+
BmpEditorData->bmp_w = BmpEditorData->bitmap->width;
145+
BmpEditorData->bmp_h = BmpEditorData->bitmap->height;
146+
BmpEditorData->state = BmpEditorStateDrawing;
147+
memcpy(
148+
BmpEditor->bitmapPath,
149+
furi_string_get_cstr(bitmapPath),
150+
strlen(furi_string_get_cstr(bitmapPath)));
151+
bmp_compute_model(BmpEditor, BmpEditor->model_data);
152+
view_dispatcher_switch_to_view(app->view_dispatcher, AppViewBmpEditor);
153+
}
142154
}
143155
furi_string_free(bitmapPath);
144156
}
@@ -149,6 +161,17 @@ static bool bmp_editor_mainmenu_input_callback(InputEvent* input_event, void* ct
149161
return false;
150162
}
151163

164+
static bool bmp_editor_error_input_callback(InputEvent* input_event, void* ctx) {
165+
AppContext* app = (AppContext*)ctx;
166+
bool consumed = false;
167+
168+
if((input_event->type == InputTypePress) || (input_event->type == InputTypeRepeat)) {
169+
view_dispatcher_send_custom_event(app->view_dispatcher, AppBmpEditorEventQuit);
170+
consumed = true;
171+
}
172+
return consumed;
173+
}
174+
152175
static bool bmp_editor_select_size_input_callback(InputEvent* input_event, void* ctx) {
153176
AppContext* app = (AppContext*)ctx;
154177
AppBmpEditor* BmpEditor = app->sceneBmpEditor;
@@ -214,7 +237,6 @@ static bool bmp_editor_draw_input_callback(InputEvent* input_event, void* ctx) {
214237
break;
215238
case InputKeyOk:
216239
bmp_editor_toggle(BmpEditor);
217-
view_dispatcher_send_custom_event(app->view_dispatcher, AppBmpEditorEventToggle);
218240
consumed = false;
219241
break;
220242
case InputKeyBack:
@@ -261,6 +283,9 @@ static bool app_scene_bmp_editor_input_callback(InputEvent* input_event, void* c
261283
case BmpEditorStateDrawing:
262284
consumed = bmp_editor_draw_input_callback(input_event, ctx);
263285
break;
286+
case BmpEditorStateSizeError:
287+
consumed = bmp_editor_error_input_callback(input_event, ctx);
288+
break;
264289
default:
265290
break;
266291
}
@@ -290,6 +315,27 @@ static void bmp_editor_drawSizePicker(Canvas* canvas, void* ctx) {
290315
canvas_draw_str(canvas, 25 + 45, 62, "OK");
291316
}
292317

318+
static void bmp_editor_drawError(Canvas* canvas, void* ctx) {
319+
// UNUSED(ctx);
320+
bmpEditorModel* BmpEditorModel = (bmpEditorModel*)ctx;
321+
bmpEditorData* BmpEditorData = BmpEditorModel->data;
322+
323+
switch(BmpEditorData->error) {
324+
case L401_ERR_WIDTH:
325+
canvas_clear(canvas);
326+
canvas_set_font(canvas, FontSecondary);
327+
canvas_draw_str_aligned(canvas, 64, 20, AlignCenter, AlignCenter, "BMP File too large to");
328+
canvas_draw_str_aligned(canvas, 64, 30, AlignCenter, AlignCenter, "be edited on flipper!");
329+
break;
330+
default:
331+
canvas_clear(canvas);
332+
canvas_set_font(canvas, FontPrimary);
333+
canvas_draw_str_aligned(canvas, 64, 10, AlignCenter, AlignCenter, "Unknown error");
334+
break;
335+
}
336+
elements_button_center(canvas, "Return");
337+
}
338+
293339
static void bmp_editor_drawBoard(Canvas* canvas, void* ctx) {
294340
bmpEditorModel* BmpEditorModel = (bmpEditorModel*)ctx;
295341
bmpEditorData* BmpEditorData = BmpEditorModel->data;
@@ -365,6 +411,9 @@ static void app_scene_bmp_editor_render_callback(Canvas* canvas, void* _model) {
365411
case BmpEditorStateDrawing:
366412
bmp_editor_drawBoard(canvas, _model);
367413
break;
414+
case BmpEditorStateSizeError:
415+
bmp_editor_drawError(canvas, _model);
416+
break;
368417
default:
369418
break;
370419
}
@@ -404,6 +453,7 @@ AppBmpEditor* app_bmp_editor_alloc(void* ctx) {
404453
appBmpEditor->model_data->bmp_pixel_spacing = 0;
405454
appBmpEditor->model_data->bmp_w = 32;
406455
appBmpEditor->model_data->bmp_h = 16;
456+
appBmpEditor->model_data->error = L401_OK;
407457
appBmpEditor->mainmenu = submenu_alloc();
408458

409459
submenu_add_item(
@@ -432,11 +482,9 @@ AppBmpEditor* app_bmp_editor_alloc(void* ctx) {
432482

433483
appBmpEditor->view = view_alloc();
434484
view_allocate_model(appBmpEditor->view, ViewModelTypeLocking, sizeof(bmpEditorModel));
435-
436485
view_set_context(appBmpEditor->view, app);
437486
view_set_draw_callback(appBmpEditor->view, app_scene_bmp_editor_render_callback);
438487
view_set_input_callback(appBmpEditor->view, app_scene_bmp_editor_input_callback);
439-
440488
return appBmpEditor;
441489
}
442490

@@ -536,11 +584,17 @@ bool app_scene_bmp_editor_on_event(void* ctx, SceneManagerEvent event) {
536584
// UNUSED(ctx);
537585
bool consumed = false;
538586
if(event.type == SceneManagerEventTypeCustom) {
539-
if(event.event == SetTextInputSaveEvent) {
587+
switch(event.event) {
588+
case AppBmpEditorEventSaveText:
540589
bmp_editor_init_bitmap(ctx);
541590
BmpEditorData->state = BmpEditorStateDrawing;
542591
view_dispatcher_switch_to_view(app->view_dispatcher, AppViewBmpEditor);
543592
consumed = true;
593+
break;
594+
case AppBmpEditorEventQuit:
595+
view_dispatcher_switch_to_view(app->view_dispatcher, BmpEditorViewMainMenu);
596+
consumed = true;
597+
break;
544598
}
545599
}
546600
// scene_manager_next_scene(app->scene_manager, AppSceneMainMenu);

401lightMessengerApp/401LightMsg_bmp_editor.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
#include <stdlib.h>
1313

1414
#include "bmp.h"
15+
#include "401_err.h"
1516
#include "drivers/sk6805.h"
1617
#include <401_light_msg_icons.h>
1718
#include <dialogs/dialogs.h>
1819
#include <gui/gui.h>
1920
#include <gui/modules/submenu.h>
2021
#include <gui/modules/text_input.h>
22+
#include <gui/elements.h>
2123
#include <gui/scene_manager.h>
2224
#include <gui/view_dispatcher.h>
2325
#include <lib/toolbox/name_generator.h>
@@ -30,6 +32,7 @@ typedef struct {
3032
typedef enum {
3133
BmpEditorStateDrawing,
3234
BmpEditorStateSelectSize,
35+
BmpEditorStateSizeError,
3336
BmpEditorStateSelectName,
3437
BmpEditorStateSelectFile,
3538
BmpEditorStateSelectSource,
@@ -64,6 +67,7 @@ typedef struct {
6467
uint8_t* bmp_canvas;
6568
bitmapMatrix* bitmap;
6669
bmpEditorState state;
70+
l401_err error;
6771
} bmpEditorData;
6872

6973
typedef struct {
@@ -84,11 +88,7 @@ typedef struct AppBmpEditor {
8488

8589
typedef enum {
8690
AppBmpEditorEventQuit,
87-
AppBmpEditorEventUp,
88-
AppBmpEditorEventDown,
89-
AppBmpEditorEventLeft,
90-
AppBmpEditorEventRight,
91-
AppBmpEditorEventToggle,
91+
AppBmpEditorEventSaveText,
9292
} AppBmpEditorCustomEvents;
9393

9494
AppBmpEditor* app_bmp_editor_alloc();

401lightMessengerApp/401_err.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ typedef enum {
1818
L401_ERR_BMP_FILE,
1919
L401_ERR_NULLPTR,
2020
L401_ERR_HARDWARE,
21+
L401_ERR_WIDTH,
2122
} l401_err;
2223

2324
#endif /* end of include guard: L401_ERR_H */

0 commit comments

Comments
 (0)