1
0
Fork 0

Codechange: Use preformatted strings for slider marks. (#13484)

pull/13489/head
Peter Nelson 2025-02-07 18:14:53 +00:00 committed by GitHub
parent c3643e3ee0
commit 99a32207b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 16 deletions

View File

@ -156,23 +156,21 @@ static void AddCustomRefreshRates()
static const int SCALE_NMARKS = (MAX_INTERFACE_SCALE - MIN_INTERFACE_SCALE) / 25 + 1; // Show marks at 25% increments
static const int VOLUME_NMARKS = 9; // Show 5 values and 4 empty marks.
static StringID ScaleMarkFunc(int, int, int value)
static std::optional<std::string> ScaleMarkFunc(int, int, int value)
{
/* Label only every 100% mark. */
if (value % 100 != 0) return STR_NULL;
if (value % 100 != 0) return std::string{};
SetDParam(0, value / 100);
SetDParam(1, 0);
return STR_GAME_OPTIONS_GUI_SCALE_MARK;
return GetString(STR_GAME_OPTIONS_GUI_SCALE_MARK, value / 100, 0);
}
static StringID VolumeMarkFunc(int, int mark, int value)
static std::optional<std::string> VolumeMarkFunc(int, int mark, int value)
{
/* Label only every other mark. */
if (mark % 2 != 0) return STR_NULL;
if (mark % 2 != 0) return std::string{};
SetDParam(0, value / 31 * 25); // 0-127 does not map nicely to 0-100. Dividing first gives us nice round numbers.
return STR_GAME_OPTIONS_VOLUME_MARK;
// 0-127 does not map nicely to 0-100. Dividing first gives us nice round numbers.
return GetString(STR_GAME_OPTIONS_VOLUME_MARK, value / 31 * 25);
}
static constexpr NWidgetPart _nested_social_plugins_widgets[] = {

View File

@ -56,18 +56,18 @@ void DrawSliderWidget(Rect r, int min_value, int max_value, int nmarks, int valu
for (int mark = 0; mark < nmarks; ++mark) {
const int mark_value = (max_value * mark) / (nmarks - 1);
const StringID str = mark_func(nmarks, mark, mark_value + min_value);
if (str == INVALID_STRING_ID) continue;
auto str = mark_func(nmarks, mark, mark_value + min_value);
if (!str.has_value()) continue;
x = mark_value;
if (_current_text_dir == TD_RTL) x = max_value - mark_value;
x = r.left + (x * (r.right - r.left - sw) / max_value) + sw / 2;
GfxDrawLine(x, r.bottom - ha + 1, x, r.bottom + (str == STR_NULL ? 0 : WidgetDimensions::scaled.hsep_normal), shadow, t);
if (str == STR_NULL) continue;
GfxDrawLine(x, r.bottom - ha + 1, x, r.bottom + (str->empty() ? 0 : WidgetDimensions::scaled.hsep_normal), shadow, t);
if (str->empty()) continue;
Dimension d = GetStringBoundingBox(str, FS_SMALL);
Dimension d = GetStringBoundingBox(*str, FS_SMALL);
x = Clamp(x - d.width / 2, r.left, r.right - d.width);
DrawString(x, x + d.width, r.bottom + 1 + WidgetDimensions::scaled.hsep_normal, str, TC_BLACK, SA_CENTER, false, FS_SMALL);
DrawString(x, x + d.width, r.bottom + 1 + WidgetDimensions::scaled.hsep_normal, *str, TC_BLACK, SA_CENTER, false, FS_SMALL);
}
}

View File

@ -13,7 +13,7 @@
#include "core/geometry_type.hpp"
#include "strings_type.h"
using SliderMarkFunc = StringID(int nmarks, int mark, int value);
using SliderMarkFunc = std::optional<std::string>(int nmarks, int mark, int value);
void DrawSliderWidget(Rect r, int min_value, int max_value, int nmarks, int value, SliderMarkFunc *mark_func);
bool ClickSliderWidget(Rect r, Point pt, int min_value, int max_value, int nmarks, int &value);