TableTower/ui/entry_handle.gd

125 lines
3.8 KiB
GDScript3
Raw Permalink Normal View History

class_name EntryHandle
extends Control
## Build mode: the six-dot drag handle at the start of a result row. Drag it
## up or down the card to reorder results; a line shows where it will land.
## On a card with number ranges the ranges stay in place and the results move
## through them (TableCardEditing.move_entry).
const W := 10.0
var card: TableCard
var eid: String
var _dragging := false
var _target := -1
var _line: ColorRect
## Holds the line: the card is a container, so a direct child would be stretched.
var _holder: Control
func _init(p_card: TableCard, p_eid: String) -> void:
card = p_card
eid = p_eid
custom_minimum_size = Vector2(W, 22)
size_flags_vertical = Control.SIZE_SHRINK_CENTER
mouse_filter = MOUSE_FILTER_STOP
mouse_default_cursor_shape = CURSOR_MOVE
tooltip_text = "Drag to move this result"
mouse_entered.connect(queue_redraw)
mouse_exited.connect(queue_redraw)
func _draw() -> void:
var hot := _dragging or get_global_rect().has_point(get_global_mouse_position())
var c := Style.MUTED if hot else Style.FAINT
for col in 2:
for r in 3:
draw_circle(Vector2(2.5 + col * 5.0, size.y / 2.0 + (r - 1) * 5.0), 1.4, c)
func _gui_input(ev: InputEvent) -> void:
var mb := ev as InputEventMouseButton
if mb and mb.button_index == MOUSE_BUTTON_LEFT:
if mb.pressed:
_dragging = true
_target = -1
var row: Control = card.rows.get(eid)
if row:
row.modulate.a = 0.5
elif _dragging:
finish()
accept_event()
elif ev is InputEventMouseMotion and _dragging:
track(get_global_mouse_position())
accept_event()
## Where the result would land for a pointer at `global`, as an index into
## the card's entries once this one is taken out. Shows the insertion line.
func track(global: Vector2) -> int:
var order := _others()
var slot := order.size()
for i in order.size():
var r := (card.rows[order[i].id] as Control).get_global_rect()
if global.y < r.get_center().y:
slot = i
break
var entries := card.table.entries
_target = entries.size() - 1
if slot < order.size():
_target = _index_without_self(order[slot])
elif not order.is_empty():
_target = _index_without_self(order[-1]) + 1
_show_line(order, slot)
return _target
## Applies the move and tidies up.
func finish() -> void:
_dragging = false
if _holder and is_instance_valid(_holder):
_holder.queue_free()
_holder = null
_line = null
var row: Control = card.rows.get(eid)
if row and is_instance_valid(row):
row.modulate.a = 1.0
queue_redraw()
var from := card.table.entries.find(card.table.find_entry(eid))
if _target >= 0 and _target != from:
card.edit_ui.move_entry(from, _target)
## The other results whose rows are on the card, top to bottom.
func _others() -> Array[TableEntry]:
var out: Array[TableEntry] = []
for e in card.table.entries:
var row: Control = card.rows.get(e.id)
if e.id != eid and row and is_instance_valid(row) and row.is_visible_in_tree():
out.append(e)
return out
func _index_without_self(e: TableEntry) -> int:
var i := card.table.entries.find(e)
var me := card.table.entries.find(card.table.find_entry(eid))
return i - 1 if me < i else i
func _show_line(order: Array[TableEntry], slot: int) -> void:
if order.is_empty():
return
var above := slot >= order.size()
var r := (card.rows[order[mini(slot, order.size() - 1)].id] as Control).get_global_rect()
if _line == null or not is_instance_valid(_line):
_holder = Control.new()
_holder.mouse_filter = MOUSE_FILTER_IGNORE
card.add_child(_holder)
_line = ColorRect.new()
_line.color = Style.ACCENT
_line.mouse_filter = MOUSE_FILTER_IGNORE
_holder.add_child(_line)
var to_local := _holder.get_global_transform().affine_inverse()
var y: float = (to_local * Vector2(r.position.x, r.end.y if above else r.position.y)).y
_line.position = Vector2(0, y - 1.5)
_line.size = Vector2(_holder.size.x, 3)