2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2007-12-21 20:49:27 +01:00
|
|
|
/** @file strings_func.h Functions related to OTTD's strings. */
|
2007-04-04 03:35:16 +02:00
|
|
|
|
2007-12-21 20:49:27 +01:00
|
|
|
#ifndef STRINGS_FUNC_H
|
|
|
|
#define STRINGS_FUNC_H
|
|
|
|
|
|
|
|
#include "strings_type.h"
|
2010-12-09 21:57:52 +01:00
|
|
|
#include "string_type.h"
|
2011-11-19 19:43:00 +01:00
|
|
|
#include "gfx_type.h"
|
2017-02-26 20:39:58 +01:00
|
|
|
#include "core/bitmath_func.hpp"
|
2023-08-12 20:14:21 +02:00
|
|
|
#include "core/strong_typedef_type.hpp"
|
2023-04-08 18:26:13 +02:00
|
|
|
#include "vehicle_type.h"
|
2017-02-26 20:39:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract the StringTab from a StringID.
|
|
|
|
* @param str String identifier
|
|
|
|
* @return StringTab from \a str
|
|
|
|
*/
|
2024-01-06 12:19:27 +01:00
|
|
|
inline StringTab GetStringTab(StringID str)
|
2017-02-26 20:39:58 +01:00
|
|
|
{
|
2017-02-26 20:41:30 +01:00
|
|
|
StringTab result = (StringTab)(str >> TAB_SIZE_BITS);
|
|
|
|
if (result >= TEXT_TAB_NEWGRF_START) return TEXT_TAB_NEWGRF_START;
|
|
|
|
if (result >= TEXT_TAB_GAMESCRIPT_START) return TEXT_TAB_GAMESCRIPT_START;
|
|
|
|
return result;
|
2017-02-26 20:39:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract the StringIndex from a StringID.
|
|
|
|
* @param str String identifier
|
|
|
|
* @return StringIndex from \a str
|
|
|
|
*/
|
2024-01-06 12:19:27 +01:00
|
|
|
inline uint GetStringIndex(StringID str)
|
2017-02-26 20:39:58 +01:00
|
|
|
{
|
2017-02-26 20:41:30 +01:00
|
|
|
return str - (GetStringTab(str) << TAB_SIZE_BITS);
|
2017-02-26 20:39:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a StringID
|
|
|
|
* @param tab StringTab
|
|
|
|
* @param index StringIndex
|
|
|
|
* @return StringID composed from \a tab and \a index
|
|
|
|
*/
|
2024-01-06 12:19:27 +01:00
|
|
|
inline StringID MakeStringID(StringTab tab, uint index)
|
2017-02-26 20:39:58 +01:00
|
|
|
{
|
2017-02-26 20:41:30 +01:00
|
|
|
if (tab == TEXT_TAB_NEWGRF_START) {
|
|
|
|
assert(index < TAB_SIZE_NEWGRF);
|
|
|
|
} else if (tab == TEXT_TAB_GAMESCRIPT_START) {
|
|
|
|
assert(index < TAB_SIZE_GAMESCRIPT);
|
|
|
|
} else {
|
|
|
|
assert(tab < TEXT_TAB_END);
|
|
|
|
assert(index < TAB_SIZE);
|
|
|
|
}
|
|
|
|
return (tab << TAB_SIZE_BITS) + index;
|
2017-02-26 20:39:58 +01:00
|
|
|
}
|
2005-02-06 09:18:00 +01:00
|
|
|
|
2021-05-14 18:32:58 +02:00
|
|
|
std::string GetString(StringID string);
|
2008-01-29 18:09:00 +01:00
|
|
|
const char *GetStringPtr(StringID string);
|
2005-07-15 16:53:44 +02:00
|
|
|
|
2023-04-08 18:26:13 +02:00
|
|
|
uint ConvertKmhishSpeedToDisplaySpeed(uint speed, VehicleType type);
|
|
|
|
uint ConvertDisplaySpeedToKmhishSpeed(uint speed, VehicleType type);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pack velocity and vehicle type for use with SCC_VELOCITY string parameter.
|
|
|
|
* @param speed Display speed for parameter.
|
|
|
|
* @param type Type of vehicle for parameter.
|
|
|
|
* @return Bit-packed velocity and vehicle type, for use with SetDParam().
|
|
|
|
*/
|
2024-01-06 12:19:27 +01:00
|
|
|
inline int64_t PackVelocity(uint speed, VehicleType type)
|
2023-04-08 18:26:13 +02:00
|
|
|
{
|
|
|
|
/* Vehicle type is a byte, so packed into the top 8 bits of the 64-bit
|
|
|
|
* parameter, although only values from 0-3 are relevant. */
|
2023-05-08 19:01:06 +02:00
|
|
|
return speed | (static_cast<uint64_t>(type) << 56);
|
2023-04-08 18:26:13 +02:00
|
|
|
}
|
2012-02-14 18:03:56 +01:00
|
|
|
|
2023-06-18 07:35:30 +02:00
|
|
|
void SetDParam(size_t n, uint64_t v);
|
|
|
|
void SetDParamMaxValue(size_t n, uint64_t max_value, uint min_count = 0, FontSize size = FS_NORMAL);
|
|
|
|
void SetDParamMaxDigits(size_t n, uint count, FontSize size = FS_NORMAL);
|
2012-12-08 18:18:51 +01:00
|
|
|
|
2023-08-12 20:14:21 +02:00
|
|
|
template <typename T, std::enable_if_t<std::is_base_of<StrongTypedefBase, T>::value, int> = 0>
|
|
|
|
void SetDParam(size_t n, T v)
|
|
|
|
{
|
2023-11-06 21:29:35 +01:00
|
|
|
SetDParam(n, v.base());
|
2023-08-12 20:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, std::enable_if_t<std::is_base_of<StrongTypedefBase, T>::value, int> = 0>
|
|
|
|
void SetDParamMaxValue(size_t n, T max_value, uint min_count = 0, FontSize size = FS_NORMAL)
|
|
|
|
{
|
2023-11-06 21:29:35 +01:00
|
|
|
SetDParamMaxValue(n, max_value.base(), min_count, size);
|
2023-08-12 20:14:21 +02:00
|
|
|
}
|
|
|
|
|
2023-06-18 07:35:30 +02:00
|
|
|
void SetDParamStr(size_t n, const char *str);
|
|
|
|
void SetDParamStr(size_t n, const std::string &str);
|
2023-06-27 16:59:44 +02:00
|
|
|
void SetDParamStr(size_t n, std::string &&str);
|
2007-07-16 11:16:58 +02:00
|
|
|
|
2024-01-16 22:46:00 +01:00
|
|
|
void CopyInDParam(const std::span<const StringParameterBackup> backup);
|
2023-06-21 06:35:06 +02:00
|
|
|
void CopyOutDParam(std::vector<StringParameterBackup> &backup, size_t num);
|
2023-06-25 06:26:37 +02:00
|
|
|
bool HaveDParamChanged(const std::vector<StringParameterBackup> &backup);
|
2023-06-21 06:35:06 +02:00
|
|
|
|
2023-06-18 07:35:30 +02:00
|
|
|
uint64_t GetDParam(size_t n);
|
2007-07-16 11:16:58 +02:00
|
|
|
|
2010-11-13 10:56:25 +01:00
|
|
|
extern TextDirection _current_text_dir; ///< Text direction of the currently selected language
|
2005-02-06 09:18:00 +01:00
|
|
|
|
2007-03-17 23:21:05 +01:00
|
|
|
void InitializeLanguagePacks();
|
2009-10-17 22:34:09 +02:00
|
|
|
const char *GetCurrentLanguageIsoCode();
|
2007-03-17 23:21:05 +01:00
|
|
|
|
2011-11-19 19:43:00 +01:00
|
|
|
/**
|
|
|
|
* A searcher for missing glyphs.
|
|
|
|
*/
|
|
|
|
class MissingGlyphSearcher {
|
|
|
|
public:
|
|
|
|
/** Make sure everything gets destructed right. */
|
2023-05-14 23:31:03 +02:00
|
|
|
virtual ~MissingGlyphSearcher() = default;
|
2011-11-19 19:43:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next string to search through.
|
2023-05-09 21:35:50 +02:00
|
|
|
* @return The next string or nullopt if there is none.
|
2011-11-19 19:43:00 +01:00
|
|
|
*/
|
2023-05-09 21:35:50 +02:00
|
|
|
virtual std::optional<std::string_view> NextString() = 0;
|
2011-11-19 19:43:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the default (font) size of the string.
|
|
|
|
* @return The font size.
|
|
|
|
*/
|
|
|
|
virtual FontSize DefaultSize() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the search, i.e. begin from the beginning again.
|
|
|
|
*/
|
|
|
|
virtual void Reset() = 0;
|
|
|
|
|
2011-11-20 13:01:42 +01:00
|
|
|
/**
|
|
|
|
* Whether to search for a monospace font or not.
|
|
|
|
* @return True if searching for monospace.
|
|
|
|
*/
|
|
|
|
virtual bool Monospace() = 0;
|
|
|
|
|
2011-11-19 22:02:37 +01:00
|
|
|
/**
|
|
|
|
* Set the right font names.
|
|
|
|
* @param settings The settings to modify.
|
|
|
|
* @param font_name The new font name.
|
2018-11-25 02:02:20 +01:00
|
|
|
* @param os_data Opaque pointer to OS-specific data.
|
2011-11-19 22:02:37 +01:00
|
|
|
*/
|
2022-09-15 19:21:27 +02:00
|
|
|
virtual void SetFontNames(struct FontCacheSettings *settings, const char *font_name, const void *os_data = nullptr) = 0;
|
2011-11-19 22:02:37 +01:00
|
|
|
|
2021-01-12 19:52:30 +01:00
|
|
|
bool FindMissingGlyphs();
|
2011-11-19 19:43:00 +01:00
|
|
|
};
|
|
|
|
|
2019-04-10 23:07:06 +02:00
|
|
|
void CheckForMissingGlyphs(bool base_font = true, MissingGlyphSearcher *search = nullptr);
|
2007-12-16 19:38:19 +01:00
|
|
|
|
2010-12-22 11:50:32 +01:00
|
|
|
#endif /* STRINGS_FUNC_H */
|