godot-control-tree/RenamableButton.gd

43 lines
1002 B
GDScript3
Raw Permalink Normal View History

extends Button
var le: LineEdit = null
signal rename_request
func _gui_input(event: InputEvent) -> void:
if event is InputEventMouseButton && event.is_doubleclick() && !le:
emit_signal("rename_request")
func remove_lineedit() -> void:
le.queue_free()
le = null
func create_lineedit() -> void:
le = LineEdit.new()
get_tree().get_root().add_child(le)
le.rect_size = get_global_rect().size
# le.rect_size.x += 64 # some padding to make it more readable
le.rect_position = get_global_rect().position
le.expand_to_text_length = true
le.text = text
le.select_all()
le.grab_focus()
le.connect("text_entered", self, "lineeedit_text_entered")
func lineeedit_text_entered(new_text: String) -> void:
le.queue_free()
le = null
text = new_text
func _input(event: InputEvent) -> void:
if le && event is InputEventMouseButton && !(le.get_global_rect().has_point(event.position)):
remove_lineedit()
if event is InputEventKey && event.scancode == KEY_ESCAPE && le:
remove_lineedit()