Skip to content

Commit

Permalink
Add background color selector to glass plots
Browse files Browse the repository at this point in the history
  • Loading branch information
rzblue committed Aug 5, 2023
1 parent c52dad6 commit 4635111
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions glass/src/lib/native/cpp/other/Plot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ class Plot {
}
}

void SetColor(const ImVec4& color) { m_backgroundColor.SetColor(color); }

private:
void EmitSettingsLimits(int axis);
void DragDropAccept(PlotView& view, size_t i, int yAxis);
Expand All @@ -143,6 +145,9 @@ class Plot {

std::string& m_name;
bool& m_visible;
static constexpr float kDefaultBackgroundColor[4] = {0.0, 0.0, 0.0,
IMPLOT_AUTO};
ColorSetting m_backgroundColor;
bool& m_showPause;
bool& m_lockPrevX;
bool& m_legend;
Expand Down Expand Up @@ -484,6 +489,8 @@ Plot::Plot(Storage& storage)
: m_seriesStorage{storage.GetChildArray("series")},
m_name{storage.GetString("name")},
m_visible{storage.GetBool("visible", true)},
m_backgroundColor{
storage.GetFloatArray("backgroundColor", kDefaultBackgroundColor)},
m_showPause{storage.GetBool("showPause", true)},
m_lockPrevX{storage.GetBool("lockPrevX", false)},
m_legend{storage.GetBool("legend", true)},
Expand Down Expand Up @@ -580,6 +587,11 @@ void Plot::EmitPlot(PlotView& view, double now, bool paused, size_t i) {
(m_mousePosition ? 0 : ImPlotFlags_NoMouseText);

if (ImPlot::BeginPlot(label, ImVec2(-1, m_height), plotFlags)) {
if (m_backgroundColor.GetColorFloat()[3] == IMPLOT_AUTO) {
SetColor(ImGui::GetStyleColorVec4(ImGuiCol_WindowBg));
}
ImPlot::PushStyleColor(ImPlotCol_PlotBg, m_backgroundColor.GetColor());

// setup legend
if (m_legend) {
ImPlotLegendFlags legendFlags =
Expand Down Expand Up @@ -656,6 +668,8 @@ void Plot::EmitPlot(PlotView& view, double now, bool paused, size_t i) {
m_xaxisRange = ImPlot::GetPlotLimits().X;

ImPlotPlot* plot = ImPlot::GetCurrentPlot();

ImPlot::PopStyleColor();
ImPlot::EndPlot();

// copy plot settings back to storage
Expand Down Expand Up @@ -715,6 +729,12 @@ void Plot::EmitSettings(size_t i) {
ImGui::Text("Edit plot name:");
ImGui::InputText("##editname", &m_name);
ImGui::Checkbox("Visible", &m_visible);
m_backgroundColor.ColorEdit3("Background color",
ImGuiColorEditFlags_NoInputs);
ImGui::SameLine();
if (ImGui::Button("Default")) {
SetColor(ImGui::GetStyleColorVec4(ImGuiCol_WindowBg));
}
ImGui::Checkbox("Show Pause Button", &m_showPause);
if (i != 0) {
ImGui::Checkbox("Lock X-axis to previous plot", &m_lockPrevX);
Expand Down

0 comments on commit 4635111

Please sign in to comment.