1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2024-11-13 22:48:50 -05:00

add HideNextItem

This commit is contained in:
epezent 2020-09-15 09:48:46 -05:00
parent f348c15fde
commit 04cc84ece0
4 changed files with 85 additions and 62 deletions

View File

@ -353,7 +353,7 @@ void Reset(ImPlotContext* ctx) {
ctx->ChildWindowMade = false;
// reset the next plot/item data
ctx->NextPlotData = ImPlotNextPlotData();
ctx->NextItemStyle = ImPlotItemStyle();
ctx->NextItemData = ImPlotNextItemData();
// reset items count
ctx->VisibleItemCount = 0;
// reset legend items

View File

@ -430,6 +430,9 @@ IMPLOT_API ImPlotPoint PixelsToPlot(float x, float y, int y_axis = IMPLOT_AUTO);
IMPLOT_API ImVec2 PlotToPixels(const ImPlotPoint& plt, int y_axis = IMPLOT_AUTO);
IMPLOT_API ImVec2 PlotToPixels(double x, double y, int y_axis = IMPLOT_AUTO);
// Hides or shows the next plot item (i.e. as if it were toggled from the legend). Use ImGuiCond_Always if you need to change this every frame.
IMPLOT_API void HideNextItem(bool hidden = true, ImGuiCond cond = ImGuiCond_Once);
//-----------------------------------------------------------------------------
// Plot Queries
//-----------------------------------------------------------------------------

View File

@ -510,7 +510,7 @@ struct ImPlotNextPlotData
};
// Temporary data storage for upcoming item
struct ImPlotItemStyle {
struct ImPlotNextItemData {
ImVec4 Colors[5]; // ImPlotCol_Line, ImPlotCol_Fill, ImPlotCol_MarkerOutline, ImPlotCol_MarkerFill, ImPlotCol_ErrorBar
float LineWeight;
ImPlotMarker Marker;
@ -525,11 +525,15 @@ struct ImPlotItemStyle {
bool RenderFill;
bool RenderMarkerLine;
bool RenderMarkerFill;
ImPlotItemStyle() {
bool HasHidden;
bool Hidden;
ImGuiCond HiddenCond;
ImPlotNextItemData() {
for (int i = 0; i < 5; ++i)
Colors[i] = IMPLOT_AUTO_COL;
LineWeight = MarkerSize = MarkerWeight = FillAlpha = ErrorBarSize = ErrorBarWeight = DigitalBitHeight = DigitalBitGap = IMPLOT_AUTO;
Marker = IMPLOT_AUTO;
LineWeight = MarkerSize = MarkerWeight = FillAlpha = ErrorBarSize = ErrorBarWeight = DigitalBitHeight = DigitalBitGap = IMPLOT_AUTO;
Marker = IMPLOT_AUTO;
HasHidden = Hidden = false;
}
};
@ -603,7 +607,7 @@ struct ImPlotContext {
int DigitalPlotItemCnt;
int DigitalPlotOffset;
ImPlotNextPlotData NextPlotData;
ImPlotItemStyle NextItemStyle;
ImPlotNextItemData NextItemData;
ImPlotInputMap InputMap;
ImPlotPoint MousePos[IMPLOT_Y_AXES];
};
@ -659,7 +663,7 @@ IMPLOT_API bool BeginItem(const char* label_id, ImPlotCol recolor_from = -1);
IMPLOT_API void EndItem();
// Register or get an existing item from the current plot
IMPLOT_API ImPlotItem* RegisterOrGetItem(const char* label_id);
IMPLOT_API ImPlotItem* RegisterOrGetItem(const char* label_id, bool* just_created = NULL);
// Get the ith plot item from the current plot
IMPLOT_API ImPlotItem* GetItem(int i);
// Get a plot item from the current plot
@ -736,7 +740,7 @@ IMPLOT_API void AddTicksCustom(const double* values, const char* const labels[],
//-----------------------------------------------------------------------------
// Get styling data for next item (call between Begin/EndItem)
inline const ImPlotItemStyle& GetItemStyle() { return GImPlot->NextItemStyle; }
inline const ImPlotNextItemData& GetItemData() { return GImPlot->NextItemData; }
// Returns true if a color is set to be automatically determined
inline bool IsColorAuto(const ImVec4& col) { return col.w == -1; }

View File

@ -48,9 +48,11 @@ namespace ImPlot {
// Item Utils
//-----------------------------------------------------------------------------
ImPlotItem* RegisterOrGetItem(const char* label_id) {
ImPlotItem* RegisterOrGetItem(const char* label_id, bool* just_created) {
ImPlotContext& gp = *GImPlot;
ImGuiID id = ImGui::GetID(label_id);
if (just_created != NULL)
*just_created = gp.CurrentPlot->Items.GetByKey(id) == NULL;
ImPlotItem* item = gp.CurrentPlot->Items.GetOrAddByKey(id);
if (item->SeenThisFrame)
return item;
@ -97,30 +99,37 @@ ImPlotItem* GetCurrentItem() {
void SetNextLineStyle(const ImVec4& col, float weight) {
ImPlotContext& gp = *GImPlot;
gp.NextItemStyle.Colors[ImPlotCol_Line] = col;
gp.NextItemStyle.LineWeight = weight;
gp.NextItemData.Colors[ImPlotCol_Line] = col;
gp.NextItemData.LineWeight = weight;
}
void SetNextFillStyle(const ImVec4& col, float alpha) {
ImPlotContext& gp = *GImPlot;
gp.NextItemStyle.Colors[ImPlotCol_Fill] = col;
gp.NextItemStyle.FillAlpha = alpha;
gp.NextItemData.Colors[ImPlotCol_Fill] = col;
gp.NextItemData.FillAlpha = alpha;
}
void SetNextMarkerStyle(ImPlotMarker marker, float size, const ImVec4& fill, float weight, const ImVec4& outline) {
ImPlotContext& gp = *GImPlot;
gp.NextItemStyle.Marker = marker;
gp.NextItemStyle.Colors[ImPlotCol_MarkerFill] = fill;
gp.NextItemStyle.MarkerSize = size;
gp.NextItemStyle.Colors[ImPlotCol_MarkerOutline] = outline;
gp.NextItemStyle.MarkerWeight = weight;
gp.NextItemData.Marker = marker;
gp.NextItemData.Colors[ImPlotCol_MarkerFill] = fill;
gp.NextItemData.MarkerSize = size;
gp.NextItemData.Colors[ImPlotCol_MarkerOutline] = outline;
gp.NextItemData.MarkerWeight = weight;
}
void SetNextErrorBarStyle(const ImVec4& col, float size, float weight) {
ImPlotContext& gp = *GImPlot;
gp.NextItemStyle.Colors[ImPlotCol_ErrorBar] = col;
gp.NextItemStyle.ErrorBarSize = size;
gp.NextItemStyle.ErrorBarWeight = weight;
gp.NextItemData.Colors[ImPlotCol_ErrorBar] = col;
gp.NextItemData.ErrorBarSize = size;
gp.NextItemData.ErrorBarWeight = weight;
}
void HideNextItem(bool hidden, ImGuiCond cond) {
ImPlotContext& gp = *GImPlot;
gp.NextItemData.HasHidden = true;
gp.NextItemData.Hidden = hidden;
gp.NextItemData.HiddenCond = cond;
}
void BustItemCache() {
@ -140,16 +149,24 @@ void BustItemCache() {
bool BeginItem(const char* label_id, ImPlotCol recolor_from) {
ImPlotContext& gp = *GImPlot;
IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "PlotX() needs to be called between BeginPlot() and EndPlot()!");
ImPlotItem* item = RegisterOrGetItem(label_id);
bool just_created;
ImPlotItem* item = RegisterOrGetItem(label_id, &just_created);
// hide/show item
if (gp.NextItemData.HasHidden) {
if (just_created || gp.NextItemData.HiddenCond == ImGuiCond_Always)
item->Show = !gp.NextItemData.Hidden;
}
if (!item->Show) {
// reset next item data
gp.NextItemStyle = ImPlotItemStyle();
gp.NextItemData = ImPlotNextItemData();
return false;
}
else {
// set current item
gp.CurrentItem = item;
ImPlotItemStyle& s = gp.NextItemStyle;
ImPlotNextItemData& s = gp.NextItemData;
// override item color
if (recolor_from != -1) {
if (!IsColorAuto(s.Colors[recolor_from]))
@ -199,7 +216,7 @@ void EndItem() {
// pop rendering clip rect
PopPlotClipRect();
// reset next item data
gp.NextItemStyle = ImPlotItemStyle();
gp.NextItemData = ImPlotNextItemData();
// set current item
gp.CurrentItem = NULL;
}
@ -346,7 +363,6 @@ struct GetterError {
const int Count;
const int Offset;
const int Stride;
};
//-----------------------------------------------------------------------------
@ -807,7 +823,7 @@ inline void PlotLineEx(const char* label_id, const Getter& getter) {
FitPoint(p);
}
}
const ImPlotItemStyle& s = GetItemStyle();
const ImPlotNextItemData& s = GetItemData();
ImDrawList& DrawList = *GetPlotDrawList();
if (getter.Count > 1 && s.RenderLine) {
const ImU32 col_line = ImGui::GetColorU32(s.Colors[ImPlotCol_Line]);
@ -886,7 +902,7 @@ inline void PlotScatterEx(const char* label_id, const Getter& getter) {
FitPoint(p);
}
}
const ImPlotItemStyle& s = GetItemStyle();
const ImPlotNextItemData& s = GetItemData();
ImDrawList& DrawList = *GetPlotDrawList();
// render markers
ImPlotMarker marker = s.Marker == ImPlotMarker_None ? ImPlotMarker_Circle : s.Marker;
@ -959,7 +975,7 @@ inline void PlotShadedEx(const char* label_id, const Getter1& getter1, const Get
FitPoint(p2);
}
}
const ImPlotItemStyle& s = GetItemStyle();
const ImPlotNextItemData& s = GetItemData();
ImDrawList & DrawList = *GetPlotDrawList();
if (s.RenderFill) {
ImU32 col = ImGui::GetColorU32(s.Colors[ImPlotCol_Fill]);
@ -1052,7 +1068,7 @@ void PlotBarsEx(const char* label_id, const Getter& getter, double width) {
FitPoint(ImPlotPoint(p.x + half_width, 0));
}
}
const ImPlotItemStyle& s = GetItemStyle();
const ImPlotNextItemData& s = GetItemData();
ImDrawList& DrawList = *GetPlotDrawList();
ImU32 col_line = ImGui::GetColorU32(s.Colors[ImPlotCol_Line]);
ImU32 col_fill = ImGui::GetColorU32(s.Colors[ImPlotCol_Fill]);
@ -1131,7 +1147,7 @@ void PlotBarsHEx(const char* label_id, const Getter& getter, THeight height) {
FitPoint(ImPlotPoint(p.x, p.y + half_height));
}
}
const ImPlotItemStyle& s = GetItemStyle();
const ImPlotNextItemData& s = GetItemData();
ImDrawList& DrawList = *GetPlotDrawList();
ImU32 col_line = ImGui::GetColorU32(s.Colors[ImPlotCol_Line]);
ImU32 col_fill = ImGui::GetColorU32(s.Colors[ImPlotCol_Fill]);
@ -1207,7 +1223,7 @@ void PlotErrorBarsEx(const char* label_id, const Getter& getter) {
FitPoint(ImPlotPoint(e.X , e.Y + e.Pos ));
}
}
const ImPlotItemStyle& s = GetItemStyle();
const ImPlotNextItemData& s = GetItemData();
ImDrawList& DrawList = *GetPlotDrawList();
const ImU32 col = ImGui::GetColorU32(s.Colors[ImPlotCol_ErrorBar]);
const bool rend_whisker = s.ErrorBarSize > 0;
@ -1273,7 +1289,7 @@ void PlotErrorBarsHEx(const char* label_id, const Getter& getter) {
FitPoint(ImPlotPoint(e.X + e.Pos, e.Y));
}
}
const ImPlotItemStyle& s = GetItemStyle();
const ImPlotNextItemData& s = GetItemData();
ImDrawList& DrawList = *GetPlotDrawList();
const ImU32 col = ImGui::GetColorU32(s.Colors[ImPlotCol_ErrorBar]);
const bool rend_whisker = s.ErrorBarSize > 0;
@ -1339,7 +1355,7 @@ inline void PlotStemsEx(const char* label_id, const GetterM& get_mark, const Get
FitPoint(get_base(i));
}
}
const ImPlotItemStyle& s = GetItemStyle();
const ImPlotNextItemData& s = GetItemData();
ImDrawList& DrawList = *GetPlotDrawList();
// render stems
if (s.RenderLine) {
@ -1569,7 +1585,7 @@ inline void PlotDigitalEx(const char* label_id, Getter getter) {
if (BeginItem(label_id, ImPlotCol_Fill)) {
ImPlotContext& gp = *GImPlot;
ImDrawList& DrawList = *GetPlotDrawList();
const ImPlotItemStyle& s = GetItemStyle();
const ImPlotNextItemData& s = GetItemData();
if (getter.Count > 1 && s.RenderFill) {
const int y_axis = GetCurrentYAxis();
int pixYMax = 0;
@ -1656,7 +1672,7 @@ void PlotRectsEx(const char* label_id, const Getter& getter) {
FitPoint(p);
}
}
const ImPlotItemStyle& s = GetItemStyle();
const ImPlotNextItemData& s = GetItemData();
if (s.RenderFill) {
ImDrawList& DrawList = *GetPlotDrawList();
ImU32 col = ImGui::GetColorU32(s.Colors[ImPlotCol_Fill]);