progress bar support max property

This commit is contained in:
xianjimli 2019-09-27 12:28:15 +08:00
parent 26ec22e983
commit 3d0a59768a
9 changed files with 147 additions and 62 deletions

View File

@ -389,7 +389,7 @@
#include "assets/inc/images/arrow_left_n.data"
#include "assets/inc/images/edit_clear_p.data"
#include "assets/inc/images/battery_2.data"
#endif/*WITH_STB_IMAGE*/
#endif /*WITH_STB_IMAGE*/
#ifdef WITH_VGCANVAS
#include "assets/inc/images/pointer_4.bsvg"
#include "assets/inc/images/ball.bsvg"
@ -397,13 +397,13 @@
#include "assets/inc/images/pointer_1.bsvg"
#include "assets/inc/images/pointer.bsvg"
#include "assets/inc/images/girl.bsvg"
#endif/*WITH_VGCANVAS*/
#endif /*WITH_VGCANVAS*/
#if defined(WITH_TRUETYPE_FONT)
#include "assets/inc/fonts/default.res"
#else/*WITH_TRUETYPE_FONT*/
#else /*WITH_TRUETYPE_FONT*/
#include "assets/inc/fonts/default.data"
#endif/*WITH_TRUETYPE_FONT*/
#endif/*WITH_FS_RES*/
#endif /*WITH_TRUETYPE_FONT*/
#endif /*WITH_FS_RES*/
ret_t assets_init(void) {
assets_manager_t* am = assets_manager();
@ -685,7 +685,7 @@ ret_t assets_init(void) {
assets_manager_add(am, image_pointer_1);
assets_manager_add(am, image_pointer);
assets_manager_add(am, image_girl);
#endif/*WITH_VGCANVAS*/
#endif /*WITH_VGCANVAS*/
#endif
tk_init_assets();

View File

@ -2,7 +2,7 @@
static inline ret_t on_timer(const timer_info_t* timer) {
widget_t* progress_bar = (widget_t*)timer->ctx;
uint8_t value = (PROGRESS_BAR(progress_bar)->value + 5) % 100;
float_t value = ((int)(PROGRESS_BAR(progress_bar)->value + 5)) % 100;
progress_bar_set_value(progress_bar, value);
return RET_REPEAT;
@ -10,7 +10,7 @@ static inline ret_t on_timer(const timer_info_t* timer) {
static inline ret_t on_inc(void* ctx, event_t* e) {
widget_t* progress_bar = (widget_t*)ctx;
uint8_t value = (PROGRESS_BAR(progress_bar)->value + 10) % 100;
float_t value = ((int)(PROGRESS_BAR(progress_bar)->value + 10)) % 100;
progress_bar_set_value(progress_bar, value);
(void)e;
return RET_OK;
@ -18,7 +18,7 @@ static inline ret_t on_inc(void* ctx, event_t* e) {
static inline ret_t on_dec(void* ctx, event_t* e) {
widget_t* progress_bar = (widget_t*)ctx;
uint8_t value = (PROGRESS_BAR(progress_bar)->value + 90) % 100;
float_t value = ((int)(PROGRESS_BAR(progress_bar)->value + 90)) % 100;
progress_bar_set_value(progress_bar, value);
(void)e;
return RET_OK;

View File

@ -5,6 +5,7 @@
* edit/mledit 获得焦点时选中文本。
* combo\_box 增加localize\_options选项
* edit min/max/step/type支持脚本绑定。
* progress bar支持max属性。
* 2019/09/26
* rename stream\_socket to stream\_tcp。

View File

@ -62,16 +62,14 @@ ret_t time_clock_set_anchor_for_str(float_t max_size, const char* anchor, float_
}
ret_t time_clock_set_hour_anchor(widget_t* widget, const char* anchor_x, const char* anchor_y) {
ret_t ret = RET_OK;
float_t tmp = 0.0f;
time_clock_t* time_clock = TIME_CLOCK(widget);
return_value_if_fail(time_clock != NULL, RET_BAD_PARAMS);
if(anchor_x != NULL) {
if (anchor_x != NULL) {
time_clock->hour_anchor_x = tk_str_copy(time_clock->hour_anchor_x, anchor_x);
}
if(anchor_y != NULL) {
if (anchor_y != NULL) {
time_clock->hour_anchor_y = tk_str_copy(time_clock->hour_anchor_y, anchor_y);
}
@ -79,16 +77,14 @@ ret_t time_clock_set_hour_anchor(widget_t* widget, const char* anchor_x, const c
}
ret_t time_clock_set_minute_anchor(widget_t* widget, const char* anchor_x, const char* anchor_y) {
ret_t ret = RET_OK;
float_t tmp = 0.0f;
time_clock_t* time_clock = TIME_CLOCK(widget);
return_value_if_fail(time_clock != NULL, RET_BAD_PARAMS);
if(anchor_x != NULL) {
if (anchor_x != NULL) {
time_clock->minute_anchor_x = tk_str_copy(time_clock->minute_anchor_x, anchor_x);
}
if(anchor_y != NULL) {
if (anchor_y != NULL) {
time_clock->minute_anchor_y = tk_str_copy(time_clock->minute_anchor_y, anchor_y);
}
@ -96,16 +92,14 @@ ret_t time_clock_set_minute_anchor(widget_t* widget, const char* anchor_x, const
}
ret_t time_clock_set_second_anchor(widget_t* widget, const char* anchor_x, const char* anchor_y) {
ret_t ret = RET_OK;
float_t tmp = 0.0f;
time_clock_t* time_clock = TIME_CLOCK(widget);
return_value_if_fail(time_clock != NULL, RET_BAD_PARAMS);
if(anchor_x != NULL) {
if (anchor_x != NULL) {
time_clock->second_anchor_x = tk_str_copy(time_clock->second_anchor_x, anchor_x);
}
if(anchor_y != NULL) {
if (anchor_y != NULL) {
time_clock->second_anchor_y = tk_str_copy(time_clock->second_anchor_y, anchor_y);
}
@ -255,17 +249,17 @@ static ret_t time_clock_set_prop(widget_t* widget, const char* name, const value
} else if (tk_str_eq(name, TIME_CLOCK_PROP_IMAGE)) {
return time_clock_set_image(widget, value_str(v));
} else if (tk_str_eq(name, TIME_CLOCK_PROP_HOUR_ANCHOR_X)) {
return time_clock_set_hour_anchor( widget, value_str(v), NULL);
return time_clock_set_hour_anchor(widget, value_str(v), NULL);
} else if (tk_str_eq(name, TIME_CLOCK_PROP_HOUR_ANCHOR_Y)) {
return time_clock_set_hour_anchor( widget, NULL, value_str(v));
return time_clock_set_hour_anchor(widget, NULL, value_str(v));
} else if (tk_str_eq(name, TIME_CLOCK_PROP_MINUTE_ANCHOR_X)) {
return time_clock_set_minute_anchor( widget, value_str(v), NULL);
return time_clock_set_minute_anchor(widget, value_str(v), NULL);
} else if (tk_str_eq(name, TIME_CLOCK_PROP_MINUTE_ANCHOR_Y)) {
return time_clock_set_minute_anchor( widget, NULL, value_str(v));
return time_clock_set_minute_anchor(widget, NULL, value_str(v));
} else if (tk_str_eq(name, TIME_CLOCK_PROP_SECOND_ANCHOR_X)) {
return time_clock_set_second_anchor( widget, value_str(v), NULL);
return time_clock_set_second_anchor(widget, value_str(v), NULL);
} else if (tk_str_eq(name, TIME_CLOCK_PROP_SECOND_ANCHOR_Y)) {
return time_clock_set_second_anchor( widget, NULL, value_str(v));
return time_clock_set_second_anchor(widget, NULL, value_str(v));
}
return RET_NOT_FOUND;
@ -360,7 +354,6 @@ static ret_t time_clock_on_paint_self(widget_t* widget, canvas_t* c) {
if (time_clock_load_image(widget, time_clock->hour_image, &bitmap) == RET_OK &&
time_clock_set_anchor_for_str(bitmap.w, time_clock->hour_anchor_x, &anchor_x) == RET_OK &&
time_clock_set_anchor_for_str(bitmap.h, time_clock->hour_anchor_y, &anchor_y) == RET_OK) {
float_t dx = dst.w / 2 - anchor_x;
float_t dy = dst.h / 2 - anchor_y;
float_t hour = time_clock->hour + time_clock->minute / 60.0f;
@ -372,8 +365,7 @@ static ret_t time_clock_on_paint_self(widget_t* widget, canvas_t* c) {
if (time_clock_load_image(widget, time_clock->minute_image, &bitmap) == RET_OK &&
time_clock_set_anchor_for_str(bitmap.w, time_clock->minute_anchor_x, &anchor_x) == RET_OK &&
time_clock_set_anchor_for_str(bitmap.h, time_clock->minute_anchor_y, &anchor_y) == RET_OK ) {
time_clock_set_anchor_for_str(bitmap.h, time_clock->minute_anchor_y, &anchor_y) == RET_OK) {
float_t dx = dst.w / 2 - anchor_x;
float_t dy = dst.h / 2 - anchor_y;
float_t minute = time_clock->minute + time_clock->second / 60.0f;
@ -386,7 +378,6 @@ static ret_t time_clock_on_paint_self(widget_t* widget, canvas_t* c) {
if (time_clock_load_image(widget, time_clock->second_image, &bitmap) == RET_OK &&
time_clock_set_anchor_for_str(bitmap.w, time_clock->second_anchor_x, &anchor_x) == RET_OK &&
time_clock_set_anchor_for_str(bitmap.h, time_clock->second_anchor_y, &anchor_y) == RET_OK) {
float_t dx = dst.w / 2 - anchor_x;
float_t dy = dst.h / 2 - anchor_y;
@ -403,11 +394,19 @@ static ret_t time_clock_on_paint_self(widget_t* widget, canvas_t* c) {
}
static const char* s_time_clock_properties[] = {
TIME_CLOCK_PROP_HOUR, TIME_CLOCK_PROP_MINUTE, TIME_CLOCK_PROP_SECOND,
TIME_CLOCK_PROP_IMAGE, TIME_CLOCK_PROP_BG_IMAGE, TIME_CLOCK_PROP_HOUR_IMAGE,
TIME_CLOCK_PROP_MINUTE_IMAGE, TIME_CLOCK_PROP_SECOND_IMAGE, TIME_CLOCK_PROP_HOUR_ANCHOR_X
TIME_CLOCK_PROP_HOUR_ANCHOR_Y, TIME_CLOCK_PROP_MINUTE_ANCHOR_X, TIME_CLOCK_PROP_MINUTE_ANCHOR_Y
TIME_CLOCK_PROP_SECOND_ANCHOR_X, TIME_CLOCK_PROP_SECOND_ANCHOR_Y, NULL};
TIME_CLOCK_PROP_HOUR,
TIME_CLOCK_PROP_MINUTE,
TIME_CLOCK_PROP_SECOND,
TIME_CLOCK_PROP_IMAGE,
TIME_CLOCK_PROP_BG_IMAGE,
TIME_CLOCK_PROP_HOUR_IMAGE,
TIME_CLOCK_PROP_MINUTE_IMAGE,
TIME_CLOCK_PROP_SECOND_IMAGE,
TIME_CLOCK_PROP_HOUR_ANCHOR_X TIME_CLOCK_PROP_HOUR_ANCHOR_Y,
TIME_CLOCK_PROP_MINUTE_ANCHOR_X,
TIME_CLOCK_PROP_MINUTE_ANCHOR_Y TIME_CLOCK_PROP_SECOND_ANCHOR_X,
TIME_CLOCK_PROP_SECOND_ANCHOR_Y,
NULL};
TK_DECL_VTABLE(time_clock) = {.size = sizeof(time_clock_t),
.type = WIDGET_TYPE_TIME_CLOCK,

View File

@ -275,8 +275,10 @@ ret_t time_clock_set_image(widget_t* widget, const char* image);
*
* @annotation ["scriptable"]
* @param {widget_t*} widget
* @param {const char*} anchor_x x(px为像素点px为相对百分比坐标0.0f1.0f)
* @param {const char*} anchor_y y(px为像素点px为相对百分比坐标0.0f1.0f)
* @param {const char*} anchor_x
* x(px为像素点px为相对百分比坐标0.0f1.0f)
* @param {const char*} anchor_y
* y(px为像素点px为相对百分比坐标0.0f1.0f)
*
* @return {ret_t} RET_OK表示成功
*/
@ -287,8 +289,10 @@ ret_t time_clock_set_hour_anchor(widget_t* widget, const char* anchor_x, const c
*
* @annotation ["scriptable"]
* @param {widget_t*} widget
* @param {const char*} anchor_x x(px为像素点px为相对百分比坐标0.0f1.0f)
* @param {const char*} anchor_y y(px为像素点px为相对百分比坐标0.0f1.0f)
* @param {const char*} anchor_x
* x(px为像素点px为相对百分比坐标0.0f1.0f)
* @param {const char*} anchor_y
* y(px为像素点px为相对百分比坐标0.0f1.0f)
*
* @return {ret_t} RET_OK表示成功
*/
@ -299,8 +303,10 @@ ret_t time_clock_set_minute_anchor(widget_t* widget, const char* anchor_x, const
*
* @annotation ["scriptable"]
* @param {widget_t*} widget
* @param {const char*} anchor_x x(px为像素点px为相对百分比坐标0.0f1.0f)
* @param {const char*} anchor_y y(px为像素点px为相对百分比坐标0.0f1.0f)
* @param {const char*} anchor_x
* x(px为像素点px为相对百分比坐标0.0f1.0f)
* @param {const char*} anchor_y
* y(px为像素点px为相对百分比坐标0.0f1.0f)
*
* @return {ret_t} RET_OK表示成功
*/
@ -332,7 +338,6 @@ ret_t time_clock_set_second_anchor(widget_t* widget, const char* anchor_x, const
/*public for subclass and runtime type check*/
TK_EXTERN_VTABLE(time_clock);
/*public for test*/
ret_t time_clock_set_anchor_for_str(float_t max_size, const char* anchor, float_t* image_anchor);

View File

@ -21,9 +21,25 @@
#include "tkc/mem.h"
#include "tkc/utils.h"
#include "widgets/progress_bar.h"
#include "base/widget_vtable.h"
#include "base/image_manager.h"
#include "widgets/progress_bar.h"
static float_t progress_bar_get_progress(widget_t* widget) {
progress_bar_t* progress_bar = PROGRESS_BAR(widget);
return_value_if_fail(progress_bar != NULL, 0);
float_t range = progress_bar->max > 0 ? progress_bar->max : 100;
range = tk_max(range, progress_bar->value);
return progress_bar->value / range;
}
uint32_t progress_bar_get_percent(widget_t* widget) {
float_t percent = progress_bar_get_progress(widget) * 100;
return tk_roundi(percent);
}
static ret_t progress_bar_on_paint_self(widget_t* widget, canvas_t* c) {
rect_t r;
@ -32,16 +48,19 @@ static ret_t progress_bar_on_paint_self(widget_t* widget, canvas_t* c) {
uint32_t radius = style_get_int(style, STYLE_ID_ROUND_RADIUS, 0);
const char* bg_image = style_get_str(style, STYLE_ID_BG_IMAGE, NULL);
image_draw_type_t draw_type = progress_bar->vertical ? IMAGE_DRAW_PATCH3_Y : IMAGE_DRAW_PATCH3_X;
float_t progress = progress_bar_get_progress(widget);
uint32_t progress_w = widget->w * progress;
uint32_t progress_h = widget->h * progress;
if (progress_bar->vertical) {
r.x = 0;
r.y = 0;
r.w = widget->w;
r.h = widget->h - (widget->h * progress_bar->value) / 100;
r.h = widget->h - progress_h;
} else {
r.y = 0;
r.h = widget->h;
r.w = widget->w - (widget->w * progress_bar->value) / 100;
r.w = widget->w - progress_w;
r.x = widget->w - r.w;
}
@ -54,11 +73,11 @@ static ret_t progress_bar_on_paint_self(widget_t* widget, canvas_t* c) {
if (progress_bar->vertical) {
r.x = 0;
r.w = widget->w;
r.h = (widget->h * progress_bar->value) / 100;
r.h = progress_h;
r.y = widget->h - r.h;
} else {
r.h = widget->h;
r.w = (widget->w * progress_bar->value) / 100;
r.w = progress_w;
r.y = 0;
r.x = 0;
}
@ -74,9 +93,9 @@ static ret_t progress_bar_on_paint_self(widget_t* widget, canvas_t* c) {
return RET_OK;
}
ret_t progress_bar_set_value(widget_t* widget, uint8_t value) {
ret_t progress_bar_set_value(widget_t* widget, float_t value) {
progress_bar_t* progress_bar = PROGRESS_BAR(widget);
return_value_if_fail(progress_bar != NULL && value <= 100, RET_BAD_PARAMS);
return_value_if_fail(progress_bar != NULL, RET_BAD_PARAMS);
if (progress_bar->value != value) {
char str[TK_NUM_MAX_LEN + 1];
@ -88,7 +107,7 @@ ret_t progress_bar_set_value(widget_t* widget, uint8_t value) {
widget_dispatch(widget, &e);
widget_invalidate(widget, NULL);
tk_snprintf(str, TK_NUM_MAX_LEN, "%d%%", progress_bar->value);
tk_snprintf(str, TK_NUM_MAX_LEN, "%d%%", progress_bar_get_percent(widget));
widget_set_text_utf8(widget, str);
}
@ -120,6 +139,9 @@ static ret_t progress_bar_get_prop(widget_t* widget, const char* name, value_t*
if (tk_str_eq(name, WIDGET_PROP_VALUE)) {
value_set_uint8(v, progress_bar->value);
return RET_OK;
} else if (tk_str_eq(name, WIDGET_PROP_MAX)) {
value_set_uint32(v, progress_bar->max);
return RET_OK;
} else if (tk_str_eq(name, WIDGET_PROP_VERTICAL)) {
value_set_bool(v, progress_bar->vertical);
return RET_OK;
@ -136,6 +158,8 @@ static ret_t progress_bar_set_prop(widget_t* widget, const char* name, const val
if (tk_str_eq(name, WIDGET_PROP_VALUE)) {
return progress_bar_set_value(widget, value_int(v));
} else if (tk_str_eq(name, WIDGET_PROP_MAX)) {
return progress_bar_set_max(widget, value_float(v));
} else if (tk_str_eq(name, WIDGET_PROP_VERTICAL)) {
return progress_bar_set_vertical(widget, value_bool(v));
} else if (tk_str_eq(name, WIDGET_PROP_SHOW_TEXT)) {
@ -145,8 +169,8 @@ static ret_t progress_bar_set_prop(widget_t* widget, const char* name, const val
return RET_NOT_FOUND;
}
static const char* s_progress_bar_clone_properties[] = {WIDGET_PROP_VALUE, WIDGET_PROP_VERTICAL,
WIDGET_PROP_SHOW_TEXT, NULL};
static const char* s_progress_bar_clone_properties[] = {
WIDGET_PROP_VALUE, WIDGET_PROP_MAX, WIDGET_PROP_VERTICAL, WIDGET_PROP_SHOW_TEXT, NULL};
TK_DECL_VTABLE(progress_bar) = {.size = sizeof(progress_bar_t),
.type = WIDGET_TYPE_PROGRESS_BAR,
.clone_properties = s_progress_bar_clone_properties,
@ -162,6 +186,7 @@ widget_t* progress_bar_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h)
progress_bar_t* progress_bar = PROGRESS_BAR(widget);
return_value_if_fail(progress_bar != NULL, NULL);
progress_bar->max = 100;
progress_bar->value = 0;
progress_bar->vertical = FALSE;
progress_bar->show_text = FALSE;
@ -174,3 +199,12 @@ widget_t* progress_bar_cast(widget_t* widget) {
return widget;
}
ret_t progress_bar_set_max(widget_t* widget, float_t max) {
progress_bar_t* progress_bar = PROGRESS_BAR(widget);
return_value_if_fail(progress_bar != NULL, RET_BAD_PARAMS);
progress_bar->max = max;
return widget_invalidate(widget, NULL);
}

View File

@ -71,11 +71,17 @@ BEGIN_C_DECLS
typedef struct _progress_bar_t {
widget_t widget;
/**
* @property {uint8_t} value
* @property {float_t} value
* @annotation ["set_prop","get_prop","readable","persitent","design","scriptable"]
* [0-100]
*/
uint8_t value;
float_t value;
/**
* @property {float_t} max
* @annotation ["set_prop","get_prop","readable","persitent","design","scriptable"]
* (100)
*/
float_t max;
/**
* @property {bool_t} vertical
* @annotation ["set_prop","get_prop","readable","persitent","design","scriptable"]
@ -129,11 +135,23 @@ widget_t* progress_bar_cast(widget_t* widget);
*
* @annotation ["scriptable"]
* @param {widget_t*} widget
* @param {uint8_t} value
* @param {float_t} value
*
* @return {ret_t} RET_OK表示成功
*/
ret_t progress_bar_set_value(widget_t* widget, uint8_t value);
ret_t progress_bar_set_value(widget_t* widget, float_t value);
/**
* @method progress_bar_set_max
*
*
* @annotation ["scriptable"]
* @param {widget_t*} widget
* @param {uint32_t} max
*
* @return {ret_t} RET_OK表示成功
*/
ret_t progress_bar_set_max(widget_t* widget, float_t max);
/**
* @method progress_bar_set_vertical
@ -157,6 +175,18 @@ ret_t progress_bar_set_vertical(widget_t* widget, bool_t vertical);
*/
ret_t progress_bar_set_show_text(widget_t* widget, bool_t show_text);
/**
* @method progress_bar_get_percent
*
*
* > max为100时percent和value取整后一致
* @annotation ["scriptable"]
* @param {widget_t*} widget
*
* @return {uint32_t}
*/
uint32_t progress_bar_get_percent(widget_t* widget);
#define PROGRESS_BAR(widget) ((progress_bar_t*)(progress_bar_cast(WIDGET(widget))))
/*public for subclass and runtime type check*/

View File

@ -19,6 +19,24 @@ TEST(progress_bar, basic) {
widget_destroy(s);
}
TEST(progress_bar, max) {
value_t v1;
value_t v2;
widget_t* s = progress_bar_create(NULL, 10, 20, 30, 40);
ASSERT_EQ(widget_get_prop_int(s, WIDGET_PROP_MAX, 0), 100);
value_set_int(&v1, 1000);
ASSERT_EQ(widget_set_prop(s, WIDGET_PROP_MAX, &v1), RET_OK);
ASSERT_EQ(widget_get_prop(s, WIDGET_PROP_MAX, &v2), RET_OK);
ASSERT_EQ(value_int(&v1), value_int(&v2));
ASSERT_EQ(progress_bar_set_value(s, 500), RET_OK);
ASSERT_EQ(progress_bar_get_percent(s), 50);
widget_destroy(s);
}
#include "log_change_events.inc"
TEST(ProgressBar, event) {

View File

@ -96,7 +96,6 @@ TEST(TimeClock, basic) {
ASSERT_EQ(string(value_str(&v1)), string(value_str(&v2)));
ASSERT_EQ(string(value_str(&v1)), string(t->second_anchor_y));
widget_destroy(w);
}
@ -138,5 +137,4 @@ TEST(TimeClock, set_anchor_for_str) {
ASSERT_EQ(time_clock_set_anchor_for_str(100.0f, "20", &image_anchor), RET_BAD_PARAMS);
ASSERT_EQ(image_anchor, 0.0f);
}