add settings manager

This commit is contained in:
Crispy 2021-05-16 14:23:22 +02:00
parent 8466a37827
commit 31b6e199ff
8 changed files with 152 additions and 8 deletions

View file

@ -13,7 +13,6 @@ target = 1
overlay_scene = ExtResource( 2 )
offset_pos = Vector3( 0.05, -0.01, 0.15 )
offset_rot = Vector3( 0, -90, -135 )
fallback_to_hmd = true
[node name="OverlayInstance Time" parent="." instance=ExtResource( 1 )]
target = 1
@ -21,4 +20,3 @@ overlay_scene = ExtResource( 3 )
offset_pos = Vector3( -0.05, 0.02, 0.15 )
offset_rot = Vector3( 0, -90, -135 )
width_meters = 0.8
fallback_to_hmd = true

View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 CrispyPin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -0,0 +1,7 @@
[plugin]
name="SettingsManager"
description="Simple settings manager for godot"
author="CrispyPin"
version=""
script="settings_manager.gd"

View file

@ -0,0 +1,107 @@
extends Node
signal setting_changed # emitted with name, value
signal settings_loaded # emitted when settings are loaded from file, needs to be connected in _init()
const DEBUG_SETTINGS = true
const SETTINGS_PATH = "user://settings.json"
const SETTINGS_DEF = {
"example_1": {
"name": "Example Toggle",
"description": "resets every restart", # optional
"flags": ["no_save"], # optional
"type": "bool",
"default": false
},
"example_2": {
"name": "Example Number",
"type": "number",
"default": 42,
"min": 1, # optional
"max": 100, # optional
"step": 4 # optional
},
"example_3": {
"name": "Example text",
"type": "string",
"default": "hello world"
},
"example_4": {
"name": "Example Vector3",
"type": "Vector3",
"default": [1, 2, 3]
}
}
var _settings = {}
func _ready() -> void:
_init_settings()
load_settings()
emit_signal("settings_loaded")
save_settings()
func get_setting(key):
return _settings[key]
func set_setting(key, val):
if _settings[key] == val:
return
_settings[key] = val
emit_signal("setting_changed", key, val)
save_settings()
if DEBUG_SETTINGS:
print("Settings changed: ", key, " -> ", val)
func _init_settings():
for key in SETTINGS_DEF:
_settings[key] = SETTINGS_DEF[key].default
if DEBUG_SETTINGS:
print("Default settings: ", _settings)
func save_settings():
var to_save = {}
for key in _settings:
if not (SETTINGS_DEF[key].has("flags") and "no_save" in SETTINGS_DEF[key].flags):
to_save[key] = _settings[key]
var file = File.new()
file.open(SETTINGS_PATH, File.WRITE)
file.store_line(to_json(to_save))
file.close()
func load_settings() -> void:
var file = File.new()
if not file.file_exists(SETTINGS_PATH):
if DEBUG_SETTINGS:
print("No settings file exists, using defaults")
return
file.open(SETTINGS_PATH, File.READ)
var new_settings = parse_json(file.get_as_text())
file.close()
# in case the settings format has changed, this is better than just clonging blindly
for key in new_settings:
if _settings.has(key):
var value = new_settings[key]
match SETTINGS_DEF[key].type:
["Vector2", "vector2"]:
_settings[key] = Vector2(value[0], value[1])
["Vector3", "vector3"]:
_settings[key] = Vector3(value[0], value[1], value[2])
_:
_settings[key] = value
if DEBUG_SETTINGS:
print("Loaded settings from file")
print(_settings)

View file

@ -0,0 +1,10 @@
tool
extends EditorPlugin
func _enter_tree() -> void:
add_autoload_singleton("Settings", "res://addons/settings-manager/settings.gd")
func _exit_tree() -> void:
remove_autoload_singleton("Settings")

View file

@ -3,14 +3,14 @@
[ext_resource path="res://overlays/time.gd" type="Script" id=1]
[ext_resource path="res://fonts/OpenSans-Regular.ttf" type="DynamicFontData" id=2]
[sub_resource type="StyleBoxFlat" id=2]
[sub_resource type="StyleBoxFlat" id=1]
bg_color = Color( 0, 0, 0, 0.705882 )
expand_margin_left = 8.0
expand_margin_right = 8.0
expand_margin_top = 8.0
expand_margin_bottom = 8.0
[sub_resource type="DynamicFont" id=1]
[sub_resource type="DynamicFont" id=2]
size = 48
font_data = ExtResource( 2 )
@ -33,7 +33,7 @@ margin_top = -32.0
margin_right = 61.0
margin_bottom = 35.0
size_flags_horizontal = 0
custom_styles/panel = SubResource( 2 )
custom_styles/panel = SubResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}
@ -41,7 +41,7 @@ __meta__ = {
[node name="Label" type="Label" parent="ColorRect"]
margin_right = 121.0
margin_bottom = 67.0
custom_fonts/font = SubResource( 1 )
custom_fonts/font = SubResource( 2 )
text = "00:00"
align = 1
script = ExtResource( 1 )

View file

@ -19,10 +19,11 @@ config/icon="res://icon.png"
[autoload]
OverlayInit="*res://addons/openvr_overlay/overlay_init.gd"
Settings="*res://addons/settings-manager/settings.gd"
[editor_plugins]
enabled=PoolStringArray( "res://addons/openvr_overlay/plugin.cfg" )
enabled=PoolStringArray( "res://addons/openvr_overlay/plugin.cfg", "res://addons/settings-manager/plugin.cfg" )
[gdnative]

View file

@ -2,4 +2,4 @@ extends Node
func _ready() -> void:
OS.window_size = Vector2()
OS.window_size = Vector2(16, 16)