Mercurial > MadButterfly
annotate examples/calculator/main.c @ 1535:9aff42a7e2b9 tip
Fix issue of add/remove a frame at a scene before all key frames of a layer.
When you added or removed a frame at a scene before all key frames of
a layer, frameline was not updated correctly. It seems nothing
happened, but domview is updated. This changeset fix this issue by
correcting logic for boundary case.
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Fri, 30 Sep 2011 22:07:28 +0800 |
parents | 7451af5d63ec |
children |
rev | line source |
---|---|
88 | 1 #include <stdio.h> |
432
331467b8e778
Make calcuator uses stext instead of text.
Thinker K.F. Li <thinker@branda.to>
parents:
280
diff
changeset
|
2 #include <string.h> |
186
530bb7728546
Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
3 #include <mb.h> |
88 | 4 #include "calculator_scr.h" |
5 | |
94 | 6 typedef struct _calc_data calc_data_t; |
7 struct _calc_data { | |
1025
32425882e351
Migrate examples/calculator/ to new bacend definition
Thinker K.F. Li <thinker@codemud.net>
parents:
432
diff
changeset
|
8 mb_rt_t *rt; |
88 | 9 calculator_scr_t *code; |
10 }; | |
11 | |
12 static struct { | |
13 int c; | |
14 int off; | |
15 } tgt_list[] = { | |
16 { 0, OFFSET(calculator_scr_t, but_0) }, | |
17 { 1, OFFSET(calculator_scr_t, but_1) }, | |
18 { 2, OFFSET(calculator_scr_t, but_2) }, | |
19 { 3, OFFSET(calculator_scr_t, but_3) }, | |
20 { 4, OFFSET(calculator_scr_t, but_4) }, | |
21 { 5, OFFSET(calculator_scr_t, but_5) }, | |
22 { 6, OFFSET(calculator_scr_t, but_6) }, | |
23 { 7, OFFSET(calculator_scr_t, but_7) }, | |
24 { 8, OFFSET(calculator_scr_t, but_8) }, | |
25 { 9, OFFSET(calculator_scr_t, but_9) }, | |
26 { '+', OFFSET(calculator_scr_t, but_add) }, | |
27 { '-', OFFSET(calculator_scr_t, but_minus) }, | |
28 { '*', OFFSET(calculator_scr_t, but_mul) }, | |
29 { '/', OFFSET(calculator_scr_t, but_div) }, | |
30 { '=', OFFSET(calculator_scr_t, but_eq) }, | |
31 { 'c', OFFSET(calculator_scr_t, but_clr) } | |
32 }; | |
33 | |
34 static int real_compute(int op, int v1, int v2) { | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
35 int r = v1; |
88 | 36 |
37 switch(op) { | |
38 case '+': | |
39 r = v1 + v2; | |
40 break; | |
41 case '-': | |
42 r = v1 - v2; | |
43 break; | |
44 case '*': | |
45 r = v1 * v2; | |
46 break; | |
47 case '/': | |
93 | 48 r = v1; |
89
90428161fc61
Prevent divide by zero error
Thinker K.F. Li <thinker@branda.to>
parents:
88
diff
changeset
|
49 if(v2) |
93 | 50 r /= v2; |
88 | 51 break; |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
52 case 'n': |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
53 r = v2; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
54 break; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
55 case 'N': |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
56 break; |
88 | 57 } |
58 | |
59 return r; | |
60 } | |
61 | |
94 | 62 static void show_text(calc_data_t *calc_data, int num, int saved, int op, |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
63 const char *suffix) { |
88 | 64 char buf[20]; |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
65 redraw_man_t *rdman; |
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
66 |
1025
32425882e351
Migrate examples/calculator/ to new bacend definition
Thinker K.F. Li <thinker@codemud.net>
parents:
432
diff
changeset
|
67 rdman = mb_runtime_rdman(calc_data->rt); |
88 | 68 |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
69 sprintf(buf, "%d%s", num, suffix); |
432
331467b8e778
Make calcuator uses stext instead of text.
Thinker K.F. Li <thinker@branda.to>
parents:
280
diff
changeset
|
70 sh_stext_set_text(calc_data->code->screen_text_u, buf); |
331467b8e778
Make calcuator uses stext instead of text.
Thinker K.F. Li <thinker@branda.to>
parents:
280
diff
changeset
|
71 calc_data->code->screen_text_u_style_blks[0].n_chars = strlen(buf); |
331467b8e778
Make calcuator uses stext instead of text.
Thinker K.F. Li <thinker@branda.to>
parents:
280
diff
changeset
|
72 sh_stext_set_style(calc_data->code->screen_text_u, |
331467b8e778
Make calcuator uses stext instead of text.
Thinker K.F. Li <thinker@branda.to>
parents:
280
diff
changeset
|
73 calc_data->code->screen_text_u_style_blks, 1); |
278
a90fd749af82
Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents:
192
diff
changeset
|
74 rdman_shape_changed(rdman, calc_data->code->screen_text_u); |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
75 |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
76 if(op == 'n') |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
77 sprintf(buf, "None"); |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
78 else |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
79 sprintf(buf, "%d%c", saved, op); |
432
331467b8e778
Make calcuator uses stext instead of text.
Thinker K.F. Li <thinker@branda.to>
parents:
280
diff
changeset
|
80 sh_stext_set_text(calc_data->code->saved_text, buf); |
331467b8e778
Make calcuator uses stext instead of text.
Thinker K.F. Li <thinker@branda.to>
parents:
280
diff
changeset
|
81 calc_data->code->saved_text_style_blks[0].n_chars = strlen(buf); |
331467b8e778
Make calcuator uses stext instead of text.
Thinker K.F. Li <thinker@branda.to>
parents:
280
diff
changeset
|
82 sh_stext_set_style(calc_data->code->saved_text, |
331467b8e778
Make calcuator uses stext instead of text.
Thinker K.F. Li <thinker@branda.to>
parents:
280
diff
changeset
|
83 calc_data->code->saved_text_style_blks, 1); |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
84 rdman_shape_changed(rdman, calc_data->code->saved_text); |
88 | 85 } |
86 | |
94 | 87 static void compute(calc_data_t *calc_data, coord_t *tgt) { |
88 | 88 int i; |
89 coord_t **coord_p; | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
90 redraw_man_t *rdman; |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
91 static int valid_num = 0; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
92 static int factor = 1; |
88 | 93 static int num = 0; |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
94 static int op = 'n'; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
95 static int saved = 0; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
96 char buf[2] = " "; |
88 | 97 |
98 for(i = 0; i < 16; i++) { | |
94 | 99 coord_p = (coord_t **)((void *)calc_data->code + tgt_list[i].off); |
88 | 100 if(*coord_p == (void *)tgt) |
101 break; | |
102 } | |
103 if(i >= 16) return; | |
104 | |
105 if(i < 10) { | |
106 num = num * 10 + i; | |
94 | 107 show_text(calc_data, num * factor, saved, op, ""); |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
108 valid_num = 1; |
88 | 109 } else { |
110 switch(tgt_list[i].c) { | |
111 case 'c': | |
112 saved = num = 0; | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
113 factor = 1; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
114 valid_num = 0; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
115 op = 'n'; |
94 | 116 show_text(calc_data, 0, saved, op, ""); |
88 | 117 break; |
118 | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
119 case '-': |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
120 if(!valid_num) { |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
121 factor *= -1; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
122 valid_num = 1; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
123 break; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
124 } |
88 | 125 case '+': |
126 case '*': | |
127 case '/': | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
128 saved = real_compute(op, saved, num * factor); |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
129 buf[0] = tgt_list[i].c; |
94 | 130 show_text(calc_data, saved, saved, 'n', buf); |
90 | 131 op = tgt_list[i].c; |
88 | 132 num = 0; |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
133 factor = 1; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
134 valid_num = 0; |
88 | 135 break; |
136 | |
137 case '=': | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
138 saved = real_compute(op, saved, num * factor); |
94 | 139 show_text(calc_data, saved, 0, 'n', ""); |
88 | 140 num = 0; |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
141 op = 'N'; |
88 | 142 break; |
143 } | |
144 } | |
1025
32425882e351
Migrate examples/calculator/ to new bacend definition
Thinker K.F. Li <thinker@codemud.net>
parents:
432
diff
changeset
|
145 rdman = mb_runtime_rdman(calc_data->rt); |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
146 rdman_redraw_changed(rdman); |
88 | 147 } |
148 | |
149 static void buttons_handler(event_t *evt, void *arg) { | |
94 | 150 calc_data_t *calc_data = (calc_data_t *)arg; |
88 | 151 |
152 switch(evt->type) { | |
153 case EVT_MOUSE_BUT_PRESS: | |
271 | 154 compute(calc_data, (coord_t *)evt->cur_tgt->obj); |
88 | 155 break; |
156 } | |
157 } | |
158 | |
94 | 159 static void setup_observers(calc_data_t *calc_data) { |
88 | 160 calculator_scr_t *calculator_scr; |
161 subject_t *subject; | |
90 | 162 coord_t *coord; |
163 int off; | |
164 int i; | |
88 | 165 |
94 | 166 calculator_scr = calc_data->code; |
88 | 167 |
90 | 168 for(i = 0; i < 16; i++) { |
169 off = tgt_list[i].off; | |
93 | 170 coord = OFF2TYPE(calculator_scr, off, coord_t *); |
90 | 171 subject = coord_get_mouse_event(coord); |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
172 subject_add_observer(subject, buttons_handler, calc_data); |
90 | 173 } |
88 | 174 } |
175 | |
176 int main(int argc, char * const argv[]) { | |
1025
32425882e351
Migrate examples/calculator/ to new bacend definition
Thinker K.F. Li <thinker@codemud.net>
parents:
432
diff
changeset
|
177 mb_rt_t *rt; |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
178 redraw_man_t *rdman; |
88 | 179 calculator_scr_t *calculator_scr; |
94 | 180 calc_data_t calc_data; |
88 | 181 |
1114
7451af5d63ec
Make tank and calculator works for console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
1025
diff
changeset
|
182 #ifdef CONSOLE_BACKEND |
7451af5d63ec
Make tank and calculator works for console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
1025
diff
changeset
|
183 rt = mb_runtime_new(NULL, 300, 400); |
7451af5d63ec
Make tank and calculator works for console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
1025
diff
changeset
|
184 #else |
1025
32425882e351
Migrate examples/calculator/ to new bacend definition
Thinker K.F. Li <thinker@codemud.net>
parents:
432
diff
changeset
|
185 rt = mb_runtime_new(":0.0", 300, 400); |
1114
7451af5d63ec
Make tank and calculator works for console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
1025
diff
changeset
|
186 #endif |
88 | 187 |
1025
32425882e351
Migrate examples/calculator/ to new bacend definition
Thinker K.F. Li <thinker@codemud.net>
parents:
432
diff
changeset
|
188 rdman = mb_runtime_rdman(rt); |
130
3a4d6179e6a9
change mb_c_source.m4 and mb_c_header.m4 to specify parent for SVG object
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
189 calculator_scr = calculator_scr_new(rdman, rdman->root_coord); |
88 | 190 |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
191 calc_data.rt = rt; |
94 | 192 calc_data.code = calculator_scr; |
193 setup_observers(&calc_data); | |
1114
7451af5d63ec
Make tank and calculator works for console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
1025
diff
changeset
|
194 |
7451af5d63ec
Make tank and calculator works for console backend
Thinker K.F. Li <thinker@codemud.net>
parents:
1025
diff
changeset
|
195 mb_runtime_flush(rt); |
88 | 196 |
1025
32425882e351
Migrate examples/calculator/ to new bacend definition
Thinker K.F. Li <thinker@codemud.net>
parents:
432
diff
changeset
|
197 mb_runtime_event_loop(rt); |
88 | 198 |
199 calculator_scr_free(calculator_scr); | |
1025
32425882e351
Migrate examples/calculator/ to new bacend definition
Thinker K.F. Li <thinker@codemud.net>
parents:
432
diff
changeset
|
200 mb_runtime_free(rt); |
88 | 201 |
202 return 0; | |
203 } |