Remove leftover PyGame based code
This commit is contained in:
parent
cde9c3485f
commit
f552becc18
183
main.py
183
main.py
@ -106,186 +106,3 @@ button_glyph_search.pack(side="left")
|
|||||||
|
|
||||||
window.config(menu=menubar)
|
window.config(menu=menubar)
|
||||||
window.mainloop()
|
window.mainloop()
|
||||||
|
|
||||||
# main loop
|
|
||||||
while True:
|
|
||||||
for event in pygame.event.get():
|
|
||||||
if event.type == pygame.QUIT:
|
|
||||||
pygame.quit()
|
|
||||||
quit()
|
|
||||||
|
|
||||||
elif event.type == pygame.MOUSEWHEEL:
|
|
||||||
char_num += event.y * -1
|
|
||||||
|
|
||||||
does_char_exist = check_char_exist(char_num)
|
|
||||||
|
|
||||||
|
|
||||||
elif event.type == pygame.KEYDOWN:
|
|
||||||
|
|
||||||
# delete char
|
|
||||||
if event.key == pygame.K_DELETE:
|
|
||||||
if does_char_exist:
|
|
||||||
del chars[chr(char_num)]
|
|
||||||
does_char_exist = False
|
|
||||||
force_deleted = True
|
|
||||||
|
|
||||||
# toggle grid
|
|
||||||
if event.key == pygame.K_g:
|
|
||||||
is_grid = not is_grid
|
|
||||||
|
|
||||||
# export font
|
|
||||||
elif event.key == pygame.K_e:
|
|
||||||
export(char_res, chars)
|
|
||||||
|
|
||||||
# print key tips
|
|
||||||
elif event.key == pygame.K_p:
|
|
||||||
cli_ui.key_tips()
|
|
||||||
|
|
||||||
|
|
||||||
# initialize current stroke
|
|
||||||
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
|
||||||
pixel_pos = cursor_pos_to_pixel(pygame.mouse.get_pos())
|
|
||||||
# if char is drawable at current position
|
|
||||||
if pixel_pos is not None:
|
|
||||||
# create char dictionary entry if it does not exist
|
|
||||||
if not does_char_exist:
|
|
||||||
chars[chr(char_num)] = np.zeros(char_res, dtype=bool)
|
|
||||||
does_char_exist = True
|
|
||||||
|
|
||||||
paint_color = not chars[chr(char_num)][pixel_pos]
|
|
||||||
|
|
||||||
# save at the end of the stroke
|
|
||||||
elif (event.type == pygame.MOUSEBUTTONUP and event.button == 1) or force_deleted:
|
|
||||||
force_deleted = False
|
|
||||||
|
|
||||||
## packbit all chars
|
|
||||||
|
|
||||||
# calculate the number of bits needed to pad char array to a multiple of 8
|
|
||||||
remainder = char_res[0] * char_res[1] % 8
|
|
||||||
padding = (8 - remainder) % 8
|
|
||||||
|
|
||||||
|
|
||||||
packed_chars = {}
|
|
||||||
for key, value in chars.items():
|
|
||||||
# reshape into 1D array
|
|
||||||
new_data = value.reshape(-1)
|
|
||||||
# pad
|
|
||||||
new_data = np.pad(new_data, (0, padding), mode="constant")
|
|
||||||
# reshape the padded array into a 2D array with 8 columns
|
|
||||||
new_data = new_data.reshape(-1, 8)
|
|
||||||
# packbits
|
|
||||||
new_data = np.packbits(new_data, axis=1).tobytes()
|
|
||||||
# convert to base64
|
|
||||||
new_data = base64.b64encode(new_data).decode("utf-8")
|
|
||||||
|
|
||||||
packed_chars[key] = new_data
|
|
||||||
|
|
||||||
proj_data["chars"] = packed_chars
|
|
||||||
|
|
||||||
with open(proj_path, "w", encoding="utf-8") as f:
|
|
||||||
json.dump(proj_data, f, indent=4, ensure_ascii=False)
|
|
||||||
|
|
||||||
print("Autosaved project.")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# perform drawing
|
|
||||||
if pygame.mouse.get_pressed()[0] and does_char_exist:
|
|
||||||
pixel_pos = cursor_pos_to_pixel(pygame.mouse.get_pos())
|
|
||||||
|
|
||||||
if pixel_pos is not None:
|
|
||||||
chars[chr(char_num)][pixel_pos] = paint_color
|
|
||||||
|
|
||||||
# if all pixels are off, delete char
|
|
||||||
if not np.any(chars[chr(char_num)]):
|
|
||||||
del chars[chr(char_num)]
|
|
||||||
does_char_exist = False
|
|
||||||
|
|
||||||
|
|
||||||
window.fill((0, 0, 0))
|
|
||||||
|
|
||||||
|
|
||||||
if does_char_exist:
|
|
||||||
current_char_array = chars[chr(char_num)]
|
|
||||||
# draw pixels from current_char_array
|
|
||||||
for y in range(char_res[1]):
|
|
||||||
for x in range(char_res[0]):
|
|
||||||
if current_char_array[x, y]:
|
|
||||||
pygame.draw.rect(
|
|
||||||
window,
|
|
||||||
pixel_color,
|
|
||||||
(
|
|
||||||
x * pixel_size,
|
|
||||||
y * pixel_size,
|
|
||||||
pixel_size,
|
|
||||||
pixel_size
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
# draw char num
|
|
||||||
font.render_to(
|
|
||||||
window,
|
|
||||||
(canva_width + 30, 30),
|
|
||||||
f"dec: {char_num}",
|
|
||||||
(255, 255, 255),
|
|
||||||
size=64,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# draw char
|
|
||||||
font.render_to(
|
|
||||||
window,
|
|
||||||
(canva_width + 30, 100),
|
|
||||||
chr(char_num),
|
|
||||||
(255, 255, 255),
|
|
||||||
size=150,
|
|
||||||
)
|
|
||||||
|
|
||||||
# draw alternate font char
|
|
||||||
alt_font.render_to(
|
|
||||||
window,
|
|
||||||
(canva_width + 200, 100),
|
|
||||||
chr(char_num),
|
|
||||||
(255, 255, 255),
|
|
||||||
size=256,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# draw char name
|
|
||||||
font.render_to(
|
|
||||||
window,
|
|
||||||
(canva_width + 30, 280),
|
|
||||||
unicodedata.name(chr(char_num), "unknown"),
|
|
||||||
(255, 255, 255),
|
|
||||||
size=16,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# draw a red cross if curren char does not exist
|
|
||||||
if not does_char_exist:
|
|
||||||
pygame.draw.line(
|
|
||||||
window,
|
|
||||||
cross_color,
|
|
||||||
(0, 0),
|
|
||||||
(canva_width, window_size[1]),
|
|
||||||
7
|
|
||||||
)
|
|
||||||
pygame.draw.line(
|
|
||||||
window,
|
|
||||||
cross_color,
|
|
||||||
(0, window_size[1]),
|
|
||||||
(canva_width, 0),
|
|
||||||
7
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
font.render_to(window, (10, 10), f"FPS: {clock.get_fps():.2f}", (255, 255, 255), size=16)
|
|
||||||
|
|
||||||
pygame.display.update()
|
|
||||||
clock.tick(fps_limit)
|
|
||||||
#d_time = clock.tick(fps_limit) / 1000
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user