| 1 |
import itertools |
| 2 |
import inspect |
| 3 |
from typing import Dict, Optional, Generator, Tuple, Type |
| 4 |
|
| 5 |
import pyglet.window |
| 6 |
import math |
| 7 |
from pyglet.shapes import ( |
| 8 |
Arc, |
| 9 |
BezierCurve, |
| 10 |
Circle, |
| 11 |
Ellipse, |
| 12 |
Sector, |
| 13 |
Line, |
| 14 |
MultiLine, |
| 15 |
Rectangle, |
| 16 |
BorderedRectangle, |
| 17 |
Triangle, |
| 18 |
Star, |
| 19 |
Polygon, |
| 20 |
Box, |
| 21 |
ShapeBase |
| 22 |
) |
| 23 |
from pyglet.text import Label |
| 24 |
|
| 25 |
INSTRUCTIONS = """- / = keys to rotate |
| 26 |
Arrow keys to change anchor points |
| 27 |
Left click + drag to adjust positions |
| 28 |
""" |
| 29 |
|
| 30 |
PX_SIZE = 10 |
| 31 |
PX_SIZE_2 = PX_SIZE * 2 |
| 32 |
|
| 33 |
|
| 34 |
colors = itertools.cycle(map(lambda c: c + (255,), [ |
| 35 |
(255, 0, 0), |
| 36 |
(0, 255, 0), |
| 37 |
(0, 0, 255), |
| 38 |
(255, 255, 0) |
| 39 |
])) |
| 40 |
|
| 41 |
|
| 42 |
shape_configs = [ |
| 43 |
(Arc, (PX_SIZE,), {}), |
| 44 |
(BezierCurve, [(0, 0), (PX_SIZE, PX_SIZE_2), (PX_SIZE_2, PX_SIZE)], {}), |
| 45 |
(Circle, (PX_SIZE,), {}), |
| 46 |
(Ellipse, (PX_SIZE, PX_SIZE_2,), {}), |
| 47 |
|
| 48 |
(Sector, (PX_SIZE,), {}), |
| 49 |
(Line, (0, 0, PX_SIZE_2, PX_SIZE_2), {}), |
| 50 |
(Rectangle, (PX_SIZE, PX_SIZE_2), {}), |
| 51 |
(BorderedRectangle, (PX_SIZE_2, PX_SIZE), {}), |
| 52 |
(Box, (PX_SIZE_2, PX_SIZE_2), {}), |
| 53 |
|
| 54 |
(Triangle, (-PX_SIZE, 0, 0, PX_SIZE, PX_SIZE, 0), {}), |
| 55 |
(Star, (PX_SIZE_2, PX_SIZE, 5), {}), |
| 56 |
(Polygon, ((-PX_SIZE, 0), (0, PX_SIZE), (PX_SIZE, 0)), {}), |
| 57 |
(MultiLine, ((-PX_SIZE, 0), (0, PX_SIZE), (PX_SIZE, 0)), {}), |
| 58 |
] |
| 59 |
|
| 60 |
|
| 61 |
def layout_points( |
| 62 |
width: int, height: int, |
| 63 |
padding: int, |
| 64 |
num_cols: int, |
| 65 |
num_rows: Optional[int] = None |
| 66 |
) -> Generator[Tuple[int, int], None, None]: |
| 67 |
"""Yield points out in a col / row grid. |
| 68 |
|
| 69 |
All units in px. |
| 70 |
Args: |
| 71 |
width: Width of the area |
| 72 |
height: Height of the area |
| 73 |
padding: How many px to pad on all sides |
| 74 |
num_cols: How many columns each row should have |
| 75 |
num_rows: Specify to prevent auto-calculation |
| 76 |
|
| 77 |
Yields: |
| 78 |
Bottom left screen position of the grid cell in px |
| 79 |
""" |
| 80 |
x_step_range = width - 2 * padding |
| 81 |
y_step_range = height - 2 * padding |
| 82 |
|
| 83 |
if num_rows is None: |
| 84 |
num_rows = math.ceil(len(shape_configs) / num_cols) |
| 85 |
|
| 86 |
x_step = x_step_range // num_cols |
| 87 |
y_step = y_step_range // num_rows |
| 88 |
|
| 89 |
for y in range(height - padding, y_step, -y_step): |
| 90 |
for x in range(padding, padding + x_step_range, x_step): |
| 91 |
yield x, y |
| 92 |
|
| 93 |
|
| 94 |
def main(): |
| 95 |
padding = 50 |
| 96 |
width, height = 600, 600 |
| 97 |
|
| 98 |
window = pyglet.window.Window( |
| 99 |
width, height, |
| 100 |
caption="Shape Translation / Rotation Spotcheck", |
| 101 |
resizable=True) |
| 102 |
|
| 103 |
shapes: Dict[Type, ShapeBase] = {} |
| 104 |
labels: Dict[Type, Label] = {} |
| 105 |
batch = pyglet.graphics.Batch() |
| 106 |
|
| 107 |
for shape_config, position in zip(shape_configs, layout_points(window.width, window.height, padding, 4)): |
| 108 |
shape_type, args, kwargs = shape_config |
| 109 |
sig = inspect.signature(shape_type) |
| 110 |
params = list(sig.parameters) |
| 111 |
print(shape_type.__name__, params) |
| 112 |
|
| 113 |
|
| 114 |
if params[:2] == ['x', 'y'] and params[2] != 'x2': |
| 115 |
final_args = position + args |
| 116 |
elif shape_type in (Polygon, BezierCurve, MultiLine): |
| 117 |
raw = [(position[0] + p[0], position[1] + p[1]) for p in args] |
| 118 |
final_args = raw |
| 119 |
print(shape_type.__name__, final_args) |
| 120 |
|
| 121 |
elif shape_type in (Line, Triangle): |
| 122 |
final_args = [ |
| 123 |
position[0] + args[0], position[1] + args[1], |
| 124 |
position[0] + args[2], position[1] + args[3]] |
| 125 |
if shape_type is Triangle: |
| 126 |
final_args += [position[0] + args[4], position[1] + args[5]] |
| 127 |
print(shape_type.__name__, final_args) |
| 128 |
|
| 129 |
else: |
| 130 |
raise TypeError("unhandled") |
| 131 |
|
| 132 |
final_kwargs = dict(color=next(colors)) |
| 133 |
final_kwargs.update(kwargs) |
| 134 |
|
| 135 |
label = pyglet.text.Label( |
| 136 |
shape_type.__name__, |
| 137 |
x=position[0], y=position[1] - padding, |
| 138 |
batch=batch, |
| 139 |
anchor_x='center', |
| 140 |
**final_kwargs |
| 141 |
) |
| 142 |
shape = shape_type(*final_args, batch=batch, **final_kwargs) |
| 143 |
shapes[shape_type] = shape |
| 144 |
labels[shape_type] = label |
| 145 |
|
| 146 |
_instructions = pyglet.text.Label( |
| 147 |
INSTRUCTIONS, |
| 148 |
x=window.width // 2, y=padding, |
| 149 |
width=window.width, |
| 150 |
anchor_x='center', |
| 151 |
align='center', |
| 152 |
batch=batch, |
| 153 |
multiline=True |
| 154 |
) |
| 155 |
|
| 156 |
@window.event |
| 157 |
def on_draw(): |
| 158 |
window.clear() |
| 159 |
batch.draw() |
| 160 |
|
| 161 |
@window.event |
| 162 |
def on_key_press(symbol, modifiers): |
| 163 |
if symbol == pyglet.window.key.MINUS: |
| 164 |
for item in shapes.values(): |
| 165 |
item.rotation -= 20 |
| 166 |
elif symbol == pyglet.window.key.EQUAL: |
| 167 |
for item in shapes.values(): |
| 168 |
item.rotation += 20 |
| 169 |
elif symbol == pyglet.window.key.LEFT: |
| 170 |
for item in shapes.values(): |
| 171 |
item.anchor_x += 10 |
| 172 |
elif symbol == pyglet.window.key.RIGHT: |
| 173 |
for item in shapes.values(): |
| 174 |
item.anchor_x -= 10 |
| 175 |
elif symbol == pyglet.window.key.DOWN: |
| 176 |
for item in shapes.values(): |
| 177 |
item.anchor_y += 10 |
| 178 |
elif symbol == pyglet.window.key.UP: |
| 179 |
for item in shapes.values(): |
| 180 |
item.anchor_y -= 10 |
| 181 |
|
| 182 |
@window.event |
| 183 |
def on_mouse_drag(x, y, dx, dy, buttons, modifiers): |
| 184 |
if buttons == pyglet.window.mouse.LEFT: |
| 185 |
for item in itertools.chain(shapes.values(), labels.values()): |
| 186 |
item_x, item_y, *item_z = item.position |
| 187 |
item.position = item_x + dx, item_y + dy, *item_z |
| 188 |
|
| 189 |
|
| 190 |
pyglet.clock.schedule_interval(window.draw, 1 / 60) |
| 191 |
pyglet.app.run() |
| 192 |
|
| 193 |
|
| 194 |
if __name__ == "__main__": |
| 195 |
main() |