awtk/demos/demo_ui_app.c

301 lines
8.2 KiB
C
Raw Normal View History

2018-03-09 21:54:46 +08:00
/**
* File: demo1_app.c
2018-05-15 09:31:58 +08:00
* Author: AWTK Develop Team
2018-03-09 21:54:46 +08:00
* Brief: basic class of all widget
*
2018-05-08 10:22:32 +08:00
* Copyright (c) 2018 - 2018 Guangzhou ZHIYUAN Electronics Co.,Ltd.
2018-03-09 21:54:46 +08:00
*
* 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 <xianjimli@hotmail.com> created
*
*/
2018-11-11 18:03:17 +08:00
#include "awtk.h"
2018-06-11 09:29:49 +08:00
#include "base/mem.h"
2018-10-22 12:34:47 +08:00
#include "base/time.h"
2018-06-11 09:29:49 +08:00
#include "base/label.h"
2018-03-09 21:54:46 +08:00
#include "base/timer.h"
#include "base/button.h"
#include "base/dialog.h"
#include "base/image.h"
#include "base/utils.h"
#include "base/window.h"
2018-10-28 17:45:34 +08:00
#include "base/main_loop.h"
2018-10-22 12:34:47 +08:00
#include "base/locale_info.h"
2018-06-11 09:29:49 +08:00
#include "base/check_button.h"
#include "base/progress_bar.h"
#include "base/image_manager.h"
#include "base/window_manager.h"
2018-07-29 12:43:39 +08:00
#include "base/widget_factory.h"
2018-03-09 21:54:46 +08:00
#include "ui_loader/ui_builder_default.h"
2018-08-23 15:33:08 +08:00
#include "ext_widgets.h"
2018-03-09 21:54:46 +08:00
2018-06-08 17:03:06 +08:00
static void install_click_hander(widget_t* widget);
2018-06-06 16:18:14 +08:00
uint32_t tk_mem_speed_test(void* buffer, uint32_t length, uint32_t* pmemcpy_speed,
uint32_t* pmemset_speed) {
uint32_t i = 0;
uint32_t cost = 0;
uint32_t total_cost = 0;
uint32_t memcpy_speed;
uint32_t memset_speed;
uint32_t max_size = 100 * 1024 * 1024;
uint32_t start = time_now_ms();
uint32_t nr = max_size / length;
for (i = 0; i < nr; i++) {
memset(buffer, 0x00, length);
}
cost = time_now_ms() - start;
total_cost = cost;
if (cost) {
memset_speed = ((max_size / cost) * 1000) / 1024;
}
start = time_now_ms();
for (i = 0; i < nr; i++) {
uint32_t half = length >> 1;
memcpy(buffer, (char*)buffer + half, half);
memcpy((char*)buffer + half, buffer, half);
}
cost = time_now_ms() - start;
total_cost += cost;
if (cost) {
memcpy_speed = ((max_size / cost) * 1000) / 1024;
}
if (pmemset_speed != NULL) {
*pmemset_speed = memset_speed;
}
if (pmemcpy_speed != NULL) {
*pmemcpy_speed = memcpy_speed;
}
return total_cost;
}
2018-06-20 17:35:32 +08:00
static void open_window(const char* name, widget_t* to_close) {
widget_t* win = to_close ? window_open_and_close(name, to_close) : window_open(name);
2018-03-09 21:54:46 +08:00
2018-06-08 17:03:06 +08:00
install_click_hander(win);
2018-03-09 21:54:46 +08:00
2018-08-04 17:11:32 +08:00
if (tk_str_eq(widget_get_type(win), WIDGET_TYPE_DIALOG)) {
2018-06-08 17:03:06 +08:00
dialog_modal(win);
}
}
2018-03-09 21:54:46 +08:00
2018-06-08 17:03:06 +08:00
static ret_t on_open_window(void* ctx, event_t* e) {
2018-06-20 17:35:32 +08:00
open_window((const char*)ctx, NULL);
2018-03-09 21:54:46 +08:00
(void)e;
return RET_OK;
}
2018-06-08 17:03:06 +08:00
static ret_t on_close(void* ctx, event_t* e) {
widget_t* win = (widget_t*)ctx;
2018-04-21 17:21:55 +08:00
(void)e;
2018-06-08 17:03:06 +08:00
return window_close(win);
2018-04-21 17:21:55 +08:00
}
2018-06-08 17:03:06 +08:00
static ret_t on_quit(void* ctx, event_t* e) {
widget_t* dialog = (widget_t*)ctx;
2018-04-21 07:43:02 +08:00
2018-06-08 17:03:06 +08:00
dialog_quit(dialog, 0);
2018-04-21 07:43:02 +08:00
(void)e;
return RET_OK;
}
2018-10-28 17:45:34 +08:00
static ret_t on_quit_app(void* ctx, event_t* e) {
2018-11-11 18:03:17 +08:00
tk_quit();
2018-10-28 17:45:34 +08:00
return RET_OK;
}
2018-06-11 09:29:49 +08:00
static ret_t on_show_fps(void* ctx, event_t* e) {
2018-06-16 11:40:13 +08:00
widget_t* button = WIDGET(ctx);
2018-06-15 11:32:03 +08:00
widget_t* widget = window_manager();
window_manager_t* wm = WINDOW_MANAGER(widget);
2018-06-11 09:29:49 +08:00
2018-06-15 11:32:03 +08:00
widget_invalidate(widget, NULL);
window_manager_set_show_fps(widget, !wm->show_fps);
widget_set_text(button, wm->show_fps ? L"Hide FPS" : L"Show FPS");
2018-06-11 09:29:49 +08:00
return RET_OK;
}
2018-06-06 17:24:28 +08:00
static ret_t on_mem_test(void* ctx, event_t* e) {
2018-06-06 16:18:14 +08:00
char text[32];
2018-06-06 17:24:28 +08:00
uint32_t size = 100 * 1024;
uint32_t memset_speed = 0;
uint32_t memcpy_speed = 0;
2018-06-16 11:40:13 +08:00
widget_t* win = WIDGET(ctx);
2018-06-08 17:03:06 +08:00
widget_t* label_memset = widget_lookup(win, "memset", TRUE);
widget_t* label_cost = widget_lookup(win, "cost", TRUE);
widget_t* label_memcpy = widget_lookup(win, "memcpy", TRUE);
2018-06-06 17:24:28 +08:00
void* buff = TKMEM_ALLOC(size);
uint32_t cost = tk_mem_speed_test(buff, size, &memcpy_speed, &memset_speed);
TKMEM_FREE(buff);
2018-06-06 16:18:14 +08:00
2018-06-06 17:24:28 +08:00
tk_snprintf(text, sizeof(text), "%ums", cost);
2018-06-08 17:03:06 +08:00
widget_set_text_utf8(label_cost, text);
2018-06-06 17:38:50 +08:00
2018-06-08 17:03:06 +08:00
tk_snprintf(text, sizeof(text), "memset: %uK/s", memset_speed);
widget_set_text_utf8(label_memset, text);
tk_snprintf(text, sizeof(text), "memcpy: %uK/s", memcpy_speed);
widget_set_text_utf8(label_memcpy, text);
2018-06-06 17:24:28 +08:00
return RET_OK;
}
2018-06-08 17:03:06 +08:00
static ret_t on_inc(void* ctx, event_t* e) {
2018-06-16 11:40:13 +08:00
widget_t* win = WIDGET(ctx);
2018-06-08 17:03:06 +08:00
widget_t* progress_bar = widget_lookup(win, "bar1", TRUE);
uint8_t value = (PROGRESS_BAR(progress_bar)->value + 10) % 100;
progress_bar_set_value(progress_bar, value);
(void)e;
return RET_OK;
}
static ret_t on_dec(void* ctx, event_t* e) {
2018-06-16 11:40:13 +08:00
widget_t* win = WIDGET(ctx);
2018-06-08 17:03:06 +08:00
widget_t* progress_bar = widget_lookup(win, "bar1", TRUE);
uint8_t value = (PROGRESS_BAR(progress_bar)->value + 90) % 100;
progress_bar_set_value(progress_bar, value);
(void)e;
return RET_OK;
}
2018-06-21 17:42:18 +08:00
static ret_t on_change_locale(void* ctx, event_t* e) {
char country[3];
char language[3];
const char* str = (const char*)ctx;
tk_strncpy(language, str, 2);
tk_strncpy(country, str + 3, 2);
2018-08-24 07:45:37 +08:00
locale_info_change(locale_info(), language, country);
2018-06-21 17:42:18 +08:00
return RET_OK;
}
2018-06-18 07:19:36 +08:00
static ret_t install_one(void* ctx, void* iter) {
widget_t* widget = WIDGET(iter);
2018-07-13 16:07:48 +08:00
if (widget->name != NULL) {
const char* name = widget->name;
2018-06-08 17:03:06 +08:00
if (strstr(name, "open:") != NULL) {
widget_on(widget, EVT_CLICK, on_open_window, (void*)(name + 5));
2018-06-29 19:33:01 +08:00
} else if (tk_str_eq(name, "memtest")) {
2018-06-08 17:03:06 +08:00
widget_t* win = widget_get_window(widget);
widget_on(widget, EVT_CLICK, on_mem_test, win);
2018-06-29 19:33:01 +08:00
} else if (tk_str_eq(name, "show_fps")) {
2018-06-15 11:32:03 +08:00
widget_on(widget, EVT_CLICK, on_show_fps, widget);
2018-06-29 19:33:01 +08:00
} else if (tk_str_eq(name, "chinese")) {
2018-06-21 17:42:18 +08:00
widget_on(widget, EVT_CLICK, on_change_locale, "zh_CN");
2018-06-29 19:33:01 +08:00
} else if (tk_str_eq(name, "english")) {
2018-06-21 17:42:18 +08:00
widget_on(widget, EVT_CLICK, on_change_locale, "en_US");
2018-06-29 19:33:01 +08:00
} else if (tk_str_eq(name, "inc_value")) {
widget_t* win = widget_get_window(widget);
widget_on(widget, EVT_CLICK, on_inc, win);
} else if (strstr(name, "dec_value") != NULL) {
2018-06-08 17:03:06 +08:00
widget_t* win = widget_get_window(widget);
widget_on(widget, EVT_CLICK, on_dec, win);
2018-06-29 19:33:01 +08:00
} else if (tk_str_eq(name, "close")) {
2018-06-08 17:03:06 +08:00
widget_t* win = widget_get_window(widget);
if (win) {
widget_on(widget, EVT_CLICK, on_close, win);
}
2018-06-29 19:33:01 +08:00
} else if (tk_str_eq(name, "quit")) {
2018-06-08 17:03:06 +08:00
widget_t* win = widget_get_window(widget);
if (win) {
widget_on(widget, EVT_CLICK, on_quit, win);
}
2018-10-28 17:45:34 +08:00
} else if (tk_str_eq(name, "exit")) {
widget_t* win = widget_get_window(widget);
if (win) {
widget_on(widget, EVT_CLICK, on_quit_app, win);
}
2018-06-08 17:03:06 +08:00
}
}
2018-06-18 07:19:36 +08:00
(void)ctx;
2018-06-08 17:03:06 +08:00
2018-06-18 07:19:36 +08:00
return RET_OK;
}
static void install_click_hander(widget_t* widget) {
widget_foreach(widget, install_one, widget);
2018-06-08 17:03:06 +08:00
}
2018-06-16 09:11:12 +08:00
#include "base/idle.h"
2018-08-24 07:45:37 +08:00
#include "base/assets_manager.h"
2018-06-16 09:11:12 +08:00
static uint32_t s_preload_nr = 0;
2018-08-24 07:45:37 +08:00
static const preload_res_t s_preload_res[] = {{ASSET_TYPE_IMAGE, "bg800x480"},
{ASSET_TYPE_IMAGE, "earth"},
{ASSET_TYPE_IMAGE, "dialog_title"},
{ASSET_TYPE_IMAGE, "rgb"},
{ASSET_TYPE_IMAGE, "rgba"}};
2018-06-16 09:11:12 +08:00
static ret_t timer_preload(const timer_info_t* timer) {
char text[64];
2018-06-16 11:40:13 +08:00
widget_t* win = WIDGET(timer->ctx);
2018-06-16 09:11:12 +08:00
uint32_t total = ARRAY_SIZE(s_preload_res);
widget_t* bar = widget_lookup(win, "bar", TRUE);
widget_t* status = widget_lookup(win, "status", TRUE);
if (s_preload_nr == total) {
2018-06-20 17:35:32 +08:00
open_window("main", win);
2018-06-16 09:11:12 +08:00
return RET_REMOVE;
} else {
uint32_t value = 0;
const preload_res_t* iter = s_preload_res + s_preload_nr++;
switch (iter->type) {
2018-08-24 07:45:37 +08:00
case ASSET_TYPE_IMAGE: {
2018-06-16 09:11:12 +08:00
bitmap_t img;
image_manager_load(image_manager(), iter->name, &img);
break;
}
default: {
2018-08-24 07:45:37 +08:00
assets_manager_ref(assets_manager(), iter->type, iter->name);
2018-06-16 09:11:12 +08:00
break;
}
}
value = (s_preload_nr * 100) / total;
tk_snprintf(text, sizeof(text), "Load: %s(%u/%u)", iter->name, s_preload_nr, total);
widget_set_value(bar, value);
widget_set_text_utf8(status, text);
return RET_REPEAT;
}
}
static ret_t show_preload_res_window() {
2018-06-22 12:01:36 +08:00
uint32_t interval = 500 / ARRAY_SIZE(s_preload_res);
2018-06-16 09:11:12 +08:00
widget_t* win = window_open("preload");
timer_add(timer_preload, win, interval);
2018-03-09 21:54:46 +08:00
return RET_OK;
}
2018-06-16 09:11:12 +08:00
ret_t application_init() {
2018-08-23 15:33:08 +08:00
tk_ext_widgets_init();
2018-07-29 12:43:39 +08:00
2018-06-16 09:11:12 +08:00
return show_preload_res_window();
}