mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Move GetCharPosInString/GetCharAtPosition to gfx_layout.
These functions are related more to layouting than graphics.pull/12771/head
parent
bbbf2b5282
commit
80ddcb9d7d
|
@ -17,6 +17,7 @@
|
||||||
#include "string_func.h"
|
#include "string_func.h"
|
||||||
#include "strings_func.h"
|
#include "strings_func.h"
|
||||||
#include "gfx_func.h"
|
#include "gfx_func.h"
|
||||||
|
#include "gfx_layout.h"
|
||||||
#include "settings_type.h"
|
#include "settings_type.h"
|
||||||
#include "console_func.h"
|
#include "console_func.h"
|
||||||
#include "rev.h"
|
#include "rev.h"
|
||||||
|
|
33
src/gfx.cpp
33
src/gfx.cpp
|
@ -895,39 +895,6 @@ Dimension GetStringListBoundingBox(std::span<const StringID> list, FontSize font
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the leading corner of a character in a single-line string relative
|
|
||||||
* to the start of the string.
|
|
||||||
* @param str String containing the character.
|
|
||||||
* @param ch Pointer to the character in the string.
|
|
||||||
* @param start_fontsize Font size to start the text with.
|
|
||||||
* @return Upper left corner of the glyph associated with the character.
|
|
||||||
*/
|
|
||||||
Point GetCharPosInString(std::string_view str, const char *ch, FontSize start_fontsize)
|
|
||||||
{
|
|
||||||
/* Ensure "ch" is inside "str" or at the exact end. */
|
|
||||||
assert(ch >= str.data() && (ch - str.data()) <= static_cast<ptrdiff_t>(str.size()));
|
|
||||||
auto it_ch = str.begin() + (ch - str.data());
|
|
||||||
|
|
||||||
Layouter layout(str, INT32_MAX, start_fontsize);
|
|
||||||
return layout.GetCharPosition(it_ch);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the character from a string that is drawn at a specific position.
|
|
||||||
* @param str String to test.
|
|
||||||
* @param x Position relative to the start of the string.
|
|
||||||
* @param start_fontsize Font size to start the text with.
|
|
||||||
* @return Index of the character position or -1 if there is no character at the position.
|
|
||||||
*/
|
|
||||||
ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize)
|
|
||||||
{
|
|
||||||
if (x < 0) return -1;
|
|
||||||
|
|
||||||
Layouter layout(str, INT32_MAX, start_fontsize);
|
|
||||||
return layout.GetCharAtPosition(x, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Draw single character horizontally centered around (x,y)
|
* Draw single character horizontally centered around (x,y)
|
||||||
* @param c Character (glyph) to draw
|
* @param c Character (glyph) to draw
|
||||||
|
|
|
@ -142,8 +142,6 @@ int GetStringLineCount(StringID str, int maxw);
|
||||||
Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion);
|
Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion);
|
||||||
Dimension GetStringMultiLineBoundingBox(std::string_view str, const Dimension &suggestion);
|
Dimension GetStringMultiLineBoundingBox(std::string_view str, const Dimension &suggestion);
|
||||||
void LoadStringWidthTable(bool monospace = false);
|
void LoadStringWidthTable(bool monospace = false);
|
||||||
Point GetCharPosInString(std::string_view str, const char *ch, FontSize start_fontsize = FS_NORMAL);
|
|
||||||
ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize = FS_NORMAL);
|
|
||||||
|
|
||||||
void DrawDirtyBlocks();
|
void DrawDirtyBlocks();
|
||||||
void AddDirtyBlock(int left, int top, int right, int bottom);
|
void AddDirtyBlock(int left, int top, int right, int bottom);
|
||||||
|
|
|
@ -409,3 +409,36 @@ void Layouter::ReduceLineCache()
|
||||||
if (linecache->size() > 4096) ResetLineCache();
|
if (linecache->size() > 4096) ResetLineCache();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the leading corner of a character in a single-line string relative
|
||||||
|
* to the start of the string.
|
||||||
|
* @param str String containing the character.
|
||||||
|
* @param ch Pointer to the character in the string.
|
||||||
|
* @param start_fontsize Font size to start the text with.
|
||||||
|
* @return Upper left corner of the glyph associated with the character.
|
||||||
|
*/
|
||||||
|
Point GetCharPosInString(std::string_view str, const char *ch, FontSize start_fontsize)
|
||||||
|
{
|
||||||
|
/* Ensure "ch" is inside "str" or at the exact end. */
|
||||||
|
assert(ch >= str.data() && (ch - str.data()) <= static_cast<ptrdiff_t>(str.size()));
|
||||||
|
auto it_ch = str.begin() + (ch - str.data());
|
||||||
|
|
||||||
|
Layouter layout(str, INT32_MAX, start_fontsize);
|
||||||
|
return layout.GetCharPosition(it_ch);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the character from a string that is drawn at a specific position.
|
||||||
|
* @param str String to test.
|
||||||
|
* @param x Position relative to the start of the string.
|
||||||
|
* @param start_fontsize Font size to start the text with.
|
||||||
|
* @return Index of the character position or -1 if there is no character at the position.
|
||||||
|
*/
|
||||||
|
ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize)
|
||||||
|
{
|
||||||
|
if (x < 0) return -1;
|
||||||
|
|
||||||
|
Layouter layout(str, INT32_MAX, start_fontsize);
|
||||||
|
return layout.GetCharAtPosition(x, 0);
|
||||||
|
}
|
||||||
|
|
|
@ -185,4 +185,7 @@ public:
|
||||||
static void ReduceLineCache();
|
static void ReduceLineCache();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Point GetCharPosInString(std::string_view str, const char *ch, FontSize start_fontsize = FS_NORMAL);
|
||||||
|
ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize = FS_NORMAL);
|
||||||
|
|
||||||
#endif /* GFX_LAYOUT_H */
|
#endif /* GFX_LAYOUT_H */
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
#include "landscape.h"
|
#include "landscape.h"
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
#include "gui.h"
|
#include "gui.h"
|
||||||
|
#include "gfx_layout.h"
|
||||||
#include "command_func.h"
|
#include "command_func.h"
|
||||||
#include "company_func.h"
|
#include "company_func.h"
|
||||||
#include "town.h"
|
#include "town.h"
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
#include "strings_func.h"
|
#include "strings_func.h"
|
||||||
#include "gfx_type.h"
|
#include "gfx_type.h"
|
||||||
#include "gfx_func.h"
|
#include "gfx_func.h"
|
||||||
|
#include "gfx_layout.h"
|
||||||
#include "window_func.h"
|
#include "window_func.h"
|
||||||
#include "core/alloc_func.hpp"
|
#include "core/alloc_func.hpp"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue