Custom Full-Width Button Utils for the Inspector using GDScript
Will work for Godot ^4.7.*
(since the default ones always fit their content and are too irritating)
Example Usage:
extends Node
@export_category("My Button Group")
@export_tool_button("Lift weapon") var lift_btn: Callable = dropWeapon
@export_tool_button("Throw weapon") var throw_btn: Callable = liftWeapon
# This needs to exist to tell Godot to use our custom drawing utility
func _property_can_revert(_property: StringName) -> bool:
return InspectorButtonUtil.expand_tool_buttons(self)
The Callable is a Godot function ofc.
Heres the main script that you place anywhere in your files:-
@tool
class_name InspectorButtonUtil
extends RefCounted
const _QUEUE_META := &"_tool_btn_pass_queued"
const _PATCHED_META := &"_full_width_patched"
## Schedules a single deferred pass to expand all @export_tool_button controls on the target object.
static func expand_tool_buttons(object: Object) -> bool:
if not Engine.is_editor_hint() or not is_instance_valid(object):
return false
if object.has_meta(_QUEUE_META):
return false
object.set_meta(_QUEUE_META, true)
Callable(_apply_expansion).call_deferred(object)
return false
static func _apply_expansion(object: Object) -> void:
if not is_instance_valid(object):
return
object.remove_meta(_QUEUE_META)
# 1. Collect all tool button property names and button labels defined on this object
var tool_btn_props: Dictionary = {}
var tool_btn_labels: Dictionary = {}
for prop in object.get_property_list():
if prop.get("hint") == PROPERTY_HINT_TOOL_BUTTON:
var prop_name := StringName(prop["name"])
tool_btn_props[prop_name] = true
# hint_string is "Label" or "Label,Icon"
var hint_str: String = prop.get("hint_string", "")
var label_text: String = hint_str.split(",")[0] if not hint_str.is_empty() else String(prop_name)
tool_btn_labels[label_text] = true
if tool_btn_props.is_empty():
return
var inspector: Node = Engine.get_singleton("EditorInterface").get_inspector()
if not inspector:
return
# 2. Traverse inspector including internal children (get_children(true))
var stack: Array[Node] = [inspector]
while stack.size() > 0:
var current: Node = stack.pop_back()
# Case A: Wrapped inside an EditorProperty
if current is EditorProperty:
var prop_name: StringName = current.get_edited_property()
if current.get_edited_object() == object and tool_btn_props.has(prop_name):
if not current.has_meta(_PATCHED_META):
current.set_meta(_PATCHED_META, true)
_patch_editor_property(current)
continue
# Case B: Injected directly as a Button / Container via EditorInspectorToolButtonPlugin
elif current is Button and tool_btn_labels.has(current.text):
if not current.has_meta(_PATCHED_META):
current.set_meta(_PATCHED_META, true)
_patch_standalone_button(current)
continue
stack.append_array(current.get_children(true))
static func _patch_editor_property(editor_prop: EditorProperty) -> void:
editor_prop.label = ""
var btn := _find_child_button(editor_prop)
if btn:
_configure_button(btn)
if editor_prop.has_method("set_bottom_editor"):
editor_prop.call("set_bottom_editor", btn)
editor_prop.queue_sort()
static func _patch_standalone_button(btn: Button) -> void:
_configure_button(btn)
var parent := btn.get_parent()
while parent and parent is Control and not (parent is VBoxContainer):
parent.size_flags_horizontal = Control.SIZE_EXPAND_FILL
if parent is EditorProperty:
parent.label = ""
if parent.has_method("set_bottom_editor"):
parent.call("set_bottom_editor", btn)
parent.queue_sort()
break
parent = parent.get_parent()
static func _configure_button(btn: Button) -> void:
btn.size_flags_horizontal = Control.SIZE_EXPAND_FILL
btn.alignment = HORIZONTAL_ALIGNMENT_CENTER
btn.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
var parent_ctrl := btn.get_parent_control()
if parent_ctrl:
parent_ctrl.size_flags_horizontal = Control.SIZE_EXPAND_FILL
static func _find_child_button(root: Node) -> Button:
var queue: Array[Node] = [root]
while queue.size() > 0:
var node: Node = queue.pop_front()
if node is Button:
return node
queue.append_array(node.get_children(true))
return null