From 5ee3ec43510b76596c03568e366fcd378de7a6f1 Mon Sep 17 00:00:00 2001 From: shweet Date: Fri, 15 Aug 2025 14:27:44 -0400 Subject: [PATCH] Changing how string format for imgui float widgets work, suppressing stbi warnings --- src/COMMON.h | 40 ++++++++++++++++++++++++++++++++++++++++ src/imgui.cpp | 12 ++++++------ src/imgui.h | 27 ++++++--------------------- src/texture.cpp | 7 +++++++ 4 files changed, 59 insertions(+), 27 deletions(-) diff --git a/src/COMMON.h b/src/COMMON.h index 75653e3..f4c54ab 100644 --- a/src/COMMON.h +++ b/src/COMMON.h @@ -128,6 +128,46 @@ static inline std::string string_quote(const std::string& string) return "\"" + string + "\""; } +#define FLOAT_FORMAT_MAX_DECIMALS 2 +#define FLOAT_FORMAT_EPSILON 1e-6f +static constexpr f32 FLOAT_FORMAT_POW10[] = {1.f, 10.f, 100.f}; + +static inline s32 f32_decimals_needed(f32 value) +{ + f32 integerPart = 0.f; + f32 fractionalPart = modff(value, &integerPart); + fractionalPart = fabsf(fractionalPart); + + if (fractionalPart < FLOAT_FORMAT_EPSILON) + return 0; + + for (s32 decimalCount = 1; decimalCount <= FLOAT_FORMAT_MAX_DECIMALS; ++decimalCount) + { + f32 scaledFraction = fractionalPart * FLOAT_FORMAT_POW10[decimalCount]; + if (fabsf(scaledFraction - roundf(scaledFraction)) < FLOAT_FORMAT_EPSILON * FLOAT_FORMAT_POW10[decimalCount]) + return decimalCount; + } + return FLOAT_FORMAT_MAX_DECIMALS; +} + +static inline const char* f32_format_get(f32 value) +{ + static std::string formatString; + const s32 decimalCount = f32_decimals_needed(value); + formatString = (decimalCount == 0) ? "%.0f" : ("%." + std::to_string(decimalCount) + "f"); + return formatString.c_str(); +} + +static inline const char* vec2_format_get(const vec2& value) +{ + static std::string formatString; + const s32 decimalCountX = f32_decimals_needed(value.x); + const s32 decimalCountY = f32_decimals_needed(value.y); + const s32 decimalCount = (decimalCountX > decimalCountY) ? decimalCountX : decimalCountY; + formatString = (decimalCount == 0) ? "%.0f" : ("%." + std::to_string(decimalCount) + "f"); + return formatString.c_str(); +} + static inline std::string path_canonical_resolve ( const std::string& inputPath, diff --git a/src/imgui.cpp b/src/imgui.cpp index 554f78d..17e66d0 100644 --- a/src/imgui.cpp +++ b/src/imgui.cpp @@ -389,12 +389,12 @@ IMGUI_ITEM_FUNCTION(_imgui_selectable, IMGUI_SELECTABLE, ImGui::Selectable(self. IMGUI_ITEM_VALUE_FUNCTION(_imgui_radio_button, IMGUI_RADIO_BUTTON, s32, ImGui::RadioButton(self.label_get(), &value, self.value)); IMGUI_ITEM_VALUE_FUNCTION(_imgui_color_button, IMGUI_COLOR_BUTTON, vec4, ImGui::ColorButton(self.label_get(), ImVec4(value), self.flags)); IMGUI_ITEM_VALUE_FUNCTION(_imgui_checkbox, IMGUI_CHECKBOX, bool, ImGui::Checkbox(self.label_get(), &value)); -IMGUI_ITEM_VALUE_CLAMP_FUNCTION(_imgui_input_int, IMGUI_INPUT_INT, s32, ImGui::InputInt(self.label.c_str(), &value, self.step, self.stepFast, self.flags)); -IMGUI_ITEM_VALUE_CLAMP_FUNCTION(_imgui_input_int2, IMGUI_INPUT_INT, ivec2, ImGui::InputInt2(self.label.c_str(), value_ptr(value), self.flags)); -IMGUI_ITEM_VALUE_CLAMP_FUNCTION(_imgui_input_float, IMGUI_INPUT_FLOAT, f32, ImGui::InputFloat(self.label.c_str(), &value, self.step, self.stepFast, self.format_get(), self.flags)); -IMGUI_ITEM_VALUE_FUNCTION(_imgui_slider_float, IMGUI_SLIDER_FLOAT, f32, ImGui::SliderFloat(self.label_get(), &value, self.min, self.max, self.format_get(), self.flags)); -IMGUI_ITEM_VALUE_FUNCTION(_imgui_drag_float, IMGUI_DRAG_FLOAT, f32, ImGui::DragFloat(self.label_get(), &value, self.speed, self.min, self.max, self.format_get())); -IMGUI_ITEM_VALUE_FUNCTION(_imgui_drag_float2, IMGUI_DRAG_FLOAT, vec2, ImGui::DragFloat2(self.label_get(), value_ptr(value), self.speed, self.min, self.max, self.format_get())); +IMGUI_ITEM_VALUE_CLAMP_FUNCTION(_imgui_input_int, IMGUI_INPUT_INT, s32, ImGui::InputInt(self.label_get(), &value, self.step, self.stepFast, self.flags)); +IMGUI_ITEM_VALUE_CLAMP_FUNCTION(_imgui_input_int2, IMGUI_INPUT_INT, ivec2, ImGui::InputInt2(self.label_get(), value_ptr(value), self.flags)); +IMGUI_ITEM_VALUE_CLAMP_FUNCTION(_imgui_input_float, IMGUI_INPUT_FLOAT, f32, ImGui::InputFloat(self.label_get(), &value, self.step, self.stepFast, f32_format_get(value), self.flags)); +IMGUI_ITEM_VALUE_FUNCTION(_imgui_slider_float, IMGUI_SLIDER_FLOAT, f32, ImGui::SliderFloat(self.label_get(), &value, self.min, self.max, f32_format_get(value), self.flags)); +IMGUI_ITEM_VALUE_FUNCTION(_imgui_drag_float, IMGUI_DRAG_FLOAT, f32, ImGui::DragFloat(self.label_get(), &value, self.speed, self.min, self.max, f32_format_get(value))); +IMGUI_ITEM_VALUE_FUNCTION(_imgui_drag_float2, IMGUI_DRAG_FLOAT, vec2, ImGui::DragFloat2(self.label_get(), value_ptr(value), self.speed, self.min, self.max, vec2_format_get(value))); IMGUI_ITEM_VALUE_FUNCTION(_imgui_color_edit3, IMGUI_COLOR_EDIT, vec3, ImGui::ColorEdit3(self.label_get(), value_ptr(value), self.flags)); IMGUI_ITEM_VALUE_FUNCTION(_imgui_color_edit4, IMGUI_COLOR_EDIT, vec4, ImGui::ColorEdit4(self.label_get(), value_ptr(value), self.flags)); IMGUI_ITEM_CHECKBOX_FUNCTION(_imgui_checkbox_selectable, _imgui_selectable(self, imgui)); diff --git a/src/imgui.h b/src/imgui.h index 6f2998f..11cc4fb 100644 --- a/src/imgui.h +++ b/src/imgui.h @@ -466,7 +466,6 @@ struct ImguiItem { std::string label{}; std::string tooltip{}; - std::string format{}; std::string& text = tooltip; std::string undoAction{}; std::string popup{}; @@ -548,11 +547,7 @@ struct ImguiItem ImguiItem copy(const ImguiItemOverride& override) const { ImguiItem out = *this; - if (override.isDisabled) - { - out.isDisabled = override.isDisabled; - out.format.clear(); - } + if (override.isDisabled) out.isDisabled = override.isDisabled; if(override.isSelected) out.isSelected = override.isSelected; if (is_popup() && imgui_is_popup_open(popup)) out.isSelected = true; if (id != ID_NONE) out.id = id + idOffset + override.id; @@ -581,7 +576,6 @@ struct ImguiItem const char* drag_drop_get() const { return dragDrop.c_str(); } const char* tooltip_get() const { return tooltip.c_str(); } const char* text_get() const { return text.c_str(); } - const char* format_get() const { return format.c_str(); } }; #define IMGUI_ITEM(NAME, ...) const inline ImguiItem NAME = []{ ImguiItem self; __VA_ARGS__; self.construct(); return self; }() @@ -888,7 +882,6 @@ IMGUI_ITEM(IMGUI_SCALE_ANM2_OPTIONS_CHILD, IMGUI_ITEM(IMGUI_SCALE_ANM2_VALUE, self.label = "Value", self.tooltip = "The size and position-related frame properties in the anm2 will be scaled by this value.", - self.format = "%.2f", self.value = 1, self.step = 0.25, self.stepFast = 1 @@ -1277,7 +1270,6 @@ IMGUI_ITEM(IMGUI_CANVAS_VIEW_CHILD, IMGUI_ITEM(IMGUI_CANVAS_ZOOM, self.label = "Zoom", self.tooltip = "Change the zoom of the canvas.", - self.format = "%.0f", self.min = CANVAS_ZOOM_MIN, self.max = CANVAS_ZOOM_MAX, self.value = CANVAS_ZOOM_DEFAULT @@ -1309,7 +1301,6 @@ IMGUI_ITEM(IMGUI_CANVAS_ANIMATION_OVERLAY, IMGUI_ITEM(IMGUI_CANVAS_ANIMATION_OVERLAY_TRANSPARENCY, self.label = "Alpha", self.tooltip = "Set the transparency of the animation overlay.", - self.format = "%.0f", self.value = 255, self.max = 255 ); @@ -1374,39 +1365,34 @@ IMGUI_ITEM(IMGUI_FRAME_PROPERTIES_POSITION, self.label = "Position", self.tooltip = "Change the position of the selected frame.", self.undoAction = "Frame Position", - self.isUseItemActivated = true, - self.format = "%.0f" + self.isUseItemActivated = true ); IMGUI_ITEM(IMGUI_FRAME_PROPERTIES_CROP, self.label = "Crop", self.tooltip = "Change the crop position of the selected frame.", self.undoAction = "Frame Crop", - self.isUseItemActivated = true, - self.format = "%.0f" + self.isUseItemActivated = true ); IMGUI_ITEM(IMGUI_FRAME_PROPERTIES_SIZE, self.label = "Size", self.tooltip = "Change the size of the crop of the selected frame.", self.undoAction = "Frame Size", - self.isUseItemActivated = true, - self.format = "%.0f" + self.isUseItemActivated = true ); IMGUI_ITEM(IMGUI_FRAME_PROPERTIES_PIVOT, self.label = "Pivot", self.tooltip = "Change the pivot of the selected frame.", self.undoAction = "Frame Pivot", - self.isUseItemActivated = true, - self.format = "%.0f" + self.isUseItemActivated = true ); IMGUI_ITEM(IMGUI_FRAME_PROPERTIES_SCALE, self.label = "Scale", self.tooltip = "Change the scale of the selected frame.", self.undoAction = "Frame Scale", - self.format = "%.0f", self.isUseItemActivated = true, self.value = 100 ); @@ -1415,8 +1401,7 @@ IMGUI_ITEM(IMGUI_FRAME_PROPERTIES_ROTATION, self.label = "Rotation", self.tooltip = "Change the rotation of the selected frame.", self.undoAction = "Frame Rotation", - self.isUseItemActivated = true, - self.format = "%.0f" + self.isUseItemActivated = true ); IMGUI_ITEM(IMGUI_FRAME_PROPERTIES_DELAY, diff --git a/src/texture.cpp b/src/texture.cpp index 82d7b7d..7ef2a58 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -1,3 +1,10 @@ +#if defined(__clang__) || defined(__GNUC__) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wmissing-field-initializers" + #pragma GCC diagnostic ignored "-Wunused-function" + #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" +#endif + #include "texture.h" #define STBI_ONLY_PNG