2024-05-25 18:50:50 +02:00
|
|
|
class_name Month
|
|
|
|
extends Control
|
|
|
|
|
|
|
|
signal day_toggled(date)
|
|
|
|
signal fill_days(date)
|
|
|
|
|
|
|
|
const DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
|
|
|
|
|
|
|
@export var day_scene: PackedScene
|
|
|
|
var year: int
|
|
|
|
var month: int
|
|
|
|
|
|
|
|
@onready var month_label = $HBoxContainer/MonthLabel
|
2024-05-28 02:58:12 +02:00
|
|
|
@onready var days = $HBoxContainer/Days
|
2024-05-25 18:50:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready():
|
|
|
|
month_label.text = "%s" % month
|
2024-05-25 21:14:03 +02:00
|
|
|
|
2024-05-25 18:50:50 +02:00
|
|
|
var days_in_month = find_days_in_month(year, month)
|
|
|
|
var date_string = "%s-%s-%s" % [year, month, 1]
|
|
|
|
var first_day = Time.get_datetime_dict_from_datetime_string(date_string, true)
|
2024-05-25 21:14:03 +02:00
|
|
|
|
2024-05-25 18:50:50 +02:00
|
|
|
# Fill the container with number corresponding to days
|
|
|
|
for i in range(first_day.weekday):
|
|
|
|
var empty_label = Label.new()
|
|
|
|
days.add_child(empty_label)
|
2024-05-28 02:58:12 +02:00
|
|
|
|
|
|
|
# Instanciate days in no save game
|
|
|
|
if FileAccess.file_exists("user://savegame.save"):
|
|
|
|
return
|
|
|
|
|
2024-05-25 18:50:50 +02:00
|
|
|
for i in range(1, days_in_month + 1):
|
2024-05-28 02:58:12 +02:00
|
|
|
instantiate_day(i, false)
|
|
|
|
|
|
|
|
|
|
|
|
func instantiate_day(day, is_pressed):
|
|
|
|
var new_day = day_scene.instantiate()
|
|
|
|
days.add_child(new_day)
|
|
|
|
new_day.set_date(year, month, day)
|
|
|
|
new_day.text = "%s" % [day]
|
|
|
|
new_day.day_toggled.connect(_on_day_toggled)
|
|
|
|
new_day.fill_days.connect(_on_fill_days)
|
|
|
|
new_day.set_pressed_no_signal(is_pressed)
|
|
|
|
new_day.add_to_group("persist")
|
2024-05-25 18:50:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
func _on_day_toggled(date):
|
|
|
|
day_toggled.emit(date)
|
|
|
|
|
|
|
|
|
|
|
|
func _on_fill_days(day):
|
|
|
|
fill_days.emit(day)
|
|
|
|
|
|
|
|
|
|
|
|
func find_days_in_month(year: int, month: int):
|
|
|
|
assert(month >= 1 and month <= 12)
|
2024-05-25 21:14:03 +02:00
|
|
|
if month == 2 and is_leap_year(year):
|
2024-05-25 18:50:50 +02:00
|
|
|
return 29
|
|
|
|
return DAYS_IN_MONTH[month - 1]
|
|
|
|
|
|
|
|
|
|
|
|
func is_leap_year(year: int):
|
|
|
|
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
|