97 lines
2.8 KiB
GDScript3
97 lines
2.8 KiB
GDScript3
|
|
class_name FillEditor
|
|||
|
|
extends VBoxContainer
|
|||
|
|
## Edits a large-area fill (Style.fill_texture spec): Flat, Straight gradient or
|
|||
|
|
## Round gradient, two to four colour stops and, for straight ones, an angle.
|
|||
|
|
|
|||
|
|
signal changed
|
|||
|
|
|
|||
|
|
const KINDS := ["solid", "linear", "radial"]
|
|||
|
|
const MAX_STOPS := 4
|
|||
|
|
|
|||
|
|
var fill: Dictionary
|
|||
|
|
|
|||
|
|
|
|||
|
|
func _init(p_fill: Dictionary, flat: Color) -> void:
|
|||
|
|
fill = p_fill.duplicate(true)
|
|||
|
|
if (fill.get("colors", []) as Array).is_empty():
|
|||
|
|
fill["colors"] = ["#" + flat.to_html(true)]
|
|||
|
|
if not fill.has("kind"):
|
|||
|
|
fill["kind"] = "solid"
|
|||
|
|
add_theme_constant_override("separation", 8)
|
|||
|
|
_rebuild()
|
|||
|
|
|
|||
|
|
|
|||
|
|
func _rebuild() -> void:
|
|||
|
|
for c in get_children():
|
|||
|
|
c.queue_free()
|
|||
|
|
var kind := KINDS.find(str(fill["kind"]))
|
|||
|
|
add_child(ModalCard.segmented(["Flat", "Straight gradient", "Round gradient"], maxi(kind, 0), _set_kind))
|
|||
|
|
var row := HBoxContainer.new()
|
|||
|
|
row.add_theme_constant_override("separation", 6)
|
|||
|
|
add_child(row)
|
|||
|
|
var cols: Array = fill["colors"]
|
|||
|
|
var shown := 1 if fill["kind"] == "solid" else cols.size()
|
|||
|
|
for i in shown:
|
|||
|
|
var pick := swatch(Color.from_string(str(cols[i]), Color.BLACK), _set_color.bind(i))
|
|||
|
|
pick.tooltip_text = "Colour" if shown == 1 else "Colour %d of %d" % [i + 1, shown]
|
|||
|
|
row.add_child(pick)
|
|||
|
|
if fill["kind"] == "solid":
|
|||
|
|
return
|
|||
|
|
if cols.size() < MAX_STOPS:
|
|||
|
|
row.add_child(_small("+", "Add a colour", func() -> void:
|
|||
|
|
cols.append(cols[-1])
|
|||
|
|
_changed(true)))
|
|||
|
|
if cols.size() > 2:
|
|||
|
|
row.add_child(_small("−", "Remove the last colour", func() -> void:
|
|||
|
|
cols.pop_back()
|
|||
|
|
_changed(true)))
|
|||
|
|
if fill["kind"] == "linear":
|
|||
|
|
var spin := SpinBox.new()
|
|||
|
|
spin.min_value = 0
|
|||
|
|
spin.max_value = 359
|
|||
|
|
spin.step = 15
|
|||
|
|
spin.suffix = "°"
|
|||
|
|
spin.value = float(fill.get("angle", 90))
|
|||
|
|
spin.tooltip_text = "Direction: 90° runs top to bottom, 0° left to right"
|
|||
|
|
spin.value_changed.connect(func(v: float) -> void:
|
|||
|
|
fill["angle"] = int(v)
|
|||
|
|
changed.emit())
|
|||
|
|
row.add_child(spin)
|
|||
|
|
|
|||
|
|
|
|||
|
|
## A colour button used across the theme editor.
|
|||
|
|
static func swatch(c: Color, on_change: Callable) -> ColorPickerButton:
|
|||
|
|
var pick := ColorPickerButton.new()
|
|||
|
|
pick.color = c
|
|||
|
|
pick.custom_minimum_size = Vector2(44, 30)
|
|||
|
|
pick.edit_alpha = true
|
|||
|
|
pick.color_changed.connect(func(nc: Color) -> void: on_change.call(nc))
|
|||
|
|
return pick
|
|||
|
|
|
|||
|
|
|
|||
|
|
func _set_kind(i: int) -> void:
|
|||
|
|
fill["kind"] = KINDS[i]
|
|||
|
|
var cols: Array = fill["colors"]
|
|||
|
|
if i > 0 and cols.size() < 2:
|
|||
|
|
var base := Color.from_string(str(cols[0]), Color.BLACK)
|
|||
|
|
cols.append("#" + base.darkened(0.35).to_html(true))
|
|||
|
|
_changed(true)
|
|||
|
|
|
|||
|
|
|
|||
|
|
func _set_color(c: Color, i: int) -> void:
|
|||
|
|
(fill["colors"] as Array)[i] = "#" + c.to_html(true)
|
|||
|
|
changed.emit()
|
|||
|
|
|
|||
|
|
|
|||
|
|
func _changed(rebuild: bool) -> void:
|
|||
|
|
if rebuild:
|
|||
|
|
_rebuild.call_deferred()
|
|||
|
|
changed.emit()
|
|||
|
|
|
|||
|
|
|
|||
|
|
func _small(text: String, tip: String, on_press: Callable) -> Button:
|
|||
|
|
var b := ModalCard.make_button(text)
|
|||
|
|
b.tooltip_text = tip
|
|||
|
|
b.pressed.connect(on_press)
|
|||
|
|
return b
|