def main(screen=None): if screen: screens = pyglet.canvas.get_display().get_screens() window = pyglet.window.Window(screen=screens[screen], fullscreen=True) else: window = pyglet.window.Window(fullscreen=True) radius = np.diff(RADIUS_RANGE) * np.random.sample() + RADIUS_RANGE[0] shape_kwargs = { 'velocity': np.random.sample(2) * np.diff(VELOCITY_RANGE) + VELOCITY_RANGE[0] } center = [window.width / 2, window.height / 2] shapes = [Shape.circle(center, radius, **shape_kwargs)] shapes.extend( Shape.regular_polygon(center, radius, verts, start_angle=np.random.random() * 2 * np.pi / verts, **shape_kwargs) for verts in range(3, MAX_POLYGON_VERTICES + 1)) change_angular_velocity(shapes) for shape in shapes[1:]: shape.enable(False) window.set_handlers(on_draw=partial(draw, window, shapes), on_key_press=partial(on_key_press, shapes)) pyglet.clock.schedule(partial(update, window, shapes)) pyglet.app.run()
def test_regular_polygon_from_dict(): spec = { 'center': [0, 0], 'radius': 1, 'n_vertices': 10, } assert Shape.from_dict(spec) == Shape.regular_polygon([0, 0], 1, 10)
def test_rectangle(): rect = Shape.rectangle([[-1, -1], [1, 1]]) other_rect = Shape.regular_polygon([0, 0], np.sqrt(2), 4, start_angle=np.pi / 4) assert rect == other_rect
def test_eq(): assert Shape.regular_polygon([0, 0], 1, 4) != Shape.regular_polygon([0, 0], 1, 5) assert Shape.regular_polygon([0, 0], 1, 4) == Shape.regular_polygon([0, 0], 1, 4, start_angle=np.pi)
def test_update(): shape = Shape.circle([0, 0], 1, velocity=[1, 1]) shape.update(1) assert np.all(np.isclose(shape.center, [1, 1])) shape = Shape.regular_polygon([0, 0], 1, 6, angular_velocity=1, velocity=[-2, 2]) shape.update(0.5) assert shape == Shape.regular_polygon([-1, 1], 1, 6, angular_velocity=1, velocity=[-2, 2], start_angle=0.5)
def test_arithmetic(): shape = Shape.circle([0, 0], 1, color=(0, 0, 0)) assert shape + [1, 10] == [1, 10] + shape == Shape.circle([1, 10], 1, color=(0, 0, 0)) assert shape - [1, 10] == Shape.circle([-1, -10], 1, color=(0, 0, 0)) assert 10 * shape == shape * 10 == Shape.circle([0, 0], 10, color=(0, 0, 0)) stretched = shape * np.array([2, 4]) assert [2, 4] * shape == stretched for point in stretched.vertices: assert np.isclose(np.linalg.norm(point / [2, 4]), 1)
def test_from_dict(): spec = { 'vertices': [[1, 0], [0, 1], [-1, 0], [0, -1]], 'color': (120, 50, 12), 'velocity': np.random.sample(2), } assert Shape.from_dict(spec) == Shape(spec['vertices'], color=spec['color'], velocity=spec['velocity'])
def test_rotate(): shape = Shape.rectangle([[-1, -1], [1, 1]]) shape.rotate(np.pi / 2) assert shape == Shape.rectangle([[-1, -1], [1, 1]]) shape.rotate(np.pi / 4) assert shape == Shape.regular_polygon([0, 0], np.sqrt(2), 4) shape = Shape.rectangle([[-1, -1], [1, 1]]) shape.rotate(np.pi / 2, [1, 1]) assert shape == Shape.rectangle([[1, -1], [3, 1]])
def test_rotate(): shape = Shape.rectangle([[-1, -1], [1, 1]]) shape.rotate(np.pi/2) assert shape == Shape.rectangle([[-1, -1], [1, 1]]) shape.rotate(np.pi/4) assert shape == Shape.regular_polygon([0, 0], np.sqrt(2), 4) shape = Shape.rectangle([[-1, -1], [1, 1]]) shape.rotate(np.pi/2, [1, 1]) assert shape == Shape.rectangle([[1, -1], [3, 1]])
def ns_switch_mode(self, new_mode): global mode_area, input_area self.mode = Modes.NormalSingle mode_label.text = 'Py Single' mode_label.y = 17 input_area = Shape.rectangle([[100, 30], [window.width, 0]], color=(50, 50, 50)) layout.y = 0 layout.height = 25 mode_area = Shape.rectangle([[0, 30], [100, 0]], color=(104, 209, 250))
def cm_switch_mode(self, new_node): global mode_area, input_area self.mode = Modes.CommandMode mode_label.text = '<CMD>' mode_label.y = 17 input_area = Shape.rectangle([[100, 30], [window.width, 0]], color=(50, 50, 50)) layout.y = 0 layout.height = 25 mode_area = Shape.rectangle([[0, 30], [100, 0]], color=(220, 89, 106))
def nm_switch_mode(self, new_mode): global mode_area, input_area self.mode = Modes.NormalMulti mode_label.text = 'Py Multi' mode_area = Shape.rectangle([[0, 130], [100, 100]], color=(209, 104, 250)) mode_label.y = 117 input_area = Shape.rectangle([[100, 130], [window.width, 0]], color=(50, 50, 50)) layout.y = 0 layout.height = 125
def test_arithmetic(): shape = Shape.circle([0, 0], 1, color=(0, 0, 0)) assert shape + [1, 10] == [1, 10] + shape == Shape.circle( [1, 10], 1, color=(0, 0, 0)) assert shape - [1, 10] == Shape.circle([-1, -10], 1, color=(0, 0, 0)) assert 10 * shape == shape * 10 == Shape.circle( [0, 0], 10, color=(0, 0, 0)) stretched = shape * np.array([2, 4]) assert [2, 4] * shape == stretched for point in stretched.vertices: assert np.isclose(np.linalg.norm(point / [2, 4]), 1)
def test_repr(): shape = Shape.circle([0, 0], 1, velocity=[1, 1], color=(1, 2, 3), colors={'a': (20, 6, 169)}) assert eval(repr(shape)) == shape
def on_resize(width, height): label.width = width label.y = height layout.width = width - 10 input_area = Shape.rectangle([[100, 30], [window.width - 100, 0]], color=(50, 50, 50))
def test_in_place_arithmetic(): shape = Shape.rectangle([[-1, -1], [1, 1]]) shape += [10, 5] assert shape != Shape.rectangle([[-1, -1], [1, 1]]) assert shape == Shape.rectangle([[9, 4], [11, 6]]) shape -= [10, 5] assert shape == Shape.rectangle([[-1, -1], [1, 1]]) shape *= 10 assert shape != Shape.rectangle([[-1, -1], [1, 1]]) assert shape == Shape.rectangle([[-10, -10], [10, 10]]) shape /= 10 assert shape == Shape.rectangle([[-1, -1], [1, 1]]) shape *= [1, 5] assert shape != Shape.rectangle([[-1, -1], [1, 1]]) assert shape == Shape.rectangle([[-1, -5], [1, 5]]) shape /= [10, 1] assert shape == Shape.rectangle([[-0.1, -5], [0.1, 5]])
def test_regular_polygon(): shape = Shape.regular_polygon([0, 0], 1, 4) assert np.all(np.isclose( shape.vertices, [[1, 0], [0, 1], [-1, 0], [0, -1]] ))
def on_draw(self): self.clear() self.beatradius = int(self.width * 0.045 * 0.5) self.beaty = int(self.height - (self.beatradius * 2.5)) self.barheight = int(self.height - (self.beatradius * 4)) self.barwidth = int(self.width * 0.06) self.begin = int(self.barwidth * 0.5) shapes = [] x = self.begin width = self.barwidth for bin in "bass", "mid", "tre": d = self.data["bins"][bin] level = int(d["level"] * self.barheight) flux = int(d["flux"] * self.barheight) trans = int(d["transient"] * 255) if not self.data["silence"]: shapes.append(Shape.circle([x + self.beatradius, self.beaty], self.beatradius, color=(0, trans, 0))) shapes.append( Shape.rectangle([(x, 0), (x+width, level)], color=(255, 0, 0))) x += width shapes.append( Shape.rectangle([(x, 0), (x+width, flux)], color=(255, 128, 0))) x += int(width * 1.5) x += self.barwidth width = int((self.width - x) / (len(self.data["spectrum"]) + 1)) g = 0 for level in self.data["spectrum"]: level = int(level * self.barheight) shapes.append( Shape.rectangle([(x, 0), (x+width, level)], color=(0, g, 255))) g = min(g + 20, 255) x += width for shape in shapes: shape.draw()
def test_enable_disable(): shape = Shape.rectangle([[-1, -1], [1, 1]], color=(100, 100, 100)) shape.enable(False) shape.draw() assert not shape._vertex_list.draw.called shape.enable(True) shape.draw() assert shape._vertex_list.draw.call_count == 1 call_args = shape._vertex_list.draw.call_args assert len(call_args[0]) == 1 gl_triangles = call_args[0][0] assert isinstance(gl_triangles, Mock)
def main(screen=None): if screen: screens = pyglet.canvas.get_display().get_screens() window = pyglet.window.Window(screen=screens[screen], fullscreen=True) else: window = pyglet.window.Window(fullscreen=True) radius = np.diff(RADIUS_RANGE) * np.random.sample() + RADIUS_RANGE[0] shape_kwargs = {'velocity': np.random.sample(2) * np.diff(VELOCITY_RANGE) + VELOCITY_RANGE[0]} center = [window.width/2, window.height/2] shapes = [Shape.circle(center, radius, **shape_kwargs)] shapes.extend(Shape.regular_polygon(center, radius, verts, start_angle=np.random.random() * 2 * np.pi / verts, **shape_kwargs) for verts in range(3, MAX_POLYGON_VERTICES + 1)) change_angular_velocity(shapes) for shape in shapes[1:]: shape.enable(False) window.set_handlers(on_draw=partial(draw, window, shapes), on_key_press=partial(on_key_press, shapes)) pyglet.clock.schedule(partial(update, window, shapes)) pyglet.app.run()
def test_vertex_list(): shape = Shape.rectangle([[-1, -1], [1, 1]], color=(100, 100, 100)) indices = [0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 1] vertices = [0, 0, -1, -1, 1, -1, 1, 1, -1, 1], colors = 5 * [100, 100, 100] args = shape._vertex_list.args assert len(args) == 4 assert args[0] == 5 assert args[1] == indices assert args[2][0] == 'v2f' assert np.all(np.isclose(args[2][1], vertices)) assert args[3][0] == 'c3B' assert np.all(np.isclose(args[3][1], colors))
def test_colors(): colors = { 'primary': (0, 0, 0), 'secondary': (100, 100, 100), 'flashing': (20, 10, 89), } shape = Shape.circle([0, 0], 1, colors=colors) assert shape.color == 'primary' shape.color = 'secondary' assert shape.color == 'secondary' shape.color = (72, 71, 8) assert shape.color == 'secondary' shape.color = 'flashing' assert shape.colors['secondary'] == (72, 71, 8)
def test_scale(): shape = Shape.circle([0, 0], 1) shape.scale(10) assert shape == Shape.circle([0, 0], 10) shape = Shape.rectangle([[-1, -1], [1, 1]]) shape.scale([2, 5]) assert shape == Shape.rectangle([[-2, -5], [2, 5]]) shape = Shape.rectangle([[-1, -1], [1, 1]]) shape.scale(2, center=[1, 1]) assert shape == Shape.rectangle([[-3, -3], [1, 1]])
def test_flip(): shape = Shape.rectangle([[-1, -1], [1, 1]]) shape.flip_x() assert shape == Shape.rectangle([[-1, -1], [1, 1]]) shape.flip_y() assert shape == Shape.rectangle([[-1, -1], [1, 1]]) shape.flip_x([1, 0]) assert shape == Shape.rectangle([[1, -1], [3, 1]]) shape.flip_y([0, 1]) assert shape == Shape.rectangle([[1, 1], [3, 3]]) shape -= [2, 2] shape.flip(np.pi/4) assert shape == Shape.rectangle([[-1, -1], [1, 1]]) shape.flip(np.pi/4, center=[-1, 1]) assert shape == Shape.rectangle([[-3, 1], [-1, 3]])
def test_flip(): shape = Shape.rectangle([[-1, -1], [1, 1]]) shape.flip_x() assert shape == Shape.rectangle([[-1, -1], [1, 1]]) shape.flip_y() assert shape == Shape.rectangle([[-1, -1], [1, 1]]) shape.flip_x([1, 0]) assert shape == Shape.rectangle([[1, -1], [3, 1]]) shape.flip_y([0, 1]) assert shape == Shape.rectangle([[1, 1], [3, 3]]) shape -= [2, 2] shape.flip(np.pi / 4) assert shape == Shape.rectangle([[-1, -1], [1, 1]]) shape.flip(np.pi / 4, center=[-1, 1]) assert shape == Shape.rectangle([[-3, 1], [-1, 3]])
def test_neq(): assert Shape.circle([0, 0], 1) != 0
def test_radius(): shape = Shape.circle([0, 0], 1) assert shape.radius == 1 shape.radius = 0.1 assert shape == Shape.circle([0, 0], 0.1)
def test_distance_to(): shape = Shape.circle([0, 0], 1) assert np.isclose(shape.distance_to([1, 1]), np.sqrt(2))
def test_regular_polygon(): shape = Shape.regular_polygon([0, 0], 1, 4) assert np.all( np.isclose(shape.vertices, [[1, 0], [0, 1], [-1, 0], [0, -1]]))
def test_center(): shape = Shape.rectangle([[-1, -1], [1, 1]]) assert np.all(np.isclose(shape.center, [0, 0])) shape.center = [1, 1] assert shape == Shape.rectangle([[0, 0], [2, 2]])
def test_circle_from_dict(): spec = { 'center': [0, 0], 'radius': 1, } assert Shape.from_dict(spec) == Shape.circle([0, 0], 1)
def test_color(): shape = Shape.circle([0, 0], 1, color=(1, 2, 3)) assert shape.color == (1, 2, 3) shape.color = (10, 20, 30) assert shape.color == (10, 20, 30)
def test_circle(): assert Shape.circle([0, 0], 1, n_vertices=50) == Shape.regular_polygon([0, 0], 1, 50)
from pyglet2d import Shape window = pyglet.window.Window(1024, 768, resizable=True) label = pyglet.text.Label('Hello, world', font_name='Consolas', font_size=12, x=0, y=window.height, multiline=True, width=window.width, anchor_x='left', anchor_y='top') input_area = Shape.rectangle([[100, 30], [window.width, 0]], color=(50, 50, 50)) mode_area = Shape.rectangle([[0, 30], [100, 0]], color=(104, 209, 250)) text_input = pyglet.text.Label() mode_label = pyglet.text.Label('Py Mixed', font_name='Consolas', font_size=12, x=50, y=17, anchor_x='center', anchor_y='center', bold=True, color=(20, 40, 150, 255)) cwd_label = pyglet.text.Label('', font_name='Consolas',
def test_rectangle(): rect = Shape.rectangle([[-1, -1], [1, 1]]) other_rect = Shape.regular_polygon([0, 0], np.sqrt(2), 4, start_angle=np.pi/4) assert rect == other_rect
def test_getitem(): assert np.all(Shape.regular_polygon([0, 0], 1, 10)[0] == [1, 0])
def test_bool(): assert Shape.circle([0, 0], 1)
def test_difference(): assert Shape.rectangle([[-1, 0], [1, 3]]) - Shape.rectangle([[0, 0], [2, 3]]) == Shape.rectangle([[-1, 0], [0, 3]])
def test_xor(): assert Shape.rectangle([[-1, 0], [0, 1]]) ^ Shape.rectangle([[0, 0], [1, 1]]) == Shape.rectangle([[-1, 0], [1, 1]]) assert ((Shape.rectangle([[-1, 0], [0.5, 1]]) ^ Shape.rectangle([[-0.5, 0], [1, 1]])) == (Shape.rectangle([[-1, 0], [1, 1]])) - Shape.rectangle([[-0.5, 0], [0.5, 1]]))
def test_covers(): a = Shape.circle([-1, 0], 1) b = a / 2 + [0.5, 0.5] assert not a.covers(b) a *= 2 assert a.covers(b)
def test_rectangle_from_dict(): spec = { 'vertices': [[-1, -1], [1, 1]], } assert Shape.from_dict(spec) == Shape.rectangle(spec['vertices'])
def test_union(): assert (Shape.rectangle([[-1, 0], [0, 1]]) | Shape.rectangle([[0, 0], [1, 1]]) == Shape.rectangle([[-1, 0], [0, 1]]) + Shape.rectangle([[0, 0], [1, 1]]) == Shape.rectangle([[-1, 0], [1, 1]]))
def test_intersection(): assert Shape.rectangle([[-1, 0], [1, 2]]) & Shape.rectangle([[0, 0], [1, 1]]) == Shape.rectangle([[0, 0], [1, 1]])