-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for variety of data types (32/64 bits, signed/unsigned, float/double) #643
Comments
It is possible but would require a bit of work/refactor. |
I got this situation and i made a templated function, theoricly it works for all numeric type if you use the right format expression (eg: %f) |
I would really appreciate this kind of templated functionality. I write engineering code that requires the use of doubles and right now I’ve been casting and copying back and forth between doubles and floats, since doubles aren't supported, but this would let me avoid that. Thanks! |
Hello all, I have pushed a enum ImGuiDataType_
{
ImGuiDataType_S32, // int
ImGuiDataType_U32, // unsigned int
ImGuiDataType_S64, // long long, __int64
ImGuiDataType_U64, // unsigned long long, unsigned __int64
ImGuiDataType_Float, // float
ImGuiDataType_Double, // double
ImGuiDataType_COUNT
}; The functions are: bool DragScalar(const char* label, ImGuiDataType data_type, void* v, float v_speed, const void* v_min, const void* v_max, const char* format = NULL, float power = 1.0f);
bool DragScalarN(const char* label, ImGuiDataType data_type, void* v, int components, float v_speed, const void* v_min, const void* v_max, const char* format = NULL, float power = 1.0f);
bool InputScalar(const char* label, ImGuiDataType data_type, void* v, const void* step, const void* step_fast, const char* format = NULL, ImGuiInputTextFlags extra_flags = 0);
bool InputScalarN(const char* label, ImGuiDataType data_type, void* v, int components, const void* step, const void* step_fast, const char* format = NULL, ImGuiInputTextFlags extra_flags = 0);
bool SliderScalar(const char* label, ImGuiDataType data_type, void* v, const void* v_min, const void* v_max, const char* format = NULL, float power = 1.0f);
bool SliderScalarN(const char* label, ImGuiDataType data_type, void* v, int components, const void* v_min, const void* v_max, const char* format = NULL, float power = 1.0f);
bool VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType data_type, void* v, const void* v_min, const void* v_max, const char* format = NULL, float power = 1.0f); Also recently added bool InputDouble(const char* label, double* v, double step = 0.0f, double step_fast = 0.0f, const char* format = "%.6f", ImGuiInputTextFlags extra_flags = 0); I've tested variety of cases including high-range, decorated format strings, power curves, etc. but there's most certainly a bug waiting to be found in there. The internal template code somehow manage to handle things like 64-bit integers and double so it's a bit messy. One of the limitation I don't think I will try to lift is that Sliders typically support half of the maximum data type range. So e.g. a Even if you don't use those data types yet, testing the branch would be useful as the code changes are affecting all types. (1) For example, this is SliderFloat now: bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, const char* format, float power)
{
return SliderScalar(label, ImGuiDataType_Float, v, &v_min, &v_max, format, power);
} The wrapper turns v_min/v_max on the stack into pointers, this is why it is useful to have a 1-line wrapper here. May end up adding them for more types... (2) (3) (4) (5) Feedback very welcome. If this work well enough, after some tweaks/polish I could package that into v1.61. Thanks! EDIT Various edits. |
…to not use obsolete formats (when using IMGUI_DISABLE_OBSOLETE_FUNCTIONS) (#643)
Just grabbed this branch and tried it out in my project. First off, this is an excellent change. I've had my own hacked together ImGui code to support un/signed integer types for a while now. It was always a pain to merge with ImGui updates. Just going to address the points you made above in order
So overall, I'm a fan of this change. Allowed me to get rid of my own hacky code. Here's the boilerplate wrappers I ended up writing. It would be nice to just have the strong typed functions already provided.
|
Thanks for the feedback @amc522. This is indeed to be merged for 1.61, I was just waiting for more testers/feedback because I'm always worrying I may have broken things with this change. Any reason why your DragScalar template are using references and not pointers? Pointers would be consistent with the rest of the imgui API. The problem with current approach is that we can't add too many entry points to imgui.h, especially considering I will add S8/U8/S16/U16 to the type list. I know it is partly irrational but aside from making sure the core functionality are easy to convert to another language, I'd prefer not making imgui.h a template festival. Instead we can consider adding e.g. a |
Just my personal style preference. It was just wrappers in my code and I was not pushing for references in ImGui. I was just showing that I would be immediately wrapping the functions.
That sounds great to me. My imgui_user.h/.cpp is pretty much just filled with C++ helpers and RAII wrappers.
If you want, I can write up a function that does that for reference. Thanks again for this change! Helps out a bunch. |
…eyboard/gamepad when speed < 1. Enforce min/max bounds when power curves are used. SliderScalar: Fixed integer/slow tweaking. (#643)
Closing this (supported since 1.61). |
This is maybe not very common, but I want to control the value of doubles. It would be nice if there was a function to do that directly, or if SliderFloat was templated based on the variable type. Likewise, the same argument could be made for the integral controls. What if I need to set a value that is larger than 2 or 4 billion? If I were to support one integral or float type, I would support the widest native type available and let the user deal with conversion to narrow types themselves.
The text was updated successfully, but these errors were encountered: