Last active
August 19, 2024 22:23
-
-
Save mrcdk/8acde7adbec17650ea7b815adc734089 to your computer and use it in GitHub Desktop.
Quick and dirty Godot 4.x plugin to add buttons to the inspector
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
@tool | |
extends Node | |
func _add_inspector_buttons() -> Array: | |
var buttons = [] | |
buttons.push_back({ | |
"name": "Test button", | |
"icon": preload("res://icon.svg"), | |
"pressed": _on_test_button_pressed | |
}) | |
buttons.push_back({ | |
"name": "Another button", | |
"pressed": _on_another_button_pressed | |
}) | |
buttons.push_back({ | |
"name": "Other button with lambda", | |
"pressed": func(): print('Lambda callback') | |
}) | |
return buttons | |
func _on_test_button_pressed() -> void: | |
print('Test pressed') | |
func _on_another_button_pressed() -> void: | |
print('Another button pressed') |
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
@tool | |
extends EditorPlugin | |
var editor_button_plugin:EditorButtonPlugin | |
func _enter_tree() -> void: | |
editor_button_plugin = EditorButtonPlugin.new() | |
add_inspector_plugin(editor_button_plugin) | |
func _exit_tree() -> void: | |
if is_instance_valid(editor_button_plugin): | |
remove_inspector_plugin(editor_button_plugin) | |
editor_button_plugin = null | |
class EditorButtonPlugin extends EditorInspectorPlugin: | |
func _can_handle(object: Object) -> bool: | |
return object.has_method('_add_inspector_buttons') | |
func _parse_begin(object: Object) -> void: | |
var buttons_data = object._add_inspector_buttons() | |
for button_data in buttons_data: | |
var name = button_data.get("name", null) | |
var icon = button_data.get("icon", null) | |
var pressed = button_data.get("pressed", null) | |
if not name: | |
push_warning('_add_inspector_buttons(): A button does not have a name key. Defaulting to: "Button"') | |
name = "Button" | |
if icon and not icon is Texture: | |
push_warning('_add_inspector_buttons(): The button <{name}> icon is not a texture.'.format({"name":name})) | |
icon = null | |
if not pressed: | |
push_warning('_add_inspector_buttons(): The button <{name}> does not have a pressed key. Skipping.'.format({"name":name})) | |
continue | |
if not pressed is Callable: | |
push_warning('_add_inspector_buttons(): The button <{name}> pressed is not a Callable. Skipping.'.format({"name":name})) | |
continue | |
var button = Button.new() | |
button.text = name | |
if icon: | |
button.icon = icon | |
button.expand_icon = true | |
button.pressed.connect(pressed) | |
add_custom_control(button) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment