TableTower/ui/board_backdrop.gd

82 lines
3 KiB
GDScript3
Raw Permalink Normal View History

class_name BoardBackdrop
extends RefCounted
## The spread's background, in screen space, back to front: the flat BG
## colour, the theme's background fill, its scene picture, then its tile
## picture (or the old dots / stars pattern when there is no tile).
## See docs/THEMES.md, "Background".
var glow: Texture2D
var scene: Texture2D
## "bottom", "center" or "top": where the scene is pinned when it is cropped.
var scene_anchor := "center"
var tile: Texture2D
var tile_opacity := 1.0
var tile_scale := 1.0
func _init(theme_set: Dictionary) -> void:
glow = Style.fill_texture(Style.BG_FILL)
var scene_keys: Dictionary = theme_set.get("background_scene", {})
scene = ThemeAssets.texture(theme_set, str(scene_keys.get("file", "")))
scene_anchor = str(scene_keys.get("anchor", "center"))
var tile_keys: Dictionary = theme_set.get("background_tile", {})
tile = ThemeAssets.texture(theme_set, str(tile_keys.get("file", "")))
if tile:
tile_opacity = float(tile_keys.get("opacity", 1.0))
tile_scale = maxf(0.1, float(tile_keys.get("scale", 1.0)))
else:
tile = pattern_texture(Style.PATTERN)
func draw(canvas: CanvasItem, rect: Rect2) -> void:
canvas.draw_rect(rect, Style.BG)
if glow:
canvas.draw_texture_rect(glow, rect, false)
if scene:
canvas.draw_texture_rect(scene, _cover(rect, scene.get_size()), false)
if tile:
canvas.draw_set_transform(Vector2.ZERO, 0.0, Vector2.ONE * tile_scale)
var tiled := Rect2(rect.position / tile_scale, rect.size / tile_scale)
canvas.draw_texture_rect(tile, tiled, true, Color(1, 1, 1, tile_opacity))
canvas.draw_set_transform(Vector2.ZERO)
## The scene's rectangle scaled to cover `rect`, centred across and pinned
## top, centre or bottom.
func _cover(rect: Rect2, picture: Vector2) -> Rect2:
var k := maxf(rect.size.x / picture.x, rect.size.y / picture.y)
var drawn := picture * k
var x := rect.position.x + (rect.size.x - drawn.x) / 2.0
var y := rect.position.y + (rect.size.y - drawn.y) / 2.0
match scene_anchor:
"top":
y = rect.position.y
"bottom":
y = rect.end.y - drawn.y
return Rect2(Vector2(x, y), drawn)
## A small tile for the built-in pattern: "dots" (a faint grid) or "stars"
## (sparse four-point twinkles). Null for "none".
static func pattern_texture(kind: String) -> ImageTexture:
match kind:
"dots":
var img := Image.create(6, 6, false, Image.FORMAT_RGBA8)
img.fill(Color(0, 0, 0, 0))
img.set_pixel(1, 1, Color(1, 1, 1, 0.06))
return ImageTexture.create_from_image(img)
"stars":
var img := Image.create(96, 96, false, Image.FORMAT_RGBA8)
img.fill(Color(0, 0, 0, 0))
var rng := RandomNumberGenerator.new()
rng.seed = 7
for i in 9:
var p := Vector2i(rng.randi_range(2, 93), rng.randi_range(2, 93))
var a := rng.randf_range(0.05, 0.13)
img.set_pixelv(p, Color(1, 1, 1, a * 1.6))
if i % 3 == 0:
for d: Vector2i in [Vector2i(1, 0), Vector2i(-1, 0), Vector2i(0, 1), Vector2i(0, -1)]:
img.set_pixelv(p + d, Color(1, 1, 1, a * 0.7))
return ImageTexture.create_from_image(img)
return null