2015-07-06 11:14:42 +02:00
|
|
|
/**
|
|
|
|
* Contains a canvas to place over the renderer for FPS-style pointer
|
|
|
|
* It's based on a javascript 2d-canvas : you'll need to render it manually.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* var container = documeng.getElementById('container');
|
|
|
|
* var renderer = new THREE.WebGLRenderer();
|
|
|
|
* renderer.setSize(width, height);
|
|
|
|
*
|
|
|
|
* var pointerCamera = new L3D.PointerCamera(50, width/height, near, fat, renderer, container);
|
|
|
|
* var mousePointer = new L3D.MousePointer(pointerCamera);
|
|
|
|
* mousePointer.domElement.width = width;
|
|
|
|
* mousePointer.domElement.height = height;
|
|
|
|
*
|
|
|
|
* container.appendChild(mousePointer.domElement);
|
|
|
|
* container.appendChild(renderer.domElement);
|
|
|
|
*
|
|
|
|
* mousePointer.render(L3D.MousePointer.BLACK);
|
|
|
|
* @memberof L3D
|
|
|
|
* @constructor
|
|
|
|
*/
|
2015-07-01 16:31:43 +02:00
|
|
|
L3D.MousePointer = function(camera) {
|
2015-06-22 14:39:41 +02:00
|
|
|
|
2015-07-06 11:14:42 +02:00
|
|
|
/**
|
|
|
|
* @type {elemnt}
|
|
|
|
* @description The document element to add on top of the renderer
|
|
|
|
*/
|
2015-06-22 09:41:59 +02:00
|
|
|
this.domElement = document.createElement('canvas');
|
2015-07-06 11:14:42 +02:00
|
|
|
|
2015-06-22 09:41:59 +02:00
|
|
|
this.domElement.style.position = 'absolute';
|
|
|
|
this.domElement.style.cssFloat = 'top-left';
|
2015-07-06 11:14:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {CanvasRenderingContext2D}
|
|
|
|
* @description The context of the canvas
|
|
|
|
*/
|
2015-06-22 09:41:59 +02:00
|
|
|
this.ctx = this.domElement.getContext('2d');
|
2015-07-06 11:14:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {Number}
|
|
|
|
* @description The size of the gun sight
|
|
|
|
*/
|
2015-06-22 09:41:59 +02:00
|
|
|
this.size = 10;
|
2015-07-06 11:14:42 +02:00
|
|
|
|
2015-06-22 09:41:59 +02:00
|
|
|
camera.mousePointer = this;
|
2015-07-06 11:14:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {Number}
|
|
|
|
* @description a L3D.MousePointer style. The current style of the mouse pointer
|
|
|
|
*/
|
2015-07-01 16:31:43 +02:00
|
|
|
this.style = L3D.MousePointer.NONE;
|
2015-07-06 11:14:42 +02:00
|
|
|
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-22 14:39:41 +02:00
|
|
|
|
2015-07-06 11:14:42 +02:00
|
|
|
/**
|
|
|
|
* @memberof L3D.MousePointer
|
|
|
|
* @type {Number}
|
|
|
|
* @static
|
|
|
|
* @description Empty style : the canvas is fully transparent
|
|
|
|
*/
|
2015-07-01 16:31:43 +02:00
|
|
|
L3D.MousePointer.NONE = 0;
|
2015-07-06 11:14:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @memberof L3D.MousePointer
|
|
|
|
* @type {Number}
|
|
|
|
* @static
|
|
|
|
* @description Black style : the canvas contains only a white cross in the middle of the screen
|
|
|
|
*/
|
2015-07-01 16:31:43 +02:00
|
|
|
L3D.MousePointer.BLACK = 1;
|
2015-07-06 11:14:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @memberof L3D.MousePointer
|
|
|
|
* @type {Number}
|
|
|
|
* @static
|
|
|
|
* @description Red style : the canvas contains only a white and red cross in the midlle
|
|
|
|
* (used for hovering stuff)
|
|
|
|
*/
|
2015-07-01 16:31:43 +02:00
|
|
|
L3D.MousePointer.RED = 2;
|
2015-06-22 14:39:41 +02:00
|
|
|
|
2015-07-06 11:14:42 +02:00
|
|
|
/**
|
|
|
|
* @memberof L3D.MousePointer
|
|
|
|
* @static
|
|
|
|
* @description Converts a style to a color
|
|
|
|
* @param {Number} style a L3D.MousePointer style (NONE, BLACK, or RED)
|
|
|
|
* @returns {string} null if input is NONE, a hex color string else
|
|
|
|
*/
|
2015-07-01 16:31:43 +02:00
|
|
|
L3D.MousePointer.toColor = function(style) {
|
2015-06-22 14:39:41 +02:00
|
|
|
|
|
|
|
switch (style) {
|
|
|
|
|
2015-07-01 16:31:43 +02:00
|
|
|
case L3D.MousePointer.NONE:
|
2015-06-22 14:39:41 +02:00
|
|
|
return null;
|
2015-07-01 16:31:43 +02:00
|
|
|
case L3D.MousePointer.BLACK:
|
2015-06-22 14:39:41 +02:00
|
|
|
return '#000000';
|
2015-07-01 16:31:43 +02:00
|
|
|
case L3D.MousePointer.RED:
|
2015-06-22 14:39:41 +02:00
|
|
|
return '#ff0000';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-22 09:41:59 +02:00
|
|
|
|
2015-07-06 11:14:42 +02:00
|
|
|
/**
|
|
|
|
* Re-renders the canvas
|
|
|
|
* For performance reasons, the rendering is done only if the style changed.
|
|
|
|
* @param {Number} style the L3D.MousePointer style you want to render
|
2015-07-08 15:09:33 +02:00
|
|
|
* @param {Boolean} [force=false] force the re-rendering (even if the style did not change)
|
2015-07-06 11:14:42 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
L3D.MousePointer.prototype.render = function(style, force) {
|
|
|
|
|
|
|
|
if (style === undefined) {
|
|
|
|
|
|
|
|
style = L3D.MousePointer.NONE;
|
2015-06-22 14:39:41 +02:00
|
|
|
|
2015-07-06 11:14:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (force === undefined) {
|
|
|
|
|
|
|
|
force = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.style !== style || force) {
|
2015-06-22 09:41:59 +02:00
|
|
|
|
2015-07-01 16:31:43 +02:00
|
|
|
if (style === L3D.MousePointer.NONE) {
|
2015-06-22 09:41:59 +02:00
|
|
|
|
2015-06-22 14:39:41 +02:00
|
|
|
// Clear canvas
|
|
|
|
this.domElement.width = this.domElement.width;
|
2015-07-01 16:31:43 +02:00
|
|
|
this.style = L3D.MousePointer.NONE;
|
2015-06-22 09:41:59 +02:00
|
|
|
|
2015-06-22 14:39:41 +02:00
|
|
|
} else {
|
2015-06-22 09:41:59 +02:00
|
|
|
|
2015-06-22 14:39:41 +02:00
|
|
|
this.domElement.width = this.domElement.width;
|
|
|
|
|
|
|
|
var i = container_size.width() / 2;
|
|
|
|
var imin = i - this.size;
|
|
|
|
var imax = i + this.size;
|
|
|
|
|
|
|
|
var j = container_size.height() / 2;
|
|
|
|
var jmin = j - this.size;
|
|
|
|
var jmax = j + this.size;
|
|
|
|
|
|
|
|
this.ctx.beginPath();
|
|
|
|
this.ctx.moveTo(imin, j);
|
|
|
|
this.ctx.lineTo(imax, j);
|
|
|
|
this.ctx.moveTo(i, jmin);
|
|
|
|
this.ctx.lineTo(i, jmax);
|
|
|
|
this.ctx.closePath();
|
|
|
|
|
|
|
|
|
|
|
|
this.ctx.lineWidth = 5;
|
|
|
|
this.ctx.strokeStyle = '#ffffff';
|
|
|
|
this.ctx.stroke();
|
|
|
|
|
|
|
|
this.ctx.lineWidth = 2;
|
2015-07-01 16:31:43 +02:00
|
|
|
this.ctx.strokeStyle = L3D.MousePointer.toColor(style);
|
2015-06-22 14:39:41 +02:00
|
|
|
this.ctx.stroke();
|
|
|
|
|
|
|
|
this.style = style;
|
|
|
|
|
|
|
|
}
|
2015-06-22 09:41:59 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-22 09:41:59 +02:00
|
|
|
|
2015-07-06 11:14:42 +02:00
|
|
|
/**
|
|
|
|
* Clears the canvas
|
|
|
|
* @param {Boolean} force force the clearing (even if the style did not change)
|
|
|
|
*/
|
|
|
|
L3D.MousePointer.prototype.clear = function(force) {
|
|
|
|
|
|
|
|
if (force === undefined) {
|
|
|
|
|
|
|
|
force = false;
|
|
|
|
|
|
|
|
}
|
2015-06-22 09:41:59 +02:00
|
|
|
|
2015-07-06 11:14:42 +02:00
|
|
|
this.render(L3D.MousePointer.NONE, force);
|
2015-06-22 09:41:59 +02:00
|
|
|
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-07-09 11:44:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the size of the canvas
|
|
|
|
* @param {Number} width the new width of the canvas
|
|
|
|
* @param {Number} height the new height of the canvas
|
|
|
|
*/
|
|
|
|
|
|
|
|
L3D.MousePointer.prototype.setSize = function(width, height) {
|
|
|
|
|
|
|
|
this.domElement.width = width;
|
|
|
|
this.domElement.height = height;
|
|
|
|
|
|
|
|
this.render(this.style, true);
|
|
|
|
|
|
|
|
};
|