Scripting MouseMove function in GUI... (Solved)

Started by IceMateria, Fri 19/08/2011 16:17:12

Previous topic - Next topic

IceMateria

Just curious if that's possible. Hotspots can use that function and I was wondering how I would go about scripting that for a GUI...

monkey0506

It's actually fairly simple to script a "mouse moves over GUI" function, although there's obviously nothing built-in for that:

Code: ags
// GlobalScript.asc

GUI *lastGUIMouseOver; // the last GUI that the mouse was over (null for none of course)

function gPanel_MouseMove()
{
  // ...
}

function gInventory_MouseMove()
{
  // ...
}

// ...

function repeatedly_execute()
{
  GUI *gat = GUI.GetAtScreenXY(mouse.x, mouse.y); // get the GUI the mouse is currently over
  if ((gat != null) && (gat != lastGUIMouseOver)) // if the mouse is over a GUI, but not the same one as the last loop...
  {
    // ...we've moved over a new GUI, call its MouseMove function
    if (gat == gPanel) gPanel_MouseMove();
    else if (gat == gInventory) gInventory_MouseMove();
    // ...
  }
  lastGUIMouseOver = gat; // store the GUI the mouse is currently over
}


The basic idea is that you just compare the GUI the mouse is currently over (if any) against the one it was over the previous game loop. If they're different, then the mouse has moved over a GUI.


monkey0506

I know you didn't ask, but for you or anyone reading this thread, if you want to do the opposite function (the mouse moves off of a GUI) it would work the same way, just call the appropriate function based on the current value of lastGUIMouseOver (before it's updated at the end of rep_exec, and in fact, you should call it from within the same if-block as the moves-over calls).

A lot of your game logic for things like this will probably end up in repeatedly_execute if there's not already an appropriate existing event. If you needed to do something like this during a blocking event (such as character speech or a dialog) then there's repeatedly_execute_always. Get to know these functions, you'll be seeing a lot of them if you're gonna be using AGS for very long. :=

Oh, and by the way, welcome to the forums!

SMF spam blocked by CleanTalk