Skip to content

Commit

Permalink
xrGame/ui: code optimization and simplification
Browse files Browse the repository at this point in the history
This includes mainly boilerplate removal and C++11 usage
  • Loading branch information
Xottab-DUTY committed Jan 31, 2019
1 parent 91d4bbb commit 58960f8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 113 deletions.
83 changes: 28 additions & 55 deletions src/xrGame/ui/UIInventoryUpgradeWnd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ CUIInventoryUpgradeWnd::CUIInventoryUpgradeWnd()
//(*m_WeaponIconsShader)->create("hud" DELIMITER "default", "ui" DELIMITER "ui_actor_weapons");
// m_OutfitIconsShader = new ui_shader();
//(*m_OutfitIconsShader)->create("hud" DELIMITER "default", "ui" DELIMITER "ui_actor_armor");
m_inv_item = NULL;
m_cur_upgrade_id = NULL;
m_current_scheme = NULL;
m_btn_repair = NULL;
m_inv_item = nullptr;
m_cur_upgrade_id = nullptr;
m_current_scheme = nullptr;
m_btn_repair = nullptr;
}

CUIInventoryUpgradeWnd::~CUIInventoryUpgradeWnd()
Expand All @@ -68,21 +68,9 @@ void CUIInventoryUpgradeWnd::Init()

CUIXmlInit::InitWindow(uiXml, "main", 0, this);

m_item = new CUIStatic();
m_item->SetAutoDelete(true);
AttachChild(m_item);
CUIXmlInit::InitStatic(uiXml, "item_static", 0, m_item);

m_back = new CUIWindow();
m_back->SetAutoDelete(true);
CUIXmlInit::InitWindow(uiXml, "back", 0, m_back);
AttachChild(m_back);

m_scheme_wnd = new CUIWindow();
m_scheme_wnd->SetAutoDelete(true);
AttachChild(m_scheme_wnd);
CUIXmlInit::InitWindow(uiXml, "scheme", 0, m_scheme_wnd);

m_item = UIHelper::CreateStatic(uiXml, "item_static", this);
m_back = UIHelper::CreateNormalWindow(uiXml, "back", this);
m_scheme_wnd = UIHelper::CreateNormalWindow(uiXml, "scheme", this);
m_btn_repair = UIHelper::Create3tButton(uiXml, "repair_button", this);

LoadCellsBacks(uiXml);
Expand Down Expand Up @@ -155,18 +143,15 @@ void CUIInventoryUpgradeWnd::Show(bool status)
void CUIInventoryUpgradeWnd::Update() { inherited::Update(); }
void CUIInventoryUpgradeWnd::Reset()
{
SCHEMES::iterator ibw = m_schemes.begin();
SCHEMES::iterator iew = m_schemes.end();
for (; ibw != iew; ++ibw)
for (Scheme* scheme : m_schemes)
{
UI_Upgrades_type::iterator ib = (*ibw)->cells.begin();
UI_Upgrades_type::iterator ie = (*ibw)->cells.end();
for (; ib != ie; ++ib)
for (auto& cell : scheme->cells)
{
(*ib)->Reset();
(*ib)->m_point->Reset();
cell->Reset();
cell->m_point->Reset();
}
}

inherited::Reset();
inherited::ResetAll();
}
Expand All @@ -178,23 +163,17 @@ void CUIInventoryUpgradeWnd::UpdateAllUpgrades()
return;
}

UI_Upgrades_type::iterator ib = m_current_scheme->cells.begin();
UI_Upgrades_type::iterator ie = m_current_scheme->cells.end();
for (; ib != ie; ++ib)
{
(*ib)->update_item(m_inv_item);
}
for (auto& cell : m_current_scheme->cells)
cell->update_item(m_inv_item);
}

void CUIInventoryUpgradeWnd::SetCurScheme(const shared_str& id)
{
SCHEMES::iterator ib = m_schemes.begin();
SCHEMES::iterator ie = m_schemes.end();
for (; ib != ie; ++ib)
for (Scheme* scheme : m_schemes)
{
if ((*ib)->name._get() == id._get())
if (scheme->name._get() == id._get())
{
m_current_scheme = (*ib);
m_current_scheme = scheme;
return;
}
}
Expand All @@ -212,7 +191,7 @@ bool CUIInventoryUpgradeWnd::install_item(CInventoryItem& inv_item, bool can_upg
#ifdef DEBUG
Msg("Inventory item <%s> cannot upgrade - Mechanic say.", inv_item.m_section_id.c_str());
#endif // DEBUG
m_current_scheme = NULL;
m_current_scheme = nullptr;
return false;
}

Expand All @@ -222,17 +201,14 @@ bool CUIInventoryUpgradeWnd::install_item(CInventoryItem& inv_item, bool can_upg
#ifdef DEBUG
Msg("Inventory item <%s> does not contain upgrade scheme.", inv_item.m_section_id.c_str());
#endif // DEBUG
m_current_scheme = NULL;
m_current_scheme = nullptr;
return false;
}

SetCurScheme(scheme_name);

UI_Upgrades_type::iterator ib = m_current_scheme->cells.begin();
UI_Upgrades_type::iterator ie = m_current_scheme->cells.end();
for (; ib != ie; ++ib)
for (UIUpgrade* ui_item : m_current_scheme->cells)
{
UIUpgrade* ui_item = (*ib);
m_scheme_wnd->AttachChild(ui_item);
// m_item->AttachChild( ui_item->m_point );
m_back->AttachChild(ui_item->m_point);
Expand Down Expand Up @@ -268,20 +244,17 @@ bool CUIInventoryUpgradeWnd::install_item(CInventoryItem& inv_item, bool can_upg
UIUpgrade* CUIInventoryUpgradeWnd::FindUIUpgrade(Upgrade_type const* upgr)
{
if (!m_current_scheme)
return nullptr;

for (UIUpgrade* cell : m_current_scheme->cells)
{
return NULL;
}
UI_Upgrades_type::iterator ib = m_current_scheme->cells.begin();
UI_Upgrades_type::iterator ie = m_current_scheme->cells.end();
for (; ib != ie; ++ib)
{
Upgrade_type* i_upgr = (*ib)->get_upgrade();
Upgrade_type* i_upgr = cell->get_upgrade();
if (upgr == i_upgr)
{
return (*ib);
return cell;
}
}
return NULL;
return nullptr;
}

bool CUIInventoryUpgradeWnd::DBClickOnUIUpgrade(Upgrade_type const* upgr)
Expand Down Expand Up @@ -356,12 +329,12 @@ void CUIInventoryUpgradeWnd::set_info_cur_upgrade(Upgrade_type* upgrade)
{
if (Device.dwTimeGlobal < uiu->FocusReceiveTime())
{
upgrade = NULL; // visible = false
upgrade = nullptr; // visible = false
}
}
else
{
upgrade = NULL;
upgrade = nullptr;
}

CUIActorMenu* parent_wnd = smart_cast<CUIActorMenu*>(m_pParentWnd);
Expand Down
36 changes: 14 additions & 22 deletions src/xrGame/ui/UITaskWnd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,35 +300,27 @@ void CUITaskWnd::OnShowQuestNpcs(CUIWindow* ui, void* d)
// --------------------------------------------------------------------------------------------------
CUITaskItem::CUITaskItem() : m_owner(nullptr), m_hint_wt(500), show_hint(false), show_hint_can(false) {}
CUITaskItem::~CUITaskItem() {}
CUIStatic* init_static_field(CUIXml& uiXml, LPCSTR path, LPCSTR path2);

void CUITaskItem::Init(CUIXml& uiXml, LPCSTR path)
{
CUIXmlInit::InitWindow(uiXml, path, 0, this);
m_hint_wt = uiXml.ReadAttribInt(path, 0, "hint_wt", 500);

string256 buff;
CUIStatic* S = nullptr;

strconcat(sizeof(buff), buff, path, ":t_icon");
if (uiXml.NavigateToNode(buff))
{
S = init_static_field(uiXml, path, "t_icon");
AttachChild(S);
}
m_info["t_icon"] = S;

strconcat(sizeof(buff), buff, path, ":t_icon_over");
if (uiXml.NavigateToNode(buff))
const auto init = [&](pcstr name, bool critical = true)
{
S = init_static_field(uiXml, path, "t_icon_over");
AttachChild(S);
}
m_info["t_icon_over"] = S;

S = init_static_field(uiXml, path, "t_caption");
AttachChild(S);
m_info["t_caption"] = S;
string256 buff;
strconcat(sizeof(buff), buff, path, ":", name);
m_info[name] = UIHelper::CreateStatic(uiXml, buff, this, critical);
};

init("t_icon", false);
init("t_icon_over", false);
init("t_caption");

// If icon exist but icon_over doesn't
// then just use icon for both cases
if (!m_info["t_icon_over"])
m_info["t_icon_over"] = m_info["t_icon"];

show_hint_can = false;
show_hint = false;
Expand Down
50 changes: 14 additions & 36 deletions src/xrGame/ui/map_hint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,25 @@
#include "GameTask.h"
#include "UIInventoryUtilities.h"
#include "string_table.h"

CUIStatic* init_static_field(CUIXml& uiXml, LPCSTR path, LPCSTR path2)
{
CUIStatic* S = new CUIStatic();
string512 buff;
S->SetAutoDelete(true);
strconcat(sizeof(buff), buff, path, ":", path2);
CUIXmlInit::InitStatic(uiXml, buff, 0, S);

return S;
}
#include "UIHelper.h"

void CUIMapLocationHint::Init(CUIXml& uiXml, LPCSTR path)
{
CUIXmlInit::InitFrameWindow(uiXml, path, 0, this);

CUIStatic* S = NULL;

S = init_static_field(uiXml, path, "simple_text");
AttachChild(S);
m_info["simple_text"] = S;

S = init_static_field(uiXml, path, "t_icon");
AttachChild(S);
m_info["t_icon"] = S;

S = init_static_field(uiXml, path, "t_caption");
AttachChild(S);
m_info["t_caption"] = S;

S = init_static_field(uiXml, path, "t_time");
AttachChild(S);
m_info["t_time"] = S;

S = init_static_field(uiXml, path, "t_time_rem");
AttachChild(S);
m_info["t_time_rem"] = S;

S = init_static_field(uiXml, path, "t_hint_text");
AttachChild(S);
m_info["t_hint_text"] = S;
const auto init = [&](pcstr name)
{
string512 buff;
strconcat(sizeof(buff), buff, path, ":", name);
m_info[name] = UIHelper::CreateStatic(uiXml, buff, this);
};

init("simple_text");
init("t_icon");
init("t_caption");
init("t_time");
init("t_time_rem");
init("t_hint_text");

m_posx_icon = m_info["t_icon"]->GetWndPos().x;
m_posx_caption = m_info["t_caption"]->GetWndPos().x;
Expand Down

0 comments on commit 58960f8

Please sign in to comment.