-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved event handling from T5Manager to T5Interface Add T5ManagerBase as an interface stub Refactored the event handling in T5Interface to call out to T5ManagerBase T5Manager extends T5ManagerBase now Adds signals for adding and removing glasses scenes
- Loading branch information
1 parent
06bf594
commit a594715
Showing
7 changed files
with
209 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,149 +1,50 @@ | ||
class_name T5Manager extends Node | ||
class_name T5Manager extends T5ManagerBase | ||
|
||
## The T5Manager node should be added to your | ||
## main scene and will manage when glasses | ||
## and wands are detected. | ||
## | ||
## This should be persistent. | ||
|
||
signal glasses_available(glasses_id : String) | ||
signal glasses_reserved(glasses_id : String) | ||
signal glasses_dropped(glasses_id : String) | ||
signal glasses_scene_was_added(glasses : T5GlassesBase) | ||
signal glasses_scene_will_be_removed(glasses : T5GlassesBase) | ||
|
||
const xr_origin_node := ^"Origin" | ||
const xr_camera_node := ^"Origin/Camera" | ||
const wand_node_list := [^"Origin/Wand_1", ^"Origin/Wand_2", ^"Origin/Wand_3", ^"Origin/Wand_4"] | ||
const wand_node_list := [^"Origin/Wand_1", ^"Origin/Wand_2"] | ||
|
||
var tilt_five_xr_interface: TiltFiveXRInterface | ||
|
||
@export var automatically_start : bool = true | ||
@export var glasses_scene : PackedScene = preload("res://addons/tiltfive/scenes/T5GlassesBase.tscn") | ||
|
||
var reserved_glasses: Dictionary | ||
|
||
# We'll add our glasses scenes as children of this node | ||
var glasses_node: Node3D | ||
|
||
# Called when the manager is loaded and added to our scene | ||
func _enter_tree(): | ||
tilt_five_xr_interface = T5Interface.get_tilt_five_xr_interface() | ||
|
||
if tilt_five_xr_interface: | ||
tilt_five_xr_interface.glasses_event.connect(on_glasses_event) | ||
|
||
# Called when the manager is removed | ||
func _exit_tree(): | ||
if tilt_five_xr_interface: | ||
tilt_five_xr_interface.glasses_event.disconnect(on_glasses_event) | ||
|
||
tilt_five_xr_interface = null | ||
|
||
# Called when our scene is fully setup | ||
func _ready(): | ||
# Create a node on our parent under which we create our glasses scenes | ||
func _ready(): | ||
glasses_node = Node3D.new() | ||
glasses_node.name = "TiltFiveGlasses" | ||
get_parent().add_child.call_deferred(glasses_node) | ||
|
||
if not tilt_five_xr_interface: | ||
return | ||
|
||
if automatically_start and !tilt_five_xr_interface.is_initialized(): | ||
tilt_five_xr_interface.initialize() | ||
|
||
func start_service() -> bool: | ||
if not tilt_five_xr_interface: | ||
return false | ||
|
||
return tilt_five_xr_interface.initialize() | ||
|
||
func has_reserved_glasses() -> bool: | ||
for glasses in reserved_glasses: | ||
if reserved_glasses[glasses]: | ||
return true | ||
return false | ||
|
||
func reserve_glasses(glasses_id : StringName, display_name := "") -> void: | ||
if not reserved_glasses.has(glasses_id): | ||
print_verbose("Warning: Tilt Five glasses id ", glasses_id, " does not exist") | ||
return | ||
if reserved_glasses[glasses_id]: | ||
print_verbose("Warning: Tilt Five glasses ", glasses_id, " already reserved") | ||
return | ||
if display_name.length() == 0: | ||
display_name = ProjectSettings.get_setting_with_override("xr/tilt_five/default_display_name") | ||
tilt_five_xr_interface.reserve_glasses(glasses_id, display_name) | ||
|
||
func start_display(glasses_id : StringName, viewport : SubViewport): | ||
var xr_origin = viewport.get_node(xr_origin_node) | ||
tilt_five_xr_interface.start_display(glasses_id, viewport, xr_origin) | ||
var t5_camera := viewport.get_node_or_null(xr_camera_node) as T5Camera3D | ||
if t5_camera: | ||
t5_camera.tracker = "/user/%s/head" % glasses_id | ||
for idx in 4: | ||
var controller = viewport.get_node_or_null(wand_node_list[idx]) as T5Controller3D | ||
if controller: | ||
controller.tracker = "/user/%s/wand_%d" % [glasses_id, idx + 1] | ||
|
||
func node_name_from_glasses_id(glasses_id: String) -> String: | ||
return "Glasses_" + glasses_id.replace("-", "_") | ||
|
||
func on_glasses_event(glasses_id, event_num): | ||
match event_num: | ||
TiltFiveXRInterface.E_AVAILABLE: | ||
print_verbose(glasses_id, " E_AVAILABLE") | ||
if not reserved_glasses.has(glasses_id): | ||
reserved_glasses[glasses_id] = false | ||
|
||
# If we're managing our glasses scene, reserve our glasses | ||
if glasses_scene: | ||
reserve_glasses(glasses_id) | ||
|
||
# Let others who are interested know | ||
glasses_available.emit(glasses_id) | ||
|
||
TiltFiveXRInterface.E_UNAVAILABLE: | ||
print_verbose(glasses_id, " E_UNAVAILABLE") | ||
|
||
# Let others who are interested know | ||
reserved_glasses.erase(glasses_id) | ||
|
||
TiltFiveXRInterface.E_RESERVED: | ||
print_verbose(glasses_id, " E_RESERVED") | ||
|
||
reserved_glasses[glasses_id] = true | ||
|
||
# If we're managing our glasses scene, instance our scene | ||
if glasses_scene: | ||
var gview = glasses_scene.instantiate() | ||
gview.name = node_name_from_glasses_id(glasses_id) | ||
glasses_node.add_child(gview) | ||
start_display(glasses_id, gview) | ||
|
||
# Let others who are interested know | ||
glasses_reserved.emit(glasses_id) | ||
|
||
TiltFiveXRInterface.E_DROPPED: | ||
print_verbose(glasses_id, " E_DROPPED") | ||
|
||
var node_name = node_name_from_glasses_id(glasses_id) | ||
var gview = glasses_node.get_node_or_null(node_name) | ||
if gview: | ||
tilt_five_xr_interface.stop_display(glasses_id) | ||
glasses_node.remove_child(gview) | ||
gview.queue_free() | ||
|
||
if reserved_glasses.get(glasses_id, false): | ||
reserved_glasses[glasses_id] = false | ||
glasses_dropped.emit(glasses_id) | ||
|
||
TiltFiveXRInterface.E_TRACKING: | ||
var gbt = tilt_five_xr_interface.get_gameboard_type(glasses_id) | ||
print_verbose(glasses_id, " E_TRACKING, Gameboard size = ", tilt_five_xr_interface.get_gameboard_extents(gbt)) | ||
|
||
TiltFiveXRInterface.E_NOT_TRACKING: | ||
print_verbose(glasses_id, " E_NOT_TRACKING") | ||
|
||
_: | ||
print_verbose(glasses_id, " - unknown event: ", event_num) | ||
|
||
func create_glasses_scene(glasses_id : String) -> Node: | ||
var gview = glasses_scene.instantiate() | ||
glasses_node.add_child(gview) | ||
glasses_scene_was_added.emit(gview) | ||
return gview | ||
|
||
func release_glasses_scene(glasses_scene : Node) -> void: | ||
glasses_scene_will_be_removed.emit(glasses_scene) | ||
glasses_node.remove_child(glasses_scene) | ||
glasses_scene.queue_free() | ||
|
||
func get_glasses_scene_viewport(glasses_scene : Node) -> SubViewport: | ||
return glasses_scene as SubViewport | ||
|
||
func get_glasses_scene_origin(glasses_scene : Node) -> T5Origin3D: | ||
return glasses_scene.get_node(xr_origin_node) | ||
|
||
func get_glasses_scene_camera(glasses_scene : Node) -> Camera3D: | ||
return glasses_scene.get_node(xr_camera_node) | ||
|
||
func get_glasses_scene_wand(glasses_scene : Node, wand_num : int) -> T5Controller3D: | ||
if wand_num < wand_node_list.size(): | ||
return glasses_scene.get_node_or_null(wand_node_list[wand_num]) as T5Controller3D | ||
return null | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
class_name T5ManagerBase extends Node | ||
|
||
## The T5ManagerBase derived node should be added to your | ||
## These functions must be overridden | ||
## | ||
## create_glasses_scene | ||
## release_glasses_scene | ||
## get_glasses_scene_viewport | ||
## get_glasses_scene_origin | ||
## get_glasses_scene_camera | ||
## get_glasses_scene_wand | ||
## | ||
## The derived node should be persistent. | ||
|
||
# Called when the manager is loaded and added to our scene | ||
func _enter_tree(): | ||
T5Interface.t5_manager = self | ||
|
||
# Called when the manager is removed | ||
func _exit_tree(): | ||
T5Interface.t5_manager = null | ||
|
||
func should_use_glasses(glasses_id : String) -> bool: | ||
return true | ||
|
||
func get_glasses_display_name(glasses_id : String) -> String: | ||
return ProjectSettings.get_setting_with_override("xr/tilt_five/default_display_name") | ||
|
||
func create_glasses_scene(glasses_id : String) -> Node: | ||
push_error("create_glasses_scene not implemented in T5ManagerBase derived class") | ||
return null | ||
|
||
func release_glasses_scene(glasses_scene : Node) -> void: | ||
push_error("destroy_glasses_scene not implemented in T5ManagerBase derived class") | ||
|
||
func get_glasses_scene_viewport(glasses_scene : Node) -> SubViewport: | ||
push_error("get_glasses_scene_viewport not implemented in T5ManagerBase derived class") | ||
return null | ||
|
||
func get_glasses_scene_origin(glasses_scene : Node) -> T5Origin3D: | ||
push_error("get_glasses_scene_origin not implemented in T5ManagerBase derived class") | ||
return null | ||
|
||
func get_glasses_scene_camera(glasses_scene : Node) -> Camera3D: | ||
push_error("get_glasses_scene_camera not implemented in T5ManagerBase derived class") | ||
return null | ||
|
||
func get_glasses_scene_wand(glasses_scene : Node, wand_num : int) -> T5Controller3D: | ||
push_error("get_glasses_scene_wand not implemented in T5ManagerBase derived class") | ||
return null | ||
|
||
func set_glasses_scene_gameboard_type(glasses_scene : Node, gameboard_type : T5Interface.GameboardType) -> void: | ||
pass | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
extends Node3D | ||
|
||
func _on_t5_manager_glasses_scene_was_added(glasses): | ||
print("Scene ", glasses.name, " added") | ||
|
||
func _on_t5_manager_glasses_scene_will_be_removed(glasses): | ||
print("Scene ", glasses.name, " removed") |
Oops, something went wrong.