Skip to content

Commit

Permalink
Adding a runtime selection dropdown into the editor
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiaanOlij committed Mar 5, 2021
1 parent fa6bf66 commit 18fde6a
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 0 deletions.
76 changes: 76 additions & 0 deletions demo/addons/godot-openxr/editor/OpenXRRunSelect.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
tool
extends OptionButton

var available_runtimes : Array = Array()
onready var platform = OS.get_name()

var home_folder = ''

func _parse_path(p_path):
# we might want to add more stuff here at some point
p_path = p_path.replace("~", home_folder)
return p_path

func _update_tooltip():
if selected > 0:
var i = get_item_id(selected)
hint_tooltip = _parse_path(available_runtimes[i]["path"])
else:
hint_tooltip = "Select the OpenXR runtime test your project with"

# Called when the node enters the scene tree for the first time.
func _ready():
var current_runtime = 0

# Parse the users home folder
home_folder = OS.get_environment("HOME")
if home_folder == '':
home_folder = OS.get_environment("HOMEDRIVE") + OS.get_environment("HOMEPATH")

# read our json file, may have entries for multiple platforms, we'll filter them later
var f = File.new()
if (f.open("res://addons/godot-openxr/runtimes.json", File.READ)) == OK:
var json = JSON.parse(f.get_as_text())
available_runtimes = json.result as Array
f.close()

# check what our current value is
var current_path = OS.get_environment("XR_RUNTIME_JSON")

if available_runtimes.size() > 0:
# reset our dropdown if applicable
clear()
add_item("Default", -1)

# check which runtimes are actually available
var dir = Directory.new()
var index = 0
for i in available_runtimes.size():
var runtime = available_runtimes[i]
var path = _parse_path(runtime["path"])
if dir.file_exists(path):
add_item(runtime["name"], i)
index = index + 1
if path == current_path:
current_runtime = index

selected = current_runtime
_update_tooltip()

visible = true
else:
# I guess nothing supported on this platform
visible = false

func _on_OpenXRRunSelect_item_selected(index):
# this need latest 3.2.4
if index == 0:
print("Returning to default")
OS.set_environment("XR_RUNTIME_JSON", "")
else:
var i = get_item_id(index)
var runtime = _parse_path(available_runtimes[i]["path"])
print("Switching to " + runtime)
OS.set_environment("XR_RUNTIME_JSON", runtime)

_update_tooltip()
14 changes: 14 additions & 0 deletions demo/addons/godot-openxr/editor/OpenXRRunSelect.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[gd_scene load_steps=2 format=2]

[ext_resource path="res://addons/godot-openxr/editor/OpenXRRunSelect.gd" type="Script" id=1]

[node name="OpenXRRunSelect" type="OptionButton"]
text = "Default"
items = [ "Default", null, false, 0, null, "SteamVR", null, false, 2, null, "Oculus", null, false, 3, null, "Microsoft MR", null, false, 4, null ]
selected = 0
script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}

[connection signal="item_selected" from="." to="." method="_on_OpenXRRunSelect_item_selected"]
7 changes: 7 additions & 0 deletions demo/addons/godot-openxr/plugin.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[plugin]

name="Godot OpenXR"
description="Godot OpenXR plugin"
author="Christoph Haag and Bastiaan Olij"
version="1.0.1"
script="plugin.gd"
14 changes: 14 additions & 0 deletions demo/addons/godot-openxr/plugin.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
tool
extends EditorPlugin

var openxr_run_select = null

func _enter_tree():
openxr_run_select = preload("res://addons/godot-openxr/editor/OpenXRRunSelect.tscn").instance()
add_control_to_container(CONTAINER_TOOLBAR, openxr_run_select)

func _exit_tree():
if openxr_run_select:
remove_control_from_container(EditorPlugin.CONTAINER_TOOLBAR, openxr_run_select)
openxr_run_select.queue_free()
openxr_run_select = null
22 changes: 22 additions & 0 deletions demo/addons/godot-openxr/runtimes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"name": "Monado",
"path": "/usr/share/openxr/1/openxr_monado.json"
},
{
"name": "SteamVR",
"path": "~/.steam/steam/steamapps/common/SteamVR/steamxr_linux64.json"
},
{
"name": "SteamVR",
"path": "C:\\Program Files (x86)\\Steam\\steamapps\\common\\SteamVR\\steamxr_win64.json"
},
{
"name": "Oculus",
"path": "C:\\Program Files\\Oculus\\Support\\oculus-runtime\\oculus_openxr_64.json"
},
{
"name": "Microsoft MR",
"path": "C:\\WINDOWS\\system32\\MixedRealityRuntime.json"
}
]
4 changes: 4 additions & 0 deletions demo/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ config/icon="res://icon.png"

window/vsync/use_vsync=false

[editor_plugins]

enabled=PoolStringArray( "res://addons/godot-openxr/plugin.cfg" )

[gdnative]

singletons=[ "res://addons/godot-openxr/godot_openxr.gdnlib" ]
Expand Down

0 comments on commit 18fde6a

Please sign in to comment.