diff options
Diffstat (limited to 'include/cru/ui')
-rw-r--r-- | include/cru/ui/base.hpp | 6 | ||||
-rw-r--r-- | include/cru/ui/click_detector.hpp | 35 | ||||
-rw-r--r-- | include/cru/ui/controls/button.hpp | 48 | ||||
-rw-r--r-- | include/cru/ui/render/render_object.hpp | 2 | ||||
-rw-r--r-- | include/cru/ui/render/window_render_object.hpp | 3 | ||||
-rw-r--r-- | include/cru/ui/ui_event.hpp | 2 |
6 files changed, 31 insertions, 65 deletions
diff --git a/include/cru/ui/base.hpp b/include/cru/ui/base.hpp index 581e6f8f..90a3f746 100644 --- a/include/cru/ui/base.hpp +++ b/include/cru/ui/base.hpp @@ -16,6 +16,12 @@ using cru::platform::TextRange; using cru::platform::Thickness; using cru::platform::native::MouseButton; +namespace mouse_buttons { +using cru::platform::native::mouse_buttons::left; +using cru::platform::native::mouse_buttons::middle; +using cru::platform::native::mouse_buttons::right; +} // namespace mouse_buttons + namespace colors { using cru::platform::colors::black; using cru::platform::colors::skyblue; diff --git a/include/cru/ui/click_detector.hpp b/include/cru/ui/click_detector.hpp index d5e4ac78..9b59c717 100644 --- a/include/cru/ui/click_detector.hpp +++ b/include/cru/ui/click_detector.hpp @@ -49,7 +49,17 @@ class ClickDetector : public Object { Control* GetControl() const { return control_; } + ClickState GetState() const { return state_; } + + // Default is enable. + bool IsEnabled() const { return enable_; } + // If disable when user is pressing, the pressing is deactivated. + void SetEnabled(bool enable); + + // Default is left and right. MouseButton GetTriggerButton() const { return trigger_button_; } + // If unset the trigger button when user is pressing, the pressing is + // deactivated. void SetTriggerButton(MouseButton trigger_button); IEvent<ClickEventArgs>* ClickEvent() { return &event_; } @@ -57,33 +67,22 @@ class ClickDetector : public Object { IEvent<ClickState>* StateChangeEvent() { return &state_change_event_; } private: - std::optional<Point>& FromButton(MouseButton button) { - switch (button) { - case MouseButton::Left: - return click_map_.left; - case MouseButton::Middle: - return click_map_.middle; - case MouseButton::Right: - return click_map_.right; - default: - UnreachableCode(); - } - } + void SetState(ClickState state); private: Control* control_; - MouseButton trigger_button_ = MouseButton::Left | MouseButton::Right; + ClickState state_; + + bool enable_ = true; + MouseButton trigger_button_ = mouse_buttons::left | mouse_buttons::right; Event<ClickEventArgs> event_; Event<ClickState> state_change_event_; std::vector<EventRevokerGuard> event_rovoker_guards_; - struct { - std::optional<Point> left = std::nullopt; - std::optional<Point> middle = std::nullopt; - std::optional<Point> right = std::nullopt; - } click_map_; + Point down_point_; + MouseButton button_; }; } // namespace cru::ui diff --git a/include/cru/ui/controls/button.hpp b/include/cru/ui/controls/button.hpp index 60b1243f..b24a9934 100644 --- a/include/cru/ui/controls/button.hpp +++ b/include/cru/ui/controls/button.hpp @@ -19,27 +19,16 @@ struct ButtonStateStyle { }; struct ButtonStyle { - // corresponds to ButtonState::Normal + // corresponds to ClickState::None ButtonStateStyle normal; - // corresponds to ButtonState::Hover + // corresponds to ClickState::Hover ButtonStateStyle hover; - // corresponds to ButtonState::Press + // corresponds to ClickState::Press ButtonStateStyle press; - // corresponds to ButtonState::PressCancel + // corresponds to ClickState::PressInactive ButtonStateStyle press_cancel; }; -enum class ButtonState { - // mouse is not in it - Normal, - // mouse is in it and not pressed - Hover, - // mouse is pressed in it - Press, - // mouse is pressed outside button - PressCancel, -}; - class Button : public ContentControl { public: static constexpr std::string_view control_type = "Button"; @@ -56,9 +45,7 @@ class Button : public ContentControl { Button& operator=(Button&& other) = delete; ~Button() override = default; - std::string_view GetControlType() const final { - return control_type; - } + std::string_view GetControlType() const final { return control_type; } render::RenderObject* GetRenderObject() const override; @@ -66,37 +53,12 @@ class Button : public ContentControl { const ButtonStyle& GetStyle() const { return style_; } void SetStyle(ButtonStyle style); - ButtonState GetState() const { return state_; } - - // Get the trigger mouse button(s). Return value might be a union of Left, - // Middle and Right. - MouseButton GetTriggerButton() const { return trigger_button_; } - // Set the trigger mouse button(s). You can set multiple buttons by passing a - // union of Left, Middle and Right. If you disable a button when user is - // pressing the same one, click will be stopped. - // Default is only Left. - void SetTriggerButton(MouseButton newTrigger); - protected: void OnChildChanged(Control* old_child, Control* new_child) override; - virtual void OnStateChange(ButtonState oldState, ButtonState newState); - - private: - void SetState(ButtonState newState) { - if (state_ == newState) return; - const auto oldState = state_; - state_ = newState; - OnStateChange(oldState, newState); - } - private: std::unique_ptr<render::BorderRenderObject> render_object_{}; - ButtonState state_ = ButtonState::Normal; - - MouseButton trigger_button_ = MouseButton::Left; - ButtonStyle style_; ClickDetector click_detector_; diff --git a/include/cru/ui/render/render_object.hpp b/include/cru/ui/render/render_object.hpp index 55b7f2da..3e351d34 100644 --- a/include/cru/ui/render/render_object.hpp +++ b/include/cru/ui/render/render_object.hpp @@ -117,6 +117,8 @@ class RenderObject : public Object { private: void SetParent(RenderObject* new_parent); + void SetRenderHostRecursive(IRenderHost* host); + private: Control* control_ = nullptr; IRenderHost* render_host_ = nullptr; diff --git a/include/cru/ui/render/window_render_object.hpp b/include/cru/ui/render/window_render_object.hpp index 8f35ad80..ae95b8ee 100644 --- a/include/cru/ui/render/window_render_object.hpp +++ b/include/cru/ui/render/window_render_object.hpp @@ -26,9 +26,6 @@ class WindowRenderObject : public RenderObject { RenderObject* HitTest(const Point& point) override; protected: - void OnAddChild(RenderObject* new_child, int position) override; - void OnRemoveChild(RenderObject* new_child, int position) override; - Size OnMeasureContent(const Size& available_size) override; void OnLayoutContent(const Rect& content_rect) override; diff --git a/include/cru/ui/ui_event.hpp b/include/cru/ui/ui_event.hpp index c9ed1b80..147d3da6 100644 --- a/include/cru/ui/ui_event.hpp +++ b/include/cru/ui/ui_event.hpp @@ -100,7 +100,7 @@ class MouseButtonEventArgs : public MouseEventArgs { MouseButtonEventArgs& operator=(MouseButtonEventArgs&& other) = default; ~MouseButtonEventArgs() override = default; - MouseButton GetMouseButton() const { return button_; } + MouseButton GetButton() const { return button_; } private: MouseButton button_; |