diff --git a/include/imgui/imgui-multilines.hpp b/include/imgui/imgui-multilines.hpp index cb92723..3a82c7d 100644 --- a/include/imgui/imgui-multilines.hpp +++ b/include/imgui/imgui-multilines.hpp @@ -22,6 +22,95 @@ namespace ImGui { static ImVec2 subtract(const ImVec2& a, const ImVec2& b){ return ImVec2(a.x-b.x, a.y-b.y); } + + static void PlotMultiLines( + const std::vector> &data, + std::string title, + const std::vector &labels, + const std::vector &colors, + float maximum_scale, + float minimum_scale, + sf::Vector2f graph_size + ){ + + int values_count = data.at(0).size(); + + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return; + + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + + const ImVec2 label_size = ImGui::CalcTextSize(title.c_str(), NULL, true); + + if (graph_size.x == 0.0f) + graph_size.x = CalcItemWidth(); + if (graph_size.y == 0.0f) + graph_size.y = label_size.y + (style.FramePadding.y * 2); + + const ImRect frame_bb(window->DC.CursorPos, + add(window->DC.CursorPos, graph_size)); + const ImRect inner_bb(add(frame_bb.Min,style.FramePadding), + subtract(frame_bb.Max , style.FramePadding)); + ImVec2 thing = ImVec2(label_size.x > 0.0f ? + style.ItemInnerSpacing.x + label_size.x : 0.0f, + 0); + const ImRect total_bb(frame_bb.Min, add(frame_bb.Max , thing)); + + ItemSize(total_bb, style.FramePadding.y); + if (!ItemAdd(total_bb, 0)) + return; + + RenderFrame(frame_bb.Min, frame_bb.Max, GetColorU32(ImGuiCol_FrameBg), + true, style.FrameRounding); + + int res_w = ImMin((int) graph_size.x, values_count ); + int item_count = values_count; + int text_margin = 10; + + for (int data_idx = 0; data_idx < data.size(); ++data_idx) + { + const float t_step = 1.0f / (float) res_w; + + float v0 = data.at(data_idx).at(0); + float t0 = 0.0f; + ImVec2 tp0 = ImVec2(t0, + 1.0f - ImSaturate((v0 - minimum_scale) / (maximum_scale - minimum_scale))); // Point in the normalized space of our target rectangle + + const ImU32 col_base = colors.at(data_idx); + const ImU32 col_hovered = InvertColorU32(colors.at(data_idx)); + + for (int n = 0; n < res_w; n++) + { + const float t1 = t0 + t_step; + const int v1_idx = (int) (t0 * item_count + 0.5f); + IM_ASSERT(v1_idx >= 0 && v1_idx < values_count); + const float v1 = data.at(data_idx).at(v1_idx); + const ImVec2 tp1 = ImVec2(t1, + 1.0f - ImSaturate((v1 - minimum_scale) / (maximum_scale - minimum_scale))); + + // NB: Draw calls are merged together by the DrawList system. Still, we should render our batch are lower level to save a bit of CPU. + ImVec2 pos0 = ImLerp(inner_bb.Min, inner_bb.Max, tp0); + ImVec2 pos1 = ImLerp(inner_bb.Min, inner_bb.Max, tp1);//ImVec2(tp1.x, 1.0f)); + + window->DrawList->AddLine(pos0, pos1, col_base); + + + t0 = t1; + tp0 = tp1; + } + + // Render the text label + ImGui::PushStyleColor(ImGuiCol_Text, (ImVec4)colors.at(data_idx)); + RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, inner_bb.Min.y + text_margin * data_idx), labels.at(data_idx).c_str()); + ImGui::PopStyleColor(1); + } + + RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, inner_bb.Max.y), title.c_str()); + } + + static void PlotMultiEx( ImGuiPlotType plot_type, const char* label, @@ -58,14 +147,9 @@ namespace ImGui { const ImRect total_bb(frame_bb.Min, add(frame_bb.Max , thing)); - // const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(graph_size.x, graph_size.y)); - // const ImRect inner_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding); - // const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0)); - - ItemSize(total_bb, style.FramePadding.y); - // if (!ItemAdd(total_bb, NULL)) - // return; + if (!ItemAdd(total_bb, 0)) + return; // Determine scale from values if not specified if (scale_min == FLT_MAX || scale_max == FLT_MAX) diff --git a/include/util.hpp b/include/util.hpp index 098eeb4..6af0cc8 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -42,8 +42,8 @@ public: } static float edit(const void* data, int idx){ - //*(int*)data = idx; - return *(int*)(data+sizeof(int)*idx); + *(int*)data = idx; + //return *(int*)(data+sizeof(int)*idx); }; void draw() { @@ -64,10 +64,33 @@ public: const char* to_names[3] = {"a", "b", "z"}; ImGuiPlotType plottype = ImGuiPlotType_Lines; ImColor color = ImColor(255, 255, 255); - ImGui::PlotMultiLines( - "label", 3, to_names, &color, &edit, - to_data, 3, 0.0f, 10.0f, ImVec2(300, 300)); + //ImGui::PlotMultiLines( + // "label", 3, to_names, &color, &edit, + // to_data, 3, 0.0f, 10.0f, ImVec2(300, 300)); + std::vector> data = { + {1, 2, 3, 4}, + {9, 3, 7, 1}, + {8, 3, 6, 2} + }; + + std::string title = "HELLO"; + + std::vector labels = { + "ONE", + "TWO", + "THREE" + }; + + std::vector colors = { + ImColor(255, 255, 255), + ImColor( 0, 255, 0), + ImColor(255, 0, 0), + }; + + sf::Vector2f graph_size(300, 300); + + ImGui::PlotMultiLines(data, title, labels, colors, 10, 0, graph_size); ImGui::PlotLines("FPS", fps_array, 1000, 0, std::to_string(1.0 / fps_average).c_str(), diff --git a/src/CLCaster.cpp b/src/CLCaster.cpp index e16e2be..dc334eb 100644 --- a/src/CLCaster.cpp +++ b/src/CLCaster.cpp @@ -374,7 +374,7 @@ void CLCaster::render_gui() { int offset = 150; int count = 1; - ImGui::Text(x.first.c_str()); + ImGui::Text("%s", x.first.c_str()); ImGui::NextColumn(); @@ -382,7 +382,7 @@ void CLCaster::render_gui() { ImGui::SetColumnOffset(count, offset); offset += 150; count++; ss << std::dec << std::to_string(settings_buffer[x.second]); - ImGui::Text(ss.str().c_str()); + ImGui::Text("%s", ss.str().c_str()); ss.clear(); ss.str(""); ImGui::NextColumn(); @@ -392,7 +392,7 @@ void CLCaster::render_gui() { ImGui::SetColumnOffset(count, offset); offset += 150; count++; ss << "0x" << std::setfill ('0') << std::setw(sizeof(int64_t)*2) << std::hex << settings_buffer[x.second]; - ImGui::Text(ss.str().c_str()); + ImGui::Text("%s", ss.str().c_str()); ss.clear(); ss.str(""); ImGui::NextColumn(); @@ -402,7 +402,7 @@ void CLCaster::render_gui() { ImGui::SetColumnOffset(count, offset); offset += 150; count++; ss << "b" << std::setfill('0') << std::setw(sizeof(int64_t) * 2) << std::bitset<64>(settings_buffer[x.second]); - ImGui::Text(ss.str().c_str()); + ImGui::Text("%s", ss.str().c_str()); ss.clear(); ss.str(""); ImGui::NextColumn();