resolve #17, 修复按钮消息传递问题

This commit is contained in:
Nomango 2018-08-12 14:05:12 +08:00
parent 91b7458766
commit 6382adb1b1
7 changed files with 70 additions and 107 deletions

View File

@ -66,7 +66,7 @@ void e2d::Game::start()
}
else
{
// ID2D1HwndRenderTarget 开启了垂直同步,在渲染时会等待显示器刷新,
// ID2D1HwndRenderTarget 开启了垂直同步在渲染时会等待显示器刷新,
// 它起到了非常稳定的延时作用,所以大部分时候不需要手动挂起线程进行延时。
// 下面的代码仅在一些情况下(例如窗口最小化时)挂起线程,防止占用过高 CPU 。
int wait = minInterval - dur.milliseconds();

View File

@ -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)

View File

@ -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()

View File

@ -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()

View File

@ -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)

View File

@ -983,12 +983,12 @@ public:
virtual void onExit() {}
// 梓囚<E6A293>
// 说明:返回 false 将阻止消息继续传递
virtual bool onKeyEvent(KeyEvent e) { return true; }
// 说明:返回 true 将阻止消息继续传递
virtual bool onKeyEvent(KeyEvent e) { return false; }
// 報炎<E5A0B1>
// 说明:返回 false 将阻止消息继续传递
virtual bool onMouseEvent(MouseEvent e) { return true; }
// 说明:返回 true 将阻止消息继续传递
virtual bool onMouseEvent(MouseEvent e) { return false; }
// 当弉<E5BD93>
virtual void onCollision(Collision collision) { }

View File

@ -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;