116 lines
2.5 KiB
C++
116 lines
2.5 KiB
C++
/**
|
|
* @file dl_ui_button_modern.h
|
|
* @brief 现代风格按钮
|
|
*
|
|
*
|
|
* @version 1.0
|
|
* @author lveyou
|
|
* @date 23-08-21
|
|
*
|
|
* @note 现代风格
|
|
*/
|
|
#pragma once
|
|
|
|
#include "ui/dl_ui_button_base.h"
|
|
#include "render/dl_sprite.h"
|
|
#include "render/dl_text.h"
|
|
|
|
namespace dl::UI
|
|
{
|
|
|
|
//! 现代风格 按钮
|
|
class ButtonNormal : public Button
|
|
{
|
|
public:
|
|
/**
|
|
* @brief 精灵组成的按钮
|
|
* @param[in] mul_spr 多个精灵,浅拷贝
|
|
* @note 最多五个精灵,普通、经过、按下、禁用(普通)、禁用(按下)
|
|
* 最少3个,缺少的禁用状态叠加灰度,4个时用一样的禁用状态。
|
|
* 可以额外拥有文本
|
|
*/
|
|
static ButtonNormal* Create(std::string_view bg)
|
|
{
|
|
ButtonNormal* ret = new ButtonNormal;
|
|
ret->_create(mul_spr);
|
|
ret->SetOrder(Z_DEFAULT_UI);
|
|
return ret;
|
|
}
|
|
|
|
ButtonNormal* Clone();
|
|
/**
|
|
* @brief 添加文本属性
|
|
* @param[in] txt 文本
|
|
* @param[in] mul_col 5种状态颜色
|
|
* @param[in] mul_offset 5种状态的文本偏移
|
|
*/
|
|
void AddText(gcText txt, const std::vector<Color>& mul_col, const std::vector<Position>& mul_offset);
|
|
|
|
void SetString(std::string_view str);
|
|
|
|
|
|
virtual bool IsPickup() override
|
|
{
|
|
return _spr[GetState()]->IsPickup();
|
|
}
|
|
virtual void SetOrder(float order) override
|
|
{
|
|
for(auto& spr : _spr)
|
|
spr->SetOrder(Math::OffsetOrder(order, 0, 0, 0));
|
|
for (auto& spr : _sprDisable)
|
|
spr->SetOrder(Math::OffsetOrder(order, 0, 0, 0));
|
|
if(_txtData)
|
|
_txtData->_txt->SetOrder(Math::OffsetOrder(order, 0, 0, -1));
|
|
}
|
|
virtual Position GetSize() override { return _spr[GetState()]->GetSize(); }
|
|
virtual Position GetAnchor() override { return _spr[GetState()]->GetAnchor(); }
|
|
|
|
~ButtonNormal()
|
|
{
|
|
delete _txtData;
|
|
}
|
|
private:
|
|
ButtonNormal() :_txtData{ nullptr } {}
|
|
|
|
void _create(const std::vector<gcSprite>& mul_spr);
|
|
|
|
Sprite9* _bg;
|
|
|
|
struct TextData
|
|
{
|
|
gcText _txt;
|
|
std::array<Color, 5> _col;
|
|
std::array<Position, 5> _offset;
|
|
};
|
|
TextData* _txtData;
|
|
|
|
void UpdateChild(State state_pre, State state)
|
|
{
|
|
if (_txtData)
|
|
{
|
|
size_t index = GetIndex(state);
|
|
_txtData->_txt->SetColor(_txtData->_col[index]);
|
|
_txtData->_txt->SetCenter(_txtData->_offset[index]);
|
|
}
|
|
}
|
|
void RenderChild(State state_pre, State state)
|
|
{
|
|
_spr[state]->Render();
|
|
if (_txtData)
|
|
_txtData->_txt->Render();
|
|
}
|
|
|
|
void _update_coor()
|
|
{
|
|
for (auto& iter : _spr)
|
|
{
|
|
iter->SetCoor(_coor);
|
|
}
|
|
_sprDisable[0]->SetCoor(_coor);
|
|
_sprDisable[1]->SetCoor(_coor);
|
|
if(_txtData)
|
|
_txtData->_txt->GetCoor()->SetParent(_coor);
|
|
}
|
|
};
|
|
|
|
} |