Middle mouse button and clicks over GUI. [SOLVED]

Started by Mr Flibble, Tue 02/01/2007 21:04:37

Previous topic - Next topic

Mr Flibble

Edit: Thought I'd save a mod some work, and add in my own Solved tags.  :)

Alright, here's the thing. I have a GUI that I want to display with a middle button click. This works fine and dandy. But I also want to be able to middle click again to close the GUI. This doesn't work.

The debug console tells me that the click is registering as a click over a GUI, so I made a button in the centre of the GUI (where the cursor would be) and set it to close the GUI if it received a middle click. But it still doesn't work. Can anyone help me with this? I just want the middle click to toggle the GUI being open or closed..

Notes:

  • I tried switching the middle button for the right mouse button, made no difference, so I'm guessing its the "Mouse click over GUI" part that I'm being an idiot over.

  • In the middle, you'll notice my code for using the mouse wheel to alter a variable. It's primative and inefficient but it works :)  and I'm only including it for the sake of completionism.

  • Also, the game will have an option for playing without the middle button and mousewheel, because I know a lot of people do not have these. I still want to make it work for the people who do, though :)

Any help would be GREATLY appreciated, because I've been tussling with this one for ages now..

Code: ags

#sectionstart on_mouse_click  // DO NOT EDIT OR REMOVE THIS LINE
function on_mouse_click(MouseButton button) // called when a mouse button is clicked. button is either LEFT or RIGHT
  {
  if (IsGamePaused() == 1) // Game is paused, so do nothing (ie. don't allow mouse click)
    {
    }
  else if (button == eMouseLeft) 
    {
    ProcessClick(mouse.x,mouse.y, mouse.Mode);
    }
  else if (button == eMouseMiddle) // MIDDLE.. 
    {   
    if (gVerbwheel.Visible== true) {
      gVerbwheel.Visible=false;
      }
      else if (gVerbwheel.Visible== false) {
        gVerbwheel.Visible=true;
    }
    } 
  else if (button == eMouseWheelNorth) 
      {
     if ((GetGlobalInt(1)==0)) {
        SetGlobalInt(1,  1);
        }
      else if((GetGlobalInt (1) ==  1)) {
        SetGlobalInt(1,  2);
        }
      else if((GetGlobalInt(1) == 2)) {
        SetGlobalInt(1,  3);
        }
      else if((GetGlobalInt(1) == 3)) {
        SetGlobalInt(1,  4);
        }
      else if((GetGlobalInt(1) == 4)) {
        SetGlobalInt(1,  0);
        }
      }
  else if (button == eMouseWheelSouth)
       {
      if ((GetGlobalInt(1)==0)) {
         SetGlobalInt(1,  4);
         }
			 else if ((GetGlobalInt(1)==4)) {
			   SetGlobalInt(1,  3);
			   }
       else if ((GetGlobalInt(1) ==3)) {
				 SetGlobalInt(1,  2);
				 }
			 else if ((GetGlobalInt(1)==2)) {
         SetGlobalInt(1,  1);
         }
       else if ((GetGlobalInt(1)==1)) {
         SetGlobalInt(1,  0);
         }
        
         
 }
 }
 
    
  
#sectionend on_mouse_click  // DO NOT EDIT OR REMOVE THIS LINE


#sectionstart interface_click  // DO NOT EDIT OR REMOVE THIS LINE
function interface_click(int interface, int button) 
  {
  if (interface == VERBWHEEL) {
   if (button==0) {
     if (button==eMouseMiddle) {
    gVerbwheel.Visible=false;
  }
 }
 else {
   gVerbwheel.Visible=false;
   }
 }
}
#sectionend interface_click  // DO NOT EDIT OR REMOVE THIS LINE


Oh, and this may be useful too.
Code: ags

#sectionstart repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE
function repeatedly_execute() 
  {
  int mousexpos= mouse.x;
  int mouseypos= mouse.y;
  if (gVerbwheel.Visible==true) {
  gVerbwheel.SetPosition(mousexpos-22,mouseypos-21);
  }
  // put anything you want to happen every game cycle here
  }
#sectionend repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE


Thanks in advance for any hints you can offer.
Ah! There is no emoticon for what I'm feeling!

Khris

Add this to the rep_ex:

Code: ags
  if (mouse.IsButtonDown(eMouseMiddle) && gVerbwheel.Visible) {
    gVerbwheel.Visible=false;
    while (mouse.IsButtonDown(eMouseMiddle)) {
      Wait(1);
    }
  }

Mr Flibble

I was really happy that the solution might be so simple.

But I just got an error message when the game started, IsButtonDown only works with left and right clicks.

So what now? Is there an alternate way to fix this problem? I'd really like to use the middle button for this GUI, I think it will be really intuitive and easy to use. It's just the idea I had.. I want to make this work. I guess I could change it to the right button, but that's more of a last resort.

During my research I noticed that AGS's middle button support isn't heavily enforced, and it isnt AGS's fault. But.. I know this can be made to work!  :)
Ah! There is no emoticon for what I'm feeling!

GarageGothic

Well, you're checking for IsGamePaused() at the beginning of the function. If the GUI is set to pop-up modal, the game WILL be paused while it's displayed so the function won't run and thus the button click doesn't register.

Mr Flibble

The GUI is set to Normal. I did check if I had set it to Popup Modal (I do love it when a simple solution comes) but.. no luck.
Thanks anyway, man.

The IsGamePaused is in there by default, if I recall.
Ah! There is no emoticon for what I'm feeling!

GarageGothic

Too bad, I like easy solutions too :). It's still the only sensible solution I can see (since you're saying that it doesn't make any difference if you insert eMouseRight instead). Are you sure the game isn't paused for one reason or another? What if you move the "if (button == eMouseMiddle)" code to the very top of on_mouse_click?

For the sake of debugging, I would remove the interface_click code at least. It shouldn't be necessary to perform the action you want and may be contributing to the problem.

Khris

Here's the easy solution:
Set the GUI's Clickable property to "No".

If you need AGS to distuingish between middle clicks to the room and middle clicks to the GUI, leave Clickable set to "Yes", double click the GUI's On click property and click Ok (to accept the proposed name of the function). Now double click the GUI itself in the editor; this will open the global script at the newly created function which you should edit to look like this:
Code: ags
#sectionstart gVerbwheel_Click  // DO NOT EDIT OR REMOVE THIS LINE
function gVerbwheel_Click(GUI *theGui, MouseButton button) {
  if (button==eMouseMiddle) theGui.Visible=false;  
}
#sectionend gVerbwheel_Click  // DO NOT EDIT OR REMOVE THIS LINE

GarageGothic

#7
But isn't on_mouse_click called whether the GUI is clickable or not? Why even distinguish between whether the mouse is over the gui or not? When the gui is visible, it's always moved so that it's under the cursor as far as I can tell from the repeatedly execute script.

If it works, cool. But I'd still like to know why the original code wasn't.

Mr Flibble

Excellent.. with KrisMuc's latest version it works.

It didn't at first, but then I had the prescence of mind to delete the button which I had created aaaages ago (as referenced in original code snippets).

I also removed the interface_click stuff like you said Garage, to be on the safe side.

Anyway, once I had made these changes.. it worked beautifully! Thanks so much guys!
Ah! There is no emoticon for what I'm feeling!

Khris

GG:
Seems like any clickable GUI under the mouse will intercept the call to on_mouse_click. And this makes sense, too, because that way one can concentrate on handling room clicks in on_mouse_click.

It wasn't necessary to distinguish between whether the mouse is over the gui or not in this case, true, but it might be for another interface, so I mentioned it for future reference.

SMF spam blocked by CleanTalk