Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate

Commit 05a9623d0a3e48dbcf84a0123682872c6bb5d16b

Refactoring model, checkpoint #3
activity.py
(4 / 10)
  
2323from dbus.service import method, signal
2424from dbus.gobject_service import ExportedGObject
2525
26import model
2627from theme import *
2728from flipsticks import flipsticks
2829
5656 innerframe.add(self.app.main)
5757 self.set_canvas(outerframe)
5858
59 """
6059 def read_file(self, filepath):
61 f = file(filepath)
62 sdata = f.read()
63 f.close()
64 self.app.restore(sdata)
60 model.load(filepath)
61 self.app.restore()
6562
6663 def write_file(self, filepath):
67 sdata = self.app.getsdata()
68 f = open(filepath,'w')
69 f.write(sdata)
70 f.close()
71 """
64 model.save(filepath)
flipsticks.py
(78 / 234)
  
2424import gobject
2525import os
2626import math
27import textwrap
28import pickle
2927
3028import model
29import screen
3130import kinematic
3231from theme import *
3332
4444 btn.set_size_request(w, h)
4545 return btn
4646
47def inarea(x,y,awidth,aheight):
48 if x+5 > awidth:
49 return False
50 if y+5 > aheight:
51 return False
52 if y < 5:
53 return False
54 if x < 5:
55 return False
56 return True
57
58def getpoints(x,y,angle,len):
59 nx = int(round(x + (len * math.cos(math.radians(angle)))))
60 ny = int(round(y - (len * math.sin(math.radians(angle)))))
61 return (nx,ny)
62
6347class flipsticks:
48 def restore(self):
49 self.drawkeyframe()
50 self.syncmaintokf()
51 self.updateentrybox()
52
6453 def expose_event(self, widget, event):
6554 x , y, width, height = event.area
6655 widget.window.draw_drawable(widget.get_style().fg_gc[gtk.STATE_NORMAL],
8181 state = event.state
8282 if state & gtk.gdk.BUTTON1_MASK and self.pixmap != None:
8383 if self.jointpressed:
84 if inarea(x,y,DRAWWIDTH,DRAWHEIGHT):
84 if _inarea(x,y,DRAWWIDTH,DRAWHEIGHT):
8585 #self.key.joints[self.jointpressed] = (x,y) # old hack way
8686 # first find the parents x,y
87 (px,py) = self.getparentjoint(self.jointpressed,self.key.joints,
87 (px,py) = model.getparentjoint(self.jointpressed,self.key.joints,
8888 self.key.middle)
8989 if x-px == 0:
9090 #computeangle = 0
9595 computeangle = int(math.degrees(math.atan(a/b)))
9696 stickname = JOINTTOSTICK[self.jointpressed]
9797 # add sum of parent angles to new angle
98 parents = self.getparentsticks(stickname)
98 parents = model.getparentsticks(stickname)
9999 panglesum = 0
100100 for parentname in parents:
101101 (pangle,plen) = self.key.sticks[parentname]
108108 if newangle < 0:
109109 newangle = 360 + newangle
110110 self.key.sticks[stickname] = (newangle,len)
111 self.setjoints() # this is overkill
111 self.key.setjoints() # this is overkill
112112 self.drawmainframe()
113113 self.updateentrybox()
114114 elif self.middlepressed:
115 if inarea(x,y,DRAWWIDTH,DRAWHEIGHT):
115 if _inarea(x,y,DRAWWIDTH,DRAWHEIGHT):
116116 xdiff = x-self.key.middle[0]
117117 ydiff = y-self.key.middle[1]
118118 self.shiftjoints(xdiff,ydiff)
119119 self.key.middle = (x,y)
120120 self.drawmainframe()
121121 elif self.rotatepressed:
122 if inarea(x,y,DRAWWIDTH,DRAWHEIGHT):
122 if _inarea(x,y,DRAWWIDTH,DRAWHEIGHT):
123123 (px,py) = self.key.middle
124124 if x-px == 0:
125125 #computeangle = 0
146146 if newsangle > 360:
147147 newsangle = newsangle - 360
148148 self.key.sticks[stickname] = (newsangle,slen)
149 self.setjoints()
149 self.key.setjoints()
150150 self.drawmainframe()
151151 self.updateentrybox()
152152
161161 state = event.state
162162 if state & gtk.gdk.BUTTON1_MASK and self.pixmap != None:
163163 if self.kfpressed >= 0:
164 if inarea(x,y,KEYFRAMEWIDTH,KEYFRAMEHEIGHT):
164 if _inarea(x,y,KEYFRAMEWIDTH,KEYFRAMEHEIGHT):
165165 xdiff = x - model.keys[self.kfpressed].x
166166 self.shiftjoints(xdiff,0, model.keys[self.kfpressed].scaled_joints)
167167 model.keys[self.kfpressed].x = x
170170
171171 def button_press_event(self, widget, event):
172172 if event.button == 1 and self.pixmap != None:
173 joint = self.injoint(event.x, event.y)
173 joint = self.key.injoint(event.x, event.y)
174174 if joint:
175175 self.jointpressed = joint
176176 self.drawmainframe()
177 elif self.inmiddle(event.x, event.y):
177 elif self.key.inmiddle(event.x, event.y):
178178 self.middlepressed = True
179179 self.drawmainframe()
180 elif self.inrotate(event.x, event.y):
180 elif self.key.inrotate(event.x, event.y):
181181 self.rotatepressed = True
182182 self.drawmainframe()
183183 return True
190190
191191 def kf_button_press_event(self, widget, event):
192192 if event.button == 1 and self.pixmap != None:
193 kfnum = self.inkeyframe(event.x, event.y)
193 kfnum = _inkeyframe(event.x, event.y)
194194 if kfnum >= 0:
195195 self.kfpressed = kfnum
196196 self.kfselected = kfnum
218218 gobject.source_remove(self.playing)
219219 self.playing = gobject.timeout_add(self.waittime, self.playframe)
220220
221
221222 def playframe(self):
222223 if not self.playing:
223224 return False
224225 else:
225226 if self.playframenum == -1:
226227 return True
227 joints = self.frames[self.playframenum]
228 parts = self.fparts[self.playframenum]
229 # draw on the main drawing area
230 area = self.toplevel.window
231 drawgc = area.new_gc()
232 drawgc.line_width = 3
233 cm = drawgc.get_colormap()
234 white = cm.alloc_color('white')
235 black = cm.alloc_color('black')
236 drawgc.fill = gtk.gdk.SOLID
237 x, y, width, height = self.mfdraw.get_allocation()
238 #self.pixmap = gtk.gdk.Pixmap(self.mfdraw.window, width, height)
239 # clear area
240 drawgc.set_foreground(white)
241 self.pixmap.draw_rectangle(drawgc,True,0,0,width,height)
242228
243 drawgc.set_foreground(black)
244 #hsize = self.key.sticks['HEAD'][1] # really half of head size
245 hsize = self.fhsize[self.playframenum]
246 middle = self.fmiddles[self.playframenum]
247 rhsize = parts['RIGHT HAND']
248 lhsize = parts['LEFT HAND']
249 self.drawstickman(drawgc,self.pixmap,middle,joints,hsize,rhsize,lhsize)
229 self._draw_frame(self.playframenum, self.pixmap)
250230 # draw circle for middle
251231 #green = cm.alloc_color('green')
252232 #drawgc.set_foreground(green)
264264 else:
265265 # part not stick
266266 self.angleentry.set_text('-')
267 self.setjoints()
267 self.key.setjoints()
268268 self.drawmainframe()
269269
270270 def updateentrybox(self):
287287 else:
288288 # part not stick
289289 self.key.parts[stickname] = newlen
290 self.setjoints()
290 self.key.setjoints()
291291 self.drawmainframe()
292292
293293 def reset(self, widget, data=None):
294 xmiddle = int(DRAWWIDTH/2.0)
295 ymiddle = int(DRAWHEIGHT/2.0)
296 self.key.middle = (xmiddle,ymiddle)
297 self.key.sticks = STICKS.copy()
298 self.key.parts = PARTS.copy()
294 self.key.reset()
299295 self.selectstickebox()
300 self.setjoints()
301296 self.drawmainframe()
302297
303298 def setframe(self, widget, data=None):
303303 model.keys[self.kfselected].clear()
304304 self.drawkeyframe()
305305
306 def makeframes(self):
307 self.frames = {}
308 self.fparts = {}
309 self.fmiddles = {}
310 self.fhsize = {}
311
312 frames = kinematic.makeframes()
313
314 for key, value in frames.items():
315 self.frames[key] = value.joints
316 self.fparts[key] = value.parts
317 self.fmiddles[key] = value.middle
318 self.fhsize[key] = value.hsize
319
320306 def shiftjoints(self,xdiff,ydiff,joints=None):
321307 if not joints:
322308 joints = self.key.joints
313313 njy = jy + ydiff
314314 joints[jname] = (njx,njy)
315315
316 def getparentsticks(self, stickname):
317 if stickname in ['RIGHT SHOULDER','LEFT SHOULDER','NECK','TORSO']:
318 return []
319 if stickname in ['HEAD']:
320 return ['NECK']
321 if stickname == 'UPPER RIGHT ARM':
322 return ['RIGHT SHOULDER']
323 if stickname == 'LOWER RIGHT ARM':
324 return ['UPPER RIGHT ARM','RIGHT SHOULDER']
325 if stickname == 'UPPER LEFT ARM':
326 return ['LEFT SHOULDER']
327 if stickname == 'LOWER LEFT ARM':
328 return ['UPPER LEFT ARM','LEFT SHOULDER']
329 if stickname == 'RIGHT HIP':
330 return ['TORSO']
331 if stickname == 'UPPER RIGHT LEG':
332 return ['RIGHT HIP','TORSO']
333 if stickname == 'LOWER RIGHT LEG':
334 return ['UPPER RIGHT LEG','RIGHT HIP','TORSO']
335 if stickname == 'RIGHT FOOT':
336 return ['LOWER RIGHT LEG','UPPER RIGHT LEG','RIGHT HIP','TORSO']
337 if stickname == 'LEFT HIP':
338 return ['TORSO']
339 if stickname == 'UPPER LEFT LEG':
340 return ['LEFT HIP','TORSO']
341 if stickname == 'LOWER LEFT LEG':
342 return ['UPPER LEFT LEG','LEFT HIP','TORSO']
343 if stickname == 'LEFT FOOT':
344 return ['LOWER LEFT LEG','UPPER LEFT LEG','LEFT HIP','TORSO']
345
346 def getparentjoint(self,jname,joints,middle):
347 if jname in ['rightshoulder','leftshoulder','groin','neck']:
348 return middle
349 parentjoints = {'rightelbow':'rightshoulder',
350 'righthand':'rightelbow',
351 'leftelbow':'leftshoulder',
352 'lefthand':'leftelbow',
353 'righthip':'groin',
354 'rightknee':'righthip',
355 'rightheel':'rightknee',
356 'righttoe':'rightheel',
357 'lefthip':'groin',
358 'leftknee':'lefthip',
359 'leftheel':'leftknee',
360 'lefttoe':'leftheel',
361 'head':'neck'}
362 return joints[parentjoints[jname]]
363
364 def setjoints(self,joints=None,sticks=None,middle=None):
365 if not joints:
366 joints = self.key.joints
367 if not sticks:
368 sticks = self.key.sticks
369 if not middle:
370 middle = self.key.middle
371 # have to traverse in order because
372 # parent joints must be set right
373 for stickname in STICKLIST:
374 (angle,len) = sticks[stickname]
375 jname = JOINTS[stickname]
376 (x,y) = self.getparentjoint(jname,joints,middle)
377 parents = self.getparentsticks(stickname)
378 panglesum = 0
379 for parentname in parents:
380 (pangle,plen) = sticks[parentname]
381 panglesum += pangle
382 (nx,ny) = getpoints(x,y,angle+panglesum,len)
383 joints[jname] = (nx,ny)
384
385316 def drawmainframe(self):
386317 area = self.toplevel.window
387318 drawgc = area.new_gc()
343343 x,y = self.key.middle
344344 self.pixmap.draw_arc(drawgc,True,x-5,y-5,10,10,0,360*64)
345345 # draw circle for rotate (should be halfway between middle and groin
346 (rx,ry) = self.getrotatepoint()
346 (rx,ry) = self.key.getrotatepoint()
347347 drawgc.set_foreground(yellow)
348348 if self.rotatepressed:
349349 drawgc.set_foreground(blue)
457457 self.fppixmap.draw_rectangle(drawgc,True,0,0,width,height)
458458 self.fpdraw.queue_draw()
459459
460 def inkeyframe(self, x, y):
461 for i in range(len(model.keys)):
462 kx = model.keys[i].x
463 if (abs(kx-x) <= 20):
464 return i
465 return -1
466
467 def injoint(self, x, y):
468 for jname in self.key.joints:
469 jx, jy = self.key.joints[jname]
470 if (abs(jx-x) <= 5) and (abs(jy-y) <= 5):
471 return jname
472
473 def inmiddle(self, x, y):
474 mx, my = self.key.middle
475 if (abs(mx-x) <= 5) and (abs(my-y) <= 5):
476 return True
477
478 def inrotate(self, x, y):
479 rx, ry = self.getrotatepoint()
480 if (abs(rx-x) <= 5) and (abs(ry-y) <= 5):
481 return True
482
483 def getrotatepoint(self):
484 (angle,len) = self.key.sticks['TORSO']
485 x,y = self.key.middle
486 (rx,ry) = getpoints(x,y,angle,int(len/2.0))
487 return (rx,ry)
488
489460 def selectstick(self, widget, event, data=None):
490461 if data:
491462 if self.stickselected:
485485 self.sizeentry.set_text(str(self.key.parts[self.stickselected]))
486486
487487 def exportanim(self, widget, data=None):
488 self.makeframes()
488 self.frames = kinematic.makeframes()
489489 fsecs = self.frames.keys()
490 tmpdir = '/tmp'
491 pngpaths = []
492490 firstpixindex = fsecs[0]
493 for i in [fsecs[0]]:
494 joints = self.frames[i]
495 parts = self.fparts[i]
496 # draw on the main drawing area
497 area = self.toplevel.window
498 drawgc = area.new_gc()
499 drawgc.line_width = 3
500 cm = drawgc.get_colormap()
501 white = cm.alloc_color('white')
502 black = cm.alloc_color('black')
503 drawgc.fill = gtk.gdk.SOLID
504 x, y, width, height = self.mfdraw.get_allocation()
505 pixmap = gtk.gdk.Pixmap(self.mfdraw.window, width, height)
506 # clear area
507 drawgc.set_foreground(white)
508 pixmap.draw_rectangle(drawgc,True,0,0,width,height)
509491
510 drawgc.set_foreground(black)
511 #hsize = self.key.sticks['HEAD'][1] # really half of head size
512 hsize = self.fhsize[i]
513 middle = self.fmiddles[i]
514 rhsize = parts['RIGHT HAND']
515 lhsize = parts['LEFT HAND']
516 self.drawstickman(drawgc,pixmap,middle,joints,hsize,rhsize,lhsize)
517 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, width, height)
518 if i == firstpixindex:
519 firstpixbuf = pixbuf
520 gtk.gdk.Pixbuf.get_from_drawable(pixbuf,pixmap,pixmap.get_colormap(),0,0,0,0,width,height)
521 filename = 'fp%03d.png' % i
522 filepath = os.path.join(tmpdir,filename)
523 pixbuf.save(filepath,'png')
524 pngpaths.append(filepath)
525 from sugar.datastore import datastore
526 mediaObject = datastore.create()
527 mediaObject.metadata['title'] = 'FlipSticks PNG'
528 thumbData = self._get_base64_pixbuf_data(firstpixbuf)
529 mediaObject.metadata['preview'] = thumbData
530 #medaiObject.metadata['icon-color'] = ''
531 mediaObject.metadata['mime_type'] = 'image/png'
532 mediaObject.file_path = pngpaths[0]
533 datastore.write(mediaObject)
492 x, y, width, height = self.mfdraw.get_allocation()
493 pixmap = gtk.gdk.Pixmap(self.mfdraw.window, width, height)
494 self._draw_frame(fsecs[0], pixmap)
495 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, width, height)
496 gtk.gdk.Pixbuf.get_from_drawable(pixbuf,pixmap,pixmap.get_colormap(),0,0,0,0,width,height)
534497
535 def _get_base64_pixbuf_data(self, pixbuf):
536 data = [""]
537 pixbuf.save_to_callback(self._save_data_to_buffer_cb, "png", {}, data)
538 import base64
539 return base64.b64encode(str(data[0]))
540
541 def _save_data_to_buffer_cb(self, buf, data):
542 data[0] += buf
543 return True
544
498 model.screen_shot(pixbuf)
499
545500 def playbackwards(self, widget, data=None):
546501 if self.playing:
547502 playimg = gtk.Image()
514514 stopimg.set_from_file(os.path.join(self.iconsdir,'big_pause.png'))
515515 stopimg.show()
516516 widget.set_image(stopimg)
517 self.makeframes()
517 self.frames = kinematic.makeframes()
518518 fsecs = self.frames.keys()
519519 fsecs.sort()
520520 if fsecs:
541541 stopimg.set_from_file(os.path.join(self.iconsdir,'big_pause.png'))
542542 stopimg.show()
543543 widget.set_image(stopimg)
544 self.makeframes()
545 #mkeys = self.fmiddles.keys()
546 #mkeys.sort()
547 #for mkey in mkeys:
548 # print '%s:(%s,%s)' % (mkey,self.fmiddles[mkey][0],self.fmiddles[mkey][1])
544 self.frames = kinematic.makeframes()
549545 fsecs = self.frames.keys()
550546 fsecs.sort()
551547 if fsecs:
560560 self.mdirpath = mdirpath
561561 self.stickselected = 'RIGHT SHOULDER'
562562
563 self.key = model.CurrentKeyFrame()
563 self.key = screen.ScreenFrame()
564564
565565 self.kfselected = 0
566 self.setjoints()
567566 self.jointpressed = None
568567 self.kfpressed = -1
569568 self.middlepressed = False
793793 self.rightvbox.pack_start(self.exporthbox,True,False,0)
794794
795795 self.hbox.pack_start(self.rightvbox,False,False,0)
796
797 def _draw_frame(self, index, pixmap):
798 joints = self.frames[index].joints
799 parts = self.frames[index].parts
800 # draw on the main drawing area
801 area = self.toplevel.window
802 drawgc = area.new_gc()
803 drawgc.line_width = 3
804 cm = drawgc.get_colormap()
805 white = cm.alloc_color('white')
806 black = cm.alloc_color('black')
807 drawgc.fill = gtk.gdk.SOLID
808 x, y, width, height = self.mfdraw.get_allocation()
809 #pixmap = gtk.gdk.Pixmap(self.mfdraw.window, width, height)
810 # clear area
811 drawgc.set_foreground(white)
812 pixmap.draw_rectangle(drawgc,True,0,0,width,height)
813
814 drawgc.set_foreground(black)
815 #hsize = self.key.sticks['HEAD'][1] # really half of head size
816 hsize = self.frames[index].hsize
817 middle = self.frames[index].middle
818 rhsize = parts['RIGHT HAND']
819 lhsize = parts['LEFT HAND']
820 self.drawstickman(drawgc, pixmap, middle, joints, hsize, rhsize, lhsize)
821
822def _inarea(x,y,awidth,aheight):
823 if x+5 > awidth:
824 return False
825 if y+5 > aheight:
826 return False
827 if y < 5:
828 return False
829 if x < 5:
830 return False
831 return True
832
833def _inkeyframe(x, y):
834 for i in range(len(model.keys)):
835 kx = model.keys[i].x
836 if (abs(kx-x) <= 20):
837 return i
838 return -1
kinematic.py
(3 / 61)
  
1212# along with this program; if not, write to the Free Software
1313# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1414
15import math
16
1715import model
1816import theme
1917
20class Frame:
18class KinematicFrame:
2119 def __init__(self, keyframe=None):
2220 if keyframe:
2321 self.assign(keyframe)
3636
3737 for i in model.keys:
3838 if not i.empty():
39 frames[i.x] = Frame(i)
39 frames[i.x] = KinematicFrame(i)
4040
4141 if not frames:
4242 return {}
6666 numframes = int((endsecs-startsecs)/float(fint))-1
6767
6868 for j in range(numframes-1): # MAYBE SHOULD BE numframes
69 frame = frames[startsecs + ((j+1)*fint)] = Frame()
69 frame = frames[startsecs + ((j+1)*fint)] = KinematicFrame()
7070
7171 frame.joints = _intjoints(start_frame.joints, end_frame.joints,
7272 j+1, numframes)
7979
8080 return frames
8181
82def getparentjoint(jname, joints, middle):
83 if jname in ['rightshoulder','leftshoulder','groin','neck']:
84 return middle
85
86 parentjoints = {'rightelbow':'rightshoulder',
87 'righthand':'rightelbow',
88 'leftelbow':'leftshoulder',
89 'lefthand':'leftelbow',
90 'righthip':'groin',
91 'rightknee':'righthip',
92 'rightheel':'rightknee',
93 'righttoe':'rightheel',
94 'lefthip':'groin',
95 'leftknee':'lefthip',
96 'leftheel':'leftknee',
97 'lefttoe':'leftheel',
98 'head':'neck'}
99
100 return joints[parentjoints[jname]]
101
102def getparentsticks(stickname):
103 if stickname in ['RIGHT SHOULDER','LEFT SHOULDER','NECK','TORSO']:
104 return []
105 if stickname in ['HEAD']:
106 return ['NECK']
107 if stickname == 'UPPER RIGHT ARM':
108 return ['RIGHT SHOULDER']
109 if stickname == 'LOWER RIGHT ARM':
110 return ['UPPER RIGHT ARM','RIGHT SHOULDER']
111 if stickname == 'UPPER LEFT ARM':
112 return ['LEFT SHOULDER']
113 if stickname == 'LOWER LEFT ARM':
114 return ['UPPER LEFT ARM','LEFT SHOULDER']
115 if stickname == 'RIGHT HIP':
116 return ['TORSO']
117 if stickname == 'UPPER RIGHT LEG':
118 return ['RIGHT HIP','TORSO']
119 if stickname == 'LOWER RIGHT LEG':
120 return ['UPPER RIGHT LEG','RIGHT HIP','TORSO']
121 if stickname == 'RIGHT FOOT':
122 return ['LOWER RIGHT LEG','UPPER RIGHT LEG','RIGHT HIP','TORSO']
123 if stickname == 'LEFT HIP':
124 return ['TORSO']
125 if stickname == 'UPPER LEFT LEG':
126 return ['LEFT HIP','TORSO']
127 if stickname == 'LOWER LEFT LEG':
128 return ['UPPER LEFT LEG','LEFT HIP','TORSO']
129 if stickname == 'LEFT FOOT':
130 return ['LOWER LEFT LEG','UPPER LEFT LEG','LEFT HIP','TORSO']
131
132def getpoints(x,y,angle,len):
133 nx = int(round(x + (len * math.cos(math.radians(angle)))))
134 ny = int(round(y - (len * math.sin(math.radians(angle)))))
135 return (nx,ny)
136
13782def _interpolate(x,x0,y0,x1,y1):
13883 if x1-x0 == 0:
13984 return y0
126126 return x0
127127 x = x0 + (count * ((x1-x0)/float(numpoints)))
128128 return int(x)
129""
model.py
(112 / 52)
  
1212# along with this program; if not, write to the Free Software
1313# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1414
15import os
16import pickle
17
1518import theme
1619import kinematic
1720
21keys = []
22
1823class KeyFrame:
1924 def __init__(self):
20 pass
25 self.clear()
2126
2227 def empty(self):
2328 return self.joints == None
3232 self.parts = x.parts.copy()
3333 self.sticks = x.sticks.copy()
3434 self.joints = x.joints.copy()
35 self.scaled_sticks = x.get_scaled_sticks()
36 self.scaled_joints = x.get_scaled_joints(self.x,
37 int(theme.KEYFRAMEHEIGHT/2.0))
3538
3639 def clear(self):
40 self.middle = None
3741 self.parts = None
3842 self.sticks = None
3943 self.scaled_sticks = None
4044 self.joints = None
4145 self.scaled_joints = None
4246
43class StoredKeyFrame(KeyFrame):
44 def __init__(self):
45 KeyFrame.__init__(self)
46 self.clear()
47def save(filename):
48 out = []
4749
48 def assign(self, x):
49 KeyFrame.assign(self, x)
50 self.scaled_sticks = x.sticks.copy()
51 self.scaled_joints = _initjoints()
52 _scalesticks(self.scaled_sticks, .2)
53 _setjoints(self.scaled_joints, self.scaled_sticks,
54 (self.x, int(theme.KEYFRAMEHEIGHT/2.0)))
50 for i in keys:
51 out.append({
52 'x' : i.x,
53 'middle' : i.middle,
54 'parts' : i.parts,
55 'sticks' : i.sticks,
56 'joints' : i.joints,
57 'scaled_sticks' : i.scaled_sticks,
58 'scaled_joints' : i.scaled_joints })
5559
56class CurrentKeyFrame(KeyFrame):
57 def __init__(self):
58 KeyFrame.__init__(self)
59 self.parts = theme.PARTS.copy()
60 self.sticks = theme.STICKS.copy()
61 self.joints = _initjoints()
62 self.middle = (int(theme.DRAWWIDTH/2.0), int(theme.DRAWHEIGHT/2.0))
60 file(filename, 'w').write(pickle.dumps(out))
6361
64 def assign(self, x):
65 KeyFrame.assign(self, x)
66 _setjoints(self.joints, self.sticks, self.middle)
62def load(filename):
63 inc = pickle.loads(file(filename, 'r').read())
6764
68keys = []
65 for i, data in enumerate(inc):
66 key = keys[i]
6967
68 key.x = data['x']
69 key.middle = data['middle']
70 key.parts = data['parts']
71 key.sticks = data['sticks']
72 key.joints = data['joints']
73 key.scaled_sticks = data['scaled_sticks']
74 key.scaled_joints = data['scaled_joints']
75
76def getparentsticks(stickname):
77 if stickname in ['RIGHT SHOULDER','LEFT SHOULDER','NECK','TORSO']:
78 return []
79 if stickname in ['HEAD']:
80 return ['NECK']
81 if stickname == 'UPPER RIGHT ARM':
82 return ['RIGHT SHOULDER']
83 if stickname == 'LOWER RIGHT ARM':
84 return ['UPPER RIGHT ARM','RIGHT SHOULDER']
85 if stickname == 'UPPER LEFT ARM':
86 return ['LEFT SHOULDER']
87 if stickname == 'LOWER LEFT ARM':
88 return ['UPPER LEFT ARM','LEFT SHOULDER']
89 if stickname == 'RIGHT HIP':
90 return ['TORSO']
91 if stickname == 'UPPER RIGHT LEG':
92 return ['RIGHT HIP','TORSO']
93 if stickname == 'LOWER RIGHT LEG':
94 return ['UPPER RIGHT LEG','RIGHT HIP','TORSO']
95 if stickname == 'RIGHT FOOT':
96 return ['LOWER RIGHT LEG','UPPER RIGHT LEG','RIGHT HIP','TORSO']
97 if stickname == 'LEFT HIP':
98 return ['TORSO']
99 if stickname == 'UPPER LEFT LEG':
100 return ['LEFT HIP','TORSO']
101 if stickname == 'LOWER LEFT LEG':
102 return ['UPPER LEFT LEG','LEFT HIP','TORSO']
103 if stickname == 'LEFT FOOT':
104 return ['LOWER LEFT LEG','UPPER LEFT LEG','LEFT HIP','TORSO']
105
106def getparentjoint(jname, joints, middle):
107 if jname in ['rightshoulder','leftshoulder','groin','neck']:
108 return middle
109
110 parentjoints = {'rightelbow':'rightshoulder',
111 'righthand':'rightelbow',
112 'leftelbow':'leftshoulder',
113 'lefthand':'leftelbow',
114 'righthip':'groin',
115 'rightknee':'righthip',
116 'rightheel':'rightknee',
117 'righttoe':'rightheel',
118 'lefthip':'groin',
119 'leftknee':'lefthip',
120 'leftheel':'leftknee',
121 'lefttoe':'leftheel',
122 'head':'neck'}
123
124 return joints[parentjoints[jname]]
125
126def screen_shot(pixbuf):
127 tmpdir = '/tmp'
128
129 filename = 'fp%03d.png' % i
130 filepath = os.path.join(tmpdir,filename)
131 pixbuf.save(filepath,'png')
132
133 from sugar.datastore import datastore
134 mediaObject = datastore.create()
135 mediaObject.metadata['title'] = 'FlipSticks PNG'
136 thumbData = _get_base64_pixbuf_data(pixbuf)
137 mediaObject.metadata['preview'] = thumbData
138 #medaiObject.metadata['icon-color'] = ''
139 mediaObject.metadata['mime_type'] = 'image/png'
140 mediaObject.file_path = filepath
141 datastore.write(mediaObject)
142
143def _save_data_to_buffer_cb(buf, data):
144 data[0] += buf
145 return True
146
147def _get_base64_pixbuf_data(pixbuf):
148 data = [""]
149 pixbuf.save_to_callback(_save_data_to_buffer_cb, "png", {}, data)
150 import base64
151 return base64.b64encode(str(data[0]))
152
70153for i in range(5):
71 key = StoredKeyFrame()
154 key = KeyFrame()
72155 keyframe_width = theme.KEYFRAMEWIDTH/5
73156 key.x = keyframe_width/2 + i*keyframe_width
74157 keys.append(key)
75
76def _scalesticks(stickdict, i):
77 for key in stickdict:
78 (angle,len) = stickdict[key]
79 newlen = int(len * i)
80 stickdict[key] = (angle,newlen)
81
82def _setjoints(joints, sticks, middle):
83 # have to traverse in order because
84 # parent joints must be set right
85 for stickname in theme.STICKLIST:
86 (angle,len) = sticks[stickname]
87 jname = theme.JOINTS[stickname]
88 (x,y) = kinematic.getparentjoint(jname, joints, middle)
89 parents = kinematic.getparentsticks(stickname)
90 panglesum = 0
91 for parentname in parents:
92 (pangle,plen) = sticks[parentname]
93 panglesum += pangle
94 (nx,ny) = kinematic.getpoints(x,y,angle+panglesum,len)
95 joints[jname] = (nx,ny)
96
97def _initjoints():
98 joints = {}
99 for stickname in theme.JOINTS:
100 jname = theme.JOINTS[stickname]
101 joints[jname] = (0,0)
102 return joints
screen.py
(104 / 0)
  
1# This program is free software; you can redistribute it and/or modify
2# it under the terms of the GNU General Public License as published by
3# the Free Software Foundation; either version 2 of the License, or
4# (at your option) any later version.
5#
6# This program is distributed in the hope that it will be useful,
7# but WITHOUT ANY WARRANTY; without even the implied warranty of
8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9# GNU General Public License for more details.
10#
11# You should have received a copy of the GNU General Public License
12# along with this program; if not, write to the Free Software
13# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
14
15import math
16
17import theme
18import model
19
20class ScreenFrame:
21 def __init__(self):
22 self.reset()
23
24 def setjoints(self):
25 _setjoints(self.joints, self.sticks, self.middle)
26
27 def reset(self):
28 self.parts = theme.PARTS.copy()
29 self.sticks = theme.STICKS.copy()
30 self.joints = _initjoints()
31 self.middle = (int(theme.DRAWWIDTH/2.0), int(theme.DRAWHEIGHT/2.0))
32 self.setjoints()
33
34 def assign(self, x):
35 self.middle = x.middle
36 self.parts = x.parts.copy()
37 self.sticks = x.sticks.copy()
38 self.joints = x.joints.copy()
39 self.setjoints()
40
41 def get_scaled_sticks(self):
42 out = self.sticks.copy()
43 for key in out:
44 (angle,len) = out[key]
45 newlen = int(len * .2)
46 out[key] = (angle,newlen)
47 return out
48
49 def get_scaled_joints(self, x, y):
50 out = _initjoints()
51 _setjoints(out, self.get_scaled_sticks(), (x, y))
52 return out
53
54 def getrotatepoint(self):
55 (angle,len) = self.sticks['TORSO']
56 x,y = self.middle
57 (rx,ry) = _getpoints(x,y,angle,int(len/2.0))
58 return (rx,ry)
59
60 def inrotate(self, x, y):
61 rx, ry = self.getrotatepoint()
62 if (abs(rx-x) <= 5) and (abs(ry-y) <= 5):
63 return True
64 return False
65
66 def injoint(self, x, y):
67 for jname in self.joints:
68 jx, jy = self.joints[jname]
69 if (abs(jx-x) <= 5) and (abs(jy-y) <= 5):
70 return jname
71 return False
72
73 def inmiddle(self, x, y):
74 mx, my = self.middle
75 if (abs(mx-x) <= 5) and (abs(my-y) <= 5):
76 return True
77 return False
78
79def _initjoints():
80 joints = {}
81 for stickname in theme.JOINTS:
82 jname = theme.JOINTS[stickname]
83 joints[jname] = (0,0)
84 return joints
85
86def _setjoints(joints, sticks, middle):
87 # have to traverse in order because
88 # parent joints must be set right
89 for stickname in theme.STICKLIST:
90 (angle,len) = sticks[stickname]
91 jname = theme.JOINTS[stickname]
92 (x,y) = model.getparentjoint(jname, joints, middle)
93 parents = model.getparentsticks(stickname)
94 panglesum = 0
95 for parentname in parents:
96 (pangle,plen) = sticks[parentname]
97 panglesum += pangle
98 (nx,ny) = _getpoints(x,y,angle+panglesum,len)
99 joints[jname] = (nx,ny)
100
101def _getpoints(x,y,angle,len):
102 nx = int(round(x + (len * math.cos(math.radians(angle)))))
103 ny = int(round(y - (len * math.sin(math.radians(angle)))))
104 return (nx,ny)