TableTower/ui/color_choice.gd

66 lines
2.1 KiB
GDScript3
Raw Normal View History

class_name ColorChoice
extends HFlowContainer
## A card's or thread's own colour: "Theme" (none), a row of presets, or any
## colour from the custom picker. Calls on_change with "#hex", or "" for Theme.
## PRESETS is shared with the right-click menus.
const PRESETS := [
["Red", "#b8382c"], ["Orange", "#c8691e"], ["Gold", "#c99a1c"], ["Green", "#2f8a4f"],
["Teal", "#13877f"], ["Blue", "#2f5d9a"], ["Violet", "#7a3fa8"], ["Pink", "#c04a86"],
["Grey", "#6b6670"],
]
var _on_change: Callable
var _value := ""
func _init(value: String, on_change: Callable) -> void:
_value = value
_on_change = on_change
add_theme_constant_override("h_separation", 4)
add_theme_constant_override("v_separation", 4)
_build()
func _build() -> void:
for c in get_children():
c.queue_free()
var theme_b := _swatch(Color(0, 0, 0, 0), "Theme colour", _value == "")
theme_b.text = "Theme"
theme_b.custom_minimum_size.x = 0
theme_b.pressed.connect(_pick.bind(""))
add_child(theme_b)
var matched := _value == ""
for p: Array in PRESETS:
var on := _value.to_lower() == str(p[1])
matched = matched or on
var b := _swatch(Color(str(p[1])), str(p[0]), on)
b.pressed.connect(_pick.bind(str(p[1])))
add_child(b)
var custom := FillEditor.swatch(Color(_value) if _value != "" and not matched else Style.TRIM, func(c: Color) -> void:
_value = "#" + c.to_html(false)
_on_change.call(_value))
custom.edit_alpha = false
custom.custom_minimum_size = Vector2(30, 24)
custom.tooltip_text = "Any colour…"
add_child(custom)
func _pick(hex: String) -> void:
_value = hex
_on_change.call(hex)
_build.call_deferred()
static func _swatch(c: Color, tip: String, on: bool) -> Button:
var b := Button.new()
b.tooltip_text = tip
b.custom_minimum_size = Vector2(24, 24)
b.focus_mode = Control.FOCUS_NONE
var fill := c if c.a > 0.0 else Style.CARD_SOFT
var normal := Style.bordered(Style.box(fill, 12, [8, 2, 8, 2]), 3 if on else 1, Style.INK if on else Style.DASH)
var hover := Style.bordered(Style.box(fill.lightened(0.12), 12, [8, 2, 8, 2]), 3 if on else 1, Style.INK)
Style.skin_button(b, normal, hover, hover, Style.INK, Style.INK)
b.add_theme_font_size_override("font_size", 13)
return b