From c0650840da32fa17d339d57aadf3a032b3005e09 Mon Sep 17 00:00:00 2001 From: lveyou <1339484752@qq.com> Date: Tue, 24 Oct 2023 20:57:29 +0800 Subject: [PATCH] =?UTF-8?q?*=20listbox=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dl/render/dl_factory.h | 2 +- dl/ui/dl_ui_button_base.h | 9 +++++++++ dl/ui/dl_ui_list_box.cpp | 5 ++--- dl/ui/dl_ui_list_box.h | 25 ++++++++++++++++--------- dl/ui/dl_ui_manager.cpp | 9 +++++++-- dl/ui/dl_ui_panel.h | 15 +++++++++++++++ 6 files changed, 50 insertions(+), 15 deletions(-) diff --git a/dl/render/dl_factory.h b/dl/render/dl_factory.h index 7306420e..b3c12062 100644 --- a/dl/render/dl_factory.h +++ b/dl/render/dl_factory.h @@ -221,7 +221,7 @@ public: gcText CreateTextDefault() { - gcText ret = CreateText(0, 16, ColorDef::RED, 1, ColorDef::GREEN); + gcText ret = CreateText(0, 16, ColorDef::WHITE); ret->SetString("Default"); return ret; } diff --git a/dl/ui/dl_ui_button_base.h b/dl/ui/dl_ui_button_base.h index 47b55063..34edfac6 100644 --- a/dl/ui/dl_ui_button_base.h +++ b/dl/ui/dl_ui_button_base.h @@ -109,6 +109,7 @@ public: Coor* GetCoor() { return _coor; } + virtual Button* Clone() = 0; virtual bool IsPickup() = 0; virtual void SetOrder(float order) = 0; virtual Position GetSize() = 0; @@ -174,9 +175,15 @@ private: Cpt::IsSelect _cptIsOpen; Cpt::SetSelect _cptSetOpen; Cpt::ElementData _cptElemData; + Cpt::Clone _cptClone; void _delete_this() { delete this; } + Components* _clone() + { + return Clone()->GetCpt(); + } + void _init_cpt() { _cptDeleteThis = std::bind(&Button::_delete_this, this); @@ -188,6 +195,7 @@ private: _cptIsClick = std::bind(&Button::IsClick, this); _cptIsOpen = std::bind(&Button::IsSelect, this); _cptSetOpen = std::bind(&Button::SetSelect, this, std::placeholders::_1); + _cptClone = std::bind(&Button::_clone, this); _cpt.PushBack(&_cptDeleteThis); _cpt.PushBack(&_cptUpdate); @@ -198,6 +206,7 @@ private: _cpt.PushBack(&_cptIsClick); _cpt.PushBack(&_cptIsOpen); _cpt.PushBack(&_cptSetOpen); + _cpt.PushBack(&_cptClone); _cpt.PushBack(&_cptElemData); } diff --git a/dl/ui/dl_ui_list_box.cpp b/dl/ui/dl_ui_list_box.cpp index 4abc8514..1fe3c83b 100644 --- a/dl/ui/dl_ui_list_box.cpp +++ b/dl/ui/dl_ui_list_box.cpp @@ -5,11 +5,10 @@ namespace dl::UI { -dl::UI::ListBox* ListBox::Create(Background* bg, Panel* item) +dl::UI::ListBox* ListBox::Create(Panel* item) { ListBox* ret = new ListBox; ret->_panel = g_ui->CreatePanel(nullptr); - ret->_bg = bg; ret->_item = item; ret->_init(); @@ -28,7 +27,7 @@ void ListBox::_init() SetOrder(Z_DEFAULT_UI); _cptElemData._hv = Align::LT; - _cptElemData._size = toPosition(_bg->GetSize()); + _cptElemData._size = toPosition(_panel->GetSize());// 初始为0 } } diff --git a/dl/ui/dl_ui_list_box.h b/dl/ui/dl_ui_list_box.h index 3442d790..27cbedf0 100644 --- a/dl/ui/dl_ui_list_box.h +++ b/dl/ui/dl_ui_list_box.h @@ -7,7 +7,7 @@ * @author lveyou * @date 22-12-17 * -* @note +* @note 出于多选支持,不使用over */ #pragma once @@ -24,11 +24,10 @@ class ListBox public: /** * @brief 创建列表框 - * @param[in] bg 背景 * @param[in] panel 元素模板 * @note 背景用于裁剪 */ - static ListBox* Create(Background* bg, Panel* item); + static ListBox* Create(Panel* item); void Update() { @@ -41,17 +40,21 @@ public: void Render() { // - _bg->Render(); + //_bg->Render(); //光标选中 + size_t i = 0; for (Panel* panel : _allItem) { if (panel->IsPickup()) { + panel->SetSelectAll(); + _cur = i; /*_sprOver->GetCoor()->SetPosition(panel->GetPosition()); _sprOver->Render();*/ break; } + ++i; } for (Panel* panel : _allItem) @@ -78,13 +81,13 @@ public: bool IsPickup() { - return _bg->IsPickup(); + return _panel->IsPickup(); } //! 设置绘制顺序 void SetOrder(float order) { - _bg->SetOrder(order); + //_bg->SetOrder(order); //_sprOver->SetOrder(Math::OffsetOrder(order, 0, 0, -2)); _item->SetOrder(Math::OffsetOrder(order, 0, 0, -1)); for (Panel* panel : _allItem) @@ -100,6 +103,13 @@ public: _locItem = loc; } + //! 设置大小 + void SetSize(const Size& size) + { + _panel->SetBackgroundSize(size); + _cptElemData._size = toPosition(size); + } + Coor* GetCoor() { return _panel->GetCoor(); } Components* GetCpt() { return &_cpt; } @@ -109,12 +119,10 @@ public: ~ListBox() { - delete _bg; } private: ListBox(): _cpt{this}, - _bg{nullptr}, _cur{-1_zu} { _init_cpt(); @@ -122,7 +130,6 @@ private: Panel* _panel; Panel* _item; // 模板 - Background* _bg; Locator _locItem;// item布局 size_t _cur; std::vector _allItem; diff --git a/dl/ui/dl_ui_manager.cpp b/dl/ui/dl_ui_manager.cpp index 43a6630e..b5d74a9c 100644 --- a/dl/ui/dl_ui_manager.cpp +++ b/dl/ui/dl_ui_manager.cpp @@ -147,14 +147,19 @@ Background* Manager::CreateBackgroundDefault() ListBox* Manager::CreateListBoxDefault() { - Background* bg = g_ui->CreateBackgroundDefault(); // item Panel* panel = g_ui->CreatePanel(); { Text* txt = g_ui->CreateTextDefault(); panel->Add(txt->GetCpt()); } - ListBox* ret = ListBox::Create(bg, panel); + { + ButtonSprite* btn = g_ui->CreateButtonSpriteDefault(); + btn->SetMode(ButtonMode::SWITCH); + panel->Add(btn->GetCpt()); + } + ListBox* ret = ListBox::Create(panel); + ret->SetSize({200, 600}); ret->SetItemLocator(createLocatorY(16.0f)); return ret; } diff --git a/dl/ui/dl_ui_panel.h b/dl/ui/dl_ui_panel.h index 9e6fb693..0516a521 100644 --- a/dl/ui/dl_ui_panel.h +++ b/dl/ui/dl_ui_panel.h @@ -155,6 +155,19 @@ public: } } /** + * @brief 设置所有子元素为选中 + */ + void SetSelectAll() + { + for (auto& iter : _ele) + { + if (auto comp = iter->Get()) + { + comp->Call(true); + } + } + } + /** * @brief 执行逻辑 * @node 默认情况会自动调用 */ @@ -273,6 +286,8 @@ public: //! 是否被鼠标选中 bool IsPickup() { + if (_bg && _bg->IsPickup()) + return true; for (Components* cpt : _ele) { Cpt::IsPickup* comp = cpt->Get();