From f04d86e0add59fb1659dd095b4cda8978bbdb8e1 Mon Sep 17 00:00:00 2001 From: James Grogan Date: Thu, 17 Nov 2022 17:33:48 +0000 Subject: [PATCH] Start adding grid --- apps/sample-gui/CMakeLists.txt | 2 + .../audio_editor/AudioEditorView.cpp | 4 +- .../image_editor/ImageEditorView.cpp | 15 ++- .../image_editor/ImageViewWidget.cpp | 32 +++++ .../sample-gui/image_editor/ImageViewWidget.h | 18 +++ .../sample-gui/text_editor/TextEditorView.cpp | 14 +- apps/sample-gui/web_client/WebClientView.cpp | 6 +- src/geometry/Point.h | 6 + src/graphics/opengl/OpenGlMeshPainter.cpp | 3 + src/mesh/AbstractFace.cpp | 22 ++++ src/mesh/AbstractFace.h | 24 ++++ src/mesh/AbstractMesh.cpp | 40 ++++++ src/mesh/AbstractMesh.h | 40 +++++- src/mesh/CMakeLists.txt | 3 +- src/mesh/MeshPrimitives.cpp | 90 +++++++++++++ src/mesh/MeshPrimitives.h | 30 +---- src/mesh/Node.cpp | 15 +++ src/mesh/Node.h | 15 +++ src/mesh/{Face.cpp => QuadFace.cpp} | 0 src/mesh/{Face.h => QuadFace.h} | 2 +- src/mesh/TriFace.cpp | 23 +--- src/mesh/TriFace.h | 19 +-- src/mesh/TriMesh.cpp | 31 ----- src/mesh/TriMesh.h | 32 ----- src/ui_elements/CMakeLists.txt | 2 + src/ui_elements/style/Theme.cpp | 26 ++++ src/ui_elements/style/Theme.h | 14 ++ src/visual_elements/AbstractVisualNode.cpp | 78 +++++++++++ src/visual_elements/AbstractVisualNode.h | 74 +++-------- src/visual_elements/CMakeLists.txt | 3 + src/visual_elements/GridNode.cpp | 123 ++++++++++++++++++ src/visual_elements/GridNode.h | 36 +++++ src/visual_elements/RectangleNode.cpp | 31 ++++- src/visual_elements/RectangleNode.h | 6 + src/visual_elements/Scene.cpp | 7 +- src/visual_elements/TextNode.cpp | 29 ++++- src/visual_elements/TextNode.h | 5 + 37 files changed, 709 insertions(+), 211 deletions(-) create mode 100644 apps/sample-gui/image_editor/ImageViewWidget.cpp create mode 100644 apps/sample-gui/image_editor/ImageViewWidget.h create mode 100644 src/mesh/AbstractFace.cpp create mode 100644 src/mesh/AbstractFace.h rename src/mesh/{Face.cpp => QuadFace.cpp} (100%) rename src/mesh/{Face.h => QuadFace.h} (68%) create mode 100644 src/visual_elements/AbstractVisualNode.cpp create mode 100644 src/visual_elements/GridNode.cpp create mode 100644 src/visual_elements/GridNode.h diff --git a/apps/sample-gui/CMakeLists.txt b/apps/sample-gui/CMakeLists.txt index f148e10..7190117 100644 --- a/apps/sample-gui/CMakeLists.txt +++ b/apps/sample-gui/CMakeLists.txt @@ -6,6 +6,7 @@ list(APPEND client_HEADERS text_editor/PlainTextDocument.h audio_editor/AudioEditorView.h image_editor/ImageEditorView.h + image_editor/ImageViewWidget.h web_client/WebClientView.h) @@ -16,6 +17,7 @@ list(APPEND client_LIB_INCLUDES text_editor/PlainTextDocument.cpp audio_editor/AudioEditorView.cpp image_editor/ImageEditorView.cpp + image_editor/ImageViewWidget.cpp web_client/WebClientView.cpp MediaTool.cpp) diff --git a/apps/sample-gui/audio_editor/AudioEditorView.cpp b/apps/sample-gui/audio_editor/AudioEditorView.cpp index f7d6442..ca3a964 100644 --- a/apps/sample-gui/audio_editor/AudioEditorView.cpp +++ b/apps/sample-gui/audio_editor/AudioEditorView.cpp @@ -1,14 +1,14 @@ #include "AudioEditorView.h" #include "Label.h" -#include "Color.h" #include "TextNode.h" +#include "Theme.h" AudioEditorView::AudioEditorView() { auto label = Label::Create(); label->setLabel("audio Editor"); - label->setBackgroundColor(Color(200, 189, 160)); + label->setBackgroundColor(Theme::getBackgroundPrimary()); label->setMargin(1); addWidget(std::move(label)); } diff --git a/apps/sample-gui/image_editor/ImageEditorView.cpp b/apps/sample-gui/image_editor/ImageEditorView.cpp index 70bc5e0..821c3f9 100644 --- a/apps/sample-gui/image_editor/ImageEditorView.cpp +++ b/apps/sample-gui/image_editor/ImageEditorView.cpp @@ -3,14 +3,25 @@ #include "Label.h" #include "Color.h" #include "TextNode.h" +#include "Theme.h" +#include "ImageViewWidget.h" + +#include "HorizontalSpacer.h" ImageEditorView::ImageEditorView() { auto label = Label::Create(); label->setLabel("Image Editor"); - label->setBackgroundColor(Color(200, 189, 160)); + label->setBackgroundColor(Theme::getBackgroundPrimary()); label->setMargin(1); - addWidget(std::move(label)); + + auto image_widget = std::make_unique(); + + auto hSpacer = HorizontalSpacer::Create(); + hSpacer->addWidgetWithScale(std::move(label), 1); + hSpacer->addWidgetWithScale(std::move(image_widget), 14); + + addWidget(std::move(hSpacer)); } std::unique_ptr ImageEditorView::Create() diff --git a/apps/sample-gui/image_editor/ImageViewWidget.cpp b/apps/sample-gui/image_editor/ImageViewWidget.cpp new file mode 100644 index 0000000..87d081e --- /dev/null +++ b/apps/sample-gui/image_editor/ImageViewWidget.cpp @@ -0,0 +1,32 @@ +#include "ImageViewWidget.h" + +#include "GridNode.h" +#include "TransformNode.h" + +#include + +ImageViewWidget::ImageViewWidget() +{ + mName = "ImageViewWidget"; +} + +void ImageViewWidget::doPaint(const PaintEvent* event) +{ + if (!mVisible) + { + return; + } + + if (!mGridNode) + { + std::cout << "Setting up grid node" << std::endl; + mGridNode = std::make_unique(mLocation); + mGridNode->setName(mName + "_GridNode"); + mGridNode->setNumX(mNumX); + mGridNode->setNumY(mNumY); + + mGridNode->setWidth(mSize.mWidth); + mGridNode->setHeight(mSize.mHeight); + mRootNode->addChild(mGridNode.get()); + } +} diff --git a/apps/sample-gui/image_editor/ImageViewWidget.h b/apps/sample-gui/image_editor/ImageViewWidget.h new file mode 100644 index 0000000..e362071 --- /dev/null +++ b/apps/sample-gui/image_editor/ImageViewWidget.h @@ -0,0 +1,18 @@ +#pragma once + +#include "Widget.h" + +#include "GridNode.h" + +class ImageViewWidget : public Widget +{ +public: + ImageViewWidget(); + +private: + void doPaint(const PaintEvent* event) override; + unsigned mNumX{5}; + unsigned mNumY{5}; + + std::unique_ptr mGridNode; +}; diff --git a/apps/sample-gui/text_editor/TextEditorView.cpp b/apps/sample-gui/text_editor/TextEditorView.cpp index d376999..b557819 100644 --- a/apps/sample-gui/text_editor/TextEditorView.cpp +++ b/apps/sample-gui/text_editor/TextEditorView.cpp @@ -1,6 +1,10 @@ -#include "HorizontalSpacer.h" #include "TextEditorView.h" + +#include "HorizontalSpacer.h" #include "VerticalSpacer.h" + +#include "Theme.h" + #include "TextNode.h" #include @@ -21,7 +25,7 @@ void TextEditorView::Initialize() { auto label = Label::Create(); label->setLabel("Text Editor"); - label->setBackgroundColor(Color(181, 189, 200)); + label->setBackgroundColor(Theme::getBannerBackground()); label->setMargin(1); auto textBox = TextBox::Create(); @@ -30,7 +34,7 @@ void TextEditorView::Initialize() auto saveButton = Button::Create(); saveButton->setLabel("Save"); - saveButton->setBackgroundColor(Color(200, 200, 200)); + saveButton->setBackgroundColor(Theme::getButtonPrimaryBackground()); saveButton->setMargin(2); auto onSave = [this](Widget* self){ if(this && mController && mTextBox) @@ -43,7 +47,7 @@ void TextEditorView::Initialize() auto clearButton = Button::Create(); clearButton->setLabel("Clear"); - clearButton->setBackgroundColor(Color(200, 200, 200)); + clearButton->setBackgroundColor(Theme::getButtonPrimaryBackground()); clearButton->setMargin(2); auto onClear = [this](Widget* self){ if(this && mController && mTextBox) @@ -56,7 +60,7 @@ void TextEditorView::Initialize() auto loadButton = Button::Create(); loadButton->setLabel("Load"); - loadButton->setBackgroundColor(Color(200, 200, 200)); + loadButton->setBackgroundColor(Theme::getButtonPrimaryBackground()); loadButton->setMargin(2); auto onLoad = [this](Widget* self){ if(this && mController && mTextBox) diff --git a/apps/sample-gui/web_client/WebClientView.cpp b/apps/sample-gui/web_client/WebClientView.cpp index 8ee2012..9dd6fea 100644 --- a/apps/sample-gui/web_client/WebClientView.cpp +++ b/apps/sample-gui/web_client/WebClientView.cpp @@ -1,14 +1,16 @@ #include "WebClientView.h" #include "Label.h" -#include "Color.h" + #include "TextNode.h" +#include "Theme.h" + WebClientView::WebClientView() { auto label = Label::Create(); label->setLabel("Web Client"); - label->setBackgroundColor(Color(200, 189, 160)); + label->setBackgroundColor(Theme::getBackgroundPrimary()); label->setMargin(1); addWidget(std::move(label)); } diff --git a/src/geometry/Point.h b/src/geometry/Point.h index 1623f9d..ad4af1f 100644 --- a/src/geometry/Point.h +++ b/src/geometry/Point.h @@ -33,6 +33,12 @@ public: double getDeltaY(const Point& point) const; + void scale(double x, double y) + { + mX = x*mX; + mY = y*mY; + } + bool operator==(const Point& rhs) const { return (mX == rhs.mX) diff --git a/src/graphics/opengl/OpenGlMeshPainter.cpp b/src/graphics/opengl/OpenGlMeshPainter.cpp index f6961bd..9fde8a3 100644 --- a/src/graphics/opengl/OpenGlMeshPainter.cpp +++ b/src/graphics/opengl/OpenGlMeshPainter.cpp @@ -87,6 +87,7 @@ void OpenGlMeshPainter::paint(SceneModel* model, DrawingContext* context) const auto height = float(surface->getHeight()); auto mesh = dynamic_cast(model->getMesh()); + std::cout << "Paint mesh for model " << model->getName() << std::endl; auto transform = model->getTransform(); @@ -97,6 +98,7 @@ void OpenGlMeshPainter::paint(SceneModel* model, DrawingContext* context) { auto x = vertices[idx]*transform.getScaleX() + transform.getLocation().getX(); vertices[idx] = 2*x/width - 1.0; + std::cout << "Vert x is " << vertices[idx] << std::endl; } else if(idx%3 == 1) { @@ -108,6 +110,7 @@ void OpenGlMeshPainter::paint(SceneModel* model, DrawingContext* context) const auto indices = mesh->getFaceNodeIds(); auto model_color = model->getColor().getAsVectorDouble(); + std::cout << "Color is " << model_color[0] << "|" << model_color[0] << std::endl; std::vector color = {float(model_color[0]), float(model_color[1]), float(model_color[2]), float(model_color[3])}; paint(vertices, indices, color); diff --git a/src/mesh/AbstractFace.cpp b/src/mesh/AbstractFace.cpp new file mode 100644 index 0000000..c41a394 --- /dev/null +++ b/src/mesh/AbstractFace.cpp @@ -0,0 +1,22 @@ +#include "AbstractFace.h" + +AbstractFace::AbstractFace(unsigned id) + : mId(id) +{ + +} + +void AbstractFace::addVectorAttribute(const std::string& tag, const std::vector& values) +{ + mVectorAttributes[tag] = values; +} + +std::vector AbstractFace::getVectorAttribute(const std::string& tag) const +{ + auto iter = mVectorAttributes.find(tag); + if (iter != mVectorAttributes.end()) + { + return iter->second; + } + return {}; +} diff --git a/src/mesh/AbstractFace.h b/src/mesh/AbstractFace.h new file mode 100644 index 0000000..5c4649d --- /dev/null +++ b/src/mesh/AbstractFace.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include +#include +#include + +class AbstractFace +{ +public: + AbstractFace(unsigned id=0); + + virtual ~AbstractFace() = default; + + virtual std::vector getNodeIds() const = 0; + + void addVectorAttribute(const std::string& tag, const std::vector& values); + + std::vector getVectorAttribute(const std::string& tag) const; + +protected: + unsigned mId{0}; + std::unordered_map > mVectorAttributes; +}; diff --git a/src/mesh/AbstractMesh.cpp b/src/mesh/AbstractMesh.cpp index e69de29..30d2676 100644 --- a/src/mesh/AbstractMesh.cpp +++ b/src/mesh/AbstractMesh.cpp @@ -0,0 +1,40 @@ +#include "AbstractMesh.h" + +void AbstractMesh::addConstantNodeVectorAttribute(const std::string& tag, const std::vector& values) +{ + +} + +std::vector > AbstractMesh::getNodeVectorAttributes(const std::string& tag) +{ + std::vector > attribs(mNodes.size()); + return attribs; +} + +void AbstractMesh::addVectorAttribute(const std::string& tag, const std::vector& values) +{ + mVectorAttributes[tag] = values; +} + +bool AbstractMesh::hasVectorAttribute(const std::string& tag) const +{ + return mVectorAttributes.find(tag) != mVectorAttributes.end(); +} + +std::vector AbstractMesh::getVectorAttribute(const std::string& tag) const +{ + auto iter = mVectorAttributes.find(tag); + if (iter != mVectorAttributes.end()) + { + return iter->second; + } + return {}; +} + +void AbstractMesh::scale(double scaleX, double scaleY) +{ + for (auto& node : mNodes) + { + node->scale(scaleX, scaleY); + } +} diff --git a/src/mesh/AbstractMesh.h b/src/mesh/AbstractMesh.h index 86183ed..68f9d31 100644 --- a/src/mesh/AbstractMesh.h +++ b/src/mesh/AbstractMesh.h @@ -1,9 +1,17 @@ #pragma once +#include "Point.h" +#include "Node.h" + +#include +#include + +using NodePtr = std::unique_ptr; +using VecNodes = std::vector; + class AbstractMesh { public: - enum class MeshType { LINE, @@ -13,5 +21,35 @@ public: virtual ~AbstractMesh() = default; + void addVectorAttribute(const std::string& tag, const std::vector& values); + + void addConstantNodeVectorAttribute(const std::string& tag, const std::vector& values); + + template + std::vector getVerticesFlat(T scaleX = 1.0, T scaleY = 1.0, T scaleZ = 1.0) const + { + std::vector ret(3*mNodes.size()); + for(std::size_t idx = 0; idxgetPoint().getX()/scaleX; + ret[3*idx + 1] = node->getPoint().getY()/scaleY; + ret[3*idx + 2] = node->getPoint().getZ()/scaleZ; + } + return ret; + } + + std::vector > getNodeVectorAttributes(const std::string& tag); + + std::vector getVectorAttribute(const std::string& tag) const; + virtual MeshType getType() const = 0; + + bool hasVectorAttribute(const std::string& tag) const; + + void scale(double scaleX, double scaleY); + +protected: + std::unordered_map > mVectorAttributes; + VecNodes mNodes; }; diff --git a/src/mesh/CMakeLists.txt b/src/mesh/CMakeLists.txt index e29d1d2..0065cc9 100644 --- a/src/mesh/CMakeLists.txt +++ b/src/mesh/CMakeLists.txt @@ -1,7 +1,8 @@ list(APPEND mesh_LIB_INCLUDES AbstractMesh.cpp Edge.cpp - Face.cpp + AbstractFace.cpp + QuadFace.cpp TriFace.cpp Node.cpp QuadMesh.cpp diff --git a/src/mesh/MeshPrimitives.cpp b/src/mesh/MeshPrimitives.cpp index e69de29..13a65f6 100644 --- a/src/mesh/MeshPrimitives.cpp +++ b/src/mesh/MeshPrimitives.cpp @@ -0,0 +1,90 @@ +#include "MeshPrimitives.h" + +#include + +std::unique_ptr MeshPrimitives::build(const Rectangle& rectangle) +{ + const auto bottom_left = rectangle.getLocation(); + const auto width = rectangle.getWidth(); + const auto height = rectangle.getHeight(); + + VecPoints locations = { + bottom_left, + Point(bottom_left, width, 0), + Point(bottom_left, width, height), + Point(bottom_left, 0, height) + }; + + EdgeIds edge_ids = { + {0, 1}, + {1, 2}, + {2, 0}, + {2, 3}, + {3, 0} + }; + + FaceIds face_ids = { + {0, 1, 2}, + {2, 3, 4} + }; + + return MeshBuilder::buildTriMesh(locations, edge_ids, face_ids); +} + +std::unique_ptr MeshPrimitives::buildExplodedGrid(unsigned numX, unsigned numY) +{ + double delta_x = 1.0/double(numX); + double delta_y = 1.0/double(numY); + + VecPoints locations (4 * numX * numY); + double offset_x = delta_x; + double offset_y = delta_y; + for (unsigned idx=0; idx build(const Rectangle& rectangle) - { - const auto bottom_left = rectangle.getLocation(); - const auto width = rectangle.getWidth(); - const auto height = rectangle.getHeight(); + static std::unique_ptr build(const Rectangle& rectangle); - VecPoints locations = { - bottom_left, - Point(bottom_left, width, 0), - Point(bottom_left, width, height), - Point(bottom_left, 0, height) - }; - - EdgeIds edge_ids = { - {0, 1}, - {1, 2}, - {2, 0}, - {2, 3}, - {3, 0} - }; - - FaceIds face_ids = { - {0, 1, 2}, - {2, 3, 4} - }; - - return MeshBuilder::buildTriMesh(locations, edge_ids, face_ids); - } + static std::unique_ptr buildExplodedGrid(unsigned numX, unsigned numY); }; diff --git a/src/mesh/Node.cpp b/src/mesh/Node.cpp index 0526f58..f35ba6f 100644 --- a/src/mesh/Node.cpp +++ b/src/mesh/Node.cpp @@ -27,4 +27,19 @@ void Node::updateIndex(unsigned index) mIndex = index; } +void Node::addVectorAttribute(const std::string& tag, const std::vector& values) +{ + mVectorAttributes[tag] = values; +} + +std::vector Node::getVectorAttribute(const std::string& tag) const +{ + auto iter = mVectorAttributes.find(tag); + if (iter != mVectorAttributes.end()) + { + return iter->second; + } + return {}; +} + diff --git a/src/mesh/Node.h b/src/mesh/Node.h index 92abdba..1536f97 100644 --- a/src/mesh/Node.h +++ b/src/mesh/Node.h @@ -2,6 +2,11 @@ #include "Point.h" +#include +#include +#include + + class Node { public: @@ -20,7 +25,17 @@ public: return mPoint; } + void addVectorAttribute(const std::string& tag, const std::vector& values); + + std::vector getVectorAttribute(const std::string& tag) const; + + void scale(double x, double y) + { + mPoint.scale(x, y); + } + private: + std::unordered_map > mVectorAttributes; unsigned mIndex{0}; Point mPoint; }; diff --git a/src/mesh/Face.cpp b/src/mesh/QuadFace.cpp similarity index 100% rename from src/mesh/Face.cpp rename to src/mesh/QuadFace.cpp diff --git a/src/mesh/Face.h b/src/mesh/QuadFace.h similarity index 68% rename from src/mesh/Face.h rename to src/mesh/QuadFace.h index a4081d4..2350e39 100644 --- a/src/mesh/Face.h +++ b/src/mesh/QuadFace.h @@ -2,7 +2,7 @@ class Edge; -class Face +class QuadFace { }; diff --git a/src/mesh/TriFace.cpp b/src/mesh/TriFace.cpp index fd8d4cb..461a0fb 100644 --- a/src/mesh/TriFace.cpp +++ b/src/mesh/TriFace.cpp @@ -3,10 +3,10 @@ #include "Edge.h" TriFace::TriFace(Edge* edge0, Edge* edge1, Edge* edge2, unsigned id) - : mEdge0(edge0), - mEdge1(edge1), - mEdge2(edge2), - mId(id) + : AbstractFace(id), + mEdge0(edge0), + mEdge1(edge1), + mEdge2(edge2) { } @@ -25,18 +25,3 @@ std::vector TriFace::getNodeIds() const { return {mEdge0->getNode0Id(), mEdge0->getNode1Id(), mEdge1->getNode1Id()}; } - -void TriFace::addVectorAttribute(const std::string& tag, const std::vector& values) -{ - mVectorAttributes[tag] = values; -} - -std::vector TriFace::getVectorAttribute(const std::string& tag) const -{ - auto iter = mVectorAttributes.find(tag); - if (iter != mVectorAttributes.end()) - { - return iter->second; - } - return {}; -} diff --git a/src/mesh/TriFace.h b/src/mesh/TriFace.h index 12412a2..32d70e9 100644 --- a/src/mesh/TriFace.h +++ b/src/mesh/TriFace.h @@ -1,29 +1,22 @@ #pragma once -#include -#include -#include -#include +#include "AbstractFace.h" class Edge; -class TriFace +class TriFace : public AbstractFace { public: TriFace(Edge* edge0, Edge* edge1, Edge* edge2, unsigned id=0); + ~TriFace(); + std::vector getNodeIds() const override; + static std::unique_ptr Create(Edge* edge0, Edge* edge1, Edge* edge2, unsigned id=0); - std::vector getNodeIds() const; - - void addVectorAttribute(const std::string& tag, const std::vector& values); - - std::vector getVectorAttribute(const std::string& tag) const; - private: - unsigned mId{0}; - std::unordered_map > mVectorAttributes; + Edge* mEdge0{nullptr}; Edge* mEdge1{nullptr}; Edge* mEdge2{nullptr}; diff --git a/src/mesh/TriMesh.cpp b/src/mesh/TriMesh.cpp index c5ce6f0..5251982 100644 --- a/src/mesh/TriMesh.cpp +++ b/src/mesh/TriMesh.cpp @@ -49,34 +49,3 @@ void TriMesh::addConstantFaceVectorAttribute(const std::string& tag, const std:: face->addVectorAttribute(tag, values); } } - -void TriMesh::addConstantNodeVectorAttribute(const std::string& tag, const std::vector& values) -{ - -} - -std::vector > TriMesh::getNodeVectorAttributes(const std::string& tag) -{ - std::vector > attribs(mNodes.size()); - return attribs; -} - -void TriMesh::addVectorAttribute(const std::string& tag, const std::vector& values) -{ - mVectorAttributes[tag] = values; -} - -bool TriMesh::hasVectorAttribute(const std::string& tag) const -{ - return mVectorAttributes.find(tag) != mVectorAttributes.end(); -} - -std::vector TriMesh::getVectorAttribute(const std::string& tag) const -{ - auto iter = mVectorAttributes.find(tag); - if (iter != mVectorAttributes.end()) - { - return iter->second; - } - return {}; -} diff --git a/src/mesh/TriMesh.h b/src/mesh/TriMesh.h index f58ed0f..79ce181 100644 --- a/src/mesh/TriMesh.h +++ b/src/mesh/TriMesh.h @@ -1,22 +1,16 @@ #pragma once #include "AbstractMesh.h" -#include "Point.h" -#include "Node.h" -#include -#include #include #include class Edge; class TriFace; -using NodePtr = std::unique_ptr; using EdgePtr = std::unique_ptr; using TriFacePtr = std::unique_ptr; -using VecNodes = std::vector; using VecEdges = std::vector; using VecFaces = std::vector; @@ -29,44 +23,18 @@ public: void populate(VecNodes& nodes, VecEdges& edges, VecFaces& faces); - template - std::vector getVerticesFlat(T scaleX = 1.0, T scaleY = 1.0, T scaleZ = 1.0) const - { - std::vector ret(3*mNodes.size()); - for(std::size_t idx = 0; idxgetPoint().getX()/scaleX; - ret[3*idx + 1] = node->getPoint().getY()/scaleY; - ret[3*idx + 2] = node->getPoint().getZ()/scaleZ; - } - return ret; - } - std::vector getFaceNodeIds() const; void addConstantFaceVectorAttribute(const std::string& tag, const std::vector& values); - void addConstantNodeVectorAttribute(const std::string& tag, const std::vector& values); - std::vector > getFaceVectorAttributes(const std::string& tag); - std::vector > getNodeVectorAttributes(const std::string& tag); - - void addVectorAttribute(const std::string& tag, const std::vector& values); - - bool hasVectorAttribute(const std::string& tag) const; - - std::vector getVectorAttribute(const std::string& tag) const; - MeshType getType() const { return MeshType::TRI; } private: - std::unordered_map > mVectorAttributes; - VecNodes mNodes; VecEdges mEdges; VecFaces mFaces; }; diff --git a/src/ui_elements/CMakeLists.txt b/src/ui_elements/CMakeLists.txt index 61ced1e..81e0308 100644 --- a/src/ui_elements/CMakeLists.txt +++ b/src/ui_elements/CMakeLists.txt @@ -21,6 +21,7 @@ list(APPEND ui_elements_LIB_INCLUDES widgets/VerticalSpacer.cpp widgets/StackWidget.cpp widgets/TextBox.cpp + style/Theme.cpp ) add_library(ui_elements SHARED ${ui_elements_LIB_INCLUDES}) @@ -28,6 +29,7 @@ add_library(ui_elements SHARED ${ui_elements_LIB_INCLUDES}) target_include_directories(ui_elements PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/" "${CMAKE_CURRENT_SOURCE_DIR}/widgets" + "${CMAKE_CURRENT_SOURCE_DIR}/style" "${CMAKE_CURRENT_SOURCE_DIR}/widgets/elements" "${CMAKE_CURRENT_SOURCE_DIR}/ui_events" "${CMAKE_CURRENT_SOURCE_DIR}/desktop_elements" diff --git a/src/ui_elements/style/Theme.cpp b/src/ui_elements/style/Theme.cpp index e69de29..2b579f2 100644 --- a/src/ui_elements/style/Theme.cpp +++ b/src/ui_elements/style/Theme.cpp @@ -0,0 +1,26 @@ +#include "Theme.h" + +Color Theme::getBackgroundPrimary() +{ + return {217, 217, 217}; +} + +Color Theme::getBannerBackground() +{ + return {181, 189, 200}; +} + +Color Theme::getButtonPrimaryBackground() +{ + return {200, 200, 200}; +} + +Color Theme::getButtonPrimaryPressed() +{ + return {}; +} + +Color Theme::getTextPrimary() +{ + return {}; +} diff --git a/src/ui_elements/style/Theme.h b/src/ui_elements/style/Theme.h index 8b13789..e4f3547 100644 --- a/src/ui_elements/style/Theme.h +++ b/src/ui_elements/style/Theme.h @@ -1 +1,15 @@ +#include "Color.h" +class Theme +{ +public: + static Color getBackgroundPrimary(); + + static Color getBannerBackground(); + + static Color getButtonPrimaryBackground(); + + static Color getButtonPrimaryPressed(); + + static Color getTextPrimary(); +}; diff --git a/src/visual_elements/AbstractVisualNode.cpp b/src/visual_elements/AbstractVisualNode.cpp new file mode 100644 index 0000000..5b1bda9 --- /dev/null +++ b/src/visual_elements/AbstractVisualNode.cpp @@ -0,0 +1,78 @@ +#include "AbstractVisualNode.h" + +AbstractVisualNode::AbstractVisualNode(const DiscretePoint& location, const std::string& name) + : mLocation(location), + mName(name) +{ + +} + +SceneItem* AbstractVisualNode::getSceneItem(std::size_t idx) const +{ + return mSceneItems[idx].get(); +} + +unsigned AbstractVisualNode::getNumSceneItems() const +{ + return mSceneItems.size(); +} + +void AbstractVisualNode::update(FontsManager* fontsManager) +{ + +} + +Image* AbstractVisualNode::getImage() const +{ + return mImage.get(); +} + +void AbstractVisualNode::syncChildren(const std::vector& children) +{ + mChildren = children; +} + +void AbstractVisualNode::addChild(AbstractVisualNode* child) +{ + mChildren.push_back(child); +} + +const std::vector& AbstractVisualNode::getChildren() const +{ + return mChildren; +} + +void AbstractVisualNode::setIsVisible(bool isVisible) +{ + //std::cout << "Setting " << mName << " visibility to " << isVisible << std::endl; + mIsVisible = isVisible; +} + +unsigned AbstractVisualNode::getNumChildren() const +{ + return mChildren.size(); +} + +void AbstractVisualNode::setName(const std::string& name) +{ + mName = name; +} + +const std::string& AbstractVisualNode::getName() const +{ + return mName; +} + +bool AbstractVisualNode::getIsVisible() const +{ + return mIsVisible; +} + +void AbstractVisualNode::setLocation(const DiscretePoint& loc) +{ + if (mLocation != loc) + { + mTransformIsDirty = true; + mLocation = loc; + } +} diff --git a/src/visual_elements/AbstractVisualNode.h b/src/visual_elements/AbstractVisualNode.h index 2ebdd1f..d7e15c5 100644 --- a/src/visual_elements/AbstractVisualNode.h +++ b/src/visual_elements/AbstractVisualNode.h @@ -15,83 +15,39 @@ class FontsManager; class AbstractVisualNode { public: - AbstractVisualNode(const DiscretePoint& location, const std::string& name = {}) - : mLocation(location), - mName(name) - { - - } + AbstractVisualNode(const DiscretePoint& location, const std::string& name = {}); virtual ~AbstractVisualNode() = default; - SceneItem* getSceneItem() const - { - return mSceneItem.get(); - } + void addChild(AbstractVisualNode* child); - virtual void update(FontsManager* fontsManager) - { + virtual SceneItem* getSceneItem(std::size_t idx) const; - } + virtual unsigned getNumSceneItems() const; - Image* getImage() const - { - return mImage.get(); - } + unsigned getNumChildren() const; - void syncChildren(const std::vector& children) - { - mChildren = children; - } + const std::vector& getChildren() const; - void addChild(AbstractVisualNode* child) - { - mChildren.push_back(child); - } + const std::string& getName() const; - const std::vector& getChildren() const - { - return mChildren; - } + Image* getImage() const; - void setIsVisible(bool isVisible) - { - //std::cout << "Setting " << mName << " visibility to " << isVisible << std::endl; - mIsVisible = isVisible; - } + bool getIsVisible() const; - unsigned getNumChildren() const - { - return mChildren.size(); - } + virtual void update(FontsManager* fontsManager); - void setName(const std::string& name) - { - mName = name; - } + void syncChildren(const std::vector& children); - const std::string& getName() const - { - return mName; - } + void setIsVisible(bool isVisible); - bool getIsVisible() const - { - return mIsVisible; - } + void setName(const std::string& name); - void setLocation(const DiscretePoint& loc) - { - if (mLocation != loc) - { - mTransformIsDirty = true; - mLocation = loc; - } - } + void setLocation(const DiscretePoint& loc); protected: DiscretePoint mLocation; - std::unique_ptr mSceneItem; + std::vector > mSceneItems; std::unique_ptr > mImage; std::vector mChildren; diff --git a/src/visual_elements/CMakeLists.txt b/src/visual_elements/CMakeLists.txt index e105441..6d4be45 100644 --- a/src/visual_elements/CMakeLists.txt +++ b/src/visual_elements/CMakeLists.txt @@ -9,6 +9,9 @@ list(APPEND visual_elements_LIB_INCLUDES SceneText.cpp Transform.cpp Texture.cpp + GridNode.cpp + AbstractVisualNode.cpp + ) add_library(visual_elements SHARED ${visual_elements_LIB_INCLUDES}) diff --git a/src/visual_elements/GridNode.cpp b/src/visual_elements/GridNode.cpp new file mode 100644 index 0000000..c1c5602 --- /dev/null +++ b/src/visual_elements/GridNode.cpp @@ -0,0 +1,123 @@ +#include "GridNode.h" + +#include "MeshPrimitives.h" + +GridNode::GridNode(const DiscretePoint& location) + : MaterialNode(location) +{ + +} + +void GridNode::setNumX(unsigned numX) +{ + if (mNumberX != numX) + { + mNumberX = numX; + mDataDirty = true; + } +} + +void GridNode::setNumY(unsigned numY) +{ + if (mNumberY != numY) + { + mNumberY = numY; + mDataDirty = true; + } +} + +void GridNode::setWidth(unsigned width) +{ + if (mWidth != width) + { + mTransformIsDirty = true; + mWidth = width; + } +} + +void GridNode::setHeight(unsigned height) +{ + if (mHeight != height) + { + mTransformIsDirty = true; + mHeight = height; + } +} + +void GridNode::setData(const std::vector& 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(); + } +} + +unsigned GridNode::getNumSceneItems() const +{ + return 1; +} + +void GridNode::update(FontsManager* fontsManager) +{ + if (!mBackgroundModel || mDataDirty) + { + auto mesh = MeshPrimitives::buildExplodedGrid(mNumberX, mNumberY); + + if (mHeight > mWidth) + { + mesh->scale(mWidth, mWidth); + } + else + { + mesh->scale(mHeight, mHeight); + } + + + if (!mBackgroundModel) + { + std::cout << "Setting up background model for grid node " << std::endl; + mBackgroundModel = std::make_unique(std::move(mesh)); + mBackgroundModel->setName(mName + "_Model"); + mBackgroundModel->updateUniformColor({200, 200, 200, 1.0}); + } + else + { + mBackgroundModel.get()->updateMesh(std::move(mesh)); + mBackgroundModel->updateUniformColor({200, 200, 200, 1.0}); + } + } + + /* + if (!mOutlineModel || mDataDirty) + { + const auto rect = Rectangle(Point(), 1, 1); + auto mesh = MeshPrimitives::build(rect); + + if (!mOutlineModel) + { + mOutlineModel = std::make_unique(std::move(mesh)); + mOutlineModel->setName(mName + "_Model"); + } + else + { + mOutlineModel.get()->updateMesh(std::move(mesh)); + } + } + */ + + mDataDirty = false; +} + diff --git a/src/visual_elements/GridNode.h b/src/visual_elements/GridNode.h new file mode 100644 index 0000000..5dfc66c --- /dev/null +++ b/src/visual_elements/GridNode.h @@ -0,0 +1,36 @@ +#pragma once + +#include "MaterialNode.h" +#include "Color.h" + +class GridNode : public MaterialNode +{ + +public: + GridNode(const DiscretePoint& location); + + void setNumX(unsigned numX); + + void setNumY(unsigned numY); + + void setData(const std::vector& colors); + + SceneItem* getSceneItem(std::size_t idx) const override; + unsigned getNumSceneItems() const override; + + void update(FontsManager* fontsManager) override; + + void setWidth(unsigned width); + void setHeight(unsigned height); + +private: + unsigned mNumberX{5}; + unsigned mNumberY{5}; + unsigned mWidth{1}; + unsigned mHeight{1}; + bool mDataDirty = true; + std::vector mData; + + std::unique_ptr mBackgroundModel; + std::unique_ptr mOutlineModel; +}; diff --git a/src/visual_elements/RectangleNode.cpp b/src/visual_elements/RectangleNode.cpp index 1178512..90d8a65 100644 --- a/src/visual_elements/RectangleNode.cpp +++ b/src/visual_elements/RectangleNode.cpp @@ -24,6 +24,23 @@ GeometryNode::Type RectangleNode::getType() return GeometryNode::Type::Rectangle; } +SceneItem* RectangleNode::getSceneItem(std::size_t idx) const +{ + if (idx == 0) + { + return mBackgroundItem.get(); + } + else + { + return nullptr; + } +} + +unsigned RectangleNode::getNumSceneItems() const +{ + return 1; +} + unsigned RectangleNode::getWidth() const { return mWidth; @@ -54,32 +71,32 @@ void RectangleNode::setHeight(unsigned height) void RectangleNode::update(FontsManager* fontsManager) { - if (!mSceneItem || mGeometryIsDirty) + if (!mBackgroundItem || mGeometryIsDirty) { const auto rect = Rectangle(Point(), 1, 1); auto mesh = MeshPrimitives::build(rect); - if (!mSceneItem) + if (!mBackgroundItem) { - mSceneItem = std::make_unique(std::move(mesh)); - mSceneItem->setName(mName + "_Model"); + mBackgroundItem = std::make_unique(std::move(mesh)); + mBackgroundItem->setName(mName + "_Model"); } else { - dynamic_cast(mSceneItem.get())->updateMesh(std::move(mesh)); + mBackgroundItem->updateMesh(std::move(mesh)); } mGeometryIsDirty = false; } if (mTransformIsDirty) { - mSceneItem->updateTransform({mLocation, static_cast(mWidth), static_cast(mHeight)}); + mBackgroundItem->updateTransform({mLocation, static_cast(mWidth), static_cast(mHeight)}); mTransformIsDirty = false; } if (mMaterialIsDirty) { - mSceneItem->updateUniformColor(mFillColor); + mBackgroundItem->updateUniformColor(mFillColor); mMaterialIsDirty = false; } } diff --git a/src/visual_elements/RectangleNode.h b/src/visual_elements/RectangleNode.h index 9c8ae6c..554f6ba 100644 --- a/src/visual_elements/RectangleNode.h +++ b/src/visual_elements/RectangleNode.h @@ -15,6 +15,9 @@ public: unsigned getWidth() const; unsigned getHeight() const; + SceneItem* getSceneItem(std::size_t idx) const override; + unsigned getNumSceneItems() const override; + void setWidth(unsigned width); void setHeight(unsigned height); @@ -22,6 +25,9 @@ public: private: unsigned mWidth{1}; unsigned mHeight{1}; + + std::unique_ptr mBackgroundItem; + std::unique_ptr mOutlineItem; }; using RectangleNodePtr = std::unique_ptr; diff --git a/src/visual_elements/Scene.cpp b/src/visual_elements/Scene.cpp index dd13ea6..b372969 100644 --- a/src/visual_elements/Scene.cpp +++ b/src/visual_elements/Scene.cpp @@ -30,9 +30,12 @@ void Scene::updateNode(AbstractVisualNode* node, FontsManager* fontsManager) node->update(fontsManager); - if(auto item = node->getSceneItem()) + for (std::size_t idx=0; idx< node->getNumSceneItems(); idx++) { - mSceneItems.push_back(node->getSceneItem()); + if (auto item = node->getSceneItem(idx)) + { + mSceneItems.push_back(item); + } } } diff --git a/src/visual_elements/TextNode.cpp b/src/visual_elements/TextNode.cpp index 0db5832..5083a9c 100644 --- a/src/visual_elements/TextNode.cpp +++ b/src/visual_elements/TextNode.cpp @@ -78,6 +78,23 @@ void TextNode::setContent(const std::string& content) } } +SceneItem* TextNode::getSceneItem(std::size_t idx) const +{ + if (idx == 0) + { + return mTextItem.get(); + } + else + { + return 0; + } +} + +unsigned TextNode::getNumSceneItems() const +{ + return 1; +} + void TextNode::updateLines(FontsManager* fontsManager) { auto original_count = mTextData.mLines.size(); @@ -116,10 +133,10 @@ void TextNode::updateLines(FontsManager* fontsManager) void TextNode::update(FontsManager* fontsManager) { - if (!mSceneItem) + if (!mTextItem) { - mSceneItem = std::make_unique(); - mSceneItem->setName(mName + "_SceneText"); + mTextItem = std::make_unique(); + mTextItem->setName(mName + "_SceneText"); } if (mTransformIsDirty || mContentIsDirty) @@ -129,20 +146,20 @@ void TextNode::update(FontsManager* fontsManager) if (mContentIsDirty || mLinesAreDirty) { - dynamic_cast(mSceneItem.get())->setTextData(mTextData); + dynamic_cast(mTextItem.get())->setTextData(mTextData); mContentIsDirty = false; mLinesAreDirty = false; } if (mTransformIsDirty) { - mSceneItem->updateTransform({mLocation}); + mTextItem->updateTransform({mLocation}); mTransformIsDirty = false; } if (mMaterialIsDirty) { - mSceneItem->updateUniformColor(mFillColor); + mTextItem->updateUniformColor(mFillColor); mMaterialIsDirty = false; } } diff --git a/src/visual_elements/TextNode.h b/src/visual_elements/TextNode.h index 15ccea8..b44eb79 100644 --- a/src/visual_elements/TextNode.h +++ b/src/visual_elements/TextNode.h @@ -20,6 +20,9 @@ public: std::string getContent() const; std::string getFontLabel() const; + SceneItem* getSceneItem(std::size_t idx) const override; + unsigned getNumSceneItems() const override; + unsigned getWidth() const; unsigned getHeight() const; @@ -39,6 +42,8 @@ private: unsigned mWidth{1}; unsigned mHeight{1}; + + std::unique_ptr mTextItem; }; using TextNodetr = std::unique_ptr;