Skip to content

Instantly share code, notes, and snippets.

@HungryProton
Created July 21, 2021 12:04
Show Gist options
  • Save HungryProton/b67938a9e10b315dbfef47815ba8fc02 to your computer and use it in GitHub Desktop.
Save HungryProton/b67938a9e10b315dbfef47815ba8fc02 to your computer and use it in GitHub Desktop.
Editor Inspector plugin example
tool
extends Resource
class_name GridShape
export var size := Vector2.ONE
export var grid := []
func _init() -> void:
resize(size.x, size.y)
func resize(x, y) -> void:
size.x = x
size.y = y
grid.clear()
grid.resize(x * y)
for i in grid.size():
grid[i] = false
func set_status_at(idx, status):
grid[idx] = status
func get_status(idx) -> bool:
if idx < 0 or idx >= size.x * size.y:
return false
return grid[idx]
extends EditorProperty
class_name GridShapeEditor
var updating := false
var ui_parent := VBoxContainer.new()
var width_spin: SpinBox
var height_spin: SpinBox
var grid: GridContainer
var style_normal: StyleBox = preload("./button_normal.tres")
var style_pressed: StyleBox = preload("./button_pressed.tres")
func _init():
var shape = GridShape.new()
var hbox = HBoxContainer.new()
width_spin = _create_spin("X", shape.size.x)
height_spin = _create_spin("Y", shape.size.y)
width_spin.connect("value_changed", self, "_on_width_changed")
height_spin.connect("value_changed", self, "_on_height_changed")
hbox.add_child(width_spin)
hbox.add_child(height_spin)
ui_parent.add_child(hbox)
add_child(ui_parent)
set_bottom_editor(ui_parent)
func _create_spin(label: String, value) -> SpinBox:
var spin = EditorSpinSlider.new()
spin.set_min(1)
spin.set_max(20)
spin.value = value
spin.label = label
spin.size_flags_horizontal = SIZE_EXPAND_FILL
return spin
func _create_grid(shape: GridShape) -> void:
if grid:
grid.queue_free()
grid = GridContainer.new()
grid.add_constant_override("vseparation", 1)
grid.add_constant_override("hseparation", 1)
grid.columns = shape.size.x
for i in shape.size.x * shape.size.y:
grid.add_child(_create_button(i, shape.get_status(i)))
ui_parent.add_child(grid)
func _create_button(idx: int, pressed: bool) -> Button:
var btn = Button.new()
btn.add_stylebox_override("normal", style_normal)
btn.add_stylebox_override("pressed", style_pressed)
btn.focus_mode = Control.FOCUS_NONE
btn.toggle_mode = true
btn.name = String(idx)
btn.rect_min_size = Vector2(20, 20)
btn.pressed = pressed
btn.connect("toggled", self, "_on_button_toggled", [idx])
return btn
func _on_width_changed(value):
if (updating):
return
width_spin.value = value
_on_size_changed()
func _on_height_changed(value):
if (updating):
return
height_spin.value = value
_on_size_changed()
func _on_size_changed() -> void:
var shape: GridShape = get_edited_object()[get_edited_property()]
if not shape:
return
shape.resize(width_spin.value, height_spin.value)
emit_changed(get_edited_property(), shape)
func _on_button_toggled(value: bool, idx: int) -> void:
var shape := _get_shape()
shape.grid[idx] = value
func update_property():
var shape := _get_shape()
updating = true
width_spin.set_value(shape.size.x)
height_spin.set_value(shape.size.y)
_create_grid(shape)
updating = false
func _get_shape() -> GridShape:
var edited_object = get_edited_object()
var property_name = get_edited_property()
var shape: GridShape = edited_object[property_name]
if not shape:
shape = GridShape.new()
edited_object[property_name] = shape
return shape
extends EditorInspectorPlugin
func can_handle(object):
return object is Component
func parse_property(object, type, path, hint, hint_text, usage):
if type == TYPE_OBJECT and hint_text == "GridShape":
add_property_editor(path, GridShapeEditor.new())
return true
return false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment