-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SUTK] Added SUTK::RenderRectArray to draw an array of filled rectangles
- Loading branch information
Showing
7 changed files
with
267 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#pragma once | ||
|
||
#include <sutk/defines.hpp> // for SUTK_API | ||
#include <sutk/Renderable.hpp> // for SUTK::Renderable abstract class | ||
#include <sutk/IColorable.hpp> // for SUTK::IColorable | ||
|
||
namespace SUTK | ||
{ | ||
class SUTK_API RenderRectFillArray : public GeometryRenderable, public IColorable | ||
{ | ||
private: | ||
Color4 m_color; | ||
bool m_isColorDirty; | ||
bool m_isActiveDirty; | ||
std::vector<Rect2Df> m_rects; | ||
bool m_isRectsDirty; | ||
|
||
// overrides of Renderable::onGlobalCoordDirty(), and onContainerResize | ||
void onGlobalCoordDirty() noexcept override; | ||
void onContainerResize(Rect2Df rect, bool isPositionChanged, bool isSizeChanged) noexcept; | ||
|
||
|
||
public: | ||
// Constructors | ||
RenderRectFillArray(UIDriver& driver, RenderableContainer* container) noexcept; | ||
|
||
// Implementation of Renderable | ||
virtual bool isDirty(); | ||
virtual void update(); | ||
|
||
// Overrides of Renderable::setActive | ||
virtual void setActive(bool isActive) noexcept override; | ||
|
||
const std::vector<Rect2Df>& getRects() const noexcept { return m_rects; } | ||
std::vector<Rect2Df>& getRectsForWrite() noexcept; | ||
|
||
virtual Color4 getColor() const noexcept { return m_color; } | ||
// it must be called in the overriding method | ||
virtual void setColor(const Color4 color) noexcept; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#include <sutk/RenderRectArray.hpp> | ||
#include <sutk/RenderableContainer.hpp> | ||
#include <common/assert.h> // for _com_assert() | ||
|
||
namespace SUTK | ||
{ | ||
RenderRectFillArray::RenderRectFillArray(UIDriver& driver, RenderableContainer* container) noexcept : GeometryRenderable(driver, container), | ||
m_color(Color4::white()), | ||
m_isColorDirty(true), | ||
m_isActiveDirty(false), | ||
m_isRectsDirty(true) | ||
{ | ||
_com_assert(container != NULL); | ||
// unit rect (1 centimeter by 1 centimeter) | ||
Rect2Df rect = { 0, 0, 1.0f, 1.0f }; | ||
getGeometry() | ||
.vertexPosition(rect.getTopLeft()) | ||
.vertexPosition(rect.getBottomLeft()) | ||
.vertexPosition(rect.getBottomRight()) | ||
.vertexPosition(rect.getTopRight()) | ||
.topology(Geometry::Topology::TriangleList) | ||
.quad(0, 1, 2, 3) | ||
.fillColor(getColor()); | ||
// we need to have at least one instance to create | ||
getRectsForWrite().push_back({ 0.0f, 0.0f, 1.0f, 1.0f}); | ||
|
||
// call update for the first time as we have geometry description already and | ||
// this geometry is supposed to be displayed in the very first frame. | ||
update(); | ||
} | ||
|
||
bool RenderRectFillArray::isDirty() | ||
{ | ||
return m_isRectsDirty || m_isColorDirty || m_isActiveDirty; | ||
} | ||
|
||
void RenderRectFillArray::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(); | ||
transformArray.clear(); | ||
for(const Rect2Df& rect : m_rects) | ||
{ | ||
Rect2Df _rect = getContainer()->getLocalCoordsToScreenCoords(rect); | ||
transformArray.push_back(_rect); | ||
} | ||
} | ||
|
||
if(m_isColorDirty) | ||
{ | ||
getGeometry().fillColor(getColor()); | ||
} | ||
|
||
if(m_isRectsDirty || m_isColorDirty) | ||
{ | ||
GfxDriverObjectHandleType handle = getGfxDriverObjectHandle(); | ||
handle = getGeometry().compile(handle); | ||
setGfxDriverObjectHandle(handle); | ||
m_isRectsDirty = false; | ||
m_isColorDirty = false; | ||
} | ||
} | ||
|
||
void RenderRectFillArray::setActive(bool isActive) noexcept | ||
{ | ||
// mandatory to call | ||
GeometryRenderable::setActive(isActive); | ||
m_isActiveDirty = true; | ||
} | ||
|
||
std::vector<Rect2Df>& RenderRectFillArray::getRectsForWrite() noexcept | ||
{ | ||
m_isRectsDirty = true; | ||
return m_rects; | ||
} | ||
|
||
void RenderRectFillArray::setColor(const Color4 color) noexcept | ||
{ | ||
m_color = color; | ||
m_isColorDirty = true; | ||
} | ||
|
||
void RenderRectFillArray::onGlobalCoordDirty() noexcept | ||
{ | ||
m_isRectsDirty = true; | ||
} | ||
|
||
void RenderRectFillArray::onContainerResize(Rect2Df rect, bool isPositionChanged, bool isSizeChanged) noexcept | ||
{ | ||
m_isRectsDirty = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters