diff --git a/src/core/Event.hpp b/src/core/Event.hpp index 8dd56d8e..3eaf7331 100644 --- a/src/core/Event.hpp +++ b/src/core/Event.hpp @@ -140,7 +140,7 @@ namespace easy2d struct Event { EventType type; - bool has_target; + Node* target; union { @@ -149,14 +149,6 @@ namespace easy2d WindowEvent win; }; - Event() - : type(0) - , has_target(false) - {} - - Event(EventType type) - : type(type) - , has_target(false) - {} + Event(EventType type = 0) : type(type), target(nullptr) {} }; } diff --git a/src/core/Node.cpp b/src/core/Node.cpp index ff089c33..d8f8273f 100644 --- a/src/core/Node.cpp +++ b/src/core/Node.cpp @@ -43,6 +43,9 @@ namespace easy2d Node::Node() : visible_(true) , pause_(false) + , hover_(false) + , pressed_(false) + , responsible_(false) , dirty_transform_(false) , dirty_transform_inverse_(false) , parent_(nullptr) @@ -128,13 +131,13 @@ namespace easy2d child->Dispatch(evt); } - if (MouseEvent::Check(evt.type)) + if (responsible_ && MouseEvent::Check(evt.type)) { if (evt.type == MouseEvent::Move) { - if (!evt.has_target && ContainsPoint(Point{ evt.mouse.x, evt.mouse.y })) + if (!evt.target && ContainsPoint(Point{ evt.mouse.x, evt.mouse.y })) { - evt.has_target = true; + evt.target = this; if (!hover_) { @@ -142,7 +145,7 @@ namespace easy2d Event hover = evt; hover.type = MouseEvent::Hover; - Dispatch(hover); + EventDispatcher::Dispatch(hover); } } else if (hover_) @@ -151,23 +154,26 @@ namespace easy2d pressed_ = false; Event out = evt; + out.target = this; out.type = MouseEvent::Out; - Dispatch(out); + EventDispatcher::Dispatch(out); } } if (evt.type == MouseEvent::Down && hover_) { pressed_ = true; + evt.target = this; } if (evt.type == MouseEvent::Up && pressed_) { pressed_ = false; + evt.target = this; Event click = evt; click.type = MouseEvent::Click; - Dispatch(click); + EventDispatcher::Dispatch(click); } } @@ -584,12 +590,17 @@ namespace easy2d children_.Clear(); } + void Node::SetResponsible(bool enable) + { + responsible_ = enable; + } + bool Node::ContainsPoint(const Point& point) const { if (size_.x == 0.f || size_.y == 0.f) return false; - math::Vector2 local = GetTransformInverseMatrix().Transform(point); + Point local = GetTransformInverseMatrix().Transform(point); return GetBounds().ContainsPoint(local); } } diff --git a/src/core/Node.h b/src/core/Node.h index b636ac7b..688db051 100644 --- a/src/core/Node.h +++ b/src/core/Node.h @@ -287,6 +287,12 @@ namespace easy2d int zorder ); + // 是否可响应 (鼠标 Hover | Out | Click 消息) + // 默认为 false + void SetResponsible( + bool enable + ); + // 判断点是否在节点内 bool ContainsPoint( const Point& point @@ -363,6 +369,7 @@ namespace easy2d bool visible_; bool hover_; bool pressed_; + bool responsible_; bool pause_; int z_order_; float opacity_; diff --git a/src/math/Rect.hpp b/src/math/Rect.hpp index 22aaefc0..00ba1459 100644 --- a/src/math/Rect.hpp +++ b/src/math/Rect.hpp @@ -99,7 +99,7 @@ namespace easy2d // 判断点是否在矩形内 inline bool ContainsPoint(const Vector2& point) const { - return point.x >= origin.x && point.x <= (origin.y + size.y) && + return point.x >= origin.x && point.x <= (origin.x + size.x) && point.y >= origin.y && point.y <= (origin.y + size.y); } diff --git a/src/ui/Button.cpp b/src/ui/Button.cpp index ed8ebbaf..1c5a557e 100644 --- a/src/ui/Button.cpp +++ b/src/ui/Button.cpp @@ -30,6 +30,8 @@ namespace easy2d , click_callback_(nullptr) , status_(Status::Normal) { + SetResponsible(true); + AddListener(MouseEvent::Hover, Closure(this, &Button::UpdateStatus)); AddListener(MouseEvent::Out, Closure(this, &Button::UpdateStatus)); AddListener(MouseEvent::Down, Closure(this, &Button::UpdateStatus)); @@ -100,7 +102,7 @@ namespace easy2d { E2D_ASSERT(MouseEvent::Check(evt.type)); - if (enabled_) + if (enabled_ && (evt.target == this)) { if (evt.type == MouseEvent::Hover) {