1
0
Fork 0

Compare commits

...

4 Commits

Author SHA1 Message Date
SamuXarick 8214eeadb2
Merge 3c56c0fe2e into 7546c1acab 2025-07-14 08:03:53 +00:00
Peter Nelson 7546c1acab
Codefix f220ed179d: GetUnicodeGlyph takes a unicode character. (#14438)
Previous change erroneously changed type to GlyphID, based on naming. It should actually be char32_t.
2025-07-14 08:01:42 +00:00
Peter Nelson a6143eea21
Codechange: Include more relevant headers for script_storage. (#14437) 2025-07-14 07:49:50 +01:00
SamuXarick 3c56c0fe2e Add: [Script] Default button in query string to reset an integer setting 2025-06-08 23:15:20 +01:00
8 changed files with 70 additions and 25 deletions

View File

@ -43,26 +43,26 @@ SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs)
} }
/** /**
* Get SpriteID associated with a GlyphID. * Get SpriteID associated with a character.
* @param key Glyph to find. * @param key Character to find.
* @return SpriteID of glyph, or 0 if not present. * @return SpriteID for character, or 0 if not present.
*/ */
SpriteID SpriteFontCache::GetUnicodeGlyph(GlyphID key) SpriteID SpriteFontCache::GetUnicodeGlyph(char32_t key)
{ {
const auto found = this->glyph_to_spriteid_map.find(key & ~SPRITE_GLYPH); const auto found = this->char_map.find(key);
if (found == std::end(this->glyph_to_spriteid_map)) return 0; if (found == std::end(this->char_map)) return 0;
return found->second; return found->second;
} }
void SpriteFontCache::SetUnicodeGlyph(char32_t key, SpriteID sprite) void SpriteFontCache::SetUnicodeGlyph(char32_t key, SpriteID sprite)
{ {
this->glyph_to_spriteid_map[key] = sprite; this->char_map[key] = sprite;
} }
void SpriteFontCache::InitializeUnicodeGlyphMap() void SpriteFontCache::InitializeUnicodeGlyphMap()
{ {
/* Clear out existing glyph map if it exists */ /* Clear out existing glyph map if it exists */
this->glyph_to_spriteid_map.clear(); this->char_map.clear();
SpriteID base; SpriteID base;
switch (this->fs) { switch (this->fs) {

View File

@ -28,8 +28,8 @@ public:
bool IsBuiltInFont() override { return true; } bool IsBuiltInFont() override { return true; }
private: private:
std::unordered_map<GlyphID, SpriteID> glyph_to_spriteid_map{}; ///< Mapping of glyphs to sprite IDs. std::unordered_map<char32_t, SpriteID> char_map{}; ///< Mapping of characters to sprite IDs.
SpriteID GetUnicodeGlyph(GlyphID key); SpriteID GetUnicodeGlyph(char32_t key);
}; };
#endif /* SPRITEFONTCACHE_H */ #endif /* SPRITEFONTCACHE_H */

View File

@ -325,7 +325,7 @@ struct GSConfigWindow : public Window {
} }
} else if (!bool_item && !config_item.complete_labels) { } else if (!bool_item && !config_item.complete_labels) {
/* Display a query box so users can enter a custom value. */ /* Display a query box so users can enter a custom value. */
ShowQueryString(GetString(STR_JUST_INT, old_val), STR_CONFIG_SETTING_QUERY_CAPTION, INT32_DIGITS_WITH_SIGN_AND_TERMINATION, this, CS_NUMERAL_SIGNED, {}); ShowQueryString(GetString(STR_JUST_INT, old_val), STR_CONFIG_SETTING_QUERY_CAPTION, INT32_DIGITS_WITH_SIGN_AND_TERMINATION, this, CS_NUMERAL_SIGNED, QueryStringFlag::EnableDefault);
} }
this->SetDirty(); this->SetDirty();
break; break;
@ -347,10 +347,17 @@ struct GSConfigWindow : public Window {
void OnQueryTextFinished(std::optional<std::string> str) override void OnQueryTextFinished(std::optional<std::string> str) override
{ {
/* The user pressed cancel */
if (!str.has_value()) return; if (!str.has_value()) return;
auto value = ParseInteger<int32_t>(*str, 10, true);
if (!value.has_value()) return; if (!str->empty()) {
this->SetValue(*value); auto value = ParseInteger<int32_t>(*str, 10, true);
if (!value.has_value()) return;
this->SetValue(*value);
} else {
this->SetDefaultValue();
}
} }
void OnDropdownSelect(WidgetID widget, int index, int) override void OnDropdownSelect(WidgetID widget, int index, int) override
@ -419,6 +426,14 @@ private:
this->gs_config->SetSetting(config_item.name, value); this->gs_config->SetSetting(config_item.name, value);
this->SetDirty(); this->SetDirty();
} }
void SetDefaultValue()
{
const ScriptConfigItem &config_item = *this->visible_settings[this->clicked_row];
if (_game_mode == GM_NORMAL && !config_item.flags.Test(ScriptConfigFlag::InGame)) return;
this->gs_config->ResetSetting(config_item.name);
this->SetDirty();
}
}; };
/** Open the GS config window. */ /** Open the GS config window. */

View File

@ -106,6 +106,16 @@ void ScriptConfig::ResetSettings()
this->settings.clear(); this->settings.clear();
} }
void ScriptConfig::ResetSetting(std::string_view name)
{
if (this->info == nullptr) return;
const ScriptConfigItem *config_item = this->info->GetConfigItem(name);
if (config_item == nullptr) return;
this->settings.erase(std::string{name});
}
void ScriptConfig::ResetEditableSettings(bool yet_to_start) void ScriptConfig::ResetEditableSettings(bool yet_to_start)
{ {
if (this->info == nullptr) return ResetSettings(); if (this->info == nullptr) return ResetSettings();

View File

@ -129,6 +129,11 @@ public:
*/ */
void ResetSettings(); void ResetSettings();
/**
* Reset a setting to its default value.
*/
void ResetSetting(std::string_view name);
/** /**
* Reset only editable and visible settings to their default value. * Reset only editable and visible settings to their default value.
*/ */

View File

@ -470,7 +470,7 @@ struct ScriptSettingsWindow : public Window {
} }
} else if (!bool_item && !config_item.complete_labels) { } else if (!bool_item && !config_item.complete_labels) {
/* Display a query box so users can enter a custom value. */ /* Display a query box so users can enter a custom value. */
ShowQueryString(GetString(STR_JUST_INT, old_val), STR_CONFIG_SETTING_QUERY_CAPTION, INT32_DIGITS_WITH_SIGN_AND_TERMINATION, this, CS_NUMERAL_SIGNED, {}); ShowQueryString(GetString(STR_JUST_INT, old_val), STR_CONFIG_SETTING_QUERY_CAPTION, INT32_DIGITS_WITH_SIGN_AND_TERMINATION, this, CS_NUMERAL_SIGNED, QueryStringFlag::EnableDefault);
} }
this->SetDirty(); this->SetDirty();
break; break;
@ -485,10 +485,17 @@ struct ScriptSettingsWindow : public Window {
void OnQueryTextFinished(std::optional<std::string> str) override void OnQueryTextFinished(std::optional<std::string> str) override
{ {
/* The user pressed cancel */
if (!str.has_value()) return; if (!str.has_value()) return;
auto value = ParseInteger<int32_t>(*str, 10, true);
if (!value.has_value()) return; if (!str->empty()) {
this->SetValue(*value); auto value = ParseInteger<int32_t>(*str, 10, true);
if (!value.has_value()) return;
this->SetValue(*value);
} else {
this->SetDefaultValue();
}
} }
void OnDropdownSelect(WidgetID widget, int index, int) override void OnDropdownSelect(WidgetID widget, int index, int) override
@ -552,6 +559,14 @@ private:
this->script_config->SetSetting(config_item.name, value); this->script_config->SetSetting(config_item.name, value);
this->SetDirty(); this->SetDirty();
} }
void SetDefaultValue()
{
const ScriptConfigItem &config_item = *this->visible_settings[this->clicked_row];
if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && !config_item.flags.Test(ScriptConfigFlag::InGame)) return;
this->script_config->ResetSetting(config_item.name);
this->SetDirty();
}
}; };
/** Widgets for the Script settings window. */ /** Widgets for the Script settings window. */

View File

@ -24,10 +24,12 @@
#include "api/script_event.hpp" #include "api/script_event.hpp"
#include "api/script_log.hpp" #include "api/script_log.hpp"
#include "../company_base.h" #include "../company_type.h"
#include "../company_func.h"
#include "../fileio_func.h" #include "../fileio_func.h"
#include "../goal_type.h"
#include "../league_type.h" #include "../league_type.h"
#include "../signs_type.h"
#include "../story_type.h"
#include "../misc/endian_buffer.hpp" #include "../misc/endian_buffer.hpp"
#include "../safeguards.h" #include "../safeguards.h"

View File

@ -12,12 +12,10 @@
#include <queue> #include <queue>
#include "../signs_func.h" #include "../command_type.h"
#include "../vehicle_func.h" #include "../company_type.h"
#include "../rail_type.h"
#include "../road_type.h" #include "../road_type.h"
#include "../group.h"
#include "../goal_type.h"
#include "../story_type.h"
#include "script_types.hpp" #include "script_types.hpp"
#include "script_log_types.hpp" #include "script_log_types.hpp"