Repository navigation
Expand file tree
/
Copy pathsettings_menu.gd
More file actions
503 lines (391 loc) · 15.6 KB
/
Copy pathsettings_menu.gd
File metadata and controls
503 lines (391 loc) · 15.6 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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
extends Control
@onready var main_buttons_container: VBoxContainer = $VBoxContainer
var drop_rezolutie: OptionButton
var slider_luminozitate: HSlider
var drop_animatii: OptionButton
# variabile pentru sistemul de reasignare taste
var is_listening_for_key: bool = false
var action_to_remap: String = ""
var remap_button_reference: Button = null
var temp_key_binds: Dictionary = {}
# valori temporare pastrate pana la salvare
var temp_audio_volume: float = 1.0
var temp_music_muted: bool = false
var temp_resolution: int = 1
var temp_brightness: float = 1.0
var temp_anim_quality: int = 2
var main_menu_wrapper: CenterContainer = null
var current_submenu_overlay: Control = null
func _ready() -> void:
# initializam tastele implicite daca nu exista deja in InputMap
_asigura_actiune("jump", KEY_SPACE)
_asigura_actiune("clone", KEY_C)
_asigura_actiune("release_clone", KEY_E)
# fundal de culoare solidă pe un layer in spate
var fundal_absolut: CanvasLayer = CanvasLayer.new()
fundal_absolut.layer = -1
var bg: ColorRect = ColorRect.new()
bg.color = ProjectSettings.get_setting("rendering/environment/defaults/default_clear_color")
bg.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
fundal_absolut.add_child(bg)
add_child(fundal_absolut)
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
_centraza_meniul_principal()
func _centraza_meniul_principal() -> void:
if main_buttons_container == null:
return
# wrapper-ul exista deja, nu il recreem
if main_menu_wrapper != null and is_instance_valid(main_menu_wrapper):
return
var parent_node: Node = main_buttons_container.get_parent()
if parent_node == null:
return
# scoatem vbox-ul din parinte ca sa il punem intr-un CenterContainer
var original_index: int = main_buttons_container.get_index()
parent_node.remove_child(main_buttons_container)
main_menu_wrapper = CenterContainer.new()
main_menu_wrapper.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
main_menu_wrapper.mouse_filter = Control.MOUSE_FILTER_IGNORE
parent_node.add_child(main_menu_wrapper)
parent_node.move_child(main_menu_wrapper, original_index)
main_menu_wrapper.add_child(main_buttons_container)
main_buttons_container.alignment = BoxContainer.ALIGNMENT_CENTER
main_buttons_container.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
main_buttons_container.size_flags_vertical = Control.SIZE_SHRINK_CENTER
func _asigura_actiune(nume: String, tasta_default: Key) -> void:
# cream actiunea daca nu exista
if not InputMap.has_action(nume):
InputMap.add_action(nume)
# stergem event-urile vechi ca sa nu se dubleze
InputMap.action_erase_events(nume)
var event: InputEventKey = InputEventKey.new()
event.keycode = tasta_default
event.physical_keycode = tasta_default
InputMap.action_add_event(nume, event)
func _on_back_button_pressed() -> void:
get_tree().change_scene_to_file("res://MainMenu.tscn")
func _on_video_button_pressed() -> void:
_creeaza_meniu_video()
func _on_keyboard_button_pressed() -> void:
_creeaza_meniu_taste()
func _on_audio_button_pressed() -> void:
_creeaza_meniu_audio()
func _inchide_submeniu_curent() -> void:
# stergem overlay-ul activ daca exista
if current_submenu_overlay != null and is_instance_valid(current_submenu_overlay):
current_submenu_overlay.queue_free()
current_submenu_overlay = null
func _get_video_resolutions() -> Array[Vector2i]:
# lista de rezolutii disponibile
return [
Vector2i(1024, 576),
Vector2i(1152, 648),
Vector2i(1280, 720),
Vector2i(1366, 768),
Vector2i(1600, 900),
Vector2i(1920, 1080)
]
func _get_video_resolution_labels() -> Array[String]:
# textele afisate in dropdown pentru fiecare rezolutie
return [
"1024x576",
"1152x648 (Native)",
"1280x720",
"1366x768",
"1600x900",
"1920x1080 (Full HD)"
]
func _get_resolution_index_for_size(size: Vector2i) -> int:
# returnam indexul rezolutiei curente in lista, sau 1 ca fallback
var rezolutii: Array[Vector2i] = _get_video_resolutions()
for i in range(rezolutii.size()):
if rezolutii[i] == size:
return i
return 1
func _creeaza_meniu_audio() -> void:
_inchide_submeniu_curent()
# citim valorile curente din audio server
var bus_idx: int = AudioServer.get_bus_index("Master")
temp_audio_volume = db_to_linear(AudioServer.get_bus_volume_db(bus_idx))
temp_music_muted = AudioServer.is_bus_mute(bus_idx)
var meniu: VBoxContainer = _creeaza_baza_submeniu("AUDIO SETTINGS")
# rand cu slider si butoane +/- pentru volum
var vol_container: HBoxContainer = HBoxContainer.new()
vol_container.alignment = BoxContainer.ALIGNMENT_CENTER
vol_container.add_theme_constant_override("separation", 15)
var btn_minus: Button = Button.new()
btn_minus.text = "-"
var slider: HSlider = HSlider.new()
slider.custom_minimum_size = Vector2(220, 30)
slider.min_value = 0.0
slider.max_value = 1.0
slider.step = 0.05
slider.value = temp_audio_volume
var btn_plus: Button = Button.new()
btn_plus.text = "+"
var eticheta_volum: Label = Label.new()
eticheta_volum.text = str(round(temp_audio_volume * 100)) + "%"
# actualizam valoarea temporara si textul procentual
var update_vol_ui := func(val: float) -> void:
temp_audio_volume = clamp(val, 0.0, 1.0)
slider.value = temp_audio_volume
eticheta_volum.text = str(round(temp_audio_volume * 100)) + "%"
slider.value_changed.connect(update_vol_ui)
btn_minus.pressed.connect(func():
update_vol_ui.call(temp_audio_volume - 0.05)
)
btn_plus.pressed.connect(func():
update_vol_ui.call(temp_audio_volume + 0.05)
)
vol_container.add_child(btn_minus)
vol_container.add_child(slider)
vol_container.add_child(btn_plus)
vol_container.add_child(eticheta_volum)
meniu.add_child(vol_container)
# buton toggle pentru oprirea sau pornirea sunetului
var btn_stop: Button = Button.new()
btn_stop.text = "PLAY SOUND" if temp_music_muted else "STOP SOUND"
btn_stop.toggle_mode = true
btn_stop.button_pressed = temp_music_muted
btn_stop.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
btn_stop.pressed.connect(func():
temp_music_muted = btn_stop.button_pressed
btn_stop.text = "PLAY SOUND" if temp_music_muted else "STOP SOUND"
)
meniu.add_child(btn_stop)
# la save aplicam volumul si mute-ul in audio server
_adauga_butoane_control(meniu, func() -> void:
AudioServer.set_bus_volume_db(bus_idx, linear_to_db(temp_audio_volume))
AudioServer.set_bus_mute(bus_idx, temp_music_muted)
)
func _creeaza_meniu_video() -> void:
_inchide_submeniu_curent()
# detectam rezolutia ferestrei curente
temp_resolution = _get_resolution_index_for_size(get_window().size)
# citim calitatea animatiilor setata anterior din GameManager
if GameManager != null:
temp_anim_quality = GameManager.anim_quality
var meniu: VBoxContainer = _creeaza_baza_submeniu("VIDEO SETTINGS")
var lab_rez: Label = Label.new()
lab_rez.text = "Resolution:"
lab_rez.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
meniu.add_child(lab_rez)
# dropdown cu rezolutiile disponibile
drop_rezolutie = OptionButton.new()
var labels: Array[String] = _get_video_resolution_labels()
for label_text in labels:
drop_rezolutie.add_item(label_text)
drop_rezolutie.selected = temp_resolution
drop_rezolutie.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
drop_rezolutie.item_selected.connect(func(index: int) -> void:
temp_resolution = index
)
meniu.add_child(drop_rezolutie)
var lab_lum: Label = Label.new()
lab_lum.text = "Brightness:"
lab_lum.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
meniu.add_child(lab_lum)
# slider pentru luminozitate cu previzualizare in timp real
slider_luminozitate = HSlider.new()
slider_luminozitate.min_value = 0.5
slider_luminozitate.max_value = 1.5
slider_luminozitate.step = 0.1
slider_luminozitate.value = temp_brightness
slider_luminozitate.custom_minimum_size = Vector2(220, 20)
slider_luminozitate.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
slider_luminozitate.value_changed.connect(func(val: float) -> void:
temp_brightness = val
# cautam sau cream un CanvasModulate global pentru luminozitate
var brightness_node: Node = get_tree().root.get_node_or_null("GlobalBrightnessFilter")
if brightness_node == null:
var modulate: CanvasModulate = CanvasModulate.new()
modulate.name = "GlobalBrightnessFilter"
get_tree().root.add_child(modulate)
brightness_node = modulate
if brightness_node is CanvasModulate:
brightness_node.color = Color(temp_brightness, temp_brightness, temp_brightness, 1.0)
)
meniu.add_child(slider_luminozitate)
var lab_anim: Label = Label.new()
lab_anim.text = "Background Animations:"
lab_anim.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
meniu.add_child(lab_anim)
# dropdown pentru calitatea animatiilor de fundal
drop_animatii = OptionButton.new()
drop_animatii.add_item("Low")
drop_animatii.add_item("Medium")
drop_animatii.add_item("High")
drop_animatii.selected = clamp(temp_anim_quality, 0, 2)
drop_animatii.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
meniu.add_child(drop_animatii)
# la save aplicam toate schimbarile video si le scriem in GameManager
_adauga_butoane_control(meniu, func() -> void:
temp_brightness = slider_luminozitate.value
temp_anim_quality = drop_animatii.selected
temp_resolution = drop_rezolutie.selected
var rezolutii: Array[Vector2i] = _get_video_resolutions()
var noua_dimensiune: Vector2i = rezolutii[temp_resolution]
_aplica_rezolutie(noua_dimensiune)
GameManager.anim_quality = temp_anim_quality
)
func _aplica_rezolutie(noua_dimensiune: Vector2i) -> void:
var win: Window = get_window()
win.mode = Window.MODE_WINDOWED
win.unresizable = false
win.size = noua_dimensiune
# scalare viewport la noua dimensiune
get_viewport().content_scale_size = noua_dimensiune
get_viewport().content_scale_mode = Window.CONTENT_SCALE_MODE_CANVAS_ITEMS
get_viewport().content_scale_aspect = Window.CONTENT_SCALE_ASPECT_EXPAND
# centram fereastra pe ecran dupa resize
win.move_to_center()
func _creeaza_meniu_taste() -> void:
_inchide_submeniu_curent()
temp_key_binds.clear()
var meniu: VBoxContainer = _creeaza_baza_submeniu("KEYBOARD SETTINGS")
var actiuni: Array[String] = ["jump", "clone", "release_clone"]
# cream cate un buton de reasignare pentru fiecare actiune
for act in actiuni:
var current_action: String = act
var btn: Button = Button.new()
var key_name: String = _get_key_name(current_action)
var action_name: String = current_action.capitalize().replace("_", " ")
btn.text = action_name + " : " + key_name
btn.custom_minimum_size = Vector2(260, 45)
btn.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
# la apasare intram in modul de ascultare pentru o noua tasta
btn.pressed.connect(func():
is_listening_for_key = true
action_to_remap = current_action
remap_button_reference = btn
btn.text = "Press any key..."
)
meniu.add_child(btn)
# la save scriem tastele temporare in InputMap
_adauga_butoane_control(meniu, func() -> void:
for act_key in temp_key_binds.keys():
InputMap.action_erase_events(act_key)
InputMap.action_add_event(act_key, temp_key_binds[act_key])
)
func _input(event: InputEvent) -> void:
# capturam apasarea de tasta doar cand suntem in modul de reasignare
if is_listening_for_key and event is InputEventKey and event.pressed and not event.echo:
var key_event: InputEventKey = event as InputEventKey
if key_event == null:
return
# escape si alt nu pot fi reasignate
if key_event.keycode == KEY_ESCAPE or key_event.keycode == KEY_ALT:
_afiseaza_eroare("This key cannot be reassigned.")
if remap_button_reference != null:
remap_button_reference.text = action_to_remap.capitalize().replace("_", " ") + " : " + _get_key_name(action_to_remap)
else:
# salvam tasta temporar pana la apasarea de save
temp_key_binds[action_to_remap] = key_event
var action_name: String = action_to_remap.capitalize().replace("_", " ")
var key_text: String = ""
# construim textul afisat pe buton pentru tasta aleasa
match key_event.keycode:
KEY_SPACE: key_text = "Space"
KEY_C: key_text = "C"
KEY_E: key_text = "E"
_: key_text = key_event.as_text_physical_keycode()
if key_text.is_empty():
key_text = key_event.as_text_key_label()
if key_text.is_empty():
key_text = OS.get_keycode_string(key_event.keycode)
if key_text.is_empty():
key_text = "Unknown"
remap_button_reference.text = action_name + " : " + key_text
is_listening_for_key = false
get_viewport().set_input_as_handled()
func _get_key_name(action: String) -> String:
# returnam numele tastei asignate actiunii, sau fallback daca nu exista
var events: Array = InputMap.action_get_events(action)
if events.is_empty():
match action:
"jump": return "Space"
"clone": return "C"
"release_clone": return "E"
_: return "None"
for ev in events:
if ev is InputEventKey:
var key_event: InputEventKey = ev as InputEventKey
match key_event.keycode:
KEY_SPACE: return "Space"
KEY_C: return "C"
KEY_E: return "E"
var txt: String = key_event.as_text_physical_keycode()
if txt.is_empty():
txt = key_event.as_text_key_label()
if txt.is_empty():
txt = OS.get_keycode_string(key_event.keycode)
if txt.is_empty():
return "Unknown"
return txt
return "None"
func _creeaza_baza_submeniu(titlu_text: String) -> VBoxContainer:
# ascundem butoanele principale si curatam overlay-ul anterior
main_buttons_container.hide()
_inchide_submeniu_curent()
# overlay transparent peste tot ecranul
var overlay: Control = Control.new()
overlay.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
add_child(overlay)
var center: CenterContainer = CenterContainer.new()
center.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
overlay.add_child(center)
var submenu: VBoxContainer = VBoxContainer.new()
submenu.alignment = BoxContainer.ALIGNMENT_CENTER
submenu.add_theme_constant_override("separation", 20)
submenu.custom_minimum_size = Vector2(420, 0)
center.add_child(submenu)
# titlul submeniului
var titlu: Label = Label.new()
titlu.text = titlu_text
titlu.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
titlu.add_theme_font_size_override("font_size", 32)
submenu.add_child(titlu)
current_submenu_overlay = overlay
return submenu
func _adauga_butoane_control(meniu: VBoxContainer, save_func: Callable) -> void:
# rand cu buton de salvare si buton de inapoi
var hbox: HBoxContainer = HBoxContainer.new()
hbox.alignment = BoxContainer.ALIGNMENT_CENTER
hbox.add_theme_constant_override("separation", 20)
var btn_save: Button = Button.new()
btn_save.text = "Save and Exit"
btn_save.custom_minimum_size = Vector2(160, 45)
btn_save.pressed.connect(func():
save_func.call()
if current_submenu_overlay != null and is_instance_valid(current_submenu_overlay):
current_submenu_overlay.queue_free()
current_submenu_overlay = null
main_buttons_container.show()
)
var btn_back: Button = Button.new()
btn_back.text = "Back"
btn_back.custom_minimum_size = Vector2(120, 45)
btn_back.pressed.connect(func():
if current_submenu_overlay != null and is_instance_valid(current_submenu_overlay):
current_submenu_overlay.queue_free()
current_submenu_overlay = null
main_buttons_container.show()
)
hbox.add_child(btn_save)
hbox.add_child(btn_back)
meniu.add_child(hbox)
func _afiseaza_eroare(mesaj: String) -> void:
# afisam un label rosu temporar pentru mesaje de eroare
var err: Label = Label.new()
err.text = mesaj
err.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
err.add_theme_color_override("font_color", Color.RED)
add_child(err)
err.set_anchors_and_offsets_preset(Control.PRESET_CENTER_BOTTOM)
err.position = Vector2(-300, -80)
err.size = Vector2(600, 40)
# stergem eroarea automat dupa 3 secunde
get_tree().create_timer(3.0).timeout.connect(func():
if is_instance_valid(err):
err.queue_free()
)