-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.py
291 lines (234 loc) · 10.1 KB
/
editor.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python3
# editor.py was 100% vibe-coded by Claude
import tkinter as tk
import numpy as np
import sys
from clack import blank_screen, clack_post, WIDTH, HEIGHT
class ClackTkEditor:
def __init__(self, root):
self.root = root
self.root.title("Clack Display Editor")
# Initialize the display grid - using transposed orientation
# The physical display is actually WIDTH=28, HEIGHT=14
# But our logical representation is keeping it consistent with the API
self.screen = blank_screen(HEIGHT, WIDTH) # Make sure orientation matches physical display
self.cell_size = 25 # Size of each cell in pixels
self.drawing = False
self.current_mode = 'draw' # 'draw' or 'erase'
self.is_rotated = False # Track the rotation state
# Create frame for the grid
self.frame = tk.Frame(root)
self.frame.pack(pady=10)
# Create canvas for the grid - default orientation is WIDTH=28, HEIGHT=14
self.canvas = tk.Canvas(
self.frame,
width=WIDTH*self.cell_size,
height=HEIGHT*self.cell_size,
bg='black'
)
self.canvas.pack()
# Create cells on the canvas
self.cells = {}
for y in range(HEIGHT):
for x in range(WIDTH):
x1 = x * self.cell_size
y1 = y * self.cell_size
x2 = x1 + self.cell_size
y2 = y1 + self.cell_size
cell = self.canvas.create_rectangle(
x1, y1, x2, y2,
fill='black', outline='gray', width=1,
tags=f"cell_{x}_{y}"
)
self.cells[(x, y)] = cell
# Create buttons frame
self.button_frame = tk.Frame(root)
self.button_frame.pack(pady=5)
# Create Clear button
self.clear_button = tk.Button(
self.button_frame,
text="Clear Grid",
command=self.clear_grid
)
self.clear_button.pack(side=tk.LEFT, padx=5)
# Create Rotate button
self.rotate_button = tk.Button(
self.button_frame,
text="Rotate Display",
command=self.rotate_display
)
self.rotate_button.pack(side=tk.LEFT, padx=5)
# Create Flip Horizontal button
self.flip_h_button = tk.Button(
self.button_frame,
text="Flip Horizontal",
command=self.flip_horizontal
)
self.flip_h_button.pack(side=tk.LEFT, padx=5)
# Create Flip Vertical button
self.flip_v_button = tk.Button(
self.button_frame,
text="Flip Vertical",
command=self.flip_vertical
)
self.flip_v_button.pack(side=tk.LEFT, padx=5)
# Bind mouse events
self.canvas.bind("<Button-1>", self.on_mouse_down)
self.canvas.bind("<B1-Motion>", self.on_mouse_drag)
self.canvas.bind("<ButtonRelease-1>", self.on_mouse_up)
# Render the initial grid
self.update_display()
def get_cell_coords(self, event):
"""Convert canvas coordinates to grid coordinates"""
# Get the current dimensions of the screen
current_height, current_width = self.screen.shape
# Simple conversion from screen coordinates to grid coordinates
x = event.x // self.cell_size
y = event.y // self.cell_size
# Make sure we don't exceed the bounds
if x >= current_width:
x = current_width - 1
if y >= current_height:
y = current_height - 1
return x, y
def on_mouse_down(self, event):
self.drawing = True
x, y = self.get_cell_coords(event)
# Get the current dimensions of the screen
current_height, current_width = self.screen.shape
# Ensure coordinates are within bounds
if not (0 <= x < current_width and 0 <= y < current_height):
return
# Set current_mode based on first clicked cell - inverse of current state
if self.screen[y, x] == b'.':
self.current_mode = 'draw'
else:
self.current_mode = 'erase'
self.toggle_cell(x, y)
def on_mouse_drag(self, event):
if not self.drawing:
return
x, y = self.get_cell_coords(event)
# Get the current dimensions of the screen
current_height, current_width = self.screen.shape
# Ensure coordinates are within bounds
if not (0 <= x < current_width and 0 <= y < current_height):
return
# Get the current state of the cell
current_state = self.screen[y, x].decode('utf-8')
# If the cell state doesn't match our current mode, toggle it
# This ensures continuous drawing/erasing behavior
if (self.current_mode == 'draw' and current_state == '.') or \
(self.current_mode == 'erase' and current_state == 'x'):
self.toggle_cell(x, y)
def on_mouse_up(self, event):
self.drawing = False
def toggle_cell(self, x, y):
"""Toggle a cell between on and off"""
# Get the current dimensions of the screen
current_height, current_width = self.screen.shape
# Ensure coordinates are within bounds
if not (0 <= x < current_width and 0 <= y < current_height):
return
if self.current_mode == 'draw':
self.screen[y, x] = b'x'
self.canvas.itemconfig(self.cells[(x, y)], fill='white')
else: # erase mode
self.screen[y, x] = b'.'
self.canvas.itemconfig(self.cells[(x, y)], fill='black')
# Update the display immediately after each cell change
self.post_to_display()
def update_display(self):
"""Update the display grid based on the screen state"""
current_height, current_width = self.screen.shape
for y in range(current_height):
for x in range(current_width):
if self.screen[y, x] == b'.':
self.canvas.itemconfig(self.cells[(x, y)], fill='black')
else:
self.canvas.itemconfig(self.cells[(x, y)], fill='white')
def clear_grid(self):
"""Clear the entire grid"""
# Get current dimensions to maintain the same orientation
current_height, current_width = self.screen.shape
self.screen = blank_screen(current_height, current_width)
self.update_display()
self.post_to_display()
def rotate_display(self):
"""Rotate the display 90 degrees - swap WIDTH (28) and HEIGHT (14)"""
# Swap WIDTH and HEIGHT for canvas dimensions
self.is_rotated = not self.is_rotated
# Rotate the matrix by using numpy's transpose
self.screen = np.transpose(self.screen)
# Create a new canvas with swapped dimensions
self.canvas.destroy()
if self.is_rotated:
self.canvas = tk.Canvas(
self.frame,
width=HEIGHT*self.cell_size,
height=WIDTH*self.cell_size,
bg='black'
)
else:
self.canvas = tk.Canvas(
self.frame,
width=WIDTH*self.cell_size,
height=HEIGHT*self.cell_size,
bg='black'
)
self.canvas.pack()
# Recreate cells on the canvas
self.cells = {}
current_height, current_width = self.screen.shape
for y in range(current_height):
for x in range(current_width):
x1 = x * self.cell_size
y1 = y * self.cell_size
x2 = x1 + self.cell_size
y2 = y1 + self.cell_size
cell = self.canvas.create_rectangle(
x1, y1, x2, y2,
fill='black' if self.screen[y, x] == b'.' else 'white',
outline='gray',
width=1,
tags=f"cell_{x}_{y}"
)
self.cells[(x, y)] = cell
# Rebind mouse events
self.canvas.bind("<Button-1>", self.on_mouse_down)
self.canvas.bind("<B1-Motion>", self.on_mouse_drag)
self.canvas.bind("<ButtonRelease-1>", self.on_mouse_up)
# Post to display
self.post_to_display()
def flip_horizontal(self):
"""Flip the display horizontally (left/right)"""
# Get current dimensions and flip along the width axis
current_height, current_width = self.screen.shape
# Create a new array with flipped content
for y in range(current_height):
self.screen[y] = self.screen[y][::-1] # Reverse each row
# Update the visual display
self.update_display()
# Update the physical display
self.post_to_display()
def flip_vertical(self):
"""Flip the display vertically (up/down)"""
# Get current dimensions and flip along the height axis
current_height, current_width = self.screen.shape
# Create a new array with flipped content
self.screen = self.screen[::-1] # Reverse the rows
# Update the visual display
self.update_display()
# Update the physical display
self.post_to_display()
def post_to_display(self):
"""Post the current state to the clack display"""
# Create a rotated version of the screen for posting to match the physical display
# This rotates the content 180 degrees to correct the orientation
rotated_screen = np.rot90(self.screen, k=2) # Rotate 180 degrees (k=2)
screen_bytes = b'\n'.join([b''.join(row) for row in rotated_screen])
clack_post(screen_bytes)
if __name__ == "__main__":
root = tk.Tk()
app = ClackTkEditor(root)
root.mainloop()