aboutsummaryrefslogtreecommitdiff
path: root/src/ui/render
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-04-24 00:03:16 +0800
committercrupest <crupest@outlook.com>2020-04-24 00:03:16 +0800
commit75ff8a6a05afd02aaadf7e3049b0a0e305241182 (patch)
tree5444bbb3ef80036cc38a827b8ccf03f48b310728 /src/ui/render
parent922d7f6c96f81a33538900f8a8992a5b6f640874 (diff)
downloadcru-75ff8a6a05afd02aaadf7e3049b0a0e305241182.tar.gz
cru-75ff8a6a05afd02aaadf7e3049b0a0e305241182.tar.bz2
cru-75ff8a6a05afd02aaadf7e3049b0a0e305241182.zip
...
Diffstat (limited to 'src/ui/render')
-rw-r--r--src/ui/render/text_render_object.cpp88
1 files changed, 83 insertions, 5 deletions
diff --git a/src/ui/render/text_render_object.cpp b/src/ui/render/text_render_object.cpp
index e8368480..cd670db1 100644
--- a/src/ui/render/text_render_object.cpp
+++ b/src/ui/render/text_render_object.cpp
@@ -7,22 +7,23 @@
#include <algorithm>
-// TODO: Null Check!!!
-
namespace cru::ui::render {
TextRenderObject::TextRenderObject(
std::shared_ptr<platform::graph::IBrush> brush,
std::shared_ptr<platform::graph::IFont> font,
- std::shared_ptr<platform::graph::IBrush> selection_brush) {
+ std::shared_ptr<platform::graph::IBrush> selection_brush,
+ std::shared_ptr<platform::graph::IBrush> caret_brush) {
Expects(brush);
Expects(font);
Expects(selection_brush);
+ Expects(caret_brush);
SetChildMode(ChildMode::None);
brush.swap(brush_);
font.swap(font_);
selection_brush.swap(selection_brush_);
+ caret_brush.swap(caret_brush_);
const auto graph_factory = GetGraphFactory();
text_layout_ = graph_factory->CreateTextLayout(font_, "");
@@ -38,11 +39,19 @@ void TextRenderObject::SetText(std::string new_text) {
text_layout_->SetText(std::move(new_text));
}
+void TextRenderObject::SetBrush(
+ std::shared_ptr<platform::graph::IBrush> new_brush) {
+ Expects(new_brush);
+ new_brush.swap(brush_);
+ InvalidatePaint();
+}
+
std::shared_ptr<platform::graph::IFont> TextRenderObject::GetFont() const {
return text_layout_->GetFont();
}
void TextRenderObject::SetFont(std::shared_ptr<platform::graph::IFont> font) {
+ Expects(font);
text_layout_->SetFont(std::move(font));
}
@@ -50,8 +59,8 @@ std::vector<Rect> TextRenderObject::TextRangeRect(const TextRange& text_range) {
return text_layout_->TextRangeRect(text_range);
}
-Point TextRenderObject::TextSingleRect(gsl::index position, bool trailing) {
- return text_layout_->TextSingleRect(position, trailing);
+Point TextRenderObject::TextSinglePoint(gsl::index position, bool trailing) {
+ return text_layout_->TextSinglePoint(position, trailing);
}
platform::graph::TextHitTestResult TextRenderObject::TextHitTest(
@@ -59,6 +68,54 @@ platform::graph::TextHitTestResult TextRenderObject::TextHitTest(
return text_layout_->HitTest(point);
}
+void TextRenderObject::SetSelectionRange(std::optional<TextRange> new_range) {
+ selection_range_ = std::move(new_range);
+ InvalidatePaint();
+}
+
+void TextRenderObject::SetSelectionBrush(
+ std::shared_ptr<platform::graph::IBrush> new_brush) {
+ Expects(new_brush);
+ new_brush.swap(selection_brush_);
+ if (selection_range_ && selection_range_->count) {
+ InvalidatePaint();
+ }
+}
+
+void TextRenderObject::SetDrawCaret(bool draw_caret) {
+ if (draw_caret_ != draw_caret) {
+ draw_caret_ = draw_caret;
+ InvalidatePaint();
+ }
+}
+
+void TextRenderObject::SetCaretPosition(gsl::index position) {
+ if (position != caret_position_) {
+ caret_position_ = position;
+ if (draw_caret_) {
+ InvalidatePaint();
+ }
+ }
+}
+
+void TextRenderObject::GetCaretBrush(
+ std::shared_ptr<platform::graph::IBrush> brush) {
+ Expects(brush);
+ brush.swap(caret_brush_);
+ if (draw_caret_) {
+ InvalidatePaint();
+ }
+}
+
+void TextRenderObject::SetCaretWidth(const float width) {
+ Expects(width >= 0.0f);
+
+ caret_width_ = width;
+ if (draw_caret_) {
+ InvalidatePaint();
+ }
+}
+
void TextRenderObject::Draw(platform::graph::IPainter* painter) {
platform::graph::util::WithTransform(
painter,
@@ -71,7 +128,28 @@ void TextRenderObject::Draw(platform::graph::IPainter* painter) {
for (const auto& rect : rects)
p->FillRectangle(rect, this->GetSelectionBrush().get());
}
+
p->DrawText(Point{}, text_layout_.get(), brush_.get());
+
+ if (this->draw_caret_ && this->caret_width_ != 0.0f) {
+ auto caret_pos = this->caret_position_;
+ gsl::index text_size = this->GetText().size();
+ if (caret_pos < 0) {
+ caret_pos = 0;
+ } else if (caret_pos > text_size) {
+ caret_pos = text_size;
+ }
+
+ const auto caret_top_center =
+ this->text_layout_->TextSinglePoint(caret_pos, false);
+
+ const auto font_height = this->font_->GetFontSize();
+ const auto caret_width = this->caret_width_;
+
+ p->FillRectangle(Rect{caret_top_center.x - caret_width / 2.0f,
+ caret_top_center.y, caret_width, font_height},
+ this->caret_brush_.get());
+ }
});
}