From 6268f3ca09afef812cf288f323cacff22fbfcffc Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 4 Apr 2019 23:08:22 +0800 Subject: Finish refactor!!! --- include/cru/common/event.hpp | 11 ++++---- include/cru/common/format.hpp | 4 +-- include/cru/platform/basic_types.hpp | 1 + include/cru/platform/matrix.hpp | 7 +++-- include/cru/platform/painter.hpp | 2 +- include/cru/platform/win/win_text_layout.hpp | 2 +- include/cru/ui/controls/button.hpp | 39 ++++++++++++++++++++++++++ include/cru/ui/controls/flex_layout.hpp | 41 ++++++++++++++++++++++++++++ include/cru/ui/controls/text_block.hpp | 39 ++++++++++++++++++++++++++ include/cru/ui/render/text_render_object.hpp | 2 +- include/cru/ui/window.hpp | 2 +- src/CMakeLists.txt | 3 +- src/main.cpp | 29 ++++++++++---------- src/platform_win/win_application.cpp | 2 +- src/platform_win/win_native_window.cpp | 6 +++- src/platform_win/win_painter.cpp | 1 + src/platform_win/win_text_layout.cpp | 15 ++++++---- src/platform_win/window_manager.cpp | 9 +++--- src/ui/content_control.cpp | 2 +- src/ui/control.cpp | 2 +- src/ui/controls/button.cpp | 10 ++++--- src/ui/controls/button.hpp | 39 -------------------------- src/ui/controls/flex_layout.cpp | 4 +-- src/ui/controls/flex_layout.hpp | 41 ---------------------------- src/ui/controls/text_block.cpp | 14 ++++++---- src/ui/controls/text_block.hpp | 39 -------------------------- src/ui/layout_control.cpp | 2 +- src/ui/render/border_render_object.cpp | 2 +- src/ui/render/flex_layout_render_object.cpp | 4 +-- src/ui/render/window_render_object.cpp | 11 +++++--- src/ui/ui_manager.cpp | 15 +++++----- src/ui/window.cpp | 23 ++++++++++++---- 32 files changed, 227 insertions(+), 196 deletions(-) create mode 100644 include/cru/ui/controls/button.hpp create mode 100644 include/cru/ui/controls/flex_layout.hpp create mode 100644 include/cru/ui/controls/text_block.hpp delete mode 100644 src/ui/controls/button.hpp delete mode 100644 src/ui/controls/flex_layout.hpp delete mode 100644 src/ui/controls/text_block.hpp diff --git a/include/cru/common/event.hpp b/include/cru/common/event.hpp index 763f8378..6228d867 100644 --- a/include/cru/common/event.hpp +++ b/include/cru/common/event.hpp @@ -22,7 +22,7 @@ class Event { : resolver_(resolver), token_(token) {} EventHandlerRevokerImpl(const EventHandlerRevokerImpl& other) = default; EventHandlerRevokerImpl(EventHandlerRevokerImpl&& other) = default; - EventHandlerRevokerImpl& operator=(EventHandlerRevokerImpl&& other) = + EventHandlerRevokerImpl& operator=(const EventHandlerRevokerImpl& other) = default; EventHandlerRevokerImpl& operator=(EventHandlerRevokerImpl&& other) = default; @@ -31,7 +31,7 @@ class Event { void operator()() { const auto true_resolver = resolver_.lock(); if (true_resolver) { - true_resolver()->RemoveHandler(token_); + (*true_resolver)()->RemoveHandler(token_); } } @@ -63,10 +63,11 @@ class Event { return EventHandlerRevoker(EventHandlerRevokerImpl(event_resolver_, token)); } - template - EventHandlerRevoker AddHandler(Args&& args...) { + template + EventHandlerRevoker AddHandler(Arg&& handler) { + static_assert(std::is_invocable_v, "Handler not invocable."); const auto token = current_token_++; - handlers_.emplace(token, EventHandler(std::forward(args)...)); + handlers_.emplace(token, EventHandler(std::forward(handler))); return EventHandlerRevoker(EventHandlerRevokerImpl(event_resolver_, token)); } diff --git a/include/cru/common/format.hpp b/include/cru/common/format.hpp index 1fb6863a..f085635a 100644 --- a/include/cru/common/format.hpp +++ b/include/cru/common/format.hpp @@ -83,7 +83,7 @@ inline std::string_view FormatToString(const std::string& string, } inline std::wstring_view FormatToString(const std::wstring_view& string, - details::TypeTag) { + details::TypeTag) { return string; } @@ -93,7 +93,7 @@ inline std::string_view FormatToString(const std::string_view& string, } inline std::wstring_view FormatToString(const wchar_t* string, - details::TypeTag) { + details::TypeTag) { return std::wstring_view(string); } diff --git a/include/cru/platform/basic_types.hpp b/include/cru/platform/basic_types.hpp index 81ee3e34..e6ab4b59 100644 --- a/include/cru/platform/basic_types.hpp +++ b/include/cru/platform/basic_types.hpp @@ -1,3 +1,4 @@ +#pragma once #include "cru/common/pre_config.hpp" namespace cru::platform { diff --git a/include/cru/platform/matrix.hpp b/include/cru/platform/matrix.hpp index e5e5cf42..37e4725e 100644 --- a/include/cru/platform/matrix.hpp +++ b/include/cru/platform/matrix.hpp @@ -1,3 +1,4 @@ +#pragma once #include "cru/common/pre_config.hpp" #include "cru/common/ui_base.hpp" @@ -29,12 +30,14 @@ struct Matrix { m31 == 0.0f && m32 == 0.0f; } - Matrix& operator*=(const Matrix& matrix) const { + Matrix& operator*=(const Matrix& matrix) { *this = Product(*this, matrix); return *this; } - Matrix operator*(const Matrix& matrix) const { return Product(this, matrix); } + Matrix operator*(const Matrix& matrix) const { + return Product(*this, matrix); + } ui::Point TransformPoint(const ui::Point& point) const { return ui::Point{point.x * m11 + point.y * m21 + m31, diff --git a/include/cru/platform/painter.hpp b/include/cru/platform/painter.hpp index eaaf61f9..b7b89fc9 100644 --- a/include/cru/platform/painter.hpp +++ b/include/cru/platform/painter.hpp @@ -19,7 +19,7 @@ struct Painter : virtual Interface { virtual void StrokeGeometry(Geometry* geometry, Brush* brush, float width) = 0; virtual void FillGeometry(Geometry* geometry, Brush* brush) = 0; - virtual void DrawText(const ui::Point& offset, TextLayout* text_layout, Brush* brush); + virtual void DrawText(const ui::Point& offset, TextLayout* text_layout, Brush* brush) = 0; virtual void EndDraw() = 0; virtual bool IsDisposed() = 0; }; diff --git a/include/cru/platform/win/win_text_layout.hpp b/include/cru/platform/win/win_text_layout.hpp index 9c93aa8c..dfb54264 100644 --- a/include/cru/platform/win/win_text_layout.hpp +++ b/include/cru/platform/win/win_text_layout.hpp @@ -17,7 +17,7 @@ class WinTextLayout : public Object, public virtual TextLayout { WinTextLayout(WinTextLayout&& other) = delete; WinTextLayout& operator=(const WinTextLayout& other) = delete; WinTextLayout& operator=(WinTextLayout&& other) = delete; - ~WinTextLayout() override; + ~WinTextLayout() override = default; std::wstring GetText() override; void SetText(std::wstring new_text) override; diff --git a/include/cru/ui/controls/button.hpp b/include/cru/ui/controls/button.hpp new file mode 100644 index 00000000..348416c5 --- /dev/null +++ b/include/cru/ui/controls/button.hpp @@ -0,0 +1,39 @@ +#pragma once +#include "../content_control.hpp" + +#include + +namespace cru::ui::render { +class BorderRenderObject; +} + +namespace cru::ui::controls { +class Button : public ContentControl { + public: + static constexpr auto control_type = L"Button"; + + static Button* Create() { return new Button(); } + + protected: + Button(); + + public: + Button(const Button& other) = delete; + Button(Button&& other) = delete; + Button& operator=(const Button& other) = delete; + Button& operator=(Button&& other) = delete; + ~Button() override = default; + + std::wstring_view GetControlType() const override final { + return control_type; + } + + render::RenderObject* GetRenderObject() const override; + + protected: + void OnChildChanged(Control* old_child, Control* new_child) override; + + private: + std::shared_ptr render_object_{}; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/flex_layout.hpp b/include/cru/ui/controls/flex_layout.hpp new file mode 100644 index 00000000..7422bc05 --- /dev/null +++ b/include/cru/ui/controls/flex_layout.hpp @@ -0,0 +1,41 @@ +#pragma once +#include "../layout_control.hpp" + +#include + +namespace cru::ui::render { +class FlexLayoutRenderObject; +} + +namespace cru::ui::controls { + +class FlexLayout : public LayoutControl { + public: + static constexpr auto control_type = L"FlexLayout"; + + static FlexLayout* Create() { return new FlexLayout(); } + + protected: + FlexLayout(); + + public: + FlexLayout(const FlexLayout& other) = delete; + FlexLayout(FlexLayout&& other) = delete; + FlexLayout& operator=(const FlexLayout& other) = delete; + FlexLayout& operator=(FlexLayout&& other) = delete; + ~FlexLayout() override = default; + + std::wstring_view GetControlType() const override final { + return control_type; + } + + render::RenderObject* GetRenderObject() const override; + + protected: + void OnAddChild(Control* child, int position) override; + void OnRemoveChild(Control* child, int position) override; + + private: + std::shared_ptr render_object_; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/controls/text_block.hpp b/include/cru/ui/controls/text_block.hpp new file mode 100644 index 00000000..45cd12b9 --- /dev/null +++ b/include/cru/ui/controls/text_block.hpp @@ -0,0 +1,39 @@ +#pragma once +#include "../no_child_control.hpp" + +#include + +namespace cru::ui::render { +class TextRenderObject; +} + +namespace cru::ui::controls { +class TextBlock : public NoChildControl { + public: + static constexpr auto control_type = L"TextBlock"; + + static TextBlock* Create() { return new TextBlock(); } + + protected: + TextBlock(); + + public: + TextBlock(const TextBlock& other) = delete; + TextBlock(TextBlock&& other) = delete; + TextBlock& operator=(const TextBlock& other) = delete; + TextBlock& operator=(TextBlock&& other) = delete; + ~TextBlock() override = default; + + std::wstring_view GetControlType() const override final { + return control_type; + } + + render::RenderObject* GetRenderObject() const override; + + std::wstring GetText() const; + void SetText(std::wstring text); + + private: + std::shared_ptr render_object_; +}; +} // namespace cru::ui::controls diff --git a/include/cru/ui/render/text_render_object.hpp b/include/cru/ui/render/text_render_object.hpp index 329af18a..527fcd71 100644 --- a/include/cru/ui/render/text_render_object.hpp +++ b/include/cru/ui/render/text_render_object.hpp @@ -21,7 +21,7 @@ class TextRenderObject : public RenderObject { TextRenderObject(TextRenderObject&& other) = delete; TextRenderObject& operator=(const TextRenderObject& other) = delete; TextRenderObject& operator=(TextRenderObject&& other) = delete; - ~TextRenderObject() override; + ~TextRenderObject() override = default; std::wstring GetText() const; void SetText(std::wstring new_text); diff --git a/include/cru/ui/window.hpp b/include/cru/ui/window.hpp index 94deb4c5..991ebf70 100644 --- a/include/cru/ui/window.hpp +++ b/include/cru/ui/window.hpp @@ -38,7 +38,7 @@ class Window final : public ContentControl { render::RenderObject* GetRenderObject() const override; - platform::NativeWindow* GetNativeWindow() const; + platform::NativeWindow* GetNativeWindow() const { return native_window_; } Control* GetMouseHoverControl() const { return mouse_hover_control_; } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4155e800..8f29ddc9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -22,7 +22,6 @@ target_link_libraries(cru_ui PUBLIC cru_platform_win) endif() - -add_executable(demo WIN32 main.cpp) +add_executable(demo main.cpp) target_link_libraries(demo PRIVATE cru_ui) diff --git a/src/main.cpp b/src/main.cpp index c35213f7..c0c90002 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,28 +1,27 @@ -#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" - -using cru::Application; -using cru::String; -using cru::StringView; +#include "cru/platform/native_window.hpp" +#include "cru/platform/ui_applicaition.hpp" +#include "cru/ui/controls/button.hpp" +#include "cru/ui/controls/flex_layout.hpp" +#include "cru/ui/controls/text_block.hpp" +#include "cru/ui/window.hpp" + +using cru::platform::UiApplication; 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; - +/* int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { #ifdef CRU_DEBUG _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); #endif +*/ - Application application(hInstance); +int main() { + auto application = UiApplication::GetInstance(); const auto window = Window::CreateOverlapped(); @@ -40,7 +39,7 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, text_block2->SetText(L"Hello World!"); flex_layout->AddChild(text_block2, 1); - window->Show(); + window->GetNativeWindow()->SetVisible(true); - return application.Run(); + return application->Run(); } diff --git a/src/platform_win/win_application.cpp b/src/platform_win/win_application.cpp index 31d8c990..ce187136 100644 --- a/src/platform_win/win_application.cpp +++ b/src/platform_win/win_application.cpp @@ -40,7 +40,7 @@ WinApplication::WinApplication(HINSTANCE h_instance) : h_instance_(h_instance) { timer_manager_ = std::make_shared(god_window_.get()); window_manager_ = std::make_shared(this); graph_manager_ = std::make_shared(); - graph_factory_ = std::make_shared(graph_manager_.get()); + graph_factory_ = std::make_shared(graph_manager_.get()); } WinApplication::~WinApplication() { instance_ = nullptr; } diff --git a/src/platform_win/win_native_window.cpp b/src/platform_win/win_native_window.cpp index 12c25674..3f34717f 100644 --- a/src/platform_win/win_native_window.cpp +++ b/src/platform_win/win_native_window.cpp @@ -34,6 +34,9 @@ WinNativeWindow::WinNativeWindow(WinApplication* application, throw Win32Error(::GetLastError(), "Failed to create window."); window_manager->RegisterWindow(hwnd_, this); + + window_render_target_.reset( + new WindowRenderTarget(application->GetGraphManager(), hwnd_)); } WinNativeWindow::~WinNativeWindow() { @@ -113,7 +116,8 @@ void WinNativeWindow::SetWindowRect(const ui::Rect& rect) { } } -Painter* WinNativeWindow::BeginPaint() { return new WinPainter(this); } +Painter* WinNativeWindow::BeginPaint() { + return new WinPainter(this); } bool WinNativeWindow::HandleNativeWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param, diff --git a/src/platform_win/win_painter.cpp b/src/platform_win/win_painter.cpp index 6ce5f4de..7c99146a 100644 --- a/src/platform_win/win_painter.cpp +++ b/src/platform_win/win_painter.cpp @@ -97,6 +97,7 @@ void WinPainter::EndDraw() { if (!IsDisposed()) { ThrowIfFailed(render_target_->EndDraw()); render_target_ = nullptr; + window_->GetWindowRenderTarget()->Present(); is_disposed = true; } } diff --git a/src/platform_win/win_text_layout.cpp b/src/platform_win/win_text_layout.cpp index 7ae87a80..04b5e928 100644 --- a/src/platform_win/win_text_layout.cpp +++ b/src/platform_win/win_text_layout.cpp @@ -17,8 +17,9 @@ WinTextLayout::WinTextLayout(GraphManager* graph_manager, font_descriptor_.swap(font); ThrowIfFailed(graph_manager_->GetDWriteFactory()->CreateTextLayout( - text_.c_str(), text_.size(), font_descriptor_->GetDWriteTextFormat(), - max_width_, max_height_, &text_layout_)); + text_.c_str(), static_cast(text_.size()), + font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_, + &text_layout_)); } std::wstring WinTextLayout::GetText() { return text_; } @@ -26,8 +27,9 @@ std::wstring WinTextLayout::GetText() { return text_; } void WinTextLayout::SetText(std::wstring new_text) { text_.swap(new_text); ThrowIfFailed(graph_manager_->GetDWriteFactory()->CreateTextLayout( - text_.c_str(), text_.size(), font_descriptor_->GetDWriteTextFormat(), - max_width_, max_height_, &text_layout_)); + text_.c_str(), static_cast(text_.size()), + font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_, + &text_layout_)); } std::shared_ptr WinTextLayout::GetFont() { @@ -39,8 +41,9 @@ void WinTextLayout::SetFont(std::shared_ptr font) { assert(f); f.swap(font_descriptor_); ThrowIfFailed(graph_manager_->GetDWriteFactory()->CreateTextLayout( - text_.c_str(), text_.size(), font_descriptor_->GetDWriteTextFormat(), - max_width_, max_height_, &text_layout_)); + text_.c_str(), static_cast(text_.size()), + font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_, + &text_layout_)); } void WinTextLayout::SetMaxWidth(float max_width) { diff --git a/src/platform_win/window_manager.cpp b/src/platform_win/window_manager.cpp index 62a73499..e6c255c3 100644 --- a/src/platform_win/window_manager.cpp +++ b/src/platform_win/window_manager.cpp @@ -2,13 +2,15 @@ #include "cru/platform/win/win_application.hpp" #include "cru/platform/win/win_native_window.hpp" +#include "cru/platform/win/window_class.hpp" #include namespace cru::platform::win { LRESULT __stdcall GeneralWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { - auto window = WinApplication::GetInstance()->GetWindowManager()->FromHandle(hWnd); + auto window = + WinApplication::GetInstance()->GetWindowManager()->FromHandle(hWnd); LRESULT result; if (window != nullptr && @@ -21,8 +23,7 @@ LRESULT __stdcall GeneralWndProc(HWND hWnd, UINT Msg, WPARAM wParam, WindowManager::WindowManager(WinApplication* application) { application_ = application; general_window_class_ = std::make_shared( - L"CruUIWindowClass", GeneralWndProc, - application->GetInstanceHandle()); + L"CruUIWindowClass", GeneralWndProc, application->GetInstanceHandle()); } void WindowManager::RegisterWindow(HWND hwnd, WinNativeWindow* window) { @@ -50,4 +51,4 @@ std::vector WindowManager::GetAllWindows() const { for (auto [key, value] : window_map_) windows.push_back(value); return windows; } -} +} // namespace cru::platform::win diff --git a/src/ui/content_control.cpp b/src/ui/content_control.cpp index 3a23164c..6383a10e 100644 --- a/src/ui/content_control.cpp +++ b/src/ui/content_control.cpp @@ -1,6 +1,6 @@ #include "cru/ui/content_control.hpp" -#include "window.hpp" +#include "cru/ui/window.hpp" #include diff --git a/src/ui/control.cpp b/src/ui/control.cpp index c5d02d40..f915af07 100644 --- a/src/ui/control.cpp +++ b/src/ui/control.cpp @@ -1,6 +1,6 @@ #include "cru/ui/control.hpp" -#include "window.hpp" +#include "cru/ui/window.hpp" #include diff --git a/src/ui/controls/button.cpp b/src/ui/controls/button.cpp index a6578251..0ac65d26 100644 --- a/src/ui/controls/button.cpp +++ b/src/ui/controls/button.cpp @@ -1,7 +1,7 @@ -#include "button.hpp" +#include "cru/ui/controls/button.hpp" -#include "ui/render/border_render_object.hpp" -#include "ui/ui_manager.hpp" +#include "cru/ui/render/border_render_object.hpp" +#include "cru/ui/ui_manager.hpp" namespace cru::ui::controls { Button::Button() { @@ -14,7 +14,9 @@ Button::Button() { render_object_->SetCornerRadius(render::CornerRadius{Point{10, 5}}); } -render::RenderObject* Button::GetRenderObject() const { return render_object_.get(); } +render::RenderObject* Button::GetRenderObject() const { + return render_object_.get(); +} void Button::OnChildChanged(Control* old_child, Control* new_child) { if (old_child != nullptr) render_object_->RemoveChild(0); diff --git a/src/ui/controls/button.hpp b/src/ui/controls/button.hpp deleted file mode 100644 index 3f313dfc..00000000 --- a/src/ui/controls/button.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once -#include "pre.hpp" - -#include - -#include "ui/content_control.hpp" - -namespace cru::ui::render { -class BorderRenderObject; -} - -namespace cru::ui::controls { -class Button : public ContentControl { - public: - static constexpr auto control_type = L"Button"; - - static Button* Create() { return new Button(); } - - protected: - Button(); - - public: - Button(const Button& other) = delete; - Button(Button&& other) = delete; - Button& operator=(const Button& other) = delete; - Button& operator=(Button&& other) = delete; - ~Button() override = default; - - StringView GetControlType() const override final { return control_type; } - - render::RenderObject* GetRenderObject() const override; - - protected: - void OnChildChanged(Control* old_child, Control* new_child) override; - - private: - std::shared_ptr render_object_{}; -}; -} // namespace cru::ui::controls diff --git a/src/ui/controls/flex_layout.cpp b/src/ui/controls/flex_layout.cpp index bdbe2d73..3b70c98c 100644 --- a/src/ui/controls/flex_layout.cpp +++ b/src/ui/controls/flex_layout.cpp @@ -1,6 +1,6 @@ -#include "flex_layout.hpp" +#include "cru/ui/controls/flex_layout.hpp" -#include "ui/render/flex_layout_render_object.hpp" +#include "cru/ui/render/flex_layout_render_object.hpp" namespace cru::ui::controls { using render::FlexLayoutRenderObject; diff --git a/src/ui/controls/flex_layout.hpp b/src/ui/controls/flex_layout.hpp deleted file mode 100644 index 9ceef1f6..00000000 --- a/src/ui/controls/flex_layout.hpp +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once -#include "pre.hpp" - -#include - -#include "ui/layout_control.hpp" - -namespace cru::ui::render { -class FlexLayoutRenderObject; -} - -namespace cru::ui::controls { - -class FlexLayout : public LayoutControl { - public: - static constexpr auto control_type = L"FlexLayout"; - - static FlexLayout* Create() { return new FlexLayout(); } - - protected: - FlexLayout(); - - public: - FlexLayout(const FlexLayout& other) = delete; - FlexLayout(FlexLayout&& other) = delete; - FlexLayout& operator=(const FlexLayout& other) = delete; - FlexLayout& operator=(FlexLayout&& other) = delete; - ~FlexLayout() override = default; - - StringView GetControlType() const override final { return control_type; } - - render::RenderObject* GetRenderObject() const override; - - protected: - void OnAddChild(Control* child, int position) override; - void OnRemoveChild(Control* child, int position) override; - - private: - std::shared_ptr render_object_; -}; -} // namespace cru::ui::controls diff --git a/src/ui/controls/text_block.cpp b/src/ui/controls/text_block.cpp index c2f8cd8e..55d83acc 100644 --- a/src/ui/controls/text_block.cpp +++ b/src/ui/controls/text_block.cpp @@ -1,7 +1,7 @@ -#include "text_block.hpp" +#include "cru/ui/controls/text_block.hpp" -#include "ui/render/text_render_object.hpp" -#include "ui/ui_manager.hpp" +#include "cru/ui/render/text_render_object.hpp" +#include "cru/ui/ui_manager.hpp" namespace cru::ui::controls { using render::TextRenderObject; @@ -11,7 +11,7 @@ TextBlock::TextBlock() { UiManager::GetInstance()->GetPredefineResources(); render_object_.reset( new TextRenderObject(predefined_resources->text_block_text_brush, - predefined_resources->text_block_text_format, + predefined_resources->text_block_font, predefined_resources->text_block_selection_brush)); } @@ -19,7 +19,9 @@ render::RenderObject* TextBlock::GetRenderObject() const { return render_object_.get(); } -String TextBlock::GetText() const { return render_object_->GetText(); } +std::wstring TextBlock::GetText() const { return render_object_->GetText(); } -void TextBlock::SetText(const String& text) { render_object_->SetText(text); } +void TextBlock::SetText(std::wstring text) { + render_object_->SetText(std::move(text)); +} } // namespace cru::ui::controls diff --git a/src/ui/controls/text_block.hpp b/src/ui/controls/text_block.hpp deleted file mode 100644 index 0d65dd67..00000000 --- a/src/ui/controls/text_block.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once -#include "pre.hpp" - -#include - -#include "ui/no_child_control.hpp" - -namespace cru::ui::render { -class TextRenderObject; -} - -namespace cru::ui::controls { -class TextBlock : public NoChildControl { - public: - static constexpr auto control_type = L"TextBlock"; - - static TextBlock* Create() { return new TextBlock(); } - - protected: - TextBlock(); - - public: - TextBlock(const TextBlock& other) = delete; - TextBlock(TextBlock&& other) = delete; - TextBlock& operator=(const TextBlock& other) = delete; - TextBlock& operator=(TextBlock&& other) = delete; - ~TextBlock() override = default; - - StringView GetControlType() const override final { return control_type; } - - render::RenderObject* GetRenderObject() const override; - - String GetText() const; - void SetText(const String& text); - - private: - std::shared_ptr render_object_; -}; -} // namespace cru::ui::controls diff --git a/src/ui/layout_control.cpp b/src/ui/layout_control.cpp index 9d789670..00215a8e 100644 --- a/src/ui/layout_control.cpp +++ b/src/ui/layout_control.cpp @@ -1,6 +1,6 @@ #include "cru/ui/layout_control.hpp" -#include "window.hpp" +#include "cru/ui/window.hpp" #include diff --git a/src/ui/render/border_render_object.cpp b/src/ui/render/border_render_object.cpp index 93e87c90..97e386dd 100644 --- a/src/ui/render/border_render_object.cpp +++ b/src/ui/render/border_render_object.cpp @@ -192,7 +192,7 @@ void BorderRenderObject::RecreateGeometry() { builder.reset(graph_factory->CreateGeometryBuilder()); f(builder.get(), outer_rect, corner_radius_); f(builder.get(), inner_rect, corner_radius_); - border_outer_geometry_.reset(builder->Build()); + geometry_.reset(builder->Build()); builder.reset(); } } // namespace cru::ui::render diff --git a/src/ui/render/flex_layout_render_object.cpp b/src/ui/render/flex_layout_render_object.cpp index 99368c23..e7840b7e 100644 --- a/src/ui/render/flex_layout_render_object.cpp +++ b/src/ui/render/flex_layout_render_object.cpp @@ -188,7 +188,7 @@ void FlexLayoutRenderObject::OnLayoutContent(const Rect& content_rect) { const float content_anchor_x = calculate_anchor( content_main_align_, 0, content_rect.width, actual_content_width); - auto anchor_x = 0; + float anchor_x = 0; for (int i = 0; i < children.size(); i++) { const auto child = children[i]; const auto size = child->GetPreferredSize(); @@ -215,7 +215,7 @@ void FlexLayoutRenderObject::OnLayoutContent(const Rect& content_rect) { const float content_anchor_y = calculate_anchor( content_main_align_, 0, content_rect.height, actual_content_height); - auto anchor_y = 0; + float anchor_y = 0; for (int i = 0; i < children.size(); i++) { const auto child = children[i]; const auto size = child->GetPreferredSize(); diff --git a/src/ui/render/window_render_object.cpp b/src/ui/render/window_render_object.cpp index ca1ab707..781087aa 100644 --- a/src/ui/render/window_render_object.cpp +++ b/src/ui/render/window_render_object.cpp @@ -1,22 +1,25 @@ #include "cru/ui/render/window_render_object.hpp" +#include "cru/platform/native_window.hpp" #include "cru/platform/painter_util.hpp" +#include "cru/ui/window.hpp" #include namespace cru::ui::render { void WindowRenderObject::MeasureAndLayout() { - const auto client_size = window_->GetClientSize(); + const auto client_size = window_->GetNativeWindow()->GetClientSize(); Measure(client_size); Layout(Rect{Point{}, client_size}); } void WindowRenderObject::Draw(platform::Painter* painter) { + painter->Clear(colors::white); if (const auto child = GetChild()) { auto offset = child->GetOffset(); - platform::util::WithTransform(painter, - platform::Matrix::Translation(offset.x, offset.y), - [child](auto rt) { child->Draw(rt); }); + platform::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 9c3c00d2..b1132ea8 100644 --- a/src/ui/ui_manager.cpp +++ b/src/ui/ui_manager.cpp @@ -1,5 +1,7 @@ #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" @@ -8,13 +10,12 @@ PredefineResources::PredefineResources() { const auto graph_factory = platform::UiApplication::GetInstance()->GetGraphFactory(); - button_normal_border_brush.reset( - graph_factory->CreateSolidColorBrush(colors::black)); - - text_block_selection_brush.reset( - graph_factory->CreateSolidColorBrush(colors::skyblue)); - text_block_text_brush.reset( - graph_factory->CreateSolidColorBrush(colors::black)); + button_normal_border_brush.reset(static_cast( + graph_factory->CreateSolidColorBrush(colors::black))); + text_block_selection_brush.reset(static_cast( + graph_factory->CreateSolidColorBrush(colors::skyblue))); + text_block_text_brush.reset(static_cast( + 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 c0ee1a83..23bda285 100644 --- a/src/ui/window.cpp +++ b/src/ui/window.cpp @@ -99,6 +99,8 @@ Window* Window::CreateOverlapped() { } Window::Window(tag_overlapped_constructor) { + using namespace std::placeholders; + native_window_ = platform::UiApplication::GetInstance()->CreateWindow(nullptr); render_object_.reset(new render::WindowRenderObject(this)); @@ -108,12 +110,21 @@ Window::Window(tag_overlapped_constructor) { event_revokers_.push_back(native_window_->PaintEvent()->AddHandler( std::bind(&Window::OnNativePaint, this))); event_revokers_.push_back(native_window_->ResizeEvent()->AddHandler( - std::bind(&Window::OnNativeResize, this))); - event_revokers_.push_back(native_window_->FocusEvent()->AddHandler( - std::bind(&Window::OnNativeFocus, this))); + std::bind(&Window::OnNativeResize, this, _1))); event_revokers_.push_back(native_window_->FocusEvent()->AddHandler( - std::bind(&Window::OnNativeFocus, this))); - //TODO! + std::bind(&Window::OnNativeFocus, this, _1))); + event_revokers_.push_back(native_window_->MouseEnterLeaveEvent()->AddHandler( + std::bind(&Window::OnNativeMouseEnterLeave, this, _1))); + event_revokers_.push_back(native_window_->MouseMoveEvent()->AddHandler( + std::bind(&Window::OnNativeMouseMove, this, _1))); + event_revokers_.push_back(native_window_->MouseDownEvent()->AddHandler( + std::bind(&Window::OnNativeMouseDown, this, _1, _2))); + event_revokers_.push_back(native_window_->MouseUpEvent()->AddHandler( + std::bind(&Window::OnNativeMouseUp, this, _1, _2))); + event_revokers_.push_back(native_window_->KeyDownEvent()->AddHandler( + std::bind(&Window::OnNativeKeyDown, this, _1))); + event_revokers_.push_back(native_window_->KeyUpEvent()->AddHandler( + std::bind(&Window::OnNativeKeyUp, this, _1))); } Window::~Window() { @@ -157,7 +168,7 @@ void Window::OnNativeDestroy() { delete this; } void Window::OnNativePaint() { const auto painter = - std::make_unique(native_window_->BeginPaint()); + std::unique_ptr(native_window_->BeginPaint()); render_object_->Draw(painter.get()); painter->EndDraw(); } -- cgit v1.2.3