From 0dcf8e686b93cca54a424affe0455d0a97d6c2ef Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 3 Mar 2020 23:36:45 +0800 Subject: ... --- include/cru/ui/controls/text_block.hpp | 23 +++++++++--- include/cru/ui/controls/text_box.hpp | 2 + include/cru/ui/controls/text_common.hpp | 66 +++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 include/cru/ui/controls/text_common.hpp (limited to 'include/cru/ui/controls') diff --git a/include/cru/ui/controls/text_block.hpp b/include/cru/ui/controls/text_block.hpp index 708b62f1..02e0eb2b 100644 --- a/include/cru/ui/controls/text_block.hpp +++ b/include/cru/ui/controls/text_block.hpp @@ -1,14 +1,18 @@ #pragma once #include "../no_child_control.hpp" +#include "text_common.hpp" + #include namespace cru::ui::render { +class StackLayoutRenderObject; class TextRenderObject; -} +class CanvasRenderObject; +} // namespace cru::ui::render namespace cru::ui::controls { -class TextBlock : public NoChildControl { +class TextBlock : public NoChildControl, public virtual ITextControl { public: static constexpr std::string_view control_type = "TextBlock"; @@ -24,16 +28,23 @@ class TextBlock : public NoChildControl { TextBlock& operator=(TextBlock&& other) = delete; ~TextBlock() override; - std::string_view GetControlType() const final { - return control_type; - } + std::string_view GetControlType() const final { return control_type; } render::RenderObject* GetRenderObject() const override; std::string GetText() const; void SetText(std::string text); + render::TextRenderObject* GetTextRenderObject() override; + render::CanvasRenderObject* GetCaretRenderObject() override; + platform::graph::IBrush* GetCaretBrush() override; + private: - std::unique_ptr render_object_; + std::unique_ptr root_render_object_; + std::unique_ptr text_render_object_; + std::unique_ptr caret_render_object_; + std::shared_ptr caret_brush_; + + std::unique_ptr service_; }; } // namespace cru::ui::controls diff --git a/include/cru/ui/controls/text_box.hpp b/include/cru/ui/controls/text_box.hpp index 76a6299e..888a9527 100644 --- a/include/cru/ui/controls/text_box.hpp +++ b/include/cru/ui/controls/text_box.hpp @@ -5,6 +5,7 @@ namespace cru::ui::render { class BorderRenderObject; +class StackLayoutRenderObject; class TextRenderObject; class CanvasRenderObject; } // namespace cru::ui::render @@ -27,6 +28,7 @@ class TextBox : public NoChildControl { private: std::unique_ptr border_render_object_; + std::unique_ptr stack_layout_render_object_; std::unique_ptr text_render_object_; std::unique_ptr caret_render_object_; }; diff --git a/include/cru/ui/controls/text_common.hpp b/include/cru/ui/controls/text_common.hpp new file mode 100644 index 00000000..7398764f --- /dev/null +++ b/include/cru/ui/controls/text_common.hpp @@ -0,0 +1,66 @@ +#pragma once +#include "../base.hpp" + +#include "cru/ui/ui_event.hpp" + +#include +#include + +namespace cru::platform::graph { +struct IBrush; +} + +namespace cru::ui::render { +class TextRenderObject; +class CanvasRenderObject; +} // namespace cru::ui::render + +namespace cru::ui::controls { + +struct ITextControl : virtual Interface { + virtual render::TextRenderObject* GetTextRenderObject() = 0; + virtual render::CanvasRenderObject* GetCaretRenderObject() = 0; + virtual platform::graph::IBrush* GetCaretBrush() = 0; +}; + +class TextControlService : public Object { + public: + TextControlService(Control* control, ITextControl* text_control); + + CRU_DELETE_COPY(TextControlService) + CRU_DELETE_MOVE(TextControlService) + + ~TextControlService(); + + public: + int GetCaretPosition() { return caret_position_; } + void SetCaretPosition(int position) { caret_position_ = position; } + + bool IsCaretVisible() { return caret_visible_; } + void SetCaretVisible(bool visible); + + // please set correct offset before calling this + void DrawCaret(platform::graph::IPainter* painter); + + private: + void SetupCaretTimer(); + void TearDownCaretTimer(); + + private: + Control* control_; + ITextControl* text_control_; + std::vector event_revoker_guards_; + + bool caret_visible_ = false; + int caret_position_ = 0; + unsigned long caret_timer_tag_; + // this is used for blinking of caret + bool caret_show_ = true; + + // nullopt means not selecting + std::optional select_down_button_; + + // before the char + int select_start_position_; +}; +} // namespace cru::ui::controls -- cgit v1.2.3