197 lines
3.5 KiB
C++
197 lines
3.5 KiB
C++
#include "ui/dl_ui_panel.h"
|
|
|
|
#include "io/dl_file.h"
|
|
#include "ui/dl_ui_manager.h"
|
|
|
|
namespace dl::UI
|
|
{
|
|
|
|
void Panel::Add(Components* cpt)
|
|
{
|
|
//ElementData组件处理
|
|
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(toSizeNeg2Zero(size), offset);
|
|
|
|
// 组件处理
|
|
for (AnyType& iter : cpt->GetVectorAny())
|
|
{
|
|
if (auto comp = iter.Cast<Cpt::GetCoor>())
|
|
{
|
|
Coor* coor = comp->Call();
|
|
coor->SetParent(_coor);
|
|
coor->SetPosition(pos);
|
|
|
|
//if (_bDisableLocator)//禁用定位器,允许用户手动设置位置
|
|
// coor->DtPosition(offset);
|
|
//else
|
|
// coor->SetPosition(_locator.Get() + offset);
|
|
}
|
|
else if (auto comp = iter.Cast<Cpt::SetOrder>())
|
|
{
|
|
comp->Call(_order);
|
|
}
|
|
else if (auto comp = iter.Cast<Panel*>())
|
|
{
|
|
*comp = this;
|
|
}
|
|
}
|
|
|
|
|
|
//元素是 spr
|
|
/*if (auto cpt = cpts->Cast<Sprite>())
|
|
{
|
|
_add_sprite(cpt);
|
|
}*/
|
|
|
|
|
|
|
|
_ele.push_back(cpt);
|
|
}
|
|
|
|
void Panel::Add(std::string_view json_path)
|
|
{
|
|
std::ifstream ifs = File::Get(json_path);
|
|
if (!ifs.is_open())
|
|
{
|
|
log_err("json文件并不存在:{}", json_path);
|
|
return;
|
|
}
|
|
json j = json::parse(ifs);
|
|
for (auto& iter : j)
|
|
{
|
|
std::string type;
|
|
std::string str;
|
|
Json::Get(iter, "type", type);
|
|
Json::Get(iter, "string", str);
|
|
//解析type
|
|
std::vector<std::string> vec_type = String::Split(type, '.');
|
|
if (vec_type.size() != 2)
|
|
{
|
|
log_err0("元素的type应该有两个字段!");
|
|
continue;
|
|
}
|
|
ElementType ele_type = String2ElementType(vec_type[0]);
|
|
if (ele_type == ElementType::UNKNOWN)
|
|
continue;
|
|
if (ele_type == ElementType::BUTTON)
|
|
{
|
|
ButtonSprite* btn = g_ui->CreateButtonSprite(vec_type[1]);
|
|
btn->SetString(lang(str));
|
|
Add(btn->GetCpt());
|
|
}
|
|
|
|
//debug(type + str);
|
|
}
|
|
}
|
|
|
|
Panel* Panel::Clone()
|
|
{
|
|
Panel* ret = g_ui->CreatePanel(_node->_parent->_v);
|
|
|
|
NTree<Panel*>::Node* node = ret->_node;
|
|
Coor* coor = ret->_coor;
|
|
|
|
*ret = *this;
|
|
|
|
ret->_coor = coor;
|
|
ret->_node = node;
|
|
if(_bg)
|
|
ret->_bg = _bg->Clone();
|
|
|
|
for (Panel* panel : _child)
|
|
{
|
|
ret->_child.push_back(panel->Clone());
|
|
}
|
|
for (Components* cpt : _ele)
|
|
{
|
|
Cpt::Clone* comp = cpt->Get<Cpt::Clone>();
|
|
if (!comp)
|
|
{
|
|
log_err("可复制panel的元素必须包含Clone组件!");
|
|
continue;
|
|
}
|
|
ret->_ele.push_back(comp->Call());
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
void Panel::SetLocator(std::string_view name)
|
|
{
|
|
SetLocator(g_ui->CreateLocator(name));
|
|
}
|
|
|
|
void Panel::Update()
|
|
{
|
|
_bUpdate = true;
|
|
if (_bNeedRefreshPos)
|
|
{
|
|
_bNeedRefreshPos = false;
|
|
|
|
_refresh_pos(_node->GetParent()->GetValue()->GetSize());
|
|
}
|
|
for (Components* cpt : _ele)
|
|
{
|
|
Cpt::Update* comp = cpt->Get<Cpt::Update>();
|
|
if(!comp)
|
|
continue;
|
|
comp->Call();
|
|
|
|
//如果自己是radio且打开,则将其它radio变为非open
|
|
if (UI::Button* c = cpt->Cast<UI::Button>())
|
|
{
|
|
if (c->GetMode() == UI::Button::Mode::RADIO
|
|
&& c->IsSelect())
|
|
{
|
|
for (Components* cpt1 : _ele)
|
|
{
|
|
if (cpt1 != cpt)
|
|
{
|
|
UI::Button* c1 = cpt1->Cast<UI::Button>();
|
|
if (c1 && c1->GetMode() == UI::Button::Mode::RADIO)
|
|
{
|
|
c1->SetSelect(false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void Panel::Render()
|
|
{
|
|
_bRender = true;
|
|
/*if (_bg)
|
|
{
|
|
_bg->_spr->Render();
|
|
}*/
|
|
|
|
for (Components* cpt : _ele)
|
|
{
|
|
Cpt::Render* comp = cpt->Get<Cpt::Render>();
|
|
if (!comp)
|
|
continue;
|
|
comp->Call();
|
|
}
|
|
}
|
|
|
|
void Panel::_refresh_pos(const Size& size)
|
|
{
|
|
_coor->SetPosition(_posPixel + _posScale * toPosition(size));
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|