Skip to content

Commit

Permalink
Clean up meter_arm logic; add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cguldner committed Feb 7, 2017
1 parent e07b308 commit 522dbd5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/c/auxiliary.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ char * get_tempo_marking(void) {
* Updates the bpm to the closest tempo marking
* @param dir - 1 for higher, -1 for lower
*/
int change_tempo_marking(int dir) {
static int change_tempo_marking(int dir) {
if(dir != 1 && dir != -1) {
APP_LOG(APP_LOG_LEVEL_ERROR, "invalid direction of %d", dir);
return -1;
Expand Down
7 changes: 3 additions & 4 deletions src/c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void select_play_click_handler(ClickRecognizerRef recognizer, void *context) {
metro_timer = NULL;

toggle_colors(false);
reset_animation();
reset_meter_arm_animation();
}
}

Expand Down Expand Up @@ -129,7 +129,7 @@ void down_tempo_click_handler(ClickRecognizerRef recognizer, void *context) {
}

void window_load(Window *window) {
Layer *window_layer = window_get_root_layer(window);
window_layer = window_get_root_layer(window);
GRect bounds = layer_get_bounds(window_layer);
int status_bar_h = PBL_IF_RECT_ELSE(16, 24);

Expand Down Expand Up @@ -239,11 +239,10 @@ void init(void) {

void deinit(void) {
app_timer_cancel(metro_timer);
// Destroy main Window
prv_save_settings();
window_destroy(window);

gpath_destroy(s_meter_path);
window_destroy(window);
}

int main(void) {
Expand Down
2 changes: 2 additions & 0 deletions src/c/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

Window *window;

Layer *window_layer;

// The actual value of the metronome
TextLayer *bpm_text_layer,
// What tempo group the bpm falls under
Expand Down
46 changes: 28 additions & 18 deletions src/c/meter_arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@

static int angle_bounds;

/**
* Creating the meter arm
*/
// The metronome arm drawn as a polygon
const GPathInfo METER_ARM_POINTS = {
// This is the amount of points
// Number of points
8,
// The shape to draw
// Position of each point
(GPoint []) {{-METER_ARM_W - METER_BOX_W, -METER_ARM_H}, {-METER_ARM_W - METER_BOX_W, -METER_ARM_H - METER_BOX_H}, {METER_ARM_W + METER_BOX_W, -METER_ARM_H - METER_BOX_H},
{METER_ARM_W + METER_BOX_W, -METER_ARM_H}, {METER_ARM_W, -METER_ARM_H}, {METER_ARM_W, 0}, {-METER_ARM_W, 0}, {-METER_ARM_W, -METER_ARM_H}}
};

void create_meter_arm(void) {
Layer *window_layer = window_get_root_layer(window);
GRect bounds = layer_get_bounds(window_layer);
angle_bounds = get_angle_bounds();

Expand All @@ -41,44 +37,58 @@ void path_layer_update_callback(Layer *layer, GContext *ctx) {
gpath_draw_filled(ctx, s_meter_path);
}

int path_angle_add(int angle) {
/**
* Updates the angle by the given amount, circling around
* if the angle would end up being greater than 360
* @param angle - How much to increase the angle by
*/
static int path_angle_add(int angle) {
return s_path_angle = (s_path_angle + angle) % 360;
}

/**
* Put the meter arm in the original position
*/
static void reset_path_angle() {
path_angle_add(-angle_bounds - s_path_angle);
}

int get_angle_bounds(void) {
int angle = 0;
float ab = ACTION_BAR_WIDTH;
GRect wb = layer_get_bounds(window_get_root_layer(window));
GRect wb = layer_get_bounds(window_layer);

// For square watches
/* Gets the ratio of the triangle sides between the horizontal and the meter arm */
float hypotenuse = METER_ARM_H + sm_sqrt(sm_powint(METER_BOX_W + METER_ARM_W, 2) + sm_powint(METER_BOX_H, 2));
// Meter arm is centered with respect to action bar, so just half that distance
#ifdef PBL_RECT
float temp = (wb.size.w - ab) / (2 * (METER_ARM_H + sm_sqrt(sm_powint(METER_BOX_W + METER_ARM_W, 2) + sm_powint(METER_BOX_H, 2))));
float horizontal_leg = (wb.size.w - ab) / 2;
#else
// For round watches
float temp = (.5*wb.size.w - ab) / (METER_ARM_H + sm_sqrt(sm_powint(METER_BOX_W + METER_ARM_W, 2) + sm_powint(METER_BOX_H, 2)));
// Meter arm is in center of screen, so half the distance of the screen minus the action bar width
float horizontal_leg = .5 * wb.size.w - ab - 5;
#endif

angle = sm_acosd(temp);
angle = sm_acosd(horizontal_leg / hypotenuse);

return 90 - angle;
}

void reset_animation(void) {
void reset_meter_arm_animation(void) {
if(forward_animate) animation_destroy(forward_animate);
if(backward_animate) animation_destroy(backward_animate);
path_angle_add(-angle_bounds - s_path_angle);
reset_path_angle();
layer_mark_dirty(s_path_layer);
}

void forward_animate_update(Animation *animation, const AnimationProgress progress) {
static void forward_animate_update(Animation *animation, const AnimationProgress progress) {
int progress_percent = ((int)progress * 100) / ANIMATION_NORMALIZED_MAX;
int delta = angle_bounds*2*progress_percent/100 - (s_path_angle+angle_bounds);

path_angle_add(delta);
layer_mark_dirty(s_path_layer);
}

void backward_animate_update(Animation *animation, const AnimationProgress progress) {
static void backward_animate_update(Animation *animation, const AnimationProgress progress) {
int progress_percent = ((int)progress * 100) / ANIMATION_NORMALIZED_MAX;
int delta = -angle_bounds*2*progress_percent/100 - (s_path_angle-angle_bounds);

Expand Down Expand Up @@ -112,7 +122,7 @@ void animate_meter_arm(bool direction, int duration) {
}

void toggle_meter_arm_visibility() {
GRect bounds = layer_get_bounds(window_get_root_layer(window));
GRect bounds = layer_get_bounds(window_layer);
// Centers the text on the round watches, looks weird if off center
int action_bar_w = ACTION_BAR_WIDTH==30 ? ACTION_BAR_WIDTH : 0,
bpm_y_pos, tempo_y_pos;
Expand Down
21 changes: 6 additions & 15 deletions src/c/meter_arm.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
#include "main.h"
#include "SmallMaths.h"

/**
* Creating the meter arm
*/
const GPathInfo METER_ARM_POINTS;
extern const GPathInfo METER_ARM_POINTS;
Layer *s_path_layer;
GPath *s_meter_path;
int s_path_angle;
int meter_color;

/**
* Creates the meter arm and sets it to the max angle on the left
*/
void create_meter_arm(void);

/**
Expand All @@ -21,13 +21,6 @@ void create_meter_arm(void);
*/
void path_layer_update_callback(Layer *layer, GContext *ctx);

/**
* Updates the angle by the given amount, circling around
* if the angle would end up being greater than 360
* @param angle - How much to increase the angle by
*/
int path_angle_add(int angle);

/**
* Return the bounds that the metronome arm can travel
*/
Expand All @@ -40,11 +33,9 @@ Animation *forward_animate, *backward_animate;
AnimationImplementation forward_impl, backward_impl;

/**
* Animate the meter arm
* Turns off the animation, and puts the arm back in its original position
*/
void reset_animation(void);
void forward_animate_update(Animation *animation, const AnimationProgress progress);
void backward_animate_update(Animation *animation, const AnimationProgress progress);
void reset_meter_arm_animation(void);

/**
* Sets up and executes the animation in the given direction for the duration
Expand Down

0 comments on commit 522dbd5

Please sign in to comment.