Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RmlUI Context handling improvements #1761

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
44 changes: 35 additions & 9 deletions rts/Rml/Backends/RmlUi_Backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,34 @@ void RmlGui::OnContextDestroy(Rml::Context* context)
state->contexts.erase(std::ranges::find(state->contexts, context));
}

Rml::Context* RmlGui::GetOrCreateContext(const std::string& name)
{
if (!RmlInitialized()) {
return nullptr;
}

Rml::Context* context = Rml::GetContext(name);
if (context == nullptr) {
context = Rml::CreateContext(name, {0, 0});
} else if (state->contexts_to_remove.contains(context)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else if (state->contexts_to_remove.contains(context)) {
} else if (state->contexts_to_remove.contains(context)) {
// can happen if name reused on the same frame

state->contexts_to_remove.erase(context);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could do without the .contains check? .erase has to search for the element anyway so we'd avoid searching it twice if present and won't see any difference if not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't realize I could do that

}

return context;
}

Rml::Context* RmlGui::GetContext(const std::string& name) {
if (!RmlInitialized()) {
return nullptr;
}

Rml::Context* context = Rml::GetContext(name);
if (context != nullptr && !state->contexts_to_remove.contains(context)) {
return context;
}
return nullptr;
}

void RmlGui::MarkContextForRemoval(Rml::Context *context) {
if (!RmlInitialized() || context == nullptr) {
return;
Expand All @@ -326,16 +354,14 @@ void RmlGui::Update()
for (const auto& context : state->contexts) {
context->Update();
}


// move clicked context to top
if (state->clicked_context) {
auto context_pos = std::ranges::find(state->contexts, state->clicked_context);
if (context_pos != state->contexts.end()) {
// debug context is always to be on top
auto start = state->contexts.begin() + (state->debug_host_context ? 1 : 0);
if (context_pos != start) {
state->contexts.erase(context_pos);
state->contexts.insert(start, state->clicked_context);
}
// debug context is always to be at index 0 so it renders on top
auto start = state->contexts.begin() + (state->debug_host_context ? 1 : 0);
auto context_pos = std::ranges::find(start, state->contexts.end(), state->clicked_context);
if (context_pos != start && context_pos != state->contexts.end()) {
std::ranges::rotate(start, context_pos, context_pos + 1);
}
state->clicked_context = nullptr;
}
Expand Down
3 changes: 3 additions & 0 deletions rts/Rml/Backends/RmlUi_Backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ namespace RmlGui

void OnContextCreate(Rml::Context* context);
void OnContextDestroy(Rml::Context* context);

Rml::Context* GetOrCreateContext(const std::string& name);
Rml::Context* GetContext(const std::string& name);
void MarkContextForRemoval(Rml::Context* context);

void BeginFrame();
Expand Down
6 changes: 3 additions & 3 deletions rts/Rml/SolLua/bind/Global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace Rml::SolLua
{
return Rml::RegisterEventType(type, interruptible, bubbles, Rml::DefaultActionPhase::None);
}

auto removeContext(Rml::Context* context) {
RmlGui::MarkContextForRemoval(context);
}
Expand Down Expand Up @@ -107,7 +107,7 @@ namespace Rml::SolLua
"CreateContext", [slp](const Rml::String& name) {
// context will be resized right away by other code
// send {0, 0} in to avoid triggering a pointless resize event in the Rml code
auto context = Rml::CreateContext(name, {0, 0});
auto context = RmlGui::GetOrCreateContext(name);
if (context != nullptr) {
slp->AddContextTracking(context);
}
Expand All @@ -124,7 +124,7 @@ namespace Rml::SolLua
),
//"RegisterTag",
//--
"GetContext", sol::resolve<Rml::Context* (const Rml::String&)>(&Rml::GetContext),
"GetContext", sol::resolve<Rml::Context* (const Rml::String&)>(&RmlGui::GetContext),
"RegisterEventType", sol::overload(&functions::registerEventType4, &functions::registerEventType3),
"AddTranslationString", [translationTable](const Rml::String& key, const Rml::String& translation, sol::this_state s) {
return translationTable->addTranslation(key, translation);
Expand Down