Skip to content

Commit 6242a4d

Browse files
committed
feels really messy now, but it's more accurate
1 parent 445110d commit 6242a4d

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

Source/Menus/BuyMenuGUI.cpp

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,63 +2281,71 @@ void BuyMenuGUI::AddPresetsToItemList() {
22812281
AllegroBitmap* pItemBitmap = nullptr;
22822282
float loadoutCost = 0;
22832283

2284+
std::vector<std::pair<int, int>> rowBounds;
2285+
rowBounds.resize(loadout.GetCargoList()->size()); // pessimistic but eh
2286+
22842287
const int maxBitmapWidth = 130;
2285-
const int margin = 2;
2288+
const int horizontalMargin = 2;
2289+
const int verticalMargin = 3;
22862290

2287-
int bitmapHeight = 0;
22882291
int bitmapWidth = 0;
2292+
int bitmapHeight = 0;
22892293

2290-
int rowHeight = -margin;
2291-
int rowWidth = 0;
2292-
for (const SceneObject* sceneObject: *loadout.GetCargoList()) {
2293-
if (dynamic_cast<const Actor*>(sceneObject)) {
2294-
// start a new row
2295-
bitmapHeight += rowHeight + margin;
2296-
bitmapWidth = std::max(bitmapWidth, rowWidth);
2297-
rowHeight = 0;
2298-
rowWidth = 0;
2294+
int row = -1;
2295+
int numRows = 0;
2296+
for (auto itr = loadout.GetCargoList()->begin(), itr_end = loadout.GetCargoList()->end(); itr != itr_end; ++itr) {
2297+
const SceneObject* sceneObject = *itr;
2298+
if (itr == loadout.GetCargoList()->begin() || dynamic_cast<const Actor*>(sceneObject)) {
2299+
++row;
2300+
++numRows;
22992301
}
23002302

2301-
if (rowWidth + sceneObject->GetGraphicalIcon()->w > maxBitmapWidth) {
2302-
continue; // don't draw anything that would overflow the bitmap
2303-
}
2303+
rowBounds[row].first += sceneObject->GetGraphicalIcon()->w + horizontalMargin;
2304+
rowBounds[row].second = std::max(rowBounds[row].second, sceneObject->GetGraphicalIcon()->h + verticalMargin);
2305+
}
2306+
2307+
if (numRows == 0) {
2308+
// this should never happen, but just in case
2309+
continue;
2310+
}
23042311

2305-
rowHeight = std::max(rowHeight, sceneObject->GetGraphicalIcon()->h);
2306-
rowWidth += sceneObject->GetGraphicalIcon()->w + margin;
2312+
for (int i = 0; i < numRows; ++i) {
2313+
bitmapWidth = std::max(bitmapWidth, rowBounds[i].first);
2314+
bitmapHeight += rowBounds[i].second;
23072315
}
23082316

2309-
// and once more for the last row
2310-
bitmapHeight += rowHeight;
2311-
bitmapWidth = std::max(bitmapWidth, rowWidth);
2317+
bitmapWidth = std::min(bitmapWidth, maxBitmapWidth);
23122318

23132319
// Generate our bitmap of all the cargo items in the loadout
23142320
pItemBitmap = new AllegroBitmap();
23152321
pItemBitmap->Create(bitmapWidth, bitmapHeight);
23162322

23172323
// Now actually draw the stuff in the appropriate places
2318-
rowHeight = -margin;
23192324
int heightOffset = 0;
23202325
int widthOffset = 0;
2321-
for (const SceneObject* sceneObject: *loadout.GetCargoList()) {
2322-
if (dynamic_cast<const Actor*>(sceneObject)) {
2323-
// start a new row
2324-
heightOffset += rowHeight + margin;
2325-
rowHeight = 0;
2326+
row = 0;
2327+
for (auto itr = loadout.GetCargoList()->begin(), itr_end = loadout.GetCargoList()->end(); itr != itr_end; ++itr) {
2328+
const SceneObject* sceneObject = *itr;
2329+
int rowWidth = rowBounds[row].first;
2330+
int rowHeight = rowBounds[row].second;
2331+
if (itr != loadout.GetCargoList()->begin() && dynamic_cast<const Actor*>(sceneObject)) {
2332+
heightOffset += rowHeight;
23262333
widthOffset = 0;
2334+
++row;
2335+
rowWidth = rowBounds[row].first;
2336+
rowHeight = rowBounds[row].second;
23272337
}
23282338

23292339
if (widthOffset + sceneObject->GetGraphicalIcon()->w > maxBitmapWidth) {
23302340
continue; // don't draw anything that would overflow the bitmap
23312341
}
23322342

2333-
rowHeight = std::max(rowHeight, sceneObject->GetGraphicalIcon()->h);
2334-
23352343
// TODO: make a smarter row structure so we can properly centre the icons if the actor isn't the tallest item in the row
23362344
// Vertically center the icon in the row
23372345
int yOffset = (rowHeight - sceneObject->GetGraphicalIcon()->h) / 2;
23382346

23392347
draw_sprite(pItemBitmap->GetBitmap(), sceneObject->GetGraphicalIcon(), widthOffset, heightOffset + yOffset);
2340-
widthOffset += sceneObject->GetGraphicalIcon()->w + margin;
2348+
widthOffset += sceneObject->GetGraphicalIcon()->w + horizontalMargin;
23412349
}
23422350

23432351
for (const SceneObject* sceneObject: *loadout.GetCargoList()) {

0 commit comments

Comments
 (0)