support responsible switch now

This commit is contained in:
Nomango 2019-01-26 13:09:58 +08:00 committed by Nomango
parent f545a15365
commit 1534c15472
5 changed files with 31 additions and 19 deletions

View File

@ -140,7 +140,7 @@ namespace easy2d
struct Event struct Event
{ {
EventType type; EventType type;
bool has_target; Node* target;
union union
{ {
@ -149,14 +149,6 @@ namespace easy2d
WindowEvent win; WindowEvent win;
}; };
Event() Event(EventType type = 0) : type(type), target(nullptr) {}
: type(0)
, has_target(false)
{}
Event(EventType type)
: type(type)
, has_target(false)
{}
}; };
} }

View File

@ -43,6 +43,9 @@ namespace easy2d
Node::Node() Node::Node()
: visible_(true) : visible_(true)
, pause_(false) , pause_(false)
, hover_(false)
, pressed_(false)
, responsible_(false)
, dirty_transform_(false) , dirty_transform_(false)
, dirty_transform_inverse_(false) , dirty_transform_inverse_(false)
, parent_(nullptr) , parent_(nullptr)
@ -128,13 +131,13 @@ namespace easy2d
child->Dispatch(evt); child->Dispatch(evt);
} }
if (MouseEvent::Check(evt.type)) if (responsible_ && MouseEvent::Check(evt.type))
{ {
if (evt.type == MouseEvent::Move) 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_) if (!hover_)
{ {
@ -142,7 +145,7 @@ namespace easy2d
Event hover = evt; Event hover = evt;
hover.type = MouseEvent::Hover; hover.type = MouseEvent::Hover;
Dispatch(hover); EventDispatcher::Dispatch(hover);
} }
} }
else if (hover_) else if (hover_)
@ -151,23 +154,26 @@ namespace easy2d
pressed_ = false; pressed_ = false;
Event out = evt; Event out = evt;
out.target = this;
out.type = MouseEvent::Out; out.type = MouseEvent::Out;
Dispatch(out); EventDispatcher::Dispatch(out);
} }
} }
if (evt.type == MouseEvent::Down && hover_) if (evt.type == MouseEvent::Down && hover_)
{ {
pressed_ = true; pressed_ = true;
evt.target = this;
} }
if (evt.type == MouseEvent::Up && pressed_) if (evt.type == MouseEvent::Up && pressed_)
{ {
pressed_ = false; pressed_ = false;
evt.target = this;
Event click = evt; Event click = evt;
click.type = MouseEvent::Click; click.type = MouseEvent::Click;
Dispatch(click); EventDispatcher::Dispatch(click);
} }
} }
@ -584,12 +590,17 @@ namespace easy2d
children_.Clear(); children_.Clear();
} }
void Node::SetResponsible(bool enable)
{
responsible_ = enable;
}
bool Node::ContainsPoint(const Point& point) const bool Node::ContainsPoint(const Point& point) const
{ {
if (size_.x == 0.f || size_.y == 0.f) if (size_.x == 0.f || size_.y == 0.f)
return false; return false;
math::Vector2 local = GetTransformInverseMatrix().Transform(point); Point local = GetTransformInverseMatrix().Transform(point);
return GetBounds().ContainsPoint(local); return GetBounds().ContainsPoint(local);
} }
} }

View File

@ -287,6 +287,12 @@ namespace easy2d
int zorder int zorder
); );
// 是否可响应 (鼠标 Hover | Out | Click 消息)
// 默认为 false
void SetResponsible(
bool enable
);
// 判断点是否在节点内 // 判断点是否在节点内
bool ContainsPoint( bool ContainsPoint(
const Point& point const Point& point
@ -363,6 +369,7 @@ namespace easy2d
bool visible_; bool visible_;
bool hover_; bool hover_;
bool pressed_; bool pressed_;
bool responsible_;
bool pause_; bool pause_;
int z_order_; int z_order_;
float opacity_; float opacity_;

View File

@ -99,7 +99,7 @@ namespace easy2d
// 判断点是否在矩形内 // 判断点是否在矩形内
inline bool ContainsPoint(const Vector2& point) const 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); point.y >= origin.y && point.y <= (origin.y + size.y);
} }

View File

@ -30,6 +30,8 @@ namespace easy2d
, click_callback_(nullptr) , click_callback_(nullptr)
, status_(Status::Normal) , status_(Status::Normal)
{ {
SetResponsible(true);
AddListener(MouseEvent::Hover, Closure(this, &Button::UpdateStatus)); AddListener(MouseEvent::Hover, Closure(this, &Button::UpdateStatus));
AddListener(MouseEvent::Out, Closure(this, &Button::UpdateStatus)); AddListener(MouseEvent::Out, Closure(this, &Button::UpdateStatus));
AddListener(MouseEvent::Down, Closure(this, &Button::UpdateStatus)); AddListener(MouseEvent::Down, Closure(this, &Button::UpdateStatus));
@ -100,7 +102,7 @@ namespace easy2d
{ {
E2D_ASSERT(MouseEvent::Check(evt.type)); E2D_ASSERT(MouseEvent::Check(evt.type));
if (enabled_) if (enabled_ && (evt.target == this))
{ {
if (evt.type == MouseEvent::Hover) if (evt.type == MouseEvent::Hover)
{ {