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

bubble_animation_view.c « views « animations « desktop « applications - github.com/ClusterM/flipperzero-firmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 607862d113324ec1594dfde44b0295498d688dd0 (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

#include "../animation_manager.h"
#include "../animation_storage.h"
#include "bubble_animation_view.h"

#include <furi_hal.h>
#include <furi.h>
#include <gui/canvas.h>
#include <gui/elements.h>
#include <gui/view.h>
#include <gui/icon_i.h>
#include <input/input.h>
#include <stdint.h>
#include <core/dangerous_defines.h>

#define ACTIVE_SHIFT 2

typedef struct {
    const BubbleAnimation* current;
    const FrameBubble* current_bubble;
    uint8_t current_frame;
    uint8_t active_cycle;
    uint8_t active_bubbles;
    uint8_t passive_bubbles;
    uint8_t active_shift;
    TickType_t active_ended_at;
    Icon* freeze_frame;
} BubbleAnimationViewModel;

struct BubbleAnimationView {
    View* view;
    FuriTimer* timer;
    BubbleAnimationInteractCallback interact_callback;
    void* interact_callback_context;
};

static void bubble_animation_activate(BubbleAnimationView* view, bool force);
static void bubble_animation_activate_right_now(BubbleAnimationView* view);

static uint8_t bubble_animation_get_frame_index(BubbleAnimationViewModel* model) {
    furi_assert(model);
    uint8_t icon_index = 0;
    const BubbleAnimation* animation = model->current;

    if(model->current_frame < animation->passive_frames) {
        icon_index = model->current_frame;
    } else {
        icon_index =
            (model->current_frame - animation->passive_frames) % animation->active_frames +
            animation->passive_frames;
    }
    furi_assert(icon_index < (animation->passive_frames + animation->active_frames));

    return animation->frame_order[icon_index];
}

static void bubble_animation_draw_callback(Canvas* canvas, void* model_) {
    furi_assert(model_);
    furi_assert(canvas);

    BubbleAnimationViewModel* model = model_;
    const BubbleAnimation* animation = model->current;

    if(model->freeze_frame) {
        uint8_t y_offset = canvas_height(canvas) - icon_get_height(model->freeze_frame);
        canvas_draw_icon(canvas, 0, y_offset, model->freeze_frame);
        return;
    }

    if(!animation) {
        return;
    }

    furi_assert(model->current_frame < 255);

    uint8_t index = bubble_animation_get_frame_index(model);
    uint8_t width = icon_get_width(&animation->icon_animation);
    uint8_t height = icon_get_height(&animation->icon_animation);
    uint8_t y_offset = canvas_height(canvas) - height;
    canvas_draw_bitmap(
        canvas, 0, y_offset, width, height, animation->icon_animation.frames[index]);

    const FrameBubble* bubble = model->current_bubble;
    if(bubble) {
        if((model->current_frame >= bubble->start_frame) &&
           (model->current_frame <= bubble->end_frame)) {
            const Bubble* b = &bubble->bubble;
            elements_bubble_str(canvas, b->x, b->y, b->text, b->align_h, b->align_v);
        }
    }
}

static const FrameBubble*
    bubble_animation_pick_bubble(BubbleAnimationViewModel* model, bool active) {
    const FrameBubble* bubble = NULL;

    if((model->active_bubbles == 0) && (model->passive_bubbles == 0)) {
        return NULL;
    }

    uint8_t index =
        furi_hal_random_get() % (active ? model->active_bubbles : model->passive_bubbles);
    const BubbleAnimation* animation = model->current;

    for(int i = 0; i < animation->frame_bubble_sequences_count; ++i) {
        if((animation->frame_bubble_sequences[i]->start_frame < animation->passive_frames) ^
           active) {
            if(!index) {
                bubble = animation->frame_bubble_sequences[i];
            }
            --index;
        }
    }

    return bubble;
}

static bool bubble_animation_input_callback(InputEvent* event, void* context) {
    furi_assert(context);
    furi_assert(event);

    BubbleAnimationView* animation_view = context;
    bool consumed = false;

    if(event->type == InputTypePress) {
        bubble_animation_activate(animation_view, false);
    }

    if(event->key == InputKeyRight) {
        /* Right button reserved for animation activation, so consume */
        consumed = true;
        if(event->type == InputTypeShort) {
            if(animation_view->interact_callback) {
                animation_view->interact_callback(animation_view->interact_callback_context);
            }
        }
    }

    return consumed;
}

static void bubble_animation_activate(BubbleAnimationView* view, bool force) {
    furi_assert(view);
    bool activate = true;
    BubbleAnimationViewModel* model = view_get_model(view->view);
    if(model->current == NULL) {
        activate = false;
    } else if(model->freeze_frame) {
        activate = false;
    } else if(model->current->active_frames == 0) {
        activate = false;
    }

    if(model->current != NULL) {
        if(!force) {
            if((model->active_ended_at + model->current->active_cooldown * 1000) >
               xTaskGetTickCount()) {
                activate = false;
            } else if(model->active_shift) {
                activate = false;
            } else if(model->current_frame >= model->current->passive_frames) {
                activate = false;
            }
        }
    }
    view_commit_model(view->view, false);

    if(!activate && !force) {
        return;
    }

    if(ACTIVE_SHIFT > 0) {
        BubbleAnimationViewModel* model = view_get_model(view->view);
        model->active_shift = ACTIVE_SHIFT;
        view_commit_model(view->view, false);
    } else {
        bubble_animation_activate_right_now(view);
    }
}

static void bubble_animation_activate_right_now(BubbleAnimationView* view) {
    furi_assert(view);

    uint8_t frame_rate = 0;

    BubbleAnimationViewModel* model = view_get_model(view->view);
    if(model->current && (model->current->active_frames > 0) && (!model->freeze_frame)) {
        model->current_frame = model->current->passive_frames;
        model->current_bubble = bubble_animation_pick_bubble(model, true);
        frame_rate = model->current->icon_animation.frame_rate;
    }
    view_commit_model(view->view, true);

    if(frame_rate) {
        furi_timer_start(view->timer, 1000 / frame_rate);
    }
}

static void bubble_animation_next_frame(BubbleAnimationViewModel* model) {
    furi_assert(model);

    if(!model->current) {
        return;
    }

    if(model->current_frame < model->current->passive_frames) {
        model->current_frame = (model->current_frame + 1) % model->current->passive_frames;
    } else {
        ++model->current_frame;
        model->active_cycle +=
            !((model->current_frame - model->current->passive_frames) %
              model->current->active_frames);
        if(model->active_cycle >= model->current->active_cycles) {
            // switch to passive
            model->active_cycle = 0;
            model->current_frame = 0;
            model->current_bubble = bubble_animation_pick_bubble(model, false);
            model->active_ended_at = xTaskGetTickCount();
        }

        if(model->current_bubble) {
            if(model->current_frame > model->current_bubble->end_frame) {
                model->current_bubble = model->current_bubble->next_bubble;
            }
        }
    }
}

static void bubble_animation_timer_callback(void* context) {
    furi_assert(context);
    BubbleAnimationView* view = context;
    bool activate = false;

    BubbleAnimationViewModel* model = view_get_model(view->view);

    if(model->active_shift > 0) {
        activate = (--model->active_shift == 0);
    }

    if(!model->freeze_frame && !activate) {
        bubble_animation_next_frame(model);
    }

    view_commit_model(view->view, !activate);

    if(activate) {
        bubble_animation_activate_right_now(view);
    }
}

/* always freeze first passive frame, because
 * animation is always activated at unfreezing and played
 * passive frame first, and 2 frames after - active
 */
static Icon* bubble_animation_clone_first_frame(const Icon* icon_orig) {
    furi_assert(icon_orig);
    furi_assert(icon_orig->frames);
    furi_assert(icon_orig->frames[0]);

    Icon* icon_clone = malloc(sizeof(Icon));
    memcpy(icon_clone, icon_orig, sizeof(Icon));

    icon_clone->frames = malloc(sizeof(uint8_t*));
    /* icon bitmap can be either compressed or not. It is compressed if
     * compressed size is less than original, so max size for bitmap is
     * uncompressed (width * height) + 1 byte (in uncompressed case)
     * for compressed header
     */
    size_t max_bitmap_size = ROUND_UP_TO(icon_orig->width, 8) * icon_orig->height + 1;
    FURI_CONST_ASSIGN_PTR(icon_clone->frames[0], malloc(max_bitmap_size));
    memcpy((void*)icon_clone->frames[0], icon_orig->frames[0], max_bitmap_size);
    FURI_CONST_ASSIGN(icon_clone->frame_count, 1);

    return icon_clone;
}

static void bubble_animation_release_frame(Icon** icon) {
    furi_assert(icon);
    furi_assert(*icon);

    free((void*)(*icon)->frames[0]);
    free((void*)(*icon)->frames);
    free(*icon);
    *icon = NULL;
}

static void bubble_animation_enter(void* context) {
    furi_assert(context);
    BubbleAnimationView* view = context;
    bubble_animation_activate(view, false);

    BubbleAnimationViewModel* model = view_get_model(view->view);
    uint8_t frame_rate = 0;
    if(model->current != NULL) {
        frame_rate = model->current->icon_animation.frame_rate;
    }
    view_commit_model(view->view, false);

    if(frame_rate) {
        furi_timer_start(view->timer, 1000 / frame_rate);
    }
}

static void bubble_animation_exit(void* context) {
    furi_assert(context);
    BubbleAnimationView* view = context;
    furi_timer_stop(view->timer);
}

BubbleAnimationView* bubble_animation_view_alloc(void) {
    BubbleAnimationView* view = malloc(sizeof(BubbleAnimationView));
    view->view = view_alloc();
    view->interact_callback = NULL;
    view->timer = furi_timer_alloc(bubble_animation_timer_callback, FuriTimerTypePeriodic, view);

    view_allocate_model(view->view, ViewModelTypeLocking, sizeof(BubbleAnimationViewModel));
    view_set_context(view->view, view);
    view_set_draw_callback(view->view, bubble_animation_draw_callback);
    view_set_input_callback(view->view, bubble_animation_input_callback);
    view_set_enter_callback(view->view, bubble_animation_enter);
    view_set_exit_callback(view->view, bubble_animation_exit);

    return view;
}

void bubble_animation_view_free(BubbleAnimationView* view) {
    furi_assert(view);

    view_set_draw_callback(view->view, NULL);
    view_set_input_callback(view->view, NULL);
    view_set_context(view->view, NULL);

    view_free(view->view);
    view->view = NULL;
    free(view);
}

void bubble_animation_view_set_interact_callback(
    BubbleAnimationView* view,
    BubbleAnimationInteractCallback callback,
    void* context) {
    furi_assert(view);

    view->interact_callback_context = context;
    view->interact_callback = callback;
}

void bubble_animation_view_set_animation(
    BubbleAnimationView* view,
    const BubbleAnimation* new_animation) {
    furi_assert(view);
    furi_assert(new_animation);

    BubbleAnimationViewModel* model = view_get_model(view->view);
    furi_assert(model);
    model->current = new_animation;

    model->active_ended_at = xTaskGetTickCount() - (model->current->active_cooldown * 1000);
    model->active_bubbles = 0;
    model->passive_bubbles = 0;
    for(int i = 0; i < new_animation->frame_bubble_sequences_count; ++i) {
        if(new_animation->frame_bubble_sequences[i]->start_frame < new_animation->passive_frames) {
            ++model->passive_bubbles;
        } else {
            ++model->active_bubbles;
        }
    }

    /* select bubble sequence */
    model->current_bubble = bubble_animation_pick_bubble(model, false);
    model->current_frame = 0;
    model->active_cycle = 0;
    view_commit_model(view->view, true);

    furi_timer_start(view->timer, 1000 / new_animation->icon_animation.frame_rate);
}

void bubble_animation_freeze(BubbleAnimationView* view) {
    furi_assert(view);

    BubbleAnimationViewModel* model = view_get_model(view->view);
    furi_assert(model->current);
    furi_assert(!model->freeze_frame);
    model->freeze_frame = bubble_animation_clone_first_frame(&model->current->icon_animation);
    model->current = NULL;
    view_commit_model(view->view, false);
    furi_timer_stop(view->timer);
}

void bubble_animation_unfreeze(BubbleAnimationView* view) {
    furi_assert(view);
    uint8_t frame_rate;

    BubbleAnimationViewModel* model = view_get_model(view->view);
    furi_assert(model->freeze_frame);
    bubble_animation_release_frame(&model->freeze_frame);
    furi_assert(model->current);
    frame_rate = model->current->icon_animation.frame_rate;
    view_commit_model(view->view, true);

    furi_timer_start(view->timer, 1000 / frame_rate);
    bubble_animation_activate(view, false);
}

View* bubble_animation_get_view(BubbleAnimationView* view) {
    furi_assert(view);

    return view->view;
}