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

GuiToggle() always returns 0 #371

Closed
SirNate0 opened this issue Jan 27, 2024 · 6 comments
Closed

GuiToggle() always returns 0 #371

SirNate0 opened this issue Jan 27, 2024 · 6 comments

Comments

@SirNate0
Copy link

raygui/src/raygui.h

Lines 2025 to 2071 in 45e7f96

// Toggle Button control
int GuiToggle(Rectangle bounds, const char *text, bool *active)
{
int result = 0;
GuiState state = guiState;
bool temp = false;
if (active == NULL) active = &temp;
// Update control
//--------------------------------------------------------------------
if ((state != STATE_DISABLED) && !guiLocked && !guiSliderDragging)
{
Vector2 mousePoint = GetMousePosition();
// Check toggle button state
if (CheckCollisionPointRec(mousePoint, bounds))
{
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) state = STATE_PRESSED;
else if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON))
{
state = STATE_NORMAL;
*active = !(*active);
}
else state = STATE_FOCUSED;
}
}
//--------------------------------------------------------------------
// Draw control
//--------------------------------------------------------------------
if (state == STATE_NORMAL)
{
GuiDrawRectangle(bounds, GuiGetStyle(TOGGLE, BORDER_WIDTH), GetColor(GuiGetStyle(TOGGLE, ((*active)? BORDER_COLOR_PRESSED : (BORDER + state*3)))), GetColor(GuiGetStyle(TOGGLE, ((*active)? BASE_COLOR_PRESSED : (BASE + state*3)))));
GuiDrawText(text, GetTextBounds(TOGGLE, bounds), GuiGetStyle(TOGGLE, TEXT_ALIGNMENT), GetColor(GuiGetStyle(TOGGLE, ((*active)? TEXT_COLOR_PRESSED : (TEXT + state*3)))));
}
else
{
GuiDrawRectangle(bounds, GuiGetStyle(TOGGLE, BORDER_WIDTH), GetColor(GuiGetStyle(TOGGLE, BORDER + state*3)), GetColor(GuiGetStyle(TOGGLE, BASE + state*3)));
GuiDrawText(text, GetTextBounds(TOGGLE, bounds), GuiGetStyle(TOGGLE, TEXT_ALIGNMENT), GetColor(GuiGetStyle(TOGGLE, TEXT + state*3)));
}
if (state == STATE_FOCUSED) GuiTooltip(bounds);
//--------------------------------------------------------------------
return result;
}

Not sure if it's a bug or not. The documentation seems to have changed from the version used with the raylib-python-cffi, but those docs made it seem like it should return true when active.

@Anut-py
Copy link
Contributor

Anut-py commented Jan 28, 2024

raylib-python-cffi is probably out of date. All the controls were changed to return ints as status values (i.e. 1 or 0). In this case the status value is always 0. You should use active to get the toggle state.

@Anut-py
Copy link
Contributor

Anut-py commented Jan 28, 2024

Actually this does seem inconsistent; GuiMessageBox for example returns the clicked index instead of using a pointer. Maybe this needs to be redesigned to be more consistent.

@raysan5
Copy link
Owner

raysan5 commented Jan 28, 2024

@SirNate0 @Anut-py Latest raygui 4.0 release was a big redesign of the library moving all controls data from return values to function parameters and return values were left for internal control states values required by users. This is still under design trying to find a common pattern for all controls on what kind of info should be returned (i.e. Mouse states over control, focus state, specific required states to be exposed...)

Help and feedback on this improvements are welcome!

@raysan5 raysan5 changed the title GuiToggle always returns 0 GuiToggle() always returns 0 Jan 28, 2024
@furudbat
Copy link
Contributor

furudbat commented Feb 9, 2024

@SirNate0 @Anut-py Latest raygui 4.0 release was a big redesign of the library moving all controls data from return values to function parameters and return values were left for internal control states values required by users. This is still under design trying to find a common pattern for all controls on what kind of info should be returned (i.e. Mouse states over control, focus state, specific required states to be exposed...)

Help and feedback on this improvements are welcome!

I also noticed this, (in the old behavior it was something like this "return 1 when clicked", the newer version broke some code of mine)

Would be nice if the Gui-Elements returns 1 when clicked or interact with, some sort of "onChange"-Event.

they are also "render-only" Gui-Elements with int as result. (like GuiGroupBox, GuiLabel, ...)
those can also return 1 when clicked, maybe enable this feature via macro (?)

For GuiToggle, it can return 1 when clicked:

        // Check toggle button state
        if (CheckCollisionPointRec(mousePoint, bounds))
        {
            if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
            {
                state = STATE_PRESSED;
                result = 1;
            }
            else if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON))
            {
                state = STATE_NORMAL;
                *active = !(*active);
                result = 1;
            }
            else state = STATE_FOCUSED;
        }

For something like GuiToggleGroup, it can be "number of GuiToggle interact": result += GuiToggle(bounds, items[i], &toggle); .

  • GuiScrollPanel can return 1 when scrolled.
  • GuiComboBox also didn't return anything, return 1 when clicked or active got changed
  • GuiTextBoxMulti returns an boolean (but GuiTextBox() returns int)
  • GuiDummyRec always returns 0 ???
  • GuiListViewEx/GuiListView result ???, return 1 when clicked, return 2 when scrolled ... (?)
  • GuiColorPanel didn't return anything, return 1 when change color (?)
  • GuiWindowBox can return different values. "clicked-in-box" (2), close window (1), ...

If some value changed or the user interact with the Gui-Element, I can do something like this:

if(GuiButton(...)) { ... }

if (GuiToggle(...)) {
    // do stuff with change toggle value
}
if (GuiTextBox(...)) {
}

// some special cases ...
if (GuiDropdownBox(....)) {
    case 1:
    // item got selected (clicked)
    break;
    case 2:
    // clicked out-of-box, "close" DropdownBox
    break;
}

I'm sure enums can help as results:

typedef enum {
    TEXTBOX_CLICKED = 1
} GuiTextBoxResult;

@raysan5
Copy link
Owner

raysan5 commented Feb 10, 2024

@furudbat Thanks for the review! Actually, I would like to find a consistent convention between all controls in that regards.

For example:

typedef enum GuiControlResult {
    RESULT_NONE = 0,
    RESULT_FOCUSED = 1,
    RESULT_CLICKED = 2,
    RESULT_SCROLLED = 3,
    RESULT_CUSTOM01 = 4,
    RESULT_CUSTOM02 = 5,
} GuiControlResult;

But every control has to be carefully analized to see what are the possible internal states that could be returned. It would be nice to unify the possible return values but also allow per-control custom return values.

Your controls list with a potential return state is a good start.

@raysan5
Copy link
Owner

raysan5 commented May 7, 2024

Opened a separate issue to review.

@raysan5 raysan5 closed this as completed May 7, 2024
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

4 participants