def on_mouse_motion(x, y, dx, dy): global xoffset, yoffset global scale_factor if not lock_position and not zooming: xoffset -= dx/mouse_sensitivity yoffset -= dy/mouse_sensitivity elif zooming: scale_factor *= math.exp(0.01*vec2.inner( (dx, dy), (1.0, 0.0))) check_hover()
def on_mouse_drag(x, y, dx, dy, buttons, modifiers): global xoffset, yoffset global drag_entity if not zooming: dx /= mouse_sensitivity dy /= mouse_sensitivity xoffset -= dx yoffset -= dy check_hover() if drag_entity: # transformation if not zooming: drag_entity.move(dx, dy) elif zooming: factor = math.exp(0.01* vec2.inner ((dx, dy), (1.0, 0.0)) ) drag_entity.scale_around_center(factor, -xoffset, -yoffset) #adjust opacity if drag_entity.being_created: diff = vec2.abs(vec2.sub( (xoffset, yoffset), (start_drag_x, start_drag_y))) drag_entity.scale = 100/scale_factor #TODO: move that into creation opacity = abs(diff/drag_entity.scale) if opacity >= 1.0: drag_entity.opacity = 1.0 drag_entity.being_created = False else: drag_entity.opacity = opacity # collision detection for entity in entities: if entity != drag_entity: xdiff = abs(entity.x-drag_entity.x) ydiff = abs(entity.y-drag_entity.y) min_x_diff = (entity.scale+drag_entity.scale)/2 min_y_diff = (entity.scale+drag_entity.scale)/3 if xdiff < min_x_diff and ydiff < min_y_diff: if entity.colliding == False: entity.colliding = True drag_entity.delete_on_release += 1 else: if entity.colliding == True: entity.colliding = False drag_entity.delete_on_release -= 1 if hover_entity: def drag_over_id(id, rel_x, scale): id.temp_value = None id.temp_share_binder = None id.temp_apply = None if rel_x < 0: #apply? id.temp_apply = True else: if not (drag_entity.id.is_ancestor_of(hover_entity.id) or drag_entity.id == hover_entity.id): if id.value: drag_over_id(id.value, rel_x-scale/4, scale/2) else: id.temp_value = drag_entity.id #reset is also in check_hover if rel_x < scale/4 and id.binds_id == None: id.temp_share_binder = True else: id.temp_share_binder = False assert hover_entity.colliding == True # mouse cursor should not be able to be outside of drag_entity drag_over_id(hover_entity.id, -xoffset-hover_entity.x, scale=hover_entity.scale)
def draw_thick_cubic_bezier(points, width, color): t = 0.0 curve_points = [] while t <= 1.0: x = points[0][0] * t * t * t + 3 * points[1][0] * t * t * ( 1 - t) + 3 * points[2][0] * t * (1 - t) * ( 1 - t) + points[3][0] * (1 - t) * (1 - t) * (1 - t) y = points[0][1] * t * t * t + 3 * points[1][1] * t * t * ( 1 - t) + 3 * points[2][1] * t * (1 - t) * ( 1 - t) + points[3][1] * (1 - t) * (1 - t) * (1 - t) curve_points.append((x, y)) t += 0.01 outer_points = [] outer_tex_coords = [] inner_points = [] inner_tex_coords = [] ortho_vector = vec2.norm( vec2.sub(curve_points[1], curve_points[0]) ) #todo SOLVED: What happens when two points are in the same spot? Two points never will be in the same spot, thanks to the direction always being added onto the endpoint. ortho_vector[0], ortho_vector[1] = -1 * ortho_vector[1], ortho_vector[0] ortho_vector = vec2.mul(ortho_vector, width) inner_points.append(vec2.sub(curve_points[0], ortho_vector)) outer_points.append(vec2.add(curve_points[0], ortho_vector)) diff = vec2.sub(inner_points[-1], vec2.vecint(inner_points[-1])) projected_diff = -vec2.inner(ortho_vector, diff) / vec2.abs(ortho_vector) inner_tex_coords.extend([0.0, 1.0 - projected_diff / width / 2, 0.0]) diff = vec2.sub(outer_points[-1], vec2.vecint(outer_points[-1])) projected_diff = -vec2.inner(ortho_vector, diff) / vec2.abs(ortho_vector) outer_tex_coords.extend([0.0, (1 - projected_diff) / width / 2, 0.0]) r = 1.0 for i in range(1, len(curve_points) - 1): ortho_vector = vec2.norm( vec2.sub(curve_points[i + 1], curve_points[i - 1])) ortho_vector[0], ortho_vector[1] = -1 * ortho_vector[1], ortho_vector[ 0] ortho_vector = vec2.mul(ortho_vector, width) inner_points.append(vec2.sub(curve_points[i], ortho_vector)) outer_points.append(vec2.add(curve_points[i], ortho_vector)) diff = vec2.sub(inner_points[-1], vec2.vecint(inner_points[-1])) projected_diff = -vec2.inner(ortho_vector, diff) / vec2.abs(ortho_vector) inner_tex_coords.extend([r, 1.0 - projected_diff / width / 2, 0.0]) diff = vec2.sub(outer_points[-1], vec2.vecint(outer_points[-1])) projected_diff = -vec2.inner(ortho_vector, diff) / vec2.abs(ortho_vector) outer_tex_coords.extend([r, (1 - projected_diff) / width / 2, 0.0]) r = -r + 1 #make r alternate between 0 and 1 ortho_vector = vec2.norm(vec2.sub(curve_points[-1], curve_points[-2])) ortho_vector[0], ortho_vector[1] = -1 * ortho_vector[1], ortho_vector[0] ortho_vector = vec2.mul(ortho_vector, width) inner_points.append(vec2.sub(curve_points[-1], ortho_vector)) outer_points.append(vec2.add(curve_points[-1], ortho_vector)) diff = vec2.sub(inner_points[-1], vec2.vecint(inner_points[-1])) projected_diff = -vec2.inner(ortho_vector, diff) / vec2.abs(ortho_vector) inner_tex_coords.extend([r, 1.0 - projected_diff / width / 2, 0.0]) diff = vec2.sub(outer_points[-1], vec2.vecint(outer_points[-1])) projected_diff = -vec2.inner(ortho_vector, diff) / vec2.abs(ortho_vector) outer_tex_coords.extend([r, (1 - projected_diff) / width / 2, 0.0]) all_points = [] tex_coords = [] tex_coords.extend(inner_tex_coords) tex_coords.extend(outer_tex_coords) for point in inner_points: x = int(point[0]) y = int(point[1]) #diff = vec2.abs((x-point[0], y-point[1])) #if bit == True: # tex_coords.extend([0.0, 1.0-diff/a, 0.0]) #else: # tex_coords.extend([1.0, 1.0-diff/a, 0.0]) #bit ^= True all_points.extend([x, y]) for point in outer_points: x = int(point[0]) y = int(point[1]) #if bit == True: # tex_coords.extend([0.0, (1-diff)/a, 0.0]) #else: # tex_coords.extend([1.0, (1-diff)/a, 0.0]) #bit ^= True all_points.extend(point) all_points = [int(p) for p in all_points] all_points = tuple(all_points) indices = [] l = len(inner_points) for i in range(l - 1): indices.append(i) indices.append(i + 1) indices.append(i + l) indices.append(i + 1) indices.append(i + 1 + l) indices.append(i + l) glEnable(gradtex.target) glBindTexture(gradtex.target, gradtex.id) pyglet.graphics.draw_indexed( len(all_points) / 2, pyglet.gl.GL_TRIANGLES, indices, ('v2i', all_points), ('t3f', tuple(tex_coords)) #('c4f', color*(len(all_points)/2)) )