-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandinterpretter.py
178 lines (142 loc) · 6.24 KB
/
commandinterpretter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import tkinter as tk
import numpy as np
import config
import math
import time
from vector import Vector
from point import Point
from polyline import Polyline
from polygon import Polygon
from lineart import LinearT
from affinet import AffineT
from utilities import *
class CommandInterpretter:
"""Runs commands, keeps track of variables, allows manipulation of variables, etc."""
def __init__(self, canvas: tk.Canvas):
"""'canvas' is the canvas that will be drawn on."""
self.math_objects = []
self._canvas = canvas
config.command_interpretter = self
self._globals = {}
self._grid_size = 30 # equivalent to scale (zoom), in pixels
self._grid_offset = (
self._grid_size * 10
) # IMPORTANT: must be a multiple of grid_size (maybe can remove this constraint later)
self._initial_transform = (
AffineT.identity()
@ AffineT.translation(self._grid_offset, self._grid_offset)
@ AffineT.scaling(self._grid_size, self._grid_size)
)
self._fps = 30
self._show_grid = True
self._canvas.bind("<Configure>", lambda e: self.draw_grid())
self._canvas.bind("<ButtonPress-3>", self.scroll_start)
self._canvas.bind("<ButtonPress-2>", self.scroll_start)
self._canvas.bind("<B3-Motion>", self.scroll_move)
self._canvas.bind("<B2-Motion>", self.scroll_move)
self._canvas.bind("<MouseWheel>", self._on_mousewheel)
self._time_last = time.time()
self._canvas.after(int(1000/self._fps),self._on_update)
@property
def show_grid(self):
return self._show_grid
@show_grid.setter
def show_grid(self, value):
self._show_grid = value
if value:
self.draw_grid()
else:
self.clear_grid()
@property
def grid_size(self):
return self._grid_size
@grid_size.setter
def grid_size(self, value):
self._grid_size = value
self._grid_offset = self._grid_size * 10
self._initial_transform = (
AffineT.identity()
@ AffineT.translation(self._grid_offset, self._grid_offset)
@ AffineT.scaling(self._grid_size, self._grid_size)
)
self.redraw()
@property
def transform(self):
"""Returns an affine matrix that transforms from canvas space to grid space."""
return self._initial_transform
def _on_mousewheel(self, event):
factor = 1.5
self.grid_size += factor * (event.delta / 120)
def _on_update(self):
"""Executed roughly every 1/fps seconds."""
time_now = time.time()
try:
if "on_update" in self._globals:
self._globals["on_update"](time_now - self._time_last)
except Exception as e:
print("Error in on_update():",e)
finally:
self._time_last = time_now
self._canvas.after(int(1000/self._fps),self._on_update)
@property
def initial_transform(self):
return self._initial_transform
def scroll_start(self, event):
self._canvas.scan_mark(event.x, event.y)
def scroll_move(self, event):
self._canvas.scan_dragto(event.x, event.y, gain=1)
self.draw_grid()
def clear_grid(self):
self._canvas.delete("grid")
def redraw(self):
# redraw grid
self.draw_grid()
# redraw mathobjects
for obj in self.math_objects:
obj.redraw()
def draw_grid(self):
if not self._show_grid:
return
self.clear_grid()
# get area that camera is viewing (only draw grid in this area)
tl, tr, br, bl = self.camera_rect()
# round to nearest grid_size
tl = (round(tl[0] / self._grid_size) * self._grid_size, round(tl[1] / self._grid_size) * self._grid_size)
tr = (round(tr[0] / self._grid_size) * self._grid_size, round(tr[1] / self._grid_size) * self._grid_size)
br = (round(br[0] / self._grid_size) * self._grid_size, round(br[1] / self._grid_size) * self._grid_size)
bl = (round(bl[0] / self._grid_size) * self._grid_size, round(bl[1] / self._grid_size) * self._grid_size)
# draw grid
for i in np.arange(tl[0], tr[0], self._grid_size):
self._canvas.create_line(i, tl[1], i, bl[1], fill="#ddd", tags="grid")
for i in np.arange(tl[1], bl[1], self._grid_size):
self._canvas.create_line(tl[0], i, tr[0], i, fill="#ddd", tags="grid")
# draw origin
self._canvas.create_line(self._grid_offset, tl[1], self._grid_offset, bl[1], fill="#666", tags="grid")
self._canvas.create_line(tl[0], self._grid_offset, tr[0], self._grid_offset, fill="#666", tags="grid")
self._canvas.tag_lower("grid")
def execute_script(self, text):
"""Runs the given text as Python code. Clears the variables first."""
config.command_interpretter = self
import utilities
utilities.options._command_interpretter = self
self.math_objects.clear()
self._globals.clear()
self._canvas.delete(tk.ALL)
self.draw_grid()
self.execute_commands_immediate(text)
def execute_commands_immediate(self, text):
"""Runs the given text as Python code. Does not clear the variables first."""
exec("from commandinterpretter import *", self._globals)
exec(text, self._globals)
def camera_rect(self):
"""Returns a rectangle representing area of the canvas that the camera (window) is currently viewing.
Returned value is (top_left, top_right, bottom_right, bottom_left), where each point is (x,y) tuple.
"""
window_width = self._canvas.winfo_width()
window_height = self._canvas.winfo_height()
return (
(self._canvas.canvasx(0), self._canvas.canvasy(0)),
(self._canvas.canvasx(window_width), self._canvas.canvasy(0)),
(self._canvas.canvasx(window_width), self._canvas.canvasy(window_height)),
(self._canvas.canvasx(0), self._canvas.canvasy(window_height)),
)