imgui_test_engine icon indicating copy to clipboard operation
imgui_test_engine copied to clipboard

How to query value from ref?

Open S-A-Martin opened this issue 2 years ago • 2 comments

How do I check the value of something by querying its ref? I'm hoping to do something like this:

ctx->ItemInputValue("sVal", 5.5f); // sets the sVal DragFloat to 5.5f
IM_CHECK_EQ("sVal", 5.5f); // Checks that the value has been set correctly

I'm not using GuiFunc, so I don't 'think' using GetVars is what I need, and I don't have the variable tied to an app class or anything.

It's being used more like:

    ImGui::NewFrame();
    {
      ImGui::Begin("My Window", NULL, window_flags);
      static float sVal = 0.0f;
      ImGui::DragFloat("sVal", &sVal);`
      ImGui::End();
    }

I just want to verify that the slider value is set correctly.

S-A-Martin avatar Jun 22 '23 12:06 S-A-Martin

Hello,

It's not really possible as the value is owned by you, so you need access to your data if you want to test against it.

We recently improved that section of the docs here: Automation-API -> Accessing your data

We have a WIP set of API (non-published yet) to "read" from emitted vertices or capture text logs, so you could technically capture the text output of the "sVal" widget (which would be e.g. "5.5" + "sVal") which may later be leveraged for this as an alternative solution.

ocornut avatar Jun 22 '23 12:06 ocornut

I have pushed helpers to do this by using selection + clipboard + parse as a way to extract values. This is not the more general version I'm hoping to further develop but it does the job for simple cases, and is a good workaround.

t = IM_REGISTER_TEST(e, "testengine", "testengine_select_read_value");
t->GuiFunc = [](ImGuiTestContext* ctx)
{
    ImGui::Begin("Test Window", NULL, ImGuiWindowFlags_NoSavedSettings);

    int v_int = 123;
    float v_float = 0.456f;
    ImGui::SliderInt("int", &v_int, 0, 200);
    ImGui::DragFloat("float", &v_float, 0.01f, 0.0f, 1.0f);
    ImGui::End();
};
t->TestFunc = [](ImGuiTestContext* ctx)
{
    ctx->SetRef("Test Window");
    int v_int = 0;
    float v_float = 0;
    ctx->ItemSelectAndReadValue("int", &v_int);
    ctx->ItemSelectAndReadValue("float", &v_float);
    IM_CHECK_EQ(v_int, 123);
    IM_CHECK_EQ(v_float, 0.456f);
};

ocornut avatar Apr 24 '24 12:04 ocornut