-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.py
More file actions
68 lines (57 loc) · 2.3 KB
/
Controller.py
File metadata and controls
68 lines (57 loc) · 2.3 KB
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
class Controller:
def __init__(self, model, view):
self.model = model
self.view = view
self._assign_callbacks()
self.model.set_formatted(False)
self.view["parent"].focus_set()
self._draw()
def _assign_callbacks(self):
commands = {"Return": self.execute,
"w":lambda: self.execute("B"),
"e":lambda: self.execute("D"),
"r":lambda: self.execute("R"),
"s":lambda: self.execute("B'"),
"d":lambda: self.execute("D'"),
"f":lambda: self.execute("R'"),
"Tab":self.toggle_keyboard_mode}
self.view["parent"].bind("<Key>", lambda e: commands[e.keysym]() if e.keysym in commands else print(e))
self.view["reset_button"]["command"] = self.reset
self.view["execute_button"]["command"] = self.execute
self.view["undo_button"]["command"] = self.undo
self.view["reload_button"]["command"] = self.reload
def toggle_keyboard_mode(self):
if str(self.view["canvas"].focus_get()) == ".!entry":
self.view["canvas"].focus()
self.view["statusbar"]["text"] = "keyboard mode on, click back on entry to turn off"
def reset(self):
print(self.model.reset())
self._draw()
def execute(self, value = None):
keyboard_mode = str(self.view["canvas"].focus_get()) != ".!entry"
print(str(self.view["canvas"].focus_get()), keyboard_mode, value)
if not keyboard_mode and value != None:
return
try:
instructions = self.view["data"].get()
print(self.model.execute(value if keyboard_mode else instructions))
print(self.model.get_recent_operations())
self.view["statusbar"]["text"] = "executed {:s}".format(value if keyboard_mode else instructions)
except:
self.view["statusbar"]["text"] = "failed to execute instructions!"
self._draw()
def undo(self):
print(self.model.undo())
print(self.model.get_recent_operations())
self._draw()
self.view["statusbar"]["text"] = "reverted to previous state"
def _draw(self):
self.view["canvas"].render(self.model["F"],top=True)
self.view["canvas"].render(self.model["B"],top=False)
def reload(self):
if self.model.load_file("algorithms.txt"):
self.view["statusbar"]["text"] = "algorithms.txt has been loaded"
else:
self.view["statusbar"]["text"] = "File not found or error in algorithms.txt file"
print(self.model)
self._draw()