Skip to content

Commit

Permalink
remove DigitalBitHeight in favor of ImPlotSpec::Size; rename DigitalB…
Browse files Browse the repository at this point in the history
…itGap to DigitalSpacing
  • Loading branch information
epezent committed Oct 2, 2023
1 parent a26d812 commit 041cf53
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 49 deletions.
24 changes: 10 additions & 14 deletions implot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ ImPlotInputMap::ImPlotInputMap() {
ImPlot::MapInputDefault(this);
}

ImPlotStyle::ImPlotStyle() {
ImPlotStyle::ImPlotStyle() {
PlotDefaultSize = ImVec2(400,300);
PlotMinSize = ImVec2(200,150);
PlotBorderSize = 1;
MinorAlpha = 0.25f;
MajorTickLen = ImVec2(10,10);
Expand All @@ -190,10 +192,7 @@ ImPlotStyle::ImPlotStyle() {
MousePosPadding = ImVec2(10,10);
AnnotationPadding = ImVec2(2,2);
FitPadding = ImVec2(0,0);
PlotDefaultSize = ImVec2(400,300);
PlotMinSize = ImVec2(200,150);
DigitalBitHeight = 8;
DigitalBitGap = 4;
DigitalSpacing = 4;

ImPlot::StyleColorsAuto(this);

Expand Down Expand Up @@ -282,6 +281,8 @@ struct ImPlotStyleVarInfo {

static const ImPlotStyleVarInfo GPlotStyleVarInfo[] =
{
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, PlotDefaultSize) }, // ImPlotStyleVar_PlotDefaultSize
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, PlotMinSize) }, // ImPlotStyleVar_PlotMinSize
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, PlotBorderSize) }, // ImPlotStyleVar_PlotBorderSize
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, MinorAlpha) }, // ImPlotStyleVar_MinorAlpha
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, MajorTickLen) }, // ImPlotStyleVar_MajorTickLen
Expand All @@ -298,10 +299,7 @@ static const ImPlotStyleVarInfo GPlotStyleVarInfo[] =
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, MousePosPadding) }, // ImPlotStyleVar_MousePosPadding
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, AnnotationPadding) }, // ImPlotStyleVar_AnnotationPadding
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, FitPadding) }, // ImPlotStyleVar_FitPadding
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, PlotDefaultSize) }, // ImPlotStyleVar_PlotDefaultSize
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, PlotMinSize) }, // ImPlotStyleVar_PlotMinSize
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, DigitalBitHeight) }, // ImPlotStyleVar_DigitalBitHeight
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, DigitalBitGap) }, // ImPlotStyleVar_DigitalBitGap
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, DigitalSpacing) }, // ImPlotStyleVar_DigitalBitGap
};

static const ImPlotStyleVarInfo* GetPlotStyleVarInfo(ImPlotStyleVar idx) {
Expand Down Expand Up @@ -4937,6 +4935,8 @@ void ShowStyleEditor(ImPlotStyle* ref) {
if (ImGui::BeginTabBar("##StyleEditor")) {
if (ImGui::BeginTabItem("Variables")) {
ImGui::Text("Plot Styling");
ImGui::SliderFloat2("PlotDefaultSize", (float*)&style.PlotDefaultSize, 0.0f, 1000, "%.0f");
ImGui::SliderFloat2("PlotMinSize", (float*)&style.PlotMinSize, 0.0f, 300, "%.0f");
ImGui::SliderFloat("PlotBorderSize", &style.PlotBorderSize, 0.0f, 2.0f, "%.0f");
ImGui::SliderFloat("MinorAlpha", &style.MinorAlpha, 0.0f, 1.0f, "%.2f");
ImGui::SliderFloat2("MajorTickLen", (float*)&style.MajorTickLen, 0.0f, 20.0f, "%.0f");
Expand All @@ -4945,8 +4945,6 @@ void ShowStyleEditor(ImPlotStyle* ref) {
ImGui::SliderFloat2("MinorTickSize", (float*)&style.MinorTickSize, 0.0f, 2.0f, "%.1f");
ImGui::SliderFloat2("MajorGridSize", (float*)&style.MajorGridSize, 0.0f, 2.0f, "%.1f");
ImGui::SliderFloat2("MinorGridSize", (float*)&style.MinorGridSize, 0.0f, 2.0f, "%.1f");
ImGui::SliderFloat2("PlotDefaultSize", (float*)&style.PlotDefaultSize, 0.0f, 1000, "%.0f");
ImGui::SliderFloat2("PlotMinSize", (float*)&style.PlotMinSize, 0.0f, 300, "%.0f");
ImGui::Text("Plot Padding");
ImGui::SliderFloat2("PlotPadding", (float*)&style.PlotPadding, 0.0f, 20.0f, "%.0f");
ImGui::SliderFloat2("LabelPadding", (float*)&style.LabelPadding, 0.0f, 20.0f, "%.0f");
Expand All @@ -4956,9 +4954,7 @@ void ShowStyleEditor(ImPlotStyle* ref) {
ImGui::SliderFloat2("MousePosPadding", (float*)&style.MousePosPadding, 0.0f, 20.0f, "%.0f");
ImGui::SliderFloat2("AnnotationPadding", (float*)&style.AnnotationPadding, 0.0f, 5.0f, "%.0f");
ImGui::SliderFloat2("FitPadding", (float*)&style.FitPadding, 0, 0.2f, "%.2f");
ImGui::Text("Miscellaneous");
ImGui::SliderFloat("DigitalBitHeight", &style.DigitalBitHeight, 0.0f, 20.0f, "%.1f");
ImGui::SliderFloat("DigitalBitGap", &style.DigitalBitGap, 0.0f, 20.0f, "%.1f");
ImGui::SliderFloat("DigitalSpacing", &style.DigitalSpacing, 0.0f, 20.0f, "%.1f");
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("Colors")) {
Expand Down
25 changes: 12 additions & 13 deletions implot.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@

#pragma once
#include "imgui.h"
#include "imgui_internal.h"

//-----------------------------------------------------------------------------
// [SECTION] Macros and Defines
Expand Down Expand Up @@ -376,6 +375,8 @@ enum ImPlotCol_ {

// Plot styling variables.
enum ImPlotStyleVar_ {
ImPlotStyleVar_PlotDefaultSize, // ImVec2, default size used when ImVec2(0,0) is passed to BeginPlot
ImPlotStyleVar_PlotMinSize, // ImVec2, minimum size plot frame can be when shrunk
ImPlotStyleVar_PlotBorderSize, // float, thickness of border around plot area
ImPlotStyleVar_MinorAlpha, // float, alpha multiplier applied to minor axis grid lines
ImPlotStyleVar_MajorTickLen, // ImVec2, major tick lengths for X and Y axes
Expand All @@ -392,10 +393,7 @@ enum ImPlotStyleVar_ {
ImPlotStyleVar_MousePosPadding, // ImVec2, padding between plot edge and interior info text
ImPlotStyleVar_AnnotationPadding, // ImVec2, text padding around annotation labels
ImPlotStyleVar_FitPadding, // ImVec2, additional fit padding as a percentage of the fit extents (e.g. ImVec2(0.1f,0.1f) adds 10% to the fit extents of X and Y)
ImPlotStyleVar_PlotDefaultSize, // ImVec2, default size used when ImVec2(0,0) is passed to BeginPlot
ImPlotStyleVar_PlotMinSize, // ImVec2, minimum size plot frame can be when shrunk
ImPlotStyleVar_DigitalBitHeight, // float, digital channels bit height (at 1) in pixels
ImPlotStyleVar_DigitalBitGap, // float, digital channels bit padding gap in pixels
ImPlotStyleVar_DigitalSpacing, // float, digital plots padding gap in pixels
ImPlotStyleVar_COUNT
};

Expand Down Expand Up @@ -473,7 +471,7 @@ struct ImPlotSpec {
ImVec4 FillColor = IMPLOT_AUTO_COL; // fill color (applies to bar faces and shaded regions); IMPLOT_AUTO_COL will use next Colormap color or current item color
float FillAlpha = 1.0f; // alpha multiplier (applies to FillColor)
ImPlotMarker Marker = ImPlotMarker_None; // marker type; specify ImPlotMarker_Auto to use the next unused marker
float Size = 4; // size of markers (~radius) and error bar whiskers (widget/height) in pixels
float Size = 4; // size of markers (radius), error bar whiskers (width or height), and digital bars (height) *in pixels*
int Offset = 0; // data index offset
int Stride = IMPLOT_AUTO; // data stride in bytes; IMPLOT_AUTO will result in sizeof(T) where T is the type passed to PlotX
ImPlotItemFlags Flags = ImPlotItemFlags_None; // optional item flags; can be composed from common ImPlotItemFlags and/or specific ImPlotXFlags where X corresponds
Expand Down Expand Up @@ -511,7 +509,7 @@ struct ImPlotSpec {
case ImProp_Flags : Flags = (ImPlotItemFlags)v; return;
default: break;
}
IM_ASSERT(0 && "User provided ImProp which cannot be set from scalar value!");
IM_ASSERT(0 && "User provided an ImProp which cannot be set from scalar value!");
}

// Set a property from an ImVec4 value.
Expand All @@ -521,7 +519,7 @@ struct ImPlotSpec {
case ImProp_FillColor : FillColor = v; return;
default: break;
}
IM_ASSERT(0 && "User provided ImProp which cannot be set from ImVec4 value!");
IM_ASSERT(0 && "User provided an ImProp which cannot be set from ImVec4 value!");
}
};

Expand Down Expand Up @@ -567,6 +565,9 @@ struct ImPlotRect {

// Plot style structure
struct ImPlotStyle {
// plot styling
ImVec2 PlotDefaultSize; // = 400,300 default size used when ImVec2(0,0) is passed to BeginPlot
ImVec2 PlotMinSize; // = 200,150 minimum size plot frame can be when shrunk
float PlotBorderSize; // = 1, line thickness of border around plot area
float MinorAlpha; // = 0.25 alpha multiplier applied to minor axis grid lines
ImVec2 MajorTickLen; // = 10,10 major tick lengths for X and Y axes
Expand All @@ -575,6 +576,7 @@ struct ImPlotStyle {
ImVec2 MinorTickSize; // = 1,1 line thickness of minor ticks
ImVec2 MajorGridSize; // = 1,1 line thickness of major grid lines
ImVec2 MinorGridSize; // = 1,1 line thickness of minor grid lines
// plot padding
ImVec2 PlotPadding; // = 10,10 padding between widget frame and plot area, labels, or outside legends (i.e. main padding)
ImVec2 LabelPadding; // = 5,5 padding between axes labels, tick labels, and plot edge
ImVec2 LegendPadding; // = 10,10 legend padding from plot edges
Expand All @@ -583,10 +585,7 @@ struct ImPlotStyle {
ImVec2 MousePosPadding; // = 10,10 padding between plot edge and interior mouse location text
ImVec2 AnnotationPadding; // = 2,2 text padding around annotation labels
ImVec2 FitPadding; // = 0,0 additional fit padding as a percentage of the fit extents (e.g. ImVec2(0.1f,0.1f) adds 10% to the fit extents of X and Y)
ImVec2 PlotDefaultSize; // = 400,300 default size used when ImVec2(0,0) is passed to BeginPlot
ImVec2 PlotMinSize; // = 200,150 minimum size plot frame can be when shrunk
float DigitalBitHeight; // = 8, digital channels bit height (at y = 1.0f) in pixels
float DigitalBitGap; // = 4, digital channels bit padding gap in pixels
float DigitalSpacing; // = 4, digital plot padding gap in pixels
// style colors
ImVec4 Colors[ImPlotCol_COUNT]; // Array of styling colors. Indexable with ImPlotCol_ enums.
// colormap
Expand Down Expand Up @@ -749,7 +748,7 @@ IMPLOT_API bool BeginSubplots(const char* title_id,
float* col_ratios = nullptr);

// Only call EndSubplots() if BeginSubplots() returns true! Typically called at the end
// of an if statement conditioned on BeginSublots(). See example above.
// of an if statement conditioned on BeginSubplots(). See example above.
IMPLOT_API void EndSubplots();

//-----------------------------------------------------------------------------
Expand Down
24 changes: 14 additions & 10 deletions implot_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -817,14 +817,16 @@ void Demo_DigitalPlots() {
ImGui::Unindent();

static bool paused = false;
static ScrollingBuffer dataDigital[2];
static ScrollingBuffer dataDigital[3];
static ScrollingBuffer dataAnalog[2];
static bool showDigital[2] = {true, false};
static bool showDigital[3] = {true, false, false};
static bool showAnalog[2] = {true, false};

char label[32];
ImGui::Checkbox("Pause", &paused);
ImGui::Checkbox("digital_0", &showDigital[0]); ImGui::SameLine();
ImGui::Checkbox("digital_1", &showDigital[1]); ImGui::SameLine();
ImGui::Checkbox("digital_2", &showDigital[2]); ImGui::SameLine();
ImGui::Checkbox("analog_0", &showAnalog[0]); ImGui::SameLine();
ImGui::Checkbox("analog_1", &showAnalog[1]);

Expand All @@ -836,6 +838,8 @@ void Demo_DigitalPlots() {
dataDigital[0].AddPoint(t, sinf(2*t) > 0.45);
if (showDigital[1])
dataDigital[1].AddPoint(t, sinf(2*t) < 0.45);
if (showDigital[2])
dataDigital[2].AddPoint(t, sinf(50*t) > 0.5);
//Analog signal values
if (showAnalog[0])
dataAnalog[0].AddPoint(t, sinf(2*t));
Expand All @@ -845,12 +849,13 @@ void Demo_DigitalPlots() {
if (ImPlot::BeginPlot("##Digital")) {
ImPlot::SetupAxisLimits(ImAxis_X1, t - 10.0, t, paused ? ImGuiCond_Once : ImGuiCond_Always);
ImPlot::SetupAxisLimits(ImAxis_Y1, -1, 1);
for (int i = 0; i < 2; ++i) {
for (int i = 0; i < 3; ++i) {
if (showDigital[i] && dataDigital[i].Data.size() > 0) {
snprintf(label, sizeof(label), "digital_%d", i);
ImPlot::PlotDigital(label, &dataDigital[i].Data[0].x, &dataDigital[i].Data[0].y, dataDigital[i].Data.size(), {
ImProp_Offset, dataDigital[i].Offset,
ImProp_Stride, 2 * sizeof(float)
ImProp_Stride, 2 * sizeof(float),
ImProp_Size, (i+1) * 4
});
}
}
Expand Down Expand Up @@ -2420,6 +2425,8 @@ void StyleSeaborn() {
colors[ImPlotCol_Selection] = ImVec4(1.00f, 0.65f, 0.00f, 1.00f);
colors[ImPlotCol_Crosshairs] = ImVec4(0.23f, 0.10f, 0.64f, 0.50f);

style.MousePosPadding = ImVec2(5,5);
style.PlotMinSize = ImVec2(300,225);
style.PlotBorderSize = 0;
style.MinorAlpha = 1.0f;
style.MajorTickLen = ImVec2(0,0);
Expand All @@ -2431,17 +2438,14 @@ void StyleSeaborn() {
style.PlotPadding = ImVec2(12,12);
style.LabelPadding = ImVec2(5,5);
style.LegendPadding = ImVec2(5,5);
style.MousePosPadding = ImVec2(5,5);
style.PlotMinSize = ImVec2(300,225);
style.DigitalBitHeight = 8;
style.DigitalBitGap = 4;
style.DigitalSpacing = 4;
}

} // namespaece MyImPlot
} // namespace MyImPlot

// WARNING:
//
// You can use "implot_internal.h" to build custom plotting fuctions or extend ImPlot.
// You can use "implot_internal.h" to build custom plotting functions or extend ImPlot.
// However, note that forward compatibility of this file is not guaranteed and the
// internal API is subject to change. At some point we hope to bring more of this
// into the public API and expose the necessary building blocks to fully support
Expand Down
2 changes: 0 additions & 2 deletions implot_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1193,8 +1193,6 @@ struct ImPlotNextPlotData
// Temporary data storage for upcoming item
struct ImPlotNextItemData {
ImPlotSpec Spec;
float DigitalBitHeight; // TODO: remove
float DigitalBitGap; // TODO: remove
bool RenderLine;
bool RenderFill;
bool RenderMarkers;
Expand Down
20 changes: 10 additions & 10 deletions implot_items.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,6 @@ bool BeginItem(const char* label_id, const ImPlotSpec& spec, const ImVec4& item_
s.Spec.FillColor = IsColorAuto(s.Spec.FillColor) ? s.Spec.LineColor : s.Spec.FillColor;
s.Spec.FillColor.w *= s.Spec.FillAlpha;
s.Spec.Marker = item->Marker;
// SPEC-TODO: remove
s.DigitalBitHeight = gp.Style.DigitalBitHeight;
s.DigitalBitGap = gp.Style.DigitalBitGap;
// apply highlight mods
if (item->LegendHovered) {
if (!ImHasFlag(gp.CurrentItems->Legend.Flags, ImPlotLegendFlags_NoHighlightItem)) {
Expand Down Expand Up @@ -2660,6 +2657,7 @@ CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
//-----------------------------------------------------------------------------

// TODO: Make this behave like all the other plot types (.e. not fixed in y axis)
// TODO: Currently broken if x or y axis is inverted! (what should happen in this case, anyway?)

template <typename Getter>
void PlotDigitalEx(const char* label_id, Getter getter, const ImPlotSpec& spec) {
Expand All @@ -2680,18 +2678,20 @@ void PlotDigitalEx(const char* label_id, Getter getter, const ImPlotSpec& spec)
itemData1 = itemData2;
continue;
}
if (ImNanOrInf(itemData2.y)) itemData2.y = ImConstrainNan(ImConstrainInf(itemData2.y));
if (ImNanOrInf(itemData2.y)) {
itemData2.y = ImConstrainNan(ImConstrainInf(itemData2.y));
}
int pixY_0 = (int)(s.Spec.LineWeight);
itemData1.y = ImMax(0.0, itemData1.y);
float pixY_1_float = s.DigitalBitHeight * (float)itemData1.y;
float pixY_1_float = s.Spec.Size * (float)itemData1.y;
int pixY_1 = (int)(pixY_1_float); //allow only positive values
int pixY_chPosOffset = (int)(ImMax(s.DigitalBitHeight, pixY_1_float) + s.DigitalBitGap);
int pixY_chPosOffset = (int)(ImMax(s.Spec.Size, pixY_1_float) + gp.Style.DigitalSpacing);
pixYMax = ImMax(pixYMax, pixY_chPosOffset);
ImVec2 pMin = PlotToPixels(itemData1,IMPLOT_AUTO,IMPLOT_AUTO);
ImVec2 pMax = PlotToPixels(itemData2,IMPLOT_AUTO,IMPLOT_AUTO);
int pixY_Offset = 0; //20 pixel from bottom due to mouse cursor label
pMin.y = (y_axis.PixelMin) + ((-gp.DigitalPlotOffset) - pixY_Offset);
pMax.y = (y_axis.PixelMin) + ((-gp.DigitalPlotOffset) - pixY_0 - pixY_1 - pixY_Offset);
int pixY_Offset = 0; // TODO: previously 20 to accomodate plot mouse cursor label position; add another padding variable?
pMin.y = y_axis.PixelMin + (-gp.DigitalPlotOffset - pixY_Offset);
pMax.y = y_axis.PixelMin + (-gp.DigitalPlotOffset - pixY_0 - pixY_1 - pixY_Offset);
//plot only one rectangle for same digital state
while (((i+2) < getter.Count) && (itemData1.y == itemData2.y)) {
const int in = (i + 1);
Expand All @@ -2700,7 +2700,7 @@ void PlotDigitalEx(const char* label_id, Getter getter, const ImPlotSpec& spec)
pMax.x = PlotToPixels(itemData2,IMPLOT_AUTO,IMPLOT_AUTO).x;
i++;
}
//do not extend plot outside plot range
// do not extend plot outside plot range
if (pMin.x < x_axis.PixelMin) pMin.x = x_axis.PixelMin;
if (pMax.x < x_axis.PixelMin) pMax.x = x_axis.PixelMin;
if (pMin.x > x_axis.PixelMax) pMin.x = x_axis.PixelMax - 1; //fix issue related to https://github.com/ocornut/imgui/issues/3976
Expand Down

0 comments on commit 041cf53

Please sign in to comment.