PageRenderTime 28ms CodeModel.GetById 21ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/demos/demo_sprite.c

http://ftk.googlecode.com/
C | 97 lines | 75 code | 21 blank | 1 comment | 2 complexity | 66d5a9b4d3f93e85735d738bdb60ec7f MD5 | raw file
 1#include "ftk.h"
 2
 3static Ret button_quit_clicked(void* ctx, void* obj)
 4{
 5	ftk_widget_unref(ctx);
 6	return RET_OK;
 7}
 8
 9Ret on_move(void* ctx, void* obj)
10{
11	ftk_logd("%s: %d %d\n", __func__, ftk_sprite_get_x(obj), ftk_sprite_get_y(obj));
12
13	return RET_OK;
14}
15
16Ret move_cursor(void* ctx, void* obj)
17{
18	FtkEvent* event = obj;
19
20	if(event->type == FTK_EVT_MOUSE_MOVE)
21	{
22		ftk_sprite_move(ctx, event->u.mouse.x, event->u.mouse.y);
23	}
24
25	return RET_OK;
26}
27
28static void remove_sprite(void* user_data)
29{
30	FtkSprite* sprite = user_data;
31
32	ftk_wnd_manager_remove_global_listener(ftk_default_wnd_manager(),
33			move_cursor, sprite);
34	ftk_sprite_show(sprite, 0);
35	ftk_sprite_destroy(sprite);
36}
37
38#ifdef FTK_AS_PLUGIN
39#include "ftk_app_demo.h"
40FTK_HIDE int FTK_MAIN(int argc, char* argv[]);
41FtkApp* ftk_app_demo_sprite_create()
42{
43	return ftk_app_demo_create(_("sprite"), ftk_main);
44}
45#else
46#define FTK_HIDE extern
47#endif /*FTK_AS_PLUGIN*/
48
49FTK_HIDE int FTK_MAIN(int argc, char* argv[])
50{
51	int width = 0;
52	int height = 0;
53	FtkWidget* win = NULL;
54	FtkWidget* button = NULL;
55	FtkSprite* sprite = NULL;
56	FtkBitmap* icon = NULL;
57
58	FTK_INIT(argc, argv);
59	
60	win = ftk_app_window_create();
61	width = ftk_widget_width(win);
62	height = ftk_widget_height(win);
63	
64	button = ftk_button_create(win, width/4, height/2, width/2, 60);
65	ftk_widget_set_text(button, "quit");
66	ftk_button_set_clicked_listener(button, button_quit_clicked, win);
67	ftk_window_set_focus(win, button);
68
69	ftk_widget_set_text(win, "sprite demo");
70	ftk_widget_show_all(win, 1);
71	FTK_QUIT_WHEN_WIDGET_CLOSE(win);
72
73#if 0
74	/*multi sprite still has some problems.*/
75	sprite = ftk_sprite_create();
76	icon=ftk_theme_load_image(ftk_default_theme(), "flag-32"FTK_STOCK_IMG_SUFFIX);
77	ftk_sprite_set_icon(sprite, icon);
78	ftk_sprite_move(sprite, width/2, 0);
79	ftk_sprite_set_move_listener(sprite, on_move, NULL);
80	ftk_sprite_show(sprite, 1);
81	ftk_main_loop_add_source(ftk_default_main_loop(), ftk_source_timer_create(200, timer, sprite));
82#endif
83
84	sprite = ftk_sprite_create();
85	icon=ftk_theme_load_image(ftk_default_theme(), "cursor"FTK_STOCK_IMG_SUFFIX);
86	ftk_sprite_set_icon(sprite, icon);
87	ftk_sprite_show(sprite, 1);
88	ftk_wnd_manager_add_global_listener(ftk_default_wnd_manager(), move_cursor, sprite);
89
90	ftk_widget_set_user_data(win, remove_sprite, sprite);
91
92	FTK_RUN();
93
94
95	return 0;
96}
97