Skip to content

Commit

Permalink
[SUTK] Various fixes; Added PassiveLabel derived from Label; Increase…
Browse files Browse the repository at this point in the history
… test coverage in Label test
  • Loading branch information
ravi688 committed Dec 23, 2024
1 parent 5dd0af5 commit c900ab6
Show file tree
Hide file tree
Showing 33 changed files with 563 additions and 96 deletions.
2 changes: 1 addition & 1 deletion sutk/include/sutk/Button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace SUTK
virtual bool onMouseClick(MouseButton button, KeyEvent action) override;

public:
Button(UIDriver& driver, Container* parent, bool isCreateDefaultGraphic = true, std::optional<GfxDriverObjectHandleType> textGroup = { }) noexcept;
Button(UIDriver& driver, Container* parent, bool isCreateDefaultGraphic = true, GfxDriverObjectHandleType textGroup = GFX_DRIVER_OBJECT_NULL_HANDLE) noexcept;
virtual ~Button() noexcept;

// Must be called in the overriding method
Expand Down
2 changes: 1 addition & 1 deletion sutk/include/sutk/ButtonGraphic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ namespace SUTK
private:
Label* m_label;
public:
DefaultButtonGraphic(UIDriver& driver, Container* parent, std::optional<GfxDriverObjectHandleType> textGroup = { }) noexcept;
DefaultButtonGraphic(UIDriver& driver, Container* parent, GfxDriverObjectHandleType textGroup = GFX_DRIVER_OBJECT_NULL_HANDLE) noexcept;
virtual ~DefaultButtonGraphic() noexcept;

// Overrides
Expand Down
6 changes: 6 additions & 0 deletions sutk/include/sutk/IGfxDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ namespace SUTK
virtual void setTextPosition(GfxDriverObjectHandleType handle, Vec3Df position) = 0;
virtual void setTextDepth(GfxDriverObjectHandleType handle, f32 depth) = 0;
virtual void setTextPointSize(GfxDriverObjectHandleType handle, f32 pointSize) = 0;
// NOTE: IGfxDriver doesn't provide any getters for font of a text,
// Any IGfxDriver implementaion is free to choose any font when creating a text and won't provide any getters for it.
// Thus, the font used while creation of the text is implementation-defined and can't be queried.
// To make it defined, it is required to call IGfxDriver::setFont().
// NOTE: It is allowed to pass GFX_DRIVER_OBJECT_NULL_HANDLE in IGfxDriver::setFont(),
// In that case, the implementation would use the "implementation defined" font which would remain uniform across all functions of IGfxDriver
virtual void setTextFont(GfxDriverObjectHandleType handle, GfxDriverObjectHandleType font) = 0;
virtual f32 getTextPointSize(GfxDriverObjectHandleType handle) = 0;
virtual void setTextColor(GfxDriverObjectHandleType handle, const Color4 color) = 0;
Expand Down
20 changes: 10 additions & 10 deletions sutk/include/sutk/Label.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@
#include <sutk/TextTraits.hpp>

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

namespace SUTK
{
class SmallText;
class UIDriver;
class TextGroupContainer;

// Abstract base class for Label class and PassiveLabel class
class SUTK_API Label : public RenderableContainer
{
private:
SmallText* m_text;
HorizontalAlignment m_hAlign;
VerticalAlignment m_vAlign;
GfxDriverObjectHandleType m_textGroup;
com::Bool m_isReassociateOnParentChange;
GfxDriverObjectHandleType m_ownedTextGroup { GFX_DRIVER_OBJECT_NULL_HANDLE };
GfxDriverObjectHandleType m_textGroup { GFX_DRIVER_OBJECT_NULL_HANDLE };
void createTextFirstTime() noexcept;
void tryReassociate() noexcept;
// It is not recomendded to call this function directly in Derived Classes or anywhere else except this class itself, that's why it is in private access mode
// Use setTextGroup() instead.
// WARN: If you call this function directly then it would create an internal Text Group instance if GFX_DRIVER_OBJECT_NULL_HANDLE is passed.
void setTextGroupForce(GfxDriverObjectHandleType textGroup) noexcept;
protected:
virtual void onAnscestorChange(Container* anscestor) noexcept override;
// Allowed to be called inside the Derived Classes
void setTextGroup(GfxDriverObjectHandleType textGroup) noexcept;
public:
// If no textGroup is provided, then it searches of any textGroup (TextGroupContainer) in the Container hierarchy
Label(UIDriver& driver, Container* parent, std::optional<GfxDriverObjectHandleType> textGroup = { }) noexcept;
Label(UIDriver& driver, Container* parent = nullptr, GfxDriverObjectHandleType textGroup = GFX_DRIVER_OBJECT_NULL_HANDLE) noexcept;
~Label() noexcept;

void setReassociateOnParentChange(com::Bool isReassociate, com::Bool isApplyNow = com::False) noexcept;
void set(const std::string_view str) noexcept;
const std::string& get() noexcept;
void setAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign) noexcept;
Expand Down
1 change: 1 addition & 0 deletions sutk/include/sutk/NotebookView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ namespace SUTK
};

class Panel;
class TextGroupContainer;

class SUTK_API NotebookView : public VBoxContainer, public GlobalMouseMoveHandlerObject
{
Expand Down
29 changes: 29 additions & 0 deletions sutk/include/sutk/PassiveLabel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <sutk/Label.hpp>

namespace SUTK
{
class TextGroupContainer;

// A PassiveLabel is slightly different from the typical Label in the sense that upon instantiation it first searches up the Container Hierarhcy (recursively)
// for a Text Group object, if it finds then it uses that Text Group object to create a SmallText instance internally.
// If it doesn't find then it creates a Text Group object internally and then creates a SmallText instance from this Text Group object.
// When this PassiveLabel object is destroyed then it also destroys the (if any) created Text Group object.
class SUTK_API PassiveLabel : public Label
{
private:
com::Bool m_isReassociateOnParentChange;
void tryReassociate() noexcept;
protected:
// Override of Label::onAnscestorChange()
virtual void onAnscestorChange(Container* anscestor) noexcept override;
public:
// If no textGroup is provided, then it creates a new text group internally only for this PassiveLabel
// PERF: It is recommened to specify a valid text group if possible to avoid creating separate text group for each label which is inefficient
PassiveLabel(UIDriver& driver, Container* parent = nullptr, GfxDriverObjectHandleType textGroup = GFX_DRIVER_OBJECT_NULL_HANDLE) noexcept;
~PassiveLabel() noexcept;

void setReassociateOnParentChange(com::Bool isReassociate, com::Bool isApplyNow = com::False) noexcept;
};
}
2 changes: 1 addition & 1 deletion sutk/include/sutk/Renderable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace SUTK
// 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
// local coordinates.
// This function onGlobalCoordDirty is called when any of the Anscestor containers change their local position.
// This function onGlobalCoordDirty is called when any of the Anscestor containers change their local position or any of the anscestors themselves get changed (setParent() calls)
virtual void onGlobalCoordDirty() noexcept { }
// This function onContainerResize is called whenever the container containing this renderable changes its size or local position.
virtual void onContainerResize(Rect2Df rect, bool isPositionChanged, bool isSizeChanged) noexcept { }
Expand Down
2 changes: 1 addition & 1 deletion sutk/include/sutk/RenderableContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace SUTK

protected:

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

virtual void onAdd(Container* parent) override;
Expand Down
2 changes: 2 additions & 0 deletions sutk/include/sutk/SGEGfxDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ namespace SUTK
virtual Vec2Df getSizeInCentimeters() override { return m_sizeCentimeters; }
virtual void render(UIDriver& driver) override;

// NOTE: Creating a Text Group object just creates a new entry, but doesn't create any SGE::BitmapText objects
// until you call createText() for the first time with this Text Group object
virtual GfxDriverObjectHandleType createTextGroup(RenderMode renderMode = RenderMode::Transparent) override;
// NOTE: It force updates all bitmap text objects and text strings created from them.
// Thus, it overrides every font inside the text group.
Expand Down
7 changes: 7 additions & 0 deletions sutk/include/sutk/SmallText.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace SUTK

// implict conversion operators
operator const std::string&() const { return m_data; }
operator const std::string_view() const { return m_data; }

// equality comparison operators
bool operator ==(const SmallTextData& data) { return m_data == data.m_data; }
Expand Down Expand Up @@ -73,6 +74,8 @@ namespace SUTK
Color4 m_color;
std::vector<ColorRange> m_colorRanges;
Vec2Df m_pos;
// Automtically initialized to FontInvalid (a.k.a GFX_DRIVER_OBJECT_NULL_HANDLE)
UIDriver::FontReference m_font {};
f32 m_pointSize;
HorizontalAlignment m_horizontalAlignment;
VerticalAlignment m_verticalAlignment;
Expand Down Expand Up @@ -130,6 +133,7 @@ namespace SUTK
void setFontSize(const f32 pointSize) noexcept;
f32 getFontSize() noexcept;
void setFont(UIDriver::FontReference font) noexcept;
UIDriver::FontReference getFont() noexcept { return m_font; }
f32 getBaselineHeight() noexcept;

// NOTE: The below two set of functions should only be used when the SmallText is active
Expand All @@ -138,5 +142,8 @@ namespace SUTK
Vec2Df getAlignedPosition(Vec2Df pos) noexcept;

void setAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign) noexcept;
std::pair<HorizontalAlignment, VerticalAlignment> getAlignment() const noexcept { return { m_horizontalAlignment, m_verticalAlignment }; }
HorizontalAlignment getHorizontalAlignment() const noexcept { return m_horizontalAlignment; }
VerticalAlignment getVerticalAlignment() const noexcept { return m_verticalAlignment; }
};
}
2 changes: 1 addition & 1 deletion sutk/include/sutk/ToggleButton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace SUTK
// Instead use setGraphic() function, which is also public
void setToggleGraphic(IToggleButtonGraphic* graphic) noexcept;
public:
ToggleButton(UIDriver& driver, Container* parent, ToggleState state = ToggleState::Off, bool isCreateDefaultGraphic = true, std::optional<GfxDriverObjectHandleType> textGroup = { }) noexcept;
ToggleButton(UIDriver& driver, Container* parent, ToggleState state = ToggleState::Off, bool isCreateDefaultGraphic = true, GfxDriverObjectHandleType textGroup = GFX_DRIVER_OBJECT_NULL_HANDLE) noexcept;
~ToggleButton() noexcept;

// Override of Button::clearAllEvents()
Expand Down
2 changes: 1 addition & 1 deletion sutk/include/sutk/ToggleButtonGraphic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace SUTK
protected:
virtual void onResize(const Rect2Df& newRect, bool isPositionChanged, bool isSizeChanged) override;
public:
DefaultToggleButtonGraphic(UIDriver& driver, Container* parent, std::optional<GfxDriverObjectHandleType> textGroup) noexcept;
DefaultToggleButtonGraphic(UIDriver& driver, Container* parent, GfxDriverObjectHandleType textGroup = GFX_DRIVER_OBJECT_NULL_HANDLE) noexcept;
~DefaultToggleButtonGraphic() noexcept;

virtual void onToggle(ToggleState state) override;
Expand Down
12 changes: 6 additions & 6 deletions sutk/include/sutk/UIDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ namespace SUTK
bool m_isDummyInputDriver;
std::vector<Renderable*> m_renderables;
GfxDriverObjectHandleType m_globalTextGroup;
// Container for accomodating debug rectangles etc.
Container* m_debugRootContainer;
// Runnables
com::UnorderedEraseSafeVectorProxy<IRunnable*> m_runnables;
OrderedInputEventsDispatcher* m_orderedEventsDispatcher;
Expand All @@ -63,6 +61,9 @@ namespace SUTK
com::Bool m_isCalledFirstTime;
std::chrono::time_point<std::chrono::steady_clock> m_prevTime;
f32 m_deltaTime;
// Caches the draw order range calculated in the last draw order recalculation
// This value is used to determine if new draw order range is different from the last calculated one
u32 m_drawOrderRange { U32_MAX };

// It should be set com::True if the current frame needs to be drawn
// Generally, it should remain set to com::False unless some UI component/widget has been marked as dirty or updated its appearance
Expand Down Expand Up @@ -98,7 +99,6 @@ namespace SUTK

f32 getDeltaTime() const noexcept { return m_deltaTime; }

Container* getDebugRootContainer() noexcept { return m_debugRootContainer; }
OrderedInputEventsDispatcher& getOrderedInputEventsDispatcher() noexcept { return *m_orderedEventsDispatcher; }
AnimationEngine& getAnimationEngine() noexcept { return *m_animationEngine; }
BuiltinThemeManager& getBuiltinThemeManager() noexcept { return *m_themeManager; }
Expand All @@ -124,10 +124,10 @@ namespace SUTK
delete obj;
}

template<ContainerT ContainerType, ContainerT ParentContainerType, typename... Args>
ContainerType* createContainer(ParentContainerType* parent, Args&&... args) noexcept
template<ContainerT ContainerType, typename... Args>
ContainerType* createContainer(Args&&... args) noexcept
{
return createObject<ContainerType>(parent, std::forward<Args&&>(args)...);
return createObject<ContainerType>(std::forward<Args&&>(args)...);
}

template<ContainerT ContainerType>
Expand Down
43 changes: 43 additions & 0 deletions sutk/include/sutk/tests/LabelTest.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include <sutk/ITest.hpp>

#include <sge-cpp/sge.hpp>

#include <sutk/UIDriver.hpp>

namespace SUTK
{
class IGfxDriver;
class Label;
class TextGroupContainer;

class LabelTest : public ITest
{
private:
UIDriver* m_uiDriver;
IGfxDriver* m_gfxDriver;
IInputDriver* m_inputDriver;
FullWindowContainer* m_rootContainer { };
Container* m_auxContainer { };
TextGroupContainer* m_txtGrpContainer1 { };
TextGroupContainer* m_txtGrpContainer2 { };
Label* m_label { };
Label* m_label2 { };
Label* m_label3 { };
UIDriver::FontReference m_font { };

public:
LabelTest() : 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;
};
}
8 changes: 7 additions & 1 deletion sutk/include/sutk/tests/NotebookTest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@

#include <sutk/UIDriver.hpp>

#include <vector>

namespace SUTK
{
class IGfxDriver;
class FullWindowContainer;
class NotebookView;
class Label;

class NotebookTest : public ITest
{
private:
UIDriver* m_uiDriver;
IGfxDriver* m_gfxDriver;
IInputDriver* m_inputDriver;
NotebookView* m_notebookView;
FullWindowContainer* m_rootContainer {};
NotebookView* m_notebookView {};
std::vector<Label*> m_labels;

public:
NotebookTest() : m_uiDriver(NULL), m_gfxDriver(NULL), m_inputDriver(NULL) { }
Expand Down
43 changes: 43 additions & 0 deletions sutk/include/sutk/tests/PassiveLabelTest.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include <sutk/ITest.hpp>

#include <sge-cpp/sge.hpp>

#include <sutk/UIDriver.hpp>

namespace SUTK
{
class IGfxDriver;
class PassiveLabel;
class TextGroupContainer;

class PassiveLabelTest : public ITest
{
private:
UIDriver* m_uiDriver;
IGfxDriver* m_gfxDriver;
IInputDriver* m_inputDriver;
FullWindowContainer* m_rootContainer { };
Container* m_auxContainer { };
TextGroupContainer* m_txtGrpContainer1 { };
TextGroupContainer* m_txtGrpContainer2 { };
PassiveLabel* m_label { };
PassiveLabel* m_label2 { };
PassiveLabel* m_label3 { };
UIDriver::FontReference m_font { };

public:
PassiveLabelTest() : 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;
};
}
2 changes: 1 addition & 1 deletion sutk/source/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace SUTK
{
Button::Button(UIDriver& driver, Container* parent, bool isCreateDefaultGraphic, std::optional<GfxDriverObjectHandleType> textGroup) noexcept : Container(driver, parent),
Button::Button(UIDriver& driver, Container* parent, bool isCreateDefaultGraphic, GfxDriverObjectHandleType textGroup) noexcept : Container(driver, parent),
MouseMoveHandlerObject(driver, this),
MouseClickHandlerObject(driver, this),
m_graphic(NULL),
Expand Down
2 changes: 1 addition & 1 deletion sutk/source/ButtonGraphic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ namespace SUTK
m_renderRect->setColor(color);
}

DefaultButtonGraphic::DefaultButtonGraphic(UIDriver& driver, Container* parent, std::optional<GfxDriverObjectHandleType> textGroup) noexcept : DefaultButtonGraphicNoLabel(driver, parent)
DefaultButtonGraphic::DefaultButtonGraphic(UIDriver& driver, Container* parent, GfxDriverObjectHandleType textGroup) noexcept : DefaultButtonGraphicNoLabel(driver, parent)
{
m_label = driver.createContainer<Label>(this, textGroup);
// size of the label's rect should be as that of ButtonGraphic's rect
Expand Down
2 changes: 1 addition & 1 deletion sutk/source/Container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ namespace SUTK
{
// create SUTK::RenderableContainer and setup its rect
// NOTE: a Debug rect must have ignore layout flag set to 'true' to avoid its participation in layouting with other non-debug elements.
m_renderRectCont = getUIDriver().createContainer<RenderableContainer>(getUIDriver().getDebugRootContainer(), com::Bool::True());
m_renderRectCont = getUIDriver().createContainer<RenderableContainer>(com::null_pointer<Container>(), com::Bool::True());
Vec2Df pos = getLocalCoordsToScreenCoords({ 0, 0 });
m_renderRectCont->setRect({ pos.x, pos.y, getRect().width, getRect().height });

Expand Down
6 changes: 5 additions & 1 deletion sutk/source/ITest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <sutk/tests/BaseThemeTest.hpp>
#include <sutk/tests/DarkLightThemeTest.hpp>
#include <sutk/tests/PanelTest.hpp>
#include <sutk/tests/LabelTest.hpp>
#include <sutk/tests/PassiveLabelTest.hpp>

namespace SUTK
{
Expand Down Expand Up @@ -61,7 +63,9 @@ namespace SUTK
{ "MULTIPLE_THEME_MODEL", [] () { return std::unique_ptr<ITest>(new MultipleThemeModelTest()); }},
{ "BASE_THEME", [] () { return std::unique_ptr<ITest>(new BaseThemeTest()); }},
{ "DARK_LIGHT_THEME", [] () { return std::unique_ptr<ITest>(new DarkLightThemeTest()); }},
{ "PANEL", [] () { return std::unique_ptr<ITest>(new PanelTest()); }}
{ "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()); }}
};

std::unique_ptr<ITest> ITest::Create(const std::string& testName)
Expand Down
Loading

0 comments on commit c900ab6

Please sign in to comment.