forked from Gosha/vrc-owo-suit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
326 lines (289 loc) · 14.5 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import params
import config
import webbrowser
from event import Event
import dearpygui.dearpygui as dpg
from enum import Enum, auto
class Element(Enum):
IP_ADDRESS_INPUT = auto()
DETECT_IP_ADDRESS_CHECKBOX = auto()
USE_OSCQUERY_CHECKBOX = auto()
SERVER_PORT_NUMBER_INPUT = auto()
FREQUENCY_SETTING_SLIDER = auto()
LEFT_PECTORAL_SETTING_SLIDER = auto()
RIGHT_PECTORAL_SETTING_SLIDER = auto()
LEFT_ABDOMINAL_SETTING_SLIDER = auto()
RIGHT_ABDOMINAL_SETTING_SLIDER = auto()
LEFT_ARM_SETTING_SLIDER = auto()
RIGHT_ARM_SETTING_SLIDER = auto()
LEFT_DORSAL_SETTING_SLIDER = auto()
RIGHT_DORSAL_SETTING_SLIDER = auto()
LEFT_LUMBAR_SETTING_SLIDER = auto()
RIGHT_LUMBAR_SETTING_SLIDER = auto()
TERMINAL_WINDOW_INPUT = auto()
CONNECT_BUTTON = auto()
SAVE_SETTINGS_BUTTON = auto()
CLEAR_CONSOLE_BUTTON = auto()
TOGGLES_INTERACTIONS_BUTTON = auto()
CONNECT_ON_STARTUP_CHECKBOX = auto()
CONTRIBUTE_BUTTON = auto()
class Gui:
def __init__(self, config: config.Config, window_width: int, window_height: int, logo_path: str):
self.config = config
self.sliders = []
self.window_width = window_width
self.window_height = window_height
self.logo_path = logo_path
self.on_connect_clicked = Event()
self.on_save_settings_clicked = Event()
self.on_clear_console_clicked = Event()
self.on_toggle_interaction_clicked = Event()
self.elements = {
Element.IP_ADDRESS_INPUT: None,
Element.DETECT_IP_ADDRESS_CHECKBOX: None,
Element.USE_OSCQUERY_CHECKBOX: None,
Element.SERVER_PORT_NUMBER_INPUT: None,
Element.FREQUENCY_SETTING_SLIDER: None,
Element.LEFT_PECTORAL_SETTING_SLIDER: None,
Element.RIGHT_PECTORAL_SETTING_SLIDER: None,
Element.LEFT_ABDOMINAL_SETTING_SLIDER: None,
Element.RIGHT_ABDOMINAL_SETTING_SLIDER: None,
Element.LEFT_ARM_SETTING_SLIDER: None,
Element.RIGHT_ARM_SETTING_SLIDER: None,
Element.LEFT_DORSAL_SETTING_SLIDER: None,
Element.RIGHT_DORSAL_SETTING_SLIDER: None,
Element.LEFT_LUMBAR_SETTING_SLIDER: None,
Element.RIGHT_LUMBAR_SETTING_SLIDER: None,
Element.TERMINAL_WINDOW_INPUT: None,
Element.CONNECT_BUTTON: None,
Element.SAVE_SETTINGS_BUTTON: None,
Element.CLEAR_CONSOLE_BUTTON: None,
Element.TOGGLES_INTERACTIONS_BUTTON: None,
Element.CONNECT_ON_STARTUP_CHECKBOX: None,
Element.CONTRIBUTE_BUTTON: None,
}
self.element_to_config_key = {
Element.USE_OSCQUERY_CHECKBOX: "use_oscquery",
Element.SERVER_PORT_NUMBER_INPUT: "server_port",
Element.IP_ADDRESS_INPUT: "owo_ip",
Element.DETECT_IP_ADDRESS_CHECKBOX: "should_detect_ip",
Element.CONNECT_ON_STARTUP_CHECKBOX: "should_connect_on_startup",
Element.FREQUENCY_SETTING_SLIDER: "frequency",
"intensities": {
Element.LEFT_PECTORAL_SETTING_SLIDER: params.owo_suit_Pectoral_L,
Element.RIGHT_PECTORAL_SETTING_SLIDER: params.owo_suit_Pectoral_R,
Element.LEFT_ABDOMINAL_SETTING_SLIDER: params.owo_suit_Abdominal_L,
Element.RIGHT_ABDOMINAL_SETTING_SLIDER: params.owo_suit_Abdominal_R,
Element.LEFT_ARM_SETTING_SLIDER: params.owo_suit_Arm_L,
Element.RIGHT_ARM_SETTING_SLIDER: params.owo_suit_Arm_R,
Element.LEFT_DORSAL_SETTING_SLIDER: params.owo_suit_Dorsal_L,
Element.RIGHT_DORSAL_SETTING_SLIDER: params.owo_suit_Dorsal_R,
Element.LEFT_LUMBAR_SETTING_SLIDER: params.owo_suit_Lumbar_L,
Element.RIGHT_LUMBAR_SETTING_SLIDER: params.owo_suit_Lumbar_R,
}
}
self.parameter_to_muscle_element = {
value: key for key, value in self.element_to_config_key.get('intensities').items()
}
self.element_labels = {
Element.LEFT_PECTORAL_SETTING_SLIDER: "Left Pectoral",
Element.RIGHT_PECTORAL_SETTING_SLIDER: "Right Pectoral",
Element.LEFT_ABDOMINAL_SETTING_SLIDER: "Left Abdominal",
Element.RIGHT_ABDOMINAL_SETTING_SLIDER: "Right Abdominal",
Element.LEFT_ARM_SETTING_SLIDER: "Left Arm",
Element.RIGHT_ARM_SETTING_SLIDER: "Right Arm",
Element.LEFT_DORSAL_SETTING_SLIDER: "Left Dorsal",
Element.RIGHT_DORSAL_SETTING_SLIDER: "Right Dorsal",
Element.LEFT_LUMBAR_SETTING_SLIDER: "Left Lumbar",
Element.RIGHT_LUMBAR_SETTING_SLIDER: "Right Lumbar",
}
self.ids_to_elements = None
def handle_connect_callback(self, sender, app_data):
self.on_connect_clicked.dispatch(sender, app_data)
def handle_save_settings_callback(self):
self.config.write_config_to_disk()
self.print_terminal("Settings Saved!")
def handle_clear_console_callback(self, sender, app_data):
self.on_clear_console_clicked.dispatch(sender, app_data)
def handle_active_muscle_update(self, parameter):
element_name = self.parameter_to_muscle_element.get(parameter)
element_id = self.elements[element_name]
existing_element_label = self.element_labels[element_name]
result = "[" + existing_element_label + "]"
dpg.configure_item(
element_id, label=result
)
def handle_active_muscle_reset(self):
for element_name in self.parameter_to_muscle_element.values():
element_id = self.elements[element_name]
label = self.element_labels[element_name]
dpg.configure_item(
element_id, label=label
)
def handle_toggle_interactions_callback(self, sender, app_data):
self.on_toggle_interaction_clicked.dispatch()
def handle_input_change(self, sender, app_data):
element = self.ids_to_elements.get(sender)
config_key = self.element_to_config_key.get(element)
# this implies its an intensity
if config_key is None:
intensities_map = self.element_to_config_key.get("intensities")
config_key = intensities_map.get(element)
intensities = self.config.get_by_key("intensities")
intensities[config_key] = app_data
self.config.update("intensities", intensities)
return
self.config.update(config_key, app_data)
def handle_contribute_callback(self, sender, app_data):
webbrowser.open("https://github.com/uzair-ashraf/vrc-owo-suit")
def handle_connecting_state_change(self, next_state):
if next_state == "CONNECTING":
dpg.configure_item(
self.elements[Element.CONNECT_BUTTON], label="Connecting...", enabled=False)
return
elif next_state == "CONNECTED":
dpg.configure_item(
self.elements[Element.CONNECT_BUTTON], label="Connected!", enabled=False)
return
elif next_state == "DISCONNECTED":
dpg.configure_item(
self.elements[Element.CONNECT_BUTTON], label="Connect", enabled=True)
return
def create_centered_image(self, tag: str, path: str):
image_width, image_height, _, data = dpg.load_image(path)
with dpg.texture_registry():
dpg.add_static_texture(
width=image_width, height=image_height, default_value=data, tag=tag)
spacer_width = (self.window_width - image_width) / 2
with dpg.group(horizontal=True):
width_spacer_id = dpg.add_spacer(width=int(spacer_width) - 25)
dpg.add_image(tag)
width_spacer_id_2 = dpg.add_spacer(width=int(spacer_width))
def resize_callback():
current_window_width = dpg.get_viewport_width()
spacer_width = (current_window_width - image_width) / 2
dpg.configure_item(width_spacer_id, width=int(spacer_width) - 25)
dpg.configure_item(width_spacer_id_2, width=int(spacer_width))
return resize_callback
def print_terminal(self, text: str) -> None:
value = dpg.get_value(self.elements[Element.TERMINAL_WINDOW_INPUT])
dpg.set_value(
self.elements[Element.TERMINAL_WINDOW_INPUT], text + '\n' + value)
def on_clear_console(self, *args) -> None:
dpg.set_value(
self.elements[Element.TERMINAL_WINDOW_INPUT], "Cleared.")
def add_listeners(self) -> None:
self.on_clear_console_clicked.add_listener(self.on_clear_console)
def create_owo_suit_ip_address_input(self):
owo_ip = self.config.get_by_key("owo_ip") or ""
dpg.add_text("OWO Suit IP Address")
self.elements[Element.IP_ADDRESS_INPUT] = dpg.add_input_text(default_value=owo_ip,
width=-1, callback=self.handle_input_change)
def create_detect_address_checkbox(self):
should_detect_ip = self.config.get_by_key("should_detect_ip")
self.elements[Element.DETECT_IP_ADDRESS_CHECKBOX] = dpg.add_checkbox(
label="Automatically Detect IP Address", default_value=should_detect_ip, callback=self.handle_input_change)
def create_server_port_input(self):
server_port = self.config.get_by_key("server_port") or 9001
dpg.add_text("Server Port Number")
self.elements[Element.SERVER_PORT_NUMBER_INPUT] = dpg.add_input_int(default_value=server_port,
width=-1, callback=self.handle_input_change)
def create_use_oscquery_checkbox(self):
use_oscquery = self.config.get_by_key("use_oscquery")
self.elements[Element.USE_OSCQUERY_CHECKBOX] = dpg.add_checkbox(
label="Use OSCQuery", default_value=use_oscquery, callback=self.handle_input_change)
def create_frequency_slider(self):
frequency = self.config.get_by_key("frequency")
dpg.add_text("Frequency Settings")
self.elements[Element.FREQUENCY_SETTING_SLIDER] = dpg.add_slider_int(
min_value=0,
max_value=100,
width=-1,
default_value=frequency,
callback=self.handle_input_change
)
def create_intensity_settings(self):
dpg.add_text("Intensity Settings")
for element, label in self.element_labels.items():
self.create_intensity_slider(element, label)
def create_intensity_slider(self, element: Element, label: str):
intensities_map = self.element_to_config_key.get("intensities")
config_key = intensities_map.get(element)
intensities = self.config.get_by_key("intensities")
default_value = intensities.get(config_key) or 0
self.elements[element] = dpg.add_slider_int(default_value=default_value, min_value=0, width=-120,
max_value=100, label=label, callback=self.handle_input_change)
def create_logs_output(self):
dpg.add_text("Logs")
self.elements[Element.TERMINAL_WINDOW_INPUT] = dpg.add_input_text(
multiline=True, readonly=True, height=90, width=-1)
def create_button_group(self):
with dpg.group(horizontal=True):
self.elements[Element.CONNECT_BUTTON] = dpg.add_button(label="Connect",
callback=self.handle_connect_callback)
self.elements[Element.SAVE_SETTINGS_BUTTON] = dpg.add_button(label="Save Settings",
callback=self.handle_save_settings_callback)
self.elements[Element.CLEAR_CONSOLE_BUTTON] = dpg.add_button(label="Clear Console",
callback=self.handle_clear_console_callback)
self.elements[Element.TOGGLES_INTERACTIONS_BUTTON] = dpg.add_button(label="Toggle Interactions",
callback=self.handle_toggle_interactions_callback)
def create_connect_startup_checkbox(self):
should_connect_on_startup = self.config.get_by_key(
"should_connect_on_startup"
)
self.elements[Element.CONNECT_ON_STARTUP_CHECKBOX] = dpg.add_checkbox(
default_value=should_connect_on_startup,
label="Automatically Connect on Startup",
callback=self.handle_input_change
)
def create_footer(self):
with dpg.group(width=-1):
self.elements[Element.CONTRIBUTE_BUTTON] = dpg.add_button(
label="\t\t\t\t Created by Shadoki.\nThis application is not affiliated with VRChat or OWO.\n\t\t\t\t Want to contribute?",
width=-1,
callback=self.handle_contribute_callback
)
def validate_connect_on_startup(self):
should_connect_on_startup = self.config.get_by_key(
"should_connect_on_startup"
)
if should_connect_on_startup:
self.handle_connect_callback(Element.CONNECT_BUTTON, None)
def init(self):
dpg.create_context()
with dpg.window(tag="MAIN_WINDOW"):
dpg.add_spacer(height=20)
handle_centered_image = self.create_centered_image(
"logo", self.logo_path)
dpg.add_spacer(height=20)
self.create_owo_suit_ip_address_input()
self.create_detect_address_checkbox()
dpg.add_spacer(height=20)
self.create_server_port_input()
self.create_use_oscquery_checkbox()
dpg.add_spacer(height=20)
self.create_frequency_slider()
dpg.add_spacer(height=20)
self.create_intensity_settings()
dpg.add_spacer(height=20)
self.create_logs_output()
dpg.add_spacer(height=20)
self.create_button_group()
dpg.add_spacer(height=20)
self.create_connect_startup_checkbox()
dpg.add_spacer(height=20)
self.create_footer()
self.add_listeners()
dpg.create_viewport(title='VRChat OWO Suit',
width=self.window_width, height=self.window_height)
dpg.set_viewport_resize_callback(handle_centered_image)
self.ids_to_elements = {
value: key for key, value in self.elements.items()}
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.set_primary_window("MAIN_WINDOW", True)
def run(self):
self.validate_connect_on_startup()
dpg.start_dearpygui()
def cleanup(self):
dpg.destroy_context()