增加EventHandler类,用于处理消息;移除Node::onUpdate方法
This commit is contained in:
parent
61b384767e
commit
857f13230f
|
|
@ -34,26 +34,22 @@ void e2d::Scene::render()
|
|||
}
|
||||
}
|
||||
|
||||
void e2d::Scene::update()
|
||||
{
|
||||
this->onUpdate();
|
||||
_root->_update();
|
||||
}
|
||||
|
||||
void e2d::Scene::dispatch(const MouseEvent & e)
|
||||
{
|
||||
if (onEvent(e))
|
||||
return;
|
||||
auto dispatcher = dynamic_cast<EventHandler*>(this);
|
||||
if (dispatcher)
|
||||
dispatcher->handle(e);
|
||||
|
||||
_root->dispatch(e);
|
||||
_root->dispatch(e, false);
|
||||
}
|
||||
|
||||
void e2d::Scene::dispatch(const KeyEvent & e)
|
||||
{
|
||||
if (onEvent(e))
|
||||
return;
|
||||
auto dispatcher = dynamic_cast<EventHandler*>(this);
|
||||
if (dispatcher)
|
||||
dispatcher->handle(e);
|
||||
|
||||
_root->dispatch(e);
|
||||
_root->dispatch(e, false);
|
||||
}
|
||||
|
||||
void e2d::Scene::add(Node * child, int order /* = 0 */)
|
||||
|
|
|
|||
|
|
@ -77,12 +77,16 @@ void e2d::CollisionManager::__updateCollider(Collider* collider)
|
|||
auto passiveNode = passive->getNode();
|
||||
// 触发两次碰撞事件
|
||||
Collision activeCollision(passiveNode, relation);
|
||||
activeNode->getParentScene()->onEvent(activeCollision);
|
||||
activeNode->onEvent(activeCollision);
|
||||
if (dynamic_cast<EventHandler*>(activeNode->getParentScene()))
|
||||
dynamic_cast<EventHandler*>(activeNode->getParentScene())->handle(activeCollision);
|
||||
if (dynamic_cast<EventHandler*>(activeNode))
|
||||
dynamic_cast<EventHandler*>(activeNode)->handle(activeCollision);
|
||||
|
||||
Collision passiveCollision(activeNode, passive->getRelationWith(collider));
|
||||
passiveNode->getParentScene()->onEvent(passiveCollision);
|
||||
passiveNode->onEvent(passiveCollision);
|
||||
if (dynamic_cast<EventHandler*>(passiveNode->getParentScene()))
|
||||
dynamic_cast<EventHandler*>(passiveNode->getParentScene())->handle(passiveCollision);
|
||||
if (dynamic_cast<EventHandler*>(passiveNode))
|
||||
dynamic_cast<EventHandler*>(passiveNode)->handle(passiveCollision);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -120,9 +120,6 @@ bool e2d::SceneManager::isTransitioning()
|
|||
|
||||
void e2d::SceneManager::update()
|
||||
{
|
||||
if (_currScene) _currScene->update();
|
||||
if (_nextScene) _nextScene->update();
|
||||
|
||||
if (_transition)
|
||||
{
|
||||
_transition->update();
|
||||
|
|
|
|||
|
|
@ -143,9 +143,9 @@ void e2d::Button::setPivot(float pivotX, float pivotY)
|
|||
SAFE_SET(_disabled, setPivot, pivotX, pivotY);
|
||||
}
|
||||
|
||||
bool e2d::Button::dispatch(const MouseEvent & e)
|
||||
bool e2d::Button::dispatch(const MouseEvent & e, bool handled)
|
||||
{
|
||||
if (_enabled && _visible && _normal)
|
||||
if (!handled && _enabled && _visible && _normal)
|
||||
{
|
||||
bool contains = _normal->containsPoint(e.getPos());
|
||||
if (e.getType() == MouseEvent::Type::LeftUp && _isSelected && contains)
|
||||
|
|
@ -186,7 +186,7 @@ bool e2d::Button::dispatch(const MouseEvent & e)
|
|||
}
|
||||
}
|
||||
|
||||
return Node::dispatch(e);
|
||||
return Node::dispatch(e, handled);
|
||||
}
|
||||
|
||||
void e2d::Button::_render()
|
||||
|
|
|
|||
|
|
@ -78,60 +78,6 @@ e2d::Node::~Node()
|
|||
}
|
||||
}
|
||||
|
||||
void e2d::Node::_update()
|
||||
{
|
||||
if (!_visible)
|
||||
return;
|
||||
|
||||
if (_children.empty())
|
||||
{
|
||||
_updateSelf();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 遍历子节点
|
||||
size_t i;
|
||||
for (i = 0; i < _children.size(); ++i)
|
||||
{
|
||||
auto child = _children[i];
|
||||
// 访问 Order 小于零的节点
|
||||
if (child->getOrder() < 0)
|
||||
{
|
||||
child->_update();
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_updateSelf();
|
||||
|
||||
// 访问其他节点
|
||||
for (; i < _children.size(); ++i)
|
||||
_children[i]->_update();
|
||||
}
|
||||
}
|
||||
|
||||
void e2d::Node::_updateSelf()
|
||||
{
|
||||
if (_needTransform)
|
||||
{
|
||||
updateTransform();
|
||||
if (_collider.isEnabled() &&
|
||||
_collider.isCollisionNotify() &&
|
||||
_collider.getShape() != Collider::Shape::None)
|
||||
{
|
||||
CollisionManager::getInstance()->__updateCollider(&_collider);
|
||||
}
|
||||
}
|
||||
|
||||
if (!Game::getInstance()->isPaused())
|
||||
{
|
||||
this->onUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
void e2d::Node::_render()
|
||||
{
|
||||
if (!_visible)
|
||||
|
|
@ -139,6 +85,7 @@ void e2d::Node::_render()
|
|||
|
||||
// 更新转换矩阵
|
||||
updateTransform();
|
||||
|
||||
// 保留差别属性
|
||||
_extrapolate = this->getProperty();
|
||||
|
||||
|
|
@ -154,10 +101,8 @@ void e2d::Node::_render()
|
|||
|
||||
if (_children.empty())
|
||||
{
|
||||
// 转换渲染器的转换矩阵
|
||||
pRT->SetTransform(_finalMatri);
|
||||
// 渲染自身
|
||||
this->onRender();
|
||||
this->draw();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -179,10 +124,8 @@ void e2d::Node::_render()
|
|||
}
|
||||
}
|
||||
|
||||
// 转换渲染器的转换矩阵
|
||||
pRT->SetTransform(_finalMatri);
|
||||
// 渲染自身
|
||||
this->onRender();
|
||||
this->draw();
|
||||
|
||||
// 访问剩余节点
|
||||
for (; i < _children.size(); ++i)
|
||||
|
|
@ -263,54 +206,51 @@ void e2d::Node::updateTransform()
|
|||
_finalMatri = _finalMatri * _parent->_initialMatri;
|
||||
}
|
||||
|
||||
// 更新碰撞体
|
||||
_collider.recreate();
|
||||
|
||||
// 通知子节点进行转换
|
||||
for (const auto& child : _children)
|
||||
{
|
||||
child->_needTransform = true;
|
||||
}
|
||||
|
||||
// ¸üÐÂÅöײÌå
|
||||
_collider.recreate();
|
||||
|
||||
if (_collider.isEnabled() &&
|
||||
_collider.isCollisionNotify() &&
|
||||
_collider.getShape() != Collider::Shape::None)
|
||||
{
|
||||
CollisionManager::getInstance()->__updateCollider(&_collider);
|
||||
}
|
||||
}
|
||||
|
||||
bool e2d::Node::dispatch(const MouseEvent & e)
|
||||
bool e2d::Node::dispatch(const MouseEvent & e, bool handled)
|
||||
{
|
||||
if (_visible)
|
||||
{
|
||||
if (onEvent(e))
|
||||
return true;
|
||||
auto dispatcher = dynamic_cast<EventHandler*>(this);
|
||||
if (dispatcher)
|
||||
dispatcher->handle(e);
|
||||
|
||||
for (size_t i = _children.size(); i >= 0 && i < _children.size(); --i)
|
||||
{
|
||||
if (_children[i]->dispatch(e))
|
||||
return true;
|
||||
|
||||
if (i == 0)
|
||||
break;
|
||||
}
|
||||
for (auto riter = _children.crbegin(); riter != _children.crend(); ++riter)
|
||||
handled = (*riter)->dispatch(e, handled);
|
||||
}
|
||||
|
||||
return false;
|
||||
return handled;
|
||||
}
|
||||
|
||||
bool e2d::Node::dispatch(const KeyEvent & e)
|
||||
bool e2d::Node::dispatch(const KeyEvent & e, bool handled)
|
||||
{
|
||||
if (_visible)
|
||||
{
|
||||
if (onEvent(e))
|
||||
return true;
|
||||
auto dispatcher = dynamic_cast<EventHandler*>(this);
|
||||
if (dispatcher)
|
||||
dispatcher->handle(e);
|
||||
|
||||
for (size_t i = _children.size(); i >= 0 && i < _children.size(); --i)
|
||||
{
|
||||
if (_children[i]->dispatch(e))
|
||||
return true;
|
||||
|
||||
if (i == 0)
|
||||
break;
|
||||
}
|
||||
for (auto riter = _children.crbegin(); riter != _children.crend(); ++riter)
|
||||
handled = (*riter)->dispatch(e, handled);
|
||||
}
|
||||
|
||||
return false;
|
||||
return handled;
|
||||
}
|
||||
|
||||
void e2d::Node::_sortChildren()
|
||||
|
|
|
|||
|
|
@ -92,7 +92,7 @@ e2d::Image * e2d::Sprite::getImage() const
|
|||
return _image;
|
||||
}
|
||||
|
||||
void e2d::Sprite::onRender() const
|
||||
void e2d::Sprite::draw() const
|
||||
{
|
||||
if (_image && _image->getBitmap())
|
||||
{
|
||||
|
|
|
|||
|
|
@ -287,7 +287,7 @@ void e2d::Text::setOutlineJoin(LineJoin outlineJoin)
|
|||
_style.outlineJoin = outlineJoin;
|
||||
}
|
||||
|
||||
void e2d::Text::onRender() const
|
||||
void e2d::Text::draw() const
|
||||
{
|
||||
if (_textLayout)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -808,6 +808,21 @@ protected:
|
|||
};
|
||||
|
||||
|
||||
// 消息处理
|
||||
class EventHandler
|
||||
{
|
||||
public:
|
||||
// 处理按键消息
|
||||
virtual void handle(KeyEvent e) { }
|
||||
|
||||
// 处理鼠标消息
|
||||
virtual void handle(MouseEvent e) { }
|
||||
|
||||
// 处理碰撞消息
|
||||
virtual void handle(Collision collision) { }
|
||||
};
|
||||
|
||||
|
||||
// 资源
|
||||
class Resource
|
||||
{
|
||||
|
|
@ -982,24 +997,10 @@ public:
|
|||
// 退出场景
|
||||
virtual void onExit() {}
|
||||
|
||||
// 按键消息
|
||||
// 说明:返回 true 将阻止消息继续传递
|
||||
virtual bool onEvent(KeyEvent e) { return false; }
|
||||
|
||||
// 鼠标消息
|
||||
// 说明:返回 true 将阻止消息继续传递
|
||||
virtual bool onEvent(MouseEvent e) { return false; }
|
||||
|
||||
// 碰撞消息
|
||||
virtual void onEvent(Collision collision) { }
|
||||
|
||||
// 关闭窗口
|
||||
// 说明:返回 false 将阻止窗口关闭
|
||||
virtual bool onCloseWindow() { return true; }
|
||||
|
||||
// 重写这个函数,它将在每一帧画面刷新时执行
|
||||
virtual void onUpdate() {}
|
||||
|
||||
// 添加节点到场景
|
||||
void add(
|
||||
Node * child, /* 要添加的节点 */
|
||||
|
|
@ -1036,9 +1037,6 @@ public:
|
|||
// 渲染场景画面
|
||||
void render();
|
||||
|
||||
// 更新场景内容
|
||||
void update();
|
||||
|
||||
// 分发鼠标消息
|
||||
void dispatch(
|
||||
const MouseEvent& e
|
||||
|
|
|
|||
|
|
@ -46,22 +46,8 @@ public:
|
|||
|
||||
virtual ~Node();
|
||||
|
||||
// 更新节点
|
||||
virtual void onUpdate() {}
|
||||
|
||||
// 渲染节点
|
||||
virtual void onRender() const {}
|
||||
|
||||
// 按键消息
|
||||
// 说明:返回 true 将阻止消息向下传递
|
||||
virtual bool onEvent(KeyEvent e) { return false; }
|
||||
|
||||
// 鼠标消息
|
||||
// 说明:返回 true 将阻止消息向下传递
|
||||
virtual bool onEvent(MouseEvent e) { return false; }
|
||||
|
||||
// 碰撞消息
|
||||
virtual void onEvent(Collision collision) { }
|
||||
virtual void draw() const {}
|
||||
|
||||
// 获取节点显示状态
|
||||
virtual bool isVisible() const;
|
||||
|
|
@ -398,23 +384,19 @@ public:
|
|||
|
||||
// 分发鼠标消息
|
||||
virtual bool dispatch(
|
||||
const MouseEvent& e
|
||||
const MouseEvent& e,
|
||||
bool handled
|
||||
);
|
||||
|
||||
// 分发按键消息
|
||||
virtual bool dispatch(
|
||||
const KeyEvent& e
|
||||
const KeyEvent& e,
|
||||
bool handled
|
||||
);
|
||||
|
||||
protected:
|
||||
E2D_DISABLE_COPY(Node);
|
||||
|
||||
// 更新节点
|
||||
virtual void _update();
|
||||
|
||||
// 更新自身
|
||||
virtual void _updateSelf();
|
||||
|
||||
// 渲染节点
|
||||
virtual void _render();
|
||||
|
||||
|
|
@ -461,9 +443,9 @@ protected:
|
|||
Scene * _parentScene;
|
||||
Node * _parent;
|
||||
Property _extrapolate;
|
||||
std::vector<Node*> _children;
|
||||
D2D1::Matrix3x2F _initialMatri;
|
||||
D2D1::Matrix3x2F _finalMatri;
|
||||
std::vector<Node*> _children;
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -521,7 +503,7 @@ public:
|
|||
virtual Image * getImage() const;
|
||||
|
||||
// 渲染精灵
|
||||
virtual void onRender() const override;
|
||||
virtual void draw() const override;
|
||||
|
||||
protected:
|
||||
E2D_DISABLE_COPY(Sprite);
|
||||
|
|
@ -725,7 +707,7 @@ public:
|
|||
);
|
||||
|
||||
// 渲染文字
|
||||
virtual void onRender() const override;
|
||||
virtual void draw() const override;
|
||||
|
||||
protected:
|
||||
E2D_DISABLE_COPY(Text);
|
||||
|
|
@ -822,7 +804,8 @@ public:
|
|||
|
||||
// 分发鼠标消息
|
||||
virtual bool dispatch(
|
||||
const MouseEvent& e
|
||||
const MouseEvent& e,
|
||||
bool handled
|
||||
) override;
|
||||
|
||||
protected:
|
||||
|
|
|
|||
Loading…
Reference in New Issue