chore: add confirm popup

This commit is contained in:
Varylios 2025-09-06 23:26:37 +02:00
parent 0f3ca8137a
commit 42db074755
7 changed files with 96 additions and 5 deletions

View file

@ -37,6 +37,9 @@ func _ready() -> void:
func _input(event: InputEvent) -> void:
if Engine.is_editor_hint():
return
if event.is_action_pressed("pause_game"):
get_tree().paused = not get_tree().paused

View file

@ -1,6 +1,10 @@
@tool
class_name Helper
const confirmPopupScene : PackedScene = preload("res://UI/confirm_popup.tscn")
const SCENE_DIR_PATTERN : String = "res://\\w+/(Scenes|Map \\d+)/$"
const SCENE_FILE_PATTERN : String = "([^/]*)\\.tscn$"
const RESOURCE_FILE_PATTERN : String = "([^/]*)\\.tres$"
@ -63,3 +67,16 @@ static func getFileFromDir(path : String, regexPattern : String, pathRegexPatter
files.append(path + file)
return files
static func showConfirmPopup(
text : String,
nodeToAppend : Node,
confirmCallback : Callable,
cancelCallback : Callable = func(): null
) -> void:
var confirmPopup : ConfirmPopup = confirmPopupScene.instantiate()
nodeToAppend.add_child(confirmPopup)
confirmPopup.label.text = text
confirmPopup.confirmed.connect(confirmCallback)
confirmPopup.canceled.connect(cancelCallback)

View file

@ -146,10 +146,11 @@ func onBodyExited(body: Node3D) -> void:
func toggleConnection(activate : bool) -> void:
if activate:
if activate && not $Range.body_entered.is_connected(onBodyEntered):
$Range.body_entered.connect(onBodyEntered)
$Range.body_exited.connect(onBodyExited)
else:
if $Range.body_entered.is_connected(onBodyEntered):
$Range.body_entered.disconnect(onBodyEntered)
$Range.body_exited.disconnect(onBodyExited)
availableTargets.clear()

18
UI/ConfirmPopup.gd Normal file
View file

@ -0,0 +1,18 @@
extends PanelContainer
class_name ConfirmPopup
signal confirmed
signal canceled
@onready var label : Label = $MarginContainer/ConfirmPrompt/Label
func _ready() -> void:
%CancelBtn.pressed.connect(emitPressedButton.bind(canceled))
%ConfirmBtn.pressed.connect(emitPressedButton.bind(confirmed))
func emitPressedButton(state : Signal) -> void:
state.emit()
queue_free()

1
UI/ConfirmPopup.gd.uid Normal file
View file

@ -0,0 +1 @@
uid://8c1ej1k0vbmn

51
UI/confirm_popup.tscn Normal file
View file

@ -0,0 +1,51 @@
[gd_scene load_steps=3 format=3 uid="uid://cxn3x8dq8vawa"]
[ext_resource type="Script" uid="uid://8c1ej1k0vbmn" path="res://UI/ConfirmPopup.gd" id="1_8vkfp"]
[ext_resource type="Theme" uid="uid://bbp4qcbwkeg7x" path="res://default_theme.tres" id="1_x7bkm"]
[node name="Panel" type="PanelContainer"]
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -194.0
offset_right = 194.0
offset_bottom = 128.0
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 3
size_flags_vertical = 3
theme = ExtResource("1_x7bkm")
script = ExtResource("1_8vkfp")
[node name="MarginContainer" type="MarginContainer" parent="."]
layout_mode = 2
theme_override_constants/margin_left = 50
theme_override_constants/margin_top = 50
theme_override_constants/margin_right = 50
theme_override_constants/margin_bottom = 25
[node name="ConfirmPrompt" type="VBoxContainer" parent="MarginContainer"]
layout_mode = 2
theme_override_constants/separation = 50
[node name="Label" type="Label" parent="MarginContainer/ConfirmPrompt"]
layout_mode = 2
size_flags_vertical = 6
horizontal_alignment = 1
[node name="HBoxContainer" type="HBoxContainer" parent="MarginContainer/ConfirmPrompt"]
layout_mode = 2
theme_override_constants/separation = 50
[node name="ConfirmBtn" type="Button" parent="MarginContainer/ConfirmPrompt/HBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
text = "Confirm"
[node name="CancelBtn" type="Button" parent="MarginContainer/ConfirmPrompt/HBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
text = "Cancel
"

View file

@ -21,8 +21,8 @@ func _ready() -> void:
EventBus.wave_has_change.connect(onWaveChange)
%NextWaveBtn.pressed.connect(EventBus.lauch_next_wave.emit)
$WaveCooldown/Timer.timeout.connect(updateWaveCooldownLabel)
%QuitLevelBtn.pressed.connect(Game.quitLevel)
%QuitGameBtn.pressed.connect(Game.quitGame)
%QuitLevelBtn.pressed.connect(Helper.showConfirmPopup.bind("Quit level ?", self, Game.quitLevel))
%QuitGameBtn.pressed.connect(Helper.showConfirmPopup.bind("Quit game ?", self, Game.quitGame))
$TowerInfoPanel.visible = false
if Engine.is_editor_hint():