/usr/share/pyshared/pymt/modules/feedback.py is in python-pymt 0.5.1-0ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | '''
FeedBack: A global feedback effect (aka Surface)
'''
from pymt import *
import os
if not 'PYMT_DOC' in os.environ:
particle_fn = os.path.join(pymt_data_dir, 'particle2.png')
ring_fn = os.path.join(pymt_data_dir, 'ring.png')
ring_img = Image(ring_fn)
ring_img.anchor_x = ring_img.width / 2
ring_img.anchor_y = ring_img.height / 2
class GlobalFeedbackTouch(MTWidget):
def __init__(self, **kwargs):
super(GlobalFeedbackTouch, self).__init__(**kwargs)
# max times of a move position (after that, it will be deleted)
self.maxtimemove = .1
# minimum time before the nomove particle appear
self.mintimenomove = 1
# maximum moves available
self.maxmoves = 20
# prepare list of moves
self.timer = 0
self.moves = []
self.moves.append([self.x, self.y, self.maxtimemove])
def on_move(self, x, y):
# reset nomove timer
self.timer = 0
# append a new move in list
self.moves.append([x, y, self.maxtimemove])
if len(self.moves) > self.maxmoves:
self.moves = self.moves[1:]
def draw(self):
# advance nomove timer
self.timer += getFrameDt()
# nomove timeout, show it !
if self.timer > self.mintimenomove:
alpha = min(0.9, (self.timer - self.mintimenomove) * 4)
set_color(1, 1, 1, alpha)
set_brush(particle_fn, size=alpha * 50)
paintLine((self.x, self.y, self.x + 1, self.y + 1))
# show moves
move_to_delete = []
have_first = False
ox, oy = 0, 0
alphastep = 1.0 / max(1, len(self.moves))
alpha = 0
# prepare brush
set_brush(particle_fn, size=5)
# show all moves
for idx in xrange(0, len(self.moves)):
# decrease timeout
self.moves[idx][2] -= getFrameDt()
x, y, timer = self.moves[idx]
# move timeout, delete it
if timer < 0:
move_to_delete.append(idx)
continue
# save the first move to draw line
if not have_first:
have_first = True
ox, oy = x, y
continue
# calcute steps for having a nice line
numsteps = max(20, int(Vector(ox, oy).distance(Vector(x, y)))/20)
# draw the line !
set_color(1, 1, 1, alpha)
paintLine((ox, oy, x, y), numsteps=10)
# prepare next move
ox, oy = x, y
alpha += alphastep
class GlobalFeedback(MTWidget):
def __init__(self, **kwargs):
super(GlobalFeedback, self).__init__(**kwargs)
self.touches = {}
self.rings = []
def on_draw(self):
alivetouches = []
for touch in getCurrentTouches():
if 'kinetic' in touch.profile and touch.mode == 'spinning':
continue
alivetouches.append(touch.id)
if touch.id not in self.touches:
self.touches[touch.id] = GlobalFeedbackTouch(pos=(touch.x, touch.y))
self.add_widget(self.touches[touch.id])
newsprite = Image(ring_img, pos=touch.pos, opacity=0.75, scale=0.10)
self.rings.append(newsprite)
else:
self.touches[touch.id].pos = (touch.x, touch.y)
touchestodel = []
for touchid in self.touches:
if touchid not in alivetouches:
touchestodel.append(touchid)
for id in touchestodel:
self.remove_widget(self.touches[id])
del self.touches[id]
# Uncomment the line below to always see feedback.
self.bring_to_front()
super(GlobalFeedback, self).on_draw()
def draw(self):
rings_to_delete = []
for i in xrange(0, len(self.rings)):
ring = self.rings[i]
ring.draw()
ring.opacity -= getFrameDt() * 1.5
ring.scale += getFrameDt() * 2
if ring.opacity <= 0:
rings_to_delete.append(ring)
for ring in rings_to_delete:
self.rings.remove(ring)
def start(win, ctx):
ctx.w = GlobalFeedback()
win.add_widget(GlobalFeedback())
def stop(win, ctx):
win.remove_widget(ctx.w)
|