Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Duckbutcher on Thu 12/04/2018 10:53:16

Title: closing inventory window with a right click
Post by: Duckbutcher on Thu 12/04/2018 10:53:16
Hey everyone, hope you're all doing well.

I have a sierra style inventory GUI which starts invisible and appears with a right-click. I've used some code which works fine, which tells the engine to make the window appear with a right click, and then disappear with another right click, simple and efficient. Here's the code in full, in function mouse click in the global script.

Code (ags) Select

//yada yada left click functions

else if (button == eMouseRight) { //if right button is pressed
     if (gInventory.Visible == true) {
       gInventory.Visible = false; //if the gui is visible, turn it off - note this should happen in all situations if the GUI is visible!
       
     }
        if (player.ActiveInventory){  //if the player has an active item
        player.ActiveInventory = null; // gets rid of it
        mouse.Mode = eModeInteract;   //changes the pointer back to default
        }
        else gInventory.Visible = true; //otherwise, turn the inventory window on



So here's my problem. I open my inventory, select my item and right click out, closing the window, BUT only when I click on the GUI background image, or outside the GUI itself. If my pointer is in the actual inventory window (ie where the item icons are selected), it doesn't respond to a right click at all and I have to move it out of the box in order to shut the GUI. I'd really like to solve this problem for a little more fluidity and consistency!

Can anyone help me out?
Title: Re: closing inventory window with a right click
Post by: Khris on Thu 12/04/2018 11:10:25
Right-clicking a GUI control should be caught by on_event / eEventGUIMouseUp/Down.
Right-clicking an inventory item can be handled with on_mouse_click / eMouseRightInv, provided you've activated custom inv click handling in General settings.
Title: Re: closing inventory window with a right click
Post by: Duckbutcher on Thu 12/04/2018 11:30:21
Ah right, yeah I was using the default setting. I'll look into the custom inventory handling. Would this also apply to clicks on an "empty" inventory window slot, ie a click on blank space? Basically I want the right click to shut the inventory wherever the right mouse is clicked.

Thanks for pointing me in the right direction!
Title: Re: closing inventory window with a right click
Post by: dayowlron on Thu 12/04/2018 12:21:55
I think your problem is more with the logic.
Supposing the inventory window is visible then line 5 will make it invisible, but then it will drop down to the rest of the code and if there is not an Active Inventory item the inventory window will be visible again.
I think you might get the results you want by putting an "else" after the bracket on line 7.
Title: Re: closing inventory window with a right click
Post by: Khris on Thu 12/04/2018 12:38:23
Quote from: Duckbutcher on Thu 12/04/2018 11:30:21Would this also apply to clicks on an "empty" inventory window slot, ie a click on blank space?
I don't think so; AGS reliably sets game.inv_activated to the ID of the clicked item, so to handle a click on the blank part of the inv window, you need to fall back to on_event.

Try this:
void RightClick() {
  if (gInventory.Visible) gInventory.Visible = false;
  else if (player.ActiveInventory != null) player.ActiveInventory = null;
  else gInventory.Visible = true;
}

  // in on_mouse_click
  if (button == eMouseRight || button == eMouseRightInv) RightClick();

// set up on_event
MouseButton last;
void on_event (EventType event, int data) {
  if (event == eEventMouseDown) {
    for (int b = eMouseLeft; b <= eMouseMiddle; b++) if (mouse.IsButtonDown(b)) last = b;
  }
  if (event == eEventMouseUp) if (last == eMouseRight) RightClick();
}