From 3f6a6a9eefc54452dc99a15f67bdfabc7e19788b Mon Sep 17 00:00:00 2001 From: erihel Date: Sat, 9 Feb 2013 23:49:38 +0100 Subject: * Syntax highlighting in cbot editor (needs to be tested) --- src/graphics/engine/text.cpp | 23 ++++++++-------- src/graphics/engine/text.h | 12 ++++----- src/ui/edit.cpp | 63 ++++++++++++++++++++++++++------------------ src/ui/edit.h | 2 +- 4 files changed, 57 insertions(+), 43 deletions(-) diff --git a/src/graphics/engine/text.cpp b/src/graphics/engine/text.cpp index dfe7a3c..48af081 100644 --- a/src/graphics/engine/text.cpp +++ b/src/graphics/engine/text.cpp @@ -153,7 +153,7 @@ void CText::FlushCache() m_lastCachedFont = nullptr; } -void CText::DrawText(const std::string &text, std::map &format, +void CText::DrawText(const std::string &text, std::vector::iterator format, float size, Math::Point pos, float width, TextAlign align, int eol, Color color) { @@ -197,7 +197,7 @@ void CText::DrawText(const std::string &text, FontType font, DrawString(text, font, size, pos, width, eol, color); } -void CText::SizeText(const std::string &text, std::map &format, +void CText::SizeText(const std::string &text, std::vector::iterator format, float size, Math::Point pos, TextAlign align, Math::Point &start, Math::Point &end) { @@ -281,7 +281,7 @@ float CText::GetHeight(FontType font, float size) float CText::GetStringWidth(const std::string &text, - std::map &format, float size) + std::vector::iterator format, float size) { float width = 0.0f; unsigned int index = 0; @@ -289,7 +289,7 @@ float CText::GetStringWidth(const std::string &text, while (index < text.length()) { FontType font = FONT_COLOBOT; - if (format.count(fmtIndex)) + //if (format.size() > fmtIndex) font = static_cast(format[fmtIndex] & FONT_MASK_FONT); UTF8Char ch; @@ -347,7 +347,7 @@ float CText::GetCharWidth(UTF8Char ch, FontType font, float size, float offset) } -int CText::Justify(const std::string &text, std::map &format, +int CText::Justify(const std::string &text, std::vector::iterator format, float size, float width) { float pos = 0.0f; @@ -357,7 +357,7 @@ int CText::Justify(const std::string &text, std::map while (index < text.length()) { FontType font = FONT_COLOBOT; - if (format.count(fmtIndex)) + //if (format.size() > fmtIndex) font = static_cast(format[fmtIndex] & FONT_MASK_FONT); UTF8Char ch; @@ -431,7 +431,7 @@ int CText::Justify(const std::string &text, FontType font, float size, float wid return index; } -int CText::Detect(const std::string &text, std::map &format, +int CText::Detect(const std::string &text, std::vector::iterator format, float size, float offset) { float pos = 0.0f; @@ -440,7 +440,7 @@ int CText::Detect(const std::string &text, std::map while (index < text.length()) { FontType font = FONT_COLOBOT; - if (format.count(fmtIndex)) + //if (format.size() > fmtIndex) font = static_cast(format[fmtIndex] & FONT_MASK_FONT); // TODO: if (font == FONT_BUTTON) @@ -504,7 +504,7 @@ int CText::Detect(const std::string &text, FontType font, float size, float offs return index; } -void CText::DrawString(const std::string &text, std::map &format, +void CText::DrawString(const std::string &text, std::vector::iterator format, float size, Math::Point pos, float width, int eol, Color color) { m_engine->SetState(ENG_RSTATE_TEXT); @@ -518,7 +518,7 @@ void CText::DrawString(const std::string &text, std::map fmtIndex) font = static_cast(format[fmtIndex] & FONT_MASK_FONT); // TODO: if (font == FONT_BUTTON) @@ -684,6 +684,7 @@ void CText::DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::P if (ch.c1 > 0 && ch.c1 < 32) { // FIXME add support for chars with code 9 10 23 if (ch.c1 == '\t') { ch.c1 = ':'; + width = 4; } else { ch.c1 = ' '; } @@ -708,7 +709,7 @@ void CText::DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::P } Math::Point p1(pos.x, pos.y + tex.charSize.y - tex.texSize.y); - Math::Point p2(pos.x + tex.texSize.x * width, pos.y + tex.charSize.y); + Math::Point p2(pos.x + tex.texSize.x, pos.y + tex.charSize.y); Math::Vector n(0.0f, 0.0f, -1.0f); // normal diff --git a/src/graphics/engine/text.h b/src/graphics/engine/text.h index 57fad43..e7578df 100644 --- a/src/graphics/engine/text.h +++ b/src/graphics/engine/text.h @@ -244,7 +244,7 @@ public: void FlushCache(); //! Draws text (multi-format) - void DrawText(const std::string &text, std::map &format, + void DrawText(const std::string &text, std::vector::iterator format, 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) @@ -253,7 +253,7 @@ public: int eol, Color color = Color(0.0f, 0.0f, 0.0f, 1.0f)); //! Calculates dimensions for text (multi-format) - void SizeText(const std::string &text, std::map &format, + void SizeText(const std::string &text, std::vector::iterator format, float size, Math::Point pos, TextAlign align, Math::Point &start, Math::Point &end); //! Calculates dimensions for text (one font) @@ -270,20 +270,20 @@ public: //! Returns width of string (multi-format) TEST_VIRTUAL float GetStringWidth(const std::string &text, - std::map &format, float size); + std::vector::iterator format, 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 TEST_VIRTUAL float GetCharWidth(UTF8Char ch, FontType font, float size, float offset); //! Justifies a line of text (multi-format) - int Justify(const std::string &text, std::map &format, + int Justify(const std::string &text, std::vector::iterator format, 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::map &format, + int Detect(const std::string &text, std::vector::iterator format, 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); @@ -292,7 +292,7 @@ protected: CachedFont* GetOrOpenFont(FontType type, float size); CharTexture CreateCharTexture(UTF8Char ch, CachedFont* font); - void DrawString(const std::string &text, std::map &format, + void DrawString(const std::string &text, std::vector::iterator format, 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 a1d213f..792e4a9 100644 --- a/src/ui/edit.cpp +++ b/src/ui/edit.cpp @@ -735,7 +735,7 @@ int CEdit::MouseDetect(Math::Point mouse) // len, offset, size, // m_fontStretch); c = m_engine->GetText()->Detect(std::string(m_text+m_lineOffset[i]).substr(0, len), - m_format, + m_format.begin() + m_lineOffset[i], size, offset); // TODO check if good } @@ -950,7 +950,7 @@ void CEdit::Draw() size = m_fontSize; // Headline \b;? - if ( beg+len < m_len && m_format.count(beg) && + if ( beg+len < m_len && m_format.size() > static_cast(beg) && (m_format[beg]&Gfx::FONT_MASK_TITLE) == Gfx::FONT_TITLE_BIG ) { start.x = ppos.x-MARGX; @@ -964,7 +964,7 @@ void CEdit::Draw() } // As \t;? - if ( beg+len < m_len && m_format.count(beg) && + if ( beg+len < m_len && m_format.size() > static_cast(beg) && (m_format[beg]&Gfx::FONT_MASK_TITLE) == Gfx::FONT_TITLE_NORM ) { start.x = ppos.x-MARGX; @@ -975,7 +975,7 @@ void CEdit::Draw() } // Subtitle \s;? - if ( beg+len < m_len && m_format.count(beg) && + if ( beg+len < m_len && m_format.size() > static_cast(beg) && (m_format[beg]&Gfx::FONT_MASK_TITLE) == Gfx::FONT_TITLE_LITTLE ) { start.x = ppos.x-MARGX; @@ -986,7 +986,7 @@ void CEdit::Draw() } // Table \tab;? - if ( beg+len < m_len && m_format.count(beg) && + if ( beg+len < m_len && m_format.size() > static_cast(beg) && (m_format[beg]&Gfx::FONT_MASK_HIGHLIGHT) == Gfx::FONT_HIGHLIGHT_TABLE ) { start.x = ppos.x-MARGX; @@ -997,7 +997,7 @@ void CEdit::Draw() } // Image \image; ? - if ( beg+len < m_len && m_format.count(beg) && + if ( beg+len < m_len && m_format.size() > static_cast(beg) && (m_format[beg]&Gfx::FONT_MASK_IMAGE) != 0 ) { line = 1; @@ -1005,7 +1005,7 @@ void CEdit::Draw() { if ( i+line >= m_lineTotal || i+line >= m_lineFirst+m_lineVisible || - (m_format.count(beg+line) && m_format[beg+line]&Gfx::FONT_MASK_IMAGE) == 0 ) break; + (m_format.size() > static_cast(beg+line) && m_format[beg+line]&Gfx::FONT_MASK_IMAGE) == 0 ) break; line ++; } @@ -1034,16 +1034,16 @@ void CEdit::Draw() else { start.x = ppos.x+m_engine->GetText()->GetStringWidth(std::string(m_text+beg).substr(0, o1-beg), - m_format, + m_format.begin() + beg, size); end.x = m_engine->GetText()->GetStringWidth(std::string(m_text+o1).substr(0, o2-o1), - m_format, + m_format.begin() + o1, size); } start.y = ppos.y-(m_bMulti?0.0f:MARGY1); end.y = m_lineHeight; - if ( m_format.count(beg) && (m_format[beg]&Gfx::FONT_MASK_TITLE) == Gfx::FONT_TITLE_BIG) end.y *= BIG_FONT; + if ( m_format.size() > static_cast(beg) && (m_format[beg]&Gfx::FONT_MASK_TITLE) == Gfx::FONT_TITLE_BIG) end.y *= BIG_FONT; DrawPart(start, end, 1); // plain yellow background } @@ -1065,7 +1065,7 @@ void CEdit::Draw() else { m_engine->GetText()->DrawText(std::string(m_text+beg).substr(0, len), - m_format, + m_format.begin() + beg, size, ppos, m_dim.x, @@ -1107,7 +1107,7 @@ void CEdit::Draw() else { m_engine->GetText()->SizeText(std::string(m_text+m_lineOffset[i]).substr(0, len), - m_format, + m_format.begin() + m_lineOffset[i], size, pos, Gfx::TEXT_ALIGN_LEFT, start, end); } @@ -1491,8 +1491,11 @@ bool CEdit::ReadText(const char *filename, int addSize) fread(buffer, 1, len, file); - if ( m_format.size() > 0 ) - m_format.clear(); + m_format.clear(); + m_format.reserve(m_maxChar+1); + for (i = 0; i <= m_maxChar+1; i++) { + m_format.push_back(0); + } fclose(file); @@ -1957,8 +1960,11 @@ void CEdit::SetMaxChar(int max) m_text = new char[m_maxChar+1]; memset(m_text, 0, m_maxChar+1); - if (m_format.size() > 0) - m_format.clear(); + m_format.clear(); + m_format.reserve(m_maxChar+1); + for (int i = 0; i <= m_maxChar+1; i++) { + m_format.push_back(0); + } m_len = 0; m_cursor1 = 0; @@ -2147,6 +2153,13 @@ bool CEdit::GetDisplaySpec() void CEdit::SetMultiFont(bool bMulti) { m_format.clear(); + + if (bMulti) { + m_format.reserve(m_maxChar+1); + for (int i = 0; i <= m_maxChar+1; i++) { + m_format.push_back(0); + } + } } // TODO check if it works correctly; was checking if variable is null @@ -2450,7 +2463,7 @@ void CEdit::MoveLine(int move, bool bWord, bool bSelect) else { c = m_engine->GetText()->Detect(std::string(m_text+m_lineOffset[line]), - m_format, + m_format.begin() + m_lineOffset[line], m_fontSize, m_lineOffset[line+1]-m_lineOffset[line]); } @@ -2481,7 +2494,7 @@ void CEdit::ColumnFix() { m_column = m_engine->GetText()->GetStringWidth( std::string(m_text+m_lineOffset[line]), - m_format, + m_format.begin() + m_lineOffset[line], m_fontSize ); } @@ -2884,7 +2897,7 @@ void CEdit::DeleteOne(int dir) { m_text[i] = m_text[i+hole]; - if ( m_format.count(i+hole) ) + if ( m_format.size() > static_cast(i + hole) ) { m_format[i] = m_format[i+hole]; } @@ -3087,13 +3100,13 @@ void CEdit::Justif() { size = m_fontSize; - if ( m_format.count(i) && (m_format[i]&Gfx::FONT_MASK_TITLE) == Gfx::FONT_TITLE_BIG ) // headline? + if ( m_format.size() > static_cast(i) && (m_format[i]&Gfx::FONT_MASK_TITLE) == Gfx::FONT_TITLE_BIG ) // headline? { size *= BIG_FONT; bDual = true; } - if ( m_format.count(i) && (m_format[i]&Gfx::FONT_MASK_IMAGE) != 0 ) // image part? + if ( m_format.size() > static_cast(i) && (m_format[i]&Gfx::FONT_MASK_IMAGE) != 0 ) // image part? { i ++; // jumps just a character (index in m_image) } @@ -3101,7 +3114,7 @@ void CEdit::Justif() { // TODO check if good i += m_engine->GetText()->Justify(std::string(m_text+i), - m_format, + m_format.begin() + i, size, width); } @@ -3296,12 +3309,12 @@ bool CEdit::SetFormat(int cursor1, int cursor2, int format) { int i; - //if ( m_format.size() == 0 ) return false; + if ( m_format.size() < static_cast(cursor2) ) + SetMultiFont(true); for ( i=cursor1 ; i m_format; // format characters + std::vector m_format; // format characters int m_len; // length used in m_text int m_cursor1; // offset cursor int m_cursor2; // offset cursor -- cgit v1.2.3-1-g7c22