I think few people know about that, but with InputInt, InputFloat, SliderInt, SliderFloat, DragInt, DragFloat, etc. you can input simple operators to affect the current value:
current value 1000 enter +100 new value 1100
current value 1000 enter /2 new value 500
current value 1000 enter *2 new value 2000
etc.
Except that - doesn't work as expected since it would be ambiguous, but you can use +-100 to subtract 100.
I suspect no one is really relying on this feature and I would like to remove it.
If you relied on this feature please raise a voice :)
I think it's more interesting to have a full expression evaluator, and honestly at this point perhaps it should be just up to the application and not imgui.
I got a got amount of answers for this on twitter,
Basically
I think will keep the feature active until we get a chance to replace it with an expression evaluator, which shouldn't be too much work.
If you would like to i have fully working implementation of calc where you can even register any single char operators. I just tested it on custom InputScalarCalc and it works nicely + using it few years for my config system
Would be interested in seeing the code yes. I think we ought to support an “expression evaluator” callback and provide a default one that users could adapt to handle eg : variables.
Ok, will prepare example to some separate repo or I can attach it as file here. Up to you, in any case it's close to midnight here so tomorrow ;-)
Here is self compilable cpp source file with implemented calculator (tested on Osx - clang).
Just a note: it could use some optimizations like not using std::string, std::stringstream for reading equation, use hardcoded operators and more, which i did not need even now when loading hundereds of small equations in my config system so its fast enough for me now.
This was attempt to make it simpler to read and change with different sets of operators (with simple adjustment it could even support word type operators (not only char) so you could create even own tiny simple scripting language). Feel free to use it as you see fit with no guarantees ;-)
Here is then my snippet for creating input fields with calculator support:
imgui_inputfloat_calc.txt
Thank you!
As is, the implementation style is not suitable for core dear imgui, but I would like to include something like that in the library eventually and this is useful as an example/reference of the things to aim for. Adding a link to this thread from the Todo document now.
Fully agree, style is way different and quite not compatible but it may help ;-) :-)
I will be looking forward for more improvements in the future, thank you for all hard work so far
First off, thanks for your library! It's fantastic.
I had an expression evaluator in my editor in the past, but after switching to imgui I was missing it, so I just tried integrating it back in and it seems to work nicely.
The evaluator I'm using can be found here, it's small and written in a simple, nice C style, though I had to mod it slightly for const: https://www.strchr.com/expression_evaluator
As an example of how to use it, essentially the following can be added just before the return memcmp in ImGui::DataTypeApplyOpFromText(): (Full change here: ExprEvalIntegration.txt)
ExprEval eval;
double result = eval.Eval( buf );
if (data_type == ImGuiDataType_S32)
{
int* v = (int*)data_ptr;
*v = (int)result;
}
else // other ImGuiDataType_ types
Maybe instead of a direct integration of an expression evaluator, we could get a callback that gives the application the const char* buf and gets back a double? Then ImGui::DataTypeApplyOpFromText can run it through the cases for each ImGuiDataType_?