@@ -2281,63 +2281,71 @@ void BuyMenuGUI::AddPresetsToItemList() {
2281
2281
AllegroBitmap* pItemBitmap = nullptr ;
2282
2282
float loadoutCost = 0 ;
2283
2283
2284
+ std::vector<std::pair<int , int >> rowBounds;
2285
+ rowBounds.resize (loadout.GetCargoList ()->size ()); // pessimistic but eh
2286
+
2284
2287
const int maxBitmapWidth = 130 ;
2285
- const int margin = 2 ;
2288
+ const int horizontalMargin = 2 ;
2289
+ const int verticalMargin = 3 ;
2286
2290
2287
- int bitmapHeight = 0 ;
2288
2291
int bitmapWidth = 0 ;
2292
+ int bitmapHeight = 0 ;
2289
2293
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;
2299
2301
}
2300
2302
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
+ }
2304
2311
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 ;
2307
2315
}
2308
2316
2309
- // and once more for the last row
2310
- bitmapHeight += rowHeight;
2311
- bitmapWidth = std::max (bitmapWidth, rowWidth);
2317
+ bitmapWidth = std::min (bitmapWidth, maxBitmapWidth);
2312
2318
2313
2319
// Generate our bitmap of all the cargo items in the loadout
2314
2320
pItemBitmap = new AllegroBitmap ();
2315
2321
pItemBitmap->Create (bitmapWidth, bitmapHeight);
2316
2322
2317
2323
// Now actually draw the stuff in the appropriate places
2318
- rowHeight = -margin;
2319
2324
int heightOffset = 0 ;
2320
2325
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;
2326
2333
widthOffset = 0 ;
2334
+ ++row;
2335
+ rowWidth = rowBounds[row].first ;
2336
+ rowHeight = rowBounds[row].second ;
2327
2337
}
2328
2338
2329
2339
if (widthOffset + sceneObject->GetGraphicalIcon ()->w > maxBitmapWidth) {
2330
2340
continue ; // don't draw anything that would overflow the bitmap
2331
2341
}
2332
2342
2333
- rowHeight = std::max (rowHeight, sceneObject->GetGraphicalIcon ()->h );
2334
-
2335
2343
// TODO: make a smarter row structure so we can properly centre the icons if the actor isn't the tallest item in the row
2336
2344
// Vertically center the icon in the row
2337
2345
int yOffset = (rowHeight - sceneObject->GetGraphicalIcon ()->h ) / 2 ;
2338
2346
2339
2347
draw_sprite (pItemBitmap->GetBitmap (), sceneObject->GetGraphicalIcon (), widthOffset, heightOffset + yOffset);
2340
- widthOffset += sceneObject->GetGraphicalIcon ()->w + margin ;
2348
+ widthOffset += sceneObject->GetGraphicalIcon ()->w + horizontalMargin ;
2341
2349
}
2342
2350
2343
2351
for (const SceneObject* sceneObject: *loadout.GetCargoList ()) {
0 commit comments