事件处理回调
Callback is a function wrapper representing a pointer to static and member functions which are expected to be executed with specified parameters at a certain moment. Callback can be passed as an argument to other function.回调是一个函数包装,表示指向静态和成员函数的指针,这些函数预期在特定时刻使用指定参数执行。回调可以作为参数传递给其他函数。
Unigine C# API lets you specify function delegates as callbacks to handle events. Unigine C#API使您可以将函数委托指定为处理事件的回调。
void node_property_added(Node n, Property property)
{
/* .. */
}
node.AddCallback(Node.CALLBACK_PROPERTY_NODE_ADD, node_property_added);
You can also use lambda expressions for the same event handling purpose:您也可以将lambda表达式用于相同的事件处理目的:
widget_button.AddCallback(Gui.CLICKED, () => Log.Message("Button pressed\n"));
Practical Use实际使用#
Callbacks are widely used in event handling. A number of Unigine API members have several predefined events which can be handled by using callbacks in certain cases.回调在事件处理中被广泛使用。许多Unigine API成员具有几个预定义的事件,在某些情况下可以使用回调来处理。
Triggers扳机#
Triggers are used to detect changes in nodes position or state. Unigine offers three types of built-in triggers:触发器用于检测节点位置或状态的变化。 Unigine提供了三种类型的内置触发器:
- NodeTrigger fires callbacks when the trigger node is enabled or the trigger node position has changed. NodeTrigger在触发节点启用或触发节点 position 更改时触发回调。
- WorldTrigger fires callbacks when any node (collider or not) gets inside or outside of it. WorldTrigger在任何节点(是否有对撞机)获取其 inside 或 outside 时触发回调。
- PhysicalTrigger fires callbacks when physical objects get inside or outside of it. PhysicalTrigger在物理对象获取 inside 或 outside 时触发回调。
Here is a simple WorldTrigger usage example:这是一个简单的WorldTrigger用法示例:
using Unigine;
namespace UnigineApp
{
class AppWorldLogic : WorldLogic
{
// implement the enter callback
void enter_callback(Node node)
{
Log.Message("\nA node named {0} has entered the trigger\n", node.Name);
}
// implement the leave callback
void leave_callback(Node node)
{
Log.Message("\nA node named {0} has left the trigger\n", node.Name);
}
WorldTrigger trigger;
int enter_callback_id;
public override bool Init()
{
// create a world trigger
trigger = new WorldTrigger(new vec3(3.0f));
// add the enter callback to be fired when a node enters the world trigger
//and keep its id to be used to remove the callback when necessary
enter_callback_id = trigger.AddEnterCallback(enter_callback);
// add the leave callback to be fired when a node leaves the world trigger
trigger.AddLeaveCallback(leave_callback);
return 1;
}
}
}
To remove the callbacks use the following code:要删除回调,请使用以下代码:
// remove the callback by using its id
trigger.RemoveEnterCallback(enter_callback_id);
// clear all leave callbacks
trigger.ClearLeaveCallbacks();
也可以看看
- C++ API示例 ( <UnigineSDK>/source/csharp/samples/Api/Nodes/NodeTrigger ).
- C++ API示例 ( <UnigineSDK>/source/csharp/samples/Api/Nodes/WorldTrigger ).
- C++ API示例 ( <UnigineSDK>/source/csharp/samples/Api/Nodes/PhysicalTrigger ).
Widgets小部件#
The widgets base class Widget allows registering callbacks for events defined in the GUI class. The following example shows how to create a WidgetButton and register a callback function for the CLICKED event:小部件基类Widget允许注册GUI类中定义的事件的回调。下面的示例演示如何创建WidgetButton并注册CLICKED事件的回调函数:
// getting the system GUI
Gui gui = Gui.Get();
// creating a button widget and setting its caption
WidgetButton widget_button = new WidgetButton(gui, "Press me");
// rearranging button size
widget_button.Arrange();
// setting button position
widget_button.SetPosition(10,10);
// setting a lambda function to handle CLICKED event
widget_button.AddCallback(Gui.CLICKED, () => Log.Message("Button pressed\n"));
// adding the created button widget to the system GUI
gui.AddChild(widget_button, Gui.ALIGN_OVERLAP | Gui.ALIGN_FIXED);
也可以看看
C# API samples located in the <UnigineSDK>/source/csharp/samples/Api/Widgets folder.位于<UnigineSDK>/source/csharp/samples/Api/Widgets文件夹中的C#API示例。
Physics物理#
You can track certain events of the physics-related Bodies and Joints:您可以跟踪与物理学相关的实体和关节的某些事件:
- Body.AddFrozenCallback() to track an event when a body freezes. Body.AddFrozenCallback()跟踪物体冻结时的事件。
- Body.AddPositionCallback() to track an event when a body changes its position. Body.AddPositionCallback()用于在主体更改其位置时跟踪事件。
- Body.AddContactEnterCallback() to track an event when a contact emerges (body starts touching another body or collidable surface). Body.AddContactEnterCallback()跟踪出现接触时的事件(身体开始接触另一个身体或可碰撞的表面)。
- Body.AddContactLeaveCallback() to track an event when a contact ends (body stops touching another body or collidable surface). Body.AddContactLeaveCallback()用来跟踪接触结束时的事件(身体停止接触另一个身体或可碰撞的表面)。
- Body.AddContactsCallback() to get all contacts of the body including new ones (enter) and the ending ones (leave). Leave contacts are removed after the callback execution stage, so this is the only point where you can still get them. Body.AddContactsCallback()获取身体的所有接触,包括新接触(输入)和结束接触(离开)。离开联系人在回调执行阶段之后被删除,因此这是仍然可以获取它们的唯一点。
- Joint.AddBrokenCallback() to track an event when a joint breaks. Joint.AddBrokenCallback()跟踪关节断开时的事件。
The following sample shows the way of registering callbacks for a BodyRigid and change the color of a mesh depending on its state:下面的示例显示了为BodyRigid注册回调并根据其状态更改网格颜色的方法:
// create a box static mesh
Mesh mesh = new Mesh();
mesh.AddBoxSurface("box_0", new vec3(1.0f));
ObjectMeshStatic node = new ObjectMeshStatic(mesh);
node.SetMaterial("mesh_base", "*");
node.Position = new dvec3(0, 0, 5.0f);
// add a rigid body
BodyRigid body = new BodyRigid(node);
// register callbacks for events by using lambdas
body.AddFrozenCallback(b => b.GetObject().SetMaterialParameterFloat4("albedo_color", new vec4(1.0f, 0.0f, 0.0f, 1.0f), 0));
body.AddPositionCallback(b => b.GetObject().SetMaterialParameterFloat4("albedo_color", new vec4(0.0f, 0.0f, 1.0f, 1.0f), 0));
body.AddContactEnterCallback((b, num) => b.GetObject().SetMaterialParameterFloat4("albedo_color", new vec4(1.0f, 1.0f, 0.0f, 1.0f), 0));
// add a shape to the body
ShapeBox shape = new ShapeBox(body, new vec3(1.0f));
也可以看看
A C# API sample located in the <UnigineSDK>/source/csharp/samples/Api/Physics/BodyCallbacks folder.位于<UnigineSDK>/source/csharp/samples/Api/Physics/BodyCallbacks文件夹中的C#API示例。
Properties物产#
Callback functions can be used to determine actions to be performed when adding or removing node and surface properties as well as when swapping node properties. Here is an example demonstrating how to track adding a node property via callbacks:回调函数可用于确定在添加或删除节点和曲面属性以及交换节点属性时要执行的操作。这是一个演示如何通过回调跟踪添加节点属性的示例:
using Unigine;
namespace UnigineApp
{
class AppWorldLogic : WorldLogic
{
// implement the enabled callback
void node_property_added(Node n, Property property)
{
Log.Message("Property \"{0}\" was added to the node named \"{1}\".\n", property.Name, n.Name);
// ...
}
Node node;
public override bool Init()
{
// somewhere in the code
Properties properties = Engine.properties;
// inheriting a new property named "my_prop" from the base property "node_base"
properties.FindManualProperty("node_base").Inherit("my_prop");
// setting our callback function on adding a node property
node.AddCallback(Node.CALLBACK_PROPERTY_NODE_ADD, node_property_added);
// adding the property named "my_prop" to the node
node.AddProperty("my_prop");
return true;
}
}
}
You can add callbacks to track any changes made to a property and its parameters and perform certain actions.您可以添加回调以跟踪对属性及其参数所做的任何更改并执行某些操作。
The example below shows how to add a callback to track changes of property parameters and report the name of the property and the changed parameter (suppose we have a manual property named my_prop with an integer parameter named my_int_param).下面的示例演示如何添加回调以跟踪属性参数的更改,并报告属性的名称和更改后的参数(假设我们有一个名为my_prop的手动属性,其整数参数为my_int_param)。
using Unigine;
namespace UnigineApp
{
class AppWorldLogic : WorldLogic
{
// implement the parameter changed callback
void parameter_changed(Property property, int num)
{
Log.Message("Parameter \"{0}\" of the property \"{1}\" has changed its value.\n", property.GetParameterPtr(num).Name, property.Name);
// ...
}
public override bool Init()
{
// somewhere in the code
// getting a manual property named "my_prop" via the Property Manager
Property property = Properties.FindManualProperty("my_prop");
// setting our callback function on parameter change
property.AddCallback(Property.CALLBACK_PARAMETER_CHANGED, parameter_changed);
// changing the value of the "my_int_param" parameter
property.SetParameterInt(property.FindParameter("my_int_param"), 3);
return true;
}
}
}
You can also add callbacks to the Properties manager to track any changes made to any property and perform certain actions:您还可以将回调添加到Properties管理器,以跟踪对任何属性所做的任何更改并执行某些操作:
public void property_removed(Property property)
{
Log.Message("Property \"{0}\" was removed.\n", property.getName());
// ...
}
// somewhere in the code
Properties properties = Engine.properties;
// inheriting a new property named "my_prop" from the base property "surface_base"
Property property = properties.FindManualProperty("surface_base").Inherit("my_prop");
// setting our callback function on property removal
properties.AddCallback(Properties.CALLBACK_REMOVED, property_removed);
// removing the property named "my_prop"
properties.RemoveProperty(properties.FindProperty("my_prop").GetGUID());
See Also也可以看看#
These are not all usage examples of event handling callbacks. The following list contains more API members supporting event handling:这些并不是事件处理回调的所有用法示例。以下列表包含更多支持事件处理的API成员:
- UserInterface - for handling events from widgets created by loading a UI file. UserInterface-用于处理通过加载UI文件创建的小部件中的事件。
- Render - callback functions can be used to get access to buffers and matrices at intermediate stages of the rendering sequence. Render-回调函数可用于在渲染序列的中间阶段访问缓冲区和矩阵。
- Console supports adding a callback function that will be executed when a text is output to the console. Console支持添加在文本输出到控制台时将执行的回调函数。
- EditorLogic - a set of editor callback functions can be overridden for certain purposes. EditorLogic-出于某些目的,可以覆盖一组编辑器回调函数。
- ComponentBase - there may be performed certain actions on destroying a component. ComponentBase-在销毁组件时可能会执行某些操作。
- AsyncQueue - сallback functions can be used to determine actions to be performed when certain resources are loaded. AsyncQueue-所有版本的函数都可以用来确定在加载某些资源时要执行的操作。
- WorldSplineGraph provides a set of callbacks for handling actions on editing points and segments. WorldSplineGraph提供了一组回调,用于处理对编辑点和线段的操作。
- Viewport - callback functions can be used to get access to buffers and matrices at intermediate stages of the rendering sequence. Viewport-回调函数可用于在渲染序列的中间阶段访问缓冲区和矩阵。
Plugins:插件: