Flint Engine
Modular C++20 2D game engine powered by SDL3
Loading...
Searching...
No Matches
ui-builder.h
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2025-2026 Manuel Osorio
3#pragma once
4#include "flint/core/log.h"
5#include "flint/ui/ui-context.h"
6#include "flint/ui/ui-node.h"
7#include "flint/ui/ui-parser.h"
8
9#include <functional>
10#include <memory>
11#include <unordered_map>
12#include <unordered_set>
13
14namespace Flint {
15 class UIBuilder {
16 public:
17 UIBuilder(IAssetLoader &assetLoader, SDL_GPUDevice *device);
18 void build(UIContext &context, const Context &parsedData);
19
20 private:
21 IAssetLoader &m_assetLoader;
22 UIContext *m_rootContext = nullptr;
23 using FactoryFunc = std::function<std::unique_ptr<UINode>(const Node *)>;
24 std::unordered_map<std::string, FactoryFunc> m_registry;
25
26 std::unordered_map<std::string, std::shared_ptr<GPUTexture>> m_textureCache;
27 std::unordered_set<std::string> m_usedTextures;
28
29 std::shared_ptr<GPUTexture> loadTexture(const std::string &src, SDL_GPUDevice *device);
30
31 void registerComponent(const std::string &type,
32 std::function<std::unique_ptr<UINode>(const Node &)> func) {
33 m_registry[type] = [func = std::move(func)](const Node *n) -> std::unique_ptr<UINode> {
34 return n ? func(*n) : nullptr;
35 };
36 }
37
38 template <typename F, typename = std::enable_if_t<std::is_invocable_v<F>>>
39 void registerComponent(const std::string &type, F &&func) {
40 m_registry[type] = [func = std::forward<F>(func)](const Node *) -> std::unique_ptr<UINode> {
41 return func();
42 };
43 }
44
45 std::unique_ptr<UINode> buildNode(const Node &dataNode);
46
47 void applyAttributes(UINode *node, const Node &dataNode);
48
49 SDL_GPUDevice *m_device = nullptr;
50 };
51}
Abstract interface for loading raw asset data by path.
Manages an isolated user interface canvas workspace sandbox, hosting its own independent node tree an...
Definition ui-context.h:27
Abstract base class for all UI widgets, managing hierarchy tracking, layout passes,...
Definition ui-node.h:30