Mercurial > MadButterfly
annotate examples/dynamic/mbbutton.c @ 511:caa087976b17 Android_Skia
Fix the issue of segmentation fault when the pattern of a mbe is not setted.
For Skia graphic engine, a mbe does not always have a pattern. For
example, a color paint does not set a pattern on a mbe for a shape.
So, we check if pattern is setted before accessing it.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Tue, 01 Dec 2009 22:55:27 +0800 |
parents | 26c302b47de1 |
children |
rev | line source |
---|---|
247 | 1 |
2 #include <stdio.h> | |
3 #include <mb.h> | |
4 #include <string.h> | |
456
26c302b47de1
Change name of header files.
Thinker K.F. Li <thinker@branda.to>
parents:
249
diff
changeset
|
5 #include "mb_af.h" |
247 | 6 |
7 | |
8 | |
9 #define CMOUSE(e) (coord_get_mouse_event(e)) | |
10 | |
11 | |
12 static void mb_button_pressed(event_t *evt, void *arg); | |
13 static void mb_button_out(event_t *evt, void *arg); | |
14 | |
15 void mb_button_add_onClick(mb_button_t *b, void (*h)(void *arg), void *arg) | |
16 { | |
17 b->press = h; | |
18 b->arg = arg; | |
19 } | |
20 | |
21 void mb_button_refresh(mb_button_t *btn) | |
22 { | |
249
ab8284c8dcee
* Add loopback reference from rdman to the backend. This is only required when we need to acquire the tman for the animation. This is not a reasonable arrangement since the animation should be backend transparent. We should not touch the backend directly from the animation. We should relocate the tman to the rdman.
wycc
parents:
247
diff
changeset
|
23 rdman_coord_changed(btn->rdman,btn->root); |
ab8284c8dcee
* Add loopback reference from rdman to the backend. This is only required when we need to acquire the tman for the animation. This is not a reasonable arrangement since the animation should be backend transparent. We should not touch the backend directly from the animation. We should relocate the tman to the rdman.
wycc
parents:
247
diff
changeset
|
24 rdman_redraw_changed(btn->rdman); |
247 | 25 } |
26 | |
27 static void mb_button_move(event_t *evt, void *arg) | |
28 { | |
29 mb_button_t *btn = (mb_button_t *) arg; | |
30 | |
31 | |
32 printf("Mouse move\n"); | |
33 coord_show(btn->active); | |
34 mb_button_refresh(btn); | |
35 } | |
36 static void mb_button_out(event_t *evt, void *arg) | |
37 { | |
38 mb_button_t *btn = (mb_button_t *) arg; | |
39 | |
40 if (btn->progm) { | |
41 mb_progm_abort(btn->progm); | |
42 btn->progm = NULL; | |
43 } | |
44 printf("mouse out\n"); | |
45 coord_hide(btn->click); | |
46 coord_hide(btn->active); | |
47 coord_show(btn->normal); | |
48 mb_button_refresh(btn); | |
49 } | |
50 | |
51 static void mb_button_show_active(event_t *evt, void *arg) | |
52 { | |
53 mb_button_t *btn = (mb_button_t *) arg; | |
54 | |
55 coord_show(btn->active); | |
56 mb_button_refresh(btn); | |
57 } | |
58 | |
59 static void mb_button_pressed(event_t *evt, void *arg) | |
60 { | |
61 mb_button_t *btn = (mb_button_t *) arg; | |
62 mb_timeval_t start, playing, now; | |
63 mb_progm_t *progm; | |
64 mb_word_t *word; | |
65 | |
66 printf("Pressed\n"); | |
67 if (btn->progm) { | |
68 mb_progm_abort(btn->progm); | |
69 btn->progm = NULL; | |
70 } | |
71 coord_show(btn->click); | |
72 coord_hide(btn->active); | |
249
ab8284c8dcee
* Add loopback reference from rdman to the backend. This is only required when we need to acquire the tman for the animation. This is not a reasonable arrangement since the animation should be backend transparent. We should not touch the backend directly from the animation. We should relocate the tman to the rdman.
wycc
parents:
247
diff
changeset
|
73 rdman_coord_changed(btn->rdman,btn->root); |
ab8284c8dcee
* Add loopback reference from rdman to the backend. This is only required when we need to acquire the tman for the animation. This is not a reasonable arrangement since the animation should be backend transparent. We should not touch the backend directly from the animation. We should relocate the tman to the rdman.
wycc
parents:
247
diff
changeset
|
74 rdman_redraw_changed(btn->rdman); |
247 | 75 |
249
ab8284c8dcee
* Add loopback reference from rdman to the backend. This is only required when we need to acquire the tman for the animation. This is not a reasonable arrangement since the animation should be backend transparent. We should not touch the backend directly from the animation. We should relocate the tman to the rdman.
wycc
parents:
247
diff
changeset
|
76 btn->progm = progm = mb_progm_new(1, btn->rdman); |
247 | 77 MB_TIMEVAL_SET(&start, 0, 500000); |
78 MB_TIMEVAL_SET(&playing, 0, 0); | |
79 word = mb_progm_next_word(progm, &start, &playing); | |
80 mb_visibility_new(VIS_HIDDEN, btn->click, word); | |
81 mb_visibility_new(VIS_VISIBLE, btn->active, word); | |
82 mb_progm_free_completed(progm); | |
83 get_now(&now); | |
249
ab8284c8dcee
* Add loopback reference from rdman to the backend. This is only required when we need to acquire the tman for the animation. This is not a reasonable arrangement since the animation should be backend transparent. We should not touch the backend directly from the animation. We should relocate the tman to the rdman.
wycc
parents:
247
diff
changeset
|
84 printf("rt = %x\n", btn->rdman->rt); |
ab8284c8dcee
* Add loopback reference from rdman to the backend. This is only required when we need to acquire the tman for the animation. This is not a reasonable arrangement since the animation should be backend transparent. We should not touch the backend directly from the animation. We should relocate the tman to the rdman.
wycc
parents:
247
diff
changeset
|
85 mb_progm_start(progm, X_MB_tman(btn->rdman->rt), &now); |
247 | 86 if (btn->press) |
87 btn->press(btn->arg); | |
88 } | |
249
ab8284c8dcee
* Add loopback reference from rdman to the backend. This is only required when we need to acquire the tman for the animation. This is not a reasonable arrangement since the animation should be backend transparent. We should not touch the backend directly from the animation. We should relocate the tman to the rdman.
wycc
parents:
247
diff
changeset
|
89 mb_button_t *mb_button_new(redraw_man_t *rdman,mb_sprite_t *sp, char *name) |
247 | 90 { |
91 mb_button_t *btn = (mb_button_t *) malloc(sizeof(mb_button_t)); | |
92 char *buf = (char *) malloc(strlen(name)+5); | |
93 | |
94 btn->root = (coord_t *) MB_SPRITE_GET_OBJ(sp, name); | |
95 sprintf(buf, "%s_normal", name); | |
96 btn->normal = (coord_t *) MB_SPRITE_GET_OBJ(sp, buf); | |
97 if (btn->normal == NULL) { | |
98 printf("Missing normal button, this is not a correct button\n"); | |
99 } | |
100 sprintf(buf, "%s_active", name); | |
101 btn->active = (coord_t *) MB_SPRITE_GET_OBJ(sp, buf); | |
102 if (btn->active == NULL) { | |
103 printf("Missing click button, this is not a correct button\n"); | |
104 } | |
105 sprintf(buf, "%s_click", name); | |
106 btn->click = (coord_t *) MB_SPRITE_GET_OBJ(sp, buf); | |
107 if (btn->active == NULL) { | |
108 printf("Missing click button, this is not a correct button\n"); | |
109 } | |
110 btn->press = NULL; | |
111 // Show only the normal button | |
112 coord_hide(btn->active); | |
113 coord_hide(btn->click); | |
114 coord_show(btn->normal); | |
115 // Move to the same position | |
116 btn->active->matrix[2] = 200; | |
117 btn->active->matrix[5] = 200; | |
118 btn->normal->matrix[2] = 200; | |
119 btn->normal->matrix[5] = 200; | |
120 btn->click->matrix[2] = 200; | |
121 btn->click->matrix[5] = 200; | |
249
ab8284c8dcee
* Add loopback reference from rdman to the backend. This is only required when we need to acquire the tman for the animation. This is not a reasonable arrangement since the animation should be backend transparent. We should not touch the backend directly from the animation. We should relocate the tman to the rdman.
wycc
parents:
247
diff
changeset
|
122 btn->rdman = rdman; |
247 | 123 btn->obs_move = subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_MOVE, mb_button_move,btn); |
124 btn->obs_press = subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_BUT_PRESS, mb_button_pressed,btn); | |
125 btn->obs_out = subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_OUT, mb_button_out,btn); | |
126 btn->progm = NULL; | |
249
ab8284c8dcee
* Add loopback reference from rdman to the backend. This is only required when we need to acquire the tman for the animation. This is not a reasonable arrangement since the animation should be backend transparent. We should not touch the backend directly from the animation. We should relocate the tman to the rdman.
wycc
parents:
247
diff
changeset
|
127 rdman_redraw_changed(rdman); |
247 | 128 return btn; |
129 } |