Search code examples
autodesk-forgeforge

Forge Viewer Extension for Toolbar: How to add a custom combox


I am trying to add a custom combobox to the toolbar in the forge viewer. Below is the code for it. I am able to successfully able to add buttons and they are functional. But combobox is not. It adds a combobox but it does show the fly out menu when I click on it. Not sure what I am doing wrong. help!

function BuildingToolbarExtension(viewer, options) {
    Autodesk.Viewing.Extension.call(this, viewer, options);
}

BuildingToolbarExtension.prototype = Object.create(Autodesk.Viewing.Extension.prototype);
BuildingToolbarExtension.prototype.constructor = BuildingToolbarExtension;

BuildingToolbarExtension.prototype.load = function () {
    // Set background environment to "Infinity Pool"
    // and make sure the environment background texture is visible
    this.viewer.setLightPreset(6);
    this.viewer.setEnvMapBackground(true);

    // Ensure the model is centered
    //this.viewer.fitToView();

    return true;
};

BuildingToolbarExtension.prototype.unload = function () {
    // nothing yet
    if (this.subToolbar) {
        this.viewer.toolbar.removeControl(this.subToolbar);
        this.subToolbar = null;
    }
};

BuildingToolbarExtension.prototype.onToolbarCreated = function (toolbar) {
    alert('TODO: customize Viewer toolbar');
    var viewer = this.viewer;

    // Button 1
    var button1 = new Autodesk.Viewing.UI.Button('show-env-bg-button');
    button1.onClick = function (e) {
        viewer.setEnvMapBackground(true);
    };
    button1.addClass('show-env-bg-button');
    button1.setToolTip('Show Environment');

    // Button 2
    var button2 = new Autodesk.Viewing.UI.Button('hide-env-bg-button');
    button2.onClick = function (e) {
        viewer.setEnvMapBackground(false);
    };
    button2.addClass('hide-env-bg-button');
    button2.setToolTip('Hide Environment');

    var comboButton = new Autodesk.Viewing.UI.ComboButton('buildings');
    comboButton.setToolTip('buildings');
    this.floors = new Autodesk.Viewing.UI.ControlGroup('my-custom-toolbar1');
    this.floors.addControl(button1);
    this.floors.addControl(button2);
    comboButton.addControl(this.floors);
    comboButton._isCollapsed = true;
    comboButton.onClick = function (e) {

        this.setCollapsed(false);
    }
    
    
    // SubToolbar
    this.subToolbar = new Autodesk.Viewing.UI.ControlGroup('my-custom-toolbar');
    this.subToolbar.addControl(button1);
    this.subToolbar.addControl(button2);
    this.subToolbar.addControl(comboButton);
    toolbar.addControl(this.subToolbar);
};

Autodesk.Viewing.theExtensionManager.registerExtension('BuildingToolbarExtension', BuildingToolbarExtension);

enter image description here


Solution

  • The ControlGroup is unnecessary in your case, please refer the following to add buttons to ComboButton

    var comboButton = new Autodesk.Viewing.UI.ComboButton('buildings');
    comboButton.setToolTip('buildings');
    
    // Button 1
    var button1 = new Autodesk.Viewing.UI.Button('show-env-bg-button');
    button1.onClick = function (e) {
        viewer.setEnvMapBackground(true);
    };
    button1.addClass('show-env-bg-button');
    button1.setToolTip('Show Environment');
    comboButton.addControl(button1);
    
    // Button 2
    var button2 = new Autodesk.Viewing.UI.Button('hide-env-bg-button');
    button2.onClick = function (e) {
        viewer.setEnvMapBackground(false);
    };
    button2.addClass('hide-env-bg-button');
    button2.setToolTip('Hide Environment');
    comboButton.addControl(button2);
    

    Here are snapshots:

    • Before opening enter image description here

    • After opening enter image description here