Skip to content

Commit

Permalink
Fix OpenTTD#7631: replace produced&transported with cargo sprites in …
Browse files Browse the repository at this point in the history
…industry directory
  • Loading branch information
glx22 committed Oct 28, 2019
1 parent dd07b23 commit d42620d
Showing 1 changed file with 39 additions and 31 deletions.
70 changes: 39 additions & 31 deletions src/industry_gui.cpp
Expand Up @@ -39,6 +39,7 @@
#include "smallmap_gui.h"
#include "widgets/dropdown_type.h"
#include "widgets/industry_widget.h"
#include "zoom_func.h"

#include "table/strings.h"

Expand Down Expand Up @@ -1302,41 +1303,19 @@ class IndustryDirectoryWindow : public Window {
}

/**
* Get the StringID to draw and set the appropriate DParams.
* @param i the industry to get the StringID of.
* @return the StringID.
* Get the list of SpriteID to draw.
* @param i the industry to get the SpriteIDs of.
* @return the SpriteIDs.
*/
StringID GetIndustryString(const Industry *i) const
std::vector<SpriteID> GetAcceptedCargoSprites(const Industry *i) const
{
const IndustrySpec *indsp = GetIndustrySpec(i->type);
byte p = 0;
std::vector<SpriteID> sprites;

/* Industry name */
SetDParam(p++, i->index);

static CargoSuffix cargo_suffix[lengthof(i->produced_cargo)];
GetAllCargoSuffixes(CARGOSUFFIX_OUT, CST_DIR, i, i->type, indsp, i->produced_cargo, cargo_suffix);

/* Industry productions */
for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
if (i->produced_cargo[j] == CT_INVALID) continue;
SetDParam(p++, i->produced_cargo[j]);
SetDParam(p++, i->last_month_production[j]);
SetDParamStr(p++, cargo_suffix[j].text);
}

/* Transported productions */
for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
if (i->produced_cargo[j] == CT_INVALID) continue;
SetDParam(p++, ToPercent8(i->last_month_pct_transported[j]));
}

/* Drawing the right string */
switch (p) {
case 1: return STR_INDUSTRY_DIRECTORY_ITEM_NOPROD;
case 5: return STR_INDUSTRY_DIRECTORY_ITEM;
default: return STR_INDUSTRY_DIRECTORY_ITEM_TWO;
sprites.push_back(CargoSpec::Get(i->produced_cargo[j])->sprite);
}
return sprites;
}

public:
Expand Down Expand Up @@ -1371,14 +1350,36 @@ class IndustryDirectoryWindow : public Window {
break;

case WID_ID_INDUSTRY_LIST: {
bool rtl = _current_text_dir == TD_RTL;
int n = 0;
int y = r.top + WD_FRAMERECT_TOP;
if (this->industries.size() == 0) {
DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_DIRECTORY_NONE);
break;
}
for (uint i = this->vscroll->GetPosition(); i < this->industries.size(); i++) {
DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, this->GetIndustryString(this->industries[i]));
const Industry *ind = this->industries[i];
SetDParam(0, ind->index);
int x = DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_DIRECTORY_ITEM_NOPROD);
x += rtl ? -5 : 5;

for (SpriteID sprite : GetAcceptedCargoSprites(ind)) {
uint width = GetSpriteSize(sprite).width;

/* For RTL we work in exactly the opposite direction. So
* decrement the space needed first, then draw to the left
* instead of drawing to the left and then incrementing
* the space. */
if (rtl) {
x -= width;
if (x < r.left + WD_FRAMERECT_LEFT) break;
}
DrawSprite(sprite, PAL_NONE, x, y);
if (!rtl) {
x += width;
if (x > r.right - WD_FRAMERECT_RIGHT) break;
}
}

y += this->resize.step_height;
if (++n == this->vscroll->GetCapacity()) break; // max number of industries in 1 window
Expand Down Expand Up @@ -1413,7 +1414,14 @@ class IndustryDirectoryWindow : public Window {
case WID_ID_INDUSTRY_LIST: {
Dimension d = GetStringBoundingBox(STR_INDUSTRY_DIRECTORY_NONE);
for (uint i = 0; i < this->industries.size(); i++) {
d = maxdim(d, GetStringBoundingBox(this->GetIndustryString(this->industries[i])));
const Industry *ind = this->industries[i];
SetDParam(0, ind->index);
Dimension b = GetStringBoundingBox(STR_INDUSTRY_DIRECTORY_ITEM_NOPROD);
b.width += 5;
for (SpriteID sprite : GetAcceptedCargoSprites(ind)) {
b.width += GetSpriteSize(sprite).width;
}
d = maxdim(d, b);
}
resize->height = d.height;
d.height *= 5;
Expand Down

0 comments on commit d42620d

Please sign in to comment.