Skip to content

Commit d021cb0

Browse files
authored
Refactor DatabaseViewComponent (#2635)
- Use layout attributes to set padding on DatabaseScreen - Use layout attributes to set margins between between item list Back button and list, and between columns - Prevent extraneous bottom padding from being applied to item list - Prevent database details column from overlapping with crew screen/ main screen controls Also: - Add counting functions to PlayerInfo to support detecting when more than one crew screen is visible
1 parent bc7e24b commit d021cb0

File tree

7 files changed

+161
-49
lines changed

7 files changed

+161
-49
lines changed

src/playerInfo.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,31 @@ void PlayerInfo::reset()
142142
crew_positions.clear();
143143
}
144144

145+
// Return the total number of positions populated by this player.
146+
int PlayerInfo::countTotalPlayerPositions()
147+
{
148+
if (crew_positions.empty()) return 0;
149+
150+
int count = 0;
151+
for (auto monitor : crew_positions)
152+
for (auto position : monitor) count++;
153+
154+
LOG(Info, "count: ", count);
155+
return count;
156+
}
157+
158+
// Return the total number of positions populated by this player.
159+
int PlayerInfo::countPlayerPositionsOnMonitor(int monitor)
160+
{
161+
if (crew_positions.empty()) return 0;
162+
163+
int count = 0;
164+
for (auto position : crew_positions[monitor]) count++;
165+
166+
LOG(Info, "count: ", count);
167+
return count;
168+
}
169+
145170
bool PlayerInfo::hasPosition(CrewPosition cp)
146171
{
147172
for(auto cps : crew_positions) {

src/playerInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class PlayerInfo : public MultiplayerObject
3030

3131
void reset();
3232

33+
int countTotalPlayerPositions();
34+
int countPlayerPositionsOnMonitor(int monitor);
3335
bool hasPosition(CrewPosition cp);
3436
bool isOnlyMainScreen(int monitor_index);
3537

src/screenComponents/databaseView.cpp

Lines changed: 83 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "components/database.h"
44
#include "components/rendering.h"
55
#include "ecs/query.h"
6+
#include "playerInfo.h"
67

78
#include "gui/gui2_listbox.h"
89
#include "gui/gui2_image.h"
@@ -17,19 +18,33 @@ DatabaseViewComponent::DatabaseViewComponent(GuiContainer* owner)
1718
{
1819
setAttribute("layout", "horizontal");
1920

20-
// Setup the navigation bar
21-
GuiElement* navigation_element = new GuiElement(this, "NAVIGATION_BAR");
22-
navigation_element->setAttribute("layout", "vertical");
23-
navigation_element->setMargins(20, 20, 20, 120)->setSize(navigation_width, GuiElement::GuiSizeMax);
24-
back_button = new GuiButton(navigation_element,"BACK_BUTTON", "Back", [this](){
25-
selected_entry = sp::ecs::Entity::fromString(back_entry);
26-
display();
27-
});
28-
back_button->setSize(GuiElement::GuiSizeMax, 50)->hide()->setMargins(0, 0, 0, 20);
29-
item_list = new GuiListbox(navigation_element, "DATABASE_ITEM_LIST", [this](int index, string value) {
30-
selected_entry = sp::ecs::Entity::fromString(value);
31-
display();
32-
});
21+
// Setup the navigation bar.
22+
navigation_element = new GuiElement(this, "DB_NAV_BAR");
23+
navigation_element
24+
->setSize(navigation_width, GuiElement::GuiSizeMax)
25+
->setAttribute("layout", "vertical");
26+
navigation_element
27+
->setAttribute("margin", "0, 20, 0, 0");
28+
29+
back_button = new GuiButton(navigation_element, "DB_BACK_BUTTON", tr("databaseView", "Back"),
30+
[this]()
31+
{
32+
selected_entry = sp::ecs::Entity::fromString(back_entry);
33+
display();
34+
}
35+
);
36+
back_button
37+
->setSize(GuiElement::GuiSizeMax, 50.0f)
38+
->hide()
39+
->setAttribute("margin", "0, 0, 0, 20");
40+
41+
item_list = new GuiListbox(navigation_element, "DB_ITEM_LIST",
42+
[this](int index, string value)
43+
{
44+
selected_entry = sp::ecs::Entity::fromString(value);
45+
display();
46+
}
47+
);
3348
item_list->setSize(GuiElement::GuiSizeMax, GuiElement::GuiSizeMax);
3449
display();
3550
}
@@ -48,6 +63,18 @@ bool DatabaseViewComponent::findAndDisplayEntry(string name)
4863
return false;
4964
}
5065

66+
DatabaseViewComponent* DatabaseViewComponent::setDetailsPadding(int padding)
67+
{
68+
details_padding = std::max(0, padding);
69+
return this;
70+
}
71+
72+
DatabaseViewComponent* DatabaseViewComponent::setItemsPadding(int padding)
73+
{
74+
items_padding = std::max(0, padding);
75+
return this;
76+
}
77+
5178
void DatabaseViewComponent::fillListBox()
5279
{
5380
item_list->setOptions({});
@@ -115,58 +142,77 @@ void DatabaseViewComponent::fillListBox()
115142

116143
void DatabaseViewComponent::display()
117144
{
118-
if (keyvalue_container)
119-
keyvalue_container->destroy();
120-
if (details_container)
121-
details_container->destroy();
122-
123-
keyvalue_container = new GuiElement(this, "KEY_VALUE_CONTAINER");
124-
keyvalue_container->setMargins(20)->setSize(400, GuiElement::GuiSizeMax)->setAttribute("layout", "vertical");
125-
126-
details_container = new GuiElement(this, "DETAILS_CONTAINER");
127-
details_container->setMargins(20)->setSize(GuiElement::GuiSizeMax, GuiElement::GuiSizeMax)->setAttribute("layout", "vertical");
128-
details_container->layout.padding.top = 50;
145+
if (keyvalue_container) keyvalue_container->destroy();
146+
if (details_container) details_container->destroy();
147+
148+
keyvalue_container = new GuiElement(this, "DB_KV_CONTAINER");
149+
keyvalue_container
150+
->setSize(400.0f, GuiElement::GuiSizeMax)
151+
->setAttribute("layout", "vertical");
152+
153+
details_container = new GuiElement(this, "DB_DETAILS_CONTAINER");
154+
details_container
155+
->setSize(GuiElement::GuiSizeMax, GuiElement::GuiSizeMax)
156+
->setAttribute("layout", "vertical");
157+
details_container
158+
->setAttribute("margin", "20, 0, 0, 0");
159+
// Set conditional padding on details and item lists.
160+
details_container
161+
->setAttribute("padding", "0, 0, " + static_cast<string>(details_padding) + ", 0");
162+
163+
navigation_element
164+
->setAttribute("padding", "0, 0, 0, " + static_cast<string>(items_padding));
129165

130166
fillListBox();
131167

132168
auto database = selected_entry.getComponent<Database>();
133-
if (!database)
134-
return;
135-
auto mrc = selected_entry.getComponent<MeshRenderComponent>();
169+
if (!database) return;
136170

171+
auto mrc = selected_entry.getComponent<MeshRenderComponent>();
137172
bool has_key_values = database->key_values.size() > 0;
138173
bool has_image_or_model = mrc || database->image != "";
139174
bool has_text = database->description.length() > 0;
140175

141176
if (has_image_or_model)
142177
{
143-
GuiElement* visual = (new GuiElement(details_container, "DATABASE_VISUAL_ELEMENT"))->setMargins(0, 0, 0, 40)->setSize(GuiElement::GuiSizeMax, GuiElement::GuiSizeMax);
178+
GuiElement* visual = (new GuiElement(details_container, "DB_VISUAL_ELEMENT"))
179+
->setSize(GuiElement::GuiSizeMax, GuiElement::GuiSizeMax);
144180

145181
if (mrc)
146182
{
147-
(new GuiRotatingModelView(visual, "DATABASE_MODEL_VIEW", selected_entry))
183+
(new GuiRotatingModelView(visual, "DB_MODEL_VIEW", selected_entry))
148184
->setSize(GuiElement::GuiSizeMax, GuiElement::GuiSizeMax);
149-
if(database->image != "")
185+
186+
if (database->image != "")
150187
{
151-
(new GuiImage(visual, "DATABASE_IMAGE", database->image))->setMargins(0)->setSize(32, 32);
188+
(new GuiImage(visual, "DB_IMAGE", database->image))
189+
->setSize(32.0f, 32.0f);
152190
}
153191
}
154192
else if(database->image != "")
155193
{
156-
auto image = new GuiImage(visual, "DATABASE_IMAGE", database->image);
157-
image->setSize(GuiElement::GuiSizeMax, GuiElement::GuiSizeMax);
194+
(new GuiImage(visual, "DB_IMAGE", database->image))
195+
->setSize(GuiElement::GuiSizeMax, GuiElement::GuiSizeMax);
158196
}
159197
}
198+
160199
if (has_text)
161200
{
162-
(new GuiScrollFormattedText(details_container, "DATABASE_LONG_DESCRIPTION", database->description))->setTextSize(24)->setSize(GuiElement::GuiSizeMax, GuiElement::GuiSizeMax);
201+
(new GuiScrollFormattedText(details_container, "DB_LONG_DESCRIPTION", database->description))
202+
->setTextSize(24.0f)
203+
->setSize(GuiElement::GuiSizeMax, GuiElement::GuiSizeMax);
163204
}
164205

165206
if (has_key_values)
166207
{
167-
for(auto& kv : database->key_values)
168-
(new GuiKeyValueDisplay(keyvalue_container, "", 0.37, kv.key, kv.value))->setSize(GuiElement::GuiSizeMax, 40);
169-
} else {
208+
for (auto& kv : database->key_values)
209+
{
210+
(new GuiKeyValueDisplay(keyvalue_container, "", 0.37f, kv.key, kv.value))
211+
->setSize(GuiElement::GuiSizeMax, 40.0f);
212+
}
213+
}
214+
else
215+
{
170216
keyvalue_container->destroy();
171217
keyvalue_container = nullptr;
172218
}

src/screenComponents/databaseView.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#ifndef DATABASE_VIEW_H
2-
#define DATABASE_VIEW_H
1+
#pragma once
32

43
#include "gui/gui2_element.h"
54
#include "ecs/entity.h"
@@ -13,21 +12,24 @@ class DatabaseViewComponent : public GuiElement
1312
DatabaseViewComponent(GuiContainer* owner);
1413

1514
bool findAndDisplayEntry(string name);
15+
DatabaseViewComponent* setDetailsPadding(int padding);
16+
DatabaseViewComponent* setItemsPadding(int padding);
1617

1718
private:
1819
bool findAndDisplayEntry(string name, sp::ecs::Entity parent);
19-
//Fill the selection listbox with options from the selected_entry, or the main database list if selected_entry is nullptr
20+
// Fill the selection listbox with options from the selected_entry, or the main database list if selected_entry is nullptr
2021
void fillListBox();
2122
void display();
2223

2324
sp::ecs::Entity selected_entry;
2425
string back_entry;
26+
GuiElement* navigation_element = nullptr;
2527
GuiButton* back_button = nullptr;
2628
GuiListbox* item_list = nullptr;
2729
GuiElement* keyvalue_container = nullptr;
2830
GuiElement* details_container = nullptr;
2931

3032
static constexpr int navigation_width = 400;
33+
int details_padding = 0;
34+
int items_padding = 0;
3135
};
32-
33-
#endif//DATABASE_VIEW_H

src/screens/crew6/scienceScreen.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,23 @@ ScienceScreen::ScienceScreen(GuiContainer* owner, CrewPosition crew_position)
185185

186186
// Prep and hide the database view.
187187
database_view = new DatabaseViewComponent(this);
188-
database_view->hide()->setSize(GuiElement::GuiSizeMax, GuiElement::GuiSizeMax);
188+
database_view
189+
->setSize(GuiElement::GuiSizeMax, GuiElement::GuiSizeMax)
190+
->hide()
191+
->setAttribute("padding", "20");
192+
193+
// Pad top of details column if crew screen selection controls are visible,
194+
// and bottom of item list column to prevent overlap with probe/radar view
195+
// selectors.
196+
int details_padding = 0;
197+
if (my_player_info)
198+
{
199+
if (my_player_info->main_screen_control != 0) details_padding = 120;
200+
else if (my_player_info->countTotalPlayerPositions() > 1) details_padding = 70;
201+
}
202+
database_view
203+
->setDetailsPadding(details_padding)
204+
->setItemsPadding(120);
189205

190206
// Probe view button
191207
probe_view_button = new GuiToggleButton(radar_view, "PROBE_VIEW", tr("scienceButton", "Probe View"), [this](bool value){
Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
11
#include "databaseScreen.h"
2+
#include "playerInfo.h"
23

34
#include "screenComponents/databaseView.h"
45
#include "screenComponents/customShipFunctions.h"
56

67
DatabaseScreen::DatabaseScreen(GuiContainer* owner)
78
: GuiOverlay(owner, "DATABASE_SCREEN", colorConfig.background)
89
{
9-
(new DatabaseViewComponent(this))->setPosition(0, 0, sp::Alignment::TopLeft)->setSize(GuiElement::GuiSizeMax, GuiElement::GuiSizeMax);
10+
// Render background decorations.
11+
(new GuiOverlay(this, "BACKGROUND_CROSSES", glm::u8vec4{255,255,255,255}))
12+
->setTextureTiled("gui/background/crosses.png");
1013

11-
(new GuiCustomShipFunctions(this, CrewPosition::databaseView, ""))->setPosition(-20, 120, sp::Alignment::TopRight)->setSize(250, GuiElement::GuiSizeMax);
14+
// Render database.
15+
DatabaseViewComponent* dvc = new DatabaseViewComponent(this);
16+
dvc->setPosition(0.0f, 0.0f, sp::Alignment::TopLeft)
17+
->setSize(GuiElement::GuiSizeMax, GuiElement::GuiSizeMax);
18+
dvc->setAttribute("padding", "20");
19+
20+
// Pad top of details column if crew screen selection controls are visible.
21+
int details_padding = 0;
22+
if (my_player_info)
23+
{
24+
if (my_player_info->main_screen_control != 0) details_padding = 120;
25+
else if (my_player_info->countTotalPlayerPositions() > 1) details_padding = 70;
26+
}
27+
dvc->setDetailsPadding(details_padding);
28+
29+
// Render Databse View custom ship functions.
30+
(new GuiCustomShipFunctions(this, CrewPosition::databaseView, "DB_CUSTOM_SHIP_FUNCTIONS"))
31+
->setSize(250.0f, GuiElement::GuiSizeMax)
32+
->setPosition(0.0f, 0.0f, sp::Alignment::TopRight)
33+
->setAttribute("padding", "0, 20, 120, 0");
1234
}

src/screens/extra/databaseScreen.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
#ifndef DATABASE_SCREEN_H
2-
#define DATABASE_SCREEN_H
1+
#pragma once
32

43
#include "gui/gui2_overlay.h"
54

5+
class DatabaseViewComponent;
6+
67
class DatabaseScreen : public GuiOverlay
78
{
89
private:
910
public:
1011
DatabaseScreen(GuiContainer* owner);
1112
};
12-
13-
#endif//DATABASE_SCREEN_H

0 commit comments

Comments
 (0)