added scrolling and zoom in simulator
This commit is contained in:
parent
97aead6f86
commit
14a099c3e4
|
@ -1,16 +1,14 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
from tkinter import Tk, Canvas, Frame, BOTH, EventType, ALL
|
||||
import tkinter as tk
|
||||
import argparse
|
||||
|
||||
gcode_file_path = " "
|
||||
bedsizex = 0
|
||||
bedsizey = 0
|
||||
scale_factor = 1.0
|
||||
x_offset = 24.0
|
||||
y_offset = 24.0
|
||||
x_offset = 48.0
|
||||
y_offset = 48.0
|
||||
laser_on = False
|
||||
last_position = {"x": x_offset, "y": 720-y_offset} #set to initial start at home pos
|
||||
last_position = {"x": 0.0, "y": 0.0}
|
||||
global canvas
|
||||
debug = True
|
||||
|
||||
|
@ -40,7 +38,7 @@ def plot_paths(canvas):
|
|||
laser_on = True
|
||||
if(line.startswith('G1')):
|
||||
x = float(line[(int(line.find('X'))+1):(int(line.find('Y')-1))]) * (scale_factor*1) + x_offset
|
||||
y = (720-y_offset) - (float(line[(int(line.find('Y'))+1):(int(line.find(';')-1))])* (scale_factor*1))
|
||||
y = (bedsizey * scale_factor-y_offset) - (float(line[(int(line.find('Y'))+1):(int(line.find(';')-1))])* (scale_factor*1))
|
||||
|
||||
|
||||
if(debug): print("[decoder] movement to " + str(x) + " " + str(y))
|
||||
|
@ -51,44 +49,75 @@ def plot_paths(canvas):
|
|||
last_position["x"] = x
|
||||
last_position["y"] = y
|
||||
|
||||
|
||||
|
||||
class SimulatorView(Frame):
|
||||
def __init__(self):
|
||||
global canvas
|
||||
super().__init__()
|
||||
canvas = Canvas(self)
|
||||
#canvas.bind("<B1-Motion>", lambda event: canvas.scan_dragto(event.x, event.y, gain=1))
|
||||
self.initUI()
|
||||
|
||||
def initUI(self):
|
||||
class Example(tk.Frame):
|
||||
def __init__(self, root):
|
||||
tk.Frame.__init__(self, root)
|
||||
self.canvas = tk.Canvas(self, width=400, height=400, background="bisque")
|
||||
self.xsb = tk.Scrollbar(self, orient="horizontal", command=self.canvas.xview)
|
||||
self.ysb = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
|
||||
self.canvas.configure(yscrollcommand=self.ysb.set, xscrollcommand=self.xsb.set)
|
||||
self.canvas.configure(scrollregion=(0,0,1000,1000))
|
||||
self.master.title("gcode simulator")
|
||||
self.pack(fill=BOTH, expand=1)
|
||||
canvas.create_line(x_offset, y_offset, x_offset, (720-y_offset), width=2, fill='black')
|
||||
canvas.create_line(1280-x_offset, y_offset, 1280-x_offset, (720-y_offset), width=2, fill='black')
|
||||
canvas.create_line(x_offset, y_offset, 1280-x_offset, y_offset, width=2, fill='black')
|
||||
canvas.create_line(x_offset, (720-y_offset), 1280-x_offset, (720-y_offset), width=2, fill='black')
|
||||
plot_paths(canvas)
|
||||
canvas.pack(fill=BOTH, expand=1)
|
||||
self.xsb.grid(row=1, column=0, sticky="ew")
|
||||
self.ysb.grid(row=0, column=1, sticky="ns")
|
||||
self.canvas.grid(row=0, column=0, sticky="nsew")
|
||||
self.grid_rowconfigure(0, weight=1)
|
||||
self.grid_columnconfigure(0, weight=1)
|
||||
|
||||
actual_bedsize_x = bedsizex * scale_factor
|
||||
actual_bedsize_y = bedsizey * scale_factor
|
||||
|
||||
def main():
|
||||
self.canvas.create_line(x_offset, y_offset, x_offset, (actual_bedsize_y-y_offset), width=2, fill='black')
|
||||
self.canvas.create_line(actual_bedsize_x-x_offset, y_offset, actual_bedsize_x-x_offset, (actual_bedsize_y-y_offset), width=2, fill='black')
|
||||
self.canvas.create_line(x_offset, y_offset, actual_bedsize_x-x_offset, y_offset, width=2, fill='black')
|
||||
self.canvas.create_line(x_offset, (actual_bedsize_y-y_offset), actual_bedsize_x-x_offset, (actual_bedsize_y-y_offset), width=2, fill='black')
|
||||
|
||||
plot_paths(self.canvas)
|
||||
|
||||
# This is what enables using the mouse:
|
||||
self.canvas.bind("<ButtonPress-1>", self.move_start)
|
||||
self.canvas.bind("<B1-Motion>", self.move_move)
|
||||
#linux scroll
|
||||
self.canvas.bind("<Button-4>", self.zoomerP)
|
||||
self.canvas.bind("<Button-5>", self.zoomerM)
|
||||
#windows scroll
|
||||
self.canvas.bind("<MouseWheel>",self.zoomer)
|
||||
|
||||
#move
|
||||
def move_start(self, event):
|
||||
self.canvas.scan_mark(event.x, event.y)
|
||||
def move_move(self, event):
|
||||
self.canvas.scan_dragto(event.x, event.y, gain=1)
|
||||
|
||||
#windows zoom
|
||||
def zoomer(self,event):
|
||||
if (event.delta > 0):
|
||||
self.canvas.scale("all", event.x, event.y, 1.1, 1.1)
|
||||
elif (event.delta < 0):
|
||||
self.canvas.scale("all", event.x, event.y, 0.9, 0.9)
|
||||
self.canvas.configure(scrollregion = self.canvas.bbox("all"))
|
||||
|
||||
#linux zoom
|
||||
def zoomerP(self,event):
|
||||
self.canvas.scale("all", event.x, event.y, 1.1, 1.1)
|
||||
self.canvas.configure(scrollregion = self.canvas.bbox("all"))
|
||||
def zoomerM(self,event):
|
||||
self.canvas.scale("all", event.x, event.y, 0.9, 0.9)
|
||||
self.canvas.configure(scrollregion = self.canvas.bbox("all"))
|
||||
|
||||
if __name__ == "__main__":
|
||||
parse_arguments()
|
||||
|
||||
if(bedsizex < bedsizey):
|
||||
scale_factor = float(720/bedsizey)
|
||||
scale_factor = float(720/bedsizey)*0.8
|
||||
else:
|
||||
scale_factor = float(1280/bedsizex)
|
||||
scale_factor = float(1280/bedsizex)*0.8
|
||||
|
||||
print("scale factor: " + str(scale_factor))
|
||||
last_position = {"x": x_offset, "y": bedsizey * scale_factor-y_offset} #set to initial start at home pos
|
||||
|
||||
#plot_paths(0)
|
||||
|
||||
root = Tk()
|
||||
ex = SimulatorView()
|
||||
root.geometry(str(1280+int(x_offset))+"x"+str(720+int(y_offset))+"+300+300")
|
||||
root.mainloop()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
root = tk.Tk()
|
||||
Example(root).pack(fill="both", expand=True)
|
||||
root.geometry(str(int(bedsizex*scale_factor+x_offset))+"x"+str(int(bedsizey*scale_factor+y_offset))+"+300+300")
|
||||
root.mainloop()
|
Loading…
Reference in New Issue