fonteditor/main.py

128 lines
3.4 KiB
Python
Raw Normal View History

2024-02-29 22:12:14 +01:00
import os
import unicodedata
from exporter import export
from canvas import *
from project import *
2024-02-29 22:12:14 +01:00
##### CONFIG #####
2024-03-01 20:37:08 +01:00
WINDOW_SIZE = (
(1280,720),
(1920,1019)
2024-02-29 22:12:14 +01:00
)[0]
cross_color = (255, 60, 25)
pixel_color = (255,) * 3
##################
project=Project()
2024-03-01 20:37:08 +01:00
def menu_file_open_project_click():
global project
global canvas_editor
file_path = tkinter.filedialog.askopenfilename(
title="Open font project",
filetypes=[("Font project (json)", "*.fontproj")]
)
2024-03-03 17:32:27 +01:00
if file_path == "" or file_path==():
return
# save last path to cache
with open("last_path_cache.txt", "w") as f:
f.write(file_path)
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()
2024-02-29 22:12:14 +01:00
2024-03-01 20:37:08 +01:00
def menu_file_save_project_click():
2024-03-02 22:35:58 +01:00
global project
path=None
if project.loaded:
path=project.path
else:
# show file save dialog
path=filedialog.asksaveasfilename(
title="Save font project",
filetypes=[("Font project (json)", "*.fontproj")],
defaultextension=".fontproj"
)
2024-03-03 17:32:27 +01:00
if path=="" or file_path==():
2024-03-02 22:35:58 +01:00
return
try:
project.save(path)
except IOError as e:
tkinter.messagebox.showerror("Saving project",f"Failed to save project '{path}': {e}")
2024-02-29 22:12:14 +01:00
2024-03-01 20:37:08 +01:00
def menu_file_save_project_as_click():
pass
2024-02-29 22:12:14 +01:00
2024-03-01 21:52:39 +01:00
def button_prev_glyph_click():
canvas_editor.prev_glyph()
2024-03-01 21:52:39 +01:00
def button_next_glyph_click():
canvas_editor.next_glyph()
2024-03-01 21:52:39 +01:00
2024-03-01 20:37:08 +01:00
def number_only_validate(val):
return str.isdigit(val) or val==""
2024-02-29 22:12:14 +01:00
2024-03-01 21:52:39 +01:00
2024-03-01 20:37:08 +01:00
window=tkinter.Tk()
window.title("fonteditor")
window.geometry(f"{WINDOW_SIZE[0]}x{WINDOW_SIZE[1]}")
2024-02-29 22:12:14 +01:00
2024-03-01 20:37:08 +01:00
menubar=tkinter.Menu(window)
menu_file=tkinter.Menu(menubar,tearoff=False)
menu_file.add_command(label="Open project",command=menu_file_open_project_click)
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)
2024-02-29 22:12:14 +01:00
canvas_editor=EditorCanvas(project,window,bg="black")
2024-03-03 17:12:32 +01:00
canvas_editor.pack(side="left",fill="both",expand=True)
2024-02-29 22:12:14 +01:00
2024-03-01 20:37:08 +01:00
frame_controls=tkinter.Frame(window)
2024-03-03 17:12:32 +01:00
frame_controls.pack(side="right")
2024-02-29 22:12:14 +01:00
2024-03-01 20:37:08 +01:00
canvas_preview=tkinter.Canvas(frame_controls,width=100,height=200,bg="black")
canvas_preview.pack(side="top")
2024-02-29 22:12:14 +01:00
2024-03-01 20:37:08 +01:00
label_glyph_name=tkinter.Label(frame_controls,text="Placeholder")
label_glyph_name.pack(side="top")
2024-02-29 22:12:14 +01:00
2024-03-01 20:37:08 +01:00
frame_nav=tkinter.Frame(frame_controls)
frame_nav.pack(side="top",pady=10)
2024-02-29 22:12:14 +01:00
2024-03-01 21:52:39 +01:00
button_prev_glyph=tkinter.Button(frame_nav,width=10,text="Previous",command=button_prev_glyph_click)
2024-03-01 20:37:08 +01:00
button_prev_glyph.pack(side="left")
2024-02-29 22:12:14 +01:00
2024-03-01 21:52:39 +01:00
button_next_glyph=tkinter.Button(frame_nav,width=10,text="Next",command=button_next_glyph_click)
2024-03-01 20:37:08 +01:00
button_next_glyph.pack(side="left")
frame_glyph_id=tkinter.Frame(frame_controls)
frame_glyph_id.pack(side="top",pady=10)
entry_glyph_id=tkinter.Entry(frame_glyph_id,validate="all",validatecommand=((window.register(number_only_validate)),"%P"))
entry_glyph_id.pack(side="left")
button_glyph_search=tkinter.Button(frame_glyph_id,width=10,text="Search")
button_glyph_search.pack(side="left")
window.config(menu=menubar)
window.mainloop()