Page 1 of 1

Injecting absolute mouse movements

Posted: Fri Oct 08, 2004 05:46
by ArmaDuck
It would be good to support injecting absolute mouse movements.

I need this because I want to use Window's mouse messages to manipulate the GUI which gives absolute positions. I have already added an injectMouseMoveAbsolute(...) method based off of injectMouseMove(...) to System so here is the code:

In CEGUISystem.h after injectMouseMove(...):

Code: Select all

   /*!
   \brief
      Method that injects an absolute mouse movement event into the system

   \param abs_x
      position the mouse moved to on the x axis.

   \param abs_y
      position the mouse moved to on the y axis.

   \return
      - true if the input was processed by the gui system.
      - false if the input was not processed by the gui system.
   */
   bool injectMouseMoveAbsolute(float abs_x, float abs_y);


In CEGUISystem.cpp after injectMouseMove(...):

Code: Select all

/*************************************************************************
   Method that injects an absolute mouse movement event into the system
*************************************************************************/
bool System::injectMouseMoveAbsolute(float abs_x, float abs_y)
{
   MouseEventArgs ma(NULL);
   MouseCursor& mouse = MouseCursor::getSingleton();

   ma.position.d_x = abs_x * d_mouseScalingFactor;
   ma.position.d_y = abs_y * d_mouseScalingFactor;
   ma.wheelChange = 0;

   // move the mouse cursor & update position in args.
   mouse.setPosition(ma.position);
   ma.position = mouse.getPosition();

   Window* dest_window = getTargetWindow(ma.position);

   // if there is no GUI sheet, then there is nowhere to send input
   if (dest_window != NULL)
   {
      if (dest_window != d_wndWithMouse)
      {
         if (d_wndWithMouse != NULL)
         {
            ma.window = d_wndWithMouse;
            d_wndWithMouse->onMouseLeaves(ma);
         }

         d_wndWithMouse = dest_window;
         ma.window = dest_window;
         dest_window->onMouseEnters(ma);
      }

      // ensure event starts as 'not handled'
      ma.handled = false;

      // loop backwards until event is handled or we run out of windows.
      while ((!ma.handled) && (dest_window != NULL))
      {
         ma.window = dest_window;
         dest_window->onMouseMove(ma);
         dest_window = dest_window->getParent();
      }

   }

   return ma.handled;
}

Injecting absolute mouse movements

Posted: Fri Oct 08, 2004 05:54
by gcarlton
This also does the job:

Code: Select all

MouseCursor::getSingleton().setPosition(x, y);
System::getSingleton().injectMouseMove(0.0f, 0.0f);

Not quite as elegant, but it works!

Injecting absolute mouse movements

Posted: Fri Oct 08, 2004 08:35
by CrazyEddie
It's a good idea, I'll put it on the list of things to add when I start adding things again :) I do think that the method would be better named "injectMousePosition" though.

The second code snippet posted by Geoff might actually be better, since it reduces code repetition.

Thanks for the suggestion,

CE.

Injecting absolute mouse movements

Posted: Fri Oct 08, 2004 17:55
by jwace81
I have a similar function for the C# port as well, although currently mine looks much more like ArmaDuck's. I think that I'll change it to be more like gcarlton's solution, and I'll make sure that the name is 'InjectMousePosition' before I commit it.
I needed it for my current work on the plugin for Chronos since the input doesn't always need to be fed to CEGUI.

J.W.

Injecting absolute mouse movements

Posted: Fri Oct 08, 2004 19:27
by ArmaDuck
gcarlton's solution does look a lot better! I'll switch my function and code over to your suggestions. Thanks!