2021-04-16 18:09:03 +00:00
|
|
|
class_name PaletteSwatch
|
2021-11-25 12:48:30 +00:00
|
|
|
extends ColorRect
|
2021-04-16 18:09:03 +00:00
|
|
|
|
|
|
|
signal pressed(mouse_button)
|
|
|
|
signal double_clicked(mouse_button, position)
|
|
|
|
signal dropped(source_index, new_index)
|
|
|
|
|
|
|
|
const DEFAULT_COLOR := Color(0.0, 0.0, 0.0, 0.0)
|
|
|
|
|
|
|
|
var index := -1
|
|
|
|
var show_left_highlight := false
|
|
|
|
var show_right_highlight := false
|
|
|
|
var empty := true setget set_empty
|
|
|
|
|
|
|
|
|
2022-10-04 10:47:31 +00:00
|
|
|
func set_swatch_size(size: Vector2) -> void:
|
|
|
|
rect_min_size = size
|
|
|
|
rect_size = size
|
2021-04-16 18:09:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _draw() -> void:
|
|
|
|
if not empty:
|
|
|
|
# Black border around swatches with a color
|
2022-10-04 10:47:31 +00:00
|
|
|
draw_rect(Rect2(Vector2.ZERO, rect_size), Color.black, false, 1)
|
2021-04-16 18:09:03 +00:00
|
|
|
|
|
|
|
if show_left_highlight:
|
|
|
|
# Display outer border highlight
|
2022-10-04 10:47:31 +00:00
|
|
|
draw_rect(Rect2(Vector2.ZERO, rect_size), Color.white, false, 1)
|
|
|
|
draw_rect(Rect2(Vector2.ONE, rect_size - Vector2(2, 2)), Color.black, false, 1)
|
2021-04-16 18:09:03 +00:00
|
|
|
|
|
|
|
if show_right_highlight:
|
|
|
|
# Display inner border highlight
|
2022-10-04 10:47:31 +00:00
|
|
|
var margin := rect_size / 4
|
|
|
|
draw_rect(Rect2(margin, rect_size - margin * 2), Color.black, false, 1)
|
2021-11-25 12:48:30 +00:00
|
|
|
draw_rect(
|
2022-10-04 10:47:31 +00:00
|
|
|
Rect2(margin - Vector2.ONE, rect_size - margin * 2 + Vector2(2, 2)),
|
2021-11-25 12:48:30 +00:00
|
|
|
Color.white,
|
|
|
|
false,
|
|
|
|
1
|
|
|
|
)
|
2021-04-16 18:09:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Enables drawing of highlights which indicate selected swatches
|
|
|
|
func show_selected_highlight(new_value: bool, mouse_button: int) -> void:
|
|
|
|
if not empty:
|
|
|
|
match mouse_button:
|
|
|
|
BUTTON_LEFT:
|
|
|
|
show_left_highlight = new_value
|
|
|
|
BUTTON_RIGHT:
|
|
|
|
show_right_highlight = new_value
|
|
|
|
update()
|
|
|
|
|
|
|
|
|
|
|
|
# Empties the swatch and displays disabled color from theme
|
|
|
|
func set_empty(new_value: bool) -> void:
|
|
|
|
empty = new_value
|
|
|
|
if empty:
|
|
|
|
mouse_default_cursor_shape = Control.CURSOR_ARROW
|
|
|
|
color = Global.control.theme.get_stylebox("disabled", "Button").bg_color
|
|
|
|
else:
|
|
|
|
mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
|
|
|
|
|
|
|
|
|
|
|
|
func get_drag_data(_position):
|
|
|
|
var data = null
|
|
|
|
if not empty:
|
|
|
|
var drag_icon: PaletteSwatch = self.duplicate()
|
|
|
|
drag_icon.show_left_highlight = false
|
|
|
|
drag_icon.show_right_highlight = false
|
|
|
|
drag_icon.empty = false
|
|
|
|
set_drag_preview(drag_icon)
|
|
|
|
data = {source_index = index}
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
func can_drop_data(_position, _data) -> bool:
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
|
|
func drop_data(_position, data) -> void:
|
|
|
|
emit_signal("dropped", data.source_index, index)
|
|
|
|
|
|
|
|
|
|
|
|
func _on_PaletteSlot_gui_input(event: InputEvent) -> void:
|
|
|
|
if event is InputEventMouseButton and event.is_pressed() and not empty:
|
|
|
|
if event.doubleclick:
|
|
|
|
emit_signal("double_clicked", event.button_index, get_global_rect().position)
|
|
|
|
else:
|
|
|
|
emit_signal("pressed", event.button_index)
|