remove: ToggleButton

This commit is contained in:
Nomango 2018-09-07 23:57:32 +08:00
parent 775e1075c8
commit 6732adccfd
9 changed files with 4 additions and 362 deletions

View File

@ -151,7 +151,10 @@ bool e2d::Button::Dispatch(const MouseEvent & e, bool handled)
bool contains = normal_->ContainsPoint(e.GetPos());
if (e.GetType() == MouseEvent::Type::LeftUp && is_selected_ && contains)
{
OnClick();
if (callback_)
{
callback_();
}
is_selected_ = false;
SetStatus(Status::Normal);
return true;
@ -250,11 +253,3 @@ void e2d::Button::UpdateVisible()
}
}
}
void e2d::Button::OnClick()
{
if (callback_)
{
callback_();
}
}

View File

@ -1,219 +0,0 @@
#include "..\e2dcomponent.h"
#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->SetAnchor(anchor_.x, anchor_.y); \
this->AddChild(New); \
} \
Old = New; \
UpdateStatus(); \
UpdateVisible(); \
} \
e2d::ToggleButton::ToggleButton()
: Button()
, checked_(true)
, normal_on_(nullptr)
, mouseover_on_(nullptr)
, selected_on_(nullptr)
, disabled_on_(nullptr)
, normal_off_(nullptr)
, mouseover_off_(nullptr)
, selected_off_(nullptr)
, disabled_off_(nullptr)
{
}
e2d::ToggleButton::ToggleButton(Node * normal_on, Node * normal_off, const Function& func)
: Button()
, checked_(true)
, normal_on_(nullptr)
, mouseover_on_(nullptr)
, selected_on_(nullptr)
, disabled_on_(nullptr)
, normal_off_(nullptr)
, mouseover_off_(nullptr)
, selected_off_(nullptr)
, disabled_off_(nullptr)
{
this->SetNormal(normal_on);
this->SetNormalOff(normal_off);
this->SetCallbackOnClick(func);
}
e2d::ToggleButton::ToggleButton(Node * normal_on, Node * normal_off, Node * selected_on, Node * selected_off, const Function& func)
: Button()
, checked_(true)
, normal_on_(nullptr)
, mouseover_on_(nullptr)
, selected_on_(nullptr)
, disabled_on_(nullptr)
, normal_off_(nullptr)
, mouseover_off_(nullptr)
, selected_off_(nullptr)
, disabled_off_(nullptr)
{
this->SetNormal(normal_on);
this->SetNormalOff(normal_off);
this->SetSelected(selected_on);
this->SetSelectedOff(selected_off);
this->SetCallbackOnClick(func);
}
e2d::ToggleButton::ToggleButton(Node * normal_on, Node * normal_off, Node * mouseover_on, Node * mouseover_off, Node * selected_on, Node * selected_off, const Function& func)
: Button()
, checked_(true)
, normal_on_(nullptr)
, mouseover_on_(nullptr)
, selected_on_(nullptr)
, disabled_on_(nullptr)
, normal_off_(nullptr)
, mouseover_off_(nullptr)
, selected_off_(nullptr)
, disabled_off_(nullptr)
{
this->SetNormal(normal_on);
this->SetNormalOff(normal_off);
this->SetMouseOver(mouseover_on);
this->SetMouseOverOff(mouseover_off);
this->SetSelected(selected_on);
this->SetSelectedOff(selected_off);
this->SetCallbackOnClick(func);
}
e2d::ToggleButton::ToggleButton(Node * normal_on, Node * normal_off, Node * mouseover_on, Node * mouseover_off, Node * selected_on, Node * selected_off, Node * disabled_on, Node * disabled_off, const Function& func)
: Button()
, checked_(true)
, normal_off_(nullptr)
, mouseover_off_(nullptr)
, selected_off_(nullptr)
, disabled_off_(nullptr)
{
this->SetNormal(normal_on);
this->SetNormalOff(normal_off);
this->SetMouseOver(mouseover_on);
this->SetMouseOverOff(mouseover_off);
this->SetSelected(selected_on);
this->SetSelectedOff(selected_off);
this->SetDisabled(disabled_on);
this->SetDisabledOff(disabled_off);
this->SetCallbackOnClick(func);
}
bool e2d::ToggleButton::IsChecked() const
{
return checked_;
}
void e2d::ToggleButton::SetChecked(bool checked)
{
if (checked_ != checked)
{
checked_ = checked;
UpdateStatus();
UpdateVisible();
}
}
void e2d::ToggleButton::SetNormal(Node * normal)
{
SET_BUTTON_NODE(normal_on_, normal);
if (normal)
{
this->SetSize(normal->GetWidth(), normal->GetHeight());
}
}
void e2d::ToggleButton::SetMouseOver(Node * mouseover)
{
SET_BUTTON_NODE(mouseover_on_, mouseover);
}
void e2d::ToggleButton::SetSelected(Node * selected)
{
SET_BUTTON_NODE(selected_on_, selected);
}
void e2d::ToggleButton::SetDisabled(Node * disabled)
{
SET_BUTTON_NODE(disabled_on_, disabled);
}
void e2d::ToggleButton::SetNormalOff(Node * normal)
{
SET_BUTTON_NODE(normal_off_, normal);
}
void e2d::ToggleButton::SetMouseOverOff(Node * mouseover)
{
SET_BUTTON_NODE(mouseover_off_, mouseover);
}
void e2d::ToggleButton::SetSelectedOff(Node * selected)
{
SET_BUTTON_NODE(selected_off_, selected);
}
void e2d::ToggleButton::SetDisabledOff(Node * disabled)
{
SET_BUTTON_NODE(disabled_off_, disabled);
}
void e2d::ToggleButton::SetAnchor(float anchor_x, float anchor_y)
{
Node::SetAnchor(anchor_x, anchor_y);
SAFE_SET(normal_on_, SetAnchor, anchor_x, anchor_y);
SAFE_SET(mouseover_on_, SetAnchor, anchor_x, anchor_y);
SAFE_SET(selected_on_, SetAnchor, anchor_x, anchor_y);
SAFE_SET(disabled_on_, SetAnchor, anchor_x, anchor_y);
SAFE_SET(normal_off_, SetAnchor, anchor_x, anchor_y);
SAFE_SET(mouseover_off_, SetAnchor, anchor_x, anchor_y);
SAFE_SET(selected_off_, SetAnchor, anchor_x, anchor_y);
SAFE_SET(disabled_off_, SetAnchor, anchor_x, anchor_y);
}
void e2d::ToggleButton::UpdateStatus()
{
if (checked_)
{
normal_ = normal_on_;
mouseover_ = mouseover_on_;
selected_ = selected_on_;
disabled_ = disabled_on_;
SAFE_SET(normal_off_, SetVisible, false);
SAFE_SET(mouseover_off_, SetVisible, false);
SAFE_SET(selected_off_, SetVisible, false);
SAFE_SET(disabled_off_, SetVisible, false);
}
else
{
normal_ = normal_off_;
mouseover_ = mouseover_off_;
selected_ = selected_off_;
disabled_ = disabled_off_;
SAFE_SET(normal_on_, SetVisible, false);
SAFE_SET(mouseover_on_, SetVisible, false);
SAFE_SET(selected_on_, SetVisible, false);
SAFE_SET(disabled_on_, SetVisible, false);
}
}
void e2d::ToggleButton::OnClick()
{
checked_ = !checked_;
UpdateStatus();
if (callback_)
{
callback_();
}
}

View File

@ -100,9 +100,6 @@ namespace e2d
// 刷新按钮显示
virtual void UpdateVisible();
// 点击回调
virtual void OnClick();
protected:
Node * normal_;
Node * mouseover_;
@ -115,125 +112,6 @@ namespace e2d
};
class ToggleButton :
public Button
{
public:
ToggleButton();
explicit ToggleButton(
Node * normal_on, /* 按钮打开时,普通状态 */
Node * normal_off, /* 按钮关闭时,普通状态 */
const Function& func = nullptr /* 按钮点击后的回调函数 */
);
explicit ToggleButton(
Node * normal_on, /* 按钮打开时,普通状态 */
Node * normal_off, /* 按钮关闭时,普通状态 */
Node * selected_on, /* 按钮打开时,鼠标按下状态 */
Node * selected_off, /* 按钮关闭时,鼠标按下状态 */
const Function& func = nullptr /* 按钮点击后的回调函数 */
);
explicit ToggleButton(
Node * normal_on, /* 按钮打开时,普通状态 */
Node * normal_off, /* 按钮关闭时,普通状态 */
Node * mouseover_on, /* 按钮打开时,鼠标移入状态 */
Node * mouseover_off, /* 按钮关闭时,鼠标移入状态 */
Node * selected_on, /* 按钮打开时,鼠标按下状态 */
Node * selected_off, /* 按钮关闭时,鼠标按下状态 */
const Function& func = nullptr /* 按钮点击后的回调函数 */
);
explicit ToggleButton(
Node * normal_on, /* 按钮打开时,普通状态 */
Node * normal_off, /* 按钮关闭时,普通状态 */
Node * mouseover_on, /* 按钮打开时,鼠标移入状态 */
Node * mouseover_off, /* 按钮关闭时,鼠标移入状态 */
Node * selected_on, /* 按钮打开时,鼠标按下状态 */
Node * selected_off, /* 按钮关闭时,鼠标按下状态 */
Node * disabled_on, /* 按钮打开时,禁用状态 */
Node * disabled_off, /* 按钮关闭时,禁用状态 */
const Function& func = nullptr /* 按钮点击后的回调函数 */
);
// 获取开关状态
bool IsChecked() const;
// 设置开关按钮的状态
void SetChecked(
bool checked
);
// 设置按钮打开状态下显示的按钮
virtual void SetNormal(
Node * normal
) override;
// 设置按钮打开状态下,鼠标移入按钮时显示的按钮
virtual void SetMouseOver(
Node * mouseover
) override;
// 设置按钮打开状态下,鼠标按下按钮时显示的按钮
virtual void SetSelected(
Node * selected
) override;
// 设置按钮打开状态下,被禁用时显示的按钮
virtual void SetDisabled(
Node * disabled
) override;
// 设置按钮关闭状态下显示的按钮
void SetNormalOff(
Node * normal
);
// 设置按钮关闭状态下,鼠标移入按钮时显示的按钮
void SetMouseOverOff(
Node * mouseover
);
// 设置按钮关闭状态下,鼠标按下按钮时显示的按钮
void SetSelectedOff(
Node * selected
);
// 设置按钮关闭状态下,按钮被禁用时显示的按钮
void SetDisabledOff(
Node * disabled
);
// 设置锚点位置
// 默认为 (0, 0), 范围 [0, 1]
virtual void SetAnchor(
float anchor_x,
float anchor_y
) override;
protected:
E2D_DISABLE_COPY(ToggleButton);
// 刷新按钮开关
virtual void UpdateStatus();
// 执行按钮函数对象
virtual void OnClick() override;
protected:
Node * normal_on_;
Node* mouseover_on_;
Node* selected_on_;
Node* disabled_on_;
Node* normal_off_;
Node* mouseover_off_;
Node* selected_off_;
Node* disabled_off_;
bool checked_;
};
// 菜单
class Menu :
public Node

View File

@ -42,7 +42,6 @@
<ClCompile Include="..\..\core\actions\Spawn.cpp" />
<ClCompile Include="..\..\core\components\Button.cpp" />
<ClCompile Include="..\..\core\components\Menu.cpp" />
<ClCompile Include="..\..\core\components\ToggleButton.cpp" />
<ClCompile Include="..\..\core\event\Collision.cpp" />
<ClCompile Include="..\..\core\event\KeyEvent.cpp" />
<ClCompile Include="..\..\core\event\MouseEvent.cpp" />

View File

@ -150,9 +150,6 @@
<ClCompile Include="..\..\core\components\Menu.cpp">
<Filter>components</Filter>
</ClCompile>
<ClCompile Include="..\..\core\components\ToggleButton.cpp">
<Filter>components</Filter>
</ClCompile>
<ClCompile Include="..\..\core\impl\Exception.cpp">
<Filter>impl</Filter>
</ClCompile>

View File

@ -186,7 +186,6 @@
<ClCompile Include="..\..\core\actions\Spawn.cpp" />
<ClCompile Include="..\..\core\components\Button.cpp" />
<ClCompile Include="..\..\core\components\Menu.cpp" />
<ClCompile Include="..\..\core\components\ToggleButton.cpp" />
<ClCompile Include="..\..\core\event\Collision.cpp" />
<ClCompile Include="..\..\core\event\KeyEvent.cpp" />
<ClCompile Include="..\..\core\event\MouseEvent.cpp" />

View File

@ -150,9 +150,6 @@
<ClCompile Include="..\..\core\components\Menu.cpp">
<Filter>components</Filter>
</ClCompile>
<ClCompile Include="..\..\core\components\ToggleButton.cpp">
<Filter>components</Filter>
</ClCompile>
<ClCompile Include="..\..\core\impl\Exception.cpp">
<Filter>impl</Filter>
</ClCompile>

View File

@ -219,7 +219,6 @@
<ClCompile Include="..\..\core\actions\Spawn.cpp" />
<ClCompile Include="..\..\core\components\Button.cpp" />
<ClCompile Include="..\..\core\components\Menu.cpp" />
<ClCompile Include="..\..\core\components\ToggleButton.cpp" />
<ClCompile Include="..\..\core\event\Collision.cpp" />
<ClCompile Include="..\..\core\event\KeyEvent.cpp" />
<ClCompile Include="..\..\core\event\MouseEvent.cpp" />

View File

@ -150,9 +150,6 @@
<ClCompile Include="..\..\core\components\Menu.cpp">
<Filter>components</Filter>
</ClCompile>
<ClCompile Include="..\..\core\components\ToggleButton.cpp">
<Filter>components</Filter>
</ClCompile>
<ClCompile Include="..\..\core\impl\Exception.cpp">
<Filter>impl</Filter>
</ClCompile>