Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Akril15 on Fri 11/01/2013 23:22:35

Title: Closing a GUI by clicking outside it [SOLVED]
Post by: Akril15 on Fri 11/01/2013 23:22:35
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?
Title: Re: Closing a GUI by clicking outside it
Post by: Ghost on Fri 11/01/2013 23:32:50
I assume you're using something like THIS to process mouse clicks- in on_mouse_click:
Code (ags) Select

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


Try changing that to
Code (ags) Select

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 ;)
Title: Re: Closing a GUI by clicking outside it
Post by: Khris on Fri 11/01/2013 23:34:27
Try this:
At the very start of on_mouse_click, add:
Code (ags) Select
  if (gInventory.Visible && (button == eMouseLeft || button == eMouseRight)) {
    gInventory.Visible = false;
    return;
  }

(Also comment out your code in rep_ex)
Title: Re: Closing a GUI by clicking outside it
Post by: MurrayL on Fri 11/01/2013 23:38:45
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.
Title: Re: Closing a GUI by clicking outside it
Post by: Akril15 on Sat 12/01/2013 19:39:21
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!