Magic_Game/src/kiwano/2d/Button.h

153 lines
4.3 KiB
C
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
2020-01-21 10:09:55 +08:00
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
2020-01-21 10:09:55 +08:00
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
2019-10-11 21:55:29 +08:00
#include <kiwano/2d/Sprite.h>
2020-01-09 08:45:00 +08:00
#include <kiwano/2d/TextActor.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2020-01-21 10:09:55 +08:00
KGE_DECLARE_SMART_PTR(Button);
KGE_DECLARE_SMART_PTR(SpriteButton);
KGE_DECLARE_SMART_PTR(TextButton);
/**
* \~chinese
2020-02-10 17:32:04 +08:00
* @brief
2020-01-21 10:09:55 +08:00
*/
class KGE_API Button : public virtual ObjectBase
{
public:
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 按钮回调函数
2020-01-21 10:09:55 +08:00
using Callback = Function<void(Button* /* self */)>;
Button();
virtual ~Button();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取按钮状态是启用还是禁用
2020-01-21 10:09:55 +08:00
bool IsEnable() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置按钮启用或禁用
2020-01-21 10:09:55 +08:00
void SetEnabled(bool enabled);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置按钮点击后的回调函数
2020-01-21 10:09:55 +08:00
void SetClickCallback(const Callback& func);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置按钮被按下时的回调函数
2020-01-21 10:09:55 +08:00
void SetPressedCallback(const Callback& func);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置鼠标移入按钮时的回调函数
2020-01-21 10:09:55 +08:00
void SetMouseOverCallback(const Callback& func);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置鼠标移出按钮时的回调函数
2020-01-21 10:09:55 +08:00
void SetMouseOutCallback(const Callback& func);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 按钮状态
2020-01-21 10:09:55 +08:00
enum class Status
{
2020-02-10 17:32:04 +08:00
Normal, ///< 普通
Hover, ///< 鼠标在按钮内
Pressed ///< 被按下
2020-01-21 10:09:55 +08:00
};
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置按钮状态
2020-01-21 10:09:55 +08:00
void SetStatus(Status status);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取按钮状态
2020-01-21 10:09:55 +08:00
Status GetStatus() const;
protected:
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 更新按钮状态
2020-01-21 10:09:55 +08:00
void UpdateStatus(Event* evt);
2020-02-11 22:56:12 +08:00
/// \~chinese
/// @brief 绑定到角色
void BindActor(Actor* actor);
2020-01-21 10:09:55 +08:00
private:
bool enabled_;
Status status_;
Callback click_callback_;
Callback pressed_callback_;
Callback mouse_over_callback_;
Callback mouse_out_callback_;
};
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 精灵按钮
2020-01-21 10:09:55 +08:00
class SpriteButton
: public Sprite
, public Button
{
public:
SpriteButton();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建精灵按钮
/// @param click 按钮点击回调函数
2020-02-06 16:54:47 +08:00
static SpriteButtonPtr Create(Callback const& click);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建精灵按钮
/// @param click 按钮点击回调函数
/// @param pressed 按钮按下回调函数
/// @param mouse_over 按钮移入回调函数
/// @param mouse_out 按钮移出回调函数
2020-02-06 16:54:47 +08:00
static SpriteButtonPtr Create(Callback const& click, Callback const& pressed, Callback const& mouse_over,
Callback const& mouse_out);
2020-01-21 10:09:55 +08:00
};
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 文字按钮
2020-01-21 10:09:55 +08:00
class TextButton
: public TextActor
, public Button
{
public:
TextButton();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建文字按钮
/// @param click 按钮点击回调函数
2020-02-06 16:54:47 +08:00
static TextButtonPtr Create(Callback const& click);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建文字按钮
/// @param click 按钮点击回调函数
/// @param pressed 按钮按下回调函数
/// @param mouse_over 按钮移入回调函数
/// @param mouse_out 按钮移出回调函数
2020-02-06 16:54:47 +08:00
static TextButtonPtr Create(Callback const& click, Callback const& pressed, Callback const& mouse_over, Callback const& mouse_out);
2020-01-21 10:09:55 +08:00
};
2020-02-11 22:56:12 +08:00
2020-01-21 10:09:55 +08:00
} // namespace kiwano