314 lines
5.8 KiB
C++
314 lines
5.8 KiB
C++
#include "ui/dl_ui_panel.h"
|
|
|
|
#include "io/dl_file.h"
|
|
#include "ui/dl_ui_manager.h"
|
|
#include "base/dl_range_find.h"
|
|
|
|
namespace dl::UI
|
|
{
|
|
|
|
void Panel::AddCpt(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 = offset;
|
|
if (_locator.GetMode() == Locator::Mode::DIRECT)
|
|
pos += _locator.Get(_allEle.size());
|
|
else if (_locator.GetMode() == Locator::Mode::ADAPT)
|
|
pos += _locator.Get(toSizeNeg2Zero(size));
|
|
else
|
|
assert(0);
|
|
|
|
//元素是 spr
|
|
//if (Text* txt = cpt->Cast<Text>())
|
|
//{
|
|
// txt->GetCoor()->SetParent(_coor);
|
|
// txt->GetText()->_update_dirty_scale();
|
|
// txt->GetText()->Refresh();
|
|
//}
|
|
|
|
// 组件处理
|
|
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);
|
|
}*/
|
|
|
|
|
|
|
|
_allEle.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);
|
|
}
|
|
|
|
//debug(type + str);
|
|
}
|
|
}
|
|
|
|
Panel* Panel::Clone()
|
|
{
|
|
Panel* ret = g_ui->CreatePanel(_node->GetParent()->GetValue());
|
|
|
|
NTree<Panel*>::Node* node = ret->_node;
|
|
Coor* coor = ret->_coor;
|
|
|
|
*ret = *this;
|
|
|
|
ret->_coor = coor;
|
|
ret->_coor->SetParent(_coor->GetParent());
|
|
ret->_node = node;
|
|
if(_bg)
|
|
ret->_bg = _bg->Clone();
|
|
|
|
ret->_child.clear();
|
|
for (Panel* panel : _child)
|
|
{
|
|
g_ui->SetPanelParent(panel->Clone(), ret);
|
|
}
|
|
|
|
ret->_allEle.clear();
|
|
for (Components* cpt : _allEle)
|
|
{
|
|
Cpt::Clone* comp = cpt->Get<Cpt::Clone>();
|
|
if (!comp)
|
|
{
|
|
log_err0("可复制panel的元素必须包含Clone组件!");
|
|
continue;
|
|
}
|
|
ret->AddCpt(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 : _allEle)
|
|
{
|
|
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 : _allEle)
|
|
{
|
|
if (cpt1 != cpt)
|
|
{
|
|
UI::Button* c1 = cpt1->Cast<UI::Button>();
|
|
if (c1 && c1->GetMode() == UI::Button::Mode::RADIO)
|
|
{
|
|
c1->SetSelect(false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
_update_child();
|
|
}
|
|
|
|
void Panel::Render()
|
|
{
|
|
if (g_ui->IsHide())
|
|
return;
|
|
|
|
_bRender = true;
|
|
/*if (_bg)
|
|
{
|
|
_bg->_spr->Render();
|
|
}*/
|
|
|
|
for (Components* cpt : _allEle)
|
|
{
|
|
Cpt::Render* comp = cpt->Get<Cpt::Render>();
|
|
if (!comp)
|
|
continue;
|
|
comp->Call();
|
|
}
|
|
_render_child();
|
|
}
|
|
|
|
void Panel::_refresh_pos(const Size& size)
|
|
{
|
|
assert(_bAutoCoor);
|
|
_coor->SetPosition(_posPixel + _posScale * toPosition(size));
|
|
}
|
|
|
|
void Panel::_update_child()
|
|
{
|
|
for (auto& iter : _node->GetChild())
|
|
{
|
|
Panel* child = iter->GetValue();
|
|
if (child->_bAutoRun)
|
|
{
|
|
child->Update();
|
|
}
|
|
}
|
|
}
|
|
|
|
void Panel::_render_child()
|
|
{
|
|
for (auto& iter : _node->GetChild())
|
|
{
|
|
Panel* child = iter->GetValue();
|
|
if (child->_bAutoRun)
|
|
{
|
|
child->Render();
|
|
}
|
|
}
|
|
}
|
|
|
|
void Panel::_refresh_size(const Size& size)
|
|
{
|
|
for (auto& iter : _allEle)
|
|
{
|
|
if (UI::Sprite* spr = iter->Cast<UI::Sprite>())
|
|
{
|
|
if (Every::Zero(spr->GetSize()))
|
|
{
|
|
spr->SetMode(size, spr->GetMode());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void Panel::OnChangeElementData(Components* cpt)
|
|
{
|
|
std::size_t i = Find::FirstIndex(_allEle, cpt);
|
|
if (i == -1)
|
|
{
|
|
log_err0("不属于Panel的成员!");
|
|
return;
|
|
}
|
|
//ElementData组件处理
|
|
Cpt::ElementData* ele_data = cpt->Get<Cpt::ElementData>();
|
|
if (!ele_data)
|
|
{
|
|
log_err0("ElementData不存在!");
|
|
return;
|
|
}
|
|
Position offset = ele_data->GetOffset();
|
|
Position size = ele_data->GetSize();
|
|
|
|
// 位置计算
|
|
Position pos = offset;
|
|
if (_locator.GetMode() == Locator::Mode::DIRECT)
|
|
pos += _locator.Get(i);
|
|
else
|
|
assert(0);
|
|
|
|
// 组件处理
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Panel::_refresh_order()
|
|
{
|
|
if (_bg)
|
|
_bg->SetOrder(Math::OffsetOrder(_order, 0, 0, 0));
|
|
for (Components*& cpt : _allEle)
|
|
{
|
|
if (auto comp = cpt->Cast<Cpt::SetOrder>())
|
|
{
|
|
comp->Call(Math::OffsetOrder(_order, 0, 0, -1));
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|