88 lines
2.7 KiB
GDScript3
88 lines
2.7 KiB
GDScript3
|
|
class_name TableGroup
|
||
|
|
extends RefCounted
|
||
|
|
## A group card: a named frame around some of the board's tables, so a whole
|
||
|
|
## block of cards can be moved, collapsed and saved as a template at once.
|
||
|
|
## Groups are presentation only; rolling never looks at them. A table belongs
|
||
|
|
## to at most one group, and groups don't nest.
|
||
|
|
##
|
||
|
|
## The rect is the frame in world coordinates, title bar included. While the
|
||
|
|
## group is expanded the board refits it around the member cards; collapsed,
|
||
|
|
## its top-left is where the compact group card sits.
|
||
|
|
|
||
|
|
## Space between the frame and its member cards.
|
||
|
|
const PAD := 22.0
|
||
|
|
## Height of the title bar at the top of the frame.
|
||
|
|
const TITLE_H := 46.0
|
||
|
|
|
||
|
|
var id: String = ""
|
||
|
|
var name: String = ""
|
||
|
|
var position: Vector2 = Vector2.ZERO
|
||
|
|
var size: Vector2 = Vector2(320, 200)
|
||
|
|
var collapsed: bool = false
|
||
|
|
## Longer description (BBCode), shown in the title bar's hover pop-up.
|
||
|
|
var details: String = ""
|
||
|
|
## Ids of the member tables, in the order they were added.
|
||
|
|
var members: Array[String] = []
|
||
|
|
|
||
|
|
|
||
|
|
func _init(p_id: String = "", p_name: String = "") -> void:
|
||
|
|
id = p_id
|
||
|
|
name = p_name
|
||
|
|
|
||
|
|
|
||
|
|
func rect() -> Rect2:
|
||
|
|
return Rect2(position, size)
|
||
|
|
|
||
|
|
|
||
|
|
func has(table_id: String) -> bool:
|
||
|
|
return members.has(table_id)
|
||
|
|
|
||
|
|
|
||
|
|
## The frame that fits around `rects` (member cards, world coordinates), with
|
||
|
|
## padding and room for the title bar. Returns the current rect for no rects.
|
||
|
|
func fit_rect(rects: Array) -> Rect2:
|
||
|
|
if rects.is_empty():
|
||
|
|
return rect()
|
||
|
|
var r: Rect2 = rects[0]
|
||
|
|
for i in range(1, rects.size()):
|
||
|
|
r = r.merge(rects[i])
|
||
|
|
return Rect2(r.position - Vector2(PAD, PAD + TITLE_H), r.size + Vector2(PAD * 2.0, PAD * 2.0 + TITLE_H))
|
||
|
|
|
||
|
|
|
||
|
|
## Moves the frame to fit `rects`. Returns true when it changed.
|
||
|
|
func fit_to(rects: Array) -> bool:
|
||
|
|
if rects.is_empty():
|
||
|
|
return false
|
||
|
|
var r := fit_rect(rects)
|
||
|
|
r = Rect2(r.position.round(), r.size.round())
|
||
|
|
if r == rect():
|
||
|
|
return false
|
||
|
|
position = r.position
|
||
|
|
size = r.size
|
||
|
|
return true
|
||
|
|
|
||
|
|
|
||
|
|
func to_dict() -> Dictionary:
|
||
|
|
var d := {"id": id, "name": name, "x": roundi(position.x), "y": roundi(position.y),
|
||
|
|
"w": roundi(size.x), "h": roundi(size.y), "members": members.duplicate()}
|
||
|
|
if collapsed:
|
||
|
|
d["collapsed"] = true
|
||
|
|
if details != "":
|
||
|
|
d["details"] = details
|
||
|
|
return d
|
||
|
|
|
||
|
|
|
||
|
|
static func from_dict(d: Dictionary) -> TableGroup:
|
||
|
|
var g := TableGroup.new(str(d.get("id", "")), str(d.get("name", "")))
|
||
|
|
g.position = Vector2(float(d.get("x", 0)), float(d.get("y", 0)))
|
||
|
|
g.size = Vector2(maxf(80.0, float(d.get("w", 320))), maxf(TITLE_H, float(d.get("h", 200))))
|
||
|
|
g.collapsed = bool(d.get("collapsed", false))
|
||
|
|
g.details = str(d.get("details", ""))
|
||
|
|
var src: Variant = d.get("members", [])
|
||
|
|
if src is Array:
|
||
|
|
for m: Variant in src:
|
||
|
|
var s := str(m)
|
||
|
|
if s != "" and not g.members.has(s):
|
||
|
|
g.members.append(s)
|
||
|
|
return g
|