Skip to content

Commit

Permalink
[SUTK] Fixed the root cause of incorrectly aligned Label's text; Adde…
Browse files Browse the repository at this point in the history
…d LABEL_TOGGLE_ACTIVE test

 - The cause of the bug was in SGE (Rendering backend)
 - Setting text data to text string didn't do anything if it was inactive, which resulted in incorrect values returned by IGfxDriver::getTextCoordFromGlyphIndex
  • Loading branch information
ravi688 committed Dec 30, 2024
1 parent 34b5ac6 commit 6669ec7
Show file tree
Hide file tree
Showing 14 changed files with 236 additions and 138 deletions.
3 changes: 1 addition & 2 deletions sutk/include/sutk/RenderRect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace SUTK
virtual void onGlobalCoordDirty() noexcept override;
virtual void onContainerResize(Rect2Df rect, bool isPositionChanged, bool isSizeChanged) noexcept override;
virtual void updateNormalizedDrawOrder(f32 normalizedDrawOrder) noexcept override;
virtual void onActiveUpdate(com::Bool isActive) noexcept override;

// Constructors
RenderRect(UIDriver& driver, RenderableContainer* container, RenderMode renderMode = RenderMode::Opaque) noexcept;
Expand All @@ -41,8 +42,6 @@ namespace SUTK
virtual bool isDirty() noexcept override;
virtual void update() noexcept override;

virtual void setActive(com::Bool isActive) noexcept override;

virtual Color4 getColor() const noexcept override { return m_color; }
virtual void setColor(const Color4 color) noexcept override;
};
Expand Down
8 changes: 3 additions & 5 deletions sutk/include/sutk/RenderRectArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ namespace SUTK
private:
Color4 m_color;
bool m_isColorDirty;
bool m_isActiveDirty;
std::vector<Rect2Df> m_rects;
bool m_isRectsDirty;

protected:
// overrides of Renderable::onGlobalCoordDirty(), and onContainerResize
virtual void onGlobalCoordDirty() noexcept override;
virtual void onContainerResize(Rect2Df rect, bool isPositionChanged, bool isSizeChanged) noexcept override;

virtual void onActiveUpdate(com::Bool isActive) noexcept override;
virtual void updateNormalizedDrawOrder(f32 normalizedDrawOrder) noexcept override;

public:
// Constructors
Expand All @@ -29,9 +30,6 @@ namespace SUTK
virtual bool isDirty() override;
virtual void update() override;

// Overrides of Renderable::setActive
virtual void setActive(com::Bool isActive) noexcept override;

const std::vector<Rect2Df>& getRects() const noexcept { return m_rects; }
std::vector<Rect2Df>& getRectsForWrite() noexcept;

Expand Down
12 changes: 5 additions & 7 deletions sutk/include/sutk/Renderable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ namespace SUTK
bool m_isDrawOrderDirty;
// Initially it is set to false
com::Bool m_isActiveDirty { };
com::Bool m_isRedraw;

// Called by UIDriver to reset the redraw flag
void setRedraw(com::Bool isRedraw) noexcept { m_isRedraw = isRedraw; }
// This must never be called outside of Renderable (at least for now)
void setDrawOrder(u32 drawOrder) { m_drawOrder = drawOrder; m_isDrawOrderDirty = true; }

Expand All @@ -46,13 +43,17 @@ namespace SUTK
// updates draw order (z-buffer) values to the GPU side memory.
// mandatory to be called in the overridng method
virtual void updateNormalizedDrawOrder(f32 normalizedDrawOrder) { m_isDrawOrderDirty = false; }
// This function onActiveUpdate() is called inside Renderable::update() whenever isActiveDirty() returns true
// A deriving class can override this method to responde to activate and deactivate status changes.
// isActive: new value
virtual void onActiveUpdate(com::Bool isActive) noexcept { }

public:
Renderable(UIDriver& driver, RenderableContainer* container = NULL) noexcept;
virtual ~Renderable() noexcept;

// Override of Activatable::setActive()
virtual void setActive(com::Bool isActive) noexcept;
virtual void setActive(com::Bool isActive) noexcept override final;

// returns true, if GPU side data is out of sync with respect to the CPU side data, otherwise false
// Must be called in the overriding method
Expand All @@ -73,9 +74,6 @@ namespace SUTK

u32 getDrawOrder() const noexcept { return m_drawOrder; }

com::Bool isRedraw() const noexcept { return com::Bool { m_isRedraw || com::cast_away_const(this)->isDirty() || isDrawOrderDirty() }; }
void redraw() noexcept { m_isRedraw = com::True; }

bool ensureUpdated() noexcept
{
if(isDirty())
Expand Down
34 changes: 26 additions & 8 deletions sutk/include/sutk/SmallText.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,31 @@ namespace SUTK
class SUTK_API SmallText : public GfxDriverRenderable, public IColorable
{
private:
bool m_isPosDirty;
bool m_isDataDirty;
bool m_isPointSizeDirty;
bool m_isColorDirty;
bool m_isColorRangesDirty;
struct DirtyTable
{
bool isPosDirty;
bool isDataDirty;
bool isPointSizeDirty;
bool isColorDirty;
bool isColorRangesDirty;
void clear() noexcept
{
isPosDirty = false;
isDataDirty = false;
isPointSizeDirty = false;
isColorDirty = false;
isColorRangesDirty = false;
}
bool isAny() noexcept
{
return isPosDirty
|| isDataDirty
|| isPointSizeDirty
|| isColorDirty
|| isColorRangesDirty;
}
};
DirtyTable m_dirtyTable {};
SmallTextData m_data;
Color4 m_color;
std::vector<ColorRange> m_colorRanges;
Expand All @@ -90,6 +110,7 @@ namespace SUTK
virtual void onGlobalCoordDirty() noexcept override;
virtual void onContainerResize(Rect2Df rect, bool isPositionChanged, bool isSizeChanged) noexcept override;
virtual void updateNormalizedDrawOrder(f32 normalizedDrawOrder) override;
virtual void onActiveUpdate(com::Bool isActive) noexcept override;
public:

SmallText(UIDriver& driver, RenderableContainer* container, GfxDriverObjectHandleType textGroup, Color4 color = SUTK::Color4::white()) noexcept;
Expand All @@ -103,9 +124,6 @@ namespace SUTK
virtual void setColor(Color4 color) noexcept override;
virtual Color4 getColor() const noexcept override;

// Override of Renderable::setActive()
virtual void setActive(com::Bool isActive) noexcept override;

void clearColorRanges() noexcept;
void addColorRange(std::size_t pos, std::size_t len, const Color4 color) noexcept;

Expand Down
3 changes: 1 addition & 2 deletions sutk/include/sutk/Text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ namespace SUTK
virtual void onGlobalCoordDirty() noexcept override;
virtual void onContainerResize(Rect2Df rect, bool isPositionChanged, bool isSizeChanged) noexcept override;
virtual void updateNormalizedDrawOrder(f32 normalizedDrawOrder) override;
virtual void onActiveUpdate(com::Bool isActive) noexcept override;
public:

Vec2Df getLocalPositionFromCursorPosition(const CursorPosition<LineCountType>& cursor) noexcept;
Expand All @@ -152,8 +153,6 @@ namespace SUTK
virtual bool isDirty() override;
virtual void update() override;

virtual void setActive(com::Bool isActive) noexcept override;

void clear() noexcept;

void setFont(UIDriver::FontReference font) noexcept;
Expand Down
36 changes: 36 additions & 0 deletions sutk/include/sutk/tests/LabelActiveToggleTest.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <sutk/ITest.hpp>

#include <sge-cpp/sge.hpp>

#include <sutk/UIDriver.hpp>

namespace SUTK
{
class IGfxDriver;
class Label;

class LabelActiveToggleTest : public ITest
{
private:
UIDriver* m_uiDriver;
IGfxDriver* m_gfxDriver;
IInputDriver* m_inputDriver;
FullWindowContainer* m_rootContainer { };
Label* m_label { };

public:
LabelActiveToggleTest() : m_uiDriver(NULL), m_gfxDriver(NULL), m_inputDriver(NULL) { }

TestInitializationData getInitializationData() override;

void initialize(SGE::Driver& driver) override;

void terminate(SGE::Driver& driver) override;

void render(SGE::Driver& driver) override;

void update(SGE::Driver& driver, float deltaTime) override;
};
}
4 changes: 3 additions & 1 deletion sutk/source/ITest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <sutk/tests/LabelTest.hpp>
#include <sutk/tests/PassiveLabelTest.hpp>
#include <sutk/tests/DrawOrderTest.hpp>
#include <sutk/tests/LabelActiveToggleTest.hpp>

namespace SUTK
{
Expand Down Expand Up @@ -67,7 +68,8 @@ namespace SUTK
{ "PANEL", [] () { return std::unique_ptr<ITest>(new PanelTest()); }},
{ "LABEL", [] () { return std::unique_ptr<ITest>(new LabelTest()); }},
{ "PASSIVE_LABEL", [] () { return std::unique_ptr<ITest>(new PassiveLabelTest()); }},
{ "DRAW_ORDER", [] () { return std::unique_ptr<ITest>(new DrawOrderTest()); }}
{ "DRAW_ORDER", [] () { return std::unique_ptr<ITest>(new DrawOrderTest()); }},
{ "LABEL_ACTIVE_TOGGLE", [] () { return std::unique_ptr<ITest>(new LabelActiveToggleTest()); }}
};

std::unique_ptr<ITest> ITest::Create(const std::string& testName)
Expand Down
24 changes: 11 additions & 13 deletions sutk/source/RenderRect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,6 @@ namespace SUTK
getUIDriver().getGfxDriver().destroyGeometry(getGfxDriverObjectHandle());
}

void RenderRect::setActive(com::Bool isActive) noexcept
{
// mandatory to call
GeometryRenderable::setActive(isActive);
// Activation and Deactivation Updates must happen immediately
if(auto handle = getGfxDriverObjectHandle(); handle != GFX_DRIVER_OBJECT_NULL_HANDLE)
{
GfxDriverObjectHandleType obj = getGfxDriver().getGeometryObject(handle);
getGfxDriver().setObjectActive(obj, isActive);
}
}

bool RenderRect::isDirty() noexcept
{
return GeometryRenderable::isDirty() || isRectDirty() || m_isColorDirty || m_isGeometryDirty;
Expand Down Expand Up @@ -71,14 +59,24 @@ namespace SUTK
void RenderRect::updateNormalizedDrawOrder(f32 normalizedDrawOrder) noexcept
{
// mandatory to be called in the overriding function
Renderable::updateNormalizedDrawOrder(normalizedDrawOrder);
GeometryRenderable::updateNormalizedDrawOrder(normalizedDrawOrder);

GfxDriverObjectHandleType handle = getGfxDriverObjectHandle();
_com_assert(handle != GFX_DRIVER_OBJECT_NULL_HANDLE);
handle = getGfxDriver().getGeometryObject(handle);
getGfxDriver().setObjectDepth(handle, normalizedDrawOrder);
}

void RenderRect::onActiveUpdate(com::Bool isActive) noexcept
{
// Activation and Deactivation Updates must happen immediately
if(auto handle = getGfxDriverObjectHandle(); handle != GFX_DRIVER_OBJECT_NULL_HANDLE)
{
GfxDriverObjectHandleType obj = getGfxDriver().getGeometryObject(handle);
getGfxDriver().setObjectActive(obj, isActive);
}
}

void RenderRect::setColor(Color4 color) noexcept
{
m_color = color;
Expand Down
39 changes: 20 additions & 19 deletions sutk/source/RenderRectArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace SUTK
RenderRectFillArray::RenderRectFillArray(UIDriver& driver, RenderableContainer* container, RenderMode renderMode) noexcept : GeometryRenderable(driver, container, renderMode),
m_color(Color4::white()),
m_isColorDirty(true),
m_isActiveDirty(false),
m_isRectsDirty(false)
{
_com_assert(container != NULL);
Expand Down Expand Up @@ -36,22 +35,13 @@ namespace SUTK

bool RenderRectFillArray::isDirty()
{
return GeometryRenderable::isDirty() || m_isRectsDirty || m_isColorDirty || m_isActiveDirty;
return GeometryRenderable::isDirty() || m_isRectsDirty || m_isColorDirty;
}

void RenderRectFillArray::update()
{
// Mandatory to be called in the overriding method
GeometryRenderable::update();
if(m_isActiveDirty)
{
auto handle = getGfxDriverObjectHandle();
_com_assert(handle != GFX_DRIVER_OBJECT_NULL_HANDLE);
auto obj = getGfxDriver().getGeometryObject(handle);
getGfxDriver().setObjectActive(obj, isActive());
m_isActiveDirty = false;
}

if(m_isRectsDirty)
{
Geometry::InstanceTransformArray& transformArray = getGeometry().getInstanceTransformArrayForWrite();
Expand All @@ -62,7 +52,6 @@ namespace SUTK
transformArray.push_back(_rect);
}
}

if(m_isColorDirty)
{
getGeometry().fillColor(getColor());
Expand All @@ -78,13 +67,6 @@ namespace SUTK
}
}

void RenderRectFillArray::setActive(com::Bool isActive) noexcept
{
// mandatory to call
GeometryRenderable::setActive(isActive);
m_isActiveDirty = true;
}

std::vector<Rect2Df>& RenderRectFillArray::getRectsForWrite() noexcept
{
m_isRectsDirty = true;
Expand All @@ -106,4 +88,23 @@ namespace SUTK
{
m_isRectsDirty = true;
}

void RenderRectFillArray::updateNormalizedDrawOrder(f32 normalizedDrawOrder) noexcept
{
// mandatory to be called in the overriding function
GeometryRenderable::updateNormalizedDrawOrder(normalizedDrawOrder);

GfxDriverObjectHandleType handle = getGfxDriverObjectHandle();
_com_assert(handle != GFX_DRIVER_OBJECT_NULL_HANDLE);
handle = getGfxDriver().getGeometryObject(handle);
getGfxDriver().setObjectDepth(handle, normalizedDrawOrder);
}

void RenderRectFillArray::onActiveUpdate(com::Bool isActive) noexcept
{
auto handle = getGfxDriverObjectHandle();
_com_assert(handle != GFX_DRIVER_OBJECT_NULL_HANDLE);
auto obj = getGfxDriver().getGeometryObject(handle);
getGfxDriver().setObjectActive(obj, isActive);
}
}
7 changes: 5 additions & 2 deletions sutk/source/Renderable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace SUTK
{
Renderable::Renderable(UIDriver& driver, RenderableContainer* container) noexcept : UIDriverObject(driver), m_container(container), m_drawOrder(0), m_isDrawOrderDirty(true), m_isRedraw(com::False)
Renderable::Renderable(UIDriver& driver, RenderableContainer* container) noexcept : UIDriverObject(driver), m_container(container), m_drawOrder(0), m_isDrawOrderDirty(true)
{
if(container != NULL)
{
Expand Down Expand Up @@ -38,15 +38,18 @@ namespace SUTK
if(Activatable::isActive() != isActive)
m_isActiveDirty = com::True;
Activatable::setActive(isActive);
m_isDrawOrderDirty = true;
}

bool Renderable::isDirty()
{
return static_cast<bool>(m_isActiveDirty);
return static_cast<bool>(m_isActiveDirty) || isActiveDirty();
}

void Renderable::update()
{
if(m_isActiveDirty)
onActiveUpdate(isActive());
m_isActiveDirty = com::False;
}

Expand Down
Loading

0 comments on commit 6669ec7

Please sign in to comment.