This page has been translated automatically.
UnigineEditor
Interface Overview
Assets Workflow
Settings and Preferences
Adjusting Node Parameters
Setting Up Materials
Setting Up Properties
Landscape Tool
Using Editor Tools for Specific Tasks
FAQ
编程
Fundamentals
Setting Up Development Environment
UnigineScript
C++
C#
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine and Tools
GUI
Double Precision Coordinates
应用程序接口
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
CIGI Client Plugin
Rendering-Related Classes
注意! 这个版本的文档是过时的,因为它描述了一个较老的SDK版本!请切换到最新SDK版本的文档。
注意! 这个版本的文档描述了一个不再受支持的旧SDK版本!请升级到最新的SDK版本。

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

Using Defines to Control Mouse Behavior#

The mouse is handled by the system script and there are three modes available:

  • Default - the mouse is grabbed when clicked (the cursor disappears and camera movement is controlled by the mouse).
  • MOUSE_SOFT (enabled by the corresponding define) - the mouse cursor disappears after being idle for a short time period.
  • MOUSE_USER (enabled by the corresponding define) - the mouse is not handled by the system (allows input handling by some custom module).

Basically there are two ways you can use these defines:

  • Add the following line
    Source code (UnigineScript)
    #define MOUSE_SOFT
    (or the same for MOUSE_USER) to your system script file (unigine.usc)
  • Add an extern_define startup command-line option. You can either set in UNIGINE SDK Browser or manually via the command line:
    Shell commands
    your_app_name* -extern_define MOUSE_SOFT
    (or the same for MOUSE_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++)
// loading an image for the mouse cursor
ImagePtr mouse_cursor = Image::create("textures/cursor.png");

// resizing the image to make it square
mouse_cursor->resize(mouse_cursor->getWidth(), mouse_cursor->getWidth());

// checking if our image is loaded successfully and has the appropriate format
if (mouse_cursor->isLoaded() && mouse_cursor->getFormat() == Image::FORMAT_RGBA8)
{
	// setting the image for the OS mouse pointer
	app->setMouseCursor(mouse_cursor->getPixels(), mouse_cursor->getWidth());
		
	// showing the OS mouse pointer
	app->setMouseShow(1);
}

// clearing pointer
mouse_cursor.clear();

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.cpp file.

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

#include "UnigineApp.h"
#include "UnigineUserInterface.h"
#include "UnigineGui.h"
#include "UnigineGame.h"

using namespace Unigine;

// auxiliary variables
App *app;
ControlsApp *controls_app;
ControlsPtr controls;
GuiPtr gui;

// widgets
WidgetLabelPtr label;
WidgetButtonPtr button;

/*...*/
			
/// function toggling the mouse cursor's state
void ToggleMouseCursor()
{
	// getting current mouse state
	int enabled = controls_app->isMouseEnabled();

	// toggling movement restriction to the application window area
	app->setMouseGrab(1 - enabled);
	
	// toggling mouse interaction with GUI elements
	gui->setMouseEnabled(enabled); 

	// toggling mouse control
	controls_app->setMouseEnabled(1 - enabled);
} 

int AppWorldLogic::init()
{
	/*...*/
	
	// initializing auxiliary variables
	app = App::get();
	controls_app = ControlsApp::get();
	controls = Game::get()->getPlayer()->getControls();
	gui = Gui::get();

	// seting a custom icon for the application window
	ImagePtr icon = Image::create("textures/cursor.png");
	if (icon->isLoaded() && icon->getFormat() == Image::FORMAT_RGBA8)
		app->setIcon(icon->getPixels2D(), icon->getWidth());
	icon.clear();

	// seting a custom title for the application window
	app->setTitle("Custom Window Title");

	// preparing UI: creating a label and a button and adding them to the GUI
	label = WidgetLabel::create(gui);
	button = WidgetButton::create(gui, "BUTTON");
	button->setPosition(10, 40);
	gui->addChild(label->getWidget(), Gui::ALIGN_OVERLAP | Gui::ALIGN_TOP | Gui::ALIGN_LEFT);
	gui->addChild(button->getWidget(), Gui::ALIGN_OVERLAP | Gui::ALIGN_TOP | Gui::ALIGN_LEFT);


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

	// resizing the image to make it square
	mouse_cursor->resize(mouse_cursor->getWidth(), mouse_cursor->getWidth());
	if (mouse_cursor->isLoaded() && mouse_cursor->getFormat() == Image::FORMAT_RGBA8)
	{
		// setting the image for the OS mouse pointer
		app->setMouseCursor(mouse_cursor->getPixels(), mouse_cursor->getWidth());
		
		// showing the OS mouse pointer
		app->setMouseShow(1);
	}

	// clearing pointer
	mouse_cursor.clear();
	return 1;
}

int AppWorldLogic::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(!app->getMouseGrab());

	// updating cursor position info
	label->setText(String::format("\n MOUSE COORDS: %d, %d)", app->getMouseX(), app->getMouseY()));

	// showing or hiding mouse cursor depending on its current state
	app->setMouseShow(!controls_app->isMouseEnabled());
	return 1;
}

/*...*/

int AppWorldLogic::shutdown()
{

	//removing all widgets grom GUI
	for (int i = 0; i < gui->getNumChildren(); i++)
		gui->removeChild(gui->getChild(i));

	// clearing pointers
	controls.clear();
	button.clear();
	label.clear();
	gui.clear();

	return 1;
}

Let us also use the following option to enable user mouse handling: -extern_define MOUSE_USER

Last update: 2020-01-14
Build: ()