def transform_with_touch(self, touch): # just do a simple one finger drag if len(self._touches) == 1: return self._apply_drag(touch) # we have more than one touch... points = [Vector(*self._last_touch_pos[t]) for t in self._touches] # we only want to transform if the touch is part of the two touches # furthest apart! So first we find anchor, the point to transform # around as the touch farthest away from touch anchor = max(points, key=lambda p: p.distance(touch.pos)) # now we find the touch farthest away from anchor, if its not the # same as touch. Touch is not one of the two touches used to transform farthest = max(points, key=anchor.distance) if points.index(farthest) != self._touches.index(touch): return # ok, so we have touch, and anchor, so we can actually compute the # transformation old_line = Vector(*touch.dpos) - anchor new_line = Vector(*touch.pos) - anchor angle = radians(new_line.angle(old_line)) * self._do_rotation scale = new_line.length() / old_line.length() new_scale = scale * self.scale if new_scale < self.scale_min or new_scale > self.scale_max: scale = 1.0 self.apply_transform(rotation_matrix(angle, (0, 0, 1)), anchor=anchor) self.apply_transform(scale_matrix(scale), anchor=anchor) #dispatch on_transform with th touch that caused it self.dispatch_event('on_transform', touch)
def apply_angle_scale_trans(self, angle, scale, trans, point=Vector(0, 0)): '''Update matrix transformation by adding new angle, scale and translate. :Parameters: `angle` : float Rotation angle to add `scale` : float Scaling value to add `trans` : Vector Vector translation to add `point` : Vector, default to (0, 0) Point to apply transformation ''' old_scale = self.scale new_scale = old_scale * scale if new_scale < self.scale_min or old_scale > self.scale_max: scale = 1 t = translation_matrix((trans[0] * self._do_translation_x, trans[1] * self._do_translation_y, 0)) t = matrix_multiply(t, translation_matrix((point[0], point[1], 0))) t = matrix_multiply(t, rotation_matrix(angle, (0, 0, 1))) t = matrix_multiply(t, scale_matrix(scale)) t = matrix_multiply(t, translation_matrix((-point[0], -point[1], 0))) self.apply_transform(t) self.dispatch_event('on_transform', None)
def transform_with_touch(self, touch): # just do a simple one finger drag if len(self._touches) == 1: return self._apply_drag(touch) # we have more than one touch... points = [Vector(*self._last_touch_pos[t]) for t in self._touches] # we only want to transform if the touch is part of the two touches # furthest apart! So first we find anchor, the point to transform # around as the touch farthest away from touch anchor = max(points, key=lambda p: p.distance(touch.pos)) # now we find the touch farthest away from anchor, if its not the # same as touch. Touch is not one of the two touches used to transform farthest = max(points, key=anchor.distance) if points.index(farthest) != self._touches.index(touch): return # ok, so we have touch, and anchor, so we can actually compute the # transformation old_line = Vector(*touch.dpos) - anchor new_line = Vector(*touch.pos) - anchor angle = radians( new_line.angle(old_line) ) * self._do_rotation scale = new_line.length() / old_line.length() new_scale = scale * self.scale if new_scale < self.scale_min or new_scale > self.scale_max: scale = 1.0 self.apply_transform(rotation_matrix(angle, (0, 0, 1)), anchor=anchor) self.apply_transform(scale_matrix(scale), anchor=anchor) #dispatch on_transform with th touch that caused it self.dispatch_event('on_transform', touch)
def apply_angle_scale_trans(self, angle, scale, trans, point=Vector(0, 0)): '''Update matrix transformation by adding new angle, scale and translate. :Parameters: `angle` : float Rotation angle to add `scale` : float Scaling value to add `trans` : Vector Vector translation to add `point` : Vector, default to (0, 0) Point to apply transformation ''' old_scale = self.scale new_scale = old_scale * scale if new_scale < self.scale_min or old_scale > self.scale_max: scale = 1 t = translation_matrix((trans[0]*self._do_translation_x, trans[1]*self._do_translation_y, 0)) t = matrix_multiply(t, translation_matrix( (point[0], point[1], 0))) t = matrix_multiply(t, rotation_matrix(angle, (0, 0, 1))) t = matrix_multiply(t, scale_matrix(scale)) t = matrix_multiply(t, translation_matrix((-point[0], -point[1], 0))) self.apply_transform(t) self.dispatch_event('on_transform', None)
def _set_rotation(self, rotation): angle_change = self.rotation - rotation r = rotation_matrix(-radians(angle_change), (0, 0, 1)) self.apply_transform(r, post_multiply=True, anchor=self.to_local(*self.center))