87 lines
3.3 KiB
GDScript
87 lines
3.3 KiB
GDScript
tool
|
|
class_name NewGamePanel
|
|
extends PopupPanel
|
|
|
|
signal start_triggered(config)
|
|
|
|
const pref_path = "user://preferences.cfg"
|
|
|
|
export var window_scale_factor = 0.9 # how big the popup will be compared to screen
|
|
|
|
var preferences = ConfigFile.new()
|
|
var fade_duration = 0.2
|
|
var fade_scale_factor = 0.9
|
|
|
|
onready var popup = $"."
|
|
onready var panel = $Panel
|
|
onready var tween = $Tween
|
|
|
|
func _init():
|
|
var err = preferences.load(pref_path)
|
|
|
|
func _ready():
|
|
assert(popup.theme != null)
|
|
var popup_style : = popup.get_stylebox("panel", "PopupPanel") as StyleBoxFlat
|
|
assert(popup_style != null)
|
|
var panel_style := panel.get_stylebox("panel", "Panel") as StyleBoxFlat
|
|
assert(panel_style != null)
|
|
var modified_panel_style = panel_style.duplicate()
|
|
modified_panel_style.corner_radius_bottom_left = popup_style.corner_radius_bottom_left
|
|
modified_panel_style.corner_radius_bottom_right = popup_style.corner_radius_bottom_right
|
|
modified_panel_style.corner_radius_top_left = popup_style.corner_radius_top_left
|
|
modified_panel_style.corner_radius_top_right = popup_style.corner_radius_top_right
|
|
panel.set("custom_styles/panel", modified_panel_style)
|
|
|
|
func fade_out():
|
|
tween.remove_all()
|
|
tween.interpolate_property(self, "rect_scale", Vector2(1.0, 1.0), Vector2(fade_scale_factor, fade_scale_factor), fade_duration, Tween.TRANS_LINEAR, Tween.EASE_IN)
|
|
var scaled_center_position: Vector2 = (OS.window_size - (rect_size * fade_scale_factor)) / 2
|
|
tween.interpolate_property(self, "rect_position", rect_position, scaled_center_position, fade_duration, Tween.TRANS_LINEAR, Tween.EASE_IN)
|
|
tween.interpolate_property(self, "modulate:a", 1.0, 0.0, fade_duration, Tween.TRANS_LINEAR, Tween.EASE_IN)
|
|
tween.interpolate_callback(self, fade_duration, "hide")
|
|
tween.start()
|
|
|
|
func _on_Cancel_pressed():
|
|
fade_out()
|
|
|
|
func _on_Start_pressed():
|
|
if $Panel/HBoxContainer/Easy.pressed:
|
|
preferences.set_value("game", "difficulty", "easy")
|
|
if $Panel/HBoxContainer/Normal.pressed:
|
|
preferences.set_value("game", "difficulty", "normal")
|
|
if $Panel/HBoxContainer/Hard.pressed:
|
|
preferences.set_value("game", "difficulty", "hard")
|
|
|
|
preferences.save(pref_path)
|
|
|
|
emit_signal("start_triggered", preferences)
|
|
fade_out()
|
|
|
|
func _on_NewGamePanel_about_to_show():
|
|
$Panel/HBoxContainer/Easy.pressed = false
|
|
$Panel/HBoxContainer/Normal.pressed = false
|
|
$Panel/HBoxContainer/Hard.pressed = false
|
|
|
|
var difficulty = preferences.get_value("game", "difficulty", "normal")
|
|
match difficulty:
|
|
"easy":
|
|
$Panel/HBoxContainer/Easy.pressed = true
|
|
"normal":
|
|
$Panel/HBoxContainer/Normal.pressed = true
|
|
"hard":
|
|
$Panel/HBoxContainer/Hard.pressed = true
|
|
_:
|
|
assert("Invalid value")
|
|
$Panel/HBoxContainer/Normal.pressed = true
|
|
|
|
$Panel/Start.grab_focus()
|
|
|
|
var size = OS.window_size * window_scale_factor
|
|
tween.interpolate_property(self, "rect_scale", Vector2(fade_scale_factor, fade_scale_factor), Vector2(1.0, 1.0), fade_duration, Tween.TRANS_LINEAR, Tween.EASE_IN)
|
|
var original_position: Vector2 = (OS.window_size - size) / 2
|
|
var scaled_center_position: Vector2 = (OS.window_size - (size * fade_scale_factor)) / 2
|
|
tween.interpolate_property(self, "rect_position", scaled_center_position, original_position, fade_duration, Tween.TRANS_LINEAR, Tween.EASE_IN)
|
|
tween.interpolate_property(self, "modulate:a", 0.0, 1.0, fade_duration, Tween.TRANS_LINEAR, Tween.EASE_IN)
|
|
tween.start()
|
|
|