Implement project opening and drawing editor canvas

This commit is contained in:
mrkubax10 2024-03-01 21:40:25 +01:00
parent 83d0058218
commit 1870471445
2 changed files with 75 additions and 58 deletions

View File

@ -119,22 +119,7 @@ def cli_main():
# open existing font project
elif choice == 1:
file_path = filedialog.askopenfilename(
title="Open font project",
filetypes=[("Font project (json)", "*.fontproj")]
)
if file_path == "":
print("\nCanceled.\n")
continue
# save last path to cache
with open("last_path_cache.txt", "w") as f:
f.write(file_path)
with open(file_path, "r") as f:
project_data = json.load(f)
pass
# open last project
elif choice == 2:
@ -173,4 +158,4 @@ def cli_main():
key_tips()
return project_data, file_path
return project_data, file_path

114
main.py
View File

@ -9,24 +9,86 @@ from exporter import export
##### CONFIG #####
# height of guide lines from the top of font pixel grid
ascender_line = 10
descender_line = 20
WINDOW_SIZE = (
"1280x720",
"1920x1019"
(1280,720),
(1920,1019)
)[0]
grid_color = (128,) * 3
grid_guide_color = (128, 128, 255)
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")
def menu_file_open_project_click():
pass
global project_data
global project_chars
global project_char_res
global last_project
global canvas_editor
global canvas_width
global canvas_height
file_path = tkinter.filedialog.askopenfilename(
title="Open font project",
filetypes=[("Font project (json)", "*.fontproj")]
)
if file_path == "":
return
# 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()
def menu_file_save_project_click():
pass
@ -39,7 +101,7 @@ def number_only_validate(val):
window=tkinter.Tk()
window.title("fonteditor")
window.geometry(WINDOW_SIZE)
window.geometry(f"{WINDOW_SIZE[0]}x{WINDOW_SIZE[1]}")
menubar=tkinter.Menu(window)
menu_file=tkinter.Menu(menubar,tearoff=False)
@ -49,7 +111,7 @@ menu_file.add_command(label="Save project as",command=menu_file_save_project_as_
menubar.add_cascade(label="File",menu=menu_file)
canvas_editor=tkinter.Canvas(window,bg="black")
canvas_editor.pack(side="left",fill="y")
canvas_editor.pack(side="left")
frame_controls=tkinter.Frame(window)
frame_controls.pack()
@ -82,8 +144,6 @@ window.config(menu=menubar)
window.mainloop()
"""proj_data, proj_path = cli_ui.cli_main()
chars = proj_data["chars"]
char_res = (proj_data["char_width"], proj_data["char_height"])
pixel_size = (window_size[1]-1) / char_res[1]
canva_width = pixel_size * char_res[0]"""
@ -101,11 +161,6 @@ def cursor_pos_to_pixel(pos):
is_grid = True
char_num = 33
def check_char_exist(char_num):
return chr(char_num) in chars
does_char_exist = check_char_exist(char_num)
force_deleted = False
@ -231,29 +286,6 @@ while True:
)
)
if is_grid:
# draw grid
for x in range(char_res[0] + 1):
x = x * pixel_size
pygame.draw.line(
window,
grid_color,
(x, 0),
(x, window_size[1]),
)
for y in range(char_res[1] + 1):
real_y = y * pixel_size
pygame.draw.line(
window,
grid_guide_color if (y == ascender_line or y == descender_line) else grid_color,
(0, real_y),
(canva_width, real_y),
)
# draw char num
font.render_to(
window,