-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgivebox.py
More file actions
executable file
·367 lines (316 loc) · 10.3 KB
/
givebox.py
File metadata and controls
executable file
·367 lines (316 loc) · 10.3 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/python3
"""reverse chess' gift module"""
import sys
import re
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo
from PIL import Image, ImageTk
from PIL import ImageFont
from PIL import ImageDraw
import PIL
import pygame # <--- ADD THIS LINE
# module
from chessassets import IMAGES, PTYPES, offboard_count
def number_image(num):
"""create number image"""
img = Image.new('RGB', (20,20), (250,250,250))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("OpenSans-Regular.ttf", 18)
#draw.text((150, -30),str(num),(0,0,0),font=font)
draw.text((0, 0),str(num),(0,0,0),font=font, align='center')
#img.save('digit_number_img_'+str(i)+'.jpg')
return img
global CLICKED
global COMMANDS
global ROOT
ROOT = None
CLICKED = {}
COMMANDS = {}
for ptype2 in ['p', 'r', 'n', 'q', 'k', 'b']:
CLICKED[ptype2] = 0
def click(gset=None, ptype=ptype2):
global CLICKED
global COMMANDS
global ROOT
CLICKED[ptype] += 1
if gset is not None:
CLICKED[ptype] = gset
else:
if ROOT is not None:
on_close()
return CLICKED[ptype]
COMMANDS[ptype2] = click
global ICONS
ICONS = []
def on_close():
"""close the button window"""
global ROOT
ROOT.quit()
ROOT.destroy()
ROOT = None
def on_close():
"""close the button window"""
global ROOT
if ROOT is not None:
ROOT.destroy() # Just destroy, don't call quit()
ROOT = None
def reset_clicked(clicked):
"""reset the click count"""
global CLICKED
global COMMANDS
global ROOT
for ptype in CLICKED:
CLICKED[ptype] = 0
assert not COMMANDS[ptype](0)
ROOT = None
def givebox(pieces, ignore_count=False):
global CLICKED
global COMMANDS
global ROOT
global ICONS
# Use existing root or create if none exists
if tk._default_root is None:
root = tk.Tk()
else:
root = tk.Toplevel() # Create a child window instead
root.geometry('500x500')
root.resizable(True, True)
root.title('Which piece to give back?'\
if not ignore_count else\
'promote pawn to which piece?')
root.protocol('WM_DELETE_WINDOW',on_close)
canvas = tk.Canvas(root, width = 500, height = 500)
clicked = reset_clicked(CLICKED)
ROOT = root
assert ROOT is not None, ROOT
button_made = {}
if not ignore_count:
counts = offboard_count(pieces)
pady = 15
padx = 20
button_count = 0
for piece in pieces:
team = piece.team
ptype = piece.ptype
if ptype in ('p', 'k') and ignore_count: # ignore_count is used for promotions
continue
if not ignore_count:
count = counts[ptype]
if not count:
continue
if piece.ptype not in button_made:
button_made[ptype] = True
command = COMMANDS[ptype]
image_str = IMAGES[(team, ptype, False)]
image=Image.open(image_str)
if not ignore_count:
add_count_to_image(image, 'P', image_str)
image = add_count_to_image(image, count, image_str)
icon = PIL.ImageTk.PhotoImage(image, master=canvas)
ICONS.append(icon)
button = ttk.Button(root, image=icon, command=command,)
button.grid(row=button_count//2, column=button_count%2, ipady=30, ipadx=30)
button_count += 1
root.mainloop()
ret = None
for icon in ICONS:
pass
for ptype in CLICKED:
if CLICKED[ptype]:
if ignore_count:
return ptype # just give the ptype for this mode
for piece in pieces:
if piece.ptype == ptype and not piece.ontheboard:
ret = piece
break
assert ret is not None, ret
assert CLICKED[ptype] == 1, (ptype, CLICKED[ptype])
break
else:
assert not CLICKED[ptype], CLICKED
if ignore_count:
print("givebox, ignore count:")
piece.ident()
return ret
def add_count_to_image(image, count, image_str):
"""add a count of off-board pieces of a ptype
to image of that piece type"""
im1 = number_image(count) # number of pieces not on the board of that type/team
if count == 'P':
image.paste(im1, (2,2))
image_str = re.sub(".png", "_p.png", image_str)
image.save(image_str)
else:
image.paste(im1, (0,0))
return image
#showinfo(
# title='Information',
# message='Download button clicked! '+str(download_clicked.a))
if __name__ == '__main__':
pass
#givebox()
def givebox(pieces, ignore_count=False):
global CLICKED
global COMMANDS
global ROOT
global ICONS
# Use existing root or create if none exists
if tk._default_root is None:
root = tk.Tk()
root.withdraw() # Hide the root
dialog = tk.Toplevel(root)
else:
dialog = tk.Toplevel() # Create a child window instead
dialog.geometry('500x500')
dialog.resizable(True, True)
dialog.title('Which piece to give back?'\
if not ignore_count else\
'promote pawn to which piece?')
dialog.protocol('WM_DELETE_WINDOW', on_close)
canvas = tk.Canvas(dialog, width = 500, height = 500)
clicked = reset_clicked(CLICKED)
ROOT = dialog # Changed from root to dialog
assert ROOT is not None, ROOT
button_made = {}
if not ignore_count:
counts = offboard_count(pieces)
pady = 15
padx = 20
button_count = 0
for piece in pieces:
team = piece.team
ptype = piece.ptype
if ptype in ('p', 'k') and ignore_count:
continue
if not ignore_count:
count = counts[ptype]
if not count:
continue
if piece.ptype not in button_made:
button_made[ptype] = True
command = COMMANDS[ptype]
image_str = IMAGES[(team, ptype, False)]
image=Image.open(image_str)
if not ignore_count:
add_count_to_image(image, 'P', image_str)
image = add_count_to_image(image, count, image_str)
icon = PIL.ImageTk.PhotoImage(image, master=canvas)
ICONS.append(icon)
button = ttk.Button(dialog, image=icon, command=command,)
button.grid(row=button_count//2, column=button_count%2, ipady=30, ipadx=30)
button_count += 1
# Use wait_window instead of mainloop
dialog.wait_window() # This blocks until dialog is destroyed
ret = None
for icon in ICONS:
pass
for ptype in CLICKED:
if CLICKED[ptype]:
if ignore_count:
return ptype
for piece in pieces:
if piece.ptype == ptype and not piece.ontheboard:
ret = piece
break
assert ret is not None, ret
assert CLICKED[ptype] == 1, (ptype, CLICKED[ptype])
break
else:
assert not CLICKED[ptype], CLICKED
if ignore_count and ret:
print("givebox, ignore count:")
piece.ident()
return ret
def givebox(pieces, ignore_count=False):
global CLICKED
global COMMANDS
global ROOT
global ICONS
# Create root if it doesn't exist
if tk._default_root is None:
hidden_root = tk.Tk()
hidden_root.withdraw()
dialog = tk.Toplevel()
dialog.geometry('500x500')
dialog.resizable(True, True)
dialog.title('Which piece to give back?'\
if not ignore_count else\
'promote pawn to which piece?')
# Don't use protocol - we'll handle it differently
canvas = tk.Canvas(dialog, width = 500, height = 500)
clicked = reset_clicked(CLICKED)
ROOT = dialog
button_made = {}
if not ignore_count:
counts = offboard_count(pieces)
pady = 15
padx = 20
button_count = 0
for piece in pieces:
team = piece.team
ptype = piece.ptype
if ptype in ('p', 'k') and ignore_count:
continue
if not ignore_count:
count = counts[ptype]
if not count:
continue
if piece.ptype not in button_made:
button_made[ptype] = True
command = COMMANDS[ptype]
image_str = IMAGES[(team, ptype, False)]
image=Image.open(image_str)
if not ignore_count:
add_count_to_image(image, 'P', image_str)
image = add_count_to_image(image, count, image_str)
icon = PIL.ImageTk.PhotoImage(image, master=canvas)
ICONS.append(icon)
button = ttk.Button(dialog, image=icon, command=command,)
button.grid(row=button_count//2, column=button_count%2, ipady=30, ipadx=30)
button_count += 1
# Manual event loop - pump both Tk and pygame events
dialog.update()
done = False
while not done:
# 1. Process Pygame events to keep it from freezing
for event in pygame.event.get():
if event.type == pygame.QUIT:
# If the main window is closed, exit everything
pygame.quit()
sys.exit()
# 2. Process Tkinter events
try:
dialog.update_idletasks()
dialog.update()
except tk.TclError:
# Window was closed (e.g., by the 'X' button)
done = True
break
# 3. Check if a button was clicked
for ptype in CLICKED:
if CLICKED[ptype]:
done = True
break
# 4. Small delay to prevent 100% CPU usage
import time
time.sleep(0.01)
# Clean up
try:
dialog.destroy()
except:
pass
ROOT = None
# Return the selected piece
ret = None
for ptype in CLICKED:
if CLICKED[ptype]:
if ignore_count:
return ptype
for piece in pieces:
if piece.ptype == ptype and not piece.ontheboard:
ret = piece
break
if ret:
break
return ret