gpml/script/month.gd
2024-05-28 02:58:12 +02:00

66 lines
1.6 KiB
GDScript

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
@onready var days = $HBoxContainer/Days
# Called when the node enters the scene tree for the first time.
func _ready():
month_label.text = "%s" % month
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)
# 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)
# Instanciate days in no save game
if FileAccess.file_exists("user://savegame.save"):
return
for i in range(1, days_in_month + 1):
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")
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)
if month == 2 and is_leap_year(year):
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)