From b370eda0d51c2d039b45fee7da6ed7616e2afa4c Mon Sep 17 00:00:00 2001 From: cropinghigh Date: Sun, 27 Dec 2020 00:56:39 +0300 Subject: [PATCH] Fix bugs+move widget --- core/src/gui/widgets/stepped_slider.cpp | 23 +++++++++++++++++++++++ core/src/gui/widgets/stepped_slider.h | 5 +++++ core/src/imgui/imgui.h | 1 - core/src/imgui/imgui_widgets.cpp | 19 ------------------- soapy_source/src/main.cpp | 7 +++++-- 5 files changed, 33 insertions(+), 22 deletions(-) create mode 100644 core/src/gui/widgets/stepped_slider.cpp create mode 100644 core/src/gui/widgets/stepped_slider.h diff --git a/core/src/gui/widgets/stepped_slider.cpp b/core/src/gui/widgets/stepped_slider.cpp new file mode 100644 index 0000000..be2f7c2 --- /dev/null +++ b/core/src/gui/widgets/stepped_slider.cpp @@ -0,0 +1,23 @@ +#include +#include +#include + +namespace ImGui { + bool SliderFloatWithSteps(const char* label, float* v, float v_min, float v_max, float v_step, const char* display_format) { + if (!display_format) { + display_format = "%.3f"; + } + + char text_buf[64] = {}; + ImFormatString(text_buf, IM_ARRAYSIZE(text_buf), display_format, *v); + + // Map from [v_min,v_max] to [0,N] + const int countValues = int((v_max-v_min)/v_step); + int v_i = int((*v - v_min)/v_step); + const bool value_changed = ImGui::SliderInt(label, &v_i, 0, countValues, text_buf); + + // Remap from [0,N] to [v_min,v_max] + *v = v_min + float(v_i) * v_step; + return value_changed; + } +} diff --git a/core/src/gui/widgets/stepped_slider.h b/core/src/gui/widgets/stepped_slider.h new file mode 100644 index 0000000..06aefe6 --- /dev/null +++ b/core/src/gui/widgets/stepped_slider.h @@ -0,0 +1,5 @@ +#pragma once + +namespace ImGui { + bool SliderFloatWithSteps(const char* label, float* v, float v_min, float v_max, float v_step, const char* display_format = "%.3f"); +} diff --git a/core/src/imgui/imgui.h b/core/src/imgui/imgui.h index db6a16a..b6c91a0 100644 --- a/core/src/imgui/imgui.h +++ b/core/src/imgui/imgui.h @@ -481,7 +481,6 @@ namespace ImGui // - CTRL+Click on any slider to turn them into an input box. Manually input values aren't clamped and can go off-bounds. // - Adjust format string to decorate the value with a prefix, a suffix, or adapt the editing and display precision e.g. "%.3f" -> 1.234; "%5.2f secs" -> 01.23 secs; "Biscuit: %.0f" -> Biscuit: 1; etc. IMGUI_API bool SliderFloat(const char* label, float* v, float v_min, float v_max, const char* format = "%.3f", float power = 1.0f); // adjust format to decorate the value with a prefix or a suffix for in-slider labels or unit display. Use power!=1.0 for power curve sliders - IMGUI_API bool SliderFloatWithSteps(const char* label, float* v, float v_min, float v_max, float v_step, const char* display_format = "%.3f"); IMGUI_API bool SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* format = "%.3f", float power = 1.0f); IMGUI_API bool SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* format = "%.3f", float power = 1.0f); IMGUI_API bool SliderFloat4(const char* label, float v[4], float v_min, float v_max, const char* format = "%.3f", float power = 1.0f); diff --git a/core/src/imgui/imgui_widgets.cpp b/core/src/imgui/imgui_widgets.cpp index aa1d72b..6961d80 100644 --- a/core/src/imgui/imgui_widgets.cpp +++ b/core/src/imgui/imgui_widgets.cpp @@ -2702,25 +2702,6 @@ bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, c return SliderScalar(label, ImGuiDataType_Float, v, &v_min, &v_max, format, power); } -bool ImGui::SliderFloatWithSteps(const char* label, float* v, float v_min, float v_max, float v_step, const char* display_format) -{ - if (!display_format) - display_format = "%.3f"; - - char text_buf[64] = {}; - ImFormatString(text_buf, IM_ARRAYSIZE(text_buf), display_format, *v); - - // Map from [v_min,v_max] to [0,N] - const int countValues = int((v_max-v_min)/v_step); - int v_i = int((*v - v_min)/v_step); - const bool value_changed = ImGui::SliderInt(label, &v_i, 0, countValues, text_buf); - - // Remap from [0,N] to [v_min,v_max] - *v = v_min + float(v_i) * v_step; - return value_changed; -} - - bool ImGui::SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* format, float power) { return SliderScalarN(label, ImGuiDataType_Float, v, 2, &v_min, &v_max, format, power); diff --git a/soapy_source/src/main.cpp b/soapy_source/src/main.cpp index 9b88fb7..faa31a0 100644 --- a/soapy_source/src/main.cpp +++ b/soapy_source/src/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -154,6 +155,7 @@ private: gainList = dev->listGains(SOAPY_SDR_RX, channelId); delete[] uiGains; uiGains = new float[gainList.size()]; + gainRanges.clear(); for (auto gain : gainList) { gainRanges.push_back(dev->getGainRange(SOAPY_SDR_RX, channelId, gain)); @@ -398,10 +400,11 @@ private: ImGui::SetNextItemWidth(menuWidth - gainNameLen); float step = _this->gainRanges[i].step(); bool res; - if(step == 0.0f) + if(step == 0.0f) { res = ImGui::SliderFloat((std::string("##_gain_sel_") + _this->name + gain).c_str(), &_this->uiGains[i], _this->gainRanges[i].minimum(), _this->gainRanges[i].maximum()); - else + } else { res = ImGui::SliderFloatWithSteps((std::string("##_gain_sel_") + _this->name + gain).c_str(), &_this->uiGains[i], _this->gainRanges[i].minimum(), _this->gainRanges[i].maximum(), step); + } if(res) { if (_this->running) { _this->dev->setGain(SOAPY_SDR_RX, _this->channelId, gain, _this->uiGains[i]);