Fix #10023: Allow negative input in text fields when needed (#10112)

This commit is contained in:
hallonsoda79 2022-11-02 20:54:07 +01:00 committed by GitHub
parent 71663bbaee
commit 0d51460f27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 6 deletions

View File

@ -2288,10 +2288,13 @@ struct GameSettingsWindow : Window {
/* Show the correct currency-translated value */
if (sd->flags & SF_GUI_CURRENCY) value64 *= _currency->rate;
CharSetFilter charset_filter = CS_NUMERAL; //default, only numeric input allowed
if (sd->min < 0) charset_filter = CS_NUMERAL_SIGNED; // special case, also allow '-' sign for negative input
this->valuewindow_entry = pe;
SetDParam(0, value64);
/* Limit string length to 14 so that MAX_INT32 * max currency rate doesn't exceed MAX_INT64. */
ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 15, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 15, this, charset_filter, QSF_ENABLE_DEFAULT);
}
this->SetDisplayedHelpText(pe);
}

View File

@ -494,11 +494,12 @@ bool strtolower(std::string &str, std::string::size_type offs)
bool IsValidChar(WChar key, CharSetFilter afilter)
{
switch (afilter) {
case CS_ALPHANUMERAL: return IsPrintable(key);
case CS_NUMERAL: return (key >= '0' && key <= '9');
case CS_NUMERAL_SPACE: return (key >= '0' && key <= '9') || key == ' ';
case CS_ALPHA: return IsPrintable(key) && !(key >= '0' && key <= '9');
case CS_HEXADECIMAL: return (key >= '0' && key <= '9') || (key >= 'a' && key <= 'f') || (key >= 'A' && key <= 'F');
case CS_ALPHANUMERAL: return IsPrintable(key);
case CS_NUMERAL: return (key >= '0' && key <= '9');
case CS_NUMERAL_SPACE: return (key >= '0' && key <= '9') || key == ' ';
case CS_NUMERAL_SIGNED: return (key >= '0' && key <= '9') || key == '-';
case CS_ALPHA: return IsPrintable(key) && !(key >= '0' && key <= '9');
case CS_HEXADECIMAL: return (key >= '0' && key <= '9') || (key >= 'a' && key <= 'f') || (key >= 'A' && key <= 'F');
default: NOT_REACHED();
}
}

View File

@ -27,6 +27,7 @@ enum CharSetFilter {
CS_ALPHANUMERAL, ///< Both numeric and alphabetic and spaces and stuff
CS_NUMERAL, ///< Only numeric ones
CS_NUMERAL_SPACE, ///< Only numbers and spaces
CS_NUMERAL_SIGNED, ///< Only numbers and '-' for negative values
CS_ALPHA, ///< Only alphabetic values
CS_HEXADECIMAL, ///< Only hexadecimal characters
};