This page has been translated automatically.
UnigineEditor
Interface Overview
Assets Workflow
Settings and Preferences
Working With Projects
Adjusting Node Parameters
Setting Up Materials
Setting Up Properties
Landscape Tool
Using Editor Tools for Specific Tasks
Extending Editor Functionality
Programming
Fundamentals
Setting Up Development Environment
UnigineScript
C++
C#
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine Tools
GUI
Double Precision Coordinates
API
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
Warning! This version of documentation is OUTDATED, as it describes an older SDK version! Please switch to the documentation for the latest SDK version.
Warning! This version of documentation describes an old SDK version which is no longer supported! Please upgrade to the latest SDK version.

Customizing Mouse Cursor and Behavior

Customizing Mouse

Customizing Mouse

This example shows how to customize mouse cursor appearance and change default mouse behavior in your application. It will help you to solve the following tasks:

  • Changing the default mouse cursor image
  • Toggling mouse cursor's visibility
  • Restricting mouse cursor movement to application window's area

Defining Mouse Behavior#

The mouse behavior is defined by assigning any of the following modes:

  • Grab - the mouse is grabbed when clicked (the cursor disappears and camera movement is controlled by the mouse). This mode is set by default.
  • Soft - the mouse cursor disappears after being idle for a short time period.
  • User - the mouse is not handled by the system (allows input handling by some custom module).

There are two ways to set the required mouse behavior mode:

  • In the Editor, by selecting the corresponding option in the Controls Settings.
  • Via code by adding the corresponding line to the logic (world or system logic, components, expressions, etc.).
Source code (C#)
ControlsApp.MouseHandle = Input.MOUSE_HANDLE.SOFT;
//or
ControlsApp.MouseHandle = Input.MOUSE_HANDLE.USER;

Customizing Mouse Cursor#

You can change the look of the mouse cursor in your application using any square RGBA8 image you want. This can be done by simply adding the following lines to your code (e.g. world's init() method):

Source code (C#)
// AppWorldLogic.cs

// loading an image for the mouse cursor
Image mouse_cursor = new Image("textures/cursor.png");

// resize the image to make it square
mouse_cursor.Resize(mouse_cursor.Width, mouse_cursor.Width);

// checking if our image is loaded successfully and has the appropriate format
if ((mouse_cursor.IsLoaded) && (mouse_cursor.Format == Image.FORMAT_RGBA8))
{
	// set the image for the OS mouse pointer
	App.setMouseCursor(mouse_cursor.getPtr(), mouse_cursor.Width);
	
	// show the OS mouse pointer
	App.setMouseShow(1);
}

// clearing pointer
mouse_cursor.clearPtr();
mouse_cursor = null;

Here is a sample RGBA8 image (32x32) you can use for your mouse cursor (download it and put it to the data/textures folder of your project):

Sample cursor image

Example Code#

Below you'll find the code that performs the following:

  • Sets a custom icon and title for the application window
  • Sets a custom mouse cursor
  • Switches between the following two mouse control modes by the ENTER key:
    • Camera control - the cursor is invisible and controls the camera (movement area restricted to the application window)
    • UI control - the cursor is visible and can be used to interact with application UI (movement area unrestricted)
  • Switches between restricted (only application window area) and unrestricted mouse movement mode by the SHIFT key

Insert the following code into the AppWorldLogic.cs file.

Notice
Unchanged methods of the AppWorldLogic class are not listed here, so leave them as they are.
Source code (C#)
// AppWorldLogic.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Unigine;

namespace UnigineApp
{
    class AppWorldLogic : WorldLogic
    {
        // auxiliary variables
        ControlsApp controls_app;
        Controls controls;
        Gui gui;

        // widgets
        WidgetLabel label;
        WidgetButton button;

        /// Method toggling the mouse cursor's state
        public void ToggleMouseCursor()
        {
            // getting current mouse state
            bool enabled = controls_app.MouseEnabled;

            // toggling movement restriction to the application window area
            App.SetMouseGrab(enabled ? 0 : 1);

            // toggling mouse interaction with GUI elements
            gui.MouseEnabled = enabled;

            // toggling mouse control
            controls_app.MouseEnabled = !enabled;
        }

        /*...*/

        public override bool Init()
        {
            /*...*/

            // enabling user mouse handling
			ControlsApp.MouseHandle = ControlsApp.MOUSE_HANDLE.USER;
			
			// initializing auxiliary variables
            controls_app = ControlsApp;
            controls = Game.Player.Controls;
            gui = Gui.Get();

            // preparing UI: creating a label and a button and adding them to the GUI
            label = new WidgetLabel(gui);
            button = new WidgetButton(gui, "BUTTON");
            button.SetPosition(10, 40);
            gui.AddChild(label, Gui.ALIGN_OVERLAP | Gui.ALIGN_TOP | Gui.ALIGN_LEFT);
            gui.AddChild(button, Gui.ALIGN_OVERLAP | Gui.ALIGN_TOP | Gui.ALIGN_LEFT);

            // setting a custom icon for the application window
            Image icon = new Image("textures/cursor.png");
            if ((icon.IsLoaded) && (icon.Format == Image.FORMAT_RGBA8))
                app.setIcon(icon.getPtr(), icon.Width);

            icon.clearPtr();
            icon = null;

            // set a custom title for the application window
            App.SetTitle("Custom Window Title");

            // loading an image for the mouse cursor
            Image mouse_cursor = new Image("textures/cursor.png");

            // resize the image to make it square
            mouse_cursor.Resize(mouse_cursor.Width, mouse_cursor.Width);
            if ((mouse_cursor.IsLoaded) && (mouse_cursor.Format == Image.FORMAT_RGBA8))
            {
                // set the image for the OS mouse pointer
                App.setMouseCursor(mouse_cursor.getPtr(), mouse_cursor.Width);

                // show the OS mouse pointer
                App.SetMouseShow(1);
            }

            // clearing pointer
            mouse_cursor.clearPtr();
            mouse_cursor = null;

            return true;
        }

        public override bool Update()
        {
            /*...*/

            // checking for STATE_USE (ENTER key by default) and toggling mouse cursor state "hidden restricted camera control" <-> "visible unrestricted UI control"
            if (controls.ClearState(Controls.STATE_USE) == 1)
                ToggleMouseCursor();

            // checking for STATE_RUN (SHIFT key by default) and toggling mouse cursor movement restriction to application window bounds
            if (controls.ClearState(Controls.STATE_RUN) == 1)
                app.setMouseGrab(1 - app.getMouseGrab());

            // updating cursor position info
            label.Text = String.Format("\n MOUSE COORDS: ({0}, {1})", app.getMouseX(), app.getMouseY());

            // showing or hiding mouse cursor depending on its current state
            App.setMouseShow(controls_app.MouseEnabled ? 0 : 1);
            return true;
        }

        /*...*/

        public override bool Shutdown()
        {
            //removing all widgets grom GUI
            for (int i = 0; i < gui.NumChildren; i++)
                gui.RemoveChild(gui.GetChild(i));

            // clearing pointers
            controls.clearPtr();
            controls = null;
            button.clearPtr();
            button = null;
            label.clearPtr();
            label = null;
            gui.clearPtr();
            gui = null;
            return true;
        }

        /*...*/
    }
}
Last update: 2020-04-10
Build: ()