Created
September 15, 2015 07:31
-
-
Save leefsmp/334b964716dd25c07592 to your computer and use it in GitHub Desktop.
ADN Megabot Viewer Extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/////////////////////////////////////////////////////////////////////////////// | |
// Megabots viewer extension | |
// by Philippe Leefsma, August 2015 | |
// | |
/////////////////////////////////////////////////////////////////////////////// | |
AutodeskNamespace("Autodesk.ADN.Viewing.Extension"); | |
Autodesk.ADN.Viewing.Extension.Megabot = function (viewer, options) { | |
Autodesk.Viewing.Extension.call(this, viewer, options); | |
var _self = this; | |
var _fixedTarget = new THREE.Vector3( | |
-3.339676111911686, 9.064717541212062, -5.362819481549721); | |
//////////////////////////////////////////////////////////////// | |
// Extension load callback | |
// | |
//////////////////////////////////////////////////////////////// | |
_self.load = function () { | |
addAdskLink(); | |
options = options || {}; | |
_self.tool = new Tool( | |
options.angularVelocity || 0.3, | |
options.idleTime || 5.0); | |
viewer.toolController.registerTool(_self.tool); | |
viewer.toolController.activateTool('megabot'); | |
viewer.addEventListener( | |
Autodesk.Viewing.CAMERA_CHANGE_EVENT, | |
function (event) { | |
viewer.navigation.setTarget(_fixedTarget); | |
}); | |
console.log('Autodesk.ADN.Viewing.Extension.Megabot loaded'); | |
return true; | |
}; | |
//////////////////////////////////////////////////////////////// | |
// Extension unload callback | |
// | |
//////////////////////////////////////////////////////////////// | |
_self.unload = function () { | |
viewer.toolController.deactivateTool('megabot'); | |
console.log('Autodesk.ADN.Viewing.Extension.Megabot unloaded'); | |
return true; | |
}; | |
//////////////////////////////////////////////////////////////// | |
// Add Autodesk Icon | |
// | |
//////////////////////////////////////////////////////////////// | |
function addAdskLink() { | |
$(viewer.container).append( | |
'<div id="divToolbar"> </div>'); | |
$('#divToolbar').css({ | |
'top': '30%', | |
'left': '20%', | |
'z-index': '100', | |
'position': 'absolute' | |
}); | |
var toolbar = new Autodesk.Viewing.UI.ToolBar(true); | |
var ctrlGroup = new Autodesk.Viewing.UI.ControlGroup( | |
"megabot"); | |
var button = new Autodesk.Viewing.UI.Button( | |
"megabot"); | |
button.icon.style.backgroundImage = | |
"url(img/adsk-48x48-32.png)"; | |
button.onClick = function (e) { | |
window.open('http://www.autodesk.com','_target'); | |
}; | |
button.setToolTip("Powered by Autodesk View & Data API"); | |
ctrlGroup.addControl(button); | |
toolbar.addControl(ctrlGroup); | |
$('#divToolbar')[0].appendChild( | |
toolbar.container); | |
} | |
//////////////////////////////////////////////////////////////// | |
// Custom Tool | |
// | |
//////////////////////////////////////////////////////////////// | |
var Tool = function (angularVelocity, idleTime) { | |
var timeDown = 0; | |
var lastTime = -1; | |
var motionCallbacks = {}; | |
var userInteracting = false; | |
this.getNames = function() { | |
return ['megabot']; | |
}; | |
this.getName = function() { | |
return 'megabot'; | |
}; | |
this.activate = function(name) { | |
setDemoState(); | |
}; | |
this.deactivate = function(name) { | |
}; | |
///////////////////////////////////////////////////////////// | |
// update is called by the framework | |
// t: time elapsed since tool activated in ms | |
///////////////////////////////////////////////////////////// | |
this.update = function(t) { | |
var dt = elapsed(t); | |
timeDown += dt; | |
for(var motionId in motionCallbacks){ | |
motionCallbacks[motionId](dt); | |
} | |
if(timeDown > idleTime && !userInteracting && !motionCallbacks['rotate']) { | |
setDemoState(); | |
} | |
return false; | |
}; | |
this.handleSingleClick = function(event, button) { | |
return false; | |
}; | |
this.handleDoubleClick = function(event, button) { | |
return false; | |
}; | |
this.handleSingleTap = function(event) { | |
return false; | |
}; | |
this.handleDoubleTap = function(event) { | |
return false; | |
}; | |
this.handleKeyDown = function(event, keyCode) { | |
return false; | |
}; | |
this.handleKeyUp = function(event, keyCode) { | |
return false; | |
}; | |
this.handleWheelInput = function(delta) { | |
return false; | |
}; | |
this.handleButtonDown = function(event, button) { | |
userInteracting = true; | |
stopRotateMotion(); | |
timeDown = 0; | |
return false; | |
}; | |
this.handleButtonUp = function(event, button) { | |
userInteracting = false; | |
return false; | |
}; | |
this.handleMouseMove = function(event) { | |
if(userInteracting) | |
timeDown = 0; | |
return false; | |
}; | |
this.handleGesture = function(event) { | |
if(userInteracting) | |
timeDown = 0; | |
return false; | |
}; | |
this.handleBlur = function(event) { | |
return false; | |
}; | |
this.handleResize = function() { | |
}; | |
///////////////////////////////////////////////////////////// | |
// Returns elapsed time since last call | |
// | |
///////////////////////////////////////////////////////////// | |
function elapsed(t) { | |
if(lastTime < 0) { | |
lastTime = t; | |
} | |
var dt = t - lastTime; | |
lastTime = t; | |
return dt * 0.001; //in sec | |
} | |
///////////////////////////////////////////////////////////// | |
// Starts/Stops motion | |
// | |
///////////////////////////////////////////////////////////// | |
function startRotateMotion(speed, axis) { | |
if(motionCallbacks['rotate']) | |
return; | |
motionCallbacks['rotate'] = function (elapsed) { | |
var pos = viewer.navigation.getPosition(); | |
var position = new THREE.Vector3( | |
pos.x, | |
pos.y, | |
pos.z | |
); | |
var rAxis = new THREE.Vector3( | |
axis.x, axis.y, axis.z | |
); | |
var matrix = new THREE.Matrix4().makeRotationAxis( | |
rAxis, | |
speed * elapsed); | |
position.applyMatrix4(matrix); | |
viewer.navigation.setPosition(position); | |
}; | |
} | |
function stopRotateMotion() { | |
delete motionCallbacks['rotate']; | |
} | |
///////////////////////////////////////////////////////////// | |
// Restore default state | |
// | |
///////////////////////////////////////////////////////////// | |
function setDemoState() { | |
startRotateMotion( | |
angularVelocity, | |
viewer.navigation.getWorldUpVector()); | |
var stateStr = '{"viewport":{"name":"","eye":[116.38987095896118,128.79426234702453,114.36672740056797],"target":[-3.339676111911686,9.064717541212062,-5.362819481549721],"up":[0,1,0],"worldUpVector":[0,1,0],"pivotPoint":[-0.021964073181152344,0.014722824096679688,0.035503387451171875],"distanceToOrbit":207.57046897047968,"aspectRatio":1.618330934927428,"projection":"orthographic","isOrthographic":true,"orthographicHeight":207.37765727725017}}'; | |
var VIEWPORT_FILTER = { | |
guid: false, | |
seedURN: false, | |
objectSet: false, | |
viewport: true, | |
renderOptions: false | |
}; | |
var state = JSON.parse(stateStr); | |
viewer.restoreState(state, VIEWPORT_FILTER, false); | |
} | |
} | |
}; | |
Autodesk.ADN.Viewing.Extension.Megabot.prototype = | |
Object.create(Autodesk.Viewing.Extension.prototype); | |
Autodesk.ADN.Viewing.Extension.Megabot.prototype.constructor = | |
Autodesk.ADN.Viewing.Extension.Megabot; | |
Autodesk.Viewing.theExtensionManager.registerExtension( | |
'Autodesk.ADN.Viewing.Extension.Megabot', | |
Autodesk.ADN.Viewing.Extension.Megabot); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment