Codechange: Make DropDownListStringItem preformat and remove other implementations. (#11063)
Having to choose between DropDownListStringItem, DropDownListCharStringItem, and DropDownListParamStringItem depending on whether to draw a StringID, a raw string, or a StringID with extra parameters was needlessly complex. Instead, allow passing a StringID or raw string to DropDownListStringItem. This will preformat the StringID into a raw string, and can therefore accept parameters via the normal SetDParam mechanism. This also means that strings no longer need to be formatted on every draw.
This commit is contained in:
@@ -32,6 +32,10 @@ void DropDownListItem::Draw(const Rect &r, bool sel, Colours bg_colour) const
|
||||
GfxFillRect(r.left, mid, r.right, mid + WidgetDimensions::scaled.bevel.top - 1, c2);
|
||||
}
|
||||
|
||||
DropDownListStringItem::DropDownListStringItem(StringID string, int result, bool masked) : DropDownListItem(result, masked), string(GetString(string))
|
||||
{
|
||||
}
|
||||
|
||||
uint DropDownListStringItem::Width() const
|
||||
{
|
||||
return GetStringBoundingBox(this->String()).width + WidgetDimensions::scaled.dropdowntext.Horizontal();
|
||||
@@ -52,24 +56,12 @@ void DropDownListStringItem::Draw(const Rect &r, bool sel, Colours bg_colour) co
|
||||
*/
|
||||
/* static */ bool DropDownListStringItem::NatSortFunc(std::unique_ptr<const DropDownListItem> const &first, std::unique_ptr<const DropDownListItem> const &second)
|
||||
{
|
||||
std::string str1 = GetString(static_cast<const DropDownListStringItem*>(first.get())->String());
|
||||
std::string str2 = GetString(static_cast<const DropDownListStringItem*>(second.get())->String());
|
||||
std::string str1 = static_cast<const DropDownListStringItem*>(first.get())->String();
|
||||
std::string str2 = static_cast<const DropDownListStringItem*>(second.get())->String();
|
||||
return StrNaturalCompare(str1, str2) < 0;
|
||||
}
|
||||
|
||||
StringID DropDownListParamStringItem::String() const
|
||||
{
|
||||
for (uint i = 0; i < lengthof(this->decode_params); i++) SetDParam(i, this->decode_params[i]);
|
||||
return this->string;
|
||||
}
|
||||
|
||||
StringID DropDownListCharStringItem::String() const
|
||||
{
|
||||
SetDParamStr(0, this->raw_string);
|
||||
return this->string;
|
||||
}
|
||||
|
||||
DropDownListIconItem::DropDownListIconItem(SpriteID sprite, PaletteID pal, StringID string, int result, bool masked) : DropDownListParamStringItem(string, result, masked), sprite(sprite), pal(pal)
|
||||
DropDownListIconItem::DropDownListIconItem(SpriteID sprite, PaletteID pal, StringID string, int result, bool masked) : DropDownListStringItem(string, result, masked), sprite(sprite), pal(pal)
|
||||
{
|
||||
this->dim = GetSpriteSize(sprite);
|
||||
this->sprite_y = dim.height;
|
||||
@@ -82,7 +74,7 @@ uint DropDownListIconItem::Height(uint width) const
|
||||
|
||||
uint DropDownListIconItem::Width() const
|
||||
{
|
||||
return DropDownListParamStringItem::Width() + this->dim.width + WidgetDimensions::scaled.hsep_wide;
|
||||
return DropDownListStringItem::Width() + this->dim.width + WidgetDimensions::scaled.hsep_wide;
|
||||
}
|
||||
|
||||
void DropDownListIconItem::Draw(const Rect &r, bool sel, Colours bg_colour) const
|
||||
|
@@ -37,48 +37,23 @@ public:
|
||||
*/
|
||||
class DropDownListStringItem : public DropDownListItem {
|
||||
public:
|
||||
StringID string; ///< String ID of item
|
||||
const std::string string; ///< String of item
|
||||
|
||||
DropDownListStringItem(StringID string, int result, bool masked) : DropDownListItem(result, masked), string(string) {}
|
||||
DropDownListStringItem(StringID string, int result, bool masked);
|
||||
DropDownListStringItem(const std::string &string, int result, bool masked) : DropDownListItem(result, masked), string(string) {}
|
||||
|
||||
bool Selectable() const override { return true; }
|
||||
uint Width() const override;
|
||||
void Draw(const Rect &r, bool sel, Colours bg_colour) const override;
|
||||
virtual StringID String() const { return this->string; }
|
||||
virtual const std::string &String() const { return this->string; }
|
||||
|
||||
static bool NatSortFunc(std::unique_ptr<const DropDownListItem> const &first, std::unique_ptr<const DropDownListItem> const &second);
|
||||
};
|
||||
|
||||
/**
|
||||
* String list item with parameters.
|
||||
*/
|
||||
class DropDownListParamStringItem : public DropDownListStringItem {
|
||||
public:
|
||||
uint64 decode_params[10]; ///< Parameters of the string
|
||||
|
||||
DropDownListParamStringItem(StringID string, int result, bool masked) : DropDownListStringItem(string, result, masked) {}
|
||||
|
||||
StringID String() const override;
|
||||
void SetParam(uint index, uint64 value) { decode_params[index] = value; }
|
||||
void SetParamStr(uint index, const char *str) { this->SetParam(index, (uint64)(size_t)str); }
|
||||
};
|
||||
|
||||
/**
|
||||
* List item containing a C char string.
|
||||
*/
|
||||
class DropDownListCharStringItem : public DropDownListStringItem {
|
||||
public:
|
||||
std::string raw_string;
|
||||
|
||||
DropDownListCharStringItem(const std::string &raw_string, int result, bool masked) : DropDownListStringItem(STR_JUST_RAW_STRING, result, masked), raw_string(raw_string) {}
|
||||
|
||||
StringID String() const override;
|
||||
};
|
||||
|
||||
/**
|
||||
* List item with icon and string.
|
||||
*/
|
||||
class DropDownListIconItem : public DropDownListParamStringItem {
|
||||
class DropDownListIconItem : public DropDownListStringItem {
|
||||
SpriteID sprite;
|
||||
PaletteID pal;
|
||||
Dimension dim;
|
||||
|
Reference in New Issue
Block a user