From ba2df2cb4201597c9dc01365641413dcbf6812d9 Mon Sep 17 00:00:00 2001 From: erihel Date: Sun, 24 Feb 2013 01:40:55 +0100 Subject: [PATCH] * Fix for satcom freeze --- src/graphics/engine/text.cpp | 35 +++++++++++++++++++++-------------- src/graphics/engine/text.h | 8 +++++++- src/ui/edit.cpp | 12 ++++++++++-- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/graphics/engine/text.cpp b/src/graphics/engine/text.cpp index 424b99b3..9dea129f 100644 --- a/src/graphics/engine/text.cpp +++ b/src/graphics/engine/text.cpp @@ -149,6 +149,7 @@ void CText::FlushCache() } void CText::DrawText(const std::string &text, std::vector::iterator format, + std::vector::iterator end, float size, Math::Point pos, float width, TextAlign align, int eol, Color color) { @@ -156,18 +157,18 @@ void CText::DrawText(const std::string &text, std::vector::iterato if (align == TEXT_ALIGN_CENTER) { - sw = GetStringWidth(text, format, size); + sw = GetStringWidth(text, format, end, size); if (sw > width) sw = width; pos.x -= sw / 2.0f; } else if (align == TEXT_ALIGN_RIGHT) { - sw = GetStringWidth(text, format, size); + sw = GetStringWidth(text, format, end, size); if (sw > width) sw = width; pos.x -= sw; } - DrawString(text, format, size, pos, width, eol, color); + DrawString(text, format, end, size, pos, width, eol, color); } void CText::DrawText(const std::string &text, FontType font, @@ -193,12 +194,13 @@ void CText::DrawText(const std::string &text, FontType font, } void CText::SizeText(const std::string &text, std::vector::iterator format, + std::vector::iterator endFormat, float size, Math::Point pos, TextAlign align, Math::Point &start, Math::Point &end) { start = end = pos; - float sw = GetStringWidth(text, format, size); + float sw = GetStringWidth(text, format, endFormat, size); end.x += sw; if (align == TEXT_ALIGN_CENTER) { @@ -276,7 +278,8 @@ float CText::GetHeight(FontType font, float size) float CText::GetStringWidth(const std::string &text, - std::vector::iterator format, float size) + std::vector::iterator format, + std::vector::iterator end, float size) { float width = 0.0f; unsigned int index = 0; @@ -284,8 +287,8 @@ float CText::GetStringWidth(const std::string &text, while (index < text.length()) { FontType font = FONT_COLOBOT; - //if (format.size() > fmtIndex) - font = static_cast(format[fmtIndex] & FONT_MASK_FONT); + if (format + fmtIndex != end) + font = static_cast(*(format + fmtIndex) & FONT_MASK_FONT); UTF8Char ch; @@ -343,6 +346,7 @@ float CText::GetCharWidth(UTF8Char ch, FontType font, float size, float offset) int CText::Justify(const std::string &text, std::vector::iterator format, + std::vector::iterator end, float size, float width) { float pos = 0.0f; @@ -352,8 +356,8 @@ int CText::Justify(const std::string &text, std::vector::iterator while (index < text.length()) { FontType font = FONT_COLOBOT; - //if (format.size() > fmtIndex) - font = static_cast(format[fmtIndex] & FONT_MASK_FONT); + if (format + fmtIndex != end) + font = static_cast(*(format + fmtIndex) & FONT_MASK_FONT); UTF8Char ch; @@ -427,6 +431,7 @@ int CText::Justify(const std::string &text, FontType font, float size, float wid } int CText::Detect(const std::string &text, std::vector::iterator format, + std::vector::iterator end, float size, float offset) { float pos = 0.0f; @@ -435,11 +440,12 @@ int CText::Detect(const std::string &text, std::vector::iterator f while (index < text.length()) { FontType font = FONT_COLOBOT; - //if (format.size() > fmtIndex) - font = static_cast(format[fmtIndex] & FONT_MASK_FONT); + + if (format + fmtIndex != end) + font = static_cast(*(format + fmtIndex) & FONT_MASK_FONT); // TODO: if (font == FONT_BUTTON) - if (font == FONT_BUTTON) continue; + //if (font == FONT_BUTTON) continue; UTF8Char ch; @@ -500,6 +506,7 @@ int CText::Detect(const std::string &text, FontType font, float size, float offs } void CText::DrawString(const std::string &text, std::vector::iterator format, + std::vector::iterator end, float size, Math::Point pos, float width, int eol, Color color) { m_engine->SetState(ENG_RSTATE_TEXT); @@ -513,8 +520,8 @@ void CText::DrawString(const std::string &text, std::vector::itera for (auto it = chars.begin(); it != chars.end(); ++it) { FontType font = FONT_COLOBOT; - //if (format.size() > fmtIndex) - font = static_cast(format[fmtIndex] & FONT_MASK_FONT); + if (format + fmtIndex != end) + font = static_cast(*(format + fmtIndex) & FONT_MASK_FONT); // TODO: if (font == FONT_BUTTON) if (font == FONT_BUTTON) continue; diff --git a/src/graphics/engine/text.h b/src/graphics/engine/text.h index d8e2aff1..6bcc59b3 100644 --- a/src/graphics/engine/text.h +++ b/src/graphics/engine/text.h @@ -242,6 +242,7 @@ public: //! Draws text (multi-format) void DrawText(const std::string &text, std::vector::iterator format, + std::vector::iterator end, float size, Math::Point pos, float width, TextAlign align, int eol, Color color = Color(0.0f, 0.0f, 0.0f, 1.0f)); //! Draws text (one font) @@ -251,6 +252,7 @@ public: //! Calculates dimensions for text (multi-format) void SizeText(const std::string &text, std::vector::iterator format, + std::vector::iterator endFormat, float size, Math::Point pos, TextAlign align, Math::Point &start, Math::Point &end); //! Calculates dimensions for text (one font) @@ -267,7 +269,8 @@ public: //! Returns width of string (multi-format) TEST_VIRTUAL float GetStringWidth(const std::string &text, - std::vector::iterator format, float size); + std::vector::iterator format, + std::vector::iterator end, float size); //! Returns width of string (single font) TEST_VIRTUAL float GetStringWidth(const std::string &text, FontType font, float size); //! Returns width of single character @@ -275,12 +278,14 @@ public: //! Justifies a line of text (multi-format) int Justify(const std::string &text, std::vector::iterator format, + std::vector::iterator end, float size, float width); //! Justifies a line of text (one font) int Justify(const std::string &text, FontType font, float size, float width); //! Returns the most suitable position to a given offset (multi-format) int Detect(const std::string &text, std::vector::iterator format, + std::vector::iterator end, float size, float offset); //! Returns the most suitable position to a given offset (one font) int Detect(const std::string &text, FontType font, float size, float offset); @@ -290,6 +295,7 @@ protected: CharTexture CreateCharTexture(UTF8Char ch, CachedFont* font); void DrawString(const std::string &text, std::vector::iterator format, + std::vector::iterator end, float size, Math::Point pos, float width, int eol, Color color); void DrawString(const std::string &text, FontType font, float size, Math::Point pos, float width, int eol, Color color); diff --git a/src/ui/edit.cpp b/src/ui/edit.cpp index 64004bb5..c0b64463 100644 --- a/src/ui/edit.cpp +++ b/src/ui/edit.cpp @@ -261,7 +261,7 @@ bool CEdit::EventProcess(const Event &event) if ( event.type == EVENT_MOUSE_MOVE ) { - if ( Detect(event.mousePos) && + if ( Detect(event.mousePos) && event.mousePos.x < m_pos.x+m_dim.x-(m_bMulti?MARGX+SCROLL_WIDTH:0.0f) ) { if ( m_bEdit ) @@ -560,7 +560,7 @@ bool CEdit::IsLinkPos(Math::Point pos) if ( i == -1 ) return false; if ( i >= m_len ) return false; - if ( (m_format[i]& Gfx::FONT_MASK_HIGHLIGHT) == Gfx::FONT_HIGHLIGHT_LINK) return true; // TODO + if ( m_format.size() > i && ((m_format[i] & Gfx::FONT_MASK_HIGHLIGHT) == Gfx::FONT_HIGHLIGHT_LINK)) return true; // TODO return false; } @@ -736,6 +736,7 @@ int CEdit::MouseDetect(Math::Point mouse) // m_fontStretch); c = m_engine->GetText()->Detect(std::string(m_text+m_lineOffset[i]).substr(0, len), m_format.begin() + m_lineOffset[i], + m_format.end(), size, offset); // TODO check if good } @@ -1035,9 +1036,11 @@ void CEdit::Draw() { start.x = ppos.x+m_engine->GetText()->GetStringWidth(std::string(m_text+beg).substr(0, o1-beg), m_format.begin() + beg, + m_format.end(), size); end.x = m_engine->GetText()->GetStringWidth(std::string(m_text+o1).substr(0, o2-o1), m_format.begin() + o1, + m_format.end(), size); } @@ -1066,6 +1069,7 @@ void CEdit::Draw() { m_engine->GetText()->DrawText(std::string(m_text+beg).substr(0, len), m_format.begin() + beg, + m_format.end(), size, ppos, m_dim.x, @@ -1108,6 +1112,7 @@ void CEdit::Draw() { m_engine->GetText()->SizeText(std::string(m_text+m_lineOffset[i]).substr(0, len), m_format.begin() + m_lineOffset[i], + m_format.end(), size, pos, Gfx::TEXT_ALIGN_LEFT, start, end); } @@ -2464,6 +2469,7 @@ void CEdit::MoveLine(int move, bool bWord, bool bSelect) { c = m_engine->GetText()->Detect(std::string(m_text+m_lineOffset[line]), m_format.begin() + m_lineOffset[line], + m_format.end(), m_fontSize, m_lineOffset[line+1]-m_lineOffset[line]); } @@ -2495,6 +2501,7 @@ void CEdit::ColumnFix() m_column = m_engine->GetText()->GetStringWidth( std::string(m_text+m_lineOffset[line]), m_format.begin() + m_lineOffset[line], + m_format.end(), m_fontSize ); } @@ -3115,6 +3122,7 @@ void CEdit::Justif() // TODO check if good i += m_engine->GetText()->Justify(std::string(m_text+i), m_format.begin() + i, + m_format.end(), size, width); }