raysan5 / raygui

A simple and easy-to-use immediate-mode gui library
zlib License
3.47k stars 298 forks source link

Event collision #285

Closed Raidez closed 1 year ago

Raidez commented 1 year ago

A simple question but how can I use a simple button but stop the event to not activate the code, when pressing the left mouse button, further.

from raylib import *

# define constants
WIDHT, HEIGHT = 800, 450
TITLE = b"RPG : Dice & Retry"
FPS = 60

################################################################################

InitWindow(WIDHT, HEIGHT, TITLE)
InitPhysics()
SetTargetFPS(FPS)

# main loop
while not WindowShouldClose():
    # update
    UpdatePhysics()

    if IsMouseButtonPressed(MOUSE_BUTTON_LEFT):
        CreatePhysicsBodyCircle(GetMousePosition(), GetRandomValue(10, 45), 10)

    # draw
    BeginDrawing()
    ClearBackground(BLACK)

    if GuiButton((WIDHT // 2 - 50, HEIGHT // 2 + 80, 100, 30), b"press me"):
        ClearBackground(WHITE)

    for i in range(GetPhysicsBodiesCount()):
        if body := GetPhysicsBody(i):
            vertices_count = GetPhysicsShapeVerticesCount(i)
            for j in range(vertices_count):
                vertex0 = GetPhysicsShapeVertex(body, j)

                jj = j + 1 if j + 1 < vertices_count else 0
                vertex1 = GetPhysicsShapeVertex(body, jj)

                DrawLineV(vertex0, vertex1, GREEN)

    EndDrawing()

# cleanup
ClosePhysics()
CloseWindow()

Yeah, I know it's not C code, but I don't think this problem is specific to Python binding.

raysan5 commented 1 year ago

@Raidez I'm afraid that's not possible on current raygui implementation, keep in mind that raygui is an immediate-mode library and it implies some limitations.

raysan5 commented 1 year ago

Not sure if I really understood your issue, just re-reading it... you can just enable a boolean on button pressed, for example:

bool enabledOption = false;
...
if (GuiButton(WIDHT/2 - 50, HEIGHT/2 + 80, 100, 30), "press me")) enabledOption = !enabledOption;

if (enabledOption)
{
    // Do/draw something
}

It will enable/disable some option every time the button is pressed.

Raidez commented 1 year ago

The problem is when I click on the GuiButton, it trigger the IsMouseButtonPressed too. I found a wacky way :

from raylib import *

# define constants
WIDHT, HEIGHT = 800, 450
TITLE = b"RPG : Dice & Retry"
FPS = 60

################################################################################

InitWindow(WIDHT, HEIGHT, TITLE) # type: ignore
InitPhysics()
SetTargetFPS(FPS)

enabledOption = False

# main loop
while not WindowShouldClose():
    # update
    UpdatePhysics()

        # check if GuiButton presed first
    if GuiButton((WIDHT // 2 - 50, HEIGHT // 2 + 80, 100, 30), b"press me"):
        enabledOption = not enabledOption
    elif IsMouseButtonReleased(MOUSE_BUTTON_LEFT): # check mouse button release only
        CreatePhysicsBodyCircle(GetMousePosition(), GetRandomValue(10, 45), 10)

    # draw
    BeginDrawing()
    ClearBackground(BLACK)

    if enabledOption:
        ClearBackground(WHITE)

    for i in range(GetPhysicsBodiesCount()):
        if body := GetPhysicsBody(i):
            vertices_count = GetPhysicsShapeVerticesCount(i)
            for j in range(vertices_count):
                vertex0 = GetPhysicsShapeVertex(body, j)

                jj = j + 1 if j + 1 < vertices_count else 0
                vertex1 = GetPhysicsShapeVertex(body, jj)

                DrawLineV(vertex0, vertex1, GREEN)

    EndDrawing()

# cleanup
ClosePhysics()
CloseWindow()