Skip to content

Commit

Permalink
docs(anim): Demo for pause, resume, seek
Browse files Browse the repository at this point in the history
  • Loading branch information
becseya committed May 8, 2024
1 parent ffbfeae commit f27c47d
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 16 deletions.
5 changes: 5 additions & 0 deletions examples/anim/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ Playback animation
.. lv_example:: anim/lv_example_anim_2
:language: c

Pause, resume, seek
------------------
.. lv_example:: anim/lv_example_anim_4
:language: c

Animation timeline
------------------
.. lv_example:: anim/lv_example_anim_timeline_1
Expand Down
1 change: 1 addition & 0 deletions examples/anim/lv_example_anim.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern "C" {
void lv_example_anim_1(void);
void lv_example_anim_2(void);
void lv_example_anim_3(void);
void lv_example_anim_4(void);
void lv_example_anim_timeline_1(void);

/**********************
Expand Down
99 changes: 99 additions & 0 deletions examples/anim/lv_example_anim_4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "../lv_examples.h"
#if LV_BUILD_EXAMPLES && LV_USE_SLIDER && LV_USE_BUTTON && LV_USE_SWITCH

struct { // globals
lv_anim_t * animation;
lv_obj_t * button_label;
lv_obj_t * slider;
} g;

static void anim_y_cb(void * var, int32_t v)
{
lv_obj_set_y(var, v);
}

static int32_t lv_anim_path_bouncing(const lv_anim_t * a)
{
/* ease in during reverse */
if(a->playback_now)
return lv_anim_path_cubic_bezier(a, LV_BEZIER_VAL_FLOAT(0), LV_BEZIER_VAL_FLOAT(0.2),
LV_BEZIER_VAL_FLOAT(0.5), LV_BEZIER_VAL_FLOAT(1));
/* ease out during forward */
else
return lv_anim_path_cubic_bezier(a, LV_BEZIER_VAL_FLOAT(0.2), LV_BEZIER_VAL_FLOAT(0),
LV_BEZIER_VAL_FLOAT(1), LV_BEZIER_VAL_FLOAT(0.5));
}

static void btn_start_event_handler(lv_event_t * e)
{
lv_anim_toggle_running(g.animation);
}

static void on_animation_pause(lv_anim_t *)
{
lv_label_set_text(g.button_label, "resume");
}

static void on_animation_resume(lv_anim_t *)
{
lv_label_set_text(g.button_label, "pause");
}

static void on_slider_value_change(lv_event_t * e)
{
lv_anim_set_act_time(g.animation, lv_slider_get_value(g.slider));
}

static void on_slider_pressed(lv_event_t *)
{
lv_anim_pause(g.animation);
}

static void on_slider_press_lost(lv_event_t *)
{
lv_anim_resume(g.animation);
}

/**
* Demonstrate pause, resume, seek
*/
void lv_example_anim_4(void)
{
lv_obj_t * red_dot = lv_obj_create(lv_screen_active());
lv_obj_set_style_bg_color(red_dot, lv_palette_main(LV_PALETTE_RED), 0);
lv_obj_set_style_radius(red_dot, LV_RADIUS_CIRCLE, 0);
lv_obj_set_size(red_dot, 15, 15);
lv_obj_align(red_dot, LV_ALIGN_TOP_LEFT, 50, 10);

g.animation = lv_anim_create();
lv_anim_set_var(g.animation, red_dot);
lv_anim_set_values(g.animation, 10, 220);
lv_anim_set_duration(g.animation, 400);
lv_anim_set_playback_duration(g.animation, 400);
lv_anim_set_repeat_count(g.animation, LV_ANIM_REPEAT_INFINITE);
lv_anim_set_path_cb(g.animation, lv_anim_path_bouncing);
lv_anim_set_exec_cb(g.animation, anim_y_cb);
lv_anim_set_pause_cb(g.animation, on_animation_pause);
lv_anim_set_resume_cb(g.animation, on_animation_resume);
lv_anim_trigger(g.animation);

lv_obj_t * btn_start = lv_button_create(lv_screen_active());
lv_obj_add_event_cb(btn_start, btn_start_event_handler, LV_EVENT_PRESSED, NULL);
lv_obj_add_flag(btn_start, LV_OBJ_FLAG_IGNORE_LAYOUT);
lv_obj_align(btn_start, LV_ALIGN_BOTTOM_LEFT, 10, -10);

g.button_label = lv_label_create(btn_start);
lv_label_set_text(g.button_label, "pause");
lv_obj_center(g.button_label);

g.slider = lv_slider_create(lv_screen_active());
lv_obj_add_event_cb(g.slider, on_slider_value_change, LV_EVENT_VALUE_CHANGED, NULL);
lv_obj_add_event_cb(g.slider, on_slider_pressed, LV_EVENT_PRESSED, NULL);
lv_obj_add_event_cb(g.slider, on_slider_press_lost, LV_EVENT_RELEASED, NULL);
lv_obj_add_flag(g.slider, LV_OBJ_FLAG_IGNORE_LAYOUT);
lv_obj_align(g.slider, LV_ALIGN_BOTTOM_LEFT, 120, -20);
lv_obj_set_size(g.slider, 150, 10);
lv_slider_set_range(g.slider, 0, 400);
}

#endif
30 changes: 14 additions & 16 deletions src/misc/lv_anim.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
static void anim_timer(lv_timer_t * param);
static void anim_mark_list_change(void);
static void anim_completed_handler(lv_anim_t * a);
static int32_t lv_anim_path_cubic_bezier(const lv_anim_t * a, int32_t x1,
int32_t y1, int32_t x2, int32_t y2);
static uint32_t convert_speed_to_time(uint32_t speed, int32_t start, int32_t end);
static void resolve_time(lv_anim_t * a);
static bool remove_concurrent_anims(lv_anim_t * a_current);
Expand Down Expand Up @@ -398,6 +396,20 @@ int32_t lv_anim_path_step(const lv_anim_t * a)
return a->start_value;
}

int32_t lv_anim_path_cubic_bezier(const lv_anim_t * a, int32_t x1, int32_t y1, int32_t x2, int32_t y2)
{
/*Calculate the current step*/
uint32_t t = lv_map(a->act_time, 0, a->duration, 0, LV_BEZIER_VAL_MAX);
int32_t step = lv_cubic_bezier(t, x1, y1, x2, y2);

int32_t new_value;
new_value = step * (a->end_value - a->start_value);
new_value = new_value >> LV_BEZIER_VAL_SHIFT;
new_value += a->start_value;

return new_value;
}

int32_t lv_anim_path_custom_bezier3(const lv_anim_t * a)
{
const struct _lv_anim_bezier3_para_t * para = &a->parameter.bezier3;
Expand Down Expand Up @@ -539,20 +551,6 @@ static void anim_mark_list_change(void)
lv_timer_resume(state.timer);
}

static int32_t lv_anim_path_cubic_bezier(const lv_anim_t * a, int32_t x1, int32_t y1, int32_t x2, int32_t y2)
{
/*Calculate the current step*/
uint32_t t = lv_map(a->act_time, 0, a->duration, 0, LV_BEZIER_VAL_MAX);
int32_t step = lv_cubic_bezier(t, x1, y1, x2, y2);

int32_t new_value;
new_value = step * (a->end_value - a->start_value);
new_value = new_value >> LV_BEZIER_VAL_SHIFT;
new_value += a->start_value;

return new_value;
}

static uint32_t convert_speed_to_time(uint32_t speed_or_time, int32_t start, int32_t end)
{
/*It was a simple time*/
Expand Down
11 changes: 11 additions & 0 deletions src/misc/lv_anim.h
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,17 @@ int32_t lv_anim_path_bounce(const lv_anim_t * a);
*/
int32_t lv_anim_path_step(const lv_anim_t * a);

/**
* A custom cubic bezier animation path, need to specify cubic-parameters manually
* @param a pointer to an animation
* @param x1 x1 parameter of cubic bezier
* @param y1 y1 parameter of cubic bezier
* @param x2 x2 parameter of cubic bezier
* @param y2 y2 parameter of cubic bezier
* @return the current value to set
*/
int32_t lv_anim_path_cubic_bezier(const lv_anim_t * a, int32_t x1, int32_t y1, int32_t x2, int32_t y2);

/**
* A custom cubic bezier animation path, need to specify cubic-parameters in a->parameter.bezier3
* @param a pointer to an animation
Expand Down

0 comments on commit f27c47d

Please sign in to comment.