為Autodesk Viewer添加自訂工具條的更好方法

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   java   使用   os   

上一篇文章中我介紹了使用Autodesk Viewer的UI API來給viewer添加自訂工具條的方法,看起來很簡單是吧。不過有個問題,就是關於自訂工具條的資訊(包括按鈕的文本、表徵圖、樣式、callback等等)全都散落在代碼中,如果要添加或者修改的話,得特別小心的掃描碼,非常容易出錯。有沒有更好的辦法呢?這篇文章就來介紹一下。

既然關於Toolbar button等京城需要更改的部分散落到各處不方便維護,那我就把他們集中到一起獨立出來。於是我寫了個自訂的toolbarConfig對象,採用JSON格式,剛好符合JavaScript的文法,如果我需要添加或者修改工具條或按鈕,只需要修改這個config對象就可以了:

/////////////////////////////////////////////////////////////////////
// custom toobar config
var toolbarConfig = {
    ‘id‘: ‘toolbar_id_1‘,
    ‘containerId‘: ‘toolbarContainer‘,
    ‘subToolbars‘: [
        {
            ‘id‘: ‘subToolbar_id_non_radio_1‘,
            ‘isRadio‘: false,
            ‘visible‘: true,
            ‘buttons‘: [
                {
                    ‘id‘: ‘buttonRotation‘,
                    ‘buttonText‘ : ‘Rotation‘,
                    ‘tooltip‘: ‘Ratate the model at X direction‘,
                    ‘cssClassName‘: ‘glyphicon glyphicon glyphicon-play-circle‘,
                    ‘iconUrl‘ :‘Images/3d_rotation.png‘,
                    ‘onclick‘: buttonRotationClick
                },
                {
                    ‘id‘: ‘buttonExplode‘,
                    ‘buttonText‘: ‘Explode‘,
                    ‘tooltip‘: ‘Explode the model‘,
                    ‘cssClassName‘: ‘‘,
                    ‘iconUrl‘: ‘Images/explode_icon.jpg‘,
                    ‘onclick‘: buttonExplodeClick
                }

            ]
        },
        {
            ‘id‘: ‘subToolbar_id_radio_1‘,
            ‘isRadio‘: true,
            ‘visible‘: true,
            ‘buttons‘: [
                {
                    ‘id‘: ‘radio_button1‘,
                    ‘buttonText‘: ‘radio_button1‘,
                    ‘tooltip‘: ‘this is tooltip for radio button1‘,
                    ‘cssClassName‘: ‘‘,
                    ‘iconUrl‘: ‘‘,
                    ‘onclick‘: radioButton1ClickCallback
                },
                {
                    ‘id‘: ‘radio_button2‘,
                    ‘buttonText‘: ‘radio_button2‘,
                    ‘tooltip‘: ‘this is tooltip for radio button2‘,
                    ‘cssClassName‘: ‘‘,
                    ‘iconUrl‘: ‘‘,
                    ‘onclick‘: radioButton2ClickCallback
                }

            ]
        }
    ]

};

function buttonRotationClick(e) {

   
}
 

function buttonExplodeClick() {
    
}

function button2ClickCallback(e) {
    alert(‘Button2 is clicked‘);
}
function radioButton1ClickCallback(e) {
    alert(‘radio Button1 is clicked‘);
}
function radioButton2ClickCallback(e) {
    alert(‘radio Button2 is clicked‘);
}

 

接下來建立一個工具方法,解讀這個toolbarConfig並利用viewer UI API來建立對於的工具條和按鈕,使用方法也和簡單:

////add custom toolbar , usage example:
//addToolbar(toolbarConfig);

////////////////////////////////////////////////////////////////////////////
function addToolbar(toolbarConfig, viewer) {

    //find the container element in client webpage first
    var containter = document.getElementById(toolbarConfig.containerId);

    // if no toolbar container on client‘s webpage, create one and append it to viewer
    if (!containter) {
        containter = document.createElement(‘div‘);
        containter.id = ‘custom_toolbar‘;
        //‘position: relative;top: 75px;left: 0px;z-index: 200;‘;
        containter.style.position = ‘relative‘;
        containter.style.top = ‘75px‘;
        containter.style.left = ‘0px‘;
        containter.style.zIndex= ‘200‘;
        viewer.clientContainer.appendChild(containter);
    }

    //create a toolbar
    var toolbar = new Autodesk.Viewing.UI.ToolBar(containter);

    for (var i = 0, len = toolbarConfig.subToolbars.length; i < len; i++) {
        var stb = toolbarConfig.subToolbars[i];
        //create a subToolbar
        var subToolbar = toolbar.addSubToolbar(stb.id, stb.isRadio);
        subToolbar.setToolVisibility(stb.visible);

        //create buttons
        for (var j = 0, len2 = stb.buttons.length; j < len2; j++) {
            var btn = stb.buttons[j];
            var button = Autodesk.Viewing.UI.ToolBar.createMenuButton(btn.id, btn.tooltip, btn.onclick);
            //set css calss if availible
            if (btn.cssClassName) {
                button.className = btn.cssClassName;
            }
            //set button text if availible
            if (btn.buttonText) {
                var btnText = document.createElement(‘span‘);
                btnText.innerText = btn.buttonText;
                button.appendChild(btnText);
            }
            //set icon image if availible
            if (btn.iconUrl) {
                var ico = document.createElement(‘img‘);
                ico.src = btn.iconUrl;
                ico.className = ‘toolbar-button‘;
                button.appendChild(ico);
            }
            //add button to sub toolbar
            toolbar.addToSubToolbar(stb.id, button);

        }



    }

下面就是運行效果了:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.