From 7b0707498982e7d884217159b392d6c7d6dc7361 Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Sun, 3 Mar 2024 20:17:42 +0100 Subject: [PATCH] Implement new project creation --- canvas.py | 3 ++- main.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++---- project.py | 1 + 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/canvas.py b/canvas.py index 425c687..7ea87f6 100644 --- a/canvas.py +++ b/canvas.py @@ -54,10 +54,11 @@ class EditorCanvas(tkinter.Canvas): def handle_draw(self,event): - self.current_char_modified=True pixel_pos=self.cursor_pos_to_pixel((event.x,event.y)) if not pixel_pos: return + self.current_char_modified=True + self.project.modified=True self.current_char_pixels[pixel_pos[0]*self.project.char_res[1]+pixel_pos[1]]=1 self.draw() diff --git a/main.py b/main.py index b889ea5..d931aee 100644 --- a/main.py +++ b/main.py @@ -19,6 +19,49 @@ pixel_color = (255,) * 3 project=Project() +def button_create_project_click(subwindow,w,h): + try: + width=int(w) + except ValueError as e: + tkinter.messagebox.showerror("Creating new project", f"Invalid value {w}: {e}") + return + try: + height=int(h) + except ValueError as e: + tkinter.messagebox.showerror("Creating new project", f"Invalid value {h}: {e}") + return + project.chars={} + project.char_res=(width,height) + project.loaded=True + canvas_editor.after_project_load() + canvas_editor.draw() + update_glyph_preview() + subwindow.destroy() + + +def menu_file_new_project_click(): + global window + if project.modified and not tkinter.messagebox.askyesno("Creating new project","You have unsaved changes, are you sure?"): + return + subwindow=tkinter.Toplevel(window) + subwindow.title("New project") + + label_width=tkinter.Label(subwindow,text="Character width") + label_width.pack(side="top") + + entry_width=tkinter.Entry(subwindow,validate="all",validatecommand=((subwindow.register(number_only_validate)),"%P")) + entry_width.pack(side="top",fill="x",padx=5) + + label_height=tkinter.Label(subwindow,text="Character height") + label_height.pack(side="top") + + entry_height=tkinter.Entry(subwindow,validate="all",validatecommand=((subwindow.register(number_only_validate)),"%P")) + entry_height.pack(side="top",fill="x",padx=5) + + button_create=tkinter.Button(subwindow,text="Create",command=lambda: button_create_project_click(subwindow,entry_width.get(),entry_height.get())) + button_create.pack(side="bottom",fill="x",padx=5) + + def menu_file_open_project_click(): global project global canvas_editor @@ -47,21 +90,26 @@ def menu_file_open_project_click(): def menu_file_save_project_click(): + global canvas_editor global project + if not project.loaded: + return + path=None - if project.loaded: + if project.path: path=project.path else: # show file save dialog - path=filedialog.asksaveasfilename( + path=tkinter.filedialog.asksaveasfilename( title="Save font project", filetypes=[("Font project (json)", "*.fontproj")], defaultextension=".fontproj" ) - if path=="" or file_path==(): + if path=="" or path==(): return + canvas_editor.save_char() try: project.save(path) except IOError as e: @@ -107,6 +155,10 @@ def button_glyph_search_click(): def number_only_validate(val): + return val.isdigit() + + +def hex_only_validate(val): for x in val: if not x.isdigit() and (ord(x)ord("f")) and (ord(x)ord("F")): return False @@ -129,6 +181,7 @@ window.geometry(f"{WINDOW_SIZE[0]}x{WINDOW_SIZE[1]}") menubar=tkinter.Menu(window) menu_file=tkinter.Menu(menubar,tearoff=False) +menu_file.add_command(label="New project",command=menu_file_new_project_click) 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) @@ -158,7 +211,7 @@ 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=tkinter.Entry(frame_glyph_id,validate="all",validatecommand=((window.register(hex_only_validate)),"%P")) entry_glyph_id.pack(side="left") button_glyph_search=tkinter.Button(frame_glyph_id,width=10,text="Search",command=button_glyph_search_click) diff --git a/project.py b/project.py index e7cdfa9..5f412c1 100644 --- a/project.py +++ b/project.py @@ -6,6 +6,7 @@ class Project: self.char_res=(0,0) self.path=None self.loaded=False + self.modified=False def load(self,path):