Reworked the EditorResourceWidget, now it uses an EditorResourcePicker.

This commit is contained in:
Relintai 2023-10-14 09:30:15 +02:00
parent 59006f2c33
commit a9344fb743
3 changed files with 37 additions and 61 deletions

View File

@ -16,6 +16,9 @@ var editor_scene : Node = null
var tool_button : ToolButton = null
func edit_resource(resource: Resource) -> void:
get_editor_interface().edit_resource(resource)
func _enter_tree():
# add_custom_type("WorldGeneratorSettings", "Resource", SWorldGeneratorSettings, null)
#

View File

@ -5,14 +5,16 @@ var _resource_type : String = "Resource"
var _resource : Resource = null
var _plugin : EditorPlugin = null
signal on_resource_changed(new_res)
var _picker : EditorResourcePicker = null
func _enter_tree():
$ResourceButton.set_drag_forwarding(self)
signal on_resource_changed(new_res)
func set_resource_type(type : String) -> void:
_resource_type = type
if _picker:
_picker.base_type = _resource_type
func set_resource(res : Resource) -> void:
if res && !res.is_class(_resource_type):
return
@ -21,21 +23,40 @@ func set_resource(res : Resource) -> void:
_resource = res
refresh_ui()
if _picker:
_picker.edited_resource = _resource
if emit:
emit_signal("on_resource_changed", _resource)
func refresh_ui() -> void:
if _resource:
var text : String = _resource.resource_name
if text == "":
text = _resource.get_class()
func set_plugin(plugin : EditorPlugin) -> void:
_plugin = plugin
func _enter_tree():
if Engine.is_editor_hint():
_picker = EditorResourcePicker.new()
_picker.set_h_size_flags(SIZE_EXPAND_FILL)
_picker.set_v_size_flags(SIZE_EXPAND_FILL)
add_child(_picker)
$ResourceButton.text = text
else:
$ResourceButton.text = "[null]"
if _resource:
_picker.edited_resource = _resource
_picker.base_type = _resource_type
_picker.connect(@"resource_changed", self, "_on_resource_changed")
_picker.connect(@"resource_selected", self, "_on_resource_selected")
func _on_resource_changed(resource: Resource) -> void:
var emit : bool = resource != _resource
_resource = resource
if emit:
emit_signal("on_resource_changed", _resource)
func _on_resource_selected(resource: Resource, edit: bool) -> void:
_plugin.edit_resource(resource)
func on_clear_button_pressed() -> void:
if _resource:
@ -45,36 +66,3 @@ func on_resource_button_pressed() -> void:
if _resource && _plugin:
_plugin.get_editor_interface().inspect_object(_resource)
#examples
#{files:[res://modules/planets/test_planet/test_world.tres], from:@@4176:[Tree:9070], type:files}
#{from:Button:[Button:917001], resource:[Resource:26180], type:resource}
func can_drop_data_fw(position, data, from_control):
return true
func drop_data_fw(position, data, from_control):
if data["type"] == "resource":
var res : Resource = data["resource"]
set_resource(res)
elif data["type"] == "files":
var files : Array = data["files"]
for f in files:
var res : Resource = load(f)
set_resource(res)
return
func get_drag_data_fw(position, from_control):
if !_resource:
return
var d : Dictionary = Dictionary()
d["from"] = self
d["resource"] = _resource
d["type"] = "resource"
return d
func set_plugin(plugin : EditorPlugin) -> void:
_plugin = plugin

View File

@ -6,18 +6,3 @@
margin_right = 376.0
margin_bottom = 40.0
script = ExtResource( 1 )
[node name="ResourceButton" type="Button" parent="."]
margin_right = 342.0
margin_bottom = 40.0
size_flags_horizontal = 3
[node name="Clear" type="Button" parent="."]
margin_left = 346.0
margin_right = 376.0
margin_bottom = 40.0
rect_min_size = Vector2( 30, 0 )
text = "X"
[connection signal="pressed" from="ResourceButton" to="." method="on_resource_button_pressed"]
[connection signal="pressed" from="Clear" to="." method="on_clear_button_pressed"]