diff options
author | crupest <crupest@outlook.com> | 2018-12-11 23:59:26 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2018-12-11 23:59:26 +0800 |
commit | 3540c1718c74e9fa3538ae92a6418eb02d27f1c5 (patch) | |
tree | 5b5ba264c7ef50e2f563d2b097596ddac20fb19d /src | |
parent | 5220ab4fee1281b2daeacf4dd50448e06eb1f5e8 (diff) | |
download | cru-3540c1718c74e9fa3538ae92a6418eb02d27f1c5.tar.gz cru-3540c1718c74e9fa3538ae92a6418eb02d27f1c5.tar.bz2 cru-3540c1718c74e9fa3538ae92a6418eb02d27f1c5.zip |
...
Diffstat (limited to 'src')
-rw-r--r-- | src/ui/render/render_object.cpp | 138 | ||||
-rw-r--r-- | src/ui/render/render_object.hpp | 236 |
2 files changed, 0 insertions, 374 deletions
diff --git a/src/ui/render/render_object.cpp b/src/ui/render/render_object.cpp index 6796f382..3e2e5265 100644 --- a/src/ui/render/render_object.cpp +++ b/src/ui/render/render_object.cpp @@ -24,113 +24,6 @@ namespace cru::ui::render render_host_->InvalidateRender(); } - SingleChildRenderObject::~SingleChildRenderObject() - { - delete child_; - } - - void SingleChildRenderObject::SetChild(RenderObject* new_child) - { - const auto old = child_; - if (old) - old->SetRenderHost(nullptr); - child_ = new_child; - if (new_child) - new_child->SetRenderHost(GetRenderHost()); - OnChildChange(old, new_child); - InvalidateRenderHost(); - } - - void SingleChildRenderObject::OnRenderHostChanged(IRenderHost* old_render_host, IRenderHost* new_render_host) - { - if (child_) - child_->SetRenderHost(new_render_host); - } - - void SingleChildRenderObject::OnChildChange(RenderObject* old_child, RenderObject* new_object) - { - - } - - ClipRenderObject::ClipRenderObject(Microsoft::WRL::ComPtr<ID2D1Geometry> clip_geometry) - : clip_geometry_(std::move(clip_geometry)) - { - - } - - void ClipRenderObject::SetClipGeometry(Microsoft::WRL::ComPtr<ID2D1Geometry> new_clip_geometry) - { - clip_geometry_ = std::move(new_clip_geometry); - InvalidateRenderHost(); - } - - void ClipRenderObject::Draw(ID2D1RenderTarget* render_target) - { - if (clip_geometry_ != nullptr) - render_target->PushLayer(D2D1::LayerParameters(D2D1::InfiniteRect(), clip_geometry_.Get()), nullptr); - const auto child = GetChild(); - if (child != nullptr) - child->Draw(render_target); - if (clip_geometry_ != nullptr) - render_target->PopLayer(); - } - - void MatrixRenderObject::ApplyAppendMatrix(ID2D1RenderTarget* render_target, const D2D1_MATRIX_3X2_F& matrix) - { - D2D1::Matrix3x2F old_matrix; - render_target->GetTransform(&old_matrix); - render_target->SetTransform(old_matrix * matrix); - } - - void MatrixRenderObject::ApplySetMatrix(ID2D1RenderTarget* render_target, const D2D1_MATRIX_3X2_F& matrix) - { - render_target->SetTransform(matrix); - } - - const MatrixRenderObject::MatrixApplier MatrixRenderObject::append_applier(ApplyAppendMatrix); - const MatrixRenderObject::MatrixApplier MatrixRenderObject::set_applier(ApplySetMatrix); - - MatrixRenderObject::MatrixRenderObject(const D2D1_MATRIX_3X2_F& matrix, MatrixApplier applier) - : matrix_(matrix), applier_(std::move(applier)) - { - - } - - void MatrixRenderObject::SetMatrix(const D2D1_MATRIX_3X2_F& new_matrix) - { - matrix_ = new_matrix; - InvalidateRenderHost(); - } - - void MatrixRenderObject::SetMatrixApplier(MatrixApplier applier) - { - applier_ = std::move(applier); - InvalidateRenderHost(); - } - - void MatrixRenderObject::Draw(ID2D1RenderTarget* render_target) - { - D2D1_MATRIX_3X2_F old_matrix; - render_target->GetTransform(&old_matrix); - applier_(render_target, matrix_); - const auto child = GetChild(); - if (child) - child->Draw(render_target); - render_target->SetTransform(&old_matrix); - } - - void OffsetRenderObject::SetOffsetX(const float new_offset_x) - { - offset_x_ = new_offset_x; - SetMatrix(D2D1::Matrix3x2F::Translation(offset_x_, offset_y_)); - } - - void OffsetRenderObject::SetOffsetY(const float new_offset_y) - { - offset_y_ = new_offset_y; - SetMatrix(D2D1::Matrix3x2F::Translation(offset_x_, offset_y_)); - } - void StrokeRenderObject::SetStrokeWidth(const float new_stroke_width) { if (stroke_width_ == new_stroke_width) @@ -225,35 +118,4 @@ namespace cru::ui::render if (draw_handler_ != nullptr) draw_handler_(render_target); } - - ContainerRenderObject::~ContainerRenderObject() - { - for (const auto child : children_) - delete child; - } - - void ContainerRenderObject::AddChild(RenderObject* child) - { - children_.push_back(child); - } - - void ContainerRenderObject::AddChild(RenderObject* child, const int position) - { - assert(position >= 0); - assert(position <= children_.size()); - children_.insert(children_.cbegin() + position, child); - } - - void ContainerRenderObject::RemoveChild(const int position) - { - assert(position >= 0); - assert(position < children_.size()); - children_.erase(children_.cbegin() + position); - } - - void ContainerRenderObject::Draw(ID2D1RenderTarget* render_target) - { - for (const auto child : children_) - child->Draw(render_target); - } } diff --git a/src/ui/render/render_object.hpp b/src/ui/render/render_object.hpp index 4432636c..b6f9f13b 100644 --- a/src/ui/render/render_object.hpp +++ b/src/ui/render/render_object.hpp @@ -18,24 +18,9 @@ namespace cru::ui::render * It represents a painting action on a d2d render target. By * overriding "Draw" virtual method, it can customize its painting * action. - * - * Render object may have implicit children to form a tree. - * RenderObject class doesn't provide any method to access children. - * Instead any concrete render object manage their own child model - * and interface and optionally call Draw on children so when root - * call Draw descendants' Draw will be called recursively. - * - * Related render objects of a control are a subtree of the whole tree. - * So render objects can be composed easily to form a whole control. - * - * Render object may do no actual drawing but perform particular - * actions on render target. Like ClipRenderObject apply a clip on - * render target, MatrixRenderObject apply a matrix on render - * target. */ - struct IRenderHost : Interface { virtual void InvalidateRender() = 0; @@ -72,130 +57,6 @@ namespace cru::ui::render }; - // It is subclass duty to call child's Draw. - class SingleChildRenderObject : public RenderObject - { - protected: - SingleChildRenderObject() = default; - public: - SingleChildRenderObject(const SingleChildRenderObject& other) = delete; - SingleChildRenderObject(SingleChildRenderObject&& other) = delete; - SingleChildRenderObject& operator=(const SingleChildRenderObject& other) = delete; - SingleChildRenderObject& operator=(SingleChildRenderObject&& other) = delete; - ~SingleChildRenderObject(); - - RenderObject* GetChild() const - { - return child_; - } - - void SetChild(RenderObject* new_child); - - protected: - void OnRenderHostChanged(IRenderHost* old_render_host, IRenderHost* new_render_host) override; - - virtual void OnChildChange(RenderObject* old_child, RenderObject* new_object); - - private: - RenderObject* child_ = nullptr; - }; - - - class ClipRenderObject final : public SingleChildRenderObject - { - public: - explicit ClipRenderObject(Microsoft::WRL::ComPtr<ID2D1Geometry> clip_geometry = nullptr); - ClipRenderObject(const ClipRenderObject& other) = delete; - ClipRenderObject(ClipRenderObject&& other) = delete; - ClipRenderObject& operator=(const ClipRenderObject& other) = delete; - ClipRenderObject& operator=(ClipRenderObject&& other) = delete; - ~ClipRenderObject() = default; - - Microsoft::WRL::ComPtr<ID2D1Geometry> GetClipGeometry() const - { - return clip_geometry_; - } - void SetClipGeometry(Microsoft::WRL::ComPtr<ID2D1Geometry> new_clip_geometry); - - void Draw(ID2D1RenderTarget* render_target) override final; - - private: - Microsoft::WRL::ComPtr<ID2D1Geometry> clip_geometry_; - }; - - - class MatrixRenderObject: public SingleChildRenderObject - { - private: - static void ApplyAppendMatrix(ID2D1RenderTarget* render_target, const D2D1_MATRIX_3X2_F& matrix); - static void ApplySetMatrix(ID2D1RenderTarget* render_target, const D2D1_MATRIX_3X2_F& matrix); - - public: - using MatrixApplier = std::function<void(ID2D1RenderTarget*, const D2D1_MATRIX_3X2_F&)>; - - static const MatrixApplier append_applier; - static const MatrixApplier set_applier; - - explicit MatrixRenderObject(const D2D1_MATRIX_3X2_F& matrix = D2D1::Matrix3x2F::Identity(), - MatrixApplier applier = append_applier); - MatrixRenderObject(const MatrixRenderObject& other) = delete; - MatrixRenderObject(MatrixRenderObject&& other) = delete; - MatrixRenderObject& operator=(const MatrixRenderObject& other) = delete; - MatrixRenderObject& operator=(MatrixRenderObject&& other) = delete; - ~MatrixRenderObject() = default; - - D2D1_MATRIX_3X2_F GetMatrix() const - { - return matrix_; - } - - void SetMatrix(const D2D1_MATRIX_3X2_F& new_matrix); - - MatrixApplier GetMatrixApplier() const - { - return applier_; - } - - void SetMatrixApplier(MatrixApplier applier); - - void Draw(ID2D1RenderTarget* render_target) override final; - - private: - D2D1_MATRIX_3X2_F matrix_; - MatrixApplier applier_; - }; - - - class OffsetRenderObject : public MatrixRenderObject - { - public: - explicit OffsetRenderObject(const float offset_x = 0.0f, const float offset_y = 0.0f) - : MatrixRenderObject(D2D1::Matrix3x2F::Translation(offset_x, offset_y)), - offset_x_(offset_x), offset_y_(offset_y) - { - - } - - float GetOffsetX() const - { - return offset_x_; - } - - void SetOffsetX(float new_offset_x); - - float GetOffsetY() const - { - return offset_y_; - } - - void SetOffsetY(float new_offset_y); - - private: - float offset_x_; - float offset_y_; - }; - - class StrokeRenderObject : public virtual RenderObject { protected: @@ -342,101 +203,4 @@ namespace cru::ui::render private: DrawHandler draw_handler_{}; }; - - - class ContainerRenderObject : public RenderObject - { - public: - ContainerRenderObject() = default; - ContainerRenderObject(const ContainerRenderObject& other) = delete; - ContainerRenderObject& operator=(const ContainerRenderObject& other) = delete; - ContainerRenderObject(ContainerRenderObject&& other) = delete; - ContainerRenderObject& operator=(ContainerRenderObject&& other) = delete; - ~ContainerRenderObject(); - - const std::vector<RenderObject*>& GetChildren() const - { - return children_; - } - - RenderObject* GetAt(const int position) const - { - assert(position >= 0); - assert(position < static_cast<int>(children_.size())); - return children_.at(static_cast<std::vector<RenderObject*>::size_type>(position)); - } - - void AddChild(RenderObject* child); - - void AddChild(RenderObject* child, int position); - - void RemoveChild(int position); - - protected: - void Draw(ID2D1RenderTarget* render_target) override; - - private: - std::vector<RenderObject*> children_; - }; - - - - // Render object tree for a control. (RO for RenderObject) - // - // ControlRO (not a SingleChildRO because child is not changed) - // | - // MatrixRO (control transform, only matrix exposed) - // | - // ClipRO (control clip, only clip geometry exposed) - // | - // OffsetRO (border offset) - // | - // ContainerRO - // / | - // StrokeRO (border) OffsetRO (padding offset) - // / | \ - // / | \ - // / | \ - // / | \ - // / | \ - // / | \ - // ContainerRO (background) | ContainerRO (foreground, symmetrical to background) - // / \ | / \ - // FillRO CustomDrawHandlerRO | FillRO CustomDrawHandlerRO - // | - // OffsetRO (content offset) - // | - // ContainerRO (content) - // / | \ - // / | \ - // / | \ - // ContainerRO (control-define content ROs) | ContainerRO (child-control ROs) - // | - // CustomDrawHandlerRO (user-define drawing) - // - class ControlRenderObject : public RenderObject - { - public: - ControlRenderObject() = default; - ControlRenderObject(const ControlRenderObject& other) = delete; - ControlRenderObject(ControlRenderObject&& other) = delete; - ControlRenderObject& operator=(const ControlRenderObject& other) = delete; - ControlRenderObject& operator=(ControlRenderObject&& other) = delete; - ~ControlRenderObject() override = default; - - - MatrixRenderObject* GetControlTransformRenderObject() const; - ClipRenderObject* GetControlClipRenderObject() const; - - OffsetRenderObject* GetBorderOffsetRenderObject() const; - RoundedRectangleStrokeRenderObject* GetBorderRenderObject() const; - - OffsetRenderObject* GetPaddingOffsetRenderObject() const; - Microsoft::WRL::ComPtr<ID2D1Geometry> GetPaddingGeometry() const; - - Point GetContentOffset() const; - ContainerRenderObject* GetContentContainer() const; - - ContainerRenderObject* GetChildrenContainer() const; - }; } |