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

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

Open
ssoher opened this issue Nov 6, 2024 · 1 comment
Open

Comments

@ssoher
Copy link
Contributor

ssoher commented Nov 6, 2024

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);
}
@raysan5 raysan5 changed the title GuiToggleGroup should return interaction status in current frame GuiToggleGroup() should return interaction status in current frame Nov 17, 2024
@raysan5
Copy link
Owner

raysan5 commented Nov 17, 2024

@ssoher Yeah, return values should be reviewed: #402

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

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

No branches or pull requests

2 participants