Skip to content

Commit b838a9c

Browse files
Add container for parsing TechTree.json file
1 parent c525883 commit b838a9c

File tree

6 files changed

+79
-56
lines changed

6 files changed

+79
-56
lines changed

src/FactionsManager.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <future>
2+
#include "Logger.hpp"
3+
#include "FactionsManager.hpp"
4+
5+
FactionManager::FactionManager()
6+
{
7+
vFactions.reserve(12);
8+
9+
for(const auto& elem : TECH_TREE_SOURCE.Query("$.TechTree").toArray())
10+
vFactions.push_back(Faction{elem.toObject()});
11+
}
12+
13+
int FactionManager::Count() { return vFactions.size(); }
14+
const Faction& FactionManager::FindByIndex(int position) { return vFactions.at(position); }
15+
const Faction& FactionManager::FindByShortName(const QString& name)
16+
{
17+
int tmp = 0;
18+
19+
for(int i = 0; i < vFactions.count(); i++)
20+
{
21+
const Faction& elem = vFactions[i];
22+
23+
if(elem.GetShortName() != name)
24+
continue;
25+
26+
tmp = i;
27+
break;
28+
}
29+
30+
return vFactions.at(tmp);
31+
}

src/FactionsManager.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
#include <memory>
3+
#include <QVector>
4+
#include "Parsers/JSONFile.hpp"
5+
#include "Faction.hpp"
6+
7+
#define FACTIONS_MANAGER FactionManager::Instance
8+
9+
class FactionManager final
10+
{
11+
private: // Data
12+
JSONFile TECH_TREE_SOURCE{PROGRAM_CONSTANTS->TECH_TREE_FILE};
13+
QVector<Faction> vFactions;
14+
public:
15+
inline static std::unique_ptr<FactionManager> Instance = nullptr;
16+
17+
private: // Methods
18+
void Parse();
19+
public:
20+
/// @brief Default constructor.
21+
FactionManager();
22+
/// @brief Return count of parsed factions.
23+
int Count();
24+
/// @brief Return faction by its index in container.
25+
const Faction& FindByIndex(int position);
26+
/// @brief Return faction reference.
27+
const Faction& FindByShortName(const QString& name);
28+
};

src/GUI/EditorWindow.cpp

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "../Logger.hpp"
1313
#include "../Unsorted.hpp"
1414
#include "../Convert.hpp"
15+
#include "../FactionsManager.hpp"
1516

1617
#include "ImageManager.hpp"
1718
#include "WindowManager.hpp"
@@ -27,8 +28,7 @@ EditorWindow::EditorWindow(QWidget* parent)
2728
, pHotkeysPanelsWidget{nullptr}
2829
, pAboutDialog{nullptr}
2930
{
30-
SetFactions();
31-
LOGMSG("Total faction count that has been read from json file: " + factionVector.size());
31+
LOGMSG("Total faction count that has been read from json file: " + FACTIONS_MANAGER->Count());
3232

3333
resize(PROGRAM_CONSTANTS->EDITOR_WINDOW_SIZE);
3434
ConfigureMenu();
@@ -42,7 +42,7 @@ EditorWindow::EditorWindow(QWidget* parent)
4242
connect(pEntitiesTreeWidget, &QTreeWidget::itemSelectionChanged, this, &EditorWindow::SetHotkeysPanels);
4343

4444
QBoxLayout* ltFactions = nullptr;
45-
int factonsCount = factionVector.size();
45+
int factonsCount = FACTIONS_MANAGER->Count();
4646

4747
if (factonsCount == Faction::BASIC_FACTION_COUNT)
4848
{
@@ -59,7 +59,7 @@ EditorWindow::EditorWindow(QWidget* parent)
5959

6060
for (int i = 0; i < 4; ++i)
6161
{
62-
const Faction currFaction = factionVector.at(sectionIndex + i);
62+
const Faction currFaction = FACTIONS_MANAGER->FindByIndex(sectionIndex + i);
6363

6464
QPushButton* factionButton = new QPushButton{currFaction.GetDisplayName()};
6565

@@ -199,7 +199,7 @@ void EditorWindow::SetGameObjectList(const QString& factionShortName)
199199
{
200200
pEntitiesTreeWidget->clear();
201201

202-
QMap<Faction::GameObject, GameObjectTypes> goMap = GetFactionRef(factionShortName).GetTechTree();
202+
QMap<Faction::GameObject, GameObjectTypes> goMap = FACTIONS_MANAGER->FindByShortName(factionShortName).GetTechTree();
203203

204204
// Skip if there are no entities of that type
205205
if(goMap.isEmpty()) return;
@@ -265,7 +265,7 @@ void EditorWindow::SetHotkeysPanels()
265265
const QString& factionShortName = specialItemInfo.first;
266266
const QString& gameObjectName = specialItemInfo.second;
267267

268-
const auto gameObjectKeyboardLayouts = GetFactionRef(factionShortName).GetKeyboardLayoutsByObjectName(gameObjectName);
268+
const auto gameObjectKeyboardLayouts = FACTIONS_MANAGER->FindByShortName(factionShortName).GetKeyboardLayoutsByObjectName(gameObjectName);
269269

270270
// Recreate panels widget
271271
if (pHotkeysPanelsWidget != nullptr) pHotkeysPanelsWidget->deleteLater();
@@ -418,40 +418,10 @@ void EditorWindow::KeyboardWindow_Update(int id)
418418
}
419419
}
420420

421-
void EditorWindow::SetFactions()
422-
{
423-
for(const auto& elem : TECH_TREE_SOURCE.Query("$.TechTree").toArray())
424-
factionVector.push_back(Faction{elem.toObject()});
425-
}
426-
427-
const Faction& EditorWindow::GetFactionRef(const QString& name)
428-
{
429-
int tmp = 0;
430-
431-
for(int i = 0; i < factionVector.count(); i++)
432-
{
433-
const Faction& elem = factionVector[i];
434-
435-
if(elem.GetShortName() == name)
436-
{
437-
tmp = i;
438-
break;
439-
}
440-
}
441-
442-
return factionVector.at(tmp);
443-
}
444-
445421
void EditorWindow::SetActionHotkey(const QString& fctShortName, const QString& goName, const QString& actName, const QString& hk)
446422
{
447-
for(Faction& fct : factionVector)
448-
{
449-
if(fct.GetShortName() == fctShortName)
450-
{
451-
fct.SetHotkey(goName, actName, hk);
452-
break;
453-
}
454-
}
423+
Faction& fct = const_cast<Faction&>(FACTIONS_MANAGER->FindByShortName(fctShortName));
424+
fct.SetHotkey(goName, actName, hk);
455425
}
456426

457427
void EditorWindow::ActAbout_Triggered()

src/GUI/EditorWindow.hpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,13 @@
66
#include <QHBoxLayout>
77

88
#include "../Parsers/JSONFile.hpp"
9-
#include "../Faction.hpp"
109
#include "ActionHotkeyWidget.hpp"
1110

1211
class EditorWindow final : public QMainWindow
1312
{
1413
Q_OBJECT
1514

1615
private: // Data
17-
JSONFile TECH_TREE_SOURCE{PROGRAM_CONSTANTS->TECH_TREE_FILE};
18-
19-
QVector<Faction> factionVector;
20-
2116
// Qt object in a single copy
2217
QButtonGroup* pFactionsButtonsGroup = nullptr;
2318

@@ -37,10 +32,6 @@ class EditorWindow final : public QMainWindow
3732
EditorWindow(QWidget* parent = nullptr);
3833

3934
private:
40-
/// @brief Read data from TechTree.json and parse it to game objects.
41-
void SetFactions();
42-
/// @brief Return faction from EditorWindow::factionVector vector.
43-
const Faction& GetFactionRef(const QString& name);
4435
/// @brief Set context menu bar functions and logics.
4536
void ConfigureMenu();
4637
/// @brief Set game object and their actions for window by short faction name.

src/GUI/Translations/ru.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@
8484
</message>
8585
<message>
8686
<location filename="../EditorWindow.cpp" line="189"/>
87-
<location filename="../EditorWindow.cpp" line="526"/>
87+
<location filename="../EditorWindow.cpp" line="496"/>
8888
<source>Settings</source>
8989
<translation>Настройки</translation>
9090
</message>
9191
<message>
9292
<location filename="../EditorWindow.cpp" line="193"/>
93-
<location filename="../EditorWindow.cpp" line="491"/>
93+
<location filename="../EditorWindow.cpp" line="461"/>
9494
<source>About</source>
9595
<translation>О программе</translation>
9696
</message>
@@ -100,27 +100,27 @@
100100
<translation>Раскладка %1</translation>
101101
</message>
102102
<message>
103-
<location filename="../EditorWindow.cpp" line="471"/>
103+
<location filename="../EditorWindow.cpp" line="441"/>
104104
<source>Authors: </source>
105105
<translation>Авторы: </translation>
106106
</message>
107107
<message>
108-
<location filename="../EditorWindow.cpp" line="472"/>
108+
<location filename="../EditorWindow.cpp" line="442"/>
109109
<source>Version: </source>
110110
<translation>Версия: </translation>
111111
</message>
112112
<message>
113-
<location filename="../EditorWindow.cpp" line="473"/>
113+
<location filename="../EditorWindow.cpp" line="443"/>
114114
<source>Program licensed with </source>
115115
<translation>Программа лицензирована под </translation>
116116
</message>
117117
<message>
118-
<location filename="../EditorWindow.cpp" line="474"/>
118+
<location filename="../EditorWindow.cpp" line="444"/>
119119
<source>GitHub repository:</source>
120120
<translation>Репозиторий на GitHub:</translation>
121121
</message>
122122
<message>
123-
<location filename="../EditorWindow.cpp" line="476"/>
123+
<location filename="../EditorWindow.cpp" line="446"/>
124124
<source>Support development:</source>
125125
<translation>Поддержать разработку:</translation>
126126
</message>

src/Main.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#include <QDebug>
1111

1212
// Project headers
13-
// #include "Parsers/CSFParser.hpp"
1413
#include "GUI/WindowManager.hpp"
14+
#include "FactionsManager.hpp"
1515
#include "ProgramConstants.hpp"
1616
#include "Logger.hpp"
1717

@@ -67,10 +67,13 @@ int main(int argc, const char** argv)
6767

6868
PROGRAM_CONSTANTS->InitializeFileSettings();
6969

70-
// Show console, that by default is hiding by Logger class.
70+
// Show console, that by default is hiding by Logger class
7171
if (PROGRAM_CONSTANTS->pSettingsFile->IsConsoleEnabled())
7272
ShowWindow(GetConsoleWindow(), SW_SHOW);
7373

74+
// Initialize TechTree.json parsing
75+
FACTIONS_MANAGER = make_unique<FactionManager>();
76+
7477
// Define logger as the singleton class, that could be used anywhere in the project
7578
WINDOW_MANAGER = make_unique<WindowManager>();
7679

0 commit comments

Comments
 (0)