parent
78cc721580
commit
cb12fe5a2d
|
@ -4,11 +4,9 @@ extends Control
|
||||||
@export var month_scene: PackedScene
|
@export var month_scene: PackedScene
|
||||||
|
|
||||||
@onready var year_label = $CenterContainer/VBoxContainer/YearLabel
|
@onready var year_label = $CenterContainer/VBoxContainer/YearLabel
|
||||||
@onready var year = $CenterContainer/VBoxContainer/Year
|
@onready var months = $CenterContainer/VBoxContainer/Months
|
||||||
@onready var period_manager = $PeriodManager
|
|
||||||
|
|
||||||
var last_focused_day = null
|
var last_focused_day = null
|
||||||
var new_focused_day = null
|
|
||||||
var all_days = []
|
var all_days = []
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,20 +22,46 @@ func _ready():
|
||||||
month.month = i
|
month.month = i
|
||||||
month.day_toggled.connect(_on_day_toggled)
|
month.day_toggled.connect(_on_day_toggled)
|
||||||
month.fill_days.connect(_on_fill_days)
|
month.fill_days.connect(_on_fill_days)
|
||||||
year.add_child(month)
|
months.add_child(month)
|
||||||
|
|
||||||
|
if FileAccess.file_exists("user://savegame.save"):
|
||||||
|
load_days()
|
||||||
|
|
||||||
|
for month in months.get_children():
|
||||||
|
if month is Month:
|
||||||
for element in month.days.get_children():
|
for element in month.days.get_children():
|
||||||
if element is Day:
|
if element is Day:
|
||||||
all_days.append(element)
|
all_days.append(element)
|
||||||
|
|
||||||
|
|
||||||
func _on_day_toggled(date):
|
func load_days():
|
||||||
# period_manager.add(date)
|
var save_game = FileAccess.open("user://savegame.save", FileAccess.READ)
|
||||||
|
while save_game.get_position() < save_game.get_length():
|
||||||
|
var json_string = save_game.get_line()
|
||||||
|
var json = JSON.new()
|
||||||
|
|
||||||
|
var parse_result = json.parse(json_string)
|
||||||
|
if not parse_result == OK:
|
||||||
|
print("JSON parse error: %s in %s at line %s" % [json.get_error_message(), json_string, json.get_error_line()])
|
||||||
|
continue
|
||||||
|
|
||||||
|
var node_data = json.get_data()
|
||||||
|
var month = get_month(node_data["month"])
|
||||||
|
month.instantiate_day(node_data["day"], node_data["is_pressed"])
|
||||||
|
|
||||||
|
|
||||||
|
func get_month(month):
|
||||||
|
for child in months.get_children():
|
||||||
|
if child is Month and child.month == month:
|
||||||
|
return child
|
||||||
|
|
||||||
|
|
||||||
|
func _on_day_toggled(date):
|
||||||
# Update last focused day.
|
# Update last focused day.
|
||||||
var focused_node = get_viewport().gui_get_focus_owner()
|
var focused_node = get_viewport().gui_get_focus_owner()
|
||||||
if focused_node is Day:
|
if focused_node is Day:
|
||||||
last_focused_day = focused_node
|
last_focused_day = focused_node
|
||||||
|
save()
|
||||||
|
|
||||||
|
|
||||||
func _on_fill_days(shift_clicked_day):
|
func _on_fill_days(shift_clicked_day):
|
||||||
|
@ -64,4 +88,24 @@ func _on_fill_days(shift_clicked_day):
|
||||||
if toggle:
|
if toggle:
|
||||||
day.set_pressed_no_signal(true)
|
day.set_pressed_no_signal(true)
|
||||||
if last_day == day:
|
if last_day == day:
|
||||||
return
|
break
|
||||||
|
|
||||||
|
save()
|
||||||
|
|
||||||
|
|
||||||
|
func save():
|
||||||
|
var save_game = FileAccess.open("user://savegame.save", FileAccess.WRITE)
|
||||||
|
var saved_nodes = get_tree().get_nodes_in_group("persist")
|
||||||
|
|
||||||
|
for node in saved_nodes:
|
||||||
|
if node.scene_file_path.is_empty():
|
||||||
|
print("persistant node '%s' is not an instanced scene, skipped" % node.name)
|
||||||
|
continue
|
||||||
|
|
||||||
|
if !node.has_method("save"):
|
||||||
|
print("persistent node '%s' is missing a save() function, skipped" % node.name)
|
||||||
|
continue
|
||||||
|
|
||||||
|
var node_data = node.call("save")
|
||||||
|
var json_string = JSON.stringify(node_data)
|
||||||
|
save_game.store_line(json_string)
|
||||||
|
|
|
@ -35,5 +35,14 @@ func is_before(other_day):
|
||||||
return false
|
return false
|
||||||
|
|
||||||
|
|
||||||
|
func save():
|
||||||
|
return {
|
||||||
|
"year": date.year,
|
||||||
|
"month": date.month,
|
||||||
|
"day": date.day,
|
||||||
|
"is_pressed": is_pressed()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func dump():
|
func dump():
|
||||||
print("%s - %s - %s" % [date.year, date.month, date.day])
|
print("%s - %s - %s" % [date.year, date.month, date.day])
|
||||||
|
|
|
@ -11,7 +11,7 @@ var year: int
|
||||||
var month: int
|
var month: int
|
||||||
|
|
||||||
@onready var month_label = $HBoxContainer/MonthLabel
|
@onready var month_label = $HBoxContainer/MonthLabel
|
||||||
@onready var days = $HBoxContainer/DaysGridContainer
|
@onready var days = $HBoxContainer/Days
|
||||||
|
|
||||||
|
|
||||||
# Called when the node enters the scene tree for the first time.
|
# Called when the node enters the scene tree for the first time.
|
||||||
|
@ -26,13 +26,24 @@ func _ready():
|
||||||
for i in range(first_day.weekday):
|
for i in range(first_day.weekday):
|
||||||
var empty_label = Label.new()
|
var empty_label = Label.new()
|
||||||
days.add_child(empty_label)
|
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):
|
for i in range(1, days_in_month + 1):
|
||||||
var day = day_scene.instantiate()
|
instantiate_day(i, false)
|
||||||
day.set_date(year, month, i)
|
|
||||||
day.text = "%s" % [i]
|
|
||||||
day.day_toggled.connect(_on_day_toggled)
|
func instantiate_day(day, is_pressed):
|
||||||
day.fill_days.connect(_on_fill_days)
|
var new_day = day_scene.instantiate()
|
||||||
days.add_child(day)
|
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):
|
func _on_day_toggled(date):
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
class_name Period
|
|
||||||
extends Node
|
|
||||||
|
|
||||||
var begin = {}
|
|
||||||
var end = {}
|
|
|
@ -1,24 +0,0 @@
|
||||||
extends Node
|
|
||||||
|
|
||||||
var periods = []
|
|
||||||
var last_period = null
|
|
||||||
|
|
||||||
|
|
||||||
func add(date):
|
|
||||||
if last_period != null:
|
|
||||||
last_period.end = date
|
|
||||||
last_period = null
|
|
||||||
dump()
|
|
||||||
return
|
|
||||||
|
|
||||||
# If no new period has begun
|
|
||||||
var period = Period.new()
|
|
||||||
period.begin = date
|
|
||||||
periods.append(period)
|
|
||||||
last_period = period
|
|
||||||
dump()
|
|
||||||
|
|
||||||
|
|
||||||
func dump():
|
|
||||||
for period in periods:
|
|
||||||
print("%s - %s" % [period.begin, period.end])
|
|
Loading…
Reference in a new issue