Game Development Community

TX missing Mouse management (NOW IMPLEMENTED)

by Giuseppe De Francesco · in Torque X 2D · 07/02/2010 (8:26 am) · 1 replies

Hey guys,

coding the input for my new title (wich will be Windows as well) I've just noticesd that the Mouse management is part missing and part simply wrong (maybe just placeholders?). So... implementing a proper mouse management. This will be committed in the SVN repo as soon it's ready (very soon!).

~Pino

P.S.: Marking this as 3.1.5 Bug as well.

About the author

In the software eng. field since 1981, in charge of R&D during last 10 years. IEEE Senior Member (and volunteer).


#1
07/03/2010 (12:44 pm)
Hey guys,

As promised the Windows mouse management has been fixed in the Core engine: you’ll find it in the Commit Revision 42 (www2.xp-dev.com/sc/change/77988/42) of the SVN repository.

I decided to keep it outside the InputMap system because I really couldn’t figure any Use Case justifying that decision, thus I’ve added a static class Mouse inside the InputManager ignoring the bits of existent not working code. This class is updated by the core engine itself. The whole thing is within a #if !XBOX block.

HOW-TO use it:

First off you need to remember that the Torque coordinate system is quite different from the basic XNA, so your very first step shall be to set the camera you’re using in your player’s movement manager or in your Game Manager as it suits you (in the sample I’m also preparing a sight cursor ):
#if !XBOX
  InputManager.Mouse.SceneCamera = _camera;
  InputManager.Mouse.ShowTheMouse = false;
  _cursor = TorqueObjectDatabase.Instance.CloneObject<T2DSceneObject>("TSight");
  TorqueObjectDatabase.Instance.Register(_cursor);
#endif

  1. Setting the SceneCamera property allows you to get the proper world coordinates for your cursor. Do not forget to set it back to null when you unload your level ;) If you don’t set the Scene Camera the class assumes that your scene size is just equal to the screen size.
  2. Setting ShowMouse to False makes the default cursor disappear so you can use your own object as mouse pointer.
Now you have all set to use it, just putting some code where you want to do something with it. Here an example from my shooter (in the ProcessTick of my MovementComponent):

#if !XBOX
    _cursor.Position = InputManager.Mouse.CurrentPosition;
    _cannon.TrackMountRotation = false;
    _cannon.Rotation = 
        T2DVectorUtil.AngleFromTarget(_cannon.Position, InputManager.Mouse.CurrentPosition);
    if (InputManager.Mouse.IsLeftButtonPressed)
    {
        if (!_fire.EffectPlaying) 
            _fire.PlayEffect(false); // shooting using particles&rsquo; collisions
    }
    else
    {
        if (_fire.EffectPlaying)
            _fire.StopEffect(true);
    }
#else
// XBOX GamePad management Here
#endif

Here’s the list of the Mouse properties (with comments):

/// <summary>
/// Current mouse position in Torque coordinates. This value is also computed
/// against the Camera is the user assignes it, otherwise it assumes a scene 
/// having the same dimension of the game screen.
/// </summary>
/// <remarks>
/// This value won't be updated if the user moves the mouse pointer outside the game
/// window.
/// </remarks>
public static Vector2 CurrentPosition

/// <summary>
/// The maximum amount of time in milliseconds that we allow the user
/// to keep a button down to raise a Click
/// </summary>
public static double ClickThreshold

/// <summary>
/// the value of the last Scroll Wheel activity
/// </summary>
public static int CurrentScrollWheelActivity

/// <summary>
/// True if the left button is down
/// </summary>
public static bool IsLeftButtonPressed

/// <summary>
/// True if the middle button is down
/// </summary>
public static bool IsMiddleButtonPressed

/// <summary>
/// True if the right button is down
/// </summary>
public static bool IsRightButtonPressed

/// <summary>
/// True if the user generated a Left Click
/// </summary>
public static bool LeftClickDetected

/// <summary>
/// True if the user generated a Right Click
/// </summary>
public static bool RightClickDetected

/// <summary>
/// True if the user generated a Middle Click
/// </summary>
public static bool MiddleClickDetected

/// <summary>
/// This should be set to the main scene camera to allow a proper 
/// computation of the Torque Coordinates of the Mouse.
/// </summary>
public static ISceneCamera SceneCamera

/// <summary>
/// Set to True by default. If you want to show your own mouse icon then set this to False.
/// </summary>
public static bool ShowTheMouse


I hope you’ll enjoy it ;)
Pino