Add base project
This commit is contained in:
parent
0cec0c70d5
commit
5abf7b3d81
19 changed files with 501 additions and 0 deletions
64
script/calendar.gd
Normal file
64
script/calendar.gd
Normal file
|
@ -0,0 +1,64 @@
|
|||
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
|
39
script/day.gd
Normal file
39
script/day.gd
Normal file
|
@ -0,0 +1,39 @@
|
|||
class_name Day
|
||||
extends Button
|
||||
|
||||
|
||||
signal day_toggled(date)
|
||||
signal fill_days(date)
|
||||
|
||||
var date = {}
|
||||
|
||||
|
||||
func _ready():
|
||||
set_toggle_mode(true);
|
||||
toggled.connect(self._on_day_toggled)
|
||||
|
||||
|
||||
func _gui_input(event):
|
||||
if event.is_action_pressed("shift_click"):
|
||||
fill_days.emit(self)
|
||||
accept_event()
|
||||
|
||||
|
||||
func _on_day_toggled(_toggled_on):
|
||||
day_toggled.emit(date)
|
||||
|
||||
|
||||
func set_date(year, month, day):
|
||||
date = {"year": year, "month": month, "day": day}
|
||||
|
||||
|
||||
func is_before(other_day):
|
||||
var day_unix_time = Time.get_unix_time_from_datetime_dict(date)
|
||||
var other_day_unix_time = Time.get_unix_time_from_datetime_dict(other_day.date)
|
||||
|
||||
if day_unix_time < other_day_unix_time:
|
||||
return true
|
||||
return false
|
||||
|
||||
func dump():
|
||||
print("%s - %s - %s" % [date.year, date.month, date.day])
|
55
script/month.gd
Normal file
55
script/month.gd
Normal file
|
@ -0,0 +1,55 @@
|
|||
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
|
||||
|
||||
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)
|
||||
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)
|
||||
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)
|
5
script/period.gd
Normal file
5
script/period.gd
Normal file
|
@ -0,0 +1,5 @@
|
|||
class_name Period
|
||||
extends Node
|
||||
|
||||
var begin = {}
|
||||
var end = {}
|
Loading…
Add table
Add a link
Reference in a new issue