108 lines
2.7 KiB
GDScript
108 lines
2.7 KiB
GDScript
extends Control
|
|
|
|
## A draggable modifier piece in the tray. Follows the cursor directly (no
|
|
## ghost). On release over the board it asks DropCatcher to apply itself.
|
|
##
|
|
## Was TrapezoidSwatch.gd — renamed because modifiers now attach to whole
|
|
## sectors, not individual segments.
|
|
|
|
@export var paint_texture: Texture2D:
|
|
set(value):
|
|
paint_texture = value
|
|
queue_redraw()
|
|
|
|
@export_range(0.0, 1.0) var top_width_ratio := 0.55
|
|
|
|
# Set by the tray when it spawns this piece.
|
|
var drop_catcher: Control
|
|
var tray: Node
|
|
var modifier: Modifier
|
|
|
|
var _dragging := false
|
|
var _home_position := Vector2.ZERO
|
|
var _drag_offset := Vector2.ZERO
|
|
|
|
|
|
func _ready() -> void:
|
|
mouse_filter = Control.MOUSE_FILTER_STOP
|
|
if custom_minimum_size == Vector2.ZERO:
|
|
custom_minimum_size = Vector2(90, 110)
|
|
size = custom_minimum_size
|
|
|
|
|
|
func _draw() -> void:
|
|
var w := size.x
|
|
var h := size.y
|
|
var inset := w * (1.0 - top_width_ratio) * 0.5
|
|
|
|
var points := PackedVector2Array([
|
|
Vector2(inset, 0), Vector2(w - inset, 0),
|
|
Vector2(w, h), Vector2(0, h),
|
|
])
|
|
|
|
if paint_texture != null:
|
|
var uvs := PackedVector2Array([
|
|
Vector2(inset / w, 0), Vector2((w - inset) / w, 0),
|
|
Vector2(1, 1), Vector2(0, 1),
|
|
])
|
|
var white := PackedColorArray([Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE])
|
|
draw_polygon(points, white, uvs, paint_texture)
|
|
else:
|
|
draw_colored_polygon(points, Color(0.8, 0.8, 0.8))
|
|
|
|
var outline := points
|
|
outline.append(points[0])
|
|
draw_polyline(outline, Color(0, 0, 0, 0.6), 2.0)
|
|
|
|
|
|
func _gui_input(event: InputEvent) -> void:
|
|
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
|
|
if event.pressed:
|
|
_start_drag()
|
|
else:
|
|
_end_drag()
|
|
|
|
|
|
func _start_drag() -> void:
|
|
_dragging = true
|
|
_home_position = global_position
|
|
_drag_offset = global_position - get_global_mouse_position()
|
|
# top_level lets us move by global_position without reparenting.
|
|
# Reparenting mid-drag breaks the release event.
|
|
top_level = true
|
|
z_index = 100
|
|
modulate.a = 0.85
|
|
|
|
|
|
func _end_drag() -> void:
|
|
if not _dragging:
|
|
return
|
|
_dragging = false
|
|
modulate.a = 1.0
|
|
z_index = 0
|
|
|
|
var applied := false
|
|
if drop_catcher == null:
|
|
push_error("SectorSwatch has no drop_catcher (tray export not set?).")
|
|
elif not drop_catcher.has_method(&"apply_modifier_at_screen_pos"):
|
|
push_error("drop_catcher has no apply_modifier_at_screen_pos method.")
|
|
else:
|
|
applied = drop_catcher.apply_modifier_at_screen_pos(
|
|
get_global_mouse_position(), modifier
|
|
)
|
|
|
|
if applied:
|
|
if tray != null and tray.has_method(&"consume"):
|
|
tray.consume()
|
|
else:
|
|
queue_free()
|
|
else:
|
|
# Missed — snap home.
|
|
top_level = false
|
|
global_position = _home_position
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
if _dragging:
|
|
global_position = get_global_mouse_position() + _drag_offset
|