Skip to content

Commit

Permalink
Added register methods for Editor-Only Singletons
Browse files Browse the repository at this point in the history
Added fetch_editor() for fetching GDScriptNativeClass
Added notification when trying to access editor-only class at runtime
Added documentation
Removed "_extract_name_from_path()" and used "trim_prefix()" instead
  • Loading branch information
xDGameStudios authored and DiasFranciscoA committed Mar 18, 2019
1 parent bfc54a8 commit 568355c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
25 changes: 25 additions & 0 deletions addons/godot-next/global/singletons.gd
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ static func fetchs(p_name: String) -> Object:
return fetch(ct.res)
return null

# Returns an editor-only singleton by its class name
static func fetch_editor(p_class: GDScriptNativeClass) -> Object:
if not Engine.editor_hint:
push_warning("Cannot access '%s' (editor-only class) at runtime." % p_class.get_class())
return null

var cache: Dictionary = SINGLETON_CACHE.get_cache()
if cache.has(p_class):
return cache[p_class]
return null

# Remove a singleton from the cache and any paths associated with it.
static func erase(p_script: Script) -> bool:
var cache: Dictionary = SINGLETON_CACHE.get_cache()
Expand Down Expand Up @@ -64,3 +75,17 @@ static func save_all() -> void:

static func _get_persistent_path(p_script: Script):
return p_script.get("SELF_RESOURCE")

# Register all editor-only singletons
static func _register_editor_singletons(plugin: EditorPlugin):
var cache: Dictionary = SINGLETON_CACHE.get_cache()

cache[UndoRedo] = plugin.get_undo_redo()

cache[EditorInterface] = plugin.get_editor_interface()

cache[ScriptEditor] = plugin.get_editor_interface().get_script_editor()
cache[EditorSelection] = plugin.get_editor_interface().get_selection()
cache[EditorSettings] = plugin.get_editor_interface().get_editor_settings()
cache[EditorFileSystem] = plugin.get_editor_interface().get_resource_filesystem()
cache[EditorResourcePreview] = plugin.get_editor_interface().get_resource_previewer()
2 changes: 2 additions & 0 deletions addons/godot-next/godot_next_plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const DelegationInspectorPlugin = preload("res://addons/godot-next/inspector_plu
var delegation_inspector_plugin

func _enter_tree() -> void:
Singletons._register_editor_singletons(self)

delegation_inspector_plugin = DelegationInspectorPlugin.new()
add_inspector_plugin(delegation_inspector_plugin)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ func _init() -> void:

func _get(p_property: String):
if p_property.begins_with(DATA_PREFIX):
var index := int(_extract_name_from_path(p_property, DATA_PREFIX + "item_"))
var index := int(p_property.trim_prefix(DATA_PREFIX + "item_"))
return _data[index] if index < _data.size() else null
return null

func _set(p_property, p_value):
if p_property.begins_with(DATA_PREFIX):
var index := int(_extract_name_from_path(p_property, DATA_PREFIX + "item_"))
var index := int(p_property.trim_prefix(DATA_PREFIX + "item_"))
if not p_value:
_data.remove(index)
property_list_changed_notify()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ var _class_type: ClassType = ClassType.new()
##### NOTIFICATIONS #####

func _get(p_property: String):
match _extract_name_from_path(p_property, SETUP_PREFIX):
match p_property.trim_prefix(SETUP_PREFIX):
"base_type":
return _type
return null

func _set(p_property: String, p_value) -> bool:
match _extract_name_from_path(p_property, SETUP_PREFIX):
match p_property.trim_prefix(SETUP_PREFIX):
"base_type":
if _type != p_value:
_type = p_value
Expand Down Expand Up @@ -75,7 +75,7 @@ func _export_setup_group() -> Array:

# Injects controls to the 'EditorInspectorPlugin'
func _parse_property(p_plugin: EditorInspectorPlugin, p_pinfo: PropertyInfo) -> bool:
match _extract_name_from_path(p_pinfo.name, DATA_PREFIX):
match p_pinfo.name.trim_prefix(DATA_PREFIX):
"dropdown":
var elements = _find_inheritors()
var control = InspectorControls.new_dropdown_appender(elements, self, "_on_dropdown_selector_selected")
Expand All @@ -90,11 +90,6 @@ func clear() -> void:

##### PRIVATE METHODS #####

func _extract_name_from_path(p_path: String, p_prefix: String) -> String:
if p_path.begins_with(p_prefix):
return p_path.substr(p_prefix.length(), p_path.length())
return ""

func _instantiate_script(p_script: Script) -> Resource:
var res: Resource = null
if ClassDB.is_parent_class(p_script.get_instance_base_type(), "Resource"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ func _init() -> void:

func _get(p_property: String):
if p_property.begins_with(DATA_PREFIX):
var key = _extract_name_from_path(p_property, DATA_PREFIX)
var key = p_property.trim_prefix(DATA_PREFIX)
return _data.get(key, null)
return null

func _set(p_property: String, p_value) -> bool:
if p_property.begins_with(DATA_PREFIX):
var key = _extract_name_from_path(p_property, DATA_PREFIX)
var key = p_property.trim_prefix(DATA_PREFIX)
if not p_value:
#warning-ignore:return_value_discarded
_data.erase(key)
Expand Down

0 comments on commit 568355c

Please sign in to comment.