Plugins development - 02

This tutorial is the continuation of the plugin development part 1, we recommend to do the previous parts before this one.

Tutorial objectives

In this tutorial we will see how we can add events to our plugin to bind an action on user click.
The action will move the camera to the zenith in 1500 ms.

Plugin events

To add events to the plugin we have to do a few things:

Add the event to the plugin manifest.

Dispatch the event from the plugin code.

Test the event by binding an action in the json configuration.

The manifest

In this tutorial we will add an event to handle the user's click on our rectangle. To do that we need to add an events object that will define the events name associated by default to a null action. The manifest now looks like this with a new onClick event.

This is not mandatory but we prefix our events by the on keyword, that's why why called our event onClick.

Dispatch the event

Once we have declared our event in the manifest json file, we need to dispatch the event from the code. In our example we need to catch a pointer onClick event and re-dispatch it from the plugin events module. Below is the full commented code of the plugin.

Bind actions to the event

Now that our event is dispatched by the events module of our plugin, we can bind actions to it from our main json configuration! If you don't know what actions are, we recommend you read the actions related tutorials.

To add an action and bind it to our onClick event we need two add a few things to the json configuration:

So, we've added an action identified by action-0. This action will call the lookAt method of the camera to animate it to pitch 90 over 1500 milliseconds. Then we add the events object to the plugin instance configuration and bind the action-0 to our onClick event.

That's it! Now if you click on the rectangle, the camera will move to pitch 90! Of course you are free to bind any action you want to your events. And you can dispatch any events you want from your plugin as long as you've declared them in the manifest.