aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/graph/window_render_target.cpp2
-rw-r--r--src/main.cpp7
-rw-r--r--src/ui/controls/button.cpp17
-rw-r--r--src/ui/controls/button.hpp10
-rw-r--r--src/ui/controls/flex_layout.cpp6
-rw-r--r--src/ui/controls/flex_layout.hpp6
-rw-r--r--src/ui/controls/text_block.cpp8
-rw-r--r--src/ui/controls/text_block.hpp6
-rw-r--r--src/ui/render/border_render_object.cpp10
-rw-r--r--src/ui/render/border_render_object.hpp11
-rw-r--r--src/ui/render/text_render_object.hpp8
-rw-r--r--src/ui/ui_manager.cpp5
-rw-r--r--src/ui/ui_manager.hpp8
13 files changed, 71 insertions, 33 deletions
diff --git a/src/graph/window_render_target.cpp b/src/graph/window_render_target.cpp
index d110101a..a36e0faf 100644
--- a/src/graph/window_render_target.cpp
+++ b/src/graph/window_render_target.cpp
@@ -93,7 +93,5 @@ void WindowRenderTarget::CreateTargetBitmap() {
graph_manager_->GetD2D1DeviceContext()->CreateBitmapFromDxgiSurface(
dxgi_back_buffer.Get(), &bitmap_properties, &bitmap));
this->target_bitmap_ = util::CreateComSharedPtr(bitmap);
-
- dxgi_back_buffer->Release();
}
} // namespace cru::graph
diff --git a/src/main.cpp b/src/main.cpp
index 906528c4..c35213f7 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,6 +1,7 @@
#include "pre.hpp"
#include "application.hpp"
+#include "ui/controls/button.hpp"
#include "ui/controls/flex_layout.hpp"
#include "ui/controls/text_block.hpp"
#include "ui/window.hpp"
@@ -11,6 +12,7 @@ using cru::StringView;
using cru::ui::Rect;
using cru::ui::Thickness;
using cru::ui::Window;
+using cru::ui::controls::Button;
using cru::ui::controls::FlexLayout;
using cru::ui::controls::TextBlock;
@@ -28,15 +30,16 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
window->SetChild(flex_layout);
+ const auto button = Button::Create();
const auto text_block1 = TextBlock::Create();
text_block1->SetText(L"Hello World!");
- flex_layout->AddChild(text_block1, 0);
+ button->SetChild(text_block1);
+ flex_layout->AddChild(button, 0);
const auto text_block2 = TextBlock::Create();
text_block2->SetText(L"Hello World!");
flex_layout->AddChild(text_block2, 1);
-
window->Show();
return application.Run();
diff --git a/src/ui/controls/button.cpp b/src/ui/controls/button.cpp
index 5d4e15cc..a6578251 100644
--- a/src/ui/controls/button.cpp
+++ b/src/ui/controls/button.cpp
@@ -1,13 +1,24 @@
#include "button.hpp"
-#include "ui/ui_manager.hpp"
#include "ui/render/border_render_object.hpp"
+#include "ui/ui_manager.hpp"
namespace cru::ui::controls {
Button::Button() {
const auto predefined_resource =
UiManager::GetInstance()->GetPredefineResources();
- render_object_ = new render::BorderRenderObject(); }
+ render_object_.reset(new render::BorderRenderObject(
+ predefined_resource->button_normal_border_brush));
+ render_object_->SetEnabled(true);
+ render_object_->SetBorderWidth(Thickness{3});
+ render_object_->SetCornerRadius(render::CornerRadius{Point{10, 5}});
+}
+
+render::RenderObject* Button::GetRenderObject() const { return render_object_.get(); }
-void Button::OnChildChanged(Control* old_child, Control* new_child) {}
+void Button::OnChildChanged(Control* old_child, Control* new_child) {
+ if (old_child != nullptr) render_object_->RemoveChild(0);
+ if (new_child != nullptr)
+ render_object_->AddChild(new_child->GetRenderObject(), 0);
+}
} // namespace cru::ui::controls
diff --git a/src/ui/controls/button.hpp b/src/ui/controls/button.hpp
index 27e7fc7b..3f313dfc 100644
--- a/src/ui/controls/button.hpp
+++ b/src/ui/controls/button.hpp
@@ -1,6 +1,8 @@
#pragma once
#include "pre.hpp"
+#include <memory>
+
#include "ui/content_control.hpp"
namespace cru::ui::render {
@@ -12,7 +14,7 @@ class Button : public ContentControl {
public:
static constexpr auto control_type = L"Button";
- static Button* Create();
+ static Button* Create() { return new Button(); }
protected:
Button();
@@ -22,7 +24,9 @@ class Button : public ContentControl {
Button(Button&& other) = delete;
Button& operator=(const Button& other) = delete;
Button& operator=(Button&& other) = delete;
- ~Button();
+ ~Button() override = default;
+
+ StringView GetControlType() const override final { return control_type; }
render::RenderObject* GetRenderObject() const override;
@@ -30,6 +34,6 @@ class Button : public ContentControl {
void OnChildChanged(Control* old_child, Control* new_child) override;
private:
- render::BorderRenderObject* render_object_;
+ std::shared_ptr<render::BorderRenderObject> render_object_{};
};
} // namespace cru::ui::controls
diff --git a/src/ui/controls/flex_layout.cpp b/src/ui/controls/flex_layout.cpp
index 289df1f1..bdbe2d73 100644
--- a/src/ui/controls/flex_layout.cpp
+++ b/src/ui/controls/flex_layout.cpp
@@ -5,12 +5,10 @@
namespace cru::ui::controls {
using render::FlexLayoutRenderObject;
-FlexLayout::FlexLayout() { render_object_ = new FlexLayoutRenderObject(); }
-
-FlexLayout::~FlexLayout() { delete render_object_; }
+FlexLayout::FlexLayout() { render_object_.reset(new FlexLayoutRenderObject()); }
render::RenderObject* FlexLayout::GetRenderObject() const {
- return render_object_;
+ return render_object_.get();
}
void FlexLayout::OnAddChild(Control* child, int position) {
diff --git a/src/ui/controls/flex_layout.hpp b/src/ui/controls/flex_layout.hpp
index 2ab3e259..9ceef1f6 100644
--- a/src/ui/controls/flex_layout.hpp
+++ b/src/ui/controls/flex_layout.hpp
@@ -1,6 +1,8 @@
#pragma once
#include "pre.hpp"
+#include <memory>
+
#include "ui/layout_control.hpp"
namespace cru::ui::render {
@@ -23,7 +25,7 @@ class FlexLayout : public LayoutControl {
FlexLayout(FlexLayout&& other) = delete;
FlexLayout& operator=(const FlexLayout& other) = delete;
FlexLayout& operator=(FlexLayout&& other) = delete;
- ~FlexLayout() override;
+ ~FlexLayout() override = default;
StringView GetControlType() const override final { return control_type; }
@@ -34,6 +36,6 @@ class FlexLayout : public LayoutControl {
void OnRemoveChild(Control* child, int position) override;
private:
- render::FlexLayoutRenderObject* render_object_;
+ std::shared_ptr<render::FlexLayoutRenderObject> render_object_;
};
} // namespace cru::ui::controls
diff --git a/src/ui/controls/text_block.cpp b/src/ui/controls/text_block.cpp
index c891b832..c2f8cd8e 100644
--- a/src/ui/controls/text_block.cpp
+++ b/src/ui/controls/text_block.cpp
@@ -9,16 +9,14 @@ using render::TextRenderObject;
TextBlock::TextBlock() {
const auto predefined_resources =
UiManager::GetInstance()->GetPredefineResources();
- render_object_ =
+ render_object_.reset(
new TextRenderObject(predefined_resources->text_block_text_brush,
predefined_resources->text_block_text_format,
- predefined_resources->text_block_selection_brush);
+ predefined_resources->text_block_selection_brush));
}
-TextBlock::~TextBlock() { delete render_object_; }
-
render::RenderObject* TextBlock::GetRenderObject() const {
- return render_object_;
+ return render_object_.get();
}
String TextBlock::GetText() const { return render_object_->GetText(); }
diff --git a/src/ui/controls/text_block.hpp b/src/ui/controls/text_block.hpp
index 4c443020..0d65dd67 100644
--- a/src/ui/controls/text_block.hpp
+++ b/src/ui/controls/text_block.hpp
@@ -1,6 +1,8 @@
#pragma once
#include "pre.hpp"
+#include <memory>
+
#include "ui/no_child_control.hpp"
namespace cru::ui::render {
@@ -22,7 +24,7 @@ class TextBlock : public NoChildControl {
TextBlock(TextBlock&& other) = delete;
TextBlock& operator=(const TextBlock& other) = delete;
TextBlock& operator=(TextBlock&& other) = delete;
- ~TextBlock() override;
+ ~TextBlock() override = default;
StringView GetControlType() const override final { return control_type; }
@@ -32,6 +34,6 @@ class TextBlock : public NoChildControl {
void SetText(const String& text);
private:
- render::TextRenderObject* render_object_;
+ std::shared_ptr<render::TextRenderObject> render_object_;
};
} // namespace cru::ui::controls
diff --git a/src/ui/render/border_render_object.cpp b/src/ui/render/border_render_object.cpp
index e1550665..49700869 100644
--- a/src/ui/render/border_render_object.cpp
+++ b/src/ui/render/border_render_object.cpp
@@ -77,8 +77,9 @@ void BorderRenderObject::RecreateGeometry() {
const auto size = GetSize();
const auto margin = GetMargin();
- const Rect outer_rect{margin.left, margin.top, size.width - margin.right,
- size.height - size.height};
+ const Rect outer_rect{margin.left, margin.top,
+ size.width - margin.GetHorizontalTotal(),
+ size.height - margin.GetVerticalTotal()};
ThrowIfFailed(border_outer_geometry->Open(&sink));
f(outer_rect, corner_radius_);
ThrowIfFailed(sink->Close());
@@ -137,6 +138,11 @@ void BorderRenderObject::OnAddChild(RenderObject* new_child, int position) {
assert(GetChildren().size() == 1);
}
+void BorderRenderObject::OnSizeChanged(const Size& old_size,
+ const Size& new_size) {
+ RecreateGeometry();
+}
+
void BorderRenderObject::OnMeasureCore(const Size& available_size) {
const auto margin = GetMargin();
const auto padding = GetPadding();
diff --git a/src/ui/render/border_render_object.hpp b/src/ui/render/border_render_object.hpp
index eccb1219..6f9a8c11 100644
--- a/src/ui/render/border_render_object.hpp
+++ b/src/ui/render/border_render_object.hpp
@@ -13,6 +13,11 @@ namespace cru::ui::render {
struct CornerRadius {
constexpr CornerRadius()
: left_top(), right_top(), left_bottom(), right_bottom() {}
+ constexpr CornerRadius(const Point& value)
+ : left_top(value),
+ right_top(value),
+ left_bottom(value),
+ right_bottom(value) {}
constexpr CornerRadius(Point left_top, Point right_top, Point left_bottom,
Point right_bottom)
: left_top(left_top),
@@ -42,7 +47,9 @@ class BorderRenderObject : public RenderObject {
void SetBrush(ID2D1Brush* new_brush);
Thickness GetBorderWidth() const { return border_thickness_; }
- void SetBorderWidth(const Thickness& thickness) { border_thickness_ = thickness; }
+ void SetBorderWidth(const Thickness& thickness) {
+ border_thickness_ = thickness;
+ }
CornerRadius GetCornerRadius() const { return corner_radius_; }
void SetCornerRadius(const CornerRadius& new_corner_radius) {
@@ -58,6 +65,8 @@ class BorderRenderObject : public RenderObject {
protected:
void OnAddChild(RenderObject* new_child, int position) override;
+ void OnSizeChanged(const Size& old_size, const Size& new_size) override;
+
void OnMeasureCore(const Size& available_size) override;
void OnLayoutCore(const Rect& rect) override;
Size OnMeasureContent(const Size& available_size) override;
diff --git a/src/ui/render/text_render_object.hpp b/src/ui/render/text_render_object.hpp
index 30d78736..db102b04 100644
--- a/src/ui/render/text_render_object.hpp
+++ b/src/ui/render/text_render_object.hpp
@@ -59,11 +59,11 @@ class TextRenderObject : public RenderObject {
private:
String text_;
- ID2D1Brush* brush_;
- IDWriteTextFormat* text_format_;
- IDWriteTextLayout* text_layout_;
+ ID2D1Brush* brush_ = nullptr;
+ IDWriteTextFormat* text_format_ = nullptr;
+ IDWriteTextLayout* text_layout_ = nullptr;
std::optional<TextRange> selection_range_ = std::nullopt;
- ID2D1Brush* selection_brush_;
+ ID2D1Brush* selection_brush_ = nullptr;
};
} // namespace cru::ui::render
diff --git a/src/ui/ui_manager.cpp b/src/ui/ui_manager.cpp
index add77516..4d14575b 100644
--- a/src/ui/ui_manager.cpp
+++ b/src/ui/ui_manager.cpp
@@ -40,12 +40,16 @@ IDWriteTextFormat* CreateDefaultTextFormat() {
PredefineResources::PredefineResources() {
try {
+ button_normal_border_brush =
+ graph::CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black));
+
text_block_selection_brush =
graph::CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::LightSkyBlue));
text_block_text_brush =
graph::CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black));
text_block_text_format = CreateDefaultTextFormat();
} catch (...) {
+ util::SafeRelease(button_normal_border_brush);
util::SafeRelease(text_block_selection_brush);
util::SafeRelease(text_block_text_brush);
util::SafeRelease(text_block_text_format);
@@ -53,6 +57,7 @@ PredefineResources::PredefineResources() {
}
PredefineResources::~PredefineResources() {
+ util::SafeRelease(button_normal_border_brush);
util::SafeRelease(text_block_selection_brush);
util::SafeRelease(text_block_text_brush);
util::SafeRelease(text_block_text_format);
diff --git a/src/ui/ui_manager.hpp b/src/ui/ui_manager.hpp
index b736381d..107b536c 100644
--- a/src/ui/ui_manager.hpp
+++ b/src/ui/ui_manager.hpp
@@ -1,11 +1,10 @@
#pragma once
#include "pre.hpp"
-#include <d2d1.h>
-#include <wrl/client.h>
-
#include "base.hpp"
+struct ID2D1Brush;
+struct IDWriteTextFormat;
namespace cru::graph {
class GraphManager;
}
@@ -25,6 +24,9 @@ class PredefineResources : public Object {
PredefineResources& operator=(PredefineResources&& other) = delete;
~PredefineResources() override;
+ // region Button
+ ID2D1Brush* button_normal_border_brush = nullptr;
+
// region TextBlock
ID2D1Brush* text_block_selection_brush = nullptr;
ID2D1Brush* text_block_text_brush = nullptr;