Mercurial > MadButterfly
annotate pyink/frameline.py @ 945:8db3dd03508f
Draw line to connect continious tween frames
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Tue, 16 Nov 2010 12:51:53 +0800 |
parents | b0edd13779ee |
children | f82ac6740a32 |
rev | line source |
---|---|
944
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
1 import pygtk |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
2 pygtk.require("2.0") |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
3 import gtk |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
4 import gtk.gdk |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
5 import gobject |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
6 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
7 def color_to_rgb(v): |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
8 return (((v >> 16) & 0xff) * 65535 / 0xff, |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
9 ((v >> 8) & 0xff) * 65535 / 0xff, |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
10 (v & 0xff) * 65535 / 0xff) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
11 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
12 class keyframe(object): |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
13 def __init__(self, frame_idx): |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
14 self.idx = frame_idx |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
15 self.left_tween = False |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
16 self.right_tween = False |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
17 self.right_tween_type = 0 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
18 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
19 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
20 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
21 ## Show frame status of a layer |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
22 # |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
23 class frameline(gtk.DrawingArea): |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
24 _type_id = 0 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
25 _frame_width = 10 # Width for each frame is 10 pixels |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
26 _select_color = 0xee2222 # color of border of selected frame |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
27 _key_mark_color = 0x000000 # color of marks for key frames. |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
28 _key_mark_sz = 4 # width and height of a key frame mark |
945
8db3dd03508f
Draw line to connect continious tween frames
Thinker K.F. Li <thinker@codemud.net>
parents:
944
diff
changeset
|
29 _tween_color = 0x808080 # color of tween line |
944
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
30 _tween_bgcolors = [0x80ff80, 0xff8080] # bg colors of tween frames |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
31 # Colors for normal frames |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
32 _normal_bgcolors = [0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xcccccc] |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
33 _normal_border = 0xaaaaaa # border color of normal frames. |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
34 _active_border = 0xff3030 # border color of an active frame |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
35 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
36 def __new__(clz, *args): |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
37 if not frameline._type_id: |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
38 frameline._type_id = gobject.type_register(frameline) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
39 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
40 fl_obj = gobject.new(frameline._type_id) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
41 return fl_obj |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
42 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
43 def __init__(self, num_frames=20): |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
44 self.connect('expose_event', self._fl_expose) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
45 self._num_frames = num_frames |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
46 self._keys = [] |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
47 self._active_frame = -1 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
48 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
49 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
50 def _fl_expose(self, widget, event): |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
51 print 'Expose %s' % (repr(event)) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
52 win = self.window |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
53 x, y, w, h, depth = win.get_geometry() |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
54 print ' Geometry of window: %dx%d+%d+%d' % (w, h, x, y) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
55 self.update() |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
56 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
57 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
58 def _draw_tween(self, first_key, last_key): |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
59 win = self.window |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
60 w_x, w_y, w_w, w_h, depth = win.get_geometry() |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
61 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
62 # |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
63 # Get background color of a tween |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
64 # |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
65 bg_idx = first_key.right_tween_type |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
66 bg_color_v = self._tween_bgcolors[bg_idx] |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
67 bg_color_rgb = color_to_rgb(bg_color_v) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
68 bg_color = gtk.gdk.Color(*bg_color_rgb) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
69 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
70 gc = self._gc |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
71 gc.set_rgb_fg_color(bg_color) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
72 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
73 draw_x = first_key.idx * self._frame_width + 1 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
74 draw_w = (last_key.idx - first_key.idx + 1) * self._frame_width - 1 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
75 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
76 win.draw_rectangle(gc, True, draw_x, 0, draw_w, w_h) |
945
8db3dd03508f
Draw line to connect continious tween frames
Thinker K.F. Li <thinker@codemud.net>
parents:
944
diff
changeset
|
77 |
8db3dd03508f
Draw line to connect continious tween frames
Thinker K.F. Li <thinker@codemud.net>
parents:
944
diff
changeset
|
78 # |
8db3dd03508f
Draw line to connect continious tween frames
Thinker K.F. Li <thinker@codemud.net>
parents:
944
diff
changeset
|
79 # Set color of tween line |
8db3dd03508f
Draw line to connect continious tween frames
Thinker K.F. Li <thinker@codemud.net>
parents:
944
diff
changeset
|
80 # |
8db3dd03508f
Draw line to connect continious tween frames
Thinker K.F. Li <thinker@codemud.net>
parents:
944
diff
changeset
|
81 line_v = self._tween_color |
8db3dd03508f
Draw line to connect continious tween frames
Thinker K.F. Li <thinker@codemud.net>
parents:
944
diff
changeset
|
82 line_rgb = color_to_rgb(line_v) |
8db3dd03508f
Draw line to connect continious tween frames
Thinker K.F. Li <thinker@codemud.net>
parents:
944
diff
changeset
|
83 line_color = gtk.gdk.Color(*line_rgb) |
8db3dd03508f
Draw line to connect continious tween frames
Thinker K.F. Li <thinker@codemud.net>
parents:
944
diff
changeset
|
84 gc.set_rgb_fg_color(line_color) |
8db3dd03508f
Draw line to connect continious tween frames
Thinker K.F. Li <thinker@codemud.net>
parents:
944
diff
changeset
|
85 |
8db3dd03508f
Draw line to connect continious tween frames
Thinker K.F. Li <thinker@codemud.net>
parents:
944
diff
changeset
|
86 # |
8db3dd03508f
Draw line to connect continious tween frames
Thinker K.F. Li <thinker@codemud.net>
parents:
944
diff
changeset
|
87 # Draw tween line |
8db3dd03508f
Draw line to connect continious tween frames
Thinker K.F. Li <thinker@codemud.net>
parents:
944
diff
changeset
|
88 # |
8db3dd03508f
Draw line to connect continious tween frames
Thinker K.F. Li <thinker@codemud.net>
parents:
944
diff
changeset
|
89 line_x1 = (first_key.idx + 0.5) * self._frame_width |
8db3dd03508f
Draw line to connect continious tween frames
Thinker K.F. Li <thinker@codemud.net>
parents:
944
diff
changeset
|
90 line_x2 = line_x1 + (last_key.idx - first_key.idx) * self._frame_width |
8db3dd03508f
Draw line to connect continious tween frames
Thinker K.F. Li <thinker@codemud.net>
parents:
944
diff
changeset
|
91 line_y = w_h * 2 / 3 |
8db3dd03508f
Draw line to connect continious tween frames
Thinker K.F. Li <thinker@codemud.net>
parents:
944
diff
changeset
|
92 win.draw_line(gc, line_x1, line_y, line_x2, line_y) |
944
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
93 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
94 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
95 def _draw_frame(self, idx): |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
96 win = self.window |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
97 w_x, w_y, w_w, w_h, depth = win.get_geometry() |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
98 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
99 gc = self._gc |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
100 bg_idx = idx % len(self._normal_bgcolors) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
101 rgb = color_to_rgb(self._normal_bgcolors[bg_idx]) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
102 color = gtk.gdk.Color(*rgb) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
103 gc.set_rgb_fg_color(color) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
104 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
105 f_x = self._frame_width * idx |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
106 win.draw_rectangle(gc, True, f_x + 1, 0, self._frame_width, w_h) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
107 next_f_x = f_x + self._frame_width |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
108 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
109 border_rgb = color_to_rgb(self._normal_border) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
110 border_color = gtk.gdk.Color(*border_rgb) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
111 gc.set_rgb_fg_color(border_color) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
112 gc.set_line_attributes(1, gtk.gdk.LINE_SOLID, |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
113 gtk.gdk.CAP_BUTT, gtk.gdk.JOIN_MITER) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
114 win.draw_line(gc, next_f_x, 0, next_f_x, w_h) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
115 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
116 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
117 def _draw_frames(self): |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
118 win = self.window |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
119 gc = gtk.gdk.GC(win) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
120 self._gc = gc |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
121 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
122 i = 0 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
123 key_i = 0 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
124 try: |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
125 key = self._keys[key_i] |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
126 except IndexError: |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
127 key = keyframe(self._num_frames) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
128 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
129 num_frames = self._num_frames |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
130 while i < num_frames: |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
131 if key.idx == i: |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
132 # |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
133 # Skip tween keys |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
134 # |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
135 first_tween_key = key |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
136 while key.idx == i or key.left_tween: |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
137 last_tween_key = key |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
138 key_i = key_i + 1 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
139 try: |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
140 key = self._keys[key_i] |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
141 except IndexError: |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
142 key = keyframe(self._num_frames) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
143 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
144 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
145 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
146 if first_tween_key != last_tween_key: |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
147 self._draw_tween(first_tween_key, last_tween_key) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
148 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
149 i = last_tween_key.idx + 1 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
150 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
151 else: |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
152 self._draw_frame(i) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
153 i = i + 1 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
154 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
155 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
156 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
157 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
158 def _draw_keyframes(self): |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
159 win = self.window |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
160 w_x, w_y, w_w, w_h, depth = win.get_geometry() |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
161 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
162 color_v = self._key_mark_color |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
163 color_rgb = color_to_rgb(color_v) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
164 color = gtk.gdk.Color(*color_rgb) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
165 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
166 gc = self._gc |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
167 gc.set_rgb_fg_color(color) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
168 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
169 for key in self._keys: |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
170 mark_sz = self._key_mark_sz |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
171 mark_x = int((key.idx + 0.5) * self._frame_width - mark_sz / 2) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
172 mark_y = w_h * 2 / 3 - mark_sz / 2 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
173 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
174 win.draw_rectangle(gc, True, mark_x, mark_y, mark_sz, mark_sz) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
175 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
176 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
177 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
178 def _draw_active(self): |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
179 win = self.window |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
180 w_x, w_y, w_w, w_h, depth = win.get_geometry() |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
181 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
182 color_v = self._active_border |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
183 color_rgb = color_to_rgb(color_v) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
184 color = gtk.gdk.Color(*color_rgb) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
185 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
186 gc = self._gc |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
187 gc.set_rgb_fg_color(color) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
188 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
189 idx = self._active_frame |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
190 line_x1 = idx * self._frame_width |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
191 line_x2 = line_x1 + self._frame_width |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
192 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
193 win.draw_line(gc, line_x1, 0, line_x1, w_h) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
194 win.draw_line(gc, line_x2, 0, line_x2, w_h) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
195 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
196 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
197 def update(self): |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
198 win = self.window |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
199 x, y, w, h, depth = win.get_geometry() |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
200 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
201 self._draw_frames() |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
202 self._draw_keyframes() |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
203 if self._active_frame != -1: |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
204 self._draw_active() |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
205 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
206 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
207 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
208 def add_keyframe(self, idx): |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
209 key_indic = [key.idx for key in self._keys] |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
210 if idx in key_indic: |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
211 return |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
212 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
213 key_indic.append(idx) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
214 key_indic.sort() |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
215 insert_pos = key_indic.index(idx) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
216 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
217 key = keyframe(idx) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
218 self._keys[insert_pos:insert_pos] = [key] |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
219 if insert_pos > 0 and self._keys[insert_pos - 1].right_tween: |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
220 key.left_tween = True |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
221 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
222 if insert_pos < (len(self._keys) - 1) and \ |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
223 self._keys[insert_pos + 1].left_tween: |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
224 key.right_tween = True |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
225 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
226 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
227 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
228 ## Tween the key frame specified by an index and the frame at right. |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
229 # |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
230 # \see http://www.entheosweb.com/Flash/shape_tween.asp |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
231 def tween(self, idx, _type=0): |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
232 key_indic = [key.idx for key in self._keys] |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
233 pos = key_indic.index(idx) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
234 key = self._keys[pos] |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
235 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
236 try: |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
237 right_key = self._keys[pos + 1] |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
238 except IndexError: |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
239 raise ValueError, 'No right key frame' |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
240 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
241 key.right_tween = True |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
242 right_key.left_tween = True |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
243 key.right_tween_type = _type |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
244 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
245 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
246 ## Set active frame |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
247 # |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
248 # The active frame is the frame that is working on. |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
249 # |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
250 def active_frame(self, idx): |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
251 if idx < 0 or idx >= self._num_frames: |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
252 raise IndexError, 'value of index (%d) is out of range' % (idx) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
253 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
254 self._active_frame = idx |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
255 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
256 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
257 def deactive(self): |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
258 self._active_frame = -1 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
259 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
260 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
261 def set_num_frames(self, num): |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
262 self._num_frames = num |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
263 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
264 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
265 def __len__(self): |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
266 return self._num_frames |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
267 pass |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
268 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
269 if __name__ == '__main__': |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
270 window = gtk.Window(gtk.WINDOW_TOPLEVEL) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
271 fl = frameline(40) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
272 fl.set_size_request(300, 30) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
273 fl.add_keyframe(3) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
274 fl.add_keyframe(9) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
275 fl.add_keyframe(15) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
276 fl.add_keyframe(20) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
277 fl.tween(3) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
278 fl.tween(15, 1) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
279 fl.active_frame(15) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
280 print 'num of frames: %d' % (len(fl)) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
281 |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
282 window.add(fl) |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
283 fl.show() |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
284 window.show() |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
285 gtk.main() |
b0edd13779ee
Implement a frameline widget
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
286 pass |