From 3915a40c9641f31843ac71bba9094e161d340408 Mon Sep 17 00:00:00 2001 From: jmsgrogan Date: Thu, 19 Jan 2023 15:42:34 +0000 Subject: [PATCH] Start cleaning icon. --- .../nodes/AbstractVisualNode.h | 2 +- .../visual_elements/nodes/PathNode.cpp | 2 +- src/rendering/visual_elements/svg/SvgNode.cpp | 76 +++++++++++++++---- src/rendering/visual_elements/svg/SvgNode.h | 17 +++-- src/ui/ui_controls/Button.cpp | 33 ++++++-- src/ui/ui_controls/Button.h | 3 + src/ui/ui_elements/IconNode.cpp | 47 +++++++++++- src/ui/ui_elements/IconNode.h | 17 ++++- src/ui/ui_elements/style/FontTokens.cpp | 2 +- test/ui_controls/TestButton.cpp | 6 +- 10 files changed, 172 insertions(+), 33 deletions(-) diff --git a/src/rendering/visual_elements/nodes/AbstractVisualNode.h b/src/rendering/visual_elements/nodes/AbstractVisualNode.h index 2256385..7a4d003 100644 --- a/src/rendering/visual_elements/nodes/AbstractVisualNode.h +++ b/src/rendering/visual_elements/nodes/AbstractVisualNode.h @@ -41,7 +41,7 @@ public: void setName(const std::string& name); - void setLocation(const Point& loc); + virtual void setLocation(const Point& loc); virtual void update(SceneInfo* sceneInfo); diff --git a/src/rendering/visual_elements/nodes/PathNode.cpp b/src/rendering/visual_elements/nodes/PathNode.cpp index 3936b74..e382129 100644 --- a/src/rendering/visual_elements/nodes/PathNode.cpp +++ b/src/rendering/visual_elements/nodes/PathNode.cpp @@ -5,7 +5,7 @@ PathNode::PathNode(const Point& loc, const std::string& psPath) : GeometryNode(loc), - mPathString(psPath) + mPathString(psPath) { } diff --git a/src/rendering/visual_elements/svg/SvgNode.cpp b/src/rendering/visual_elements/svg/SvgNode.cpp index 754ba0c..dd77c85 100644 --- a/src/rendering/visual_elements/svg/SvgNode.cpp +++ b/src/rendering/visual_elements/svg/SvgNode.cpp @@ -7,7 +7,7 @@ #include "SvgShapeElements.h" SvgNode::SvgNode(const Point& location) - : AbstractVisualNode(location) + : MaterialNode(location) { } @@ -18,7 +18,25 @@ void SvgNode::setContent(std::unique_ptr doc) mContentDirty = true; mChildren.clear(); - mManagedChildren.clear(); + mGeometryNodes.clear(); +} + +void SvgNode::setWidth(unsigned width) +{ + if (mWidth != width) + { + mWidth = width; + mTransformIsDirty = true; + } +} + +void SvgNode::setHeight(unsigned height) +{ + if (mHeight != height) + { + mHeight = height; + mTransformIsDirty = true; + } } void SvgNode::updateTransform() @@ -37,25 +55,23 @@ void SvgNode::createOrUpdateGeometry(SceneInfo* sceneInfo) for (const auto& svg_element : mContent->getRoot()->getChildren()) { - std::unique_ptr node; + std::unique_ptr geom_node; if (svg_element->getTagName() == "circle") { - onCircle(svg_element.get(), node); + onCircle(svg_element.get(), geom_node); } else if (svg_element->getTagName() == "path") { - onPath(svg_element.get(), node); + onPath(svg_element.get(), geom_node); } - if (!node) + AbstractVisualNode* raw_node{ nullptr }; + if (geom_node) { - continue; + raw_node = geom_node.get(); + mGeometryNodes.push_back(std::move(geom_node)); } - - auto raw_node = node.get(); - mManagedChildren.push_back(std::move(node)); - addChild(raw_node); } } @@ -73,9 +89,43 @@ void SvgNode::update(SceneInfo* sceneInfo) updateTransform(); mTransformIsDirty = false; } + + if (mMaterialIsDirty) + { + if (mHasFillColor) + { + for (const auto& geom_node : mGeometryNodes) + { + geom_node->setFillColor(mFillColor); + } + } + else + { + for (const auto& geom_node : mGeometryNodes) + { + geom_node->setHasFillColor(false); + } + } + + if (mHasStrokeColor) + { + for (const auto& geom_node : mGeometryNodes) + { + geom_node->setStrokeColor(mStrokeColor); + } + } + else + { + for (const auto& geom_node : mGeometryNodes) + { + geom_node->setHasStrokeColor(false); + } + } + mMaterialIsDirty = false; + } } -void SvgNode::onCircle(XmlElement* element, std::unique_ptr& node) +void SvgNode::onCircle(XmlElement* element, std::unique_ptr& node) { auto svg_circle = dynamic_cast(element); auto loc = svg_circle->getLocation(); @@ -100,7 +150,7 @@ void SvgNode::onCircle(XmlElement* element, std::unique_ptr& node = std::move(circle_node); } -void SvgNode::onPath(XmlElement* element, std::unique_ptr& node) +void SvgNode::onPath(XmlElement* element, std::unique_ptr& node) { auto svg_path = dynamic_cast(element); diff --git a/src/rendering/visual_elements/svg/SvgNode.h b/src/rendering/visual_elements/svg/SvgNode.h index e936a79..8251fc1 100644 --- a/src/rendering/visual_elements/svg/SvgNode.h +++ b/src/rendering/visual_elements/svg/SvgNode.h @@ -1,28 +1,35 @@ #pragma once -#include "AbstractVisualNode.h" +#include "MaterialNode.h" +#include "GeometryNode.h" #include "SvgDocument.h" #include #include -class SvgNode : public AbstractVisualNode +class SvgNode : public MaterialNode { public: SvgNode(const Point& location); void setContent(std::unique_ptr doc); + void setWidth(unsigned width); + void setHeight(unsigned height); + void update(SceneInfo* sceneInfo); private: void createOrUpdateGeometry(SceneInfo* sceneInfo); void updateTransform(); - void onCircle(XmlElement* element, std::unique_ptr& node); - void onPath(XmlElement* element, std::unique_ptr& node); + void onCircle(XmlElement* element, std::unique_ptr& node); + void onPath(XmlElement* element, std::unique_ptr& node); + + unsigned mWidth{ 0 }; + unsigned mHeight{ 0 }; bool mContentDirty{ true }; - std::vector > mManagedChildren; + std::vector > mGeometryNodes; std::unique_ptr mContent; }; diff --git a/src/ui/ui_controls/Button.cpp b/src/ui/ui_controls/Button.cpp index 67e212c..141a53b 100644 --- a/src/ui/ui_controls/Button.cpp +++ b/src/ui/ui_controls/Button.cpp @@ -21,6 +21,8 @@ Button::Button(ButtonData::Component component) { mStyle.mComponent = component; mName = "Button"; + setWidth(150); + setMaxWidth(150); setHeight(mStyle.getContainerHeight()); setMaxHeight(mStyle.getContainerHeight()); @@ -95,6 +97,9 @@ void Button::updateState() setLabelTextColor(mStyle.getLabelTextColor()); setLabelTextOpacity(mStyle.getLabelOpacity()); setLabelTextTypescale(mStyle.getLabelTypescale()); + + setIconColor(mStyle.getIconColor()); + //setIconOpacity() } void Button::setLabelTextColor(Theme::Sys::Color color) @@ -124,6 +129,24 @@ void Button::setLabelTextTypescale(Theme::Sys::Typescale typescale) } } +void Button::setIconColor(Theme::Sys::Color color) +{ + if (mIconColor != color) + { + mIconColor = color; + mMaterialDirty = true; + } +} + +void Button::setIconOpacity(float opacity) +{ + if (mIconOpacity!= opacity) + { + mIconOpacity = opacity; + mMaterialDirty = true; + } +} + void Button::onMyMouseEvent(const MouseEvent* event) { if (!mEnabled) @@ -237,6 +260,10 @@ void Button::updateIcon(const PaintEvent* event) mIconNode->setContent(IconNode::IconType::Svg, MediaResourceManager::getSvgIconNode(mIcon)); mRootNode->addChild(mIconNode.get()); } + else if (mContentDirty && mIcon != Resource::Icon::Svg::NONE) + { + mIconNode->setContent(IconNode::IconType::Svg, MediaResourceManager::getSvgIconNode(mIcon)); + } if (!mIconNode) { @@ -252,11 +279,7 @@ void Button::updateIcon(const PaintEvent* event) { auto icon_fill = event->getThemesManager()->getColor(mIconColor); icon_fill.setAlpha(mIconOpacity); - } - - if (mContentDirty) - { - mIconNode->setContent(IconNode::IconType::Svg, MediaResourceManager::getSvgIconNode(mIcon)); + mIconNode->setFillColor(icon_fill); } if (mVisibilityDirty) diff --git a/src/ui/ui_controls/Button.h b/src/ui/ui_controls/Button.h index 5e068df..4443aae 100644 --- a/src/ui/ui_controls/Button.h +++ b/src/ui/ui_controls/Button.h @@ -43,6 +43,9 @@ protected: void setLabelTextOpacity(float opacity); void setLabelTextTypescale(Theme::Sys::Typescale typescale); + void setIconColor(Theme::Sys::Color color); + void setIconOpacity(float opacity); + void updateLabel(const PaintEvent* event); void updateIcon(const PaintEvent* event); diff --git a/src/ui/ui_elements/IconNode.cpp b/src/ui/ui_elements/IconNode.cpp index 4f2fa55..975d9ea 100644 --- a/src/ui/ui_elements/IconNode.cpp +++ b/src/ui/ui_elements/IconNode.cpp @@ -1,14 +1,55 @@ #include "IconNode.h" +#include "SvgNode.h" + IconNode::IconNode(const Point& loc) : AbstractVisualNode(loc) { } -void IconNode::setContent(IconType type, std::unique_ptr content) +void IconNode::setSvgContent(std::unique_ptr svg_node) { - mType = type; - mContent = std::move(content); + mChildren.clear(); + + mType = IconType::Svg; + + mSvgContent = std::move(svg_node); mContentDirty = true; + + addChild(mSvgContent.get()); +} + +void IconNode::setFillColor(const Color& color) +{ + if (mType == IconType::Svg) + { + mSvgContent->setFillColor(color); + mSvgContent->setHasStrokeColor(false); + } +} + +void IconNode::setWidth(unsigned width) +{ + if (mType == IconType::Svg) + { + mSvgContent->setWidth(width); + } +} + +void IconNode::setHeight(unsigned height) +{ + if (mType == IconType::Svg) + { + mSvgContent->setFillColor(height); + mSvgContent->setHasStrokeColor(false); + } +} + +void IconNode::setOutlineColor(const Color& color) +{ + if (mType == IconType::Svg) + { + dynamic_cast(mContent.get())->setStrokeColor(color); + } } \ No newline at end of file diff --git a/src/ui/ui_elements/IconNode.h b/src/ui/ui_elements/IconNode.h index 5c17085..ca3a287 100644 --- a/src/ui/ui_elements/IconNode.h +++ b/src/ui/ui_elements/IconNode.h @@ -2,6 +2,8 @@ #include "AbstractVisualNode.h" +#include "SvgNode.h" + class IconNode : public AbstractVisualNode { public: @@ -14,10 +16,21 @@ public: IconNode(const Point& loc); - void setContent(IconType type, std::unique_ptr); + void setLocation(const Point& loc) override; + + void setWidth(unsigned width); + + void setHeight(unsigned height); + + void setFillColor(const Color& color); + + void setOutlineColor(const Color& color); + + void setSvgContent(std::unique_ptr svg_node); private: - std::unique_ptr mContent; + std::unique_ptr mSvgContent; + IconType mType{ IconType::Svg }; bool mContentDirty{ true }; diff --git a/src/ui/ui_elements/style/FontTokens.cpp b/src/ui/ui_elements/style/FontTokens.cpp index 57a0651..e97f25e 100644 --- a/src/ui/ui_elements/style/FontTokens.cpp +++ b/src/ui/ui_elements/style/FontTokens.cpp @@ -42,7 +42,7 @@ unsigned FontTokens::getSize(Theme::Sys::Typescale typescale) switch (typescale) { case Theme::Sys::Typescale::Label_Large: - return static_cast(57/3); + return static_cast(57/3.5); default: return 57; } diff --git a/test/ui_controls/TestButton.cpp b/test/ui_controls/TestButton.cpp index b1cb3fe..508904e 100644 --- a/test/ui_controls/TestButton.cpp +++ b/test/ui_controls/TestButton.cpp @@ -23,18 +23,20 @@ TEST_CASE(TestButton_Elevated, "ui_controls") enabled_button->setLabel("Enabled"); enabled_button->setSvgIcon(Resource::Icon::Svg::HOME_MEDIUM); - /* + auto disabled_button = Button::Create(ButtonData::Component::Elevated); disabled_button->setEnabled(false); disabled_button->setLabel("Disabled"); + disabled_button->setSvgIcon(Resource::Icon::Svg::HOME_MEDIUM); + /* auto pressed_button = Button::Create(ButtonData::Component::Elevated); pressed_button->setLabel("Pressed"); */ spacer.addWidget(std::move(enabled_button)); - //spacer.addWidget(std::move(disabled_button)); + spacer.addWidget(std::move(disabled_button)); //spacer.addWidget(std::move(pressed_button)); auto node = spacer.getRootNode();