Split editor canvas and project data to separate files

This commit is contained in:
2024-03-02 12:28:14 +01:00
parent aba843ef1e
commit b215b8ad39
3 changed files with 129 additions and 87 deletions

104
main.py
View File

@@ -2,11 +2,12 @@ import os
import numpy as np
import cli_ui
import unicodedata
import json
import base64
import tkinter
from exporter import export
from canvas import *
from project import *
##### CONFIG #####
WINDOW_SIZE = (
@@ -14,57 +15,16 @@ WINDOW_SIZE = (
(1920,1019)
)[0]
GRID_COLOR = "#808080"
GRID_GUIDE_COLOR = (128, 128, 255)
GRID_SIZE = 32
cross_color = (255, 60, 25)
pixel_color = (255,) * 3
##################
project_data={}
project_chars = {}
project_char_res = None
current_project=""
current_char=33
canvas_width=0
canvas_height=0
def check_char_exist(char_num):
global project_chars
return chr(char_num) in project_chars
def draw_editor_canvas():
global canvas_editor
global project_char_res
global canvas_width
global canvas_height
canvas_editor.delete("all")
# draw grid
for x in range(project_char_res[0] + 1):
x = x * GRID_SIZE
canvas_editor.create_line((x,0),(x,canvas_height),width=1,fill=GRID_COLOR)
for y in range(project_char_res[1] + 1):
y = y * GRID_SIZE
canvas_editor.create_line((0,y),(canvas_width,y),width=1,fill=GRID_COLOR)
if check_char_exist(current_char):
pass
else:
canvas_editor.create_line((0,0),(canvas_width,canvas_height),width=3,fill="red")
canvas_editor.create_line((canvas_width,0),(0,canvas_height),width=3,fill="red")
project=Project()
def menu_file_open_project_click():
global project_data
global project_chars
global project_char_res
global last_project
global project
global canvas_editor
global canvas_width
global canvas_height
file_path = tkinter.filedialog.askopenfilename(
title="Open font project",
@@ -76,19 +36,16 @@ def menu_file_open_project_click():
# save last path to cache
with open("last_path_cache.txt", "w") as f:
f.write(file_path)
current_project=file_path
with open(file_path, "r") as f:
project_data = json.load(f)
project_chars = project_data["chars"]
project_char_res = (project_data["char_width"], project_data["char_height"])
canvas_width=project_char_res[0]*GRID_SIZE
canvas_height=project_char_res[1]*GRID_SIZE
canvas_editor.config(width=canvas_width,height=canvas_height)
draw_editor_canvas()
try:
project.load(file_path)
except AttributeError as e:
tkinter.messagebox.showerror("Opening project",f"Project '{file_path}' is invalid: {e}")
except IOError as e:
tkinter.messagebox.showerror("Opening project",f"Failed to open project '{file_path}': {e}")
finally:
canvas_editor.after_project_load()
canvas_editor.draw()
def menu_file_save_project_click():
@@ -98,25 +55,11 @@ def menu_file_save_project_as_click():
pass
def button_prev_glyph_click():
global current_char
current_char-=1
keep_char_num_in_bounds()
draw_editor_canvas()
canvas_editor.prev_glyph()
def button_next_glyph_click():
global current_char
current_char+=1
keep_char_num_in_bounds()
draw_editor_canvas()
def keep_char_num_in_bounds():
global current_char
if current_char<0:
current_char=0
elif current_char>99999:
current_char=99999
canvas_editor.next_glyph()
def number_only_validate(val):
@@ -134,7 +77,7 @@ menu_file.add_command(label="Save project",command=menu_file_save_project_click)
menu_file.add_command(label="Save project as",command=menu_file_save_project_as_click)
menubar.add_cascade(label="File",menu=menu_file)
canvas_editor=tkinter.Canvas(window,bg="black")
canvas_editor=EditorCanvas(project,window,bg="black")
canvas_editor.pack(side="left")
frame_controls=tkinter.Frame(window)
@@ -167,19 +110,6 @@ button_glyph_search.pack(side="left")
window.config(menu=menubar)
window.mainloop()
"""proj_data, proj_path = cli_ui.cli_main()
pixel_size = (window_size[1]-1) / char_res[1]
canva_width = pixel_size * char_res[0]"""
def cursor_pos_to_pixel(pos):
pixel_pos = (int(pos[0] // pixel_size), int(pos[1] // pixel_size))
if pixel_pos[0] < 0 or pixel_pos[1] < 0 or pixel_pos[0] >= char_res[0] or pixel_pos[1] >= char_res[1]:
return None
return pixel_pos
# main loop
while True:
for event in pygame.event.get():