Mercurial > MadButterfly
comparison pyink/frameline.py @ 954:75bc6d9f4e0a
Show hover mark when the pointer over a frame
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Wed, 17 Nov 2010 12:52:34 +0800 |
parents | 2346d3238e03 |
children | 7631dbbbb2be |
comparison
equal
deleted
inserted
replaced
953:2346d3238e03 | 954:75bc6d9f4e0a |
---|---|
118 _tween_bgcolors = [0x80ff80, 0xff8080] # bg colors of tween frames | 118 _tween_bgcolors = [0x80ff80, 0xff8080] # bg colors of tween frames |
119 # Colors for normal frames | 119 # Colors for normal frames |
120 _normal_bgcolors = [0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xcccccc] | 120 _normal_bgcolors = [0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xcccccc] |
121 _normal_border = 0xaaaaaa # border color of normal frames. | 121 _normal_border = 0xaaaaaa # border color of normal frames. |
122 _active_border = 0xff3030 # border color of an active frame | 122 _active_border = 0xff3030 # border color of an active frame |
123 _hover_border_color = 0xa0a0a0 # border when the pointer over a frame | |
123 | 124 |
124 FRAME_BUT_PRESS = 'frame-button-press' | 125 FRAME_BUT_PRESS = 'frame-button-press' |
125 | 126 |
126 def __new__(clz, *args): | 127 def __new__(clz, *args): |
127 if not frameline._type: | 128 if not frameline._type: |
138 return fl_obj | 139 return fl_obj |
139 | 140 |
140 def __init__(self, num_frames=20): | 141 def __init__(self, num_frames=20): |
141 self.connect('button-press-event', self._press_hdl) | 142 self.connect('button-press-event', self._press_hdl) |
142 self.connect('expose-event', self._fl_expose) | 143 self.connect('expose-event', self._fl_expose) |
144 self.connect('motion-notify-event', self._motion_hdl) | |
143 self._num_frames = num_frames | 145 self._num_frames = num_frames |
144 self._keys = [] | 146 self._keys = [] |
145 self._active_frame = -1 | 147 self._active_frame = -1 |
148 self._last_hover = -1 # frame index of last hover | |
146 pass | 149 pass |
147 | 150 |
148 def _press_hdl(self, widget, event): | 151 def _press_hdl(self, widget, event): |
149 frame = event.x / self._frame_width | 152 frame = event.x / self._frame_width |
150 but = event.button | 153 but = event.button |
151 self.emit(frameline.FRAME_BUT_PRESS, frame, but) | 154 self.emit(frameline.FRAME_BUT_PRESS, frame, but) |
155 pass | |
156 | |
157 def _motion_hdl(self, widget, event): | |
158 frame = int(event.x / self._frame_width) | |
159 self._draw_hover(frame) | |
152 pass | 160 pass |
153 | 161 |
154 def _fl_expose(self, widget, event): | 162 def _fl_expose(self, widget, event): |
155 win = self.window | 163 win = self.window |
156 x, y, w, h, depth = win.get_geometry() | 164 x, y, w, h, depth = win.get_geometry() |
158 self._gc = gtk.gdk.GC(win) | 166 self._gc = gtk.gdk.GC(win) |
159 # | 167 # |
160 # register for button press event | 168 # register for button press event |
161 # | 169 # |
162 emask = win.get_events() | 170 emask = win.get_events() |
163 emask = emask | gtk.gdk.BUTTON_PRESS_MASK | 171 emask = emask | gtk.gdk.BUTTON_PRESS_MASK | \ |
172 gtk.gdk.POINTER_MOTION_MASK | |
164 win.set_events(emask) | 173 win.set_events(emask) |
165 pass | 174 pass |
166 self.update() | 175 self.update() |
167 pass | 176 pass |
168 | 177 |
195 gc.set_rgb_fg_color(line_color) | 204 gc.set_rgb_fg_color(line_color) |
196 | 205 |
197 # | 206 # |
198 # Draw tween line | 207 # Draw tween line |
199 # | 208 # |
200 line_x1 = int(first_key.idx + 0.5) * self._frame_width | 209 line_x1 = int((first_key.idx + 0.5) * self._frame_width) |
201 line_x2 = line_x1 + (last_key.idx - first_key.idx) * self._frame_width | 210 line_x2 = line_x1 + (last_key.idx - first_key.idx) * self._frame_width |
202 line_y = int(w_h * 2 / 3) | 211 line_y = int(w_h * 2 / 3) |
203 win.draw_line(gc, line_x1, line_y, line_x2, line_y) | 212 win.draw_line(gc, line_x1, line_y, line_x2, line_y) |
204 pass | 213 pass |
205 | 214 |
206 def _draw_frame(self, idx): | 215 def _draw_normal_frame(self, idx): |
207 win = self.window | 216 win = self.window |
208 w_x, w_y, w_w, w_h, depth = win.get_geometry() | 217 w_x, w_y, w_w, w_h, depth = win.get_geometry() |
209 | 218 |
210 gc = self._gc | 219 gc = self._gc |
211 bg_idx = idx % len(self._normal_bgcolors) | 220 bg_idx = idx % len(self._normal_bgcolors) |
212 rgb = color_to_rgb(self._normal_bgcolors[bg_idx]) | 221 rgb = color_to_rgb(self._normal_bgcolors[bg_idx]) |
213 color = gtk.gdk.Color(*rgb) | 222 color = gtk.gdk.Color(*rgb) |
214 gc.set_rgb_fg_color(color) | 223 gc.set_rgb_fg_color(color) |
215 | 224 |
216 f_x = self._frame_width * idx | 225 f_x = self._frame_width * idx |
217 win.draw_rectangle(gc, True, f_x + 1, 0, self._frame_width, w_h) | 226 win.draw_rectangle(gc, True, f_x + 1, 0, self._frame_width - 1, w_h) |
218 next_f_x = f_x + self._frame_width | 227 next_f_x = f_x + self._frame_width |
219 | 228 |
220 border_rgb = color_to_rgb(self._normal_border) | 229 border_rgb = color_to_rgb(self._normal_border) |
221 border_color = gtk.gdk.Color(*border_rgb) | 230 border_color = gtk.gdk.Color(*border_rgb) |
222 gc.set_rgb_fg_color(border_color) | 231 gc.set_rgb_fg_color(border_color) |
223 gc.set_line_attributes(1, gtk.gdk.LINE_SOLID, | 232 gc.set_line_attributes(1, gtk.gdk.LINE_SOLID, |
224 gtk.gdk.CAP_BUTT, gtk.gdk.JOIN_MITER) | 233 gtk.gdk.CAP_BUTT, gtk.gdk.JOIN_MITER) |
225 win.draw_line(gc, next_f_x, 0, next_f_x, w_h) | 234 win.draw_line(gc, next_f_x, 0, next_f_x, w_h) |
226 pass | 235 pass |
227 | 236 |
228 def _draw_frames(self): | 237 def _draw_all_frames(self): |
229 win = self.window | 238 win = self.window |
230 w_x, w_y, w_w, w_h, depth = win.get_geometry() | 239 w_x, w_y, w_w, w_h, depth = win.get_geometry() |
231 gc = self._gc | 240 gc = self._gc |
232 | 241 |
233 i = 0 | 242 i = 0 |
258 self._draw_tween(first_tween_key, last_tween_key) | 267 self._draw_tween(first_tween_key, last_tween_key) |
259 | 268 |
260 i = last_tween_key.idx + 1 | 269 i = last_tween_key.idx + 1 |
261 pass | 270 pass |
262 else: | 271 else: |
263 self._draw_frame(i) | 272 self._draw_normal_frame(i) |
264 i = i + 1 | 273 i = i + 1 |
265 pass | 274 pass |
266 pass | 275 pass |
267 | 276 |
268 border_rgb = color_to_rgb(self._normal_border) | 277 border_rgb = color_to_rgb(self._normal_border) |
270 gc.set_rgb_fg_color(border_color) | 279 gc.set_rgb_fg_color(border_color) |
271 stop_x = num_frames * self._frame_width | 280 stop_x = num_frames * self._frame_width |
272 win.draw_line(gc, 0, w_h - 1, stop_x, w_h - 1) | 281 win.draw_line(gc, 0, w_h - 1, stop_x, w_h - 1) |
273 pass | 282 pass |
274 | 283 |
284 def _draw_keyframe(self, frame_idx): | |
285 win = self.window | |
286 w_x, w_y, w_w, w_h, depth = win.get_geometry() | |
287 | |
288 color_v = self._key_mark_color | |
289 color_rgb = color_to_rgb(color_v) | |
290 color = gtk.gdk.Color(*color_rgb) | |
291 | |
292 gc = self._gc | |
293 gc.set_rgb_fg_color(color) | |
294 | |
295 mark_sz = self._key_mark_sz | |
296 mark_x = int((frame_idx + 0.5) * self._frame_width - mark_sz / 2) | |
297 mark_y = w_h * 2 / 3 - mark_sz / 2 | |
298 | |
299 win.draw_rectangle(gc, True, mark_x, mark_y, mark_sz, mark_sz) | |
300 pass | |
301 | |
275 def _draw_keyframes(self): | 302 def _draw_keyframes(self): |
276 win = self.window | 303 win = self.window |
277 w_x, w_y, w_w, w_h, depth = win.get_geometry() | 304 w_x, w_y, w_w, w_h, depth = win.get_geometry() |
278 | 305 |
279 color_v = self._key_mark_color | 306 color_v = self._key_mark_color |
310 win.draw_line(gc, line_x1, 0, line_x1, w_h) | 337 win.draw_line(gc, line_x1, 0, line_x1, w_h) |
311 win.draw_line(gc, line_x2, 0, line_x2, w_h) | 338 win.draw_line(gc, line_x2, 0, line_x2, w_h) |
312 win.draw_line(gc, line_x1, w_h - 1, line_x2, w_h - 1) | 339 win.draw_line(gc, line_x1, w_h - 1, line_x2, w_h - 1) |
313 win.draw_line(gc, line_x1, 0, line_x2, 0) | 340 win.draw_line(gc, line_x1, 0, line_x2, 0) |
314 pass | 341 pass |
342 | |
343 ## \brief Redraw a frame specified by an index. | |
344 # | |
345 def _redraw_frame(self, frame_idx): | |
346 keys = [key.idx for key in self._keys] | |
347 try: | |
348 pos = keys.index(frame_idx) | |
349 except ValueError: | |
350 keys.append(frame_idx) | |
351 keys.sort() | |
352 pos = keys.index(frame_idx) - 1 | |
353 pass | |
354 key = self._keys[pos] | |
355 | |
356 if key.right_tween or (key.left_tween and key.idx == frame_idx): | |
357 first_key = last_key = key | |
358 first_pos = last_pos = pos | |
359 while first_pos > 0 and first_key.left_tween: | |
360 first_pos = first_pos - 1 | |
361 first_key = self._keys[first_pos] | |
362 pass | |
363 max_pos = len(self._keys) - 1 | |
364 while last_pos < max_pos and last_key.right_tween: | |
365 last_pos = last_pos + 1 | |
366 last_key = self._keys[last_pos] | |
367 pass | |
368 | |
369 self._draw_tween(first_key, last_key) | |
370 | |
371 for i in range(first_pos, last_pos + 1): | |
372 key = self._keys[i] | |
373 self._draw_keyframe(key.idx) | |
374 pass | |
375 pass | |
376 else: | |
377 self._draw_normal_frame(frame_idx) | |
378 if key.idx == frame_idx: | |
379 self._draw_keyframe(frame_idx) | |
380 pass | |
381 pass | |
382 pass | |
383 | |
384 ## \brief Show a mark for the pointer for a frame. | |
385 # | |
386 def _draw_hover(self, frame_idx): | |
387 if self._last_hover != -1: | |
388 self._redraw_frame(self._last_hover) | |
389 pass | |
390 | |
391 self._draw_active() | |
392 | |
393 win = self.window | |
394 w_x, w_y, w_w, w_h, depth = win.get_geometry() | |
395 gc = self._gc | |
396 | |
397 color_rgb = color_to_rgb(self._hover_border_color) | |
398 color = gtk.gdk.Color(*color_rgb) | |
399 gc.set_rgb_fg_color(color) | |
400 | |
401 line_x1 = frame_idx * self._frame_width + 1 | |
402 line_x2 = line_x1 + self._frame_width - 2 | |
403 | |
404 win.draw_line(gc, line_x1, 1, line_x1, w_h - 2) | |
405 win.draw_line(gc, line_x2, 1, line_x2, w_h - 2) | |
406 win.draw_line(gc, line_x1, 1, line_x2, 1) | |
407 win.draw_line(gc, line_x1, w_h - 2, line_x2, w_h - 2) | |
408 | |
409 self._last_hover = frame_idx | |
410 pass | |
315 | 411 |
316 def update(self): | 412 def update(self): |
317 win = self.window | 413 win = self.window |
318 x, y, w, h, depth = win.get_geometry() | 414 x, y, w, h, depth = win.get_geometry() |
319 | 415 |
320 self._draw_frames() | 416 self._draw_all_frames() |
321 self._draw_keyframes() | 417 self._draw_keyframes() |
322 if self._active_frame != -1: | 418 if self._active_frame != -1: |
323 self._draw_active() | 419 self._draw_active() |
324 pass | 420 pass |
325 pass | 421 pass |