This repository has been archived by the owner on Jun 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gui.py
211 lines (182 loc) · 8.65 KB
/
gui.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import threading
import uuid
from bot import *
class Handler(logging.StreamHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def emit(self, record):
colors = {
logging.DEBUG: 'grey10',
logging.INFO: 'white'
}
if isinstance(record.msg, str) and record.msg in global_settings.messages:
record.msg = global_settings.messages[record.msg]
Sg.cprint(self.format(record), text_color=colors.get(record.levelno, 'red'))
class GUI:
def __init__(self):
self.queue = queue.Queue()
self.bot_thread = None
self.downloading_new_version = False
self.last_window_check = time()
self.window = self.create_window()
display_changelog()
handler = Handler()
handler.setFormatter(MyFormatter())
global_settings.gui_handler = handler
global_settings.set_debug_state()
logger.addHandler(handler)
Config.load() # Display old config warning on bot launch and not on settings opened
@staticmethod
def create_window():
Sg.theme('DarkGrey10')
layout = [
[Sg.Text(' ', size=(1, 1), key='bannability_warning1', font=(global_settings.font, 30), text_color='red')],
[Sg.Text(' ', size=(1, 1), key='bannability_warning2', font=(global_settings.font, 30), text_color='red')],
[Sg.Text(' ', size=(1, 1), key='bannability_warning3', font=(global_settings.font, 30), text_color='red')],
[Sg.Text(' ', size=(1, 1), key='title', font=(global_settings.font, 20))],
[Sg.Text(' ', size=(1, 1), key='version', font=(global_settings.font, 13))],
[Sg.Text(' ', size=(1, 1), key='press_start', font=(global_settings.font, 14), metadata=0)],
[Sg.Multiline(size=(90, 10), key='log', disabled=True, autoscroll=True,
write_only=True, reroute_cprint=True, reroute_stderr=global_settings.compiled, font=(global_settings.font, 12), text_color='red')]
]
buttons = [
[
Sg.Button('', key='toggle', font=(global_settings.font, 12), metadata=0),
Sg.Button('', key='instructions', font=(global_settings.font, 12)),
Sg.Button('', key='settings', font=(global_settings.font, 12)),
Sg.Button('', key='exit', font=(global_settings.font, 12)),
],
[
Sg.Button('', key='delayed_stop', font=(global_settings.font, 12), metadata=0, visible=False),
Sg.Button('', key='take_screenshot', font=(global_settings.font, 12)),
],
]
if not global_settings.compiled:
buttons[0].append(Sg.Button('', key='test', font=(global_settings.font, 12)))
layout += buttons
layout.append([Sg.Text(' ', font='Any 50')])
if global_settings.new_version:
global_settings.new_version.cleanup()
update_column_layout = [
[Sg.Text(' ', size=(1, 1), key='update_available_title', font=(global_settings.font, 25))],
[Sg.Text(' ', size=(1, 1), key='update_available_version', font=(global_settings.font, 15))],
[Sg.Button('', key='update_available_button', font=(global_settings.font, 12))],
[Sg.Text(' ', font='Any 50')]
]
layout.append([Sg.Column(update_column_layout)])
window = Sg.Window('', layout, icon=global_settings.icon, metadata='main_window_title').Finalize()
global_settings.update_window(window)
return window
def toggle_bot(self):
if self.bot_thread and self.bot_thread.is_alive():
logger.info('stop_bot')
self.queue.put_nowait('STOP')
else:
logger.info('start_bot')
config = Config.load()
logger.debug(global_settings)
logger.debug(config)
bot = BrawlhallaBot(config, Hotkeys.load(), self.queue)
self.bot_thread = threading.Thread(target=bot.main_loop, daemon=True)
self.bot_thread.start()
def clear_queue(self):
while not self.queue.empty():
try:
self.queue.get_nowait()
except queue.Empty:
continue
self.queue.task_done()
def delayed_stop(self):
if self.bot_thread and self.bot_thread.is_alive():
if self.window['delayed_stop'].metadata:
logger.info('cancel_stop')
self.clear_queue()
else:
logger.info('delayed_stop')
self.queue.put_nowait('DELAYED_STOP')
self.window['delayed_stop'].metadata = not self.window['delayed_stop'].metadata
def refresh_buttons(self):
if self.downloading_new_version:
self.window['toggle'].update(disabled=True)
self.window['delayed_stop'].update(visible=False)
self.window['instructions'].update(disabled=True)
self.window['settings'].update(disabled=True)
self.window['update_available_button'].update(disabled=True)
self.window['exit'].update(disabled=True)
elif self.bot_thread and self.bot_thread.is_alive():
self.window['toggle'].metadata = 1
self.window['press_start'].metadata = 1
self.window['delayed_stop'].Update(visible=True)
self.window['instructions'].Update(disabled=True)
self.window['settings'].Update(disabled=True)
else:
self.window['toggle'].metadata = 0
self.window['press_start'].metadata = 0
self.window['delayed_stop'].metadata = 0
self.window['delayed_stop'].Update(visible=False)
self.window['instructions'].Update(disabled=False)
self.window['settings'].Update(disabled=False)
self.clear_queue()
@staticmethod
def display_instructions():
contents = global_settings.language.LAYOUT_MAPPING.get('instructions_contents',
global_settings.get_language('English').LAYOUT_MAPPING.get('instructions_contents', 'Should be stuff here'))
Sg.popup(*contents, font=(global_settings.font, 13),
title=global_settings.language.LAYOUT_MAPPING.get('instructions_window_title', 'Instructions'), icon=global_settings.icon)
def configure(self):
settings = GUIConfig()
self.window.disable()
self.window.hide()
settings.start_loop()
self.window.enable()
self.window.un_hide()
def autostart(self):
if not global_settings.autostart:
return
if self.bot_thread and self.bot_thread.is_alive():
self.last_window_check = time()
elif time() - self.last_window_check > 300:
logger.debug('autostart_check')
bh = BrawlhallaProcess.find()
if not bh:
self.toggle_bot()
self.last_window_check = time()
def main_loop(self):
while True:
event, values = self.window.Read(timeout=50)
if event in (Sg.WINDOW_CLOSED, 'exit'):
break
elif event == 'toggle':
self.toggle_bot()
elif event == 'delayed_stop':
self.delayed_stop()
elif event == 'instructions':
self.display_instructions()
elif event == 'settings':
self.configure()
elif event == 'test':
logger.info('test')
logger.debug('test')
elif event == 'take_screenshot':
bh = BrawlhallaProcess.find()
if bh is None:
logger.error('bh_not_running')
else:
screenshot_name = f'screenshot-{uuid.uuid4()}.png'
screenshot_path = Path(os.getenv('LOCALAPPDATA')) / 'BHBot' / screenshot_name
bh.make_screenshot().save(screenshot_path)
logger.info('took_screenshot', str(screenshot_path))
elif event == 'update_available_button':
global_settings.new_version.download(background=True)
self.downloading_new_version = True
elif not self.downloading_new_version:
self.autostart()
if global_settings.compiled and global_settings.new_version and self.downloading_new_version and global_settings.new_version.is_downloaded():
global_settings.new_version.extract_restart()
self.refresh_buttons()
global_settings.update_window(self.window)
self.window.close()
if __name__ == '__main__':
Singleton()
gui = GUI()
gui.main_loop()