1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2024-11-26 12:18:52 -05:00

Merge pull request #15 from ozlb/master

PlotDigital multi level
This commit is contained in:
Evan Pezent 2020-05-12 07:20:35 -05:00 committed by GitHub
commit 1e627d81ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 17 deletions

View File

@ -388,6 +388,7 @@ struct ImPlotContext {
ImNextPlotData NextPlotData;
// Digital plot item count
int DigitalPlotItemCnt;
int DigitalPlotOffset;
};
/// Global plot context
@ -1292,6 +1293,7 @@ bool BeginPlot(const char* title, const char* x_label, const char* y_label, cons
gp.LegendLabels.Buf.resize(0);
// reset digital plot items count
gp.DigitalPlotItemCnt = 0;
gp.DigitalPlotOffset = 0;
return true;
}
@ -1803,7 +1805,8 @@ static const ImPlotStyleVarInfo GPlotStyleVarInfo[] =
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, MarkerWeight) }, // ImPlotStyleVar_MarkerWeight
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, ErrorBarSize) }, // ImPlotStyleVar_ErrorBarSize
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, ErrorBarWeight) }, // ImPlotStyleVar_ErrorBarWeight
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, DigitalBitHeight) } // ImPlotStyleVar_DigitalBitHeight
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, DigitalBitHeight) }, // ImPlotStyleVar_DigitalBitHeight
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, DigitalBitGap) } // ImPlotStyleVar_DigitalBitGap
};
static const ImPlotStyleVarInfo* GetPlotStyleVarInfo(ImPlotStyleVar idx)
@ -2579,26 +2582,26 @@ inline void PlotDigitalEx(const char* label_id, Getter getter, int count, int of
if (count > 1 && rend_line) {
//
const float mx = (gp.PixelRange[ax].Max.x - gp.PixelRange[ax].Min.x) / gp.CurrentPlot->XAxis.Range.Size();
float pixY_0 = line_weight;
float pixY_1 = gp.Style.DigitalBitHeight;
float pixY_Offset = 20;//20 pixel from bottom due to mouse cursor label
float pixY_chOffset = pixY_1 + 3; //3 pixels between channels
ImVec2 pMin, pMax;
float y0 = (gp.PixelRange[ax].Min.y) + ((-pixY_chOffset * gp.DigitalPlotItemCnt) - pixY_0 - pixY_Offset);
float y1 = (gp.PixelRange[ax].Min.y) + ((-pixY_chOffset * gp.DigitalPlotItemCnt) - pixY_1 - pixY_Offset);
const int segments = count - 1;
int i1 = offset;
int pixYMax = 0;
for (int s = 0; s < segments; ++s) {
const int i2 = (i1 + 1) % count;
ImVec2 itemData1 = getter(i1);
ImVec2 itemData2 = getter(i2);
i1 = i2;
int pixY_0 = line_weight;
int pixY_1 = gp.Style.DigitalBitHeight * ImMax(0.0f, itemData1.y); //allow only positive values
int pixY_chPosOffset = ImMax((int)gp.Style.DigitalBitHeight, pixY_1) + gp.Style.DigitalBitGap;
pixYMax = ImMax(pixYMax, pixY_chPosOffset);
ImVec2 pMin, pMax;
pMin.x = gp.PixelRange[ax].Min.x + mx * (itemData1.x - gp.CurrentPlot->XAxis.Range.Min);
pMin.y = (gp.PixelRange[ax].Min.y) + ((-pixY_chOffset * gp.DigitalPlotItemCnt) - pixY_Offset);
pMax.x = gp.PixelRange[ax].Min.x + mx * (itemData2.x - gp.CurrentPlot->XAxis.Range.Min);
pMax.y = ((int) itemData1.y == 0) ? y0 : y1;
int pixY_Offset = 20;//20 pixel from bottom due to mouse cursor label
pMin.y = (gp.PixelRange[ax].Min.y) + ((-gp.DigitalPlotOffset) - pixY_Offset);
pMax.y = (gp.PixelRange[ax].Min.y) + ((-gp.DigitalPlotOffset) - pixY_0 - pixY_1 - pixY_Offset);
//plot only one rectangle for same digital state
while (((s+2) < segments) && ((int) itemData1.y == (int) itemData2.y)) {
while (((s+2) < segments) && (itemData1.y == itemData2.y)) {
const int i2 = (i1 + 1) % count;
itemData2 = getter(i2);
pMax.x = gp.PixelRange[ax].Min.x + mx * (itemData2.x - gp.CurrentPlot->XAxis.Range.Min);
@ -2618,6 +2621,7 @@ inline void PlotDigitalEx(const char* label_id, Getter getter, int count, int of
}
}
gp.DigitalPlotItemCnt++;
gp.DigitalPlotOffset += pixYMax;
}
ImGui::PopClipRect();

View File

@ -94,7 +94,8 @@ enum ImPlotStyleVar_ {
ImPlotStyleVar_MarkerWeight, // float, outline weight of markers in pixels
ImPlotStyleVar_ErrorBarSize, // float, error bar whisker width in pixels
ImPlotStyleVar_ErrorBarWeight, // float, error bar whisker weight in pixels
ImPlotStyleVar_DigitalBitHeight, // float, digital channels bit height (at 1)
ImPlotStyleVar_DigitalBitHeight, // float, digital channels bit height (at 1) in pixels
ImPlotStyleVar_DigitalBitGap, // float, digital channels bit padding gap in pixels
ImPlotStyleVar_COUNT
};
@ -137,7 +138,8 @@ struct ImPlotStyle {
float MarkerWeight; // = 1, outline weight of markers in pixels
float ErrorBarSize; // = 5, error bar whisker width in pixels
float ErrorBarWeight; // = 1.5, error bar whisker weight in pixels
float DigitalBitHeight; // = 8, digital channels bit height (at y = 1.0f)
float DigitalBitHeight; // = 8, digital channels bit height (at y = 1.0f) in pixels
float DigitalBitGap; // = 4, digital channels bit padding gap in pixels
ImVec4 Colors[ImPlotCol_COUNT]; // array of plot specific colors
ImPlotStyle();
};

View File

@ -602,7 +602,6 @@ void ShowDemoWindow(bool* p_open) {
ImGui::Indent();
ImGui::Text("you can drag analog signals over the rising/falling digital edge.");
ImGui::Unindent();
static float bitHeight = 8;
ImGui::BeginGroup();
if (ImGui::Button("Clear", {100, 0})) {
for (int i = 0; i < K_PLOT_DIGITAL_CH_COUNT; ++i)
@ -613,7 +612,11 @@ void ShowDemoWindow(bool* p_open) {
if (ImGui::Button(paused ? "Resume" : "Pause", {100,0}))
paused = !paused;
ImGui::SetNextItemWidth(100);
static float bitHeight = 8;
ImGui::DragFloat("##Bit Height", &bitHeight, 1, 5, 25, "%.0f px");
ImGui::SetNextItemWidth(100);
static float bitGap = 4;
ImGui::DragFloat("##Bit Gap", &bitGap, 1, 2, 20, "%.0f px");
ImGui::Separator();
for (int i = 0; i < K_PLOT_DIGITAL_CH_COUNT; ++i) {
char label[32];
@ -649,7 +652,7 @@ void ShowDemoWindow(bool* p_open) {
dataDigital[i].AddPoint(t, sin(2*t) < 0.45);
i++;
if (showDigital[i])
dataDigital[i].AddPoint(t, sin(2*t) > 0.83);
dataDigital[i].AddPoint(t, (int)t % 5);
i++;
if (showDigital[i])
dataDigital[i].AddPoint(t, sin(2*t) < 0.17);
@ -675,8 +678,9 @@ void ShowDemoWindow(bool* p_open) {
char label[32];
sprintf(label, "digital_%d", i);
ImPlot::PushStyleVar(ImPlotStyleVar_DigitalBitHeight, bitHeight);
ImPlot::PushStyleVar(ImPlotStyleVar_DigitalBitGap, bitGap);
ImPlot::Digital(label, &dataDigital[i].Data[0].x, &dataDigital[i].Data[0].y, dataDigital[i].Data.size(), dataDigital[i].Offset, 2 * sizeof(float));
ImPlot::PopStyleVar();
ImPlot::PopStyleVar(2);
}
}
for (int i = 0; i < K_PLOT_ANALOG_CH_COUNT; ++i) {
@ -767,4 +771,4 @@ void ShowDemoWindow(bool* p_open) {
ImGui::End();
}
} // namespace ImGui
} // namespace ImPlot