From f8f02f9d4e3220f205dcd6112c039c40e00656ec Mon Sep 17 00:00:00 2001 From: BalazsJako Date: Fri, 7 Jun 2019 19:24:05 +0100 Subject: [PATCH] Removed static local variables. It might have been a problem when multiple instances were created. --- TextEditor.cpp | 28 +++++++++++++--------------- TextEditor.h | 2 ++ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/TextEditor.cpp b/TextEditor.cpp index 1f90db1f..53e55537 100644 --- a/TextEditor.cpp +++ b/TextEditor.cpp @@ -47,6 +47,7 @@ TextEditor::TextEditor() , mHandleMouseInputs(true) , mIgnoreImGuiChild(false) , mShowWhitespaces(true) + , mStartTime(std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count()) { SetPalette(GetDarkPalette()); SetLanguageDefinition(LanguageDefinition::HLSL()); @@ -863,8 +864,7 @@ void TextEditor::Render() mPalette[i] = ImGui::ColorConvertFloat4ToU32(color); } - static std::string buffer; - assert(buffer.empty()); + assert(mLineBuffer.empty()); auto contentSize = ImGui::GetWindowContentRegionMax(); auto drawList = ImGui::GetWindowDrawList(); @@ -975,10 +975,8 @@ void TextEditor::Render() // Render the cursor if (focused) { - static auto timeStart = std::chrono::system_clock::now(); - auto timeEnd = std::chrono::system_clock::now(); - auto diff = timeEnd - timeStart; - auto elapsed = std::chrono::duration_cast(diff).count(); + auto timeEnd = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); + auto elapsed = timeEnd - mStartTime; if (elapsed > 400) { float width = 1.0f; @@ -1005,7 +1003,7 @@ void TextEditor::Render() ImVec2 cend(textScreenPos.x + cx + width, lineStartScreenPos.y + mCharAdvance.y); drawList->AddRectFilled(cstart, cend, mPalette[(int)PaletteIndex::Cursor]); if (elapsed > 800) - timeStart = timeEnd; + mStartTime = timeEnd; } } } @@ -1019,13 +1017,13 @@ void TextEditor::Render() auto& glyph = line[i]; auto color = GetGlyphColor(glyph); - if ((color != prevColor || glyph.mChar == '\t' || glyph.mChar == ' ') && !buffer.empty()) + if ((color != prevColor || glyph.mChar == '\t' || glyph.mChar == ' ') && !mLineBuffer.empty()) { const ImVec2 newOffset(textScreenPos.x + bufferOffset.x, textScreenPos.y + bufferOffset.y); - drawList->AddText(newOffset, prevColor, buffer.c_str()); - auto textSize = ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, -1.0f, buffer.c_str(), nullptr, nullptr); + drawList->AddText(newOffset, prevColor, mLineBuffer.c_str()); + auto textSize = ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, -1.0f, mLineBuffer.c_str(), nullptr, nullptr); bufferOffset.x += textSize.x; - buffer.clear(); + mLineBuffer.clear(); } prevColor = color; @@ -1066,16 +1064,16 @@ void TextEditor::Render() { auto l = UTF8CharLength(glyph.mChar); while (l-- > 0) - buffer.push_back(line[i++].mChar); + mLineBuffer.push_back(line[i++].mChar); } ++columnNo; } - if (!buffer.empty()) + if (!mLineBuffer.empty()) { const ImVec2 newOffset(textScreenPos.x + bufferOffset.x, textScreenPos.y + bufferOffset.y); - drawList->AddText(newOffset, prevColor, buffer.c_str()); - buffer.clear(); + drawList->AddText(newOffset, prevColor, mLineBuffer.c_str()); + mLineBuffer.clear(); } ++lineNo; diff --git a/TextEditor.h b/TextEditor.h index 65436dec..bd52e131 100644 --- a/TextEditor.h +++ b/TextEditor.h @@ -382,6 +382,8 @@ class TextEditor ErrorMarkers mErrorMarkers; ImVec2 mCharAdvance; Coordinates mInteractiveStart, mInteractiveEnd; + std::string mLineBuffer; + uint64_t mStartTime; float mLastClick; };