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

Disable RmlUI document if scene is not visible #517

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
28 changes: 27 additions & 1 deletion Source/Urho3D/RmlUI/RmlUIComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "../Core/Context.h"
#include "../Graphics/Material.h"
#include "../Graphics/Renderer.h"
#include "../IO/Log.h"
#include "../Resource/BinaryFile.h"
#include "../RmlUI/RmlCanvasComponent.h"
Expand Down Expand Up @@ -73,6 +74,8 @@ void RmlUIComponent::RegisterObject(Context* context)

void RmlUIComponent::Update(float timeStep)
{
UpdateIsSceneActive();

navigationManager_->Update();
// There should be only a few of RmlUIComponent enabled at a time, so this is not a performance issue.
UpdateConnectedCanvas();
Expand Down Expand Up @@ -360,9 +363,32 @@ void RmlUIComponent::RemoveDataModel()
dataModelName_.clear();
}

void RmlUIComponent::UpdateIsSceneActive()
{
auto* renderer = GetSubsystem<Renderer>();
if (renderer)
{
bool isSceneActive = false;
for (unsigned viewportIndex = 0; viewportIndex < renderer->GetNumViewports(); ++viewportIndex)
{
const auto* viewport = renderer->GetViewport(viewportIndex);
if (viewport)
{
isSceneActive = viewport->GetScene() == GetScene();
break;
}
}
if (isSceneActive_ != isSceneActive)
{
isSceneActive_ = isSceneActive;
UpdateDocumentOpen();
}
}
}

void RmlUIComponent::UpdateDocumentOpen()
{
const bool shouldBeOpen = IsEnabledEffective() && !resource_.name_.empty();
const bool shouldBeOpen = isSceneActive_ && IsEnabledEffective() && !resource_.name_.empty();
const bool isOpen = document_ != nullptr;

if (shouldBeOpen && !isOpen)
Expand Down
3 changes: 3 additions & 0 deletions Source/Urho3D/RmlUI/RmlUIComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class URHO3D_API RmlUIComponent : public LogicComponent
void UpdateDocumentOpen();
void UpdateConnectedCanvas();

void UpdateIsSceneActive();
Rml::DataModelConstructor CreateDataModel();
void RemoveDataModel();

Expand Down Expand Up @@ -185,6 +186,8 @@ class URHO3D_API RmlUIComponent : public LogicComponent
Rml::DataModelHandle dataModel_;
/// Name of the data model.
ea::string dataModelName_;
/// On last update was the component's scene active or not.
bool isSceneActive_{true};
};

}