Skip to content

Commit

Permalink
Don't update palette on every frame in text editor
Browse files Browse the repository at this point in the history
  • Loading branch information
goossens committed Sep 12, 2023
1 parent af100a5 commit 7573aeb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
17 changes: 14 additions & 3 deletions 3rdparty/imguieditor/TextEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const std::unordered_map<char, char> TextEditor::CLOSE_TO_OPEN_CHAR = {
{']' , '['}
};

TextEditor::Palette TextEditor::mDefaultPalette = TextEditor::GetDarkPalette();

// TODO
// - multiline comments vs single-line: latter is blocking start of a ML

Expand Down Expand Up @@ -58,7 +60,7 @@ TextEditor::TextEditor()
, mStartTime(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count())
, mLastClick(-1.0f)
{
SetPalette(GetDarkPalette());
SetPalette(mDefaultPalette);
mLines.push_back(Line());
}

Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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));
Expand Down
9 changes: 7 additions & 2 deletions 3rdparty/imguieditor/TextEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;

Expand All @@ -505,4 +508,6 @@ class IMGUI_API TextEditor
uint64_t mStartTime;

float mLastClick;

static bool IsGlyphWordChar(const Glyph& aGlyph);
};

0 comments on commit 7573aeb

Please sign in to comment.