Skip to content

Commit

Permalink
Remove type check and use pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
carson-katri committed Jan 23, 2018
1 parent 5896210 commit 16a5301
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 85 deletions.
6 changes: 1 addition & 5 deletions rwatch/ui/animation/animation.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ void _animation_update(Animation *anim)
if (anim->impl.update)
//anim->impl.update(anim, ANIMATION_NORMALIZED_MAX);
if (anim->impl.teardown)
anim->impl.teardown(anim);

// Free the prop anim
if (anim->property_anim)
property_animation_destroy(anim->property_anim);
anim->impl.teardown(anim);

anim->scheduled = 0;
return;
Expand Down
89 changes: 37 additions & 52 deletions rwatch/ui/animation/property_animation.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,83 +13,84 @@

void property_animation_update_grect(PropertyAnimation * property_animation, const uint32_t distance_normalized)
{
int progress = (distance_normalized * 100) / ANIMATION_NORMALIZED_MAX;

Layer *layer = (Layer *) property_animation->subject;

GRect *from = &property_animation->values.from.grect;
GRect *to = &property_animation->values.to.grect;
GRect *from = (GRect *) property_animation->values.from;
GRect *to = (GRect *) property_animation->values.to;

if (from)
{
GRect getter_value = property_animation->impl.accessors.getter.grect(layer);
from = &getter_value;
}

GRect new_rect = GRect(LERP(from->origin.x, to->origin.x), LERP(from->origin.y, to->origin.y), LERP(from->size.w, to->size.w), LERP(from->size.h, to->size.h));

GRect new_rect = GRect(from->origin.x + (((to->origin.x - from->origin.x) / 100.0)*progress), from->origin.y + (((to->origin.y - from->origin.y) / 100.0)*progress), from->size.w + (((to->size.w - from->size.w) / 100.0)*progress), from->size.h + (((to->size.h - from->size.h) / 100.0)*progress));
if (property_animation->impl.accessors.setter.grect)
property_animation->impl.accessors.setter.grect(layer, new_rect);
//layer_set_frame(layer, new_rect);
layer_mark_dirty(layer);
}

void property_animation_update_gpoint(PropertyAnimation * property_animation, const uint32_t distance_normalized)
{
int progress = (distance_normalized * 100) / ANIMATION_NORMALIZED_MAX;

Layer *layer = (Layer *) property_animation->subject;

GRect *from = &property_animation->values.from.grect;
GRect *to = &property_animation->values.to.grect;
GPoint *from = (GPoint *) property_animation->values.from;
GPoint *to = (GPoint *) property_animation->values.to;

GRect new_rect = *from;
new_rect.origin.x = from->origin.x + (((to->origin.x - from->origin.x) / 100.0)*progress);
new_rect.origin.y = from->origin.y + (((to->origin.y - from->origin.y) / 100.0)*progress);
GPoint new_origin = GPoint(LERP(from->x, from->x), LERP(from->y, to->y));
if (property_animation->impl.accessors.setter.gpoint)
property_animation->impl.accessors.setter.gpoint(layer, new_rect.origin);
//layer_set_frame(layer, new_rect);
layer_mark_dirty(layer);
property_animation->impl.accessors.setter.gpoint(layer, new_origin);
}

void property_animation_update_int16(PropertyAnimation * property_animation, const uint32_t distance_normalized)
{
int progress = (distance_normalized * 100) / ANIMATION_NORMALIZED_MAX;
int16_t *int16 = (int16_t *) property_animation->subject;

int16_t *from = &property_animation->values.from.int16;
int16_t *to = &property_animation->values.to.int16;
int16_t *from = (int16_t *) property_animation->values.from;
int16_t *to = (int16_t *) property_animation->values.to;

property_animation->impl.accessors.setter.int16(int16, (int)(((to - from) / 100.0)*progress));
property_animation->impl.accessors.setter.int16(int16, LERP(from, to));
}

void property_animation_update_uint32(PropertyAnimation * property_animation, const uint32_t distance_normalized)
{
int progress = (distance_normalized * 100) / ANIMATION_NORMALIZED_MAX;
uint32_t *uint32 = (uint32_t *) property_animation->subject;

uint32_t *from = &property_animation->values.from.uint32;
uint32_t *to = &property_animation->values.to.uint32;
uint32_t *from = &property_animation->values.from;
uint32_t *to = &property_animation->values.to;

property_animation->impl.accessors.setter.uint32(uint32, (int)(((to - from) / 100.0)*progress));
if (property_animation->impl.accessors.setter.uint32)
property_animation->impl.accessors.setter.uint32(uint32, LERP(from, to));
}

void property_animation_update_gcolor8(PropertyAnimation * property_animation, const uint32_t distance_normalized)
{
int progress = (distance_normalized * 100) / ANIMATION_NORMALIZED_MAX;

GColor8 *gcolor = (GColor8 *) property_animation->subject;

GColor8 *from = &property_animation->values.from.gcolor;
GColor8 *to = &property_animation->values.to.gcolor;
GColor8 *from = (GColor8 *) property_animation->values.from;
GColor8 *to = (GColor8 *) property_animation->values.to;

GColor8 new_gcolor = *from;
new_gcolor.r = from->r + (((to->r - from->r) / 100.0)*progress);
new_gcolor.g = from->g + (((to->g - from->g) / 100.0)*progress);
new_gcolor.b = from->b + (((to->g - from->b) / 100.0)*progress);
new_gcolor.a = from->a + (((to->a - from->a) / 100.0)*progress);
new_gcolor.r = LERP(from->r, to->r);
new_gcolor.g = LERP(from->g, to->g);
new_gcolor.b = LERP(from->b, to->b);
new_gcolor.a = LERP(from->a, to->a);

if (property_animation->impl.accessors.setter.gcolor)
property_animation->impl.accessors.setter.gcolor(gcolor, new_gcolor);
}

static void property_animation_teardown(Animation * animation)
{
property_animation_destroy(animation->property_anim);
}

PropertyAnimation * property_animation_create_layer_frame(struct Layer * layer, GRect * from_frame, GRect * to_frame)
{
const PropertyAnimationImplementation implementation = {
.base = {
.update = (AnimationUpdateImplementation) property_animation_update_grect,
.teardown = (AnimationTeardownImplementation) property_animation_teardown,
},
.accessors = {
.setter = { .grect = (GRectSetter) layer_set_frame },
Expand Down Expand Up @@ -131,25 +132,9 @@ PropertyAnimation * property_animation_create(const PropertyAnimationImplementat
property_animation->impl = *implementation;
property_animation->subject = subject;

// Check the type of the values
if (((GRect *) from_value)->size.w != NULL)
{
// GRect
property_animation->values.from.grect = *(GRect *) from_value;
property_animation->values.to.grect = *(GRect *) to_value;
}
else if (((GPoint *) from_value)->x != NULL)
{
// GPoint
property_animation->values.from.gpoint = *(GPoint *) from_value;
property_animation->values.to.gpoint = *(GPoint *) to_value;
}
else
{
// int16_t
property_animation->values.from.int16 = *(int16_t *) from_value;
property_animation->values.to.int16 = *(int16_t *) to_value;
}
// Set the values
property_animation->values.from = from_value;
property_animation->values.to = to_value;

return property_animation;
}
Expand Down
21 changes: 5 additions & 16 deletions rwatch/ui/animation/property_animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

#include "appmanager.h"

// Use to easily calculate changes
#define LERP(a, b) ((a) + ((b) - (a)) * distance_normalized / ANIMATION_NORMALIZED_MAX)

typedef GPoint GPointReturn;
typedef GRect GRectReturn;
typedef void (*Int16Setter)(void *subject, int16_t int16);
Expand Down Expand Up @@ -60,22 +63,8 @@ typedef struct PropertyAnimation
Animation *animation;
struct
{
union
{
GRect grect;
GPoint gpoint;
int16_t int16;
uint32_t uint32;
GColor8 gcolor;
} to;
union
{
GRect grect;
GPoint gpoint;
int16_t int16;
uint32_t uint32;
GColor8 gcolor;
} from;
void *from;
void *to;
} values;

PropertyAnimationImplementation impl;
Expand Down
21 changes: 9 additions & 12 deletions rwatch/ui/layer/scroll_layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ ScrollLayer *scroll_layer_create(GRect frame)
return slayer;
}

PropertyAnimation *prop_anim;

void scroll_layer_destroy(ScrollLayer *layer)
{
layer_destroy(layer->layer);
Expand Down Expand Up @@ -98,17 +96,16 @@ void scroll_layer_set_content_offset(ScrollLayer *scroll_layer, GPoint offset, b
{
GSize slayer_size = layer_get_frame(scroll_layer->layer).size;
GRect frame = layer_get_frame(scroll_layer->content_sublayer);
GRect to = GRect(CLAMP(offset.x, slayer_size.w - frame.size.w, 0),
CLAMP(offset.y, slayer_size.h - frame.size.h, 0),
frame.size.w,
frame.size.h);

if (frame.origin.x != to.origin.x || frame.origin.y != to.origin.y || frame.size.w != to.size.w || frame.size.h != to.size.h) {
prop_anim = property_animation_create_layer_frame(scroll_layer->content_sublayer, &frame, &to);
Animation *anim = property_animation_get_animation(prop_anim);
animation_set_duration(anim, 100);
animation_schedule(anim);
}
scroll_layer->scroll_offset = GRect(CLAMP(offset.x, slayer_size.w - frame.size.w, 0),
CLAMP(offset.y, slayer_size.h - frame.size.h, 0),
frame.size.w,
frame.size.h);

scroll_layer->animation = property_animation_create_layer_frame(scroll_layer->content_sublayer, &frame, &scroll_layer->scroll_offset);
Animation *anim = property_animation_get_animation(scroll_layer->animation);
animation_set_duration(anim, 100);
animation_schedule(anim);
}

GPoint scroll_layer_get_content_offset(ScrollLayer *scroll_layer)
Expand Down
1 change: 1 addition & 0 deletions rwatch/ui/layer/scroll_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ typedef struct ScrollLayer
Layer *content_sublayer;
Layer *shadow_sublayer;
PropertyAnimation *animation;
GRect scroll_offset;
ScrollLayerCallbacks callbacks;
void *context;
} ScrollLayer;
Expand Down

0 comments on commit 16a5301

Please sign in to comment.