raygui icon indicating copy to clipboard operation
raygui copied to clipboard

`GuiToggleGroup()` should return interaction status in current frame

Open ssoher opened this issue 1 year ago • 1 comments

Currently we can't do if(GuiToggleGroup(...)) because the return value is always 0. It should return 1 in the frames where user interacts with it. The only way to achieve running any logic when it's interacted is to store the active parameter right before drawing it and checking if the value of active is changed afterwards.

Suggested changes:

int GuiToggleGroup(Rectangle bounds, const char *text, int *active)
{
    #if !defined(RAYGUI_TOGGLEGROUP_MAX_ITEMS)
        #define RAYGUI_TOGGLEGROUP_MAX_ITEMS    32
    #endif
    float initBoundsX = bounds.x;

    int temp = 0;
    if (active == NULL) active = &temp;

+    int prev_active = active;

    bool toggle = false;    // Required for individual toggles

    // Get substrings items from text (items pointers)
    int rows[RAYGUI_TOGGLEGROUP_MAX_ITEMS] = { 0 };
    int itemCount = 0;
    const char **items = GuiTextSplit(text, ';', &itemCount, rows);

    int prevRow = rows[0];

    for (int i = 0; i < itemCount; i++)
    {
        if (prevRow != rows[i])
        {
            bounds.x = initBoundsX;
            bounds.y += (bounds.height + GuiGetStyle(TOGGLE, GROUP_PADDING));
            prevRow = rows[i];
        }

        if (i == (*active))
        {
            toggle = true;
            GuiToggle(bounds, items[i], &toggle);
        }
        else
        {
            toggle = false;
            GuiToggle(bounds, items[i], &toggle);
            if (toggle) *active = i;
        }

        bounds.x += (bounds.width + GuiGetStyle(TOGGLE, GROUP_PADDING));
    }

+    return active != prev_active;
}

Usage

Current (ugh):

enum editor_visualization_mode prev_vis = current_editor_visualization_mode;
GuiToggleGroup(layout_rectangle, "Render;Indices;Weights", &(int)current_editor_visualization_mode);
if(prev_vis != current_editor_visualization_mode) {
    terrain_set_current_visualize_mode(current_editor_visualization_mode);
}

With changes:

if(GuiToggleGroup(layout_rectangle, "Render;Indices;Weights", &(int)current_editor_visualization_mode)) {
    terrain_set_current_visualize_mode(current_editor_visualization_mode);
}

ssoher avatar Nov 06 '24 05:11 ssoher

@ssoher Yeah, return values should be reviewed: https://github.com/raysan5/raygui/issues/402

But I'd like to review it carefully to find some consistency between all controls.

raysan5 avatar Nov 17 '24 11:11 raysan5