Break gdraw animations into dedicated file
This commit is contained in:
66
src/c/gdraw_anim.c
Normal file
66
src/c/gdraw_anim.c
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "gdraw_anim.h"
|
||||
|
||||
static uint8_t s_pin_stroke_width = 1;
|
||||
static Animation *s_pin_stroke_anim;
|
||||
static Layer *s_pin_layer;
|
||||
|
||||
static uint8_t get_stroke_width_for_progress(AnimationProgress progress) {
|
||||
const uint8_t min_width = 1;
|
||||
const uint8_t max_width = 3;
|
||||
const uint8_t delta = max_width - min_width;
|
||||
const AnimationProgress half = ANIMATION_NORMALIZED_MAX / 2;
|
||||
|
||||
if (progress <= half) {
|
||||
return min_width + (uint8_t)(delta * progress * 2 / ANIMATION_NORMALIZED_MAX);
|
||||
}
|
||||
return max_width - (uint8_t)(delta * (progress - half) * 2 / ANIMATION_NORMALIZED_MAX);
|
||||
}
|
||||
|
||||
static void pin_stroke_anim_update(Animation *animation, const AnimationProgress progress) {
|
||||
s_pin_stroke_width = get_stroke_width_for_progress(progress);
|
||||
if (s_pin_layer) {
|
||||
layer_mark_dirty(s_pin_layer);
|
||||
}
|
||||
}
|
||||
|
||||
static void pin_stroke_anim_stopped(Animation *animation, bool finished, void *context) {
|
||||
s_pin_stroke_anim = NULL;
|
||||
animation_destroy(animation);
|
||||
}
|
||||
|
||||
void pin_animation_init(Layer *pin_layer) {
|
||||
s_pin_layer = pin_layer;
|
||||
s_pin_stroke_width = 1;
|
||||
}
|
||||
|
||||
void pin_animation_start(void) {
|
||||
if (s_pin_stroke_anim) {
|
||||
animation_unschedule(s_pin_stroke_anim);
|
||||
animation_destroy(s_pin_stroke_anim);
|
||||
s_pin_stroke_anim = NULL;
|
||||
}
|
||||
|
||||
static const AnimationImplementation s_pin_stroke_anim_impl = {
|
||||
.update = pin_stroke_anim_update,
|
||||
};
|
||||
|
||||
s_pin_stroke_anim = animation_create();
|
||||
animation_set_implementation(s_pin_stroke_anim, &s_pin_stroke_anim_impl);
|
||||
animation_set_duration(s_pin_stroke_anim, 400);
|
||||
animation_set_curve(s_pin_stroke_anim, AnimationCurveEaseInOut);
|
||||
animation_set_handlers(s_pin_stroke_anim, (AnimationHandlers){.stopped = pin_stroke_anim_stopped}, NULL);
|
||||
animation_schedule(s_pin_stroke_anim);
|
||||
}
|
||||
|
||||
void pin_animation_deinit(void) {
|
||||
if (s_pin_stroke_anim) {
|
||||
animation_unschedule(s_pin_stroke_anim);
|
||||
animation_destroy(s_pin_stroke_anim);
|
||||
s_pin_stroke_anim = NULL;
|
||||
}
|
||||
s_pin_layer = NULL;
|
||||
}
|
||||
|
||||
uint8_t pin_animation_get_stroke_width(void) {
|
||||
return s_pin_stroke_width;
|
||||
}
|
||||
8
src/c/gdraw_anim.h
Normal file
8
src/c/gdraw_anim.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <pebble.h>
|
||||
|
||||
void pin_animation_init(Layer *pin_layer);
|
||||
void pin_animation_deinit(void);
|
||||
void pin_animation_start(void);
|
||||
uint8_t pin_animation_get_stroke_width(void);
|
||||
56
src/c/main.c
56
src/c/main.c
@@ -1,3 +1,4 @@
|
||||
#include "gdraw_anim.h"
|
||||
#include <pebble.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -22,8 +23,6 @@ static GDrawCommandImage *s_pin_icon;
|
||||
// declare animation statics
|
||||
static GRect s_sleep_bar_start = GRect(0, 0, PBL_DISPLAY_WIDTH, PBL_DISPLAY_HEIGHT);
|
||||
static GRect s_sleep_icon_start = GRect((PBL_DISPLAY_WIDTH / 2) - 13, (PBL_DISPLAY_HEIGHT / 2) - 12, 25, 25);
|
||||
static uint8_t s_pin_stroke_width = 1;
|
||||
static Animation *s_pin_stroke_anim;
|
||||
|
||||
// declare time tracking statics
|
||||
static time_t s_sleep_timestamp;
|
||||
@@ -70,52 +69,11 @@ static void draw_pin() {
|
||||
}
|
||||
gdraw_command_set_fill_color(command, GColorClear);
|
||||
#endif
|
||||
gdraw_command_set_stroke_width(command, s_pin_stroke_width);
|
||||
gdraw_command_set_stroke_width(command, pin_animation_get_stroke_width());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t get_stroke_width_for_progress(AnimationProgress progress) {
|
||||
const uint8_t min_width = 1;
|
||||
const uint8_t max_width = 3;
|
||||
const uint8_t delta = max_width - min_width;
|
||||
const AnimationProgress half = ANIMATION_NORMALIZED_MAX / 2;
|
||||
|
||||
if (progress <= half) {
|
||||
return min_width + (uint8_t)(delta * progress * 2 / ANIMATION_NORMALIZED_MAX);
|
||||
}
|
||||
return max_width - (uint8_t)(delta * (progress - half) * 2 / ANIMATION_NORMALIZED_MAX);
|
||||
}
|
||||
|
||||
static void pin_stroke_anim_update(Animation *animation, const AnimationProgress progress) {
|
||||
s_pin_stroke_width = get_stroke_width_for_progress(progress);
|
||||
layer_mark_dirty(s_pin_icon_layer);
|
||||
}
|
||||
|
||||
static void pin_stroke_anim_stopped(Animation *animation, bool finished, void *context) {
|
||||
s_pin_stroke_anim = NULL;
|
||||
animation_destroy(animation);
|
||||
}
|
||||
|
||||
static void pin_stroke_anim_start(void) {
|
||||
if (s_pin_stroke_anim) {
|
||||
animation_unschedule(s_pin_stroke_anim);
|
||||
animation_destroy(s_pin_stroke_anim);
|
||||
s_pin_stroke_anim = NULL;
|
||||
}
|
||||
|
||||
static const AnimationImplementation s_pin_stroke_anim_impl = {
|
||||
.update = pin_stroke_anim_update,
|
||||
};
|
||||
|
||||
s_pin_stroke_anim = animation_create();
|
||||
animation_set_implementation(s_pin_stroke_anim, &s_pin_stroke_anim_impl);
|
||||
animation_set_duration(s_pin_stroke_anim, 400);
|
||||
animation_set_curve(s_pin_stroke_anim, AnimationCurveEaseInOut);
|
||||
animation_set_handlers(s_pin_stroke_anim, (AnimationHandlers){.stopped = pin_stroke_anim_stopped}, NULL);
|
||||
animation_schedule(s_pin_stroke_anim);
|
||||
}
|
||||
|
||||
static void pin_icon_update_proc(Layer *layer, GContext *ctx) {
|
||||
graphics_context_set_antialiased(ctx, false);
|
||||
draw_pin();
|
||||
@@ -142,9 +100,9 @@ static void main_window_load(Window *window) {
|
||||
}
|
||||
|
||||
// pin icon
|
||||
s_pin_stroke_width = 1;
|
||||
s_pin_icon_layer = layer_create(GRect(PBL_DISPLAY_WIDTH - PBL_IF_ROUND_ELSE(27, 25), (PBL_DISPLAY_HEIGHT / 2) - 13, 25, 25));
|
||||
layer_set_update_proc(s_pin_icon_layer, pin_icon_update_proc);
|
||||
pin_animation_init(s_pin_icon_layer);
|
||||
|
||||
#if PBL_DISPLAY_WIDTH >= 200
|
||||
s_sleep_time_layer = text_layer_create(GRect(0, PBL_IF_ROUND_ELSE(21, -2), PBL_DISPLAY_WIDTH, 30));
|
||||
@@ -205,11 +163,7 @@ static void main_window_load(Window *window) {
|
||||
}
|
||||
|
||||
static void main_window_unload(Window *window) {
|
||||
if (s_pin_stroke_anim) {
|
||||
animation_unschedule(s_pin_stroke_anim);
|
||||
animation_destroy(s_pin_stroke_anim);
|
||||
s_pin_stroke_anim = NULL;
|
||||
}
|
||||
pin_animation_deinit();
|
||||
text_layer_destroy(s_last_watched_layer);
|
||||
text_layer_destroy(s_sleep_time_layer);
|
||||
layer_destroy(s_sleep_icon_layer);
|
||||
@@ -259,7 +213,7 @@ static void send_pin_to_pkjs() {
|
||||
}
|
||||
|
||||
static void select_single_click_handler(ClickRecognizerRef recognizer, void *context) {
|
||||
pin_stroke_anim_start();
|
||||
pin_animation_start();
|
||||
send_pin_to_pkjs();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user