FrodeTo
FrodeTo

Reputation: 83

Prevent zoom in Forge viewer when clicking in Model Browser

There has been a change in the click behavior in the model browser from version 2 to version 3 of the Forge Viewer. In v2, a single click would select the elements and a double click would zoom to the selected elements. In v3, a single click will zoom to the elements. Sometimes this is great, but often it would be nice to disable this behavior. Is there an easy way to do this today? And if not, could it be possible to add a disableZoomOnSelection function to the viewer API?

I know that the eyes in the browser will take care of the show and hide elements, but it’s very easy to klick in the three by accident and suddenly the viewer zoom without the user intention.

Regards Frode

Upvotes: 0

Views: 1182

Answers (2)

Jaime Rosales
Jaime Rosales

Reputation: 1288

The model browser receives an options object in its constructor. There, you can specify the actions for different events (click, clickCtrl, clickShift, etc).

To set the old behavior you can try the following:

var options = {};

options.docStructureConfig = {
  "click": {
    "onObject": ["isolate"]
  },
  "clickCtrl": {
    "onObject": ["toggleVisibility"]
  }
 };

 NOP_VIEWER.setModelStructurePanel(new ave.ViewerModelStructurePanel(NOP_VIEWER, "", options));

NOP_VIEWER can be replaced with your own viewer variable.

Upvotes: 0

Felipe
Felipe

Reputation: 4375

I dig that code for you looking at the implementation of the ViewerModelStructurePanel that I was exposing in that article: Supporting multiple models in the new ModelStructurePanel

Events that occur in the tree are mapped to predefined actions through the options.docStructureConfig object, so the workaround is to instantiate a new ViewerModelStructurePanel with the desired options:

viewer.addEventListener(Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT, () => {

    var options = {
      docStructureConfig: {
          // go with default, which is viewer.select(node)
          //  click: {
          //    onObject: ["toggleOverlayedSelection"]
          //},
          clickShift: {
            onObject: ["toggleMultipleOverlayedSelection"]
        },
          clickCtrl: {
          onObject: ["toggleMultipleOverlayedSelection"]
        }
      }
    }

    var customModelStructurePanel =
      new Autodesk.Viewing.Extensions.ViewerModelStructurePanel(
        viewer, 'Browser', options)

    viewer.setModelStructurePanel(customModelStructurePanel)
  })

The double-click however is not linked to an event in the current implementation, so for a more powerful customization I would recommend you replace the whole implementation by a custom one as exposed in the article and implement desired action handlers. I implemented it as drop-in replacement, so in that case you just need to include it to your html after the viewer script and don't have to replace the model browser in OBJECT_TREE_CREATED_EVENT

Upvotes: 0

Related Questions