diff options
author | crupest <crupest@outlook.com> | 2019-04-10 19:42:46 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-04-10 19:42:46 +0800 |
commit | 7351020a582d70a1495249fba87d342c8a1fb634 (patch) | |
tree | e80f225041dc3816b3dce21c7e15aadbb211602e /src/ui | |
parent | a94a806f69586e08a30fff0cdb3e52b0ce7acfa5 (diff) | |
download | cru-7351020a582d70a1495249fba87d342c8a1fb634.tar.gz cru-7351020a582d70a1495249fba87d342c8a1fb634.tar.bz2 cru-7351020a582d70a1495249fba87d342c8a1fb634.zip |
Refactor.
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/CMakeLists.txt | 22 | ||||
-rw-r--r-- | src/ui/control.cpp | 4 | ||||
-rw-r--r-- | src/ui/render/border_render_object.cpp | 32 | ||||
-rw-r--r-- | src/ui/render/flex_layout_render_object.cpp | 11 | ||||
-rw-r--r-- | src/ui/render/render_object.cpp | 8 | ||||
-rw-r--r-- | src/ui/render/text_render_object.cpp | 28 | ||||
-rw-r--r-- | src/ui/render/window_render_object.cpp | 8 | ||||
-rw-r--r-- | src/ui/ui_manager.cpp | 17 | ||||
-rw-r--r-- | src/ui/window.cpp | 16 |
9 files changed, 83 insertions, 63 deletions
diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt new file mode 100644 index 00000000..900948ca --- /dev/null +++ b/src/ui/CMakeLists.txt @@ -0,0 +1,22 @@ +add_library(cru_ui STATIC + content_control.cpp + control.cpp + layout_control.cpp + no_child_control.cpp + ui_manager.cpp + window.cpp + controls/button.cpp + controls/flex_layout.cpp + controls/text_block.cpp + render/border_render_object.cpp + render/flex_layout_render_object.cpp + render/render_object.cpp + render/text_render_object.cpp + render/window_render_object.cpp) + +target_include_directories(cru_ui PUBLIC ${PROJECT_SOURCE_DIR}/include .) + +if(WIN32) +target_link_libraries(cru_ui PUBLIC cru_win_native) +endif() + diff --git a/src/ui/control.cpp b/src/ui/control.cpp index f915af07..7563dc91 100644 --- a/src/ui/control.cpp +++ b/src/ui/control.cpp @@ -63,7 +63,7 @@ void Control::OnAttachToWindow(Window* window) {} void Control::OnDetachToWindow(Window* window) {} -void Control::OnMouseClickBegin(platform::MouseButton button) {} +void Control::OnMouseClickBegin(platform::native::MouseButton button) {} -void Control::OnMouseClickEnd(platform::MouseButton button) {} +void Control::OnMouseClickEnd(platform::native::MouseButton button) {} } // namespace cru::ui diff --git a/src/ui/render/border_render_object.cpp b/src/ui/render/border_render_object.cpp index 97e386dd..541be473 100644 --- a/src/ui/render/border_render_object.cpp +++ b/src/ui/render/border_render_object.cpp @@ -1,26 +1,26 @@ - #include "cru/ui/render/border_render_object.hpp" +#include "cru/ui/render/border_render_object.hpp" #include "cru/platform/debug.hpp" -#include "cru/platform/geometry.hpp" -#include "cru/platform/graph_factory.hpp" -#include "cru/platform/painter_util.hpp" -#include "cru/platform/ui_applicaition.hpp" +#include "cru/platform/graph/geometry.hpp" +#include "cru/platform/graph/graph_factory.hpp" +#include "cru/platform/graph/painter_util.hpp" #include <algorithm> #include <cassert> namespace cru::ui::render { -BorderRenderObject::BorderRenderObject(std::shared_ptr<platform::Brush> brush) { +BorderRenderObject::BorderRenderObject( + std::shared_ptr<platform::graph::Brush> brush) { assert(brush); this->border_brush_ = std::move(brush); RecreateGeometry(); } -void BorderRenderObject::Draw(platform::Painter* painter) { +void BorderRenderObject::Draw(platform::graph::Painter* painter) { painter->FillGeometry(geometry_.get(), border_brush_.get()); if (const auto child = GetChild()) { auto offset = child->GetOffset(); - platform::util::WithTransform( + platform::graph::util::WithTransform( painter, platform::Matrix::Translation(offset.x, offset.y), [child](auto p) { child->Draw(p); }); } @@ -75,13 +75,13 @@ void BorderRenderObject::OnMeasureCore(const Size& available_size) { auto coerced_margin_border_padding_size = margin_border_padding_size; if (coerced_margin_border_padding_size.width > available_size.width) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Measure: horizontal length of padding, border and margin is bigger " L"than available length."); coerced_margin_border_padding_size.width = available_size.width; } if (coerced_margin_border_padding_size.height > available_size.height) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Measure: vertical length of padding, border and margin is bigger " L"than available length."); coerced_margin_border_padding_size.height = available_size.height; @@ -113,13 +113,13 @@ void BorderRenderObject::OnLayoutCore(const Rect& rect) { auto coerced_content_available_size = content_available_size; if (coerced_content_available_size.width < 0) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Layout: horizontal length of padding, border and margin is bigger " L"than available length."); coerced_content_available_size.width = 0; } if (coerced_content_available_size.height < 0) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Layout: vertical length of padding, border and margin is bigger " L"than " L"available length."); @@ -154,7 +154,7 @@ void BorderRenderObject::RecreateGeometry() { geometry_.reset(); border_outer_geometry_.reset(); - auto f = [](platform::GeometryBuilder* builder, const Rect& rect, + auto f = [](platform::graph::GeometryBuilder* builder, const Rect& rect, const CornerRadius& corner) { builder->BeginFigure(Point(rect.left + corner.left_top.x, rect.top)); builder->LineTo(Point(rect.GetRight() - corner.right_top.x, rect.top)); @@ -181,9 +181,9 @@ void BorderRenderObject::RecreateGeometry() { const Rect outer_rect{margin.left, margin.top, size.width - margin.GetHorizontalTotal(), size.height - margin.GetVerticalTotal()}; - const auto graph_factory = - platform::UiApplication::GetInstance()->GetGraphFactory(); - std::unique_ptr<platform::GeometryBuilder> builder{graph_factory->CreateGeometryBuilder()}; + const auto graph_factory = platform::graph::GraphFactory::GetInstance(); + std::unique_ptr<platform::graph::GeometryBuilder> builder{ + graph_factory->CreateGeometryBuilder()}; f(builder.get(), outer_rect, corner_radius_); border_outer_geometry_.reset(builder->Build()); builder.reset(); diff --git a/src/ui/render/flex_layout_render_object.cpp b/src/ui/render/flex_layout_render_object.cpp index e7840b7e..7528439f 100644 --- a/src/ui/render/flex_layout_render_object.cpp +++ b/src/ui/render/flex_layout_render_object.cpp @@ -1,15 +1,13 @@ #include "cru/ui/render/flex_layout_render_object.hpp" #include "cru/platform/debug.hpp" -#include "cru/platform/painter_util.hpp" +#include "cru/platform/graph/painter_util.hpp" #include <algorithm> #include <cassert> #include <functional> namespace cru::ui::render { -using namespace platform; - FlexChildLayoutData* FlexLayoutRenderObject::GetChildLayoutData(int position) { assert(position >= 0 && position < child_layout_data_.size()); // Position out of bound. @@ -17,11 +15,12 @@ FlexChildLayoutData* FlexLayoutRenderObject::GetChildLayoutData(int position) { return &child_layout_data_[position]; } -void FlexLayoutRenderObject::Draw(platform::Painter* painter) { +void FlexLayoutRenderObject::Draw(platform::graph::Painter* painter) { for (const auto child : GetChildren()) { auto offset = child->GetOffset(); - util::WithTransform(painter, Matrix::Translation(offset.x, offset.y), - [child](auto p) { child->Draw(p); }); + platform::graph::util::WithTransform( + painter, platform::Matrix::Translation(offset.x, offset.y), + [child](auto p) { child->Draw(p); }); } } diff --git a/src/ui/render/render_object.cpp b/src/ui/render/render_object.cpp index 111128b2..3cb7e4df 100644 --- a/src/ui/render/render_object.cpp +++ b/src/ui/render/render_object.cpp @@ -54,13 +54,13 @@ void RenderObject::OnMeasureCore(const Size& available_size) { auto coerced_margin_padding_size = margin_padding_size; if (coerced_margin_padding_size.width > available_size.width) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Measure: horizontal length of padding and margin is bigger than " L"available length."); coerced_margin_padding_size.width = available_size.width; } if (coerced_margin_padding_size.height > available_size.height) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Measure: vertical length of padding and margin is bigger than " L"available length."); coerced_margin_padding_size.height = available_size.height; @@ -82,13 +82,13 @@ void RenderObject::OnLayoutCore(const Rect& rect) { auto coerced_content_available_size = content_available_size; if (coerced_content_available_size.width < 0) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Layout: horizontal length of padding and margin is bigger than " L"available length."); coerced_content_available_size.width = 0; } if (coerced_content_available_size.height < 0) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Layout: vertical length of padding and margin is bigger than " L"available length."); coerced_content_available_size.height = 0; diff --git a/src/ui/render/text_render_object.cpp b/src/ui/render/text_render_object.cpp index bdf48c9a..c886ee7a 100644 --- a/src/ui/render/text_render_object.cpp +++ b/src/ui/render/text_render_object.cpp @@ -1,18 +1,17 @@ #include "cru/ui/render/text_render_object.hpp" -#include "cru/platform/graph_factory.hpp" -#include "cru/platform/painter_util.hpp" -#include "cru/platform/text_layout.hpp" -#include "cru/platform/ui_applicaition.hpp" +#include "cru/platform/graph/graph_factory.hpp" +#include "cru/platform/graph/painter_util.hpp" +#include "cru/platform/graph/text_layout.hpp" #include <algorithm> #include <cassert> namespace cru::ui::render { TextRenderObject::TextRenderObject( - std::shared_ptr<platform::Brush> brush, - std::shared_ptr<platform::FontDescriptor> font, - std::shared_ptr<platform::Brush> selection_brush) { + std::shared_ptr<platform::graph::Brush> brush, + std::shared_ptr<platform::graph::FontDescriptor> font, + std::shared_ptr<platform::graph::Brush> selection_brush) { assert(brush); assert(font); assert(selection_brush); @@ -21,8 +20,7 @@ TextRenderObject::TextRenderObject( font.swap(font_); selection_brush.swap(selection_brush_); - const auto graph_factory = - platform::UiApplication::GetInstance()->GetGraphFactory(); + const auto graph_factory = platform::graph::GraphFactory::GetInstance(); text_layout_.reset(graph_factory->CreateTextLayout(font_, L"")); } @@ -35,20 +33,22 @@ void TextRenderObject::SetText(std::wstring new_text) { text_layout_->SetText(std::move(new_text)); } -std::shared_ptr<platform::FontDescriptor> TextRenderObject::GetFont() const { +std::shared_ptr<platform::graph::FontDescriptor> TextRenderObject::GetFont() + const { return text_layout_->GetFont(); } -void TextRenderObject::SetFont(std::shared_ptr<platform::FontDescriptor> font) { +void TextRenderObject::SetFont( + std::shared_ptr<platform::graph::FontDescriptor> font) { text_layout_->SetFont(std::move(font)); } -void TextRenderObject::Draw(platform::Painter* painter) { - platform::util::WithTransform( +void TextRenderObject::Draw(platform::graph::Painter* painter) { + platform::graph::util::WithTransform( painter, platform::Matrix::Translation(GetMargin().left + GetPadding().left, GetMargin().top + GetPadding().top), - [this](platform::Painter* p) { + [this](platform::graph::Painter* p) { if (this->selection_range_.has_value()) { const auto&& rects = text_layout_->TextRangeRect(this->selection_range_.value()); diff --git a/src/ui/render/window_render_object.cpp b/src/ui/render/window_render_object.cpp index 781087aa..4fc57ad1 100644 --- a/src/ui/render/window_render_object.cpp +++ b/src/ui/render/window_render_object.cpp @@ -1,7 +1,7 @@ #include "cru/ui/render/window_render_object.hpp" -#include "cru/platform/native_window.hpp" -#include "cru/platform/painter_util.hpp" +#include "cru/platform/graph/painter_util.hpp" +#include "cru/platform/native/native_window.hpp" #include "cru/ui/window.hpp" #include <cassert> @@ -13,11 +13,11 @@ void WindowRenderObject::MeasureAndLayout() { Layout(Rect{Point{}, client_size}); } -void WindowRenderObject::Draw(platform::Painter* painter) { +void WindowRenderObject::Draw(platform::graph::Painter* painter) { painter->Clear(colors::white); if (const auto child = GetChild()) { auto offset = child->GetOffset(); - platform::util::WithTransform( + platform::graph::util::WithTransform( painter, platform::Matrix::Translation(offset.x, offset.y), [child](auto rt) { child->Draw(rt); }); } diff --git a/src/ui/ui_manager.cpp b/src/ui/ui_manager.cpp index b1132ea8..2ac1c8e7 100644 --- a/src/ui/ui_manager.cpp +++ b/src/ui/ui_manager.cpp @@ -1,20 +1,19 @@ #include "cru/ui/ui_manager.hpp" -#include "cru/platform/brush.hpp" -#include "cru/platform/font.hpp" -#include "cru/platform/graph_factory.hpp" -#include "cru/platform/ui_applicaition.hpp" +#include "cru/platform/graph/brush.hpp" +#include "cru/platform/graph/font.hpp" +#include "cru/platform/graph/graph_factory.hpp" +#include "cru/platform/native/ui_applicaition.hpp" namespace cru::ui { PredefineResources::PredefineResources() { - const auto graph_factory = - platform::UiApplication::GetInstance()->GetGraphFactory(); + const auto graph_factory = platform::graph::GraphFactory::GetInstance(); - button_normal_border_brush.reset(static_cast<platform::Brush*>( + button_normal_border_brush.reset(static_cast<platform::graph::Brush*>( graph_factory->CreateSolidColorBrush(colors::black))); - text_block_selection_brush.reset(static_cast<platform::Brush*>( + text_block_selection_brush.reset(static_cast<platform::graph::Brush*>( graph_factory->CreateSolidColorBrush(colors::skyblue))); - text_block_text_brush.reset(static_cast<platform::Brush*>( + text_block_text_brush.reset(static_cast<platform::graph::Brush*>( graph_factory->CreateSolidColorBrush(colors::black))); text_block_font.reset(graph_factory->CreateFontDescriptor(L"等线", 24.0f)); } diff --git a/src/ui/window.cpp b/src/ui/window.cpp index f909e0f9..347a7a49 100644 --- a/src/ui/window.cpp +++ b/src/ui/window.cpp @@ -1,8 +1,8 @@ #include "cru/ui/window.hpp" -#include "cru/platform/native_window.hpp" -#include "cru/platform/painter.hpp" -#include "cru/platform/ui_applicaition.hpp" +#include "cru/platform/graph/painter.hpp" +#include "cru/platform/native/native_window.hpp" +#include "cru/platform/native/ui_applicaition.hpp" #include "cru/ui/render/window_render_object.hpp" #include <cassert> @@ -102,7 +102,7 @@ Window::Window(tag_overlapped_constructor) { using namespace std::placeholders; native_window_ = - platform::UiApplication::GetInstance()->CreateWindow(nullptr); + platform::native::UiApplication::GetInstance()->CreateWindow(nullptr); render_object_.reset(new render::WindowRenderObject(this)); event_revoker_guard_.Add(native_window_->DestroyEvent()->AddHandler( @@ -124,7 +124,7 @@ Window::Window(tag_overlapped_constructor) { event_revoker_guard_.Add(native_window_->KeyDownEvent()->AddHandler( std::bind(&Window::OnNativeKeyDown, this, _1))); event_revoker_guard_.Add(native_window_->KeyUpEvent()->AddHandler( - std::bind(&Window::OnNativeKeyUp, this, _1))); + std::bind(&Window::OnNativeKeyUp, this, _1))); } Window::~Window() { @@ -168,7 +168,7 @@ void Window::OnNativeDestroy() { delete this; } void Window::OnNativePaint() { const auto painter = - std::unique_ptr<platform::Painter>(native_window_->BeginPaint()); + std::unique_ptr<platform::graph::Painter>(native_window_->BeginPaint()); render_object_->Draw(painter.get()); painter->EndDraw(); } @@ -202,13 +202,13 @@ void Window::OnNativeMouseMove(const Point& point) { point); } -void Window::OnNativeMouseDown(platform::MouseButton button, +void Window::OnNativeMouseDown(platform::native::MouseButton button, const Point& point) { Control* control = HitTest(point); DispatchEvent(control, &Control::MouseDownEvent, nullptr, point, button); } -void Window::OnNativeMouseUp(platform::MouseButton button, const Point& point) { +void Window::OnNativeMouseUp(platform::native::MouseButton button, const Point& point) { Control* control = HitTest(point); DispatchEvent(control, &Control::MouseUpEvent, nullptr, point, button); } |