Mouse and Touch Screen (Cursor)

Introduction

Gum supports reading from the mouse and touch screen for events. Both the mouse and touch screen report their actions through the Cursor class. Controls which respond to click events (such as Button and CheckBox) automatically read from the Cursor.

Code Example: Accessing the Cursor

GumService contains an instance of the Cursor class. The following code shows how to access the Cursor class and create rectangles when the Cursor detects a click.

protected override void Update(GameTime gameTime)
{
    GumUI.Update(gameTime);

    Cursor cursor = GumUI.Cursor;

    if(cursor.PrimaryClick)
    {
        ColoredRectangleRuntime rectangle = new ();
        rectangle.AddToRoot();
        rectangle.X = cursor.XRespectingGumZoomAndBounds();
        rectangle.Y = cursor.YRespectingGumZoomAndBounds();
        rectangle.Color = Color.Red;
    }

    base.Update(gameTime);
}
Rectangles created in response to Cursor clicks

Checking Control Over

The Cursor class reports information about what it is over which can be checked in events or in an Update call.

WindowOver

The WindowOver property returns the visual that the Cursor is over. The following code shows how to detect the Button that the Cursor is hovering over.

Cursor.WindowOver displaying the element that the cursor is over

Disabling the Cursor Globally

The Cursor instance reported by GumService can be replaced with a custom implementation of the ICursor interface. A custom ICursor class can be created to modify its behavior. For example, the following implementation disables all behavior:

This DisabledCursor class can be assigned to disable all Cursor actions as shown in the following code:

DisabledCursor used to prevent UI interaction

Last updated

Was this helpful?