442 lines
8.5 KiB
C++
442 lines
8.5 KiB
C++
#include "ui/dl_ui_panel.h"
|
||
|
||
#ifndef DL_DISABLE_RENDER
|
||
#include "io/dl_file.h"
|
||
#include "ui/dl_ui_manager.h"
|
||
#include "ui/dl_ui_edit_box.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{};
|
||
int order_offset{};
|
||
if (ele_data)
|
||
{//计算offset
|
||
offset = ele_data->GetOffset();
|
||
size = ele_data->GetSize();
|
||
order_offset = ele_data->GetOrderOffset();
|
||
}
|
||
|
||
// 位置计算
|
||
bool is_loc_none = false;
|
||
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 if (_locator.GetMode() == Locator::Mode::NONE)
|
||
{
|
||
is_loc_none = true;
|
||
}
|
||
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);
|
||
|
||
// 空定位器,允许用户手动设置位置
|
||
if(!is_loc_none)
|
||
coor->SetPosition(pos);
|
||
}
|
||
else if (auto comp = iter.Cast<Cpt::SetOrder>())
|
||
{
|
||
comp->Call(Math::OffsetOrder(_order, 0, order_offset));
|
||
}
|
||
else if (auto comp = iter.Cast<Panel*>())
|
||
{// 添加自己为父节点
|
||
*comp = this;
|
||
}
|
||
}
|
||
|
||
_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_auto(str));
|
||
Add(btn);
|
||
}
|
||
|
||
//debug(type + str);
|
||
}
|
||
}
|
||
|
||
|
||
void Panel::AddPanel(Panel* child)
|
||
{
|
||
g_ui->SetPanelParent(child, this);
|
||
}
|
||
|
||
|
||
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;
|
||
|
||
// 复制子节点
|
||
for(auto node_child : _node->GetChild())
|
||
{
|
||
g_ui->SetPanelParent(node_child->GetValue()->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()
|
||
{
|
||
if (_bUpdate)
|
||
{
|
||
log_err("已经调用过Update!");
|
||
return;
|
||
}
|
||
_bUpdate = true;
|
||
if (_bNeedRefreshPos)
|
||
{
|
||
_bNeedRefreshPos = false;
|
||
_refresh_pos(GetParent()->GetSize());
|
||
}
|
||
for (Components* cpt : _allEle)
|
||
{
|
||
if (Cpt::ElementData* comp = cpt->Get<Cpt::ElementData>())
|
||
{
|
||
if(!comp->_bRunAble || !comp->_bUpdateAble)
|
||
continue;
|
||
}
|
||
|
||
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;
|
||
if (_bRender)
|
||
{
|
||
log_warn("已经调用过Render:{}", _name);
|
||
return;
|
||
}
|
||
|
||
// 这里包含自动调用、手动调用的情况,但自动调用后执行
|
||
_bRender = true;
|
||
/*if (_bg)
|
||
{
|
||
_bg->_spr->Render();
|
||
}*/
|
||
|
||
for (Components* cpt : _allEle)
|
||
{
|
||
if (Cpt::ElementData* comp = cpt->Get<Cpt::ElementData>())
|
||
{
|
||
if (!comp->_bRunAble || !comp->_bRenderAble)
|
||
continue;
|
||
}
|
||
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));
|
||
|
||
for (auto& iter : _node->GetChild())
|
||
{
|
||
Panel* child = iter->GetValue();
|
||
if (child->_bAutoCoor)
|
||
child->_refresh_pos(GetSize());
|
||
}
|
||
}
|
||
|
||
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();
|
||
}
|
||
else if(child->_bAutoRender && child->_bUpdate && !child->_bRender)
|
||
{
|
||
child->Render();
|
||
}
|
||
}
|
||
}
|
||
|
||
void Panel::_refresh_size()
|
||
{
|
||
if (_bSizeFull)
|
||
{
|
||
// 父节点大小
|
||
if (_node->GetParent())
|
||
{
|
||
_size = _node->GetParent()->GetValue()->_size;
|
||
}
|
||
else
|
||
{
|
||
_size = g_system->GetWindowSize();
|
||
}
|
||
}
|
||
|
||
// 子元素相关处理
|
||
for (auto& iter : _allEle)
|
||
{
|
||
if (UI::Sprite* spr = iter->Cast<UI::Sprite>())
|
||
{
|
||
if (spr->GetAutoSize() != Sprite::AutoSize::NONE)
|
||
{
|
||
spr->SetMode(spr->GetAutoSize(), spr->GetFillMode());
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
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();
|
||
|
||
// 位置计算
|
||
bool is_loc_none = false;
|
||
Position pos = offset;
|
||
if (_locator.GetMode() == Locator::Mode::DIRECT)
|
||
pos += _locator.Get(i);
|
||
else if (_locator.GetMode() == Locator::Mode::NONE)
|
||
is_loc_none = true;
|
||
else
|
||
assert(0);
|
||
|
||
// 组件处理
|
||
for (AnyType& iter : cpt->GetVectorAny())
|
||
{
|
||
if (auto comp = iter.Cast<Cpt::GetCoor>())
|
||
{
|
||
Coor* coor = comp->Call();
|
||
coor->SetParent(_coor);
|
||
if (!is_loc_none)
|
||
coor->SetPosition(pos);
|
||
else
|
||
;
|
||
|
||
|
||
//if (_bDisableLocator)//禁用定位器,允许用户手动设置位置
|
||
// coor->DtPosition(offset);
|
||
//else
|
||
// coor->SetPosition(_locator.Get() + offset);
|
||
}
|
||
}
|
||
}
|
||
|
||
void Panel::_refresh_order()
|
||
{
|
||
switch (_orderMode)
|
||
{
|
||
case Panel::AUTO:
|
||
{
|
||
auto node_parent = _node->GetParent();
|
||
if (node_parent)
|
||
{
|
||
Panel* parent = node_parent->GetValue();
|
||
size_t i = parent->GetChildIndex(this);
|
||
_order = Math::OffsetOrder(parent->_order, 0, -(int)i - 1);
|
||
}
|
||
}
|
||
break;
|
||
case Panel::CUSTOM:
|
||
{
|
||
auto node_parent = _node->GetParent();
|
||
if (node_parent)
|
||
{
|
||
Panel* parent = node_parent->GetValue();
|
||
size_t i = parent->GetChildIndex(this);
|
||
_order = Math::OffsetOrder(parent->_order, 0, _orderInParent);
|
||
}
|
||
}
|
||
break;
|
||
case Panel::FORCE:
|
||
default:
|
||
break;
|
||
}
|
||
// 更新元素
|
||
for (Components*& cpt : _allEle)
|
||
{
|
||
if (auto comp = cpt->Cast<Cpt::SetOrder>())
|
||
{
|
||
comp->Call(Math::OffsetOrder(_order, 0, 0, -1));
|
||
}
|
||
}
|
||
// 更新子节点
|
||
for (auto& iter : _node->GetChild())
|
||
{
|
||
Panel* child = iter->GetValue();
|
||
child->_refresh_order();
|
||
}
|
||
}
|
||
|
||
dl::UI::ButtonSprite* Panel::ExGetButtonSprite()
|
||
{
|
||
auto* btn = GetElement<UI::Button>();
|
||
auto* ret = dynamic_cast<UI::ButtonSprite*>(btn);
|
||
if (!ret)
|
||
log_warn("获取第{}个元素失败!", 0);
|
||
return ret;
|
||
}
|
||
|
||
dl::UI::EditBox* Panel::ExGetEditBox(size_t n /*= 0*/)
|
||
{
|
||
auto ret = GetElement<UI::EditBox>(n);
|
||
if (!ret)
|
||
log_warn("获取第{}个元素失败!", n);
|
||
return ret;
|
||
}
|
||
|
||
dl::UI::Text* Panel::ExGetText(size_t n /*= 0*/)
|
||
{
|
||
auto ret = GetElement<UI::Text>(n);
|
||
if (!ret)
|
||
log_warn("获取第{}个元素失败!", n);
|
||
return ret;
|
||
}
|
||
|
||
void Panel::ExSetString(std::string_view str, size_t n /*= 0*/)
|
||
{
|
||
UI::Text* txt = ExGetText(n);
|
||
if (txt)
|
||
txt->SetString(str);
|
||
}
|
||
|
||
void Panel::ExSetButtonSpriteString(std::string_view str, size_t n /*= 0*/)
|
||
{
|
||
auto btn = ExGetButtonSprite();
|
||
if (!btn)
|
||
return;
|
||
btn->SetString(str);
|
||
}
|
||
|
||
}
|
||
|
||
#endif |