Skip to content

Commit

Permalink
Start on GUI, auto-expand canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Feb 6, 2024
1 parent 224e5a2 commit 7d18fae
Showing 1 changed file with 74 additions and 17 deletions.
91 changes: 74 additions & 17 deletions Tools/ged/ged.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from dataclasses import dataclass, field
import tkinter as tk
from tkinter.font import Font
from tkinter import ttk

MIN_ELEMENT_WIDTH = MIN_ELEMENT_HEIGHT = 2
GRID_ALIGNMENT = 8
Expand Down Expand Up @@ -254,8 +255,8 @@ def __init__(self, tk_root, width=320, height=240, scale=1):
self.width = width
self.height = height
self.scale = scale
c = self.canvas = tk.Canvas(tk_root, background='black')
c.pack(side=tk.TOP)
c = self.canvas = tk.Canvas(tk_root, background='gray')
c.pack(side=tk.BOTTOM, expand=True, fill=tk.BOTH)
c.bind('<1>', self.canvas_select)
c.bind('<Motion>', self.canvas_move)
c.bind('<B1-Motion>', self.canvas_drag)
Expand All @@ -267,17 +268,24 @@ def __init__(self, tk_root, width=320, height=240, scale=1):
self.state = State.IDLE

def set_size(self, width, height):
if width == self.width and height == self.height:
return
self.width = width
self.height = height
self.size_changed()

def size_changed(self):
self.canvas.configure(width=self.width*self.scale, height=self.height*self.scale)

def set_scale(self, scale):
if scale == self.scale:
return
self.scale = scale
self.size_changed()

def size_changed(self):
# self.canvas.configure(width=self.width * self.scale, height=self.height * self.scale)
self.redraw()
if self.sel_items:
self.sel_bounds = self.draw_handles()

def tk_bounds(self, rect):
return (
rect.x * self.scale,
Expand Down Expand Up @@ -320,7 +328,7 @@ def remove_item(self, item):

def get_current(self):
tags = self.canvas.gettags(tk.CURRENT)
if not tags:
if len(tags) < 2:
return None, None
elem = Element(int(tags[1]))
if tags[0] == 'handle':
Expand Down Expand Up @@ -456,33 +464,82 @@ def canvas_end_move(self, evt):
if self.state != State.DRAGGING:
return
self.state = State.IDLE
self.remove_handles()
self.redraw() # Fix Z-ordering and ensure consistency
self.sel_bounds = self.draw_handles()


def redraw(self):
self.canvas.delete(tk.ALL)
r = self.tk_bounds(Rect(0, 0, self.width, self.height))
self.canvas.create_rectangle((r), fill='black', outline='')
for item in self.display_list:
item.draw(self)


def run():
root = tk.Tk(className='GED')
root.title('Graphical Layout Editor')
def btn_click():
handler = Handler(root)

# Menus
def fileNew():
pass
# self.reset()
# self.reload()
# self.editDevice(self.config.devices[0])

def fileOpen():
pass
# filename = filedialog.askopenfilename(
# title='Select profile ' + HW_EXT + ' file',
# filetypes=hwFilter,
# initialdir=os.getcwd())
# if len(filename) != 0 and checkProfilePath(filename):
# self.loadConfig(filename)

def fileSave():
pass
# filename = self.json['name']
# filename = filedialog.asksaveasfilename(
# title='Save profile to file',
# filetypes=hwFilter,
# initialfile=filename,
# initialdir=os.getcwd())
# if len(filename) != 0 and checkProfilePath(filename):
# ext = os.path.splitext(filename)[1]
# if ext != HW_EXT:
# filename += HW_EXT
# json_save(self.json, filename)

def fileList():
for item in handler.display_list:
print(repr(item))
btn = tk.Button(root, text='Hello', command=btn_click)
btn.pack(side=tk.TOP)
handler = Handler(root)
handler.add_random_shapes(20)

sel_item = None
sel_elem = None
cap_item = None
mouse_pos = None
sel_pos = None
# Toolbar
toolbar = ttk.Frame(root)
toolbar.pack(side=tk.TOP, fill=tk.X)
col = 0
def addButton(text, command):
btn = ttk.Button(toolbar, text=text, command=command)
nonlocal col
btn.grid(row=0, column=col)
col += 1
addButton('New', fileNew)
addButton('Open...', fileOpen)
addButton('Save...', fileSave)
sep = ttk.Separator(toolbar, orient=tk.VERTICAL)
sep.grid(row=0, column=col, sticky=tk.NS)
col += 1
addButton('List', fileList)
def changeScale():
scale = 1 + handler.scale % 4
handler.set_scale(scale)
addButton('scale', changeScale)
# addButton('Edit Config', self.editConfig)
# addButton('Add Device', self.addDevice)

handler.add_random_shapes(20)
handler.set_scale(2)

tk.mainloop()

Expand Down

0 comments on commit 7d18fae

Please sign in to comment.