rt-thread/examples/gui/demo_view_buffer_animation.c

151 lines
4.1 KiB
C
Raw Normal View History

#include <rtgui/dc.h>
#include <rtgui/rtgui_system.h>
#include <rtgui/widgets/container.h>
#include "demo_view.h"
/*
* ֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>DC<EFBFBD>ϻ<EFBFBD>ͼ<EFBFBD><EFBFBD>ʵ<EFBFBD>ֶ<EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><EFBFBD>
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҷ<EFBFBD>"
*/
static rt_int8_t dx = 1, dy = 1;
static rtgui_rect_t text_rect;
static rtgui_timer_t *timer;
static struct rtgui_dc *dc_buffer;
static void timeout(struct rtgui_timer *timer, void *parameter)
{
struct rtgui_dc *dc;
rtgui_rect_t rect;
rtgui_widget_t *widget;
/* <20>ؼ<EFBFBD>(view)ͨ<><CDA8>parameter<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݸ<EFBFBD><DDB8><EFBFBD>ʱ<EFBFBD><CAB1> */
widget = (rtgui_widget_t *)parameter;
/* <20><><EFBFBD>ÿؼ<C3BF><D8BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DC */
dc = rtgui_dc_begin_drawing(widget);
if (dc == RT_NULL) /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DC<44><43><EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ؼ<EFBFBD><D8BC>򸸿ؼ<F2B8B8BF><D8BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC>DC<44>ǻ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>ɹ<EFBFBD><C9B9>ģ<EFBFBD> */
return ;
/* <20><><EFBFBD><EFBFBD>demo view<65><77><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>жϱ߽<CFB1> */
demo_view_get_logic_rect(RTGUI_CONTAINER(widget), &rect);
rect.y2 -= 5;
/* <20>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7>ǵ<EFBFBD>һ<EFBFBD>λ<EFBFBD>ͼ */
if ((text_rect.x1 == 0) && (text_rect.y1 == 0))
{
rtgui_rect_moveto(&text_rect, rect.x1, rect.y1);
}
/* <20><><EFBFBD><EFBFBD>dx<64><78>dy */
if (text_rect.x2 >= rect.x2) dx = -1;
if (text_rect.x1 < rect.x1) dx = 1;
if (text_rect.y2 >= rect.y2) dy = -1;
if (text_rect.y1 < rect.y1) dy = 1;
/* <20>ƶ<EFBFBD><C6B6>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB> */
text_rect.x1 += dx;
text_rect.x2 += dx;
text_rect.y1 += dy;
text_rect.y2 += dy;
/* <20><>ͼ */
rect = text_rect;
rect.x2 += 2;
rect.y2 += 2;
rtgui_dc_blit(dc_buffer, NULL, dc, &rect);
/* <20><>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD> */
rtgui_dc_end_drawing(dc);
}
static rt_bool_t animation_event_handler(struct rtgui_object *object, rtgui_event_t *event)
{
struct rtgui_widget *widget = RTGUI_WIDGET(object);
if (event->type == RTGUI_EVENT_PAINT)
{
struct rtgui_dc *dc;
rtgui_rect_t rect;
/* <20><>Ϊ<EFBFBD>õ<EFBFBD><C3B5><EFBFBD>demo view<65><77><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6B1BE><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֿؼ<D6BF><D8BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڻ<EFBFBD>ͼʱ<CDBC><CAB1>Ҫ<EFBFBD><D2AA>demo view<65>Ȼ<EFBFBD>ͼ */
rtgui_container_event_handler(object, event);
/* <20><><EFBFBD>ÿؼ<C3BF><D8BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DC */
dc = rtgui_dc_begin_drawing(widget);
if (dc == RT_NULL) /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DC<44><43><EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ؼ<EFBFBD><D8BC>򸸿ؼ<F2B8B8BF><D8BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC>DC<44>ǻ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>ɹ<EFBFBD><C9B9>ģ<EFBFBD> */
return RT_FALSE;
/* <20><><EFBFBD><EFBFBD>demo view<65><77><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
demo_view_get_logic_rect(RTGUI_CONTAINER(widget), &rect);
/* <20><>ͼ */
rect = text_rect;
rtgui_rect_inflate(&rect, +1);
rtgui_dc_blit(dc_buffer, NULL, dc, &rect);
/* <20><>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD> */
rtgui_dc_end_drawing(dc);
}
else
{
/* <20><><EFBFBD><EFBFBD>Ĭ<EFBFBD>ϵ<EFBFBD><CFB5>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
return rtgui_container_event_handler(object, event);
}
return RT_FALSE;
}
static rt_bool_t animation_on_show(struct rtgui_object *object, struct rtgui_event *event)
{
rt_kprintf("buffer animation on show\n");
rtgui_timer_start(timer);
return RT_TRUE;
}
static rt_bool_t animation_on_hide(struct rtgui_object *object, struct rtgui_event *event)
{
rt_kprintf("buffer animation on hide\n");
rtgui_timer_stop(timer);
return RT_TRUE;
}
struct rtgui_container *demo_view_buffer_animation(void)
{
struct rtgui_container *container;
container = demo_view("DC <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
if (container != RT_NULL)
rtgui_object_set_event_handler(RTGUI_OBJECT(container), animation_event_handler);
rtgui_font_get_metrics(RTGUI_WIDGET_FONT(container), "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", &text_rect);
if (dc_buffer == RT_NULL)
{
rtgui_rect_t rect;
rect.x1 = 0;
rect.x2 = rtgui_rect_width(text_rect) + 2;
rect.y1 = 0;
rect.y2 = rtgui_rect_height(text_rect) + 2;
/* <20><><EFBFBD><EFBFBD> DC Buffer<65><72><EFBFBD><EFBFBD> 50<35><30><EFBFBD><EFBFBD> 50 */
dc_buffer = rtgui_dc_buffer_create(rtgui_rect_width(rect), rtgui_rect_height(rect));
RTGUI_DC_FC(dc_buffer) = RTGUI_WIDGET_BACKGROUND(container);
rtgui_dc_fill_rect(dc_buffer, &rect);
RTGUI_DC_FC(dc_buffer) = black;
rect.x1 = 1;
rect.y1 = 1;
rtgui_dc_draw_text(dc_buffer, "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", &rect);
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
timer = rtgui_timer_create(1, RT_TIMER_FLAG_PERIODIC, timeout, (void *)container);
rtgui_widget_set_onshow(RTGUI_WIDGET(container), animation_on_show);
rtgui_widget_set_onhide(RTGUI_WIDGET(container), animation_on_hide);
return container;
}