Custom Operator Basics
In this example, we will be introducing you to the concept of custom operators and how to respond to events by creating a simple operator that highlights entities under the mouse cursor. Operators allow you to hook into various events provided by the web browser and perform well-defined actions based on user input.
- Create a basic operator skeleton
- Respond to events
- Use picking to find entity under cursor
- Select entities via the SelectionManager
Operator Class Skeleton
We start by creating another empty JS document called SelectOperator.js which will contain the JS code for the operator we are about to create. We will only be handling onMouseDown events, so we have created only a single event handler for now.
Add functionality from SelectOperator.js to your application by including it in your HTML page:
The next step is to perform some action on onMouseDown. In this example, we want to highlight the geometry under the mouse cursor. To accomplish this we are using the pickFromPoint() function which is on the view object.
The pickFromPoint function performs a ray-based collision with the geometry under the pick location within a given radius and returns the nodeid of the "best candidate" which is usually the entity closest to the camera on the pick location. This function requires a configuration object which determines which geometry types it should consider for performing the pick operation (faces, edges, points).
We also want to make sure that any existing selection is cleared out.
DOM Element for Operator Switching
In order to activate this new operator, we need to push it on the stack. The first step is to add another option in our DOM Select element:
Instantiating an Operator
A new operator must be first instantiated and then registered with the HOOPS Web Viewer before it can be used. Let's do this in the initEvents() function from the Menu.js file we set up in the previous section. Registering only makes the operator available to be used but does not activate it yet. It also returns a unique ID that we will use to activate the operator when the user switches to it.
When an operator is placed on the stack, user interaction with the Web Viewer will prompt the event to evaluate the stack. If the user action matches an operator on the stack, the corresponding event handler will be invoked. The traversal of the stack will stop once an action has been matched to an operator. By default, HOOPS Communicator enables the Selection and Navigate operators.
Switching to a New Operator
To switch to the new operator, we can activate it just like any of the standard operators by providing its ID to the OperatorManager. Let's modify the conditional code at the end of customOperatorSelect.onclick() (in Menu.js) so it supports the select operator:
Now you should be able to switch to your new operator via the menu and see the default highlighting in action.
Operators are a convenient programming paradigm for code in your application related to user interactions with the 3D window, but keep in mind that you can make calls to the HOOPS Web Viewer API from anywhere in your application and write your own operator-like functionality outside of the operator framework discussed here. The OperatorManager just exists to make your life easier – it is not required.