From 7573aeb985da6ef097911b4ccd6c1dd46c6e13c5 Mon Sep 17 00:00:00 2001 From: Johan Goossens Date: Tue, 12 Sep 2023 09:43:27 -0400 Subject: [PATCH] Don't update palette on every frame in text editor --- 3rdparty/imguieditor/TextEditor.cpp | 17 ++++++++++++++--- 3rdparty/imguieditor/TextEditor.h | 9 +++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/3rdparty/imguieditor/TextEditor.cpp b/3rdparty/imguieditor/TextEditor.cpp index 567059f0..d9771076 100644 --- a/3rdparty/imguieditor/TextEditor.cpp +++ b/3rdparty/imguieditor/TextEditor.cpp @@ -20,6 +20,8 @@ const std::unordered_map TextEditor::CLOSE_TO_OPEN_CHAR = { {']' , '['} }; +TextEditor::Palette TextEditor::mDefaultPalette = TextEditor::GetDarkPalette(); + // TODO // - multiline comments vs single-line: latter is blocking start of a ML @@ -58,7 +60,7 @@ TextEditor::TextEditor() , mStartTime(std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count()) , mLastClick(-1.0f) { - SetPalette(GetDarkPalette()); + SetPalette(mDefaultPalette); mLines.push_back(Line()); } @@ -82,9 +84,15 @@ const char* TextEditor::GetLanguageDefinitionName() const return mLanguageDefinition != nullptr ? mLanguageDefinition->mName.c_str() : "unknown"; } +void TextEditor::SetDefaultPalette(const Palette& aValue) +{ + mDefaultPalette = aValue; +} + void TextEditor::SetPalette(const Palette& aValue) { mPaletteBase = aValue; + UpdatePalette(); } std::string TextEditor::GetText(const Coordinates& aStart, const Coordinates& aEnd) const @@ -1056,10 +1064,12 @@ void TextEditor::HandleMouseInputs() void TextEditor::UpdatePalette() { /* Update palette with the current alpha from style */ + mPaletteAlpha = ImGui::GetStyle().Alpha; + for (int i = 0; i < (int)PaletteIndex::Max; ++i) { auto color = U32ColorToVec4(mPaletteBase[i]); - color.w *= ImGui::GetStyle().Alpha; + color.w *= mPaletteAlpha; mPalette[i] = ImGui::ColorConvertFloat4ToU32(color); } } @@ -1483,7 +1493,8 @@ bool TextEditor::Render(const char* aTitle, bool aParentIsFocused, const ImVec2& OnCursorPositionChanged(); mState.mCursorPositionChanged = false; - UpdatePalette(); + if (mPaletteAlpha != ImGui::GetStyle().Alpha) + UpdatePalette(); ImGui::PushStyleColor(ImGuiCol_ChildBg, ImGui::ColorConvertU32ToFloat4(mPalette[(int)PaletteIndex::Background])); ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, 0.0f)); diff --git a/3rdparty/imguieditor/TextEditor.h b/3rdparty/imguieditor/TextEditor.h index 2ce2e0e4..e3efaa44 100644 --- a/3rdparty/imguieditor/TextEditor.h +++ b/3rdparty/imguieditor/TextEditor.h @@ -253,6 +253,9 @@ class IMGUI_API TextEditor void SetLanguageDefinition(const LanguageDefinition& aLanguageDef); const char* GetLanguageDefinitionName() const; + const Palette& GetDefaultPalette() const { return mDefaultPalette; } + void SetDefaultPalette(const Palette& aValue); + const Palette& GetPalette() const { return mPaletteBase; } void SetPalette(const Palette& aValue); @@ -385,8 +388,6 @@ class IMGUI_API TextEditor static const Palette& GetLightPalette(); static const Palette& GetRetroBluePalette(); - static bool IsGlyphWordChar(const Glyph& aGlyph); - void ImGuiDebugPanel(const std::string& panelName = "Debug"); void UnitTests(); private: @@ -494,8 +495,10 @@ class IMGUI_API TextEditor bool mCompletePairedGlyphs; float mLongestLineLength; + static Palette mDefaultPalette; Palette mPaletteBase; Palette mPalette; + float mPaletteAlpha; const LanguageDefinition* mLanguageDefinition = nullptr; RegexList mRegexList; @@ -505,4 +508,6 @@ class IMGUI_API TextEditor uint64_t mStartTime; float mLastClick; + + static bool IsGlyphWordChar(const Glyph& aGlyph); };