Mercurial > MadButterfly
comparison pyink/frameline.py @ 1152:d333baa702a9
Refactory drawing function of frameline to frameline_draw
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Mon, 27 Dec 2010 14:18:05 +0800 |
parents | 6586cd10c92f |
children | 5abf419d66e5 |
comparison
equal
deleted
inserted
replaced
1151:71c72e8d6755 | 1152:d333baa702a9 |
---|---|
103 win.draw_layout(gc, mark_x, number_y, layout) | 103 win.draw_layout(gc, mark_x, number_y, layout) |
104 pass | 104 pass |
105 pass | 105 pass |
106 pass | 106 pass |
107 | 107 |
108 ## Show frame status of a layer | 108 ## \brief Drawing on the screen for a frameline. |
109 # | 109 # |
110 # \section frameline_sigs Signals | 110 # This class contain all functions that drawing thing on the screen for a |
111 # - 'frame-button-pree' for user press on a frame. | 111 # frameline. It is used by descendants to drawing a frameline. This class is |
112 # - callback(widget, frame_idx, button) | 112 # only responsible to draw screen, the logical part is the responsible of |
113 # deriving classes. | |
113 # | 114 # |
114 # All methos that change state of the frameline, must call methods to update | 115 # This class should only include functions about drawing, no any control, logic |
115 # the screen. | 116 # function, and state should be implemented here. This class should change/or |
117 # read states of instances. It is stateless. | |
116 # | 118 # |
117 class frameline(gtk.DrawingArea): | 119 class frameline_draw(gtk.DrawingArea): |
118 _type = 0 | 120 _type = 0 |
119 _frame_width = 10 # Width for each frame is 10 pixels | 121 _frame_width = 10 # Width for each frame is 10 pixels |
120 _select_color = 0xee2222 # color of border of selected frame | 122 _select_color = 0xee2222 # color of border of selected frame |
121 _key_mark_color = 0x000000 # color of marks for key frames. | 123 _key_mark_color = 0x000000 # color of marks for key frames. |
122 _key_mark_sz = 4 # width and height of a key frame mark | 124 _key_mark_sz = 4 # width and height of a key frame mark |
127 _normal_bgcolors = [0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xcccccc] | 129 _normal_bgcolors = [0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xcccccc] |
128 _normal_border = 0xaaaaaa # border color of normal frames. | 130 _normal_border = 0xaaaaaa # border color of normal frames. |
129 _active_border = 0xff3030 # border color of an active frame | 131 _active_border = 0xff3030 # border color of an active frame |
130 _hover_border_color = 0xa0a0a0 # border when the pointer over a frame | 132 _hover_border_color = 0xa0a0a0 # border when the pointer over a frame |
131 | 133 |
134 def __new__(clz, *args): | |
135 if not clz._type: | |
136 clz._type = gobject.type_register(clz) | |
137 pass | |
138 fl_obj = gobject.new(clz._type) | |
139 return fl_obj | |
140 | |
141 def _draw_tween(self, first_idx, last_idx, tween_type): | |
142 win = self.window | |
143 w_x, w_y, w_w, w_h, depth = win.get_geometry() | |
144 | |
145 # | |
146 # Get background color of a tween | |
147 # | |
148 bg_idx = tween_type | |
149 bg_color_v = self._tween_bgcolors[bg_idx] | |
150 bg_color_rgb = color_to_rgb(bg_color_v) | |
151 bg_color = gtk.gdk.Color(*bg_color_rgb) | |
152 | |
153 gc = self._gc | |
154 gc.set_rgb_fg_color(bg_color) | |
155 | |
156 draw_x = first_idx * self._frame_width + 1 | |
157 draw_w = (last_idx - first_idx + 1) * self._frame_width - 1 | |
158 | |
159 win.draw_rectangle(gc, True, draw_x, 0, draw_w, w_h) | |
160 | |
161 # | |
162 # Set color of tween line | |
163 # | |
164 line_v = self._tween_color | |
165 line_rgb = color_to_rgb(line_v) | |
166 line_color = gtk.gdk.Color(*line_rgb) | |
167 gc.set_rgb_fg_color(line_color) | |
168 | |
169 # | |
170 # Draw tween line | |
171 # | |
172 line_x1 = int((first_idx + 0.5) * self._frame_width) | |
173 line_x2 = line_x1 + (last_idx - first_idx) * self._frame_width | |
174 line_y = int(w_h * 2 / 3) | |
175 win.draw_line(gc, line_x1, line_y, line_x2, line_y) | |
176 pass | |
177 | |
178 def _draw_normal_frame(self, idx): | |
179 win = self.window | |
180 w_x, w_y, w_w, w_h, depth = win.get_geometry() | |
181 | |
182 gc = self._gc | |
183 bg_idx = idx % len(self._normal_bgcolors) | |
184 rgb = color_to_rgb(self._normal_bgcolors[bg_idx]) | |
185 color = gtk.gdk.Color(*rgb) | |
186 gc.set_rgb_fg_color(color) | |
187 | |
188 f_x = self._frame_width * idx | |
189 win.draw_rectangle(gc, True, f_x + 1, 0, self._frame_width - 1, w_h) | |
190 next_f_x = f_x + self._frame_width | |
191 | |
192 border_rgb = color_to_rgb(self._normal_border) | |
193 border_color = gtk.gdk.Color(*border_rgb) | |
194 gc.set_rgb_fg_color(border_color) | |
195 gc.set_line_attributes(1, gtk.gdk.LINE_SOLID, | |
196 gtk.gdk.CAP_BUTT, gtk.gdk.JOIN_MITER) | |
197 win.draw_line(gc, next_f_x, 0, next_f_x, w_h) | |
198 pass | |
199 | |
200 ## \brief Draw a bottom line from start to the point before stop frame. | |
201 # | |
202 def _draw_bottom_line(self, start_idx, stop_idx): | |
203 win = self.window | |
204 w_x, w_y, w_w, w_h, depth = win.get_geometry() | |
205 gc = self._gc | |
206 | |
207 border_rgb = color_to_rgb(self._normal_border) | |
208 border_color = gtk.gdk.Color(*border_rgb) | |
209 gc.set_rgb_fg_color(border_color) | |
210 start_x = start_idx * self._frame_width | |
211 stop_x = stop_idx * self._frame_width | |
212 win.draw_line(gc, start_x, w_h - 1, stop_x, w_h - 1) | |
213 pass | |
214 | |
215 def _draw_active(self, idx): | |
216 win = self.window | |
217 w_x, w_y, w_w, w_h, depth = win.get_geometry() | |
218 | |
219 color_v = self._active_border | |
220 color_rgb = color_to_rgb(color_v) | |
221 color = gtk.gdk.Color(*color_rgb) | |
222 | |
223 gc = self._gc | |
224 gc.set_rgb_fg_color(color) | |
225 | |
226 line_x1 = idx * self._frame_width | |
227 line_x2 = line_x1 + self._frame_width | |
228 | |
229 win.draw_line(gc, line_x1, 0, line_x1, w_h) | |
230 win.draw_line(gc, line_x2, 0, line_x2, w_h) | |
231 win.draw_line(gc, line_x1, w_h - 1, line_x2, w_h - 1) | |
232 win.draw_line(gc, line_x1, 0, line_x2, 0) | |
233 pass | |
234 | |
235 def _draw_keyframe_(self, frame_idx): | |
236 win = self.window | |
237 w_x, w_y, w_w, w_h, depth = win.get_geometry() | |
238 | |
239 color_v = self._key_mark_color | |
240 color_rgb = color_to_rgb(color_v) | |
241 color = gtk.gdk.Color(*color_rgb) | |
242 | |
243 gc = self._gc | |
244 gc.set_rgb_fg_color(color) | |
245 | |
246 mark_sz = self._key_mark_sz | |
247 mark_x = int((frame_idx + 0.5) * self._frame_width - mark_sz / 2) | |
248 mark_y = w_h * 2 / 3 - mark_sz / 2 | |
249 | |
250 win.draw_rectangle(gc, True, mark_x, mark_y, mark_sz, mark_sz) | |
251 pass | |
252 | |
253 ## \brief Show a mark for the pointer for a frame. | |
254 # | |
255 def _draw_hover(self, frame_idx): | |
256 win = self.window | |
257 w_x, w_y, w_w, w_h, depth = win.get_geometry() | |
258 gc = self._gc | |
259 | |
260 color_rgb = color_to_rgb(self._hover_border_color) | |
261 color = gtk.gdk.Color(*color_rgb) | |
262 gc.set_rgb_fg_color(color) | |
263 | |
264 line_x1 = frame_idx * self._frame_width + 1 | |
265 line_x2 = line_x1 + self._frame_width - 2 | |
266 | |
267 win.draw_line(gc, line_x1, 1, line_x1, w_h - 2) | |
268 win.draw_line(gc, line_x2, 1, line_x2, w_h - 2) | |
269 win.draw_line(gc, line_x1, 1, line_x2, 1) | |
270 win.draw_line(gc, line_x1, w_h - 2, line_x2, w_h - 2) | |
271 pass | |
272 pass | |
273 | |
274 ## Show frame status of a layer | |
275 # | |
276 # \section frameline_sigs Signals | |
277 # - 'frame-button-pree' for user press on a frame. | |
278 # - callback(widget, frame_idx, button) | |
279 # | |
280 # All methos that change state of the frameline, must call methods to update | |
281 # the screen. | |
282 # | |
283 class frameline(frameline_draw): | |
284 _sig_frame_but_press = None | |
285 | |
132 # tween types | 286 # tween types |
133 TWEEN_TYPE_NONE = 0 | 287 TWEEN_TYPE_NONE = 0 |
134 TWEEN_TYPE_MOVE = 1 | 288 TWEEN_TYPE_MOVE = 1 |
135 TWEEN_TYPE_SHAPE = 2 | 289 TWEEN_TYPE_SHAPE = 2 |
136 | 290 |
137 FRAME_BUT_PRESS = 'frame-button-press' | 291 FRAME_BUT_PRESS = 'frame-button-press' |
138 | 292 |
139 def __new__(clz, *args): | 293 def __new__(clz, *args): |
140 if not frameline._type: | 294 fl_obj = frameline_draw.__new__(clz, *args) |
141 frameline._type = gobject.type_register(frameline) | 295 |
296 if not clz._sig_frame_but_press: | |
142 but_press = gobject.signal_new(frameline.FRAME_BUT_PRESS, | 297 but_press = gobject.signal_new(frameline.FRAME_BUT_PRESS, |
143 frameline._type, | 298 frameline._type, |
144 gobject.SIGNAL_RUN_FIRST, | 299 gobject.SIGNAL_RUN_FIRST, |
145 gobject.TYPE_NONE, | 300 gobject.TYPE_NONE, |
146 (gobject.TYPE_INT, | 301 (gobject.TYPE_INT, |
147 gobject.TYPE_INT)) | 302 gobject.TYPE_INT)) |
148 frameline._sig_frame_but_press = but_press | 303 clz._sig_frame_but_press = but_press |
149 pass | 304 pass |
150 fl_obj = gobject.new(frameline._type) | |
151 return fl_obj | 305 return fl_obj |
152 | 306 |
153 def __init__(self, num_frames=20): | 307 def __init__(self, num_frames=20): |
154 self.connect('button-press-event', self._press_hdl) | 308 self.connect('button-press-event', self._press_hdl) |
155 self.connect('expose-event', self._fl_expose) | 309 self.connect('expose-event', self._fl_expose) |
272 self._drawing = True | 426 self._drawing = True |
273 pass | 427 pass |
274 self.update() | 428 self.update() |
275 pass | 429 pass |
276 | 430 |
277 def _draw_tween(self, first_pos, last_pos): | |
278 win = self.window | |
279 w_x, w_y, w_w, w_h, depth = win.get_geometry() | |
280 first_key = self._keys[first_pos] | |
281 last_key = self._keys[last_pos] | |
282 | |
283 # | |
284 # Get background color of a tween | |
285 # | |
286 bg_idx = first_key.right_tween_type | |
287 bg_color_v = self._tween_bgcolors[bg_idx] | |
288 bg_color_rgb = color_to_rgb(bg_color_v) | |
289 bg_color = gtk.gdk.Color(*bg_color_rgb) | |
290 | |
291 gc = self._gc | |
292 gc.set_rgb_fg_color(bg_color) | |
293 | |
294 draw_x = first_key.idx * self._frame_width + 1 | |
295 draw_w = (last_key.idx - first_key.idx + 1) * self._frame_width - 1 | |
296 | |
297 win.draw_rectangle(gc, True, draw_x, 0, draw_w, w_h) | |
298 | |
299 # | |
300 # Set color of tween line | |
301 # | |
302 line_v = self._tween_color | |
303 line_rgb = color_to_rgb(line_v) | |
304 line_color = gtk.gdk.Color(*line_rgb) | |
305 gc.set_rgb_fg_color(line_color) | |
306 | |
307 # | |
308 # Draw tween line | |
309 # | |
310 line_x1 = int((first_key.idx + 0.5) * self._frame_width) | |
311 line_x2 = line_x1 + (last_key.idx - first_key.idx) * self._frame_width | |
312 line_y = int(w_h * 2 / 3) | |
313 win.draw_line(gc, line_x1, line_y, line_x2, line_y) | |
314 pass | |
315 | |
316 def _draw_normal_frame(self, idx): | |
317 win = self.window | |
318 w_x, w_y, w_w, w_h, depth = win.get_geometry() | |
319 | |
320 gc = self._gc | |
321 bg_idx = idx % len(self._normal_bgcolors) | |
322 rgb = color_to_rgb(self._normal_bgcolors[bg_idx]) | |
323 color = gtk.gdk.Color(*rgb) | |
324 gc.set_rgb_fg_color(color) | |
325 | |
326 f_x = self._frame_width * idx | |
327 win.draw_rectangle(gc, True, f_x + 1, 0, self._frame_width - 1, w_h) | |
328 next_f_x = f_x + self._frame_width | |
329 | |
330 border_rgb = color_to_rgb(self._normal_border) | |
331 border_color = gtk.gdk.Color(*border_rgb) | |
332 gc.set_rgb_fg_color(border_color) | |
333 gc.set_line_attributes(1, gtk.gdk.LINE_SOLID, | |
334 gtk.gdk.CAP_BUTT, gtk.gdk.JOIN_MITER) | |
335 win.draw_line(gc, next_f_x, 0, next_f_x, w_h) | |
336 pass | |
337 | |
338 ## \brief Draw a bottom line from start to the point before stop frame. | |
339 # | |
340 def _draw_bottom_line(self, start_idx, stop_idx): | |
341 win = self.window | |
342 w_x, w_y, w_w, w_h, depth = win.get_geometry() | |
343 gc = self._gc | |
344 | |
345 border_rgb = color_to_rgb(self._normal_border) | |
346 border_color = gtk.gdk.Color(*border_rgb) | |
347 gc.set_rgb_fg_color(border_color) | |
348 start_x = start_idx * self._frame_width | |
349 stop_x = stop_idx * self._frame_width | |
350 win.draw_line(gc, start_x, w_h - 1, stop_x, w_h - 1) | |
351 pass | |
352 | |
353 def _draw_active(self, idx): | |
354 win = self.window | |
355 w_x, w_y, w_w, w_h, depth = win.get_geometry() | |
356 | |
357 color_v = self._active_border | |
358 color_rgb = color_to_rgb(color_v) | |
359 color = gtk.gdk.Color(*color_rgb) | |
360 | |
361 gc = self._gc | |
362 gc.set_rgb_fg_color(color) | |
363 | |
364 line_x1 = idx * self._frame_width | |
365 line_x2 = line_x1 + self._frame_width | |
366 | |
367 win.draw_line(gc, line_x1, 0, line_x1, w_h) | |
368 win.draw_line(gc, line_x2, 0, line_x2, w_h) | |
369 win.draw_line(gc, line_x1, w_h - 1, line_x2, w_h - 1) | |
370 win.draw_line(gc, line_x1, 0, line_x2, 0) | |
371 pass | |
372 | |
373 def _draw_keyframe(self, frame_idx): | 431 def _draw_keyframe(self, frame_idx): |
374 # Only keyframes that is not right-side of NONE type tween should be | 432 # Only keyframes that is not right-side of NONE type tween should be |
375 # draw. | 433 # draw. |
376 pos = self._find_keyframe(frame_idx) | 434 pos = self._find_keyframe(frame_idx) |
377 key = self._keys[pos] | 435 key = self._keys[pos] |
378 if key.left_tween and not key.right_tween: | 436 if key.left_tween and not key.right_tween: |
379 left_key = self._keys[pos - 1] | 437 left_key = self._keys[pos - 1] |
380 if left_key.right_tween_type == 0: | 438 if left_key.right_tween_type == 0: |
381 return | 439 return |
382 pass | 440 pass |
383 | 441 |
384 win = self.window | 442 self._draw_keyframe_(frame_idx) |
385 w_x, w_y, w_w, w_h, depth = win.get_geometry() | 443 pass |
386 | |
387 color_v = self._key_mark_color | |
388 color_rgb = color_to_rgb(color_v) | |
389 color = gtk.gdk.Color(*color_rgb) | |
390 | |
391 gc = self._gc | |
392 gc.set_rgb_fg_color(color) | |
393 | |
394 mark_sz = self._key_mark_sz | |
395 mark_x = int((frame_idx + 0.5) * self._frame_width - mark_sz / 2) | |
396 mark_y = w_h * 2 / 3 - mark_sz / 2 | |
397 | |
398 win.draw_rectangle(gc, True, mark_x, mark_y, mark_sz, mark_sz) | |
399 pass | |
400 | 444 |
401 def _draw_keyframes(self): | 445 def _draw_keyframes(self): |
402 for key in self._keys: | 446 for key in self._keys: |
403 self._draw_keyframe(key.idx) | 447 self._draw_keyframe(key.idx) |
404 pass | 448 pass |
405 pass | 449 pass |
406 | 450 |
407 ## \brief Show a mark for the pointer for a frame. | |
408 # | |
409 def _draw_hover(self, frame_idx): | |
410 win = self.window | |
411 w_x, w_y, w_w, w_h, depth = win.get_geometry() | |
412 gc = self._gc | |
413 | |
414 color_rgb = color_to_rgb(self._hover_border_color) | |
415 color = gtk.gdk.Color(*color_rgb) | |
416 gc.set_rgb_fg_color(color) | |
417 | |
418 line_x1 = frame_idx * self._frame_width + 1 | |
419 line_x2 = line_x1 + self._frame_width - 2 | |
420 | |
421 win.draw_line(gc, line_x1, 1, line_x1, w_h - 2) | |
422 win.draw_line(gc, line_x2, 1, line_x2, w_h - 2) | |
423 win.draw_line(gc, line_x1, 1, line_x2, 1) | |
424 win.draw_line(gc, line_x1, w_h - 2, line_x2, w_h - 2) | |
425 pass | |
426 | |
427 ## \brief Redraw a frame specified by an index. | 451 ## \brief Redraw a frame specified by an index. |
428 # | 452 # |
429 def _draw_frame(self, frame_idx): | 453 def _draw_frame(self, frame_idx): |
430 if not self._drawing: | 454 if not self._drawing: |
431 return | 455 return |
473 # | 497 # |
474 # Skip tween keys | 498 # Skip tween keys |
475 # | 499 # |
476 first_tween_pos, last_tween_pos = \ | 500 first_tween_pos, last_tween_pos = \ |
477 self._find_tween_range(key_pos) | 501 self._find_tween_range(key_pos) |
478 self._draw_tween(first_tween_pos, last_tween_pos) | 502 first_tween_key = self._keys[first_tween_pos] |
503 last_tween_key = self._keys[last_tween_pos] | |
504 self._draw_tween(first_tween_key.idx, last_tween_key.idx, | |
505 first_tween_key.right_tween_type) | |
479 last_tween_key = self._keys[last_tween_pos] | 506 last_tween_key = self._keys[last_tween_pos] |
480 i = last_tween_key.idx + 1 | 507 i = last_tween_key.idx + 1 |
481 else: | 508 else: |
482 self._draw_normal_frame(i) | 509 self._draw_normal_frame(i) |
483 if key.idx == i: | 510 if key.idx == i: |
503 | 530 |
504 first_pos, last_pos = self._find_tween_range(key_pos) | 531 first_pos, last_pos = self._find_tween_range(key_pos) |
505 first_key = self._keys[first_pos] | 532 first_key = self._keys[first_pos] |
506 last_key = self._keys[last_pos] | 533 last_key = self._keys[last_pos] |
507 | 534 |
508 self._draw_tween(first_pos, last_pos) | 535 self._draw_tween(first_key.idx, last_key.idx, |
536 first_key.right_tween_type) | |
509 self._draw_bottom_line(first_key.idx, last_key.idx + 1) | 537 self._draw_bottom_line(first_key.idx, last_key.idx + 1) |
510 | 538 |
511 for i in range(first_pos, last_pos + 1): | 539 for i in range(first_pos, last_pos + 1): |
512 key = self._keys[i] | 540 key = self._keys[i] |
513 self._draw_keyframe(key.idx) | 541 self._draw_keyframe(key.idx) |