diff options
Diffstat (limited to 'CruUI-Generate/cru_ui.hpp')
-rw-r--r-- | CruUI-Generate/cru_ui.hpp | 463 |
1 files changed, 398 insertions, 65 deletions
diff --git a/CruUI-Generate/cru_ui.hpp b/CruUI-Generate/cru_ui.hpp index 93360e3a..6dff57cd 100644 --- a/CruUI-Generate/cru_ui.hpp +++ b/CruUI-Generate/cru_ui.hpp @@ -3,19 +3,9 @@ //-------begin of file: src\any_map.hpp //-------------------------------------------------------- -#include <any> -#include <unordered_map> -#include <functional> -#include <optional> -#include <typeinfo> - -//-------------------------------------------------------- -//-------begin of file: src\base.hpp -//-------------------------------------------------------- - // ReSharper disable once CppUnusedIncludeDirective //-------------------------------------------------------- -//-------begin of file: src\global_macros.hpp +//-------begin of file: src\pre.hpp //-------------------------------------------------------- #ifdef _DEBUG @@ -25,18 +15,35 @@ #ifdef CRU_DEBUG #define CRU_DEBUG_LAYOUT #endif + + +#ifdef CRU_DEBUG +// ReSharper disable once IdentifierTypo +// ReSharper disable once CppInconsistentNaming +#define _CRTDBG_MAP_ALLOC +#include <cstdlib> +#include <crtdbg.h> +#endif +//-------------------------------------------------------- +//-------end of file: src\pre.hpp +//-------------------------------------------------------- + +#include <any> +#include <unordered_map> +#include <functional> +#include <optional> +#include <typeinfo> + //-------------------------------------------------------- -//-------end of file: src\global_macros.hpp +//-------begin of file: src\base.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective #include <string> #include <stdexcept> #include <string_view> #include <chrono> -#include <optional> -// ReSharper disable once CppUnusedIncludeDirective -#include <type_traits> namespace cru { @@ -86,33 +93,6 @@ namespace cru if (!condition) throw std::invalid_argument(error_message.data()); } - - template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>> - float Coerce(const T n, const std::optional<T> min, const std::optional<T> max) - { - if (min.has_value() && n < min.value()) - return min.value(); - if (max.has_value() && n > max.value()) - return max.value(); - return n; - } - - template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>> - float Coerce(const T n, const std::nullopt_t, const std::optional<T> max) - { - if (max.has_value() && n > max.value()) - return max.value(); - return n; - } - - template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>> - float Coerce(const T n, const std::optional<T> min, const std::nullopt_t) - { - if (min.has_value() && n < min.value()) - return min.value(); - return n; - } - } //-------------------------------------------------------- //-------end of file: src\base.hpp @@ -121,6 +101,8 @@ namespace cru //-------begin of file: src\format.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + namespace cru { @@ -325,10 +307,13 @@ namespace cru //-------begin of file: src\application.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + //-------------------------------------------------------- //-------begin of file: src\system_headers.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective //include system headers @@ -348,6 +333,8 @@ namespace cru #include <dxgi1_2.h> #include <wrl/client.h> + +#include <VersionHelpers.h> //-------------------------------------------------------- //-------end of file: src\system_headers.hpp //-------------------------------------------------------- @@ -469,6 +456,8 @@ namespace cru //-------begin of file: src\exception.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + #include <optional> @@ -529,6 +518,7 @@ namespace cru { //-------begin of file: src\timer.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective #include <map> #include <chrono> @@ -594,14 +584,17 @@ namespace cru //-------begin of file: src\ui\window.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + #include <map> -#include <list> #include <memory> //-------------------------------------------------------- //-------begin of file: src\ui\control.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + #include <unordered_map> #include <any> #include <utility> @@ -610,6 +603,8 @@ namespace cru //-------begin of file: src\ui\ui_base.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + #include <optional> @@ -760,6 +755,16 @@ namespace cru::ui return Point(left + width, top + height); } + constexpr Point GetLeftBottom() const + { + return Point(left, top + height); + } + + constexpr Point GetRightTop() const + { + return Point(left + width, top); + } + constexpr Size GetSize() const { return Size(width, height); @@ -832,6 +837,8 @@ namespace cru::ui //-------begin of file: src\ui\layout_base.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + #include <unordered_set> @@ -977,12 +984,16 @@ namespace cru::ui //-------begin of file: src\ui\events\ui_event.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + #include <optional> //-------------------------------------------------------- //-------begin of file: src\cru_event.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + #include <type_traits> #include <functional> #include <unordered_map> @@ -1147,6 +1158,30 @@ namespace cru::ui::events }; + class MouseWheelEventArgs : public MouseEventArgs + { + public: + MouseWheelEventArgs(Object* sender, Object* original_sender, const Point& point, const float delta) + : MouseEventArgs(sender, original_sender, point), delta_(delta) + { + + } + MouseWheelEventArgs(const MouseWheelEventArgs& other) = default; + MouseWheelEventArgs(MouseWheelEventArgs&& other) = default; + MouseWheelEventArgs& operator=(const MouseWheelEventArgs& other) = default; + MouseWheelEventArgs& operator=(MouseWheelEventArgs&& other) = default; + ~MouseWheelEventArgs() override = default; + + float GetDelta() const + { + return delta_; + } + + private: + float delta_; + }; + + class DrawEventArgs : public UiEventArgs { public: @@ -1366,6 +1401,7 @@ namespace cru::ui::events using UiEvent = Event<UiEventArgs>; using MouseEvent = Event<MouseEventArgs>; using MouseButtonEvent = Event<MouseButtonEventArgs>; + using MouseWheelEvent = Event<MouseWheelEventArgs>; using DrawEvent = Event<DrawEventArgs>; using PositionChangedEvent = Event<PositionChangedEventArgs>; using SizeChangedEvent = Event<SizeChangedEventArgs>; @@ -1382,6 +1418,8 @@ namespace cru::ui::events //-------begin of file: src\ui\border_property.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + @@ -1469,6 +1507,8 @@ namespace cru::ui //-------begin of file: src\ui\cursor.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + #include <memory> @@ -1500,7 +1540,9 @@ namespace cru::ui { extern Cursor::Ptr arrow; extern Cursor::Ptr hand; - extern Cursor::Ptr i_beam; + extern Cursor::Ptr i_beam; + + void LoadSystemCursors(); } } //-------------------------------------------------------- @@ -1520,12 +1562,22 @@ namespace cru::ui Point lefttop_position_absolute; }; + class Control : public Object { friend class Window; friend class LayoutManager; protected: + struct GeometryInfo + { + Microsoft::WRL::ComPtr<ID2D1Geometry> border_geometry = nullptr; + Microsoft::WRL::ComPtr<ID2D1Geometry> padding_content_geometry = nullptr; + Microsoft::WRL::ComPtr<ID2D1Geometry> content_geometry = nullptr; + }; + + + protected: struct WindowConstructorTag {}; //Used for constructor for class Window. explicit Control(bool container = false); @@ -1615,9 +1667,18 @@ namespace cru::ui // fill and stroke with width of border. virtual bool IsPointInside(const Point& point); + // Get the top control among all descendants (including self) in local coordinate. + virtual Control* HitTest(const Point& point); //*************** region: graphic *************** + bool IsClipContent() const + { + return clip_content_; + } + + void SetClipContent(bool clip); + //Draw this control and its child controls. void Draw(ID2D1DeviceContext* device_context); @@ -1733,6 +1794,8 @@ namespace cru::ui //Raised when a mouse button is pressed in the control and released in the control with mouse not leaving it between two operations. events::MouseButtonEvent mouse_click_event; + events::MouseWheelEvent mouse_wheel_event; + events::KeyEvent key_down_event; events::KeyEvent key_up_event; events::CharEvent char_event; @@ -1758,12 +1821,11 @@ namespace cru::ui //Invoked when the control is detached to a window. Overrides should invoke base. virtual void OnDetachToWindow(Window* window); + //*************** region: graphic events *************** private: + void OnDrawDecoration(ID2D1DeviceContext* device_context); void OnDrawCore(ID2D1DeviceContext* device_context); - protected: - - //*************** region: graphic events *************** virtual void OnDrawContent(ID2D1DeviceContext* device_context); virtual void OnDrawForeground(ID2D1DeviceContext* device_context); virtual void OnDrawBackground(ID2D1DeviceContext* device_context); @@ -1785,7 +1847,12 @@ namespace cru::ui void RaisePositionChangedEvent(events::PositionChangedEventArgs& args); void RaiseSizeChangedEvent(events::SizeChangedEventArgs& args); - void RegenerateBorderGeometry(); + void RegenerateGeometryInfo(); + + const GeometryInfo& GetGeometryInfo() const + { + return geometry_info_; + } //*************** region: mouse event *************** virtual void OnMouseEnter(events::MouseEventArgs& args); @@ -1802,6 +1869,9 @@ namespace cru::ui virtual void OnMouseUpCore(events::MouseButtonEventArgs& args); virtual void OnMouseClickCore(events::MouseButtonEventArgs& args); + virtual void OnMouseWheel(events::MouseWheelEventArgs& args); + virtual void OnMouseWheelCore(events::MouseWheelEventArgs& args); + void RaiseMouseEnterEvent(events::MouseEventArgs& args); void RaiseMouseLeaveEvent(events::MouseEventArgs& args); void RaiseMouseMoveEvent(events::MouseEventArgs& args); @@ -1809,6 +1879,8 @@ namespace cru::ui void RaiseMouseUpEvent(events::MouseButtonEventArgs& args); void RaiseMouseClickEvent(events::MouseButtonEventArgs& args); + void RaiseMouseWheelEvent(events::MouseWheelEventArgs& args); + virtual void OnMouseClickBegin(MouseButton button); virtual void OnMouseClickEnd(MouseButton button); @@ -1842,6 +1914,9 @@ namespace cru::ui virtual Size OnMeasureContent(const Size& available_size); virtual void OnLayoutContent(const Rect& rect); + // Called by Layout after set position and size. + virtual void AfterLayoutSelf(); + private: // Only for layout manager to use. // Check if the old position is updated to current position. @@ -1858,10 +1933,8 @@ namespace cru::ui private: bool is_container_; - protected: - Window * window_ = nullptr; // protected for Window class to write it as itself in constructor. + Window * window_ = nullptr; - private: Control * parent_ = nullptr; std::vector<Control*> children_{}; @@ -1891,8 +1964,9 @@ namespace cru::ui bool is_bordered_ = false; BorderProperty border_property_; - Microsoft::WRL::ComPtr<ID2D1Geometry> border_geometry_ = nullptr; - Microsoft::WRL::ComPtr<ID2D1Geometry> in_border_geometry_ = nullptr; //used for foreground and background brush. + GeometryInfo geometry_info_{}; + + bool clip_content_ = false; Microsoft::WRL::ComPtr<ID2D1Brush> foreground_brush_ = nullptr; Microsoft::WRL::ComPtr<ID2D1Brush> background_brush_ = nullptr; @@ -2155,15 +2229,6 @@ namespace cru::ui void SetSizeFitContent(const Size& max_size = Size(1000, 1000)); - //*************** region: functions *************** - - //Refresh control list. - //It should be invoked every time a control is added or removed from the tree. - void RefreshControlList(); - - //Get the most top control at "point". - Control* HitTest(const Point& point); - //*************** region: focus *************** @@ -2224,7 +2289,8 @@ namespace cru::ui void OnMouseLeaveInternal(); void OnMouseDownInternal(MouseButton button, POINT point); void OnMouseUpInternal(MouseButton button, POINT point); - + + void OnMouseWheelInternal(short delta, POINT point); void OnKeyDownInternal(int virtual_code); void OnKeyUpInternal(int virtual_code); void OnCharInternal(wchar_t c); @@ -2267,8 +2333,6 @@ namespace cru::ui Window* parent_window_ = nullptr; std::shared_ptr<graph::WindowRenderTarget> render_target_{}; - std::list<Control*> control_list_{}; - Control* mouse_hover_control_ = nullptr; bool window_focus_ = false; @@ -2290,6 +2354,8 @@ namespace cru::ui //-------begin of file: src\cru_debug.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + #include <functional> @@ -2337,6 +2403,8 @@ namespace cru::debug //-------begin of file: src\ui\controls\linear_layout.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + namespace cru::ui::controls { @@ -2388,10 +2456,14 @@ namespace cru::ui::controls //-------begin of file: src\ui\controls\text_block.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + //-------------------------------------------------------- //-------begin of file: src\ui\controls\text_control.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + namespace cru::ui::controls { @@ -2529,6 +2601,8 @@ namespace cru::ui::controls //-------begin of file: src\ui\controls\toggle_button.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + namespace cru::ui::controls { @@ -2598,6 +2672,8 @@ namespace cru::ui::controls //-------begin of file: src\ui\controls\button.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + #include <initializer_list> @@ -2644,6 +2720,8 @@ namespace cru::ui::controls //-------begin of file: src\ui\controls\text_box.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + namespace cru::ui::controls { @@ -2699,6 +2777,8 @@ namespace cru::ui::controls //-------begin of file: src\ui\controls\list_item.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + #include <map> #include <initializer_list> @@ -2770,6 +2850,8 @@ namespace cru::ui::controls //-------begin of file: src\ui\controls\popup_menu.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + #include <vector> #include <utility> #include <functional> @@ -2793,6 +2875,8 @@ namespace cru::ui::controls //-------begin of file: src\ui\controls\frame_layout.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + #include <initializer_list> @@ -2827,9 +2911,175 @@ namespace cru::ui::controls //-------end of file: src\ui\controls\frame_layout.hpp //-------------------------------------------------------- //-------------------------------------------------------- +//-------begin of file: src\ui\controls\scroll_control.hpp +//-------------------------------------------------------- + +// ReSharper disable once CppUnusedIncludeDirective + +#include <optional> +#include <initializer_list> + + +namespace cru::ui::controls +{ + // Done: OnMeasureContent + // Done: OnLayoutContent + // Done: HitTest(no need) + // Done: Draw(no need) + // Done: API + // Done: ScrollBar + // Done: MouseEvent + class ScrollControl : public Control + { + private: + struct ScrollBarInfo + { + Rect border = Rect(); + Rect bar = Rect(); + }; + + enum class Orientation + { + Horizontal, + Vertical + }; + + public: + enum class ScrollBarVisibility + { + None, + Auto, + Always + }; + + static ScrollControl* Create(const std::initializer_list<Control*>& children = std::initializer_list<Control*>{}) + { + const auto control = new ScrollControl(true); + for (auto child : children) + control->AddChild(child); + return control; + } + + static constexpr auto control_type = L"ScrollControl"; + + protected: + explicit ScrollControl(bool container); + public: + ScrollControl(const ScrollControl& other) = delete; + ScrollControl(ScrollControl&& other) = delete; + ScrollControl& operator=(const ScrollControl& other) = delete; + ScrollControl& operator=(ScrollControl&& other) = delete; + ~ScrollControl() override; + + StringView GetControlType() const override final; + + bool IsHorizontalScrollEnabled() const + { + return horizontal_scroll_enabled_; + } + + void SetHorizontalScrollEnabled(bool enable); + + bool IsVerticalScrollEnabled() const + { + return vertical_scroll_enabled_; + } + + void SetVerticalScrollEnabled(bool enable); + + + ScrollBarVisibility GetHorizontalScrollBarVisibility() const + { + return horizontal_scroll_bar_visibility_; + } + + void SetHorizontalScrollBarVisibility(ScrollBarVisibility visibility); + + ScrollBarVisibility GetVerticalScrollBarVisibility() const + { + return vertical_scroll_bar_visibility_; + } + + void SetVerticalScrollBarVisibility(ScrollBarVisibility visibility); + + float GetViewWidth() const + { + return view_width_; + } + + float GetViewHeight() const + { + return view_height_; + } + + float GetScrollOffsetX() const + { + return offset_x_; + } + + float GetScrollOffsetY() const + { + return offset_y_; + } + + // nullopt for not set. value is auto-coerced. + void SetScrollOffset(std::optional<float> x, std::optional<float> y); + + protected: + void SetViewWidth(float length); + void SetViewHeight(float length); + + Size OnMeasureContent(const Size& available_size) override final; + void OnLayoutContent(const Rect& rect) override final; + + void AfterLayoutSelf() override; + + void OnDrawForeground(ID2D1DeviceContext* device_context) override; + + void OnMouseDownCore(events::MouseButtonEventArgs& args) override final; + void OnMouseMoveCore(events::MouseEventArgs& args) override final; + void OnMouseUpCore(events::MouseButtonEventArgs& args) override final; + + void OnMouseWheelCore(events::MouseWheelEventArgs& args) override; + + private: + void CoerceAndSetOffsets(float offset_x, float offset_y, bool update_children = true); + void UpdateScrollBarVisibility(); + void UpdateScrollBarBorderInfo(); + void UpdateScrollBarBarInfo(); + + private: + bool horizontal_scroll_enabled_ = true; + bool vertical_scroll_enabled_ = true; + + ScrollBarVisibility horizontal_scroll_bar_visibility_ = ScrollBarVisibility::Auto; + ScrollBarVisibility vertical_scroll_bar_visibility_ = ScrollBarVisibility::Auto; + + bool is_horizontal_scroll_bar_visible_ = false; + bool is_vertical_scroll_bar_visible_ = false; + + float offset_x_ = 0.0f; + float offset_y_ = 0.0f; + + float view_width_ = 0.0f; + float view_height_ = 0.0f; + + ScrollBarInfo horizontal_bar_info_; + ScrollBarInfo vertical_bar_info_; + + std::optional<Orientation> is_pressing_scroll_bar_ = std::nullopt; + float pressing_delta_ = 0.0f; + }; +} +//-------------------------------------------------------- +//-------end of file: src\ui\controls\scroll_control.hpp +//-------------------------------------------------------- +//-------------------------------------------------------- //-------begin of file: src\graph\graph.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + #include <memory> #include <functional> @@ -3010,6 +3260,8 @@ namespace cru::graph //-------begin of file: src\ui\ui_manager.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + namespace cru::graph @@ -3068,6 +3320,10 @@ namespace cru::ui Microsoft::WRL::ComPtr<ID2D1Brush> list_item_select_border_brush; Microsoft::WRL::ComPtr<ID2D1Brush> list_item_select_fill_brush; + //region ScrollControl + Microsoft::WRL::ComPtr<ID2D1Brush> scroll_bar_background_brush; + Microsoft::WRL::ComPtr<ID2D1Brush> scroll_bar_border_brush; + Microsoft::WRL::ComPtr<ID2D1Brush> scroll_bar_brush; #ifdef CRU_DEBUG_LAYOUT //region debug @@ -3113,6 +3369,8 @@ namespace cru::ui //-------begin of file: src\ui\convert_util.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + namespace cru::ui @@ -3131,9 +3389,84 @@ namespace cru::ui //-------end of file: src\ui\convert_util.hpp //-------------------------------------------------------- //-------------------------------------------------------- +//-------begin of file: src\math_util.hpp +//-------------------------------------------------------- + +// ReSharper disable once CppUnusedIncludeDirective + +// ReSharper disable once CppUnusedIncludeDirective +#include <type_traits> +#include <optional> + +namespace cru +{ + template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>> + float Coerce(const T n, const std::optional<T> min, const std::optional<T> max) + { + if (min.has_value() && n < min.value()) + return min.value(); + if (max.has_value() && n > max.value()) + return max.value(); + return n; + } + + template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>> + float Coerce(const T n, const T min, const T max) + { + if (n < min) + return min; + if (n > max) + return max; + return n; + } + + template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>> + float Coerce(const T n, const std::nullopt_t, const std::optional<T> max) + { + if (max.has_value() && n > max.value()) + return max.value(); + return n; + } + + template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>> + float Coerce(const T n, const std::optional<T> min, const std::nullopt_t) + { + if (min.has_value() && n < min.value()) + return min.value(); + return n; + } + + template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>> + float Coerce(const T n, const std::nullopt_t, const T max) + { + if (n > max) + return max; + return n; + } + + template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>> + float Coerce(const T n, const T min, const std::nullopt_t) + { + if (n < min) + return min; + return n; + } + + template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>> + T AtLeast0(const T value) + { + return value < static_cast<T>(0) ? static_cast<T>(0) : value; + } +} +//-------------------------------------------------------- +//-------end of file: src\math_util.hpp +//-------------------------------------------------------- +//-------------------------------------------------------- //-------begin of file: src\ui\animations\animation.hpp //-------------------------------------------------------- +// ReSharper disable once CppUnusedIncludeDirective + #include <unordered_map> |