stuff-from-scratch/src/rendering/visual_elements/nodes/GridNode.cpp
2023-01-17 10:13:25 +00:00

140 lines
2.9 KiB
C++

#include "GridNode.h"
#include "MeshPrimitives.h"
GridNode::GridNode(const Point& location)
: MaterialNode(location)
{
}
void GridNode::setNumX(std::size_t numX)
{
if (mNumberX != numX)
{
mNumberX = numX;
mDataDirty = true;
}
}
void GridNode::setNumY(std::size_t numY)
{
if (mNumberY != numY)
{
mNumberY = numY;
mDataDirty = true;
}
}
void GridNode::setWidth(double width)
{
if (mWidth != width)
{
mTransformIsDirty = true;
mWidth = width;
}
}
void GridNode::setHeight(double height)
{
if (mHeight != height)
{
mTransformIsDirty = true;
mHeight = height;
}
}
void GridNode::setData(const std::vector<Color>& colors)
{
if (mData != colors)
{
mData = colors;
mDataDirty = true;
}
}
SceneItem* GridNode::getSceneItem(std::size_t idx) const
{
if (idx == 0)
{
return mBackgroundModel.get();
}
else
{
return mOutlineModel.get();
}
}
std::size_t GridNode::getNumSceneItems() const
{
return 2;
}
void GridNode::update(SceneInfo* sceneInfo)
{
if (!mBackgroundModel || mDataDirty)
{
auto mesh = MeshPrimitives::buildExplodedGridAsTriMesh(mNumberX, mNumberY);
if (!mBackgroundModel)
{
mBackgroundModel = std::make_unique<SceneModel>(std::move(mesh));
mBackgroundModel->setName(mName + "_BackgroundModel");
}
else
{
mBackgroundModel.get()->updateMesh(std::move(mesh));
}
}
if (mOutlineModel || mDataDirty)
{
auto mesh = MeshPrimitives::buildExplodedGridAsLineMesh(mNumberX, mNumberY);
if (!mOutlineModel)
{
mOutlineModel = std::make_unique<SceneModel>(std::move(mesh));
mOutlineModel->setName(mName + "_OulineModel");
}
else
{
mOutlineModel.get()->updateMesh(std::move(mesh));
}
}
if (mDataDirty)
{
auto difference = mNumberX*mNumberY - mData.size();
mData.resize(mNumberX*mNumberY, {});
auto node_data = std::vector<std::vector<double> >(4*mNumberX*mNumberY);
for (unsigned idx=0; idx< node_data.size(); idx++)
{
if (idx < mData.size())
{
node_data[idx] = mData[idx].getAsVectorDouble();
}
}
//mBackgroundModel->getMesh()->addVectorAttribute("Color", mData);
}
if (mTransformIsDirty)
{
auto size = mHeight > mWidth ? mWidth : mHeight;
mBackgroundModel->updateTransform({mLocation, static_cast<double>(size), static_cast<double>(size)});
mOutlineModel->updateTransform({mLocation, static_cast<double>(size), static_cast<double>(size)});
mTransformIsDirty = false;
}
if (mMaterialIsDirty)
{
mOutlineModel->setFillColor(mStrokeColor);
mMaterialIsDirty = false;
}
mDataDirty = false;
}