Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 73 additions & 10 deletions engine/src/toast/ui/document_preprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,16 @@ auto resolveColorReferences(std::string_view source, const ColorResolver& resolv
return result;
}

auto preprocessDocument(std::string_view rml, const PreprocessContext& ctx) -> DocumentScan {
ZoneScoped;
DocumentScan scan;
namespace {

struct ScanState {
DocumentScan& scan;
std::vector<std::string> aliases; // data-for loop variables, locals of the document
std::vector<std::string> member_roots; // identifiers read through . or [
std::vector<std::string> array_candidates; // plain containers of data-for
};

void scanMarkup(std::string_view rml, ScanState& state, std::vector<std::string>& template_links) {
// can attributes and {{ }} expressions
size_t i = 0;
while (i < rml.size()) {
Expand All @@ -246,7 +249,7 @@ auto preprocessDocument(std::string_view rml, const PreprocessContext& ctx) -> D
if (end == std::string_view::npos) {
break;
}
collectIdentifiers(rml.substr(i + 2, end - i - 2), scan.binds, &member_roots);
collectIdentifiers(rml.substr(i + 2, end - i - 2), state.scan.binds, &state.member_roots);
i = end + 2;
continue;
}
Expand All @@ -256,6 +259,14 @@ auto preprocessDocument(std::string_view rml, const PreprocessContext& ctx) -> D
const size_t tag_end = rml.find('>', i);
const std::string_view tag = rml.substr(i, tag_end == std::string_view::npos ? rml.size() - i : tag_end - i);

if (tag.size() > 5 && findCaseInsensitive(tag.substr(0, 5), "<link") == 0) {
const auto type = attributeValue(tag, "type");
const auto href = attributeValue(tag, "href");
if (type && href && !href->empty() && *type == "text/template") {
template_links.emplace_back(*href);
}
}

size_t a = tag.find_first_of(" \t\r\n");
if (a == std::string_view::npos) {
i = tag_end == std::string_view::npos ? rml.size() : tag_end + 1;
Expand Down Expand Up @@ -302,21 +313,21 @@ auto preprocessDocument(std::string_view rml, const PreprocessContext& ctx) -> D
}

if (name.starts_with("data-event-") || (name.starts_with("on") && value.contains('('))) {
collectEventName(value, scan.events);
collectEventName(value, state.scan.events);
} else if (name == "data-for") {
// "item : items" or "item, index : items" binds the container
std::string_view container;
parseDataFor(value, aliases, container);
parseDataFor(value, state.aliases, container);
if (!container.empty()) {
collectIdentifiers(container, scan.binds, &member_roots);
collectIdentifiers(container, state.scan.binds, &state.member_roots);
if (const auto plain = asPlainIdentifier(container); !plain.empty()) {
pushUnique(array_candidates, std::string(plain));
pushUnique(state.array_candidates, std::string(plain));
}
}
} else if (name.starts_with("data-") && name != "data-model" && !name.starts_with("data-loc-")) {
collectIdentifiers(value, scan.binds, &member_roots);
collectIdentifiers(value, state.scan.binds, &state.member_roots);
if (name == "data-checked") {
collectIdentifiers(value, scan.bool_binds);
collectIdentifiers(value, state.scan.bool_binds);
}
}
}
Expand All @@ -327,6 +338,58 @@ auto preprocessDocument(std::string_view rml, const PreprocessContext& ctx) -> D

i++;
}
}

void scanTemplates(const PreprocessContext& ctx, ScanState& state, const std::vector<std::string>& document_links) {
if (!ctx.template_loader) {
return;
}

struct Pending {
std::string referrer;
std::string href;
};

std::vector<Pending> pending;
pending.reserve(document_links.size());
for (const auto& href : document_links) {
pending.push_back({ctx.document_uri, href});
}

std::vector<std::string> visited {ctx.document_uri};
for (size_t i = 0; i < pending.size(); ++i) {
const Pending link = pending[i]; // copied, the vector grows below
const auto loaded = ctx.template_loader(link.referrer, link.href);
if (!loaded) {
TOAST_WARN("UI", "Template '{}' linked from '{}' could not be read, its binds are not bound", link.href, link.referrer);
continue;
}
if (std::ranges::find(visited, loaded->uri) != visited.end()) {
continue;
}
visited.push_back(loaded->uri);

std::vector<std::string> nested;
scanMarkup(loaded->source, state, nested);
for (auto& href : nested) {
pending.push_back({loaded->uri, std::move(href)});
}
}
}

}

auto preprocessDocument(std::string_view rml, const PreprocessContext& ctx) -> DocumentScan {
ZoneScoped;
DocumentScan scan;
ScanState state {.scan = scan};
std::vector<std::string> template_links;
scanMarkup(rml, state, template_links);
scanTemplates(ctx, state, template_links);

auto& aliases = state.aliases;
auto& member_roots = state.member_roots;
auto& array_candidates = state.array_candidates;

// loop variables are locals of their `data-for`
const auto is_alias = [&](const std::string& name) { return std::ranges::find(aliases, name) != aliases.end(); };
Expand Down
10 changes: 10 additions & 0 deletions engine/src/toast/ui/document_preprocess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,20 @@ namespace ui {

using ColorResolver = std::function<std::optional<std::string>(std::string_view)>;

struct TemplateSource {
std::string uri;
std::string source;
};

using TemplateLoader = std::function<std::optional<TemplateSource>(std::string_view referrer, std::string_view href)>;

struct PreprocessContext {
std::vector<std::string> style_uris;
ColorResolver color_resolver;
bool inject_data_model = false;

std::string document_uri;
TemplateLoader template_loader;
};

struct DocumentScan {
Expand Down
2 changes: 2 additions & 0 deletions engine/src/toast/ui/nodes/panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ void Panel::loadDocument() {
preprocess_ctx.inject_data_model = true;
preprocess_ctx.style_uris = ui.globalStyleUris();
preprocess_ctx.color_resolver = [&ui](std::string_view name) { return ui.colorHex(name); };
preprocess_ctx.document_uri = std::string(m_element.path());
preprocess_ctx.template_loader = &ui::UISystem::loadTemplateSource;

const auto scan = ui::preprocessDocument(m_element->source(), preprocess_ctx);
m_bind_store->reconcile(scan);
Expand Down
2 changes: 2 additions & 0 deletions engine/src/toast/ui/nodes/panel_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ void Panel3D::loadDocument() {
preprocess_ctx.inject_data_model = true;
preprocess_ctx.style_uris = ui.globalStyleUris();
preprocess_ctx.color_resolver = [&ui](std::string_view name) { return ui.colorHex(name); };
preprocess_ctx.document_uri = std::string(m_element.path());
preprocess_ctx.template_loader = &ui::UISystem::loadTemplateSource;

const auto scan = ui::preprocessDocument(m_element->source(), preprocess_ctx);
m_bind_store->reconcile(scan);
Expand Down
24 changes: 24 additions & 0 deletions engine/src/toast/ui/ui_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ UISystem::UISystem() noexcept {
m_asset_listener.subscribe<event::UIAssetReloaded>([this](const event::UIAssetReloaded& e) {
m_documents_reload_pending = true;
m_stylesheet_cache_dirty = m_stylesheet_cache_dirty || e.type == "ui_style" || e.type == "color_scheme";
m_template_cache_dirty = m_template_cache_dirty || e.type == "ui_element";
TOAST_INFO("UI", "Queued document reload after {} changed ({})", e.type, e.uid);
});

Expand Down Expand Up @@ -207,6 +208,10 @@ void UISystem::tick() noexcept {
Rml::Factory::ClearStyleSheetCache();
m_stylesheet_cache_dirty = false;
}
if (m_template_cache_dirty) {
Rml::Factory::ClearTemplateCache();
m_template_cache_dirty = false;
}
reloadAllDocuments();
}

Expand Down Expand Up @@ -319,6 +324,25 @@ void UISystem::unregisterGlobalStyle(const assets::Handle<assets::UIStyle>& styl
}
}

auto UISystem::loadTemplateSource(std::string_view referrer, std::string_view href) -> std::optional<TemplateSource> {
Rml::SystemInterface* system = Rml::GetSystemInterface();
if (system == nullptr) {
return std::nullopt;
}

Rml::String uri;
system->JoinPath(uri, Rml::String(referrer), Rml::String(href));
if (!uri.contains("://")) {
return std::nullopt;
}

auto bytes = assets::AssetManager::get().tryLoadBytes(uri);
if (!bytes) {
return std::nullopt;
}
return TemplateSource {.uri = std::move(uri), .source = std::string(bytes->begin(), bytes->end())};
}

auto UISystem::globalStyleUris() const -> std::vector<std::string> {
std::vector<std::string> uris;
uris.reserve(m_global_styles.size());
Expand Down
5 changes: 5 additions & 0 deletions engine/src/toast/ui/ui_system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#pragma once
#include "assets.hpp"
#include "document_preprocess.hpp"

#include <cstdint>
#include <functional>
Expand Down Expand Up @@ -106,6 +107,9 @@ class UISystem {
void setLanguage(std::string language);
void reloadAllDocuments();

[[nodiscard]]
static auto loadTemplateSource(std::string_view referrer, std::string_view href) -> std::optional<TemplateSource>;

[[nodiscard]]
auto translate(std::string_view input) const -> std::optional<std::string>;
[[nodiscard]]
Expand Down Expand Up @@ -169,6 +173,7 @@ class UISystem {

bool m_documents_reload_pending = false;
bool m_stylesheet_cache_dirty = false;
bool m_template_cache_dirty = false;

uint64_t m_next_context_id = 1; ///< context names must be unique
uint64_t m_frame_counter = 0;
Expand Down
Loading