Closing a GUI by clicking outside it [SOLVED]

Started by Akril15, Fri 11/01/2013 23:22:35

Previous topic - Next topic

Akril15

Hello,

I'm working on a game with a Sierra-style inventory window. However, instead of using a button to close the window, I want the window to close when the player clicks outside it. This is a stripped-down version of the code I'm using (in repeatedly_execute):
Quote
GUI *thegui = GUI.GetAtScreenXY(mouse.x,mouse.y);
if (thegui==null && gInventory.Visible==true && mouse.IsButtonDown(eMouseLeft)) {
gInventory.Visible=false;
}

This method almost works, except for one problem: Clicking outside the window does close it, but it also runs an interaction depending on what hotspot/object I clicked on and what mouse mode is currently active -- e.g., if the Look mode is active and I happen click on the player character when I close the GUI, the game closes the GUI and runs the player charcter's Look interaction.

How do I stop the game from doing this?

Ghost

#1
I assume you're using something like THIS to process mouse clicks- in on_mouse_click:
Code: ags

if (button == eMouseLeft) {
    ProcessClick(mouse.x, mouse.y, mouse.Mode );
  }


Try changing that to
Code: ags

if (button == eMouseLeft && !gInventory.Visible) {
    ProcessClick(mouse.x, mouse.y, mouse.Mode );
  }


That *should* do the trick...

[edit]
If you're using left and right clicks, you'll want to alter BOTH conditions ;)

Khris

Try this:
At the very start of on_mouse_click, add:
Code: ags
  if (gInventory.Visible && (button == eMouseLeft || button == eMouseRight)) {
    gInventory.Visible = false;
    return;
  }

(Also comment out your code in rep_ex)

MurrayL

#3
Another really simple solution: make the GUI the same size as the game screen, and add transparent borders to the background image you're using. Now you can use the regular OnClick event for the GUI to close it.

The only recommended extra work would be to add some simple checks to only close the GUI if the mouse is outside the 'active' rectangle where the visible GUI lives (super easy stuff - if((mouse.x<20 || mouse.x>620) && (mouse.y<20 || mouse.y>420), for example). It'll still work without the extra checks, but in my experience it means there's a tendency for the player to accidentally close the GUI by mis-clicking.

Akril15

Khris' solution did the trick. It didn't work at first, but then I realized that I just needed to delete my old code in rep_exec. :tongue:

Thank you, Khris!

SMF spam blocked by CleanTalk