From 5dc738a57930271194bd86673eb86f149096a7b2 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 19 Mar 2019 16:21:54 +0800 Subject: ... --- src/cru_debug.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'src/cru_debug.cpp') diff --git a/src/cru_debug.cpp b/src/cru_debug.cpp index 9c61d052..b9226132 100644 --- a/src/cru_debug.cpp +++ b/src/cru_debug.cpp @@ -2,10 +2,8 @@ #include "system_headers.hpp" -namespace cru::debug -{ - void DebugMessage(const StringView& message) - { - ::OutputDebugStringW(message.data()); - } +namespace cru::debug { +void DebugMessage(const StringView& message) { + ::OutputDebugStringW(message.data()); } +} // namespace cru::debug -- cgit v1.2.3 From 962dc18ee4827b464764ec3708be3d00a9143971 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 21 Mar 2019 22:22:02 +0800 Subject: ... --- CruUI.vcxproj | 6 ++ CruUI.vcxproj.filters | 33 +++++----- src/base.hpp | 8 --- src/cru_debug.cpp | 2 + src/cru_debug.hpp | 4 ++ src/ui/content_control.cpp | 32 ++++++++++ src/ui/content_control.hpp | 31 +++++++++ src/ui/control.cpp | 110 +------------------------------- src/ui/control.hpp | 136 +--------------------------------------- src/ui/controls/flex_layout.hpp | 4 +- src/ui/controls/text_block.hpp | 2 +- src/ui/layout_control.cpp | 52 +++++++++++++++ src/ui/layout_control.hpp | 33 ++++++++++ src/ui/no_child_control.cpp | 5 ++ src/ui/no_child_control.hpp | 26 ++++++++ src/ui/window.cpp | 86 +++++++++++++++++++++++++ src/ui/window.hpp | 9 +-- src/util/format.hpp | 42 +++++++------ 18 files changed, 327 insertions(+), 294 deletions(-) create mode 100644 src/ui/content_control.cpp create mode 100644 src/ui/content_control.hpp create mode 100644 src/ui/layout_control.cpp create mode 100644 src/ui/layout_control.hpp create mode 100644 src/ui/no_child_control.cpp create mode 100644 src/ui/no_child_control.hpp (limited to 'src/cru_debug.cpp') diff --git a/CruUI.vcxproj b/CruUI.vcxproj index 2e1359f9..d307222b 100644 --- a/CruUI.vcxproj +++ b/CruUI.vcxproj @@ -117,6 +117,9 @@ + + + @@ -131,6 +134,9 @@ + + + diff --git a/CruUI.vcxproj.filters b/CruUI.vcxproj.filters index 3bd5d30a..55b19e92 100644 --- a/CruUI.vcxproj.filters +++ b/CruUI.vcxproj.filters @@ -81,6 +81,15 @@ Source Files + + Source Files + + + Source Files + + + Source Files + @@ -170,6 +179,15 @@ Header Files + + Header Files + + + Header Files + + + Header Files + @@ -199,24 +217,9 @@ Source Files - - Source Files - - - Source Files - - - Source Files - Source Files - - Source Files - - - Source Files - diff --git a/src/base.hpp b/src/base.hpp index a62a745b..2c511c4b 100644 --- a/src/base.hpp +++ b/src/base.hpp @@ -7,9 +7,6 @@ #include namespace cru { -template -struct type_tag {}; - // typedefs using String = std::wstring; using MultiByteString = std::string; @@ -38,9 +35,4 @@ struct Interface { [[noreturn]] inline void UnreachableCode() { throw std::logic_error("Unreachable code."); } - -inline void Require(const bool condition, - const MultiByteStringView& error_message) { - if (!condition) throw std::invalid_argument(error_message.data()); -} } // namespace cru diff --git a/src/cru_debug.cpp b/src/cru_debug.cpp index b9226132..331d2bce 100644 --- a/src/cru_debug.cpp +++ b/src/cru_debug.cpp @@ -3,7 +3,9 @@ #include "system_headers.hpp" namespace cru::debug { +#ifdef CRU_DEBUG void DebugMessage(const StringView& message) { ::OutputDebugStringW(message.data()); } +#endif } // namespace cru::debug diff --git a/src/cru_debug.hpp b/src/cru_debug.hpp index 9c22a24f..58431d56 100644 --- a/src/cru_debug.hpp +++ b/src/cru_debug.hpp @@ -7,7 +7,11 @@ #include "util/format.hpp" namespace cru::debug { +#ifdef CRU_DEBUG void DebugMessage(const StringView& message); +#else +inline void DebugMessage(const StringView& message) {} +#endif #ifdef CRU_DEBUG inline void DebugTime(const std::function& action, diff --git a/src/ui/content_control.cpp b/src/ui/content_control.cpp new file mode 100644 index 00000000..960867b2 --- /dev/null +++ b/src/ui/content_control.cpp @@ -0,0 +1,32 @@ +#include "content_control.hpp" + +#include "window.hpp" + +namespace cru::ui { +ContentControl::ContentControl() + : child_vector_{nullptr}, child_(child_vector_[0]) {} + +ContentControl::~ContentControl() { delete child_; } + +void ContentControl::SetChild(Control* child) { + if (dynamic_cast(child)) + throw std::invalid_argument("Can't add a window as child."); + + if (child == child_) return; + + const auto window = GetWindow(); + const auto old_child = child_; + child_ = child; + if (old_child) { + old_child->_SetParent(nullptr); + old_child->_SetDescendantWindow(nullptr); + } + if (child) { + child->_SetParent(this); + child->_SetDescendantWindow(window); + } + OnChildChanged(old_child, child); +} + +void ContentControl::OnChildChanged(Control* old_child, Control* new_child) {} +} // namespace cru::ui diff --git a/src/ui/content_control.hpp b/src/ui/content_control.hpp new file mode 100644 index 00000000..88e7f60f --- /dev/null +++ b/src/ui/content_control.hpp @@ -0,0 +1,31 @@ +#pragma once +#include "pre.hpp" + +#include "control.hpp" + +namespace cru::ui { +class ContentControl : public Control { + protected: + ContentControl(); + + public: + ContentControl(const ContentControl& other) = delete; + ContentControl(ContentControl&& other) = delete; + ContentControl& operator=(const ContentControl& other) = delete; + ContentControl& operator=(ContentControl&& other) = delete; + ~ContentControl() override; + + const std::vector& GetChildren() const override final { + return child_vector_; + } + Control* GetChild() const { return child_; } + void SetChild(Control* child); + + protected: + virtual void OnChildChanged(Control* old_child, Control* new_child); + + private: + std::vector child_vector_; + Control*& child_; +}; +} // namespace cru::ui diff --git a/src/ui/control.cpp b/src/ui/control.cpp index 98986d3c..e19754dc 100644 --- a/src/ui/control.cpp +++ b/src/ui/control.cpp @@ -35,14 +35,14 @@ void Control::_SetDescendantWindow(Window* window) { void Control::TraverseDescendants( const std::function& predicate) { - TraverseDescendantsInternal(this, predicate); + _TraverseDescendants(this, predicate); } -void Control::TraverseDescendantsInternal( +void Control::_TraverseDescendants( Control* control, const std::function& predicate) { predicate(control); for (auto c : control->GetChildren()) - TraverseDescendantsInternal(c, predicate); + _TraverseDescendants(c, predicate); } bool Control::RequestFocus() { auto window = GetWindow(); @@ -76,108 +76,4 @@ void Control::OnDetachToWindow(Window* window) {} void Control::OnMouseClickBegin(MouseButton button) {} void Control::OnMouseClickEnd(MouseButton button) {} - -const std::vector NoChildControl::empty_control_vector{}; - -ContentControl::ContentControl() - : child_vector_{nullptr}, child_(child_vector_[0]) {} - -ContentControl::~ContentControl() { delete child_; } - -void ContentControl::SetChild(Control* child) { - if (child == child_) return; - - const auto window = GetWindow(); - const auto old_child = child_; - child_ = child; - if (old_child) { - old_child->_SetParent(nullptr); - old_child->_SetDescendantWindow(nullptr); - } - if (child) { - child->_SetParent(this); - child->_SetDescendantWindow(window); - } - OnChildChanged(old_child, child); -} - -void ContentControl::OnChildChanged(Control* old_child, Control* new_child) {} - -void ControlAddChildCheck(Control* control) { - if (control->GetParent() != nullptr) - throw std::invalid_argument("The control already has a parent."); - - if (dynamic_cast(control)) - throw std::invalid_argument("Can't add a window as child."); -} - -Layout::~Layout() { - for (const auto child : children_) delete child; -} - -void Layout::AddChild(Control* control, const int position) { - ControlAddChildCheck(control); - - if (position < 0 || static_cast(position) > - this->children_.size()) - throw std::invalid_argument("The position is out of range."); - - children_.insert(this->children_.cbegin() + position, control); - - control->_SetParent(this); - control->_SetDescendantWindow(GetWindow()); - - OnAddChild(control, position); -} - -void Layout::RemoveChild(const int position) { - if (position < 0 || static_castchildren_.size())>(position) >= - this->children_.size()) - throw std::invalid_argument("The position is out of range."); - - const auto i = children_.cbegin() + position; - const auto child = *i; - - children_.erase(i); - - child->_SetParent(nullptr); - child->_SetDescendantWindow(nullptr); - - OnRemoveChild(child, position); -} - -void Layout::OnAddChild(Control* child, int position) {} - -void Layout::OnRemoveChild(Control* child, int position) {} - -std::list GetAncestorList(Control* control) { - std::list l; - while (control != nullptr) { - l.push_front(control); - control = control->GetParent(); - } - return l; -} - -Control* FindLowestCommonAncestor(Control* left, Control* right) { - if (left == nullptr || right == nullptr) return nullptr; - - auto&& left_list = GetAncestorList(left); - auto&& right_list = GetAncestorList(right); - - // the root is different - if (left_list.front() != right_list.front()) return nullptr; - - // find the last same control or the last control (one is ancestor of the - // other) - auto left_i = left_list.cbegin(); - auto right_i = right_list.cbegin(); - while (true) { - if (left_i == left_list.cend()) return *(--left_i); - if (right_i == right_list.cend()) return *(--right_i); - if (*left_i != *right_i) return *(--left_i); - ++left_i; - ++right_i; - } -} } // namespace cru::ui diff --git a/src/ui/control.hpp b/src/ui/control.hpp index 9ff407b0..8454e981 100644 --- a/src/ui/control.hpp +++ b/src/ui/control.hpp @@ -48,7 +48,7 @@ class Control : public Object { void _SetDescendantWindow(Window* window); private: - static void TraverseDescendantsInternal( + static void _TraverseDescendants( Control* control, const std::function& predicate); public: @@ -110,138 +110,4 @@ class Control : public Object { Cursor::Ptr cursor_{}; }; - -class NoChildControl : public Control { - private: - static const std::vector empty_control_vector; - - protected: - NoChildControl() = default; - - public: - NoChildControl(const NoChildControl& other) = delete; - NoChildControl(NoChildControl&& other) = delete; - NoChildControl& operator=(const NoChildControl& other) = delete; - NoChildControl& operator=(NoChildControl&& other) = delete; - ~NoChildControl() override = default; - - protected: - const std::vector& GetChildren() const override final { - return empty_control_vector; - } -}; - -class ContentControl : public Control { - protected: - ContentControl(); - - public: - ContentControl(const ContentControl& other) = delete; - ContentControl(ContentControl&& other) = delete; - ContentControl& operator=(const ContentControl& other) = delete; - ContentControl& operator=(ContentControl&& other) = delete; - ~ContentControl() override; - - const std::vector& GetChildren() const override final { - return child_vector_; - } - Control* GetChild() const { return child_; } - void SetChild(Control* child); - - protected: - virtual void OnChildChanged(Control* old_child, Control* new_child); - - private: - std::vector child_vector_; - Control*& child_; -}; - -class Layout : public Control { - protected: - Layout() = default; - - public: - Layout(const Layout& other) = delete; - Layout(Layout&& other) = delete; - Layout& operator=(const Layout& other) = delete; - Layout& operator=(Layout&& other) = delete; - ~Layout() override; - - const std::vector& GetChildren() const override final { - return children_; - } - - void AddChild(Control* control, int position); - - void RemoveChild(int position); - - protected: - virtual void OnAddChild(Control* child, int position); - virtual void OnRemoveChild(Control* child, int position); - - private: - std::vector children_; -}; - -//*************** region: event dispatcher helper *************** - -// Dispatch the event. -// -// This will raise routed event of the control and its parent and parent's -// parent ... (until "last_receiver" if it's not nullptr) with appropriate args. -// -// First tunnel from top to bottom possibly stopped by "handled" flag in -// EventArgs. Second bubble from bottom to top possibly stopped by "handled" -// flag in EventArgs. Last direct to each control. -// -// Args is of type "EventArgs". The first init argument is "sender", which is -// automatically bound to each receiving control. The second init argument is -// "original_sender", which is unchanged. And "args" will be perfectly forwarded -// as the rest arguments. -template -void DispatchEvent(Control* const original_sender, - events::RoutedEvent Control::*event_ptr, - Control* const last_receiver, Args&&... args) { - std::list receive_list; - - auto parent = original_sender; - while (parent != last_receiver) { - receive_list.push_back(parent); - parent = parent->GetParent(); - } - - auto handled = false; - - // tunnel - for (auto i = receive_list.crbegin(); i != receive_list.crend(); ++i) { - EventArgs event_args(*i, original_sender, std::forward(args)...); - (*i->*event_ptr).tunnel.Raise(event_args); - if (event_args.IsHandled()) { - handled = true; - break; - } - } - - // bubble - if (!handled) { - for (auto i : receive_list) { - EventArgs event_args(i, original_sender, std::forward(args)...); - (i->*event_ptr).bubble.Raise(event_args); - if (event_args.IsHandled()) break; - } - } - - // direct - for (auto i : receive_list) { - EventArgs event_args(i, original_sender, std::forward(args)...); - (i->*event_ptr).direct.Raise(event_args); - } -} - -//*************** region: tree helper *************** - -// Find the lowest common ancestor. -// Return nullptr if "left" and "right" are not in the same tree. -Control* FindLowestCommonAncestor(Control* left, Control* right); - } // namespace cru::ui diff --git a/src/ui/controls/flex_layout.hpp b/src/ui/controls/flex_layout.hpp index 682ed8dc..2ab3e259 100644 --- a/src/ui/controls/flex_layout.hpp +++ b/src/ui/controls/flex_layout.hpp @@ -1,7 +1,7 @@ #pragma once #include "pre.hpp" -#include "ui/control.hpp" +#include "ui/layout_control.hpp" namespace cru::ui::render { class FlexLayoutRenderObject; @@ -9,7 +9,7 @@ class FlexLayoutRenderObject; namespace cru::ui::controls { -class FlexLayout : public Layout { +class FlexLayout : public LayoutControl { public: static constexpr auto control_type = L"FlexLayout"; diff --git a/src/ui/controls/text_block.hpp b/src/ui/controls/text_block.hpp index c345c5ab..4c443020 100644 --- a/src/ui/controls/text_block.hpp +++ b/src/ui/controls/text_block.hpp @@ -1,7 +1,7 @@ #pragma once #include "pre.hpp" -#include "ui/control.hpp" +#include "ui/no_child_control.hpp" namespace cru::ui::render { class TextRenderObject; diff --git a/src/ui/layout_control.cpp b/src/ui/layout_control.cpp new file mode 100644 index 00000000..d2b430dd --- /dev/null +++ b/src/ui/layout_control.cpp @@ -0,0 +1,52 @@ +#include "layout_control.hpp" + +#include "window.hpp" + +namespace cru::ui { +void ControlAddChildCheck(Control* control) { + if (control->GetParent() != nullptr) + throw std::invalid_argument("The control already has a parent."); + + if (dynamic_cast(control)) + throw std::invalid_argument("Can't add a window as child."); +} + +LayoutControl::~LayoutControl() { + for (const auto child : children_) delete child; +} + +void LayoutControl::AddChild(Control* control, const int position) { + ControlAddChildCheck(control); + + if (position < 0 || static_cast(position) > + this->children_.size()) + throw std::invalid_argument("The position is out of range."); + + children_.insert(this->children_.cbegin() + position, control); + + control->_SetParent(this); + control->_SetDescendantWindow(GetWindow()); + + OnAddChild(control, position); +} + +void LayoutControl::RemoveChild(const int position) { + if (position < 0 || static_castchildren_.size())>(position) >= + this->children_.size()) + throw std::invalid_argument("The position is out of range."); + + const auto i = children_.cbegin() + position; + const auto child = *i; + + children_.erase(i); + + child->_SetParent(nullptr); + child->_SetDescendantWindow(nullptr); + + OnRemoveChild(child, position); +} + +void LayoutControl::OnAddChild(Control* child, int position) {} + +void LayoutControl::OnRemoveChild(Control* child, int position) {} +} // namespace cru::ui diff --git a/src/ui/layout_control.hpp b/src/ui/layout_control.hpp new file mode 100644 index 00000000..53f53186 --- /dev/null +++ b/src/ui/layout_control.hpp @@ -0,0 +1,33 @@ +#pragma once +#include "pre.hpp" + +#include "control.hpp" + +namespace cru::ui { +class LayoutControl : public Control { + protected: + LayoutControl() = default; + + public: + LayoutControl(const LayoutControl& other) = delete; + LayoutControl(LayoutControl&& other) = delete; + LayoutControl& operator=(const LayoutControl& other) = delete; + LayoutControl& operator=(LayoutControl&& other) = delete; + ~LayoutControl() override; + + const std::vector& GetChildren() const override final { + return children_; + } + + void AddChild(Control* control, int position); + + void RemoveChild(int position); + + protected: + virtual void OnAddChild(Control* child, int position); + virtual void OnRemoveChild(Control* child, int position); + + private: + std::vector children_; +}; +} // namespace cru::ui diff --git a/src/ui/no_child_control.cpp b/src/ui/no_child_control.cpp new file mode 100644 index 00000000..e6bbe813 --- /dev/null +++ b/src/ui/no_child_control.cpp @@ -0,0 +1,5 @@ +#include "no_child_control.hpp" + +namespace cru::ui { +const std::vector NoChildControl::empty_control_vector{}; +} diff --git a/src/ui/no_child_control.hpp b/src/ui/no_child_control.hpp new file mode 100644 index 00000000..26b5546f --- /dev/null +++ b/src/ui/no_child_control.hpp @@ -0,0 +1,26 @@ +#pragma once +#include "pre.hpp" + +#include "control.hpp" + +namespace cru::ui { +class NoChildControl : public Control { + private: + static const std::vector empty_control_vector; + + protected: + NoChildControl() = default; + + public: + NoChildControl(const NoChildControl& other) = delete; + NoChildControl(NoChildControl&& other) = delete; + NoChildControl& operator=(const NoChildControl& other) = delete; + NoChildControl& operator=(NoChildControl&& other) = delete; + ~NoChildControl() override = default; + + protected: + const std::vector& GetChildren() const override final { + return empty_control_vector; + } +}; +} // namespace cru::ui diff --git a/src/ui/window.cpp b/src/ui/window.cpp index f92811a7..b976ca6a 100644 --- a/src/ui/window.cpp +++ b/src/ui/window.cpp @@ -7,6 +7,92 @@ #include "render/window_render_object.hpp" namespace cru::ui { +namespace { +// Dispatch the event. +// +// This will raise routed event of the control and its parent and parent's +// parent ... (until "last_receiver" if it's not nullptr) with appropriate args. +// +// First tunnel from top to bottom possibly stopped by "handled" flag in +// EventArgs. Second bubble from bottom to top possibly stopped by "handled" +// flag in EventArgs. Last direct to each control. +// +// Args is of type "EventArgs". The first init argument is "sender", which is +// automatically bound to each receiving control. The second init argument is +// "original_sender", which is unchanged. And "args" will be perfectly forwarded +// as the rest arguments. +template +void DispatchEvent(Control* const original_sender, + events::RoutedEvent Control::*event_ptr, + Control* const last_receiver, Args&&... args) { + std::list receive_list; + + auto parent = original_sender; + while (parent != last_receiver) { + receive_list.push_back(parent); + parent = parent->GetParent(); + } + + auto handled = false; + + // tunnel + for (auto i = receive_list.crbegin(); i != receive_list.crend(); ++i) { + EventArgs event_args(*i, original_sender, std::forward(args)...); + (*i->*event_ptr).tunnel.Raise(event_args); + if (event_args.IsHandled()) { + handled = true; + break; + } + } + + // bubble + if (!handled) { + for (auto i : receive_list) { + EventArgs event_args(i, original_sender, std::forward(args)...); + (i->*event_ptr).bubble.Raise(event_args); + if (event_args.IsHandled()) break; + } + } + + // direct + for (auto i : receive_list) { + EventArgs event_args(i, original_sender, std::forward(args)...); + (i->*event_ptr).direct.Raise(event_args); + } +} + +std::list GetAncestorList(Control* control) { + std::list l; + while (control != nullptr) { + l.push_front(control); + control = control->GetParent(); + } + return l; +} + +Control* FindLowestCommonAncestor(Control* left, Control* right) { + if (left == nullptr || right == nullptr) return nullptr; + + auto&& left_list = GetAncestorList(left); + auto&& right_list = GetAncestorList(right); + + // the root is different + if (left_list.front() != right_list.front()) return nullptr; + + // find the last same control or the last control (one is ancestor of the + // other) + auto left_i = left_list.cbegin(); + auto right_i = right_list.cbegin(); + while (true) { + if (left_i == left_list.cend()) return *(--left_i); + if (right_i == right_list.cend()) return *(--right_i); + if (*left_i != *right_i) return *(--left_i); + ++left_i; + ++right_i; + } +} +} // namespace + LRESULT __stdcall GeneralWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { auto window = WindowManager::GetInstance()->FromHandle(hWnd); diff --git a/src/ui/window.hpp b/src/ui/window.hpp index 7a28c257..3e0422b1 100644 --- a/src/ui/window.hpp +++ b/src/ui/window.hpp @@ -5,7 +5,7 @@ #include #include "system_headers.hpp" -#include "control.hpp" +#include "content_control.hpp" #include "events/ui_event.hpp" #include "window_class.hpp" @@ -230,13 +230,6 @@ class Window final : public ContentControl { bool window_focus_ = false; Control* focus_control_ = this; // "focus_control_" can't be nullptr - Control* mouse_capture_control_ = nullptr; - - bool is_layout_invalid_ = false; - -#ifdef CRU_DEBUG_LAYOUT - bool debug_layout_ = false; -#endif }; } // namespace cru::ui diff --git a/src/util/format.hpp b/src/util/format.hpp index 874c5b43..7c1cee05 100644 --- a/src/util/format.hpp +++ b/src/util/format.hpp @@ -5,26 +5,29 @@ namespace cru::util { namespace details { -constexpr StringView PlaceHolder(type_tag) { return StringView(L"{}"); } +template +struct TypeTag {}; -constexpr MultiByteStringView PlaceHolder(type_tag) { +constexpr StringView PlaceHolder(TypeTag) { return StringView(L"{}"); } + +constexpr MultiByteStringView PlaceHolder(TypeTag) { return MultiByteStringView("{}"); } template void FormatInternal(TString& string) { - const auto find_result = string.find(PlaceHolder(type_tag{})); + const auto find_result = string.find(PlaceHolder(TypeTag{})); if (find_result != TString::npos) throw std::invalid_argument("There is more placeholders than args."); } template void FormatInternal(TString& string, const T& arg, const TRest&... args) { - const auto find_result = string.find(PlaceHolder(type_tag{})); + const auto find_result = string.find(PlaceHolder(TypeTag{})); if (find_result == TString::npos) throw std::invalid_argument("There is less placeholders than args."); - string.replace(find_result, 2, FormatToString(arg, type_tag{})); + string.replace(find_result, 2, FormatToString(arg, TypeTag{})); FormatInternal(string, args...); } } // namespace details @@ -43,13 +46,13 @@ MultiByteString Format(const MultiByteStringView& format, const T&... args) { return result; } -#define CRU_FORMAT_NUMBER(type) \ - inline String FormatToString(const type number, type_tag) { \ - return std::to_wstring(number); \ - } \ - inline MultiByteString FormatToString(const type number, \ - type_tag) { \ - return std::to_string(number); \ +#define CRU_FORMAT_NUMBER(type) \ + inline String FormatToString(const type number, details::TypeTag) { \ + return std::to_wstring(number); \ + } \ + inline MultiByteString FormatToString(const type number, \ + details::TypeTag) { \ + return std::to_string(number); \ } CRU_FORMAT_NUMBER(int) @@ -65,30 +68,33 @@ CRU_FORMAT_NUMBER(double) #undef CRU_FORMAT_NUMBER -inline StringView FormatToString(const String& string, type_tag) { +inline StringView FormatToString(const String& string, + details::TypeTag) { return string; } inline MultiByteString FormatToString(const MultiByteString& string, - type_tag) { + details::TypeTag) { return string; } -inline StringView FormatToString(const StringView& string, type_tag) { +inline StringView FormatToString(const StringView& string, + details::TypeTag) { return string; } inline MultiByteStringView FormatToString(const MultiByteStringView& string, - type_tag) { + details::TypeTag) { return string; } -inline StringView FormatToString(const wchar_t* string, type_tag) { +inline StringView FormatToString(const wchar_t* string, + details::TypeTag) { return StringView(string); } inline MultiByteStringView FormatToString(const char* string, - type_tag) { + details::TypeTag) { return MultiByteString(string); } } // namespace cru::util -- cgit v1.2.3 From e8be3841457853daefc26d0ca00256ad8c44f593 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 23 Mar 2019 23:52:07 +0800 Subject: ... --- CruUI.vcxproj | 15 +++++++++--- CruUI.vcxproj.filters | 21 +++++++++-------- src/application.cpp | 5 ++-- src/application.hpp | 2 +- src/cru_debug.cpp | 2 +- src/exception.hpp | 2 +- src/graph/graph.hpp | 8 +++++-- src/system_headers.hpp | 20 ---------------- src/timer.hpp | 2 +- src/ui/control.cpp | 9 -------- src/ui/control.hpp | 14 ----------- src/ui/controls/button.cpp | 0 src/ui/controls/button.hpp | 8 +++++++ src/ui/controls/text_block.cpp | 2 ++ src/ui/cursor.cpp | 24 ------------------- src/ui/cursor.hpp | 37 ----------------------------- src/ui/d2d_util.hpp | 2 +- src/ui/events/ui_event.cpp | 6 +---- src/ui/events/ui_event.hpp | 45 +++++------------------------------- src/ui/events/window_event.cpp | 3 +++ src/ui/events/window_event.hpp | 42 +++++++++++++++++++++++++++++++++ src/ui/input_util.cpp | 2 +- src/ui/render/text_render_object.cpp | 2 ++ src/ui/ui_manager.cpp | 2 ++ src/ui/ui_manager.hpp | 3 ++- src/ui/window.cpp | 23 ++---------------- src/ui/window.hpp | 6 ++--- src/ui/window_class.hpp | 2 +- src/util/string_util.cpp | 5 ++-- 29 files changed, 114 insertions(+), 200 deletions(-) delete mode 100644 src/system_headers.hpp create mode 100644 src/ui/controls/button.cpp create mode 100644 src/ui/controls/button.hpp delete mode 100644 src/ui/cursor.cpp delete mode 100644 src/ui/cursor.hpp create mode 100644 src/ui/events/window_event.cpp create mode 100644 src/ui/events/window_event.hpp (limited to 'src/cru_debug.cpp') diff --git a/CruUI.vcxproj b/CruUI.vcxproj index 374a882c..4143bb43 100644 --- a/CruUI.vcxproj +++ b/CruUI.vcxproj @@ -83,6 +83,7 @@ MachineX86 true Windows + D3D11.lib;D2d1.lib;DWrite.lib;%(AdditionalDependencies) @@ -100,6 +101,7 @@ Windows true true + D3D11.lib;D2d1.lib;DWrite.lib;%(AdditionalDependencies) @@ -108,6 +110,9 @@ stdcpplatest CRU_X64;%(PreprocessorDefinitions) + + D3D11.lib;D2d1.lib;DWrite.lib;%(AdditionalDependencies) + @@ -115,9 +120,14 @@ $(ProjectDir)\src;%(AdditionalIncludeDirectories) CRU_X64;%(PreprocessorDefinitions) + + D3D11.lib;D2d1.lib;DWrite.lib;%(AdditionalDependencies) + + + @@ -135,6 +145,8 @@ + + @@ -144,7 +156,6 @@ - @@ -161,14 +172,12 @@ - - diff --git a/CruUI.vcxproj.filters b/CruUI.vcxproj.filters index 5554927b..01575ce2 100644 --- a/CruUI.vcxproj.filters +++ b/CruUI.vcxproj.filters @@ -42,9 +42,6 @@ Source Files - - Source Files - Source Files @@ -90,6 +87,12 @@ Source Files + + Source Files + + + Source Files + @@ -104,9 +107,6 @@ Header Files - - Header Files - Header Files @@ -128,9 +128,6 @@ Header Files - - Header Files - Header Files @@ -188,6 +185,12 @@ Header Files + + Header Files + + + Header Files + diff --git a/src/application.cpp b/src/application.cpp index e580b56b..aafca6fe 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -1,8 +1,9 @@ #include "application.hpp" +#include + #include "exception.hpp" #include "timer.hpp" -#include "ui/cursor.hpp" #include "ui/window_class.hpp" namespace cru { @@ -79,8 +80,6 @@ Application::Application(HINSTANCE h_instance) : h_instance_(h_instance) { throw std::runtime_error("Must run on Windows 8 or later."); god_window_ = std::make_unique(this); - - ui::cursors::LoadSystemCursors(); } Application::~Application() { diff --git a/src/application.hpp b/src/application.hpp index acf264c3..f5f69ea4 100644 --- a/src/application.hpp +++ b/src/application.hpp @@ -1,12 +1,12 @@ #pragma once #include "pre.hpp" +#include #include #include #include #include #include -#include "system_headers.hpp" #include "base.hpp" diff --git a/src/cru_debug.cpp b/src/cru_debug.cpp index 331d2bce..81945227 100644 --- a/src/cru_debug.cpp +++ b/src/cru_debug.cpp @@ -1,6 +1,6 @@ #include "cru_debug.hpp" -#include "system_headers.hpp" +#include namespace cru::debug { #ifdef CRU_DEBUG diff --git a/src/exception.hpp b/src/exception.hpp index db2572f1..ade51d54 100644 --- a/src/exception.hpp +++ b/src/exception.hpp @@ -1,8 +1,8 @@ #pragma once #include "pre.hpp" +#include #include -#include "system_headers.hpp" #include "base.hpp" diff --git a/src/graph/graph.hpp b/src/graph/graph.hpp index bad5b6d0..af14cc50 100644 --- a/src/graph/graph.hpp +++ b/src/graph/graph.hpp @@ -1,9 +1,13 @@ #pragma once #include "pre.hpp" +#include +#include +#include +#include +#include #include #include -#include "system_headers.hpp" #include "base.hpp" @@ -101,7 +105,7 @@ class GraphManager final : public Object { Microsoft::WRL::ComPtr GetSystemFontCollection() const { - return dwrite_system_font_collection_.Get(); + return dwrite_system_font_collection_; } private: diff --git a/src/system_headers.hpp b/src/system_headers.hpp deleted file mode 100644 index 5517d71a..00000000 --- a/src/system_headers.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include "pre.hpp" - -//include system headers -#include -#include - -#pragma comment(lib, "D3D11.lib") -#include - -#pragma comment(lib, "D2d1.lib") -#include - -#pragma comment(lib, "DWrite.lib") -#include - -#include -#include - -#include diff --git a/src/timer.hpp b/src/timer.hpp index 685e83b9..7199adc2 100644 --- a/src/timer.hpp +++ b/src/timer.hpp @@ -1,11 +1,11 @@ #pragma once #include "pre.hpp" +#include #include #include #include #include -#include "system_headers.hpp" #include "base.hpp" diff --git a/src/ui/control.cpp b/src/ui/control.cpp index 5c629fd6..318d591a 100644 --- a/src/ui/control.cpp +++ b/src/ui/control.cpp @@ -55,15 +55,6 @@ bool Control::HasFocus() { return window->GetFocusControl() == this; } -void Control::SetCursor(const Cursor::Ptr& cursor) { - if (cursor != cursor_) { - cursor_ = cursor; - const auto window = GetWindow(); - if (window && window->GetMouseHoverControl() == this) - window->UpdateCursor(); - } -} - void Control::OnParentChanged(Control* old_parent, Control* new_parent) {} void Control::OnAttachToWindow(Window* window) {} diff --git a/src/ui/control.hpp b/src/ui/control.hpp index 8454e981..a44399bf 100644 --- a/src/ui/control.hpp +++ b/src/ui/control.hpp @@ -1,12 +1,8 @@ #pragma once #include "pre.hpp" -#include "system_headers.hpp" - #include "base.hpp" -#include "cursor.hpp" #include "events/ui_event.hpp" -#include "input_util.hpp" #include "ui_base.hpp" namespace cru::ui { @@ -60,14 +56,6 @@ class Control : public Object { bool HasFocus(); - //*************** region: cursor *************** - // If cursor is set to null, then it uses parent's cursor. - // Window's cursor can't be null. - public: - Cursor::Ptr GetCursor() const { return cursor_; } - - void SetCursor(const Cursor::Ptr& cursor); - //*************** region: events *************** public: // Raised when mouse enter the control. @@ -107,7 +95,5 @@ class Control : public Object { private: Window* window_ = nullptr; Control* parent_ = nullptr; - - Cursor::Ptr cursor_{}; }; } // namespace cru::ui diff --git a/src/ui/controls/button.cpp b/src/ui/controls/button.cpp new file mode 100644 index 00000000..e69de29b diff --git a/src/ui/controls/button.hpp b/src/ui/controls/button.hpp new file mode 100644 index 00000000..010c3f5b --- /dev/null +++ b/src/ui/controls/button.hpp @@ -0,0 +1,8 @@ +#pragma once +#include "pre.hpp" + +#include "ui/control.hpp" + +namespace cru::ui::controls { + +} diff --git a/src/ui/controls/text_block.cpp b/src/ui/controls/text_block.cpp index c891b832..85116910 100644 --- a/src/ui/controls/text_block.cpp +++ b/src/ui/controls/text_block.cpp @@ -1,5 +1,7 @@ #include "text_block.hpp" +#include + #include "ui/render/text_render_object.hpp" #include "ui/ui_manager.hpp" diff --git a/src/ui/cursor.cpp b/src/ui/cursor.cpp deleted file mode 100644 index d8c362ed..00000000 --- a/src/ui/cursor.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "cursor.hpp" - -#include "exception.hpp" - -namespace cru::ui { -Cursor::Cursor(HCURSOR handle, const bool auto_release) - : handle_(handle), auto_release_(auto_release) {} - -Cursor::~Cursor() { - if (auto_release_) ::DestroyCursor(handle_); -} - -namespace cursors { -Cursor::Ptr arrow{}; -Cursor::Ptr hand{}; -Cursor::Ptr i_beam{}; - -void LoadSystemCursors() { - arrow = std::make_shared(::LoadCursorW(nullptr, IDC_ARROW), false); - hand = std::make_shared(::LoadCursorW(nullptr, IDC_HAND), false); - i_beam = std::make_shared(::LoadCursorW(nullptr, IDC_IBEAM), false); -} -} // namespace cursors -} // namespace cru::ui diff --git a/src/ui/cursor.hpp b/src/ui/cursor.hpp deleted file mode 100644 index aec3fc40..00000000 --- a/src/ui/cursor.hpp +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -// ReSharper disable once CppUnusedIncludeDirective -#include "pre.hpp" - -#include -#include "system_headers.hpp" - -#include "base.hpp" - -namespace cru::ui { -class Cursor : public Object { - public: - using Ptr = std::shared_ptr; - - Cursor(HCURSOR handle, bool auto_release); - Cursor(const Cursor& other) = delete; - Cursor(Cursor&& other) = delete; - Cursor& operator=(const Cursor& other) = delete; - Cursor& operator=(Cursor&& other) = delete; - ~Cursor() override; - - HCURSOR GetHandle() const { return handle_; } - - private: - HCURSOR handle_; - bool auto_release_; -}; - -namespace cursors { -extern Cursor::Ptr arrow; -extern Cursor::Ptr hand; -extern Cursor::Ptr i_beam; - -void LoadSystemCursors(); -} // namespace cursors -} // namespace cru::ui diff --git a/src/ui/d2d_util.hpp b/src/ui/d2d_util.hpp index 96a017dc..2ec8ba98 100644 --- a/src/ui/d2d_util.hpp +++ b/src/ui/d2d_util.hpp @@ -1,7 +1,7 @@ #pragma once #include "pre.hpp" -#include "system_headers.hpp" +#include #include "ui_base.hpp" diff --git a/src/ui/events/ui_event.cpp b/src/ui/events/ui_event.cpp index ee3a68dc..78d56a83 100644 --- a/src/ui/events/ui_event.cpp +++ b/src/ui/events/ui_event.cpp @@ -1,7 +1,3 @@ #include "ui_event.hpp" -#include "ui/control.hpp" - -namespace cru::ui::events -{ -} +namespace cru::ui::events {} diff --git a/src/ui/events/ui_event.hpp b/src/ui/events/ui_event.hpp index 572cf8d6..7fe4e6eb 100644 --- a/src/ui/events/ui_event.hpp +++ b/src/ui/events/ui_event.hpp @@ -2,13 +2,14 @@ #include "pre.hpp" #include -#include "system_headers.hpp" #include "base.hpp" #include "cru_event.hpp" #include "ui/ui_base.hpp" #include "ui/input_util.hpp" +struct ID2D1RenderTarget; + namespace cru::ui { class Control; } @@ -112,18 +113,18 @@ class MouseWheelEventArgs : public MouseEventArgs { class DrawEventArgs : public UiEventArgs { public: DrawEventArgs(Object* sender, Object* original_sender, - ID2D1DeviceContext* device_context) - : UiEventArgs(sender, original_sender), device_context_(device_context) {} + ID2D1RenderTarget* render_target) + : UiEventArgs(sender, original_sender), render_target_(render_target) {} DrawEventArgs(const DrawEventArgs& other) = default; DrawEventArgs(DrawEventArgs&& other) = default; DrawEventArgs& operator=(const DrawEventArgs& other) = default; DrawEventArgs& operator=(DrawEventArgs&& other) = default; ~DrawEventArgs() = default; - ID2D1DeviceContext* GetDeviceContext() const { return device_context_; } + ID2D1RenderTarget* GetRenderTarget() const { return render_target_; } private: - ID2D1DeviceContext* device_context_; + ID2D1RenderTarget* render_target_; }; class FocusChangeEventArgs : public UiEventArgs { @@ -160,40 +161,6 @@ class ToggleEventArgs : public UiEventArgs { bool new_state_; }; -struct WindowNativeMessage { - HWND hwnd; - int msg; - WPARAM w_param; - LPARAM l_param; -}; - -class WindowNativeMessageEventArgs : public UiEventArgs { - public: - WindowNativeMessageEventArgs(Object* sender, Object* original_sender, - const WindowNativeMessage& message) - : UiEventArgs(sender, original_sender), - message_(message), - result_(std::nullopt) {} - WindowNativeMessageEventArgs(const WindowNativeMessageEventArgs& other) = - default; - WindowNativeMessageEventArgs(WindowNativeMessageEventArgs&& other) = default; - WindowNativeMessageEventArgs& operator=( - const WindowNativeMessageEventArgs& other) = default; - WindowNativeMessageEventArgs& operator=( - WindowNativeMessageEventArgs&& other) = default; - ~WindowNativeMessageEventArgs() override = default; - - WindowNativeMessage GetWindowMessage() const { return message_; } - - std::optional GetResult() const { return result_; } - - void SetResult(const std::optional result) { result_ = result; } - - private: - WindowNativeMessage message_; - std::optional result_; -}; - class KeyEventArgs : public UiEventArgs { public: KeyEventArgs(Object* sender, Object* original_sender, int virtual_code) diff --git a/src/ui/events/window_event.cpp b/src/ui/events/window_event.cpp new file mode 100644 index 00000000..a217136c --- /dev/null +++ b/src/ui/events/window_event.cpp @@ -0,0 +1,3 @@ +#include "window_event.hpp" + +namespace cru::ui::events {} diff --git a/src/ui/events/window_event.hpp b/src/ui/events/window_event.hpp new file mode 100644 index 00000000..21c644af --- /dev/null +++ b/src/ui/events/window_event.hpp @@ -0,0 +1,42 @@ +#pragma once +#include "pre.hpp" + +#include + +#include "ui_event.hpp" + +namespace cru::ui::events { +struct WindowNativeMessage { + HWND hwnd; + int msg; + WPARAM w_param; + LPARAM l_param; +}; + +class WindowNativeMessageEventArgs : public UiEventArgs { + public: + WindowNativeMessageEventArgs(Object* sender, Object* original_sender, + const WindowNativeMessage& message) + : UiEventArgs(sender, original_sender), + message_(message), + result_(std::nullopt) {} + WindowNativeMessageEventArgs(const WindowNativeMessageEventArgs& other) = + default; + WindowNativeMessageEventArgs(WindowNativeMessageEventArgs&& other) = default; + WindowNativeMessageEventArgs& operator=( + const WindowNativeMessageEventArgs& other) = default; + WindowNativeMessageEventArgs& operator=( + WindowNativeMessageEventArgs&& other) = default; + ~WindowNativeMessageEventArgs() override = default; + + WindowNativeMessage GetWindowMessage() const { return message_; } + + std::optional GetResult() const { return result_; } + + void SetResult(const std::optional result) { result_ = result; } + + private: + WindowNativeMessage message_; + std::optional result_; +}; +} diff --git a/src/ui/input_util.cpp b/src/ui/input_util.cpp index 3fe34f10..193cba4a 100644 --- a/src/ui/input_util.cpp +++ b/src/ui/input_util.cpp @@ -1,6 +1,6 @@ #include "input_util.hpp" -#include "system_headers.hpp" +#include namespace cru::ui { bool IsKeyDown(const int virtual_code) { diff --git a/src/ui/render/text_render_object.cpp b/src/ui/render/text_render_object.cpp index d57335ad..e8967d48 100644 --- a/src/ui/render/text_render_object.cpp +++ b/src/ui/render/text_render_object.cpp @@ -1,5 +1,7 @@ #include "text_render_object.hpp" +#include +#include #include #include "exception.hpp" diff --git a/src/ui/ui_manager.cpp b/src/ui/ui_manager.cpp index bcda4133..26b1fe62 100644 --- a/src/ui/ui_manager.cpp +++ b/src/ui/ui_manager.cpp @@ -1,5 +1,7 @@ #include "ui_manager.hpp" +#include + #include "application.hpp" #include "exception.hpp" #include "graph/graph.hpp" diff --git a/src/ui/ui_manager.hpp b/src/ui/ui_manager.hpp index 3fd2adc9..c2331dd4 100644 --- a/src/ui/ui_manager.hpp +++ b/src/ui/ui_manager.hpp @@ -1,7 +1,8 @@ #pragma once #include "pre.hpp" -#include "system_headers.hpp" +#include +#include #include "base.hpp" diff --git a/src/ui/window.cpp b/src/ui/window.cpp index ca3356ff..7b00ca05 100644 --- a/src/ui/window.cpp +++ b/src/ui/window.cpp @@ -1,7 +1,8 @@ #include "window.hpp" +#include + #include "application.hpp" -#include "cursor.hpp" #include "exception.hpp" #include "graph/graph.hpp" #include "render/window_render_object.hpp" @@ -153,17 +154,6 @@ inline POINT DipToPi(const Point& dip_point) { return result; } -namespace { -Cursor::Ptr GetCursorInherit(Control* control) { - while (control != nullptr) { - const auto cursor = control->GetCursor(); - if (cursor != nullptr) return cursor; - control = control->GetParent(); - } - return cursors::arrow; -} -} // namespace - Window* Window::CreateOverlapped() { return new Window(tag_overlapped_constructor{}); } @@ -220,8 +210,6 @@ void Window::AfterCreateHwnd(WindowManager* window_manager) { render_target_ = graph::GraphManager::GetInstance()->CreateWindowRenderTarget(hwnd_); - SetCursor(cursors::arrow); - render_object_ = new render::WindowRenderObject(this); } @@ -522,12 +510,6 @@ Control* Window::ReleaseCurrentMouseCapture() { } } -void Window::UpdateCursor() { - if (IsWindowValid() && mouse_hover_control_ != nullptr) { - SetCursorInternal(GetCursorInherit(mouse_hover_control_)->GetHandle()); - } -} - #ifdef CRU_DEBUG_LAYOUT void Window::SetDebugLayout(const bool value) { if (debug_layout_ != value) { @@ -719,7 +701,6 @@ void Window::DispatchMouseHoverControlChangeEvent(Control* old_control, DispatchEvent(new_control, &Control::mouse_enter_event, lowest_common_ancestor, point); // dispatch mouse enter event. - UpdateCursor(); } } } diff --git a/src/ui/window.hpp b/src/ui/window.hpp index 3e0422b1..1c48bf43 100644 --- a/src/ui/window.hpp +++ b/src/ui/window.hpp @@ -1,12 +1,13 @@ #pragma once #include "pre.hpp" +#include #include #include -#include "system_headers.hpp" #include "content_control.hpp" #include "events/ui_event.hpp" +#include "events/window_event.hpp" #include "window_class.hpp" namespace cru::graph { @@ -164,9 +165,6 @@ class Window final : public ContentControl { Control* CaptureMouseFor(Control* control); Control* ReleaseCurrentMouseCapture(); - //*************** region: cursor *************** - void UpdateCursor(); - public: //*************** region: events *************** Event activated_event; diff --git a/src/ui/window_class.hpp b/src/ui/window_class.hpp index 66babd94..72a7c431 100644 --- a/src/ui/window_class.hpp +++ b/src/ui/window_class.hpp @@ -1,7 +1,7 @@ #pragma once #include "pre.hpp" -#include "system_headers.hpp" +#include #include "base.hpp" diff --git a/src/util/string_util.cpp b/src/util/string_util.cpp index 3c765259..c9391fc6 100644 --- a/src/util/string_util.cpp +++ b/src/util/string_util.cpp @@ -1,6 +1,7 @@ #include "string_util.hpp" -#include "system_headers.hpp" +#include + #include "exception.hpp" namespace cru::util { @@ -18,4 +19,4 @@ MultiByteString ToUtf8String(const StringView& string) { "Failed to convert wide string to UTF-8."); return result; } -} +} // namespace cru::util -- cgit v1.2.3