UI improvements ++

This commit is contained in:
dingo
2026-04-14 16:02:24 -04:00
parent 7a88fd96b9
commit ad031a8e30
26 changed files with 1094 additions and 203 deletions
@@ -15,6 +15,7 @@ extends Node
## get_color() when get_texture() returns null.
signal loaded
signal texture_loaded(type: String)
const CACHE_PATH := "user://highlight_config.json"
const REQUEST_TYPE := "content.get_highlight_config"
@@ -30,6 +31,7 @@ const DEFAULT_HEX : Dictionary = {
## Whether the server config has been loaded (or the cache has been used).
var is_ready : bool = false
var _fetch_in_flight : bool = false
# ── Internal state ─────────────────────────────────────────────────────────────
@@ -50,7 +52,11 @@ var _pending_fetches : Dictionary = {}
func _ready() -> void:
_load_cache()
_fetch_from_server()
_prefetch_known_textures()
Auth.auth_success.connect(_on_auth_success)
Network.connected.connect(_on_network_connected)
if not Auth.user_id.is_empty():
_fetch_from_server()
# ── Public API ─────────────────────────────────────────────────────────────────
@@ -58,6 +64,8 @@ func _ready() -> void:
## Returns the loaded Texture2D for this highlight type, or null if not yet loaded.
## Starts loading the texture on first call if not already in progress.
func get_texture(type: String) -> Texture2D:
if not is_ready:
_fetch_from_server()
var key := type.to_lower()
if _textures.has(key):
return _textures[key]
@@ -70,6 +78,8 @@ func get_texture(type: String) -> Texture2D:
## Returns the fallback Color for this highlight type.
func get_color(type: String) -> Color:
if not is_ready:
_fetch_from_server()
var key := type.to_lower()
var cfg := _effective_config(key)
var hex : String = cfg.get("fallbackHex", DEFAULT_HEX.get(key, "#FFFFFF66"))
@@ -118,20 +128,29 @@ func _effective_config(key: String) -> Dictionary:
func _fetch_from_server() -> void:
if _fetch_in_flight:
return
if not Network.is_online():
return
if Auth.user_id.is_empty():
return
_fetch_in_flight = true
Network.request(
REQUEST_TYPE,
{},
func(_type: String, resp: Variant) -> void:
_fetch_in_flight = false
_on_server_response(resp),
func(_code: String, _msg: String) -> void:
# Use cached config if available; set is_ready anyway.
is_ready = true
loaded.emit()
func(code: String, msg: String) -> void:
_fetch_in_flight = false
if code not in ["DISCONNECTED", "AUTH_REQUIRED"]:
push_warning("HighlightConfig: config fetch failed - %s %s" % [code, msg])
)
func _on_server_response(resp: Variant) -> void:
var configs : Array = (resp as Dictionary).get("configs", [])
_server_configs.clear()
for entry : Dictionary in configs:
var key : String = str(entry.get("highlightType", "")).to_lower()
if key.is_empty():
@@ -144,34 +163,51 @@ func _on_server_response(resp: Variant) -> void:
"fallbackHex": str(entry.get("fallbackHex", DEFAULT_HEX.get(key, "#FFFFFF66"))),
}
_save_cache()
_prefetch_known_textures()
is_ready = true
loaded.emit()
func _on_auth_success(_user_id: String, _display_name: String) -> void:
_fetch_from_server()
func _on_network_connected() -> void:
if not Auth.user_id.is_empty():
_fetch_from_server()
func _start_texture_load(key: String, cfg: Dictionary) -> void:
if _pending_fetches.has(key):
return
_pending_fetches[key] = true
var url : String = cfg.get("cdnPath", "")
var url := _normalize_url(str(cfg.get("cdnPath", "")))
if url.is_empty():
return
_pending_fetches[key] = true
var http := HTTPRequest.new()
add_child(http)
http.request_completed.connect(
func(result: int, _code: int, _headers: PackedStringArray, body: PackedByteArray) -> void:
func(result: int, code: int, _headers: PackedStringArray, body: PackedByteArray) -> void:
http.queue_free()
_pending_fetches.erase(key)
if result != HTTPRequest.RESULT_SUCCESS or body.is_empty():
if result != HTTPRequest.RESULT_SUCCESS or code != 200 or body.is_empty():
push_warning("HighlightConfig: failed to download %s (result=%d http=%d url=%s)" % [key, result, code, url])
return
var img := Image.new()
if img.load_png_from_buffer(body) != OK:
if img.load_webp_from_buffer(body) != OK:
return
var err := _load_image_from_body(img, url, body)
if err != OK:
push_warning("HighlightConfig: failed to decode %s from %s" % [key, url])
return
var tex := ImageTexture.create_from_image(img)
_textures[key] = tex
texture_loaded.emit(key)
)
http.request(url)
var err := http.request(url)
if err != OK:
_pending_fetches.erase(key)
http.queue_free()
push_warning("HighlightConfig: request start failed for %s (err=%d url=%s)" % [key, err, url])
func _parse_hex(hex: String) -> Color:
@@ -202,8 +238,39 @@ func _load_cache() -> void:
func _save_cache() -> void:
DirAccess.make_dir_recursive_absolute("user://")
var f := FileAccess.open(CACHE_PATH, FileAccess.WRITE)
if f == null:
return
f.store_string(JSON.stringify(_server_configs))
f.close()
func _prefetch_known_textures() -> void:
for key in _server_configs.keys():
var cfg := _effective_config(str(key))
if not cfg.is_empty():
_start_texture_load(str(key), cfg)
for key in _overrides.keys():
var cfg := _effective_config(str(key))
if not cfg.is_empty():
_start_texture_load(str(key), cfg)
func _normalize_url(url: String) -> String:
return url.strip_edges().replace(" ", "%20")
func _load_image_from_body(img: Image, url: String, body: PackedByteArray) -> Error:
var ext := url.split("?")[0].get_extension().to_lower()
if ext in ["jpg", "jpeg"]:
return img.load_jpg_from_buffer(body)
if ext == "webp":
return img.load_webp_from_buffer(body)
var png_err := img.load_png_from_buffer(body)
if png_err == OK:
return OK
var webp_err := img.load_webp_from_buffer(body)
if webp_err == OK:
return OK
return img.load_jpg_from_buffer(body)