Skip to content

Commit

Permalink
[SUTK] Various fixes, and debugging improvements (more diagnostic mes…
Browse files Browse the repository at this point in the history
…sages upon invalid usage of the API)
  • Loading branch information
ravi688 committed Nov 9, 2024
1 parent d127bf0 commit c71a21c
Show file tree
Hide file tree
Showing 17 changed files with 103 additions and 14 deletions.
1 change: 1 addition & 0 deletions sutk/include/sutk/ButtonGraphic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ namespace SUTK

public:
DefaultButtonGraphicNoLabel(UIDriver& driver, Container* parent) noexcept;
~DefaultButtonGraphicNoLabel() noexcept;

RenderRectFillRound& getRenderRect() noexcept { return *m_renderRect; }
};
Expand Down
3 changes: 2 additions & 1 deletion sutk/include/sutk/Container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ namespace SUTK
protected:
// this can only be called by SUTK::UIDriver
Container(SUTK::UIDriver& driver, Container* parent = NULL, com::Bool isLayoutIgnore = com::Bool::False(), Layer layer = InvalidLayer);
virtual ~Container();

friend class UIDriver;

Expand Down Expand Up @@ -110,6 +109,7 @@ namespace SUTK
void setParentChildRelation(Container* parent, std::size_t index = std::numeric_limits<std::size_t>::max()) noexcept;

public:
virtual ~Container();

// IMPLEMENTATION of IDebuggable
virtual void enableDebug(bool isEnable = true, Color4 color = Color4::green()) noexcept override;
Expand All @@ -136,6 +136,7 @@ namespace SUTK
// childs getters
std::vector<Container*>& getChilds() noexcept { return getContainerList(); }
const std::vector<Container*>& getChilds() const noexcept { return getContainerList(); }
std::size_t getChildCount() const noexcept { return getChilds().size(); }


// parent getters
Expand Down
2 changes: 2 additions & 0 deletions sutk/include/sutk/ContainerUtility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace SUTK
static void RenderablesVisit(Container* container, const std::function<void(Renderable*)>& visitor) noexcept;
static void ContainersWalkUpUntil(Container* container, const std::function<bool(Container*)>& visitor) noexcept;
static void ContainersVisit(Container* container, const std::function<void(Container*)>& visitor) noexcept;
static void ContainersDestroyRecursive(Container* container) noexcept;
static void ContainersVisitChildEnd(Container* container, const std::function<void(Container*)>& visitor) noexcept;
static GfxDriverObjectHandleType findTextGroupHandle(Container* container) noexcept;

typedef void (*IInputEventHandlerObjectsVisitor)(std::vector<IInputEventHandlerObject*>& eventHandlerObj);
Expand Down
21 changes: 21 additions & 0 deletions sutk/include/sutk/ImageContainer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <sutk/UIDriver.hpp> // for UIDriver::ImageReference
#include <sutk/RenderableContainer.hpp>

namespace SUTK
{
class RenderImage;

class SUTK_API ImageContainer : public RenderableContainer
{
private:
RenderImage* m_renderImage;
public:
ImageContainer(UIDriver& driver, Container* parent, com::Bool isLayoutIgnore = com::Bool::False(), Layer layer = InvalidLayer) noexcept;
~ImageContainer() noexcept;

void setImage(UIDriver::ImageReference image) noexcept;
RenderImage* getRenderImage() noexcept { return m_renderImage; }
};
}
3 changes: 3 additions & 0 deletions sutk/include/sutk/NotebookView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <common/Concepts.hpp> // for com::CompareFunction

#include <optional> // for std::optional

namespace SUTK
{
Expand All @@ -20,6 +21,7 @@ namespace SUTK
private:
void* m_data;
void (*m_dataDeleter)(void*);
std::optional<std::function<void(NotebookPage*)>> m_onPageRemove;
TabView* m_tabView;
Container* m_container;

Expand All @@ -31,6 +33,7 @@ namespace SUTK
public:
NotebookPage(Container* container) noexcept;
~NotebookPage() noexcept;
void setOnRemove(std::function<void(NotebookPage*)> onPageRemove) noexcept { m_onPageRemove = onPageRemove; }
void setLabel(const std::string_view str) noexcept;
Container* getContainer() noexcept { return m_container; }
u32 getIndex() const noexcept;
Expand Down
5 changes: 2 additions & 3 deletions sutk/include/sutk/Renderable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ namespace SUTK
class Renderable : public UIDriverObject, public Activatable
{
friend class UIDriver;
friend class Container;
friend class RenderableContainer;

private:
RenderableContainer* m_container;
u32 m_drawOrder;
bool m_isDrawOrderDirty;

friend class Container;

protected:
friend class RenderableContainer;
// Rendering Geometries (or Text) requires conversion from local coordinates to global coordinates (target rendering backend, SGE)
// So, whenever any of the Anscestor containers change their position, the old transformed coordinates become invalid (dirty),
// and this requires to walk recursively up the container tree to finally calculate the new global coordiantes with possibly same
Expand Down
5 changes: 3 additions & 2 deletions sutk/include/sutk/RenderableContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ namespace SUTK

class RenderableContainer : public Container
{
friend class Renderable;
friend class UIDriver;
private:
Renderable* m_renderable;

friend class UIDriver;

protected:

RenderableContainer(UIDriver& driver, Container* parent, com::Bool isLayoutIgnore = com::Bool::False(), Layer layer = InvalidLayer) noexcept;
~RenderableContainer() noexcept;

virtual void onAdd(Container* parent) override;
virtual void onParentResize(const Rect2Df& newRect, bool isPositionChanged, bool isSizeChanged) override;
Expand Down
2 changes: 2 additions & 0 deletions sutk/include/sutk/TextGroupContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace SUTK
class TextGroup;
class SUTK_API TextGroupContainer : public RenderableContainer
{
private:
TextGroup* m_textGroup;
public:
TextGroupContainer(UIDriver& driver, Container* parent = NULL, com::Bool isLayoutIgnore = com::Bool::False(), Layer layer = InvalidLayer) noexcept;

Expand Down
2 changes: 1 addition & 1 deletion sutk/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ OBJECTS= $(addsuffix .o, $(SOURCES))
LIBS = $(EXTERNAL_LIBRARIES)

#Flags and Defines
DEBUG_DEFINES = -DGLOBAL_DEBUG -DDEBUG -DLOG_DEBUG
DEBUG_DEFINES = -DGLOBAL_DEBUG -DDEBUG -DLOG_DEBUG -DCONTAINER_DEBUG
RELEASE_DEFINES = -DGLOBAL_RELEASE -DRELEASE -DLOG_RELEASE
DEFINES = $(BUILD_DEFINES)

Expand Down
5 changes: 5 additions & 0 deletions sutk/source/ButtonGraphic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ namespace SUTK
m_renderRect = driver.createRenderable<RenderRectFillRound>(this);
}

DefaultButtonGraphicNoLabel::~DefaultButtonGraphicNoLabel() noexcept
{
getUIDriver().destroyRenderable<RenderRectFillRound>(m_renderRect);
}

void DefaultButtonGraphicNoLabel::onColorChange(Color4 color) noexcept
{
m_renderRect->setColor(color);
Expand Down
5 changes: 4 additions & 1 deletion sutk/source/Container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ namespace SUTK

Container::~Container()
{
com_assert(COM_DESCRIPTION(getChildCount() == 0), "You must destroy child containers first!");

if(m_parent)
m_parent->remove(this);

#ifdef CONTAINER_DEBUG
if(m_renderRectCont)
{
getUIDriver().destroyContainer<RenderableContainer>(m_renderRectCont);
// Renderable should be destroyed first.
_com_assert(m_renderRect != com::null_pointer<RenderRectOutline>());
getUIDriver().destroyRenderable<RenderRectOutline>(m_renderRect);
getUIDriver().destroyContainer<RenderableContainer>(m_renderRectCont);
}
#endif

Expand Down
17 changes: 17 additions & 0 deletions sutk/source/ContainerUtility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ namespace SUTK
ContainersVisit(child, visitor);
}

void ContainerUtility::ContainersDestroyRecursive(Container* container) noexcept
{
ContainersVisitChildEnd(container, [](Container* container) noexcept
{
container->getUIDriver().destroyContainer<Container>(container);
});
}

void ContainerUtility::ContainersVisitChildEnd(Container* container, const std::function<void(Container*)>& visitor) noexcept
{
if(!container) return;
std::vector<Container*>& childs = container->getChilds();
while(childs.size() > 0)
ContainersVisitChildEnd(childs.back(), visitor);
visitor(container);
}

GfxDriverObjectHandleType ContainerUtility::findTextGroupHandle(Container* container) noexcept
{
GfxDriverObjectHandleType handle = GFX_DRIVER_OBJECT_NULL_HANDLE;
Expand Down
20 changes: 20 additions & 0 deletions sutk/source/ImageContainer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <sutk/ImageContainer.hpp>
#include <sutk/RenderImage.hpp>

namespace SUTK
{
ImageContainer::ImageContainer(UIDriver& driver, Container* parent, com::Bool isLayoutIgnore, Layer layer) noexcept : RenderableContainer(driver, parent, isLayoutIgnore, layer)
{
m_renderImage = driver.createRenderable<RenderImage>(this);
}

ImageContainer::~ImageContainer() noexcept
{
getUIDriver().destroyRenderable<RenderImage>(m_renderImage);
}

void ImageContainer::setImage(UIDriver::ImageReference image) noexcept
{
m_renderImage->setImage(image);
}
}
5 changes: 3 additions & 2 deletions sutk/source/NotebookView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ namespace SUTK
TabView::~TabView() noexcept
{
auto& driver = getUIDriver();
driver.destroyContainer<Button>(m_closeButton);
driver.destroyContainer<ImageButtonGraphic>(m_closeButtonGraphic);
driver.destroyContainer<Button>(m_closeButton);
}

void TabView::setLabel(const std::string_view str) noexcept
Expand Down Expand Up @@ -320,7 +320,8 @@ namespace SUTK
m_head = page->getNext();
if(tabView->m_next)
tabView->m_next->m_prev = tabView->m_prev;
m_tabContainer->remove(tabView);
if(page->m_onPageRemove.has_value())
page->m_onPageRemove.value() (page);
driver.destroyContainer<Container>(page->m_container);
driver.destroyContainer<TabView>(page->m_tabView);
delete page;
Expand Down
10 changes: 10 additions & 0 deletions sutk/source/Renderable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,24 @@ namespace SUTK
Renderable::Renderable(UIDriver& driver, RenderableContainer* container) noexcept : UIDriverObject(driver), m_container(container), m_drawOrder(0), m_isDrawOrderDirty(true)
{
if(container != NULL)
{
// NOTE: We can't call RenderableContainer::getRenderable() here because it is virtual, and can be overriden
// And in that override, one may instantiate this Renderable() which will lead to non-terminating recursion!
com_assert(COM_DESCRIPTION(!container->m_renderable), "Only one Renderable is allowed per RenderableContainer");
container->setRenderable(this);
}
driver.addRenderable(this);

setDrawOrder((container == NULL) ? 0 : container->getDepth());
}

Renderable::~Renderable() noexcept
{
if(getContainer())
{
getContainer()->setRenderable(com::null_pointer<Renderable>());
m_container = com::null_pointer<RenderableContainer>();
}
getUIDriver().removeRenderable(this);
}

Expand Down
4 changes: 4 additions & 0 deletions sutk/source/RenderableContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ namespace SUTK
RenderableContainer::RenderableContainer(UIDriver& driver, Container* parent, com::Bool isLayoutIgnore, Layer layer) noexcept : Container(driver, parent, isLayoutIgnore, layer), m_renderable(NULL)
{

}
RenderableContainer::~RenderableContainer() noexcept
{
com_assert(COM_DESCRIPTION(!getRenderable()), "You must destroy Renderable before RenderableContainer!");
}
void RenderableContainer::onAdd(Container* parent)
{
Expand Down
7 changes: 3 additions & 4 deletions sutk/source/TextGroupContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

namespace SUTK
{
TextGroupContainer::TextGroupContainer(UIDriver& driver, Container* parent, com::Bool isLayoutIgnore, Layer layer) noexcept : RenderableContainer(driver, parent, isLayoutIgnore, layer)
TextGroupContainer::TextGroupContainer(UIDriver& driver, Container* parent, com::Bool isLayoutIgnore, Layer layer) noexcept : RenderableContainer(driver, parent, isLayoutIgnore, layer), m_textGroup(com::null_pointer<TextGroup>())
{
TextGroup* textGroup = driver.createRenderable<TextGroup>(this);
setRenderable(textGroup);
m_textGroup = driver.createRenderable<TextGroup>(this);
}

GfxDriverObjectHandleType TextGroupContainer::getGfxDriverObjectHandle() noexcept
Expand All @@ -16,6 +15,6 @@ namespace SUTK

TextGroup* TextGroupContainer::getTextGroup() noexcept
{
return com::iknow_down_cast<TextGroup*>(getRenderable());
return m_textGroup;
}
}

0 comments on commit c71a21c

Please sign in to comment.