消息处理方式更新

This commit is contained in:
Nomango 2018-08-13 00:10:27 +08:00
parent bdbe00e228
commit 61b384767e
5 changed files with 31 additions and 48 deletions

View File

@ -3,8 +3,7 @@
#include "..\e2dmanager.h" #include "..\e2dmanager.h"
e2d::Scene::Scene() e2d::Scene::Scene()
: _autoUpdate(true) : _root(nullptr)
, _root(nullptr)
{ {
_root = new (e2d::autorelease) Node(); _root = new (e2d::autorelease) Node();
_root->retain(); _root->retain();
@ -37,16 +36,13 @@ void e2d::Scene::render()
void e2d::Scene::update() void e2d::Scene::update()
{ {
if (_autoUpdate) this->onUpdate();
{
onUpdate();
}
_root->_update(); _root->_update();
} }
void e2d::Scene::dispatch(const MouseEvent & e) void e2d::Scene::dispatch(const MouseEvent & e)
{ {
if (onMouseEvent(e)) if (onEvent(e))
return; return;
_root->dispatch(e); _root->dispatch(e);
@ -54,17 +50,12 @@ void e2d::Scene::dispatch(const MouseEvent & e)
void e2d::Scene::dispatch(const KeyEvent & e) void e2d::Scene::dispatch(const KeyEvent & e)
{ {
if (onKeyEvent(e)) if (onEvent(e))
return; return;
_root->dispatch(e); _root->dispatch(e);
} }
void e2d::Scene::setAutoUpdate(bool bAutoUpdate)
{
_autoUpdate = bAutoUpdate;
}
void e2d::Scene::add(Node * child, int order /* = 0 */) void e2d::Scene::add(Node * child, int order /* = 0 */)
{ {
_root->addChild(child, order); _root->addChild(child, order);

View File

@ -77,12 +77,12 @@ void e2d::CollisionManager::__updateCollider(Collider* collider)
auto passiveNode = passive->getNode(); auto passiveNode = passive->getNode();
// 触发两次碰撞事件 // 触发两次碰撞事件
Collision activeCollision(passiveNode, relation); Collision activeCollision(passiveNode, relation);
activeNode->getParentScene()->onCollision(activeCollision); activeNode->getParentScene()->onEvent(activeCollision);
activeNode->onCollision(activeCollision); activeNode->onEvent(activeCollision);
Collision passiveCollision(activeNode, passive->getRelationWith(collider)); Collision passiveCollision(activeNode, passive->getRelationWith(collider));
passiveNode->getParentScene()->onCollision(passiveCollision); passiveNode->getParentScene()->onEvent(passiveCollision);
passiveNode->onCollision(passiveCollision); passiveNode->onEvent(passiveCollision);
} }
} }
} }

View File

@ -63,7 +63,6 @@ e2d::Node::Node()
, _clipEnabled(false) , _clipEnabled(false)
, _needSort(false) , _needSort(false)
, _needTransform(false) , _needTransform(false)
, _autoUpdate(true)
, _positionFixed(false) , _positionFixed(false)
, _collider(this) , _collider(this)
, _extrapolate(Property::Origin) , _extrapolate(Property::Origin)
@ -127,7 +126,7 @@ void e2d::Node::_updateSelf()
} }
} }
if (_autoUpdate && !Game::getInstance()->isPaused()) if (!Game::getInstance()->isPaused())
{ {
this->onUpdate(); this->onUpdate();
} }
@ -278,12 +277,17 @@ bool e2d::Node::dispatch(const MouseEvent & e)
{ {
if (_visible) if (_visible)
{ {
if (onMouseEvent(e)) if (onEvent(e))
return true; return true;
for (auto iter = _children.rbegin(); iter != _children.rend(); ++iter) for (size_t i = _children.size(); i >= 0 && i < _children.size(); --i)
if ((*iter)->dispatch(e)) {
if (_children[i]->dispatch(e))
return true; return true;
if (i == 0)
break;
}
} }
return false; return false;
@ -293,12 +297,17 @@ bool e2d::Node::dispatch(const KeyEvent & e)
{ {
if (_visible) if (_visible)
{ {
if (onKeyEvent(e)) if (onEvent(e))
return true; return true;
for (auto iter = _children.rbegin(); iter != _children.rend(); ++iter) for (size_t i = _children.size(); i >= 0 && i < _children.size(); --i)
if ((*iter)->dispatch(e)) {
if (_children[i]->dispatch(e))
return true; return true;
if (i == 0)
break;
}
} }
return false; return false;
@ -957,11 +966,6 @@ bool e2d::Node::intersects(Node * node)
relation != D2D1_GEOMETRY_RELATION_DISJOINT; relation != D2D1_GEOMETRY_RELATION_DISJOINT;
} }
void e2d::Node::setAutoUpdate(bool bAutoUpdate)
{
_autoUpdate = bAutoUpdate;
}
void e2d::Node::resumeAllActions() void e2d::Node::resumeAllActions()
{ {
ActionManager::getInstance()->resumeAllBindedWith(this); ActionManager::getInstance()->resumeAllBindedWith(this);

View File

@ -984,14 +984,14 @@ public:
// 按键消息 // 按键消息
// 说明:返回 true 将阻止消息继续传递 // 说明:返回 true 将阻止消息继续传递
virtual bool onKeyEvent(KeyEvent e) { return false; } virtual bool onEvent(KeyEvent e) { return false; }
// 鼠标消息 // 鼠标消息
// 说明:返回 true 将阻止消息继续传递 // 说明:返回 true 将阻止消息继续传递
virtual bool onMouseEvent(MouseEvent e) { return false; } virtual bool onEvent(MouseEvent e) { return false; }
// 碰撞消息 // 碰撞消息
virtual void onCollision(Collision collision) { } virtual void onEvent(Collision collision) { }
// 关闭窗口 // 关闭窗口
// 说明:返回 false 将阻止窗口关闭 // 说明:返回 false 将阻止窗口关闭
@ -1000,11 +1000,6 @@ public:
// 重写这个函数,它将在每一帧画面刷新时执行 // 重写这个函数,它将在每一帧画面刷新时执行
virtual void onUpdate() {} virtual void onUpdate() {}
// 开启或禁用 onUpdate 函数
void setAutoUpdate(
bool bAutoUpdate
);
// 添加节点到场景 // 添加节点到场景
void add( void add(
Node * child, /* 要添加的节点 */ Node * child, /* 要添加的节点 */
@ -1058,7 +1053,6 @@ protected:
E2D_DISABLE_COPY(Scene); E2D_DISABLE_COPY(Scene);
protected: protected:
bool _autoUpdate;
Node * _root; Node * _root;
}; };

View File

@ -54,14 +54,14 @@ public:
// 按键消息 // 按键消息
// 说明:返回 true 将阻止消息向下传递 // 说明:返回 true 将阻止消息向下传递
virtual bool onKeyEvent(KeyEvent e) { return false; } virtual bool onEvent(KeyEvent e) { return false; }
// 鼠标消息 // 鼠标消息
// 说明:返回 true 将阻止消息向下传递 // 说明:返回 true 将阻止消息向下传递
virtual bool onMouseEvent(MouseEvent e) { return false; } virtual bool onEvent(MouseEvent e) { return false; }
// 碰撞消息 // 碰撞消息
virtual void onCollision(Collision collision) { } virtual void onEvent(Collision collision) { }
// 获取节点显示状态 // 获取节点显示状态
virtual bool isVisible() const; virtual bool isVisible() const;
@ -188,11 +188,6 @@ public:
bool value bool value
); );
// 开启或禁用 onUpdate 函数
void setAutoUpdate(
bool bAutoUpdate
);
// 设置节点名称 // 设置节点名称
void setName( void setName(
const String& name const String& name
@ -458,7 +453,6 @@ protected:
float _pivotY; float _pivotY;
int _order; int _order;
bool _visible; bool _visible;
bool _autoUpdate;
bool _clipEnabled; bool _clipEnabled;
bool _needSort; bool _needSort;
bool _needTransform; bool _needTransform;