resolve #17, 修复按钮消息传递问题
This commit is contained in:
parent
91b7458766
commit
6382adb1b1
|
|
@ -66,7 +66,7 @@ void e2d::Game::start()
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// ID2D1HwndRenderTarget 开启了垂直同步,在渲染时会等待显示器刷新,
|
// ID2D1HwndRenderTarget 开启了垂直同步,在渲染时会等待显示器刷新,
|
||||||
// 它起到了非常稳定的延时作用,所以大部分时候不需要手动挂起线程进行延时。
|
// 它起到了非常稳定的延时作用,所以大部分时候不需要手动挂起线程进行延时。
|
||||||
// 下面的代码仅在一些情况下(例如窗口最小化时)挂起线程,防止占用过高 CPU 。
|
// 下面的代码仅在一些情况下(例如窗口最小化时)挂起线程,防止占用过高 CPU 。
|
||||||
int wait = minInterval - dur.milliseconds();
|
int wait = minInterval - dur.milliseconds();
|
||||||
|
|
|
||||||
|
|
@ -47,18 +47,18 @@ void e2d::Scene::update()
|
||||||
void e2d::Scene::dispatch(const MouseEvent & e)
|
void e2d::Scene::dispatch(const MouseEvent & e)
|
||||||
{
|
{
|
||||||
if (onMouseEvent(e))
|
if (onMouseEvent(e))
|
||||||
{
|
return;
|
||||||
|
|
||||||
_root->dispatch(e);
|
_root->dispatch(e);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void e2d::Scene::dispatch(const KeyEvent & e)
|
void e2d::Scene::dispatch(const KeyEvent & e)
|
||||||
{
|
{
|
||||||
if (onKeyEvent(e))
|
if (onKeyEvent(e))
|
||||||
{
|
return;
|
||||||
|
|
||||||
_root->dispatch(e);
|
_root->dispatch(e);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void e2d::Scene::setAutoUpdate(bool bAutoUpdate)
|
void e2d::Scene::setAutoUpdate(bool bAutoUpdate)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,19 @@
|
||||||
|
|
||||||
#define SAFE_SET(pointer, func, ...) if (pointer) { pointer->##func(__VA_ARGS__); }
|
#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()
|
e2d::Button::Button()
|
||||||
: _func(nullptr)
|
: _func(nullptr)
|
||||||
|
|
@ -85,84 +98,26 @@ bool e2d::Button::isEnable() const
|
||||||
|
|
||||||
void e2d::Button::setNormal(Node * normal)
|
void e2d::Button::setNormal(Node * normal)
|
||||||
{
|
{
|
||||||
if (normal != _normal)
|
SET_BUTTON_NODE(_normal, normal);
|
||||||
{
|
|
||||||
// 移除旧的
|
|
||||||
if (_normal)
|
|
||||||
{
|
|
||||||
this->removeChild(_normal);
|
|
||||||
}
|
|
||||||
// 添加新的
|
|
||||||
if (normal)
|
if (normal)
|
||||||
{
|
{
|
||||||
normal->setPivot(_pivotX, _pivotY);
|
|
||||||
this->addChild(normal);
|
|
||||||
this->setSize(normal->getWidth(), normal->getHeight());
|
this->setSize(normal->getWidth(), normal->getHeight());
|
||||||
}
|
}
|
||||||
_normal = normal;
|
|
||||||
|
|
||||||
_updateVisible();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void e2d::Button::setMouseOver(Node * mouseover)
|
void e2d::Button::setMouseOver(Node * mouseover)
|
||||||
{
|
{
|
||||||
if (mouseover != _normal)
|
SET_BUTTON_NODE(_mouseover, mouseover);
|
||||||
{
|
|
||||||
// 移除旧的
|
|
||||||
if (_mouseover)
|
|
||||||
{
|
|
||||||
this->removeChild(_mouseover);
|
|
||||||
}
|
|
||||||
// 添加新的
|
|
||||||
if (mouseover)
|
|
||||||
{
|
|
||||||
mouseover->setPivot(_pivotX, _pivotY);
|
|
||||||
this->addChild(mouseover);
|
|
||||||
}
|
|
||||||
_mouseover = mouseover;
|
|
||||||
_updateVisible();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void e2d::Button::setSelected(Node * selected)
|
void e2d::Button::setSelected(Node * selected)
|
||||||
{
|
{
|
||||||
if (selected != _normal)
|
SET_BUTTON_NODE(_selected, selected);
|
||||||
{
|
|
||||||
// 移除旧的
|
|
||||||
if (_selected)
|
|
||||||
{
|
|
||||||
this->removeChild(_selected);
|
|
||||||
}
|
|
||||||
// 添加新的
|
|
||||||
if (selected)
|
|
||||||
{
|
|
||||||
selected->setPivot(_pivotX, _pivotY);
|
|
||||||
this->addChild(selected);
|
|
||||||
}
|
|
||||||
_selected = selected;
|
|
||||||
_updateVisible();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void e2d::Button::setDisabled(Node * disabled)
|
void e2d::Button::setDisabled(Node * disabled)
|
||||||
{
|
{
|
||||||
if (disabled != _normal)
|
SET_BUTTON_NODE(_disabled, disabled);
|
||||||
{
|
|
||||||
// 移除旧的
|
|
||||||
if (_disabled)
|
|
||||||
{
|
|
||||||
this->removeChild(_disabled);
|
|
||||||
}
|
|
||||||
// 添加新的
|
|
||||||
if (disabled)
|
|
||||||
{
|
|
||||||
disabled->setPivot(_pivotX, _pivotY);
|
|
||||||
this->addChild(disabled);
|
|
||||||
}
|
|
||||||
_disabled = disabled;
|
|
||||||
_updateVisible();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void e2d::Button::setEnabled(bool enabled)
|
void e2d::Button::setEnabled(bool enabled)
|
||||||
|
|
@ -188,21 +143,25 @@ void e2d::Button::setPivot(float pivotX, float pivotY)
|
||||||
SAFE_SET(_disabled, setPivot, pivotX, 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)
|
if (_enabled && _visible && _normal)
|
||||||
{
|
{
|
||||||
bool contains = _normal->containsPoint(e.getPos());
|
bool contains = _normal->containsPoint(e.getPos());
|
||||||
if (e.getType() == MouseEvent::Type::LeftUp && _isSelected && contains)
|
if (e.getType() == MouseEvent::Type::LeftUp && _isSelected && contains)
|
||||||
{
|
{
|
||||||
_isSelected = false;
|
|
||||||
_runCallback();
|
_runCallback();
|
||||||
|
_isSelected = false;
|
||||||
_setStatus(Status::Normal);
|
_setStatus(Status::Normal);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
else if (e.getType() == MouseEvent::Type::LeftDown)
|
else if (e.getType() == MouseEvent::Type::LeftDown)
|
||||||
{
|
{
|
||||||
_isSelected = contains;
|
_isSelected = contains;
|
||||||
_setStatus(contains ? Status::Selected : Status::Normal);
|
_setStatus(contains ? Status::Selected : Status::Normal);
|
||||||
|
|
||||||
|
if (contains)
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
else if (e.getType() == MouseEvent::Type::LeftUp)
|
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)
|
else if (e.getType() == MouseEvent::Type::Move && _isSelected && contains)
|
||||||
{
|
{
|
||||||
_setStatus(Status::Selected);
|
_setStatus(Status::Selected);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -218,11 +178,15 @@ void e2d::Button::dispatch(const MouseEvent & e)
|
||||||
{
|
{
|
||||||
_isSelected = false;
|
_isSelected = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
_setStatus(contains ? Status::Mouseover : Status::Normal);
|
_setStatus(contains ? Status::Mouseover : Status::Normal);
|
||||||
|
|
||||||
|
if (contains)
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Node::dispatch(e);
|
return Node::dispatch(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
void e2d::Button::_render()
|
void e2d::Button::_render()
|
||||||
|
|
|
||||||
|
|
@ -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))
|
if (onMouseEvent(e))
|
||||||
return;
|
return true;
|
||||||
|
|
||||||
if (!_children.empty())
|
for (auto iter = _children.rbegin(); iter != _children.rend(); ++iter)
|
||||||
{
|
if ((*iter)->dispatch(e))
|
||||||
for (auto child : _children)
|
return true;
|
||||||
{
|
|
||||||
child->dispatch(e);
|
return false;
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void e2d::Node::dispatch(const KeyEvent & e)
|
bool e2d::Node::dispatch(const KeyEvent & e)
|
||||||
{
|
{
|
||||||
if (!onKeyEvent(e))
|
if (onKeyEvent(e))
|
||||||
return;
|
return true;
|
||||||
|
|
||||||
if (!_children.empty())
|
for (auto iter = _children.rbegin(); iter != _children.rend(); ++iter)
|
||||||
{
|
if ((*iter)->dispatch(e))
|
||||||
for (auto child : _children)
|
return true;
|
||||||
{
|
|
||||||
child->dispatch(e);
|
return false;
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void e2d::Node::_sortChildren()
|
void e2d::Node::_sortChildren()
|
||||||
|
|
|
||||||
|
|
@ -125,7 +125,10 @@ void e2d::ToggleButton::setChecked(bool checked)
|
||||||
void e2d::ToggleButton::setNormal(Node * normal)
|
void e2d::ToggleButton::setNormal(Node * normal)
|
||||||
{
|
{
|
||||||
SET_BUTTON_NODE(_normalOn, 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)
|
void e2d::ToggleButton::setMouseOver(Node * mouseover)
|
||||||
|
|
|
||||||
|
|
@ -983,12 +983,12 @@ public:
|
||||||
virtual void onExit() {}
|
virtual void onExit() {}
|
||||||
|
|
||||||
// 梓囚<E6A293>連
|
// 梓囚<E6A293>連
|
||||||
// 说明:返回 false 将阻止消息继续传递
|
// 说明:返回 true 将阻止消息继续传递
|
||||||
virtual bool onKeyEvent(KeyEvent e) { return true; }
|
virtual bool onKeyEvent(KeyEvent e) { return false; }
|
||||||
|
|
||||||
// 報炎<E5A0B1>連
|
// 報炎<E5A0B1>連
|
||||||
// 说明:返回 false 将阻止消息继续传递
|
// 说明:返回 true 将阻止消息继续传递
|
||||||
virtual bool onMouseEvent(MouseEvent e) { return true; }
|
virtual bool onMouseEvent(MouseEvent e) { return false; }
|
||||||
|
|
||||||
// 当弉<E5BD93>連
|
// 当弉<E5BD93>連
|
||||||
virtual void onCollision(Collision collision) { }
|
virtual void onCollision(Collision collision) { }
|
||||||
|
|
|
||||||
|
|
@ -53,12 +53,12 @@ public:
|
||||||
virtual void onRender() const {}
|
virtual void onRender() const {}
|
||||||
|
|
||||||
// 按键消息
|
// 按键消息
|
||||||
// 说明:返回 false 将阻止消息继续传递
|
// 说明:返回 true 将阻止消息向下传递
|
||||||
virtual bool onKeyEvent(KeyEvent e) { return true; }
|
virtual bool onKeyEvent(KeyEvent e) { return false; }
|
||||||
|
|
||||||
// 鼠标消息
|
// 鼠标消息
|
||||||
// 说明:返回 false 将阻止消息继续传递
|
// 说明:返回 true 将阻止消息向下传递
|
||||||
virtual bool onMouseEvent(MouseEvent e) { return true; }
|
virtual bool onMouseEvent(MouseEvent e) { return false; }
|
||||||
|
|
||||||
// 碰撞消息
|
// 碰撞消息
|
||||||
virtual void onCollision(Collision collision) { }
|
virtual void onCollision(Collision collision) { }
|
||||||
|
|
@ -402,12 +402,12 @@ public:
|
||||||
void updateTransform();
|
void updateTransform();
|
||||||
|
|
||||||
// 分发鼠标消息
|
// 分发鼠标消息
|
||||||
virtual void dispatch(
|
virtual bool dispatch(
|
||||||
const MouseEvent& e
|
const MouseEvent& e
|
||||||
);
|
);
|
||||||
|
|
||||||
// 分发按键消息
|
// 分发按键消息
|
||||||
virtual void dispatch(
|
virtual bool dispatch(
|
||||||
const KeyEvent& e
|
const KeyEvent& e
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -827,7 +827,7 @@ public:
|
||||||
) override;
|
) override;
|
||||||
|
|
||||||
// 分发鼠标消息
|
// 分发鼠标消息
|
||||||
virtual void dispatch(
|
virtual bool dispatch(
|
||||||
const MouseEvent& e
|
const MouseEvent& e
|
||||||
) override;
|
) override;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue