GM:S 1.4 Question regarding button using Draw GUI

Discussion in 'Programming' started by Navimaster, Jul 15, 2016.

Tags:
  1. Navimaster

    Navimaster Guest

    This is my button drawn under the Draw GUI event.

    [​IMG]

    Fairly simple. It draws a rectangle and the text "New Game" on it. When the mouse hovers over it, it lights up. When you click it, the graphic changes. It's a good button.

    [​IMG] [​IMG]


    The problem occurs when I enter the area of the button while HOLDING the mouse button down.


    [​IMG]

    See how it behaves? Instead of lighting up, it gets pressed.

    All because I am holding the mouse button as I come in. Any way to avoid this and have it light up instead?
     
    Last edited by a moderator: Jul 15, 2016
  2. jazzzar

    jazzzar Member

    Joined:
    Jun 29, 2016
    Posts:
    514
    Code:
    If (distance_to_point(mouse_x,mouse_y)<=0)
    {
        If mouse_check_button(mb_left)
        draw_sprite(blahblahblah)
    }
    Hope it helps
     
  3. Navimaster

    Navimaster Guest

    Sorry, I don't understand your answer.

    It's a drawn button. It's reacting to the rectangle coordinate area.
     
  4. RangerX

    RangerX Member

    Joined:
    Jun 20, 2016
    Posts:
    2,587
    You are using "mouse check button". Isn't there a "mouse check button pressed" or something that is available? (i'd have to check, I am not near my GMS)
     
  5. Navimaster

    Navimaster Guest

    I tried the mouse_check_button_pressed approach... and it immediately lights it back up after I click.

    Any other suggestions?

    Also, the "mouse check button" behaves the way I want it to for the most part. If you hold the button down, it stays down.

    The problem is not what the button does when my mouse is on top of it. The problem is what happens when I'm holding the mouse down as I approach the button.

    Let me illustrate..

    [​IMG] <-- Holding down the mouse button while on top of it? Fine. [​IMG]


    [​IMG] <-- Holding down the mouse button as I approach it? Not fine. X
     
  6. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,398
    unfortunately this will not be possible without states.
    You can store variables like 'clicked', 'hovered' and all, but this can get a little messy.
    I would recomend you implement it as a state machine. (Check out my tutorial here:

    Sorry, couldnt just paste a link without GMC wanting to embed the video as media
    ).
    I would have a neutral state, a hovered state and then a cliked state.
    from the neutral state you can go to the hover state by hovering on the button.
    from the hover state you can go back to the neutral state by removing the mouse or the clicked state by clicking the button.
    from the cliked state you can go the the hover by releasing in the button, or neutrl state by releasing outised the button.

    Hope this helps!
     
  7. Navimaster

    Navimaster Guest

    Interesting... I've never heard of them. I'll check out your tutorial and hopefully apply a working system to my buttons. Very helpful, thank you!
     
  8. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,398
    My i also point out it is extremely bad form to avoid objects and use magic numbers like you are doing!
    i strongly advise you use a separate object for each button! (if you want to move a button around now, you have tons of number to change D:)
     
  9. Navimaster

    Navimaster Guest

    Oh, hmm... so I shouldn't use Draw GUI to draw my UI? I guess I could, but like you said, it might be harder later on to do more complex things without objects.

    Alright then, I'll use objects for my buttons from now on. Thank you for the tips.
     
  10. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,398
    Create a button object, but have it be drawn in the draw gui event of that button.
     
  11. Zachari

    Zachari Guest

    Hello, I'm trying to solve another issue with my buttons/GUI and i found this thread, I just have a question...
    If i do this, the button doesn't react to mouse inputs because its actual coordinates don't correspond to where the gui is, How can you line your button up with the gui button?
     
    ParodyKnaveBob likes this.
  12. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,398
    rather than the mouse pressed events, do the check in the step event using device_mouse_x_to_gui and device_mouse_y_to_gui
     
  13. WoohooDaThird

    WoohooDaThird Member

    Joined:
    Sep 13, 2019
    Posts:
    1
    I know this thread is old but you can use the mouse_check_button_pressed(<button>) to check if a button was pressed. This means someone will have to hold down the button then release instead of just holding it down.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice