gpml/script/month.gd

55 lines
1.3 KiB
GDScript3
Raw Normal View History

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
@onready var days = $HBoxContainer/DaysGridContainer
# 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)
for i in range(1, days_in_month + 1):
var day = day_scene.instantiate()
day.set_date(year, month, i)
day.text = "%s" % [i]
day.day_toggled.connect(_on_day_toggled)
day.fill_days.connect(_on_fill_days)
days.add_child(day)
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)