Mod API: UI service (#2189)

This commit is contained in:
Luke Street
2026-07-09 10:31:34 -06:00
committed by GitHub
parent f3ca94b338
commit 6849baac06
33 changed files with 2385 additions and 66 deletions
+60 -2
View File
@@ -5,6 +5,8 @@
#include "m_Do/m_Do_audio.h"
#include <algorithm>
namespace dusk::ui {
namespace {
@@ -18,8 +20,16 @@ Rml::ElementDocument* load_document(const Rml::String& source) {
} // namespace
Document::Document(const Rml::String& source, bool passive)
: mDocument(load_document(source)), mPassive(passive) {
Document::Document(const Rml::String& source, bool passive, DocumentScope scope)
: mDocument(load_document(source)), mScope(scope), mPassive(passive) {
if (mDocument != nullptr) {
if (const auto* base = mDocument->GetStyleSheetContainer()) {
// Clone a pristine snapshot to rebuild from on every restyle
mBaseStyleSheets = base->CombineStyleSheetContainer(Rml::StyleSheetContainer{});
}
apply_scoped_styles(*this);
}
// Block events while hidden (except for Menu command); play nav sounds when visible
listen(
Rml::EventId::Keydown,
@@ -91,6 +101,51 @@ bool Document::focus() {
return false;
}
bool Document::set_document_styles(const Rml::String& rcss) {
if (rcss.empty()) {
mDocumentStyleSheets = nullptr;
} else {
auto sheet = Rml::Factory::InstanceStyleSheetString(rcss);
if (sheet == nullptr) {
return false;
}
mDocumentStyleSheets = std::move(sheet);
}
apply_scoped_styles(*this);
return true;
}
void Document::restyle(std::span<const Rml::StyleSheetContainer* const> sheets) {
if (mDocument == nullptr) {
return;
}
const bool wantsExtra =
mDocumentStyleSheets != nullptr ||
std::ranges::any_of(sheets, [](const auto* sheet) { return sheet != nullptr; });
// Nothing to add
if (!wantsExtra && !mRestyled) {
return;
}
auto combined = mBaseStyleSheets;
const auto combine = [&combined](const Rml::StyleSheetContainer& sheet) {
if (combined != nullptr) {
combined = combined->CombineStyleSheetContainer(sheet);
} else {
combined = sheet.CombineStyleSheetContainer(Rml::StyleSheetContainer{});
}
};
for (const auto* sheet : sheets) {
if (sheet != nullptr) {
combine(*sheet);
}
}
if (mDocumentStyleSheets != nullptr) {
combine(*mDocumentStyleSheets);
}
mDocument->SetStyleSheetContainer(std::move(combined));
mRestyled = wantsExtra;
}
void Document::listen(Rml::Element* element, Rml::EventId event,
ScopedEventListener::Callback callback, bool capture) {
if (element == nullptr) {
@@ -139,6 +194,9 @@ bool Document::handle_nav_event(Rml::Event& event) {
bool Document::handle_nav_command(Rml::Event& event, NavCommand cmd) {
if (cmd == NavCommand::Menu) {
if (game_obscured_below(*this)) {
return true;
}
mDoAud_seStartMenu(visible() ? kSoundMenuClose : kSoundMenuOpen);
toggle();
return true;