diff --git a/core/Base/Game.cpp b/core/Base/Game.cpp index 1587901f..59ccea5a 100644 --- a/core/Base/Game.cpp +++ b/core/Base/Game.cpp @@ -66,7 +66,7 @@ void e2d::Game::start() } else { - // ID2D1HwndRenderTarget 开启了垂直同步,在渲染时会等待显示器刷新, + // ID2D1HwndRenderTarget 开启了垂直同步,在渲染时会等待显示器刷新, // 它起到了非常稳定的延时作用,所以大部分时候不需要手动挂起线程进行延时。 // 下面的代码仅在一些情况下(例如窗口最小化时)挂起线程,防止占用过高 CPU 。 int wait = minInterval - dur.milliseconds(); diff --git a/core/Common/Scene.cpp b/core/Common/Scene.cpp index ecfdd39b..fac317d7 100644 --- a/core/Common/Scene.cpp +++ b/core/Common/Scene.cpp @@ -47,17 +47,17 @@ void e2d::Scene::update() void e2d::Scene::dispatch(const MouseEvent & e) { if (onMouseEvent(e)) - { - _root->dispatch(e); - } + return; + + _root->dispatch(e); } void e2d::Scene::dispatch(const KeyEvent & e) { if (onKeyEvent(e)) - { - _root->dispatch(e); - } + return; + + _root->dispatch(e); } void e2d::Scene::setAutoUpdate(bool bAutoUpdate) diff --git a/core/Node/Button.cpp b/core/Node/Button.cpp index cbea2340..04d13ac1 100644 --- a/core/Node/Button.cpp +++ b/core/Node/Button.cpp @@ -3,6 +3,19 @@ #define SAFE_SET(pointer, func, ...) if (pointer) { pointer->##func(__VA_ARGS__); } +#define SET_BUTTON_NODE(Old, New) \ + if (New != Old) \ + { \ + if (Old) this->removeChild(Old); \ + if (New) \ + { \ + New->setPivot(_pivotX, _pivotY); \ + this->addChild(New); \ + } \ + Old = New; \ + _updateVisible(); \ + } \ + e2d::Button::Button() : _func(nullptr) @@ -85,84 +98,26 @@ bool e2d::Button::isEnable() const void e2d::Button::setNormal(Node * normal) { - if (normal != _normal) + SET_BUTTON_NODE(_normal, normal); + if (normal) { - // 移除旧的 - if (_normal) - { - this->removeChild(_normal); - } - // 添加新的 - if (normal) - { - normal->setPivot(_pivotX, _pivotY); - this->addChild(normal); - this->setSize(normal->getWidth(), normal->getHeight()); - } - _normal = normal; - - _updateVisible(); + this->setSize(normal->getWidth(), normal->getHeight()); } } void e2d::Button::setMouseOver(Node * mouseover) { - if (mouseover != _normal) - { - // 移除旧的 - if (_mouseover) - { - this->removeChild(_mouseover); - } - // 添加新的 - if (mouseover) - { - mouseover->setPivot(_pivotX, _pivotY); - this->addChild(mouseover); - } - _mouseover = mouseover; - _updateVisible(); - } + SET_BUTTON_NODE(_mouseover, mouseover); } void e2d::Button::setSelected(Node * selected) { - if (selected != _normal) - { - // 移除旧的 - if (_selected) - { - this->removeChild(_selected); - } - // 添加新的 - if (selected) - { - selected->setPivot(_pivotX, _pivotY); - this->addChild(selected); - } - _selected = selected; - _updateVisible(); - } + SET_BUTTON_NODE(_selected, selected); } void e2d::Button::setDisabled(Node * disabled) { - if (disabled != _normal) - { - // 移除旧的 - if (_disabled) - { - this->removeChild(_disabled); - } - // 添加新的 - if (disabled) - { - disabled->setPivot(_pivotX, _pivotY); - this->addChild(disabled); - } - _disabled = disabled; - _updateVisible(); - } + SET_BUTTON_NODE(_disabled, disabled); } void e2d::Button::setEnabled(bool enabled) @@ -188,21 +143,25 @@ void e2d::Button::setPivot(float pivotX, float pivotY) SAFE_SET(_disabled, setPivot, pivotX, pivotY); } -void e2d::Button::dispatch(const MouseEvent & e) +bool e2d::Button::dispatch(const MouseEvent & e) { if (_enabled && _visible && _normal) { bool contains = _normal->containsPoint(e.getPos()); if (e.getType() == MouseEvent::Type::LeftUp && _isSelected && contains) { - _isSelected = false; _runCallback(); + _isSelected = false; _setStatus(Status::Normal); + return true; } else if (e.getType() == MouseEvent::Type::LeftDown) { _isSelected = contains; _setStatus(contains ? Status::Selected : Status::Normal); + + if (contains) + return true; } else if (e.getType() == MouseEvent::Type::LeftUp) { @@ -211,6 +170,7 @@ void e2d::Button::dispatch(const MouseEvent & e) else if (e.getType() == MouseEvent::Type::Move && _isSelected && contains) { _setStatus(Status::Selected); + return true; } else { @@ -218,11 +178,15 @@ void e2d::Button::dispatch(const MouseEvent & e) { _isSelected = false; } + _setStatus(contains ? Status::Mouseover : Status::Normal); + + if (contains) + return true; } } - Node::dispatch(e); + return Node::dispatch(e); } void e2d::Button::_render() diff --git a/core/Node/Node.cpp b/core/Node/Node.cpp index caf3de17..fc0e77a4 100644 --- a/core/Node/Node.cpp +++ b/core/Node/Node.cpp @@ -274,32 +274,28 @@ void e2d::Node::updateTransform() } } -void e2d::Node::dispatch(const MouseEvent & e) +bool e2d::Node::dispatch(const MouseEvent & e) { - if (!onMouseEvent(e)) - return; + if (onMouseEvent(e)) + return true; - if (!_children.empty()) - { - for (auto child : _children) - { - child->dispatch(e); - } - } + for (auto iter = _children.rbegin(); iter != _children.rend(); ++iter) + if ((*iter)->dispatch(e)) + return true; + + return false; } -void e2d::Node::dispatch(const KeyEvent & e) +bool e2d::Node::dispatch(const KeyEvent & e) { - if (!onKeyEvent(e)) - return; + if (onKeyEvent(e)) + return true; - if (!_children.empty()) - { - for (auto child : _children) - { - child->dispatch(e); - } - } + for (auto iter = _children.rbegin(); iter != _children.rend(); ++iter) + if ((*iter)->dispatch(e)) + return true; + + return false; } void e2d::Node::_sortChildren() diff --git a/core/Node/ToggleButton.cpp b/core/Node/ToggleButton.cpp index d824b4d6..467db624 100644 --- a/core/Node/ToggleButton.cpp +++ b/core/Node/ToggleButton.cpp @@ -12,7 +12,7 @@ this->addChild(New); \ } \ Old = New; \ - _updateStatus(); \ + _updateStatus(); \ _updateVisible(); \ } \ @@ -125,7 +125,10 @@ void e2d::ToggleButton::setChecked(bool checked) void e2d::ToggleButton::setNormal(Node * normal) { SET_BUTTON_NODE(_normalOn, normal); - this->setSize(_normalOn->getWidth(), _normalOn->getHeight()); + if (normal) + { + this->setSize(normal->getWidth(), normal->getHeight()); + } } void e2d::ToggleButton::setMouseOver(Node * mouseover) diff --git a/core/e2dcommon.h b/core/e2dcommon.h index 9ebe3c44..0ac13060 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -983,12 +983,12 @@ public: virtual void onExit() {} // 按键消息 - // 说明:返回 false 将阻止消息继续传递 - virtual bool onKeyEvent(KeyEvent e) { return true; } + // 说明:返回 true 将阻止消息继续传递 + virtual bool onKeyEvent(KeyEvent e) { return false; } // 鼠标消息 - // 说明:返回 false 将阻止消息继续传递 - virtual bool onMouseEvent(MouseEvent e) { return true; } + // 说明:返回 true 将阻止消息继续传递 + virtual bool onMouseEvent(MouseEvent e) { return false; } // 碰撞消息 virtual void onCollision(Collision collision) { } diff --git a/core/e2dnode.h b/core/e2dnode.h index 5f386225..21814188 100644 --- a/core/e2dnode.h +++ b/core/e2dnode.h @@ -53,12 +53,12 @@ public: virtual void onRender() const {} // 按键消息 - // 说明:返回 false 将阻止消息继续传递 - virtual bool onKeyEvent(KeyEvent e) { return true; } + // 说明:返回 true 将阻止消息向下传递 + virtual bool onKeyEvent(KeyEvent e) { return false; } // 鼠标消息 - // 说明:返回 false 将阻止消息继续传递 - virtual bool onMouseEvent(MouseEvent e) { return true; } + // 说明:返回 true 将阻止消息向下传递 + virtual bool onMouseEvent(MouseEvent e) { return false; } // 碰撞消息 virtual void onCollision(Collision collision) { } @@ -402,12 +402,12 @@ public: void updateTransform(); // 分发鼠标消息 - virtual void dispatch( + virtual bool dispatch( const MouseEvent& e ); // 分发按键消息 - virtual void dispatch( + virtual bool dispatch( const KeyEvent& e ); @@ -827,7 +827,7 @@ public: ) override; // 分发鼠标消息 - virtual void dispatch( + virtual bool dispatch( const MouseEvent& e ) override;