65 lines
1.7 KiB
GDScript
65 lines
1.7 KiB
GDScript
class_name Calendar
|
|
extends Control
|
|
|
|
@export var month_scene: PackedScene
|
|
|
|
@onready var year_label = $CenterContainer/VBoxContainer/YearLabel
|
|
@onready var year = $CenterContainer/VBoxContainer/Year
|
|
@onready var period_manager = $PeriodManager
|
|
|
|
var last_focused_day = null
|
|
var new_focused_day = null
|
|
var all_days = []
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready():
|
|
var current_year = Time.get_date_dict_from_system().year
|
|
|
|
year_label.text = "%s" % current_year
|
|
|
|
for i in range (1, 13):
|
|
var month = month_scene.instantiate()
|
|
month.year = current_year
|
|
month.month = i
|
|
month.day_toggled.connect(_on_day_toggled)
|
|
month.fill_days.connect(_on_fill_days)
|
|
year.add_child(month)
|
|
|
|
for element in month.days.get_children():
|
|
if element is Day:
|
|
all_days.append(element)
|
|
|
|
|
|
func _on_day_toggled(date):
|
|
# period_manager.add(date)
|
|
|
|
# Update last focused day.
|
|
var focused_node = get_viewport().gui_get_focus_owner()
|
|
if focused_node is Day:
|
|
last_focused_day = focused_node
|
|
|
|
|
|
func _on_fill_days(shift_clicked_day):
|
|
# Toogle day from last focused day to current day.
|
|
|
|
# Assert that last focused day is different from date.
|
|
if last_focused_day == null:
|
|
return
|
|
|
|
if shift_clicked_day == last_focused_day:
|
|
return
|
|
|
|
# Determine if date is before or after the last focused day.
|
|
var first_day = last_focused_day if last_focused_day.is_before(shift_clicked_day) else shift_clicked_day
|
|
var last_day = shift_clicked_day if first_day == last_focused_day else last_focused_day
|
|
|
|
# Toggle days between the two days.
|
|
var toggle = false
|
|
for day in all_days:
|
|
if first_day == day:
|
|
toggle = true
|
|
if toggle:
|
|
day.set_pressed_no_signal(true)
|
|
if last_day == day:
|
|
return
|