change to using paths to refer to overlay types for better future expansion

This commit is contained in:
Crispy 2021-06-29 01:20:53 +02:00
parent b15c00f5f0
commit 01c11ffd32
7 changed files with 56 additions and 42 deletions

View file

@ -11,8 +11,8 @@ signal offset_changed
const TARGETS = ["head", "left", "right", "world"]
export (String, "head", "left", "right", "world") var target = "left" setget set_target
export var overlay_scene: PackedScene = \
preload("res://addons/openvr_overlay/MissingOverlay.tscn") setget set_overlay_scene
export var overlay_scene: PackedScene# = \
# preload("res://addons/openvr_overlay/MissingOverlay.tscn") setget set_overlay_scene
export var width_meters := 0.4 setget set_width_in_meters
export var alpha := 1.0 setget set_alpha
export var add_grabbing := true # add grabbing module
@ -31,7 +31,8 @@ var current_target: String = "world" setget _set_current_target
var fallback = ["left", "right", "head"] # TODO setget that updates tracking (not important)
var interaction_handler: Node
var overlay_visible := true setget set_overlay_visible
var type := "main"
#var type := "main"
var path := "res://special_overlays/MainOverlay.tscn" setget set_path
onready var container = $OverlayViewport/Container
@ -60,7 +61,6 @@ func _ready() -> void:
call_deferred("update_offset")
func add_cursor():
interaction_handler.add_child(load("res://addons/openvr_overlay/OverlayCursor.tscn").instance())
@ -149,8 +149,9 @@ func set_width_in_meters(width: float) -> void:
emit_signal("width_changed")
func set_overlay_scene(scene: PackedScene) -> void:
overlay_scene = scene
func set_path(new: String) -> void:
path = new
overlay_scene = load(path)
if not container:
return
if container.get_child_count() > 0:

View file

@ -20,29 +20,34 @@ func _load_overlays():
return
loaded = true
print("Loading in overlays")
for key in Settings.s.overlays:
if not key == "MainOverlay":
add_overlay(Settings.s.overlays[key].type, key)
for name in Settings.s.overlays:
if not name == "MainOverlay":
add_overlay(name)
func add_overlay(type, name):
print("Adding overlay '", name, "' of type '", type, "'")
var scene = load("res://overlays/" + type + ".tscn")
if not scene:
print("Unknown overlay type: '", type, "'")
scene = load("res://special_overlays/UnknownType.tscn")
func add_overlay(name):
print("Adding overlay '", name, "'")
# var scene = load("res://overlays/" + type + ".tscn")
# if not scene:
# print("Unknown overlay type: '", type, "'")
# scene = load("res://special_overlays/UnknownType.tscn")
var instance = preload("res://addons/openvr_overlay/OverlayInstance.tscn").instance()
instance.name = name
instance.overlay_scene = scene
instance.type = type
# instance.overlay_scene = scene
# instance.type = type
instance.add_child(preload("res://OverlaySettingsSync.tscn").instance())
add_child(instance)
emit_signal("added_overlay", name)
func create_overlay(path, name):
Settings.s.overlays[name] = {}
Settings.s.overlays[name].path = path
add_overlay(name)
func remove_overlay(name):
print("Removing overlay '", name, "'")
var to_remove = get_node(name)

View file

@ -2,4 +2,4 @@ extends Control
func _ready() -> void:
$Label.text += get_viewport().get_parent().type
$Label.text += get_viewport().get_parent().path

View file

@ -26,7 +26,7 @@ func save_all() -> void:
return
if not Settings.s.overlays.has(p.name):
Settings.s.overlays[p.name] = {}
_save_prop("type", p.type)
_save_prop("path", p.path)
_save_prop("visible", p.overlay_visible)
_save_prop("width", p.width_meters)
_save_prop("alpha", p.alpha)
@ -44,6 +44,8 @@ func load_all() -> void:
if Settings.s.overlays.has(p.name):
var new = Settings.s.overlays[p.name]
if new.has("path"):
p.path = new.path
if new.has("visible"):
p.overlay_visible = new.visible
if new.has("width"):

View file

@ -1,6 +1,6 @@
extends Node
const PATH = "user://overlay_data.json"
const PATH = "user://overlay_data_DEV.json"
const DEF = {
"grab_mode": {
"name": "Grab mode",
@ -16,10 +16,10 @@ const DEF = {
"definition": {
"type": "dict",
"definition": {
"type": {
"name": "Overlay type",
"path": {
"name": "Overlay scene path",
"type": "string",
"default": "UI_demo"
"default": "res://special_overlays/UnknownType.tscn"
},
"visible": {
"name": "Overlay Visible",
@ -71,7 +71,7 @@ const DEF = {
},
"default": {
"MainOverlay": {
"type": "MainOverlay",
"path": "res://special_overlays/MainOverlay.tscn",
"visible": true,
"width": 0.4,
"alpha": 1.0,

View file

@ -2,28 +2,28 @@ extends Control
signal add_menu_closed
var types: Array
var paths: Array
func _ready() -> void:
visible = false
types = get_overlay_types()
for t in types:
paths = get_overlay_paths()
for p in paths:
var btn = preload("res://ui/AddOverlayButton.tscn").instance()
btn.text = t
btn.connect("pressed", self, "add_overlay", [t])
btn.text = path_to_name(p)
btn.connect("pressed", self, "add_overlay", [p])
$MarginContainer/VBoxContainer.add_child(btn)
func add_overlay(type):
OverlayManager.add_overlay(type, type + " " + str(randi()%1000))
func add_overlay(path):
OverlayManager.create_overlay(path, path_to_name(path) + " " + str(randi()%1000))
visible = false
emit_signal("add_menu_closed")
func get_overlay_types(path := "res://overlays/"):
func get_overlay_paths(root := "res://overlays/"):
var found = []
var dir = Directory.new()
if dir.open(path) == OK:
if dir.open(root) == OK:
dir.list_dir_begin(true)
var file_name = dir.get_next()
while file_name != "":
@ -31,8 +31,11 @@ func get_overlay_types(path := "res://overlays/"):
# TODO make recursive, must include folder as prefix for type
pass
else:
found.append(file_name.split(".")[0])
found.append(root + file_name)
file_name = dir.get_next()
else:
print("An error occurred when trying to access the path ", path)
print("An error occurred when trying to access ", root)
return found
func path_to_name(path: String) -> String:
return path.split("/")[-1].split(".")[0]

View file

@ -16,13 +16,15 @@ func _ready() -> void:
$MoreOptions/Container/List/Target.selected = overlay.TARGETS.find(overlay.target)
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"
#TODO use signal to activate warning
# $BasicOptions/List/Warning.visible = overlay.overlay_scene == preload("res://special_overlays/UnknownType.tscn")
$BasicOptions/List/Warning/WarningInfo/Label.text = overlay.path + "\nnot found"
func _on_Visibility_toggled(state: bool) -> void:
if overlay.type and overlay.type != "main":
overlay.overlay_visible = state
# TODO check always visible flag
# if overlay.type and overlay.type != "main":
overlay.overlay_visible = state
func _on_Grab_toggled(state: bool) -> void:
@ -38,8 +40,9 @@ func _overlay_visible_changed(state: bool):
func _on_Remove_pressed() -> void:
if overlay.type and overlay.type != "main":
OverlayManager.remove_overlay(overlay_name)
# TODO check stay flag
# if overlay.type and overlay.type != "main":
OverlayManager.remove_overlay(overlay_name)
func _on_Reset_pressed() -> void: