Skip to content
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

Question: does anyone know/use the math operators of InputInt/InputFloat ? #1691

Closed
ocornut opened this issue Mar 22, 2018 · 9 comments
Closed
Labels

Comments

@ocornut
Copy link
Owner

ocornut commented Mar 22, 2018

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.

@ocornut
Copy link
Owner Author

ocornut commented Apr 4, 2018

I got a got amount of answers for this on twitter,

Basically

  • majority of people didn't know about this
  • the other half got slightly excited when reading the question, thinking there was a proper expression evaluator, only to realize it is a much more simplified feature
  • a few people actually knew and used it as is.

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.

@sadovsf
Copy link

sadovsf commented May 26, 2019

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

@ocornut
Copy link
Owner Author

ocornut commented May 26, 2019

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.

@sadovsf
Copy link

sadovsf commented May 26, 2019

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 ;-)

@sadovsf
Copy link

sadovsf commented May 27, 2019

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 ;-)

calc.txt

Here is then my snippet for creating input fields with calculator support:
imgui_inputfloat_calc.txt

@ocornut
Copy link
Owner Author

ocornut commented May 27, 2019

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.

@sadovsf
Copy link

sadovsf commented May 27, 2019

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

@JimmyLord
Copy link

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_?

@ebkgne
Copy link

ebkgne commented Jun 7, 2024

Hello,

I would love to be able to do math in inputInt (and the ctrl+clicked part of SliderScalarN), and also reactive this feature +100, +-100, *100, ... you mentioned @ocornut , but not sure where to ?

if I write 1+1 inside the input created by this line

int i; if (InputInt("xxx",&i...)) { /* no sign of string to evaluate here ? */}

So I got better luck with ImGuiInputTextFlags_Callback , memcpying data->buf to data->UserData,

my question is, is that the efficient way to go ?

Also I added attributes ImGuiInputTextFlags and ImGuiInputTextCallback to SliderScalarN definition (and so on) to pass to underlying ctrl+click InputInt , I could make a PR if it sounds usefull

SliderScalarN(const char* label, ... , ImGuiInputTextFlags temp_flags = 0, ImGuiInputTextCallback temp_callback = NULL, void* temp_user_data = NULL);

so I can do ( I also changed SliderScalarN to return N's offset )

int x = ImGui::SliderScalarN("name", &data, type, ...,ImGuiInputTextFlags_CallbackAlways | ImGuiInputTextFlags_EnterReturnsTrue, [](auto data){ memcpy(data->UserData, data->Buf, data->BufSize); return 1;}, &buf); if (x) *data = evaluate_expression(buf[x], type);

This partly works, But if the value is already set to 1 and I edit the input to 1+1, the SliderScalarN wont return true because of value_changed comparison fail, as DataTypeApplyFromText returns 1 for 1+1 .... would it make sens that if the input is not pure valid p_data returns NaN or something ?

thx for suggestions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants