|
|
@@ -1,69 +1,111 @@
|
|
|
import { Vector2 } from 'three';
|
|
|
|
|
|
+/**
|
|
|
+ * A helper for {@link SelectionBox}.
|
|
|
+ *
|
|
|
+ * It visualizes the current selection box with a `div` container element.
|
|
|
+ */
|
|
|
class SelectionHelper {
|
|
|
|
|
|
+ /**
|
|
|
+ * Constructs a new selection helper.
|
|
|
+ *
|
|
|
+ * @param {(WebGPURenderer|WebGLRenderer)} renderer - The renderer.
|
|
|
+ * @param {string} cssClassName - The CSS class name of the `div`.
|
|
|
+ */
|
|
|
constructor( renderer, cssClassName ) {
|
|
|
|
|
|
+ /**
|
|
|
+ * The visualization of the selection box.
|
|
|
+ *
|
|
|
+ * @type {HTMLDivElement}
|
|
|
+ */
|
|
|
this.element = document.createElement( 'div' );
|
|
|
this.element.classList.add( cssClassName );
|
|
|
this.element.style.pointerEvents = 'none';
|
|
|
|
|
|
+ /**
|
|
|
+ * A reference to the renderer.
|
|
|
+ *
|
|
|
+ * @type {(WebGPURenderer|WebGLRenderer)}
|
|
|
+ */
|
|
|
this.renderer = renderer;
|
|
|
|
|
|
- this.startPoint = new Vector2();
|
|
|
- this.pointTopLeft = new Vector2();
|
|
|
- this.pointBottomRight = new Vector2();
|
|
|
-
|
|
|
+ /**
|
|
|
+ * Whether the mouse or pointer is pressed down.
|
|
|
+ *
|
|
|
+ * @type {boolean}
|
|
|
+ * @default false
|
|
|
+ */
|
|
|
this.isDown = false;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Whether helper is enabled or not.
|
|
|
+ *
|
|
|
+ * @type {boolean}
|
|
|
+ * @default true
|
|
|
+ */
|
|
|
this.enabled = true;
|
|
|
|
|
|
- this.onPointerDown = function ( event ) {
|
|
|
+ // private
|
|
|
+
|
|
|
+ this._startPoint = new Vector2();
|
|
|
+ this._pointTopLeft = new Vector2();
|
|
|
+ this._pointBottomRight = new Vector2();
|
|
|
+
|
|
|
+ this._onPointerDown = function ( event ) {
|
|
|
|
|
|
if ( this.enabled === false ) return;
|
|
|
|
|
|
this.isDown = true;
|
|
|
- this.onSelectStart( event );
|
|
|
+ this._onSelectStart( event );
|
|
|
|
|
|
}.bind( this );
|
|
|
|
|
|
- this.onPointerMove = function ( event ) {
|
|
|
+ this._onPointerMove = function ( event ) {
|
|
|
|
|
|
if ( this.enabled === false ) return;
|
|
|
|
|
|
if ( this.isDown ) {
|
|
|
|
|
|
- this.onSelectMove( event );
|
|
|
+ this._onSelectMove( event );
|
|
|
|
|
|
}
|
|
|
|
|
|
}.bind( this );
|
|
|
|
|
|
- this.onPointerUp = function ( ) {
|
|
|
+ this._onPointerUp = function ( ) {
|
|
|
|
|
|
if ( this.enabled === false ) return;
|
|
|
|
|
|
this.isDown = false;
|
|
|
- this.onSelectOver();
|
|
|
+ this._onSelectOver();
|
|
|
|
|
|
}.bind( this );
|
|
|
|
|
|
- this.renderer.domElement.addEventListener( 'pointerdown', this.onPointerDown );
|
|
|
- this.renderer.domElement.addEventListener( 'pointermove', this.onPointerMove );
|
|
|
- this.renderer.domElement.addEventListener( 'pointerup', this.onPointerUp );
|
|
|
+ this.renderer.domElement.addEventListener( 'pointerdown', this._onPointerDown );
|
|
|
+ this.renderer.domElement.addEventListener( 'pointermove', this._onPointerMove );
|
|
|
+ this.renderer.domElement.addEventListener( 'pointerup', this._onPointerUp );
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Call this method if you no longer want use to the controls. It frees all internal
|
|
|
+ * resources and removes all event listeners.
|
|
|
+ */
|
|
|
dispose() {
|
|
|
|
|
|
- this.renderer.domElement.removeEventListener( 'pointerdown', this.onPointerDown );
|
|
|
- this.renderer.domElement.removeEventListener( 'pointermove', this.onPointerMove );
|
|
|
- this.renderer.domElement.removeEventListener( 'pointerup', this.onPointerUp );
|
|
|
+ this.renderer.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
|
|
|
+ this.renderer.domElement.removeEventListener( 'pointermove', this._onPointerMove );
|
|
|
+ this.renderer.domElement.removeEventListener( 'pointerup', this._onPointerUp );
|
|
|
|
|
|
this.element.remove(); // in case disposal happens while dragging
|
|
|
|
|
|
}
|
|
|
|
|
|
- onSelectStart( event ) {
|
|
|
+ // private
|
|
|
+
|
|
|
+ _onSelectStart( event ) {
|
|
|
|
|
|
this.element.style.display = 'none';
|
|
|
|
|
|
@@ -74,28 +116,28 @@ class SelectionHelper {
|
|
|
this.element.style.width = '0px';
|
|
|
this.element.style.height = '0px';
|
|
|
|
|
|
- this.startPoint.x = event.clientX;
|
|
|
- this.startPoint.y = event.clientY;
|
|
|
+ this._startPoint.x = event.clientX;
|
|
|
+ this._startPoint.y = event.clientY;
|
|
|
|
|
|
}
|
|
|
|
|
|
- onSelectMove( event ) {
|
|
|
+ _onSelectMove( event ) {
|
|
|
|
|
|
this.element.style.display = 'block';
|
|
|
|
|
|
- this.pointBottomRight.x = Math.max( this.startPoint.x, event.clientX );
|
|
|
- this.pointBottomRight.y = Math.max( this.startPoint.y, event.clientY );
|
|
|
- this.pointTopLeft.x = Math.min( this.startPoint.x, event.clientX );
|
|
|
- this.pointTopLeft.y = Math.min( this.startPoint.y, event.clientY );
|
|
|
+ this._pointBottomRight.x = Math.max( this._startPoint.x, event.clientX );
|
|
|
+ this._pointBottomRight.y = Math.max( this._startPoint.y, event.clientY );
|
|
|
+ this._pointTopLeft.x = Math.min( this._startPoint.x, event.clientX );
|
|
|
+ this._pointTopLeft.y = Math.min( this._startPoint.y, event.clientY );
|
|
|
|
|
|
- this.element.style.left = this.pointTopLeft.x + 'px';
|
|
|
- this.element.style.top = this.pointTopLeft.y + 'px';
|
|
|
- this.element.style.width = ( this.pointBottomRight.x - this.pointTopLeft.x ) + 'px';
|
|
|
- this.element.style.height = ( this.pointBottomRight.y - this.pointTopLeft.y ) + 'px';
|
|
|
+ this.element.style.left = this._pointTopLeft.x + 'px';
|
|
|
+ this.element.style.top = this._pointTopLeft.y + 'px';
|
|
|
+ this.element.style.width = ( this._pointBottomRight.x - this._pointTopLeft.x ) + 'px';
|
|
|
+ this.element.style.height = ( this._pointBottomRight.y - this._pointTopLeft.y ) + 'px';
|
|
|
|
|
|
}
|
|
|
|
|
|
- onSelectOver() {
|
|
|
+ _onSelectOver() {
|
|
|
|
|
|
this.element.remove();
|
|
|
|