dl
dl_ui_button_sprite.h
浏览该文件的文档.
1
12#pragma once
13
14#ifndef DL_DISABLE_RENDER
16#include "render/dl_sprite.h"
17#include "render/dl_text.h"
18
19namespace dl::UI
20{
21
23class ButtonSprite : public Button
24{
25public:
33 static ButtonSprite* Create(const std::vector<gcSprite>& mul_spr)
34 {
35 ButtonSprite* ret = new ButtonSprite;
36 ret->_init(mul_spr);
39 return ret;
40 }
41
42 virtual ButtonSprite* Clone() override;
43
44 std::string GetString()
45 {
46 if (!_txtData)
47 {
48 return {};
49 }
50 return _txtData->_txt->GetString();
51 }
52
58 void AddText(gcText txt, const std::vector<Color>& mul_col, const std::vector<Position>& mul_offset);
60
61 void SetString(std::string_view str);
63 {
64 if (_txtData)
65 _txtData->_hv = hv;
66 }
67
74 void SetSpriteQuad(Button::State state, const Quad& quad);
81 void SetSpriteColor(Button::State state, const Color& color);
82
83 gcSprite GetSpriteCurrent() { return _spr[GetState()]; }
84
85 virtual bool IsPickup() override
86 {
87 return _spr[GetState()]->IsPickup();
88 }
89 virtual void SetOrder(float order) override
90 {
91 for (auto& spr : _spr)
92 spr->SetOrder(Math::OffsetOrder(order, 0, 0, 0));
93 if(_txtData)
94 _txtData->_txt->SetOrder(Math::OffsetOrder(order, 0, 0, -1));
95 }
96 virtual Position GetSize() override { return _spr[GetState()]->GetSize(); }
97 virtual Position GetAnchor() override { return _spr[GetState()]->GetAnchor(); }
98 virtual void RenderDebug() override;
99
101 {
102 delete _txtData;
103 }
104private:
105 ButtonSprite() :_txtData{ nullptr } {}
106
107 void _init(const std::vector<gcSprite>& mul_spr);
108
110
111 struct TextData
112 {
113 gcText _txt;
114 Align::Direction2 _hv;// 在按钮的位置
117 };
118 TextData* _txtData;
119
120 virtual void UpdateChild(State state_pre, State state) override
121 {
122 if (_txtData)
123 {
124 size_t index = GetIndex(state);
125 _txtData->_txt->SetColor(_txtData->_col[index]);
126 _txtData->_txt->GetCoor()->SetPosition(
127 _txtData->_offset[index]
128 + Size2Anchor(_spr[index]->GetSize(), toPosition(_txtData->_txt->GetSize()), _txtData->_hv));
129 }
130 }
131 virtual void RenderChild(State state_pre, State state) override
132 {
133 _spr[state]->Render();
134 if (_txtData)
135 _txtData->_txt->Render();
136 }
137
138 void _update_coor()
139 {
140 for (auto& iter : _spr)
141 {
142 iter->SetCoor(_coor);
143 }
144 if(_txtData)
145 _txtData->_txt->GetCoor()->SetParent(_coor);
146 }
147};
148
149}
150
151#endif
枚举类数组
std::size_t GetIndex(State state)
返回索引
精灵按钮(注意精灵没有Coor)
void AddText(gcText txt, const std::vector< Color > &mul_col, const std::vector< Position > &mul_offset)
添加文本属性
virtual Position GetAnchor() override
virtual ButtonSprite * Clone() override
void SetSpriteQuad(Button::State state, const Quad &quad)
设置精灵quad
virtual void RenderDebug() override
void SetString(std::string_view str)
virtual Position GetSize() override
static ButtonSprite * Create(const std::vector< gcSprite > &mul_spr)
精灵组成的按钮
void SetSpriteColor(Button::State state, const Color &color)
设置精灵颜色
virtual void SetOrder(float order) override
virtual bool IsPickup() override
void SetTextAlign(const Align::Direction2 &hv)
2d绘图元素
文本绘制
UI系统之按钮基类
Array< Direction, 2 > Direction2
constexpr float OffsetOrder(float order, int u=1, int v=0, int w=0)
偏移order
Position2 Position
constexpr Array< Position::value_type, N > toPosition(const Array< T, N > &a)
转换到Position,已知长度
constexpr float Z_DEFAULT_UI
GC< Sprite > gcSprite
GC< Text > gcText
Array< float, 4 > Color
Array< Position, 4 > Quad