A Quadrilateral Cowboy clone intended to help me learn Game Dev
at main 84 lines 2.3 kB view raw
1## Button that is used as a custom tab implementation for the multiline tab bar. 2@tool 3extends Button 4 5signal close_pressed 6signal right_clicked 7signal dragged_over 8signal dropped(source_index: int, target_index: int) 9 10var close_button: Button 11 12func _ready() -> void: 13 alignment = HORIZONTAL_ALIGNMENT_LEFT 14 action_mode = ACTION_MODE_BUTTON_PRESS 15 auto_translate_mode = Node.AUTO_TRANSLATE_MODE_DISABLED 16 toggle_mode = true 17 18func show_close_button(): 19 if (close_button == null): 20 close_button = create_close_button() 21 22 add_child(close_button) 23 close_button.set_anchors_and_offsets_preset(Control.PRESET_CENTER_RIGHT) 24 25func hide_close_button(): 26 if (close_button != null): 27 remove_child(close_button) 28 29func create_close_button() -> Button: 30 close_button = Button.new() 31 close_button.icon = EditorInterface.get_editor_theme().get_icon(&"Close", &"EditorIcons") 32 close_button.flat = true 33 close_button.focus_mode = Control.FOCUS_NONE 34 close_button.pressed.connect(on_close_pressed) 35 36 return close_button 37 38func _gui_input(event: InputEvent) -> void: 39 if event is InputEventMouseButton && event.pressed: 40 if event.button_index == MOUSE_BUTTON_MIDDLE: 41 on_close_pressed() 42 elif (event.button_index == MOUSE_BUTTON_RIGHT): 43 button_pressed = true 44 on_right_click() 45 46func on_right_click(): 47 right_clicked.emit() 48 49func on_close_pressed() -> void: 50 close_pressed.emit() 51 52func _get_drag_data(at_position: Vector2) -> Variant: 53 var preview: Button = Button.new() 54 preview.text = text 55 preview.icon = icon 56 preview.alignment = HORIZONTAL_ALIGNMENT_LEFT 57 preview.auto_translate_mode = Node.AUTO_TRANSLATE_MODE_DISABLED 58 preview.add_theme_stylebox_override(&"normal", get_theme_stylebox(&"normal")) 59 60 set_drag_preview(preview) 61 62 var drag_data: Dictionary[String, Variant] 63 drag_data["type"] = "script_list_element" 64 drag_data["script_list_element"] = EditorInterface.get_script_editor().get_current_editor() 65 drag_data["index"] = get_index() 66 67 return drag_data 68 69func _can_drop_data(at_position: Vector2, data: Variant) -> bool: 70 if !(data is Dictionary): 71 return false 72 73 var can_drop: bool = data.has("index") 74 75 if (can_drop): 76 dragged_over.emit() 77 78 return can_drop 79 80func _drop_data(at_position: Vector2, data: Variant) -> void: 81 if (!_can_drop_data(at_position, data)): 82 return 83 84 dropped.emit(data["index"], get_index())