3
3
#include < NAS2D/Utility.h>
4
4
#include < NAS2D/EventHandler.h>
5
5
#include < NAS2D/Renderer/Renderer.h>
6
- #include < NAS2D/Math/Rectangle.h>
7
6
8
7
#include " ../../Constants/UiConstants.h"
9
8
#include " ../../Cache.h"
@@ -38,19 +37,22 @@ namespace
38
37
constexpr NAS2D::Vector<int > CategorySelectorPadding{2 , 2 };
39
38
constexpr NAS2D::Vector<int > SectionPadding {10 , 10 };
40
39
41
- struct CategoryPanel
42
- {
43
- NAS2D::Rectangle<int > rect{};
44
- NAS2D::Rectangle<int > imageSlice{};
45
- std::string name{};
46
- bool selected{false };
47
- };
48
40
49
- CategoryPanel* SelectedCategory{ nullptr };
50
-
51
- Rectangle<int > IconArea{};
41
+ std::vector<ListBoxItemText> availableTopics (const std::string& categoryName, const TechnologyCatalog& catalog, const ResearchTracker& tracker)
42
+ {
43
+ std::vector<ListBoxItemText> itemsToAdd;
44
+ const auto & completedTopics = tracker.completedResearch ();
45
+ for (const auto & topic : catalog.technologiesInCategory (categoryName))
46
+ {
47
+ const auto it = std::find (completedTopics.begin (), completedTopics.end (), topic.id );
48
+ if (it == completedTopics.end ())
49
+ {
50
+ itemsToAdd.emplace_back (ListBoxItemText{topic.name , topic.id });
51
+ }
52
+ }
52
53
53
- std::vector<CategoryPanel> CategoryPanels;
54
+ return itemsToAdd;
55
+ }
54
56
}
55
57
56
58
@@ -78,6 +80,9 @@ ResearchReport::ResearchReport() :
78
80
button->toggle (false );
79
81
}
80
82
83
+ add (lstResearchTopics, {});
84
+ lstResearchTopics.selectionChanged ().connect ({this , &ResearchReport::handleTopicChanged});
85
+
81
86
const Point <int > buttonStartPosition{rect ().position .x + MarginSize * 3 + CategoryIconSize, rect ().position .y + MarginSize * 2 + fontBigBold.height ()};
82
87
const int buttonSpacing = btnAllTopics.size ().x + MarginSize;
83
88
@@ -95,37 +100,84 @@ ResearchReport::~ResearchReport()
95
100
96
101
void ResearchReport::fillLists ()
97
102
{
103
+ lstResearchTopics.clear ();
104
+ resetCategorySelection ();
105
+ onAllTopicsClicked ();
98
106
}
99
107
100
108
101
109
void ResearchReport::clearSelected ()
102
110
{
111
+ lstResearchTopics.clearSelected ();
112
+ resetCategorySelection ();
113
+ onAllTopicsClicked ();
103
114
}
104
115
105
116
106
117
void ResearchReport::refresh ()
107
118
{
108
- if (CategoryPanels.size () < 1 ) { return ; }
119
+ if (mCategoryPanels .size () < 1 ) { return ; }
120
+
121
+ adjustCategoryIconSpacing ();
122
+
123
+ resetCategorySelection ();
124
+ onAllTopicsClicked ();
125
+
126
+ setSectionRects ();
127
+
128
+ lstResearchTopics.area (mResearchTopicArea );
129
+ }
130
+
131
+
132
+ void ResearchReport::setSectionRects ()
133
+ {
134
+ mCategoryIconArea =
135
+ {
136
+ mCategoryPanels .begin ()->rect .startPoint (),
137
+ {
138
+ CategoryIconSize,
139
+ mCategoryPanels .rbegin ()->rect .endPoint ().y - mCategoryPanels .begin ()->rect .startPoint ().y
140
+ }
141
+ };
142
+
143
+ mResearchTopicArea =
144
+ {
145
+ {
146
+ rect ().position .x + MarginSize * 3 + CategoryIconSize,
147
+ rect ().position .y + fontBigBold.height () + btnAllTopics.size ().y + MarginSize * 3
148
+ },
149
+
150
+ {
151
+ ((rect ().size .x / 3 ) * 2 ) - (MarginSize * 4 ) - CategoryIconSize,
152
+ rect ().size .y - MarginSize * 4 - fontBigBold.height () - btnAllTopics.size ().y
153
+ }
154
+ };
155
+ }
156
+
109
157
110
- const int minimumHeight = CategoryIconSize * (static_cast <int >(CategoryPanels.size ()));
111
- const int padding = ((rect ().size .y - 20 ) - minimumHeight) / static_cast <int >(CategoryPanels.size () - 1 );
158
+ void ResearchReport::adjustCategoryIconSpacing ()
159
+ {
160
+ const int minimumHeight = CategoryIconSize * (static_cast <int >(mCategoryPanels .size ()));
161
+ const int padding = ((rect ().size .y - 20 ) - minimumHeight) / static_cast <int >(mCategoryPanels .size () - 1 );
112
162
113
- for (size_t i = 0 ; i < CategoryPanels .size (); ++i)
163
+ for (size_t i = 0 ; i < mCategoryPanels .size (); ++i)
114
164
{
115
165
const NAS2D::Point <int > point{rect ().position .x + 10 , rect ().position .y + 10 + static_cast <int >(i) * CategoryIconSize + static_cast <int >(i) * padding};
116
- CategoryPanels [i].rect = {point, {CategoryIconSize, CategoryIconSize}};
166
+ mCategoryPanels [i].rect = {point, {CategoryIconSize, CategoryIconSize}};
117
167
}
168
+ }
118
169
119
- CategoryPanels.front ().selected = true ;
120
- SelectedCategory = &CategoryPanels.front ();
121
170
122
- onAllTopicsClicked ();
171
+ void ResearchReport::resetCategorySelection ()
172
+ {
173
+ for (auto & panel : mCategoryPanels )
174
+ {
175
+ panel.selected = false ;
176
+ }
123
177
124
- IconArea = {
125
- {rect ().position .x + MarginSize * 3 + CategoryIconSize,
126
- rect ().position .y + fontBigBold.height () + btnAllTopics.size ().y + MarginSize * 3 },
127
- {((rect ().size .x / 3 ) * 2 ) - (MarginSize * 4 ) - CategoryIconSize,
128
- rect ().size .y - MarginSize * 4 - fontBigBold.height () - btnAllTopics.size ().y }};
178
+ mCategoryPanels .front ().selected = true ;
179
+ mSelectedCategory = &mCategoryPanels .front ();
180
+ handleCategoryChanged ();
129
181
}
130
182
131
183
@@ -143,14 +195,14 @@ void ResearchReport::injectTechReferences(TechnologyCatalog& catalog, ResearchTr
143
195
144
196
for (const auto & category : mTechCatalog ->categories ())
145
197
{
146
- CategoryPanels .emplace_back (CategoryPanel{
198
+ mCategoryPanels .emplace_back (CategoryPanel{
147
199
{{0 , 0 }, {CategoryIconSize, CategoryIconSize}},
148
200
{{(category.icon_index % columns) * CategoryIconSize, (category.icon_index / columns) * CategoryIconSize}, {CategoryIconSize, CategoryIconSize}},
149
201
category.name ,
150
202
false });
151
203
}
152
204
153
- std::sort (CategoryPanels .begin (), CategoryPanels .end (), [](const auto & a, const auto & b) { return a.name < b.name ; });
205
+ std::sort (mCategoryPanels .begin (), mCategoryPanels .end (), [](const auto & a, const auto & b) { return a.name < b.name ; });
154
206
refresh ();
155
207
}
156
208
@@ -177,26 +229,34 @@ void ResearchReport::onMouseDown(NAS2D::EventHandler::MouseButton button, NAS2D:
177
229
return ;
178
230
}
179
231
180
- CategoryPanel* lastPanel = SelectedCategory;
232
+ if (mCategoryIconArea .contains (position))
233
+ {
234
+ handleMouseDownInCategories (position);
235
+ }
236
+ }
237
+
238
+
239
+ void ResearchReport::handleMouseDownInCategories (NAS2D::Point <int >& position)
240
+ {
241
+ CategoryPanel* lastPanel = mSelectedCategory ;
181
242
bool panelClickedOn = false ;
182
- for (auto & panel : CategoryPanels )
243
+ for (auto & panel : mCategoryPanels )
183
244
{
184
- if (panel.rect .contains (position))
245
+ panel.selected = false ;
246
+
247
+ if (panel.rect .contains (position))
185
248
{
186
249
panel.selected = true ;
187
- SelectedCategory = &panel;
250
+ mSelectedCategory = &panel;
188
251
panelClickedOn = true ;
189
- }
190
- else
191
- {
192
- panel.selected = false ;
252
+ handleCategoryChanged ();
193
253
}
194
254
}
195
255
196
- if (!panelClickedOn && lastPanel != nullptr )
256
+ if (!panelClickedOn && lastPanel != nullptr )
197
257
{
198
- SelectedCategory = lastPanel;
199
- SelectedCategory ->selected = true ;
258
+ mSelectedCategory = lastPanel;
259
+ mSelectedCategory ->selected = true ;
200
260
}
201
261
}
202
262
@@ -250,7 +310,7 @@ void ResearchReport::drawCategories() const
250
310
{
251
311
auto & renderer = Utility<Renderer>::get ();
252
312
253
- for (const auto & panel : CategoryPanels )
313
+ for (const auto & panel : mCategoryPanels )
254
314
{
255
315
const auto panelRect = Rectangle<int >::Create (
256
316
panel.rect .position - CategorySelectorPadding,
@@ -275,7 +335,7 @@ void ResearchReport::drawTopicHeader() const
275
335
auto & renderer = Utility<Renderer>::get ();
276
336
renderer.drawText (
277
337
fontBigBold,
278
- SelectedCategory ->name ,
338
+ mSelectedCategory ->name ,
279
339
rect ().position + Vector<int >{SectionPadding.x * 3 + CategoryIconSize, SectionPadding.y },
280
340
ColorText);
281
341
}
@@ -284,25 +344,18 @@ void ResearchReport::drawTopicHeader() const
284
344
void ResearchReport::drawVerticalSectionSpacer (const int startX) const
285
345
{
286
346
auto & renderer = Utility<Renderer>::get ();
287
- renderer.drawLine (
288
- Point <int >{startX, rect ().position .y + SectionPadding.y },
289
- Point <int >{startX, rect ().position .y + rect ().size .y - SectionPadding.y },
290
- ColorText);
291
- }
292
-
293
347
294
- void ResearchReport::drawTopicIconPanel () const
295
- {
296
- auto & renderer = Utility<Renderer>::get ();
297
- renderer.drawBox (IconArea, ColorText);
348
+ const Point <int > start{startX, rect ().position .y + SectionPadding.y };
349
+ const Point <int > end{startX, rect ().position .y + rect ().size .y - SectionPadding.y };
350
+ renderer.drawLine (start, end, ColorText);
298
351
}
299
352
300
353
301
354
void ResearchReport::drawResearchPointsPanel () const
302
355
{
303
356
auto & renderer = Utility<Renderer>::get ();
304
357
305
- const auto startPoint = rect ().position + Vector<int >{SectionPadding.x * 5 + CategoryIconSize + IconArea .size .x , SectionPadding.y };
358
+ const auto startPoint = rect ().position + Vector<int >{SectionPadding.x * 5 + CategoryIconSize + mResearchTopicArea .size .x , SectionPadding.y };
306
359
307
360
renderer.drawText (fontBigBold, " Research Generated Per Turn" , startPoint, ColorText);
308
361
@@ -323,14 +376,32 @@ void ResearchReport::drawResearchPointsPanel() const
323
376
}
324
377
325
378
379
+ void ResearchReport::handleCategoryChanged ()
380
+ {
381
+ lstResearchTopics.clear ();
382
+
383
+ std::vector<ListBoxItemText> itemsToAdd = availableTopics (mSelectedCategory ->name , *mTechCatalog , *mResearchTracker );
384
+
385
+ std::sort (itemsToAdd.begin (), itemsToAdd.end ());
386
+ for (auto & item : itemsToAdd)
387
+ {
388
+ lstResearchTopics.add (item);
389
+ }
390
+ }
391
+
392
+
393
+ void ResearchReport::handleTopicChanged ()
394
+ {
395
+ }
396
+
397
+
326
398
void ResearchReport::draw () const
327
399
{
328
400
drawCategories ();
329
401
330
- drawVerticalSectionSpacer (CategoryPanels .front ().rect .endPoint ().x + SectionPadding.x );
402
+ drawVerticalSectionSpacer (mCategoryPanels .front ().rect .endPoint ().x + SectionPadding.x );
331
403
332
404
drawTopicHeader ();
333
- drawTopicIconPanel ();
334
405
335
406
drawVerticalSectionSpacer ((rect ().size .x / 3 ) * 2 );
336
407
0 commit comments