-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwindow_functions.py
147 lines (128 loc) · 5.13 KB
/
window_functions.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
from math import dist, ceil
from tkinter import NORMAL, DISABLED
from tkinter.ttk import Progressbar
from config import *
from draw_erase import change_shape, handle_body_properties
from global_items import handle, window_commands, bodies, evolution_status, window, checkbuttons_for_triangle
from tips import info_handle, mouse_clicked_on_body, prepare_info_handle
import global_items
def change_in_dict(button: object):
'''Changing the corresponding value in the dictionary.'''
key = button.dict_key
value = button.variable.get()
window_commands[key] = value
if not value:
return
match key:
case 'dont-eat-plants':
window_commands['smart-plant'] = False
case 'smart-plant':
window_commands['dont-eat-plants'] = False
case 'dont-eat-bodies':
window_commands['smart-body'] = False
case 'smart-body':
window_commands['dont-eat-bodies'] = False
for checkbutton in checkbuttons_for_triangle:
checkbutton.variable.set(window_commands[checkbutton.dict_key])
@handle
def start_pause_button_change_state(action: str):
global_items.start_pause_button.configure(state=NORMAL if action == ENABLE else DISABLED)
window.update()
@handle
def pause_mode(enable: bool):
if enable:
window_commands['run/pause'] = PAUSE
global_items.start_pause_button.configure(text=START_TEXT)
else:
window_commands['run/pause'] = RUN
global_items.start_pause_button.configure(text=PAUSE_TEXT)
window.update()
def mouse_bind():
global bind
bind = global_items.canvas.bind('<Button-1>', mouse_clicked)
def mouse_unbind():
global_items.canvas.unbind('<Button-1>', bind)
replace_shapes = {
CIRCLE: TRIANGLE,
TRIANGLE: CIRCLE,
RHOMBUS: SQUARE,
SQUARE: RHOMBUS
}
def mouse_clicked(event):
'''Maintaining the features that work with the mouse inside the evolution field.'''
for body in bodies:
if dist((event.x, event.y), (body.x+CANVAS_BORDER, body.y+CANVAS_BORDER)) <= HALF_BODY_SIZE*1.2:
change_shape(body, replace_shapes[body.shape])
mouse_clicked_on_body(body)
checkbuttons_state(NORMAL if body.shape == TRIANGLE else DISABLED)
if evolution_status.selected_body is None:
evolution_status.selected_body = body
else:
if body is evolution_status.selected_body:
evolution_status.selected_body = None
else:
change_shape(evolution_status.selected_body, replace_shapes[evolution_status.selected_body.shape])
evolution_status.selected_body = body
return
def user_select_body():
'''Maintaining the process of the user selecting the body.'''
@handle
def selected() -> bool: # A separate function is required for the decorator to work along with it
handle_body_properties()
info_handle()
return window_commands['run/pause'] == RUN
prepare_info_handle()
while not selected():
continue
@handle
def checkbuttons_state(action: str):
'''Changing states of buttons which provide the access to the behaviour of the species which is selected by the user.'''
for checkbutton in checkbuttons_for_triangle:
checkbutton.configure(state=action)
@handle
def disable_checkbuttons_checkmarks():
'''Removing the checkmarks of buttons which provide the access to the behaviour of the species which is selected by the user.'''
for checkbutton in checkbuttons_for_triangle:
checkbutton.variable.set(False)
@handle
def restore_checkbuttons_checkmarks():
'''Restoring the current states of checkmarks.'''
for checkbutton in checkbuttons_for_triangle:
checkbutton.variable.set(window_commands[checkbutton.dict_key])
@handle
def set_checkbuttons_for_evolution():
'''
Deactivating all of the checkbuttons which provide the access to the behaviour of the species which is selected by the user and removing the checkmark of these checkbuttons
if it's needed.
'''
if evolution_status.selected_body is None or evolution_status.selected_body.shape != TRIANGLE:
disable_checkbuttons_checkmarks()
checkbuttons_state(DISABLED)
def show_progress_bar():
global progress_bar, canvas_window, previous_value
progress_bar = Progressbar(
master=global_items.canvas,
length=300,
mode='determinate',
maximum=100
)
canvas_window = global_items.canvas.create_window(
HALF_EVOLUTION_FIELD_SIZE['width'],
HALF_EVOLUTION_FIELD_SIZE['height'],
window=progress_bar
)
previous_value = 0
def update_progress(to_show: float):
'''Progressing the progress of the progess bar.'''
global previous_value
new_value = ceil(to_show*100)
if new_value > previous_value:
progress_bar['value'] = new_value
previous_value = new_value
def delete_progress_bar():
global canvas_window
try:
global_items.canvas.delete(canvas_window)
global_items.canvas.update()
except NameError: # NameError is thrown if the progress bar does not exist yet
pass