Utility/RichTextDrawer: Fixes block removal not adjusting glyph indexes

+ Add HasBlocks method
This commit is contained in:
Lynix
2019-09-24 18:44:12 +02:00
committed by Jérôme Leclercq
parent 1ea653ab5b
commit 3c7addc262
3 changed files with 38 additions and 20 deletions

View File

@@ -54,6 +54,8 @@ namespace Nz
std::size_t GetLineCount() const override;
float GetMaxLineWidth() const override;
inline bool HasBlocks() const;
void MergeBlocks();
void RemoveBlock(std::size_t index);
@@ -62,7 +64,7 @@ namespace Nz
inline void SetBlockColor(std::size_t index, const Color& color);
inline void SetBlockFont(std::size_t index, FontRef font);
inline void SetBlockStyle(std::size_t index, TextStyleFlags style);
inline void SetBlockText(std::size_t index, const String& str);
inline void SetBlockText(std::size_t index, String str);
inline void SetDefaultCharacterSize(unsigned int characterSize);
inline void SetDefaultColor(const Color& color);

View File

@@ -9,6 +9,16 @@ namespace Nz
{
inline std::size_t RichTextDrawer::FindBlock(std::size_t glyphIndex) const
{
auto it = m_blocks.begin();
for (; it != m_blocks.end(); ++it)
{
if (it->glyphIndex > glyphIndex)
break;
}
assert(it != m_blocks.begin());
return std::distance(m_blocks.begin(), it) - 1;
/*
// Binary search
std::size_t count = m_blocks.size();
std::size_t step;
@@ -25,14 +35,14 @@ namespace Nz
if (m_blocks[i].glyphIndex < glyphIndex)
{
first = i + 1;
first = ++i;
count -= step + 1;
}
else
count = step;
}
return i;
return i;*/
}
inline auto RichTextDrawer::GetBlock(std::size_t index) -> BlockRef
@@ -193,6 +203,11 @@ namespace Nz
}
}
inline bool RichTextDrawer::HasBlocks() const
{
return !m_blocks.empty();
}
inline void RichTextDrawer::SetBlockCharacterSize(std::size_t index, unsigned int characterSize)
{
NazaraAssert(index < m_blocks.size(), "Invalid block index");
@@ -232,13 +247,13 @@ namespace Nz
InvalidateGlyphs();
}
inline void RichTextDrawer::SetBlockText(std::size_t index, const String& str)
inline void RichTextDrawer::SetBlockText(std::size_t index, String str)
{
NazaraAssert(index < m_blocks.size(), "Invalid block index");
std::size_t previousLength = m_blocks[index].text.GetLength();
m_blocks[index].text = str;
m_blocks[index].text = std::move(str);
std::size_t newLength = m_blocks[index].text.GetLength();
if (newLength != previousLength)