aboutsummaryrefslogtreecommitdiff
path: root/src/ui/render/text_render_object.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-03-24 19:06:17 +0800
committercrupest <crupest@outlook.com>2019-03-24 19:06:17 +0800
commit79d1d76509dbf6cf9c79f8eb55968535982975aa (patch)
tree322ed87621506f2e56f4887c73370db769843a29 /src/ui/render/text_render_object.cpp
parente8be3841457853daefc26d0ca00256ad8c44f593 (diff)
downloadcru-79d1d76509dbf6cf9c79f8eb55968535982975aa.tar.gz
cru-79d1d76509dbf6cf9c79f8eb55968535982975aa.tar.bz2
cru-79d1d76509dbf6cf9c79f8eb55968535982975aa.zip
...
Diffstat (limited to 'src/ui/render/text_render_object.cpp')
-rw-r--r--src/ui/render/text_render_object.cpp67
1 files changed, 54 insertions, 13 deletions
diff --git a/src/ui/render/text_render_object.cpp b/src/ui/render/text_render_object.cpp
index e8967d48..b66fffa3 100644
--- a/src/ui/render/text_render_object.cpp
+++ b/src/ui/render/text_render_object.cpp
@@ -5,19 +5,61 @@
#include <algorithm>
#include "exception.hpp"
-#include "graph/graph.hpp"
+#include "graph/graph_manager.hpp"
+#include "graph/graph_util.hpp"
+#include "util/com_util.hpp"
namespace cru::ui::render {
-TextRenderObject::TextRenderObject(
- Microsoft::WRL::ComPtr<ID2D1Brush> brush,
- Microsoft::WRL::ComPtr<IDWriteTextFormat> format,
- Microsoft::WRL::ComPtr<ID2D1Brush> selection_brush)
- : brush_(std::move(brush)),
- text_format_(std::move(format)),
- selection_brush_(std::move(selection_brush)) {
+TextRenderObject::TextRenderObject(ID2D1Brush* brush, IDWriteTextFormat* format,
+ ID2D1Brush* selection_brush) {
+ assert(brush);
+ assert(format);
+ assert(selection_brush);
+ brush->AddRef();
+ format->AddRef();
+ selection_brush->AddRef();
+ this->brush_ = brush;
+ this->text_format_ = format;
+ this->selection_brush_ = selection_brush;
+ try {
+ RecreateTextLayout();
+ } catch (...) {
+ brush->Release();
+ format->Release();
+ selection_brush->Release();
+ throw;
+ }
+}
+
+TextRenderObject::~TextRenderObject() {
+ util::SafeRelease(brush_);
+ util::SafeRelease(text_format_);
+ util::SafeRelease(text_layout_);
+ util::SafeRelease(selection_brush_);
+}
+
+void TextRenderObject::SetBrush(ID2D1Brush* new_brush) {
+ assert(new_brush);
+ util::SafeRelease(brush_);
+ new_brush->AddRef();
+ brush_ = new_brush;
+}
+
+void TextRenderObject::SetTextFormat(IDWriteTextFormat* new_text_format) {
+ assert(new_text_format);
+ util::SafeRelease(text_format_);
+ new_text_format->AddRef();
+ text_format_ = new_text_format;
RecreateTextLayout();
}
+void TextRenderObject::SetSelectionBrush(ID2D1Brush* new_brush) {
+ assert(new_brush);
+ util::SafeRelease(selection_brush_);
+ new_brush->AddRef();
+ selection_brush_ = new_brush;
+}
+
namespace {
void DrawSelectionRect(ID2D1RenderTarget* render_target,
IDWriteTextLayout* layout, ID2D1Brush* brush,
@@ -54,9 +96,8 @@ void TextRenderObject::Draw(ID2D1RenderTarget* render_target) {
D2D1::Matrix3x2F::Translation(GetMargin().left + GetPadding().left,
GetMargin().top + GetPadding().top),
[this](auto rt) {
- DrawSelectionRect(rt, text_layout_.Get(), selection_brush_.Get(),
- selection_range_);
- rt->DrawTextLayout(D2D1::Point2F(), text_layout_.Get(), brush_.Get());
+ DrawSelectionRect(rt, text_layout_, selection_brush_, selection_range_);
+ rt->DrawTextLayout(D2D1::Point2F(), text_layout_, brush_);
});
}
@@ -99,7 +140,7 @@ void TextRenderObject::OnLayoutContent(const Rect& content_rect) {}
void TextRenderObject::RecreateTextLayout() {
assert(text_format_ != nullptr);
- text_layout_ = nullptr; // release last one
+ util::SafeRelease(text_layout_);
const auto dwrite_factory =
graph::GraphManager::GetInstance()->GetDWriteFactory();
@@ -107,7 +148,7 @@ void TextRenderObject::RecreateTextLayout() {
const auto&& size = GetSize();
ThrowIfFailed(dwrite_factory->CreateTextLayout(
- text_.c_str(), static_cast<UINT32>(text_.size()), text_format_.Get(),
+ text_.c_str(), static_cast<UINT32>(text_.size()), text_format_,
size.width, size.height, &text_layout_));
}
} // namespace cru::ui::render