This page has been translated automatically.
视频教程
界面
要领
高级
实用建议
专业(SIM)
UnigineEditor
界面概述
资源工作流程
Version Control
设置和首选项
项目开发
调整节点参数
Setting Up Materials
设置属性
照明
Sandworm
使用编辑器工具执行特定任务
如何擴展編輯器功能
嵌入式节点类型
Nodes
Objects
Effects
Decals
光源
Geodetics
World Nodes
Sound Objects
Pathfinding Objects
Players
编程
基本原理
搭建开发环境
C++
C#
UnigineScript
UUSL (Unified UNIGINE Shader Language)
Plugins
File Formats
材质和着色器
Rebuilding the Engine Tools
GUI
双精度坐标
应用程序接口
Animations-Related Classes
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
VR-Related Classes
创建内容
内容优化
材质
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials

自定义鼠标光标和行为

Customizing Mouse

Customizing Mouse自定义鼠标

This example shows how to customize the 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 handling modes.切换鼠标光标的处理模式
  • Customizing your application's icon and title.自定义应用程序的图标和标题

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.Grab:点击时抓住鼠标(光标消失,相机移动由鼠标控制)。此模式默认设置为
  • Soft - the mouse cursor disappears after being idle for a short time period.Soft:鼠标光标在短时间内闲置后消失。
  • User - the mouse is not handled by the system (allows input handling by some custom module).User:鼠标不被系统处理(允许一些自定义模块处理输入)。

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.)通过代码将相应的行添加到逻辑(worldsystem逻辑,组件,表达式等)
源代码 (C++)
ControlsApp::setMouseHandle(Input::MOUSE_HANDLE_SOFT);
//or
ControlsApp::setMouseHandle(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. the world's init() method):您可以使用您想要的任何方的RGBA8图像在应用程序中更改鼠标光标的外观。 这可以通过简单地在代码中添加以下行来完成(例如在世界的init()方法):

源代码 (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
	Input::setMouseCursorCustom(mouse_cursor);

	// showing the OS mouse pointer
	Input::setMouseCursorSystem(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):这是一个示例RGBA8图像(32x32),您可以将其用于鼠标光标(下载并将其放到项目的 data/textures 文件夹中):

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 three handling modes on pressing the ENTER key.在按下ENTER键的三种处理模式之间切换

Insert the following code into the AppWorldLogic.cpp file.将以下代码插入到AppWorldLogic.cpp文件中。

注意
Unchanged methods of the AppWorldLogic class are not listed here, so leave them as they are.这里没有列出AppWorldLogic类的未更改方法,因此请保持原样。
源代码 (C++)
#include "AppWorldLogic.h"
#include "UnigineUserInterface.h"
#include "UnigineGui.h"
#include "UnigineGame.h"

using namespace Unigine;

// auxiliary variables
ControlsPtr controls;
GuiPtr gui;
String handling_mode[3] = { "GRAB", "SOFT", "USER" };

// widgets
WidgetLabelPtr label;
WidgetLabelPtr label2;
WidgetButtonPtr button;

AppWorldLogic::AppWorldLogic()
{
}

AppWorldLogic::~AppWorldLogic()
{
}

int AppWorldLogic::init()
{
	// initializing auxiliary variables
	controls = Game::getPlayer()->getControls();
	gui = Gui::getCurrent();
    EngineWindowPtr MWindow = WindowManager::getMainWindow();
	// setting a custom icon for the application window
	ImagePtr icon = Image::create("textures/cursor.png");
	if (icon->isLoaded() && icon->getFormat() == Image::FORMAT_RGBA8)
		MWindow->setIcon(icon);
	icon.clear();

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

	// preparing UI: creating 2 labels and a button and adding them to the GUI
	label = WidgetLabel::create(gui);
	label2 = WidgetLabel::create(gui);
	button = WidgetButton::create(gui, "BUTTON");
	label->setPosition(10, 20);
	label2->setPosition(10, 40);
	button->setPosition(10, 80);
	gui->addChild(label, Gui::ALIGN_OVERLAP | Gui::ALIGN_TOP | Gui::ALIGN_LEFT);
	gui->addChild(label2, Gui::ALIGN_OVERLAP | Gui::ALIGN_TOP | Gui::ALIGN_LEFT);
	gui->addChild(button, 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());
	// 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
		Input::setMouseCursorCustom(mouse_cursor);

		// showing the OS mouse pointer
		Input::setMouseCursorSystem(1);
	}

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

int AppWorldLogic::update()
{
	// checking for STATE_USE (ENTER key by default) and toggling mouse handling mode
	if (controls->clearState(Controls::STATE_USE) == 1) {
		switch (ControlsApp::getMouseHandle())
		{
		case Input::MOUSE_HANDLE_GRAB:
			// if the mouse is currently grabbed, we disable grabbing and restore GUI interaction
			if (Input::isMouseGrab())
			{
				Input::setMouseGrab(false);
				Gui::getCurrent()->setMouseEnabled(true);
			}
			Input::setMouseHandle(Input::MOUSE_HANDLE_SOFT);
			break;
		case Input::MOUSE_HANDLE_SOFT:
			Input::setMouseHandle(Input::MOUSE_HANDLE_USER);
			break;
		case Input::MOUSE_HANDLE_USER:
			Input::setMouseHandle(Input::MOUSE_HANDLE_GRAB);
			break;
		}
	}

	// updating info on the current cursor position and mouse handling mode 
	label->setText(String::format("\n MOUSE COORDS: %d, %d)", Gui::getCurrent()->getMouseX(), Gui::getCurrent()->getMouseY()));
	label2->setText(String::format("\n MOUSE HANDLE: %s", handling_mode[Input::getMouseHandle()].get()));

	return 1;
}

// ...
int AppWorldLogic::postUpdate()
{
	// ...
	return 1;
}

int AppWorldLogic::updatePhysics()
{
	// ...
	return 1;
}

int AppWorldLogic::shutdown()
{
	// ...
	return 1;
}

int AppWorldLogic::save(const Unigine::StreamPtr &stream)
{
	// ...
	UNIGINE_UNUSED(stream);
	return 1;
}

int AppWorldLogic::restore(const Unigine::StreamPtr &stream)
{
	// ...
	UNIGINE_UNUSED(stream);
	return 1;
}
最新更新: 2024-06-07
Build: ()