* ui系统改进

This commit is contained in:
2023-09-30 21:23:02 +08:00
parent 2331f7e0c6
commit 5fd07a3d8a
14 changed files with 158 additions and 56 deletions
+13
View File
@@ -257,6 +257,13 @@ private:
std::string _name;
};
inline Components* createComponents(std::string_view name = {})
{
Components* ret = new Components((int*)nullptr);
ret->SetName(name);
return ret;
}
namespace Cpt
{
/**
@@ -300,6 +307,7 @@ namespace Cpt
using Update = Function<std::function<void()>, str_uid("Update")>;//!< 逻辑
using Render = Function<std::function<void()>, str_uid("Render")>;//!< 绘制
using SetOrder = Function<std::function<void(float)>, str_uid("SetOrder")>;//!< 设置order
using SetOpen = Function<std::function<void(bool)>, str_uid("SetOpen")>;//!< 设置是否打开
using DeleteThis = Function<std::function<void()>, str_uid("DeleteThis")>;//!< 删除自身
using Clone = Function<std::function<Components*()>, str_uid("Clone")>;//!< 克隆
@@ -325,6 +333,11 @@ namespace Cpt
{
return -Align::Size2Anchor(_size, _hv) + _anchor;
}
Position GetSize()
{
return _size;
}
public:
Position _size;
Position _anchor;
+10 -10
View File
@@ -3,9 +3,9 @@
namespace dl::UI
{
ButtonBase* ButtonBase::_clickControl = nullptr;
Button* Button::_clickControl = nullptr;
void ButtonBase::_update_button()
void Button::_update_button()
{
if (_clickControl == this)
{
@@ -60,7 +60,7 @@ void ButtonBase::_update_button()
}
}
void ButtonBase::_update_switch()
void Button::_update_switch()
{
_isSwitchClick = false;
if (_clickControl == this)
@@ -121,7 +121,7 @@ void ButtonBase::_update_switch()
}
}
void ButtonBase::_update_radio()
void Button::_update_radio()
{
//点击了里面,变为open(并不能撤销)
if (IsPickup())
@@ -149,7 +149,7 @@ void ButtonBase::_update_radio()
#define snd_play
void ButtonBase::Update()
void Button::Update()
{
if (_bDisable)
{
@@ -182,19 +182,19 @@ void ButtonBase::Update()
UpdateChild(_statePre, _state);
}
void ButtonBase::Render()
void Button::Render()
{
RenderChild(_statePre, _state);
}
void ButtonBase::SetOpen(bool open)
void Button::SetOpen(bool open)
{
_bOpen = open;
//放止Release事件
_state = _bOpen ? State::DOWN : (_state == State::OVER ? State::OVER : State::NORMAL);
}
bool ButtonBase::IsClick()
bool Button::IsClick()
{
switch (_mode)
{
@@ -209,12 +209,12 @@ bool ButtonBase::IsClick()
}
}
bool ButtonBase::IsThrough()
bool Button::IsThrough()
{
return (_statePre == State::NORMAL) && (_state == State::OVER);
}
void ButtonBase::SetMode(Mode mode)
void Button::SetMode(Mode mode)
{
_mode = mode;
}
+20 -14
View File
@@ -24,7 +24,7 @@ namespace dl::UI
{
//! 按钮(Button、Switch、Radio)
class ButtonBase
class Button
{
public:
enum class State
@@ -42,7 +42,7 @@ public:
RADIO, //!< 收音机按钮,按下后就不能弹起,只能通过代码弹起
};
ButtonBase() :
Button() :
_state(State::NORMAL),
_statePre(State::NORMAL),
_mode(Mode::BUTTON),
@@ -55,7 +55,7 @@ public:
}
//克隆使用
ButtonBase(const ButtonBase& b) :
Button(const Button& b) :
_state(b._state),
_statePre(b._statePre),
_mode(b._mode),
@@ -63,7 +63,7 @@ public:
_bDisable(b._bDisable),
_cpt{ this }
{
_coor = Coor::Create(const_cast<ButtonBase&>(b).GetCoor()->GetParent());
_coor = Coor::Create(const_cast<Button&>(b).GetCoor()->GetParent());
_init_cpt();
}
@@ -125,7 +125,7 @@ public:
Mode GetMode() { return _mode; }
virtual ~ButtonBase()
virtual ~Button()
{
delete _coor;
}
@@ -171,19 +171,23 @@ private:
Cpt::SetOrder _cptSetOrder;
Cpt::GetCoor _cptGetCoor;
Cpt::IsClick _cptIsClick;
Cpt::IsOpen _cptIsOpen;
Cpt::SetOpen _cptSetOpen;
Cpt::ElementData _cptElemData;
void _delete_this() { delete this; }
void _init_cpt()
{
_cptDeleteThis = std::bind(&ButtonBase::_delete_this, this);
_cptUpdate = std::bind(&ButtonBase::Update, this);
_cptRender = std::bind(&ButtonBase::Render, this);
_cptIsPickup = std::bind(&ButtonBase::IsPickup, this);
_cptSetOrder = std::bind(&ButtonBase::SetOrder, this, std::placeholders::_1);
_cptGetCoor = std::bind(&ButtonBase::GetCoor, this);
_cptIsClick = std::bind(&ButtonBase::IsClick, this);
_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::IsOpen, this);
_cptSetOpen = std::bind(&Button::SetOpen, this, std::placeholders::_1);
_cpt.PushBack(&_cptDeleteThis);
_cpt.PushBack(&_cptUpdate);
@@ -192,12 +196,14 @@ private:
_cpt.PushBack(&_cptSetOrder);
_cpt.PushBack(&_cptGetCoor);
_cpt.PushBack(&_cptIsClick);
_cpt.PushBack(&_cptIsOpen);
_cpt.PushBack(&_cptSetOpen);
_cpt.PushBack(&_cptElemData);
}
static ButtonBase* _clickControl;//防止在按钮外面按下左键移动到按钮内也能触发Click
static Button* _clickControl;//防止在按钮外面按下左键移动到按钮内也能触发Click
/**
* @brief 重载此函数,制定控件行为
@@ -210,6 +216,6 @@ private:
void _update_radio();
};
using ButtonMode = ButtonBase::Mode;
using ButtonMode = Button::Mode;
}
+5 -5
View File
@@ -1,13 +1,13 @@
/**
* @file dl_ui_button_normal.h
* @brief 默认按钮
* @file dl_ui_button_modern.h
* @brief 现代风格按钮
*
*
* @version 1.0
* @author lveyou
* @date 23-08-21
*
* @note
* @note 现代风格
*/
#pragma once
@@ -18,8 +18,8 @@
namespace dl::UI
{
//! 默认按钮
class ButtonNormal : public ButtonBase
//! 现代风格 按钮
class ButtonNormal : public Button
{
public:
/**
+2 -2
View File
@@ -19,7 +19,7 @@ namespace dl::UI
{
//! 精灵按钮(注意精灵没有Coor)
class ButtonSprite : public ButtonBase
class ButtonSprite : public Button
{
public:
/**
@@ -74,7 +74,7 @@ private:
void _create(const std::vector<gcSprite>& mul_spr);
ArrayEnum<gcSprite, ButtonBase::State::MAX_NUM> _spr;
ArrayEnum<gcSprite, Button::State::MAX_NUM> _spr;
std::array<gcSprite, 2> _sprDisable;
struct TextData
+1 -1
View File
@@ -19,7 +19,7 @@ namespace dl::UI
{
//! 文本变色按钮
class ButtonTextColor : public ButtonBase
class ButtonTextColor : public Button
{
public:
/**
+2 -2
View File
@@ -319,9 +319,9 @@ void Manager::_reg_button(json& j, const std::string& name)
}
//mode
if (mode == "switch")
btn->SetMode(UI::ButtonBase::Mode::SWITCH);
btn->SetMode(UI::Button::Mode::SWITCH);
else if (mode == "radio")
btn->SetMode(UI::ButtonBase::Mode::RADIO);
btn->SetMode(UI::Button::Mode::RADIO);
_allBtnSprite[name] = btn;
log_msg("成功注册一个ButtonSprite:{}", name);
+11 -14
View File
@@ -12,27 +12,24 @@ void Panel::Add(Components* cpt)
Cpt::ElementData* ele_data = cpt->Get<Cpt::ElementData>();
Position offset{};
Position size{};
if (ele_data)
{//计算offset
offset = ele_data->GetOffset();
size = ele_data->GetSize();
}
//组件处理
// 位置计算
Position pos = _locator.Get(Math::CastU(size), offset);
// 组件处理
for (AnyType& iter : cpt->GetVectorAny())
{
if (auto comp = iter.Cast<Cpt::GetCoor>())
{
Coor* coor = comp->Call();
coor->SetParent(_coor);
if (ele_data)
{
coor->SetPosition(_locator.Get(
Math::CastU(ele_data->_size), offset));
}
else
{
log_warn0("TODO:暂未处理没有ele_data情况!");
}
coor->SetPosition(pos);
//if (_bDisableLocator)//禁用定位器,允许用户手动设置位置
// coor->DtPosition(offset);
@@ -149,17 +146,17 @@ void Panel::Update()
comp->Call();
//如果自己是radio且打开,则将其它radio变为非open
if (UI::ButtonBase* c = cpt->Cast<UI::ButtonBase>())
if (UI::Button* c = cpt->Cast<UI::Button>())
{
if (c->GetMode() == UI::ButtonBase::Mode::RADIO
if (c->GetMode() == UI::Button::Mode::RADIO
&& c->IsOpen())
{
for (Components* cpt1 : _ele)
{
if (cpt1 != cpt)
{
UI::ButtonBase* c1 = cpt1->Cast<UI::ButtonBase>();
if (c1 && c1->GetMode() == UI::ButtonBase::Mode::RADIO)
UI::Button* c1 = cpt1->Cast<UI::Button>();
if (c1 && c1->GetMode() == UI::Button::Mode::RADIO)
{
c1->SetOpen(false);
}
+15
View File
@@ -111,6 +111,21 @@ public:
_order = order;
}
/**
* @brief 设置被选中的元素
*/
void SetSelect(std::size_t index)
{
std::size_t i = 0;
for (auto& iter : _ele)
{
if (auto cpt = iter->Get<Cpt::SetOpen>())
{
cpt->Call(i == index);
}
}
}
/**
* @brief 执行逻辑
* @node 默认情况会自动调用
+6 -6
View File
@@ -80,19 +80,19 @@ void Run()
{
g_uiMain->Run();
base->txt->Render();
//base->txt->Render();
/*base->spr->Render();
base->txt->Render();
base->spr_logo->Render();*/
g_gfx->Dot({ 400, 300, 0.5f });
g_gfx->Line({ 20, 20, 0 }, { 800, 600, 0 });
g_gfx->Triangle({ 400, 10, 0 }, { 10, 500, 0 }, { 750, 550, 0 });
g_gfx->TriangleSolid({ 0, 0, 0 }, { 100, 0, 0 }, { 10, 250, 0 });
//g_gfx->Dot({ 400, 300, 0.5f });
//g_gfx->Line({ 20, 20, 0 }, { 800, 600, 0 });
//g_gfx->Triangle({ 400, 10, 0 }, { 10, 500, 0 }, { 750, 550, 0 });
//g_gfx->TriangleSolid({ 0, 0, 0 }, { 100, 0, 0 }, { 10, 250, 0 });
//g_gfx->Love();
base->spr9->Render();
//base->spr9->Render();
}
void Release()
+47 -2
View File
@@ -20,6 +20,7 @@ public:
UIMain()
{
_panel = g_ui->CreatePanel();
_panel->SetAutoRun(false);
_panel->SetPosScale({ 0.5f, 0.1f });
_panel->SetLocator("main");
@@ -72,10 +73,21 @@ public:
_btn->Render();
_lbx->Render();
_panel->Update();
_panel->Render();
}
else if (name == "gfx")
{
_uiInfo._txtTitle->SetString("Graphics 基础绘图");
_uiInfo._txtInfo->SetString("基本几何图形绘制");
_uiInfo._panelInfo->Update();
if (_uiInfo._btnBack->IsClick())
{
_panel->SetSelect(-1);
}
_uiInfo._panelInfo->Render();
}
@@ -86,7 +98,40 @@ public:
delete _lbx;
}
private:
UI::Panel* _panel;
UI::Panel* _panel;// 主菜单
struct UIInfo
{
UI::Panel* _panelInfo;// 界面介绍
UI::Text* _txtTitle;
UI::Text* _txtInfo;
UI::ButtonSprite* _btnBack;
UIInfo()
{
_panelInfo = g_ui->CreatePanel();
_panelInfo->SetAutoRun(false);
_panelInfo->SetPosScale({ 0.0f, 0.0f });
_panelInfo->SetLocator("main_info");
_panelInfo->Add(createComponents());
_txtTitle = g_ui->CreateText("t_normal");
_panelInfo->Add(_txtTitle->GetCpt());
_txtInfo = g_ui->CreateText("c_normal");
_panelInfo->Add(_txtInfo->GetCpt());
_panelInfo->Add(createComponents());
_btnBack = g_ui->CreateButtonSpriteDefault();
_btnBack->SetString("返回");
_panelInfo->Add(_btnBack->GetCpt());
}
}_uiInfo;
UI::ButtonTextColor *_btn;
UI::ListBox* _lbx;
+6
View File
@@ -0,0 +1,6 @@
{
"type": "locator",
"name": "main_info",
"dt": [0, 32],
"layout": "y"
}
+10
View File
@@ -0,0 +1,10 @@
{
"comment": "用于一般内容的文字",
"type": "text",
"name": "c_normal",
"id": 0,
"size": 24,
"color": "white",
"string": "内容",
"anchor": "center"
}
+10
View File
@@ -0,0 +1,10 @@
{
"comment": "用于一般标题的文字",
"type": "text",
"name": "t_normal",
"id": 0,
"size": 32,
"color": "white",
"string": "标题",
"anchor": "center"
}