diff --git a/dl/dl_component.h b/dl/dl_component.h index 675b9ebe..01f66181 100644 --- a/dl/dl_component.h +++ b/dl/dl_component.h @@ -241,6 +241,16 @@ public: VectorAny& GetVectorAny() { return _vec; } + std::string GetName() + { + return _name; + } + + //! 设置自定义名字 + void SetName(std::string_view name) + { + _name = name; + } private: VectorAny _vec; AnyType _any; @@ -291,6 +301,7 @@ namespace Cpt using Render = Function, str_uid("Render")>;//!< 绘制 using SetOrder = Function, str_uid("SetOrder")>;//!< 设置order using DeleteThis = Function, str_uid("DeleteThis")>;//!< 删除自身 + using Clone = Function, str_uid("Clone")>;//!< 克隆 //! 语言本地化 class Lang diff --git a/dl/ui/dl_ui_button_base.h b/dl/ui/dl_ui_button_base.h index 80ad6410..decc81f0 100644 --- a/dl/ui/dl_ui_button_base.h +++ b/dl/ui/dl_ui_button_base.h @@ -210,4 +210,6 @@ private: void _update_radio(); }; +using ButtonMode = ButtonBase::Mode; + } \ No newline at end of file diff --git a/dl/ui/dl_ui_list_box.h b/dl/ui/dl_ui_list_box.h index 29b5175a..6e028193 100644 --- a/dl/ui/dl_ui_list_box.h +++ b/dl/ui/dl_ui_list_box.h @@ -88,6 +88,7 @@ public: { _bg->SetOrder(order); //_sprOver->SetOrder(Math::OffsetOrder(order, 0, 0, -2)); + _item->SetOrder(Math::OffsetOrder(order, 0, 0, -1)); for (Panel* panel : _allItem) { panel->SetOrder(Math::OffsetOrder(order, 0, 0, -1)); diff --git a/dl/ui/dl_ui_manager.cpp b/dl/ui/dl_ui_manager.cpp index 7034d619..2e00ba64 100644 --- a/dl/ui/dl_ui_manager.cpp +++ b/dl/ui/dl_ui_manager.cpp @@ -74,6 +74,11 @@ dl::Locator::SL Manager::CreateLocator(std::string_view name) return iter->second; } +dl::UI::Text* Manager::CreateTextDefault() +{ + return UI::Text::Create(g_factory->CreateTextDefault()); +} + MsgBox* Manager::CreateMsgBoxDefault() { Background* bg = CreateBackgroundDefault(); diff --git a/dl/ui/dl_ui_manager.h b/dl/ui/dl_ui_manager.h index 4740ca31..e75df435 100644 --- a/dl/ui/dl_ui_manager.h +++ b/dl/ui/dl_ui_manager.h @@ -98,7 +98,7 @@ public: CLOSE, }; - + UI::Text* CreateTextDefault(); MsgBox* CreateMsgBoxDefault(); ButtonSprite* CreateButtonSpriteDefault(ButtonStyle style = ButtonStyle::NORMAL); Background* CreateBackgroundDefault(); @@ -142,7 +142,7 @@ public: * @brief Components克隆 * @note 由于类型信息丢失,只能枚举克隆 */ - Components* CloneCpt(Components* cpt) + /*Components* CloneCpt(Components* cpt) { if (auto p = cpt->Cast()) return p->Clone()->GetCpt(); @@ -150,7 +150,7 @@ public: return p->Clone()->GetCpt(); assert(0); return nullptr; - } + }*/ /** * @brief 绘制调试信息 diff --git a/dl/ui/dl_ui_panel.cpp b/dl/ui/dl_ui_panel.cpp index bc1fad07..9f89e124 100644 --- a/dl/ui/dl_ui_panel.cpp +++ b/dl/ui/dl_ui_panel.cpp @@ -107,7 +107,8 @@ Panel* Panel::Clone() ret->_coor = Coor::Create(); ret->_node = node; - ret->_bg = _bg->Clone(); + if(_bg) + ret->_bg = _bg->Clone(); for (Panel* panel : _child) { @@ -115,7 +116,13 @@ Panel* Panel::Clone() } for (Components* cpt : _ele) { - ret->_ele.push_back(g_ui->CloneCpt(cpt)); + Cpt::Clone* comp = cpt->Get(); + if (!comp) + { + log_err("可复制panel的元素必须包含Clone组件!"); + continue; + } + ret->_ele.push_back(comp->Call()); } return ret; } diff --git a/dl/ui/dl_ui_panel.h b/dl/ui/dl_ui_panel.h index 02776b1d..d6ad5437 100644 --- a/dl/ui/dl_ui_panel.h +++ b/dl/ui/dl_ui_panel.h @@ -132,7 +132,7 @@ public: * @brief 返回被点击的元素 * @retval 没有返回-1 */ - size_t GetIndexCilck() + size_t GetClickIndex() { size_t i = 0; for (auto& iter : _ele) @@ -147,6 +147,29 @@ public: return -1; } + /** + * @brief 返回被选中的元素 + * @retval 没有返回-1 + */ + Components* GetSelect() + { + for (auto& iter : _ele) + { + if (auto cpt = iter->Get()) + { + if (cpt->Call()) + return iter; + } + } + return nullptr; + } + + std::string GetSelectName() + { + Components* cpt = GetSelect(); + return cpt ? cpt->GetName() : ""; + } + /** * @brief index元素是否open状态 * @retval 失败返回false,并输出日志 @@ -188,8 +211,7 @@ public: //! 返回第一个text UI::Text* GetText() { - //Cpt::Get(_ele) - return nullptr; + return Cpt::GetFirst(_ele); } //! 是否被鼠标选中 diff --git a/dl/ui/dl_ui_text.h b/dl/ui/dl_ui_text.h index 1a3d8593..5d3407b4 100644 --- a/dl/ui/dl_ui_text.h +++ b/dl/ui/dl_ui_text.h @@ -123,9 +123,15 @@ private: Cpt::GetCoor _cptGetCoor; Cpt::Lang _cptLang; Cpt::ElementData _cptElemData; + Cpt::Clone _cptClone; void _delete_this() { delete this; } + Components* _clone() + { + return Clone()->GetCpt(); + } + void _init_cpt() { _cptDeleteThis = std::bind(&Text::_delete_this, this); @@ -133,12 +139,15 @@ private: _cptRender = std::bind(&Text::Render, this); _cptGetCoor = std::bind(&Text::GetCoor, this); _cptSetOrder = std::bind(&Text::SetOrder, this, std::placeholders::_1); + _cptClone = std::bind(&Text::_clone, this); _cpt.PushBack(&_cptDeleteThis); _cpt.PushBack(&_cptSetOrder); _cpt.PushBack(&_cptRender); _cpt.PushBack(&_cptGetCoor); _cpt.PushBack(&_cptSetOrder); + _cpt.PushBack(&_cptClone); + _cpt.PushBack(&_cptElemData); } diff --git a/dltest/main.cpp b/dltest/main.cpp index 2a4d9c85..a1af0f6e 100644 --- a/dltest/main.cpp +++ b/dltest/main.cpp @@ -90,7 +90,7 @@ void Run() 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(); + //g_gfx->Love(); base->spr9->Render(); } diff --git a/dltest/ui_main.h b/dltest/ui_main.h index f8f14f21..c1a7071a 100644 --- a/dltest/ui_main.h +++ b/dltest/ui_main.h @@ -29,8 +29,15 @@ public: _panel->Add(txt->GetCpt()); } + std::array menu = { + "gfx", "sprite", "text" + }; + for(auto& iter : menu) { UI::ButtonSprite* btn = g_ui->CreateButtonSpriteDefault(); + btn->GetCpt()->SetName(iter); + btn->SetMode(UI::ButtonMode::RADIO); + btn->SetString(iter); _panel->Add(btn->GetCpt()); } @@ -44,22 +51,40 @@ public: { UI::Background* bg = g_ui->CreateBackgroundDefault(); UI::Panel* panel = g_ui->CreatePanel(); + { + UI::Text* txt = g_ui->CreateTextDefault(); + panel->Add(txt->GetCpt()); + } _lbx = UI::ListBox::Create(bg, panel); - UI::Panel* item = _lbx->PushBackItem(); - UI::Text* txt = item->GetText(); - if(txt) - txt->SetString("lbx0"); + for (std::size_t i = 0; i < 10; ++i) + { + UI::Panel* item = _lbx->PushBackItem(); + UI::Text* txt = item->GetText(); + if (txt) + txt->SetString(fmt::format("lbx________________{}", i)); + } + } } void Run() { - _btn->Update(); - _lbx->Update(); + std::string name = _panel->GetSelectName(); + if (name.empty()) + { + _btn->Update(); + _lbx->Update(); - _btn->Render(); - _lbx->Render(); + _btn->Render(); + _lbx->Render(); + } + else if (name == "gfx") + { + + } + + } ~UIMain() {