Forge 工具 handleButtonDown 和 handleButtonUp 函数未被调用

Forge tool handleButtonDown and handleButtonUp functions not getting called

我在 https://forge.autodesk.com/blog/custom-window-selection-forge-viewer-part-iii which is located at https://github.com/Autodesk-Forge/forge-rcdb.nodejs/blob/master/src/client/viewer.components/Viewer.Extensions.Dynamic/Viewing.Extension.SelectionWindow/Viewing.Extension.SelectionWindow.Tool.js as well as the documentation at https://developer.autodesk.com/en/docs/viewer/v2/reference/javascript/toolinterface/ 查看教程的示例代码 --- 这些函数中的大多数都在我的工具中被正确调用,例如 handleSingleClick、handleMouseMove、handleKeyDown 等等,但是其中两个它们没有被击中——handleButtonDown 和 handleButtonUp。我使用的是查看器版本 3.3.x,但我已更新为使用 4.0.x,认为这可能有助于解决问题,但两个版本都出现了同样的问题。感谢您的帮助。

以下代码块来自Autodesk.Viewing.ToolController#__invokeStack()_toolStack代表ToolController中激活的工具,method代表[=16=开始的回调函数],即 handleSingleClick、handleMouseMove、handleKeyDown、handleButtonDown、handleButtonUp 等

for( var n = _toolStack.length;  --n >= 0;  )
{
    var tool = _toolStack[n];

    if( tool[method] && tool[method](arg1, arg2) )
    {
        return true;
    }
}

根据我的经验,如果在您的自定义工具之前执行 handleButtonDownhandleButtonUp 等句柄函数并且 returned 为真,那么您的句柄将永远不会打电话。


幸运的是,Forge Viewer (v3.2) 开始为在 ToolController 中注册的自定义工具调用优先级机制。 ToolController会使用优先级编号对其中的工具进行排序,每个工具的优先级编号默认为0。您可以覆盖优先级,使您的工具在其他工具之前被命中,这样,添加函数 getPriority() 到 return 大于 0 的数字:

this.getPriority = function() {
   return 100;
};

我发现当使用 ES6 和 class 语法时,从 Autodesk.Viewing.ToolInterface 扩展你的工具将阻止覆盖正常工作,可能是因为它不是使用 prototype 实现的在查看器源代码中。

您可以简单地创建一个 class 并实现您的工具感兴趣的方法:

// KO: not working! 
class MyTool extends Autodesk.Viewing.ToolInterface {

     getName () {
         return 'MyTool'
     } 

     getNames () {

         return ['MyTool']
     }   

     handleButtonDown (event, button) {

         return false
     }
}

// OK 
class MyTool {

     getName () {
         return 'MyTool'
     } 

     getNames () {

         return ['MyTool']
     }    

     handleButtonDown (event, button) {

         return false
     }
}