Files
dl_old/dl/ui/dl_ui_button_base.h
T
2023-12-23 16:39:54 +08:00

236 lines
5.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file dl_ui_button_base.h
* @brief UI系统之按钮基类
*
*
* @version 1.0
* @author lveyou
* @date 22-10-08
*
* @note 按钮、开关、Radio的逻辑相似,都算Control
* @note 以主流的界面设计(渐变效果)
* @note 状态是瞬间切换,方便代码处理(界面是渐变处理)
* @note tab等操作与控件无关,在Panel处理(添加一个合适大小的选中框)
*/
#pragma once
#include "math/dl_coor.h"
#include "dl_type.h"
#include "io/dl_input.h"
#include "math/dl_align.h"
#include "misc/dl_component.h"
namespace dl::UI
{
//! 按钮(Button、Switch、Radio)
class Button
{
public:
enum class State
{
NORMAL, //!< 普通
OVER, //!< 鼠标经过
DOWN, //!< 持续按下
MAX_NUM
};
enum class Mode
{
BUTTON, //!< 按钮
SWITCH, //!< 开关,按下后不会自动弹起,再次点击后弹起
RADIO, //!< 收音机按钮,按下后就不能弹起,只能通过代码弹起
};
Button() :
_state(State::NORMAL),
_statePre(State::NORMAL),
_mode(Mode::BUTTON),
_bOpen{ false },
_bDisable(false),
_cpt{this}
{
_coor = Coor::Create();
_init_cpt();
}
//克隆使用
Button(const Button& b) :
_state(b._state),
_statePre(b._statePre),
_mode(b._mode),
_bOpen{ b._bOpen },
_bDisable(b._bDisable),
_cpt{ this }
{
_coor = Coor::Create(const_cast<Button&>(b).GetCoor()->GetParent());
_init_cpt();
}
void Update();
void Render();
/**
* @brief 控件被点击
* @note 对于 button来说,单击并释放才会触发
* @note 对于 switch来说,单击并释放才会触发
* @note 对于 radio来说,单击才会触发
*/
bool IsClick();
//! 经过按钮,直到离开控件再次进入才会返回真
bool IsThrough();
bool IsRadio() { return _mode == Mode::RADIO; }
bool IsSelect() { return _bOpen; }
void SetSelect(bool open);
void SetMode(Mode mode);
void SetDisable(bool disable) { _bDisable = disable; }
/**
* @brief 设置按钮音效
* @param[in] click 点击音效
* @param[in] over 经过音效
*
* @note 默认为空字符串,为没有音效
*/
void SetSound(std::string_view click, std::string_view over)
{
_strSndClick = std::string{ click };
_strSndOver = std::string{ over };
}
State GetState() { return _state; }
Coor* GetCoor() { return _coor; }
virtual Button* Clone() = 0;
virtual bool IsPickup() = 0;
virtual void SetOrder(float order) = 0;
virtual Position GetSize() = 0;
virtual Position GetAnchor() = 0;
void SetElementData(const Align::Direction2& hv)
{
_cptElemData._hv = hv;
_refresh_element_data();
}
Components* GetCpt() { return &_cpt; }
Mode GetMode() { return _mode; }
virtual ~Button()
{
delete _coor;
}
private:
State _state;
State _statePre;
bool _bDisable;
Mode _mode;
bool _bOpen;//! 普通按钮不使用
bool _isSwitchClick;//! 开关按钮辅助判断
//! 经过与点击音效
std::string _strSndClick;
std::string _strSndOver;
protected:
Coor* _coor;
//! 返回索引
std::size_t GetIndex(State state)
{
if (_bDisable)
{
if (state == State::NORMAL || state == State::OVER)
return 3;
else
return 4;
}
else
{
return (std::size_t)state;
}
}
//
void _refresh_element_data()
{
_cptElemData._size = GetSize();
_cptElemData._anchor = GetAnchor();
}
private:
//--------------------------组件------------------------
Components _cpt;
Cpt::DeleteThis _cptDeleteThis;
Cpt::Update _cptUpdate;
Cpt::Render _cptRender;
Cpt::IsPickup _cptIsPickup;
Cpt::SetOrder _cptSetOrder;
Cpt::GetCoor _cptGetCoor;
Cpt::IsClick _cptIsClick;
Cpt::IsSelect _cptIsOpen;
Cpt::SetSelect _cptSetOpen;
Cpt::ElementData _cptElemData;
Cpt::Clone _cptClone;
void _delete_this() { delete this; }
Components* _clone()
{
return Clone()->GetCpt();
}
void _init_cpt()
{
_cptDeleteThis = std::bind(&Button::_delete_this, this);
_cptUpdate = std::bind(&Button::Update, this);
_cptRender = std::bind(&Button::Render, this);
_cptIsPickup = std::bind(&Button::IsPickup, this);
_cptSetOrder = std::bind(&Button::SetOrder, this, std::placeholders::_1);
_cptGetCoor = std::bind(&Button::GetCoor, this);
_cptIsClick = std::bind(&Button::IsClick, this);
_cptIsOpen = std::bind(&Button::IsSelect, this);
_cptSetOpen = std::bind(&Button::SetSelect, this, std::placeholders::_1);
_cptClone = std::bind(&Button::_clone, this);
_cpt.PushBack(&_cptDeleteThis);
_cpt.PushBack(&_cptUpdate);
_cpt.PushBack(&_cptRender);
_cpt.PushBack(&_cptIsPickup);
_cpt.PushBack(&_cptSetOrder);
_cpt.PushBack(&_cptGetCoor);
_cpt.PushBack(&_cptIsClick);
_cpt.PushBack(&_cptIsOpen);
_cpt.PushBack(&_cptSetOpen);
_cpt.PushBack(&_cptClone);
_cpt.PushBack(&_cptElemData);
}
static Button* _clickControl;//防止在按钮外面按下左键移动到按钮内也能触发Click
/**
* @brief 重载此函数,制定控件行为
*/
virtual void UpdateChild(State state_pre, State state) = 0;
virtual void RenderChild(State state_pre, State state) = 0;
void _update_button();
void _update_switch();
void _update_radio();
};
using ButtonMode = Button::Mode;
using ButtonState = Button::State;
}