diff --git a/demos/SConscript b/demos/SConscript index fbd1cedb9..3ec950149 100644 --- a/demos/SConscript +++ b/demos/SConscript @@ -13,6 +13,7 @@ env.Program(os.path.join(BIN_DIR, 'demo1'), ['demo_main.c', 'demo1_app.c']); env.Program(os.path.join(BIN_DIR, 'demo2'), ['demo_main.c', 'demo2_app.c']); env.Program(os.path.join(BIN_DIR, 'demoui'), ['demo_ui_app.c', 'demo_main.c']); env.Program(os.path.join(BIN_DIR, 'demotr'), ['demo_tr_app.c', 'demo_main.c']); +env.Program(os.path.join(BIN_DIR, 'demo_animator'), ['demo_animator_app.c', 'demo_main.c']); env.Program(os.path.join(BIN_DIR, 'prefix_xml_ui'), ['prefix_xml_ui.c']); diff --git a/demos/demo_animator_app.c b/demos/demo_animator_app.c new file mode 100644 index 000000000..8b1501c04 --- /dev/null +++ b/demos/demo_animator_app.c @@ -0,0 +1,81 @@ +/** + * File: demo1_app.c + * Author: AWTK Develop Team + * Brief: basic class of all widget + * + * Copyright (c) 2018 - 2018 Guangzhou ZHIYUAN Electronics Co.,Ltd. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * License file for more details. + * + */ + +/** + * History: + * ================================================================ + * 2018-02-16 Li XianJing created + * + */ + +#include "base/timer.h" +#include "base/enums.h" +#include "base/button.h" +#include "base/dialog.h" +#include "base/image.h" +#include "base/label.h" +#include "base/mem.h" +#include "base/utils.h" +#include "base/utf8.h" +#include "base/window.h" +#include "base/slider.h" +#include "base/group_box.h" +#include "base/check_button.h" +#include "base/image_manager.h" +#include "base/progress_bar.h" +#include "widget_animators/widget_animator_move.h" + +static ret_t on_start(void* ctx, event_t* e) { + widget_animator_start((widget_animator_t*)ctx); + + return RET_OK; +} + +static ret_t on_stop(void* ctx, event_t* e) { + widget_animator_stop((widget_animator_t*)ctx); + + return RET_OK; +} + +ret_t application_init() { + widget_t* image = NULL; + widget_t* stop = NULL; + widget_t* start = NULL; + widget_t* progress_bar = NULL; + widget_t* win = window_create(NULL, 0, 0, 0, 0); + widget_animator_t* animator = NULL; + + start = button_create(win, 10, 5, 80, 30); + widget_set_text(start, L"Start"); + + stop = button_create(win, 128, 5, 80, 30); + widget_set_text(stop, L"Stop"); + + image = image_create(win, 10, 230, 100, 100); + image_set_image_name(image, "earth"); + + animator = widget_animator_move_create(image, 1000, NULL); + widget_animator_set_yoyo(animator, TRUE); + + progress_bar = progress_bar_create(win, 10, 80, 168, 20); + + animator = widget_animator_move_create(image, 1000, easing_get(EASING_SIN_INOUT)); + widget_animator_move_set_params(animator, image->x, image->y, image->x + 100, image->y + 100); + widget_animator_set_yoyo(animator, TRUE); + + widget_on(start, EVT_CLICK, on_start, animator); + widget_on(stop, EVT_CLICK, on_stop, animator); + + return RET_OK; +} diff --git a/src/SConscript b/src/SConscript index 2e20fefbf..8b6cd38c6 100644 --- a/src/SConscript +++ b/src/SConscript @@ -10,13 +10,14 @@ sources=Glob('base/*.c') +\ Glob('font/*.c') + \ Glob('blend/*.c') + \ Glob('image_loader/*.c') + \ + Glob('widget_animators/*.c') + \ ['platforms/platform_default.c', 'tk.c']; if os.environ['LCD'] == 'NANOVG': - sources += ['animator/window_animator_nanovg.c']; + sources += ['window_animators/window_animator_nanovg.c']; sources += ['lcd/lcd_nanovg.c', 'main_loop/main_loop_nanovg.c']; else: - sources += ['animator/window_animator_fb.c']; + sources += ['window_animators/window_animator_fb.c']; sources += ['lcd/lcd_sdl2.c', 'main_loop/main_loop_sdl2.c'] if os.environ['FRAME_BUFFER_FORMAT'] =='rgba8888': diff --git a/src/base/idle.c b/src/base/idle.c index aeee50e90..1d89f9b04 100644 --- a/src/base/idle.c +++ b/src/base/idle.c @@ -100,8 +100,10 @@ ret_t idle_dispatch(void) { return RET_OK; } + nr = s_idle_manager->size; idles = (idle_info_t**)s_idle_manager->elms; - for (i = 0, nr = s_idle_manager->size; i < nr; i++) { + + for (i = 0; i < nr; i++) { idle_info_t* iter = idles[i]; if (iter->on_idle) { iter->on_idle(iter); diff --git a/src/base/main_loop.c b/src/base/main_loop.c index 5ba92a05d..f6bd53ea4 100644 --- a/src/base/main_loop.c +++ b/src/base/main_loop.c @@ -35,6 +35,16 @@ ret_t main_loop_quit(main_loop_t* l) { return l->quit(l); } +ret_t main_loop_wakeup(main_loop_t* l) { + return_value_if_fail(l != NULL, RET_BAD_PARAMS); + + if(l->wakeup != NULL) { + l->wakeup(l); + } + + return RET_OK; +} + ret_t main_loop_destroy(main_loop_t* l) { return_value_if_fail(l != NULL && l->destroy != NULL, RET_BAD_PARAMS); l->running = FALSE; diff --git a/src/base/main_loop.h b/src/base/main_loop.h index 3a65586f9..705b41c02 100644 --- a/src/base/main_loop.h +++ b/src/base/main_loop.h @@ -31,11 +31,13 @@ typedef struct _main_loop_t main_loop_t; typedef ret_t (*main_loop_run_t)(main_loop_t* l); typedef ret_t (*main_loop_quit_t)(main_loop_t* l); +typedef ret_t (*main_loop_wakeup_t)(main_loop_t* l); typedef ret_t (*main_loop_destroy_t)(main_loop_t* l); struct _main_loop_t { main_loop_run_t run; main_loop_quit_t quit; + main_loop_wakeup_t wakeup; main_loop_destroy_t destroy; bool_t running; @@ -47,6 +49,7 @@ main_loop_t* main_loop(void); ret_t main_loop_set(main_loop_t* loop); ret_t main_loop_run(main_loop_t* l); +ret_t main_loop_wakeup(main_loop_t* l); ret_t main_loop_quit(main_loop_t* l); ret_t main_loop_destroy(main_loop_t* l); diff --git a/src/base/widget_animator.c b/src/base/widget_animator.c new file mode 100644 index 000000000..c5dfc531f --- /dev/null +++ b/src/base/widget_animator.c @@ -0,0 +1,127 @@ +/** + * File: widget_animator.h + * Author: AWTK Develop Team + * Brief: widget animator interface + * + * Copyright (c) 2018 - 2018 Guangzhou ZHIYUAN Electronics Co.,Ltd. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * License file for more details. + * + */ + +/** + * History: + * ================================================================ + * 2018-05-15 Li XianJing created + * + */ + +#include "base/mem.h" +#include "base/time.h" +#include "base/timer.h" +#include "base/widget_animator.h" + +ret_t widget_animator_init(widget_animator_t* animator, widget_t* widget, uint32_t duration, easing_func_t easing) { + return_value_if_fail(animator != NULL && widget != NULL, RET_BAD_PARAMS); + + animator->widget = widget; + animator->duration = duration; + animator->easing = easing != NULL ? easing : easing_get(EASING_LINEAR); + + return RET_OK; +} + +static ret_t widget_animator_on_timer(const timer_info_t* timer) { + uint32_t now = timer->now; + widget_animator_t* animator = (widget_animator_t*)(timer->ctx); + uint32_t end_time = animator->start_time + animator->duration; + float_t time_percent = ((float_t)(now - animator->start_time))/animator->duration; + + if(time_percent > 1) { + time_percent = 1; + } + + if(animator->reversed) { + time_percent = 1 - time_percent; + } + + widget_animator_update(animator, animator->easing(time_percent)); + + if(now >= end_time) { + if(animator->repeat) { + animator->start_time = now; + } else if(animator->yoyo) { + animator->start_time = now; + animator->reversed = !animator->reversed; + } else { + widget_animator_destroy(animator); + return RET_REMOVE; + } + } + + return RET_REPEAT; +} + +ret_t widget_animator_start(widget_animator_t* animator) { + return_value_if_fail(animator != NULL && animator->timer_id == 0, RET_BAD_PARAMS); + + animator->start_time = time_now_ms(); + animator->timer_id = timer_add(widget_animator_on_timer, animator, 0); + + return RET_OK; +} + +ret_t widget_animator_stop(widget_animator_t* animator) { + return_value_if_fail(animator != NULL && animator->timer_id > 0, RET_BAD_PARAMS); + + timer_remove(animator->timer_id); + animator->timer_id = 0; + + return RET_OK; +} + +ret_t widget_animator_update(widget_animator_t* animator, float_t percent) { + return_value_if_fail(animator != NULL && animator->update != NULL, RET_BAD_PARAMS); + + return animator->update(animator, percent); +} + +ret_t widget_animator_set_yoyo(widget_animator_t* animator, bool_t value) { + return_value_if_fail(animator != NULL && animator->update != NULL, RET_BAD_PARAMS); + + animator->yoyo = value; + + return RET_OK; +} + +ret_t widget_animator_set_repeat(widget_animator_t* animator, bool_t value) { + return_value_if_fail(animator != NULL && animator->update != NULL, RET_BAD_PARAMS); + + animator->repeat = value; + + return RET_OK; +} + +ret_t widget_animator_set_reversed(widget_animator_t* animator, bool_t value) { + return_value_if_fail(animator != NULL && animator->update != NULL, RET_BAD_PARAMS); + + animator->reversed = value; + + return RET_OK; +} + +ret_t widget_animator_destroy(widget_animator_t* animator) { + return_value_if_fail(animator != NULL, RET_BAD_PARAMS); + + if(animator->destroy != NULL) { + return animator->destroy(animator); + } else { + TKMEM_FREE(animator); + } + + return RET_OK; +} + diff --git a/src/base/widget_animator.h b/src/base/widget_animator.h new file mode 100644 index 000000000..3d13a572b --- /dev/null +++ b/src/base/widget_animator.h @@ -0,0 +1,69 @@ +/** + * File: widget_animator.h + * Author: AWTK Develop Team + * Brief: widget animator interface + * + * Copyright (c) 2018 - 2018 Guangzhou ZHIYUAN Electronics Co.,Ltd. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * License file for more details. + * + */ + +/** + * History: + * ================================================================ + * 2018-05-15 Li XianJing created + * + */ + +#ifndef TK_WIDGET_ANIMATOR_H +#define TK_WIDGET_ANIMATOR_H + +#include "base/easing.h" +#include "base/widget.h" + +BEGIN_C_DECLS + +struct _widget_animator_t; +typedef struct _widget_animator_t widget_animator_t; + +typedef ret_t (*widget_animator_update_t)(widget_animator_t* animator, float_t percent); +typedef ret_t (*widget_animator_destroy_t)(widget_animator_t* animator); + +/** + * @class widget_animator_t + * 控件动画接口。 + */ +typedef struct _widget_animator_t { + widget_t* widget; + bool_t yoyo; + bool_t repeat; + bool_t reversed; + uint32_t now; + uint32_t start_time; + uint32_t duration; + uint32_t timer_id; + + easing_func_t easing; + widget_animator_update_t update; + widget_animator_destroy_t destroy; +} widget_animator_t; + +ret_t widget_animator_init(widget_animator_t* animator, widget_t* widget, uint32_t duration, easing_func_t easing); +ret_t widget_animator_update(widget_animator_t* animator, float_t percent); +ret_t widget_animator_start(widget_animator_t* animator); +ret_t widget_animator_stop(widget_animator_t* animator); + +ret_t widget_animator_set_yoyo(widget_animator_t* animator, bool_t value); +ret_t widget_animator_set_repeat(widget_animator_t* animator, bool_t value); +ret_t widget_animator_set_reversed(widget_animator_t* animator, bool_t value); + +ret_t widget_animator_destroy(widget_animator_t* animator); + +END_C_DECLS + +#endif/*TK_WIDGET_ANIMATOR_H*/ + diff --git a/src/widget_animators/widget_animator_move.c b/src/widget_animators/widget_animator_move.c new file mode 100644 index 000000000..5b98ddbce --- /dev/null +++ b/src/widget_animators/widget_animator_move.c @@ -0,0 +1,58 @@ +/** + * File: widget_animator_move.h + * Author: AWTK Develop Team + * Brief: widget animator move + * + * Copyright (c) 2018 - 2018 Guangzhou ZHIYUAN Electronics Co.,Ltd. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * License file for more details. + * + */ + +/** + * History: + * ================================================================ + * 2018-05-15 Li XianJing created + * + */ + +#include "base/mem.h" +#include "widget_animators/widget_animator_move.h" + +static ret_t widget_animator_move_update(widget_animator_t* animator, float_t percent) { + widget_animator_move_t* move = (widget_animator_move_t*)animator; + return_value_if_fail(move != NULL, RET_BAD_PARAMS); + + animator->widget->x = move->x_from + (move->x_to - move->x_from) * percent; + animator->widget->y = move->y_from + (move->y_to - move->y_from) * percent; + widget_invalidate(animator->widget, NULL); + + return RET_OK; +} + +widget_animator_t* widget_animator_move_create(widget_t* widget, uint32_t duration, easing_func_t easing) { + widget_animator_t* animator = NULL; + return_value_if_fail(widget != NULL && duration > 0, NULL); + + animator = (widget_animator_t*)TKMEM_ZALLOC(widget_animator_move_t); + return_value_if_fail(widget_animator_init(animator, widget, duration, easing) == RET_OK, NULL); + animator->update = widget_animator_move_update; + + return animator; +} + +ret_t widget_animator_move_set_params(widget_animator_t* animator, xy_t x_from, xy_t y_from, xy_t x_to, xy_t y_to) { + widget_animator_move_t* move = (widget_animator_move_t*)animator; + return_value_if_fail(move != NULL, RET_BAD_PARAMS); + + move->x_to = x_to; + move->y_to = y_to; + move->x_from = x_from; + move->y_from = y_from; + + return RET_OK; +} + diff --git a/src/widget_animators/widget_animator_move.h b/src/widget_animators/widget_animator_move.h new file mode 100644 index 000000000..744f959d7 --- /dev/null +++ b/src/widget_animators/widget_animator_move.h @@ -0,0 +1,48 @@ +/** + * File: widget_animator_move.h + * Author: AWTK Develop Team + * Brief: widget animator move + * + * Copyright (c) 2018 - 2018 Guangzhou ZHIYUAN Electronics Co.,Ltd. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * License file for more details. + * + */ + +/** + * History: + * ================================================================ + * 2018-05-15 Li XianJing created + * + */ + +#ifndef TK_WIDGET_ANIMATOR_MOVE_H +#define TK_WIDGET_ANIMATOR_MOVE_H + +#include "base/widget_animator.h" + +BEGIN_C_DECLS + +/** + * @class widget_animator_move_t + * 移动控件位置动画接口。 + */ +typedef struct _widget_animator_move_t { + widget_animator_t base; + + xy_t x_to; + xy_t y_to; + xy_t x_from; + xy_t y_from; +} widget_animator_move_t; + +widget_animator_t* widget_animator_move_create(widget_t* widget, uint32_t duration, easing_func_t easing); +ret_t widget_animator_move_set_params(widget_animator_t* animator, xy_t x_from, xy_t y_from, xy_t x_to, xy_t y_to); + +END_C_DECLS + +#endif/*TK_WIDGET_ANIMATOR_MOVE_H*/ + diff --git a/src/animator/bottom_to_top.inc b/src/window_animators/bottom_to_top.inc similarity index 100% rename from src/animator/bottom_to_top.inc rename to src/window_animators/bottom_to_top.inc diff --git a/src/animator/center_scale.inc b/src/window_animators/center_scale.inc similarity index 100% rename from src/animator/center_scale.inc rename to src/window_animators/center_scale.inc diff --git a/src/animator/common.inc b/src/window_animators/common.inc similarity index 100% rename from src/animator/common.inc rename to src/window_animators/common.inc diff --git a/src/animator/fade.inc b/src/window_animators/fade.inc similarity index 100% rename from src/animator/fade.inc rename to src/window_animators/fade.inc diff --git a/src/animator/htranslate.inc b/src/window_animators/htranslate.inc similarity index 100% rename from src/animator/htranslate.inc rename to src/window_animators/htranslate.inc diff --git a/src/animator/top_to_bottom.inc b/src/window_animators/top_to_bottom.inc similarity index 100% rename from src/animator/top_to_bottom.inc rename to src/window_animators/top_to_bottom.inc diff --git a/src/animator/vtranslate.inc b/src/window_animators/vtranslate.inc similarity index 100% rename from src/animator/vtranslate.inc rename to src/window_animators/vtranslate.inc diff --git a/src/animator/window_animator_fb.c b/src/window_animators/window_animator_fb.c similarity index 100% rename from src/animator/window_animator_fb.c rename to src/window_animators/window_animator_fb.c diff --git a/src/animator/window_animator_nanovg.c b/src/window_animators/window_animator_nanovg.c similarity index 100% rename from src/animator/window_animator_nanovg.c rename to src/window_animators/window_animator_nanovg.c diff --git a/src/animator/window_animator_null.c b/src/window_animators/window_animator_null.c similarity index 100% rename from src/animator/window_animator_null.c rename to src/window_animators/window_animator_null.c