refactor overlay saving/loading

This commit is contained in:
Crispy 2021-06-15 18:49:24 +02:00
parent 2fefd5a03b
commit f0b0bc84b5
7 changed files with 84 additions and 61 deletions

View file

@ -1,6 +1,7 @@
[gd_scene load_steps=5 format=2]
[gd_scene load_steps=6 format=2]
[ext_resource path="res://addons/openvr_overlay/overlay_instance.gd" type="Script" id=1]
[ext_resource path="res://overlay_settings_sync.gd" type="Script" id=2]
[ext_resource path="res://addons/godot-openvr/OpenVROverlay.gdns" type="Script" id=3]
[sub_resource type="StyleBoxFlat" id=1]
@ -64,3 +65,6 @@ texture = SubResource( 2 )
expand = true
stretch_mode = 5
flip_v = true
[node name="OverlaySettingsSync" type="Node" parent="."]
script = ExtResource( 2 )

View file

@ -73,7 +73,6 @@ func finish_move():
# revert target
_overlay.update_current_target()
_overlay.save_settings()
_interaction._update_target()
_interaction.pause_triggers = false

View file

@ -1,10 +1,12 @@
extends Spatial
signal type_changed
signal overlay_visible_changed
signal width_changed
signal alpha_changed
signal offset_changed
signal target_changed
signal overlay_visibility_changed
signal fallback_changed
signal offset_changed
const TARGETS = ["head", "left", "right", "world"]
export (String, "head", "left", "right", "world") var target = "left" setget set_target
@ -18,7 +20,7 @@ export var add_cursor := false # add cursor module
var _tracker_id := 0
var _offsets:Dictionary = {
"head": {"pos": Vector3(0, 0, -0.5), "rot": Quat()},
"head": {"pos": Vector3(0, 0, -0.35), "rot": Quat()},
"left": {"pos": Vector3(), "rot": Quat()},
"right": {"pos": Vector3(), "rot": Quat()},
"world": {"pos": Vector3(0,1,0), "rot": Quat()},
@ -43,7 +45,6 @@ func _ready() -> void:
$VROverlayViewport.size = OverlayInit.ovr_interface.get_render_targetsize()
set_notify_transform(true)
load_settings()
if add_cursor or add_grabbing:
interaction_handler = load("res://addons/openvr_overlay/OverlayInteraction.tscn").instance()
add_child(interaction_handler)
@ -66,47 +67,6 @@ func add_grab():
interaction_handler.add_child(load("res://addons/openvr_overlay/OverlayGrab.tscn").instance())
func load_settings():
if Settings.s.overlays.has(name):
var loaded = Settings.s.overlays[name]
if loaded.has("fallback"):
fallback = loaded.fallback
if loaded.has("target"):
set_target(loaded.target)
if loaded.has("offsets"):
for t_key in loaded.offsets:
var t_offset = loaded.offsets[t_key]
_offsets[t_key].pos = t_offset.pos
_offsets[t_key].rot = t_offset.rot
update_offset()
if loaded.has("width"):
set_width_in_meters(loaded.width)
if loaded.has("visible"):
set_overlay_visible(loaded.visible)
if loaded.has("alpha"):
set_alpha(loaded.alpha)
else:
save_settings()
func save_settings():
if not Settings.s.overlays.has(name):
Settings.s.overlays[name] = {}
Settings.s.overlays[name].target = target
Settings.s.overlays[name].width = width_meters
Settings.s.overlays[name].alpha = alpha
Settings.s.overlays[name].fallback = fallback
Settings.s.overlays[name].type = type
if not Settings.s.overlays[name].has("offsets"):
Settings.s.overlays[name]["offsets"] = {}
for t_key in TARGETS:
Settings.s.overlays[name].offsets[t_key] = _offsets[t_key]
func update_tracker_id():
_tracker_id = -1
match current_target:
@ -150,9 +110,7 @@ func update_current_target():
func set_overlay_visible(state: bool):
overlay_visible = state
$VROverlayViewport.overlay_visible = state
Settings.s.overlays[name].visible = state
emit_signal("overlay_visibility_changed", state)
save_settings()
emit_signal("overlay_visible_changed", state)
func _tracker_changed(tracker_name: String, type: int, id: int):
@ -210,7 +168,6 @@ func reset_offset() -> void:
if current_target == "head":
_offsets[current_target].pos.z = -0.5
update_offset()
save_settings()
func _notification(what: int) -> void:

View file

@ -8,8 +8,10 @@ var DEBUG_SETTINGS = false
var SETTINGS_PATH = "user://overlay_data.json"
const SETTINGS_DEF = preload("res://addons/settings-manager/settings_definition.gd").DEF
var s: Dictionary = {}
var _saved_hash: int
var has_loaded := false
var s := {}
var _saved_str: String
func _ready() -> void:
_init_settings()
@ -18,7 +20,10 @@ func _ready() -> void:
func _on_SaveTimer_timeout() -> void:
if s.hash() != _saved_hash:
var new_s = str(s)
if new_s != _saved_str:
_saved_str = new_s
print("Saving to ", SETTINGS_PATH)
force_save()
@ -55,8 +60,6 @@ func force_save():
file.close()
emit_signal("settings_saved")
_saved_hash = s.hash()
if DEBUG_SETTINGS:
print("Settings saved to file")
@ -100,10 +103,10 @@ func load_settings() -> void:
for key in new_settings:
s[key] = _load_sub_setting(new_settings[key], SETTINGS_DEF[key])
emit_signal("settings_loaded")
if DEBUG_SETTINGS:
print("Loaded settings from file")
# print(s)
emit_signal("settings_loaded")
has_loaded = true
func _load_sub_setting(val, def):

View file

@ -39,7 +39,7 @@ func add_overlay(type, name):
instance.overlay_scene = scene
instance.type = type
add_child(instance)
instance.update_offset()
# instance.update_offset()
emit_signal("added_overlay", name)

View file

@ -0,0 +1,60 @@
extends Node
onready var p = get_parent()
var loaded := false
func _ready() -> void:
p = get_parent()
call_deferred("load_all")
p.connect("type_changed", self, "save_all")
p.connect("overlay_visible_changed", self, "save_all")
p.connect("width_changed", self, "save_all")
p.connect("alpha_changed", self, "save_all")
p.connect("target_changed", self, "save_all")
p.connect("fallback_changed", self, "save_all")
p.connect("offset_changed", self, "save_all")
func save_all(_args=null) -> void:
if not loaded:
return
if not Settings.s.overlays.has(p.name):
Settings.s.overlays[p.name] = {}
_save_prop("type", p.type)
_save_prop("visible", p.overlay_visible)
_save_prop("width", p.width_meters)
_save_prop("alpha", p.alpha)
_save_prop("target", p.target)
_save_prop("fallback", p.fallback)
_save_prop("offsets", p._offsets.duplicate(true))
func _save_prop(prop_name: String, prop_value) -> void:
Settings.s.overlays[p.name][prop_name] = prop_value
func load_all() -> void:
if Settings.s.overlays.has(p.name):
var loaded = Settings.s.overlays[p.name]
# type is assigned at creation
if loaded.has("visible"):
p.overlay_visible = loaded.visible
if loaded.has("width"):
p.width_meters = loaded.width
if loaded.has("alpha"):
p.alpha = loaded.alpha
if loaded.has("target"):
p.target = loaded.target
if loaded.has("fallback"):
p.fallback = loaded.fallback
if loaded.has("offsets"):# thorough in case some values are missing in file
for t_key in loaded.offsets:
var offset = loaded.offsets[t_key]
p.set_offset(t_key, offset.pos, offset.rot)
else:
print("FAILED")
save_all()
loaded = true

View file

@ -14,7 +14,7 @@ func _ready() -> void:
$BasicOptions/Label.text = overlay_name
name = overlay_name
$MoreOptions/Container/List/Target.selected = overlay.TARGETS.find(overlay.target)
overlay.connect("overlay_visibility_changed", self, "_overlay_visibility_changed")
overlay.connect("overlay_visible_changed", self, "_overlay_visible_changed")
$BasicOptions/List/Warning.visible = overlay.overlay_scene == preload("res://special_overlays/UnknownType.tscn")
$BasicOptions/List/Warning/WarningInfo/Label.text = overlay.type + "\nnot found"
@ -29,7 +29,7 @@ func _on_Grab_toggled(state: bool) -> void:
overlay.get_node("OverlayInteraction").grab_mode = state
func _overlay_visibility_changed(state: bool):
func _overlay_visible_changed(state: bool):
$BasicOptions/List/Visibility.pressed = state
if state:
$BasicOptions/List/Visibility.icon = preload("res://icons/visible.svg")