Skip to content

GuiToggleGroup() should return interaction status in current frame #439

@ssoher

Description

@ssoher

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions