diff --git a/dl/dl_component.h b/dl/dl_component.h index 01f66181..296bb403 100644 --- a/dl/dl_component.h +++ b/dl/dl_component.h @@ -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, str_uid("Update")>;//!< 逻辑 using Render = Function, str_uid("Render")>;//!< 绘制 using SetOrder = Function, str_uid("SetOrder")>;//!< 设置order + using SetOpen = Function, str_uid("SetOpen")>;//!< 设置是否打开 using DeleteThis = Function, str_uid("DeleteThis")>;//!< 删除自身 using Clone = Function, str_uid("Clone")>;//!< 克隆 @@ -325,6 +333,11 @@ namespace Cpt { return -Align::Size2Anchor(_size, _hv) + _anchor; } + + Position GetSize() + { + return _size; + } public: Position _size; Position _anchor; diff --git a/dl/ui/dl_ui_button_base.cpp b/dl/ui/dl_ui_button_base.cpp index c4088d3a..a9bd6c0b 100644 --- a/dl/ui/dl_ui_button_base.cpp +++ b/dl/ui/dl_ui_button_base.cpp @@ -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; } diff --git a/dl/ui/dl_ui_button_base.h b/dl/ui/dl_ui_button_base.h index decc81f0..b90ebd45 100644 --- a/dl/ui/dl_ui_button_base.h +++ b/dl/ui/dl_ui_button_base.h @@ -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(b).GetCoor()->GetParent()); + _coor = Coor::Create(const_cast(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; } \ No newline at end of file diff --git a/dl/ui/dl_ui_button_normal.h b/dl/ui/dl_ui_button_normal.h index 47e2c562..34947b99 100644 --- a/dl/ui/dl_ui_button_normal.h +++ b/dl/ui/dl_ui_button_normal.h @@ -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: /** diff --git a/dl/ui/dl_ui_button_sprite.h b/dl/ui/dl_ui_button_sprite.h index 3514919a..584565be 100644 --- a/dl/ui/dl_ui_button_sprite.h +++ b/dl/ui/dl_ui_button_sprite.h @@ -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& mul_spr); - ArrayEnum _spr; + ArrayEnum _spr; std::array _sprDisable; struct TextData diff --git a/dl/ui/dl_ui_button_text_color.h b/dl/ui/dl_ui_button_text_color.h index 61c96bcd..6604e780 100644 --- a/dl/ui/dl_ui_button_text_color.h +++ b/dl/ui/dl_ui_button_text_color.h @@ -19,7 +19,7 @@ namespace dl::UI { //! 文本变色按钮 -class ButtonTextColor : public ButtonBase +class ButtonTextColor : public Button { public: /** diff --git a/dl/ui/dl_ui_manager.cpp b/dl/ui/dl_ui_manager.cpp index b584b50f..b59762f9 100644 --- a/dl/ui/dl_ui_manager.cpp +++ b/dl/ui/dl_ui_manager.cpp @@ -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); diff --git a/dl/ui/dl_ui_panel.cpp b/dl/ui/dl_ui_panel.cpp index 9f89e124..e4dc3ad0 100644 --- a/dl/ui/dl_ui_panel.cpp +++ b/dl/ui/dl_ui_panel.cpp @@ -12,27 +12,24 @@ void Panel::Add(Components* cpt) Cpt::ElementData* ele_data = cpt->Get(); 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()) { 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()) + if (UI::Button* c = cpt->Cast()) { - 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(); - if (c1 && c1->GetMode() == UI::ButtonBase::Mode::RADIO) + UI::Button* c1 = cpt1->Cast(); + if (c1 && c1->GetMode() == UI::Button::Mode::RADIO) { c1->SetOpen(false); } diff --git a/dl/ui/dl_ui_panel.h b/dl/ui/dl_ui_panel.h index d6ad5437..00b03a11 100644 --- a/dl/ui/dl_ui_panel.h +++ b/dl/ui/dl_ui_panel.h @@ -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->Call(i == index); + } + } + } + /** * @brief 执行逻辑 * @node 默认情况会自动调用 diff --git a/dltest/main.cpp b/dltest/main.cpp index a1af0f6e..91b6b3ff 100644 --- a/dltest/main.cpp +++ b/dltest/main.cpp @@ -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() diff --git a/dltest/ui_main.h b/dltest/ui_main.h index 26a71acb..08e426e8 100644 --- a/dltest/ui_main.h +++ b/dltest/ui_main.h @@ -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; diff --git a/res/ui/locator/main_info.json b/res/ui/locator/main_info.json new file mode 100644 index 00000000..3acb06fb --- /dev/null +++ b/res/ui/locator/main_info.json @@ -0,0 +1,6 @@ +{ + "type": "locator", + "name": "main_info", + "dt": [0, 32], + "layout": "y" +} \ No newline at end of file diff --git a/res/ui/text/c_normal.json b/res/ui/text/c_normal.json new file mode 100644 index 00000000..2469c916 --- /dev/null +++ b/res/ui/text/c_normal.json @@ -0,0 +1,10 @@ +{ + "comment": "用于一般内容的文字", + "type": "text", + "name": "c_normal", + "id": 0, + "size": 24, + "color": "white", + "string": "内容", + "anchor": "center" +} diff --git a/res/ui/text/t_normal.json b/res/ui/text/t_normal.json new file mode 100644 index 00000000..212a1615 --- /dev/null +++ b/res/ui/text/t_normal.json @@ -0,0 +1,10 @@ +{ + "comment": "用于一般标题的文字", + "type": "text", + "name": "t_normal", + "id": 0, + "size": 32, + "color": "white", + "string": "标题", + "anchor": "center" +}