-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from andrewisen-tikab/feature
Add `TunnelControls`
- Loading branch information
Showing
7 changed files
with
313 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import * as THREE from 'three'; | ||
import { AbstractGrout3D, AbstractGrout3DParams } from './core'; | ||
import Tunnel3D from './Tunnel3D'; | ||
|
||
// const doSomething = () => {}; | ||
|
||
export default class Grout3D extends THREE.Object3D implements AbstractGrout3D { | ||
public isGrout3D: boolean = true; | ||
|
||
public screenLength: number = 1; | ||
|
||
public angle: number = 5 * THREE.MathUtils.DEG2RAD; | ||
|
||
public cutDepth: number = 1; | ||
|
||
public overlap: number = 0; | ||
|
||
public holeLength: number = 10; | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
private _params: AbstractGrout3DParams | null = null; | ||
|
||
private _tunnel: Tunnel3D; | ||
|
||
constructor(tunnel: Tunnel3D, params?: AbstractGrout3DParams) { | ||
super(); | ||
this._tunnel = tunnel; | ||
if (params) this._params = params; | ||
this._build(); | ||
} | ||
|
||
// private _calculateNewParams(): boolean { | ||
// if (!this._params) throw new Error('Grout3D: params is are defined'); | ||
|
||
// const { holeLength, angle, cutDepth } = this._params; | ||
|
||
// if (angle != null && holeLength != null) { | ||
// doSomething(); | ||
// } else if (holeLength != null && cutDepth != null) { | ||
// doSomething(); | ||
// } else if (angle != null && cutDepth != null) { | ||
// doSomething(); | ||
// } else { | ||
// console.error('params', this._params); | ||
// return false; | ||
// } | ||
// return true; | ||
// } | ||
|
||
private _build() { | ||
// if (!this._calculateNewParams()) | ||
// throw new Error('Grout3D: params are not correctly defined'); | ||
|
||
const { holeLength } = this; | ||
|
||
const geometry = new THREE.CylinderGeometry(1 / 3, 1 / 3, holeLength, 32); | ||
const material = new THREE.MeshBasicMaterial({ | ||
color: 0xff0000, | ||
depthTest: false, | ||
depthWrite: false, | ||
}); | ||
const cylinder = new THREE.Mesh(geometry, material); | ||
cylinder.rotateX(Math.PI / 2); | ||
cylinder.position.set(0, 0, holeLength / 2); | ||
this.add(cylinder); | ||
|
||
const { tunnelHeight, tunnelRoofHeight } = this._tunnel; | ||
this.position.set(0, tunnelHeight + tunnelRoofHeight, 0); | ||
this.rotation.set(-this.angle, 0, 0); | ||
} | ||
|
||
public update(params?: AbstractGrout3DParams): void { | ||
if (params) this._params = params; | ||
this.clear(); | ||
this._build(); | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
export type Listener = (event?: DispatcherEvent) => void; | ||
|
||
export interface DispatcherEvent { | ||
type: string; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
[key: string]: any; | ||
} | ||
|
||
export class EventDispatcher { | ||
private _listeners: { [type: string]: Listener[] } = {}; | ||
|
||
/** | ||
* Adds the specified event listener. | ||
* @param type event name | ||
* @param listener handler function | ||
* @category Methods | ||
*/ | ||
addEventListener(type: string, listener: Listener): void { | ||
const listeners = this._listeners; | ||
|
||
if (listeners[type] === undefined) listeners[type] = []; | ||
|
||
if (listeners[type].indexOf(listener) === -1) listeners[type].push(listener); | ||
} | ||
|
||
/** | ||
* Presence of the specified event listener. | ||
* @param type event name | ||
* @param listener handler function | ||
* @category Methods | ||
*/ | ||
hasEventListener(type: string, listener: Listener): boolean { | ||
const listeners = this._listeners; | ||
|
||
return listeners[type] !== undefined && listeners[type].indexOf(listener) !== -1; | ||
} | ||
|
||
/** | ||
* Removes the specified event listener | ||
* @param type event name | ||
* @param listener handler function | ||
* @category Methods | ||
*/ | ||
removeEventListener(type: string, listener: Listener): void { | ||
const listeners = this._listeners; | ||
const listenerArray = listeners[type]; | ||
|
||
if (listenerArray !== undefined) { | ||
const index = listenerArray.indexOf(listener); | ||
|
||
if (index !== -1) listenerArray.splice(index, 1); | ||
} | ||
} | ||
|
||
/** | ||
* Removes all event listeners | ||
* @param type event name | ||
* @category Methods | ||
*/ | ||
removeAllEventListeners(type?: string): void { | ||
if (!type) { | ||
this._listeners = {}; | ||
return; | ||
} | ||
|
||
if (Array.isArray(this._listeners[type])) this._listeners[type].length = 0; | ||
} | ||
|
||
/** | ||
* Fire an event type. | ||
* @param event DispatcherEvent | ||
* @category Methods | ||
*/ | ||
dispatchEvent(event: DispatcherEvent): void { | ||
const listeners = this._listeners; | ||
const listenerArray = listeners[event.type]; | ||
|
||
if (listenerArray !== undefined) { | ||
event.target = this; | ||
const array = listenerArray.slice(0); | ||
|
||
for (let i = 0, l = array.length; i < l; i++) { | ||
array[i].call(this, event); | ||
} | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Grout3D from '../Grout3D'; | ||
import Tunnel3D from '../Tunnel3D'; | ||
import { AbstractGrout3DParams, AbstractTunnel3DParams } from '../core'; | ||
import { EventDispatcher } from './EventDispatcher'; | ||
|
||
export default class TunnelControls extends EventDispatcher { | ||
private _tunnel: Tunnel3D | null = null; | ||
private _grout: Grout3D | null = null; | ||
|
||
constructor() { | ||
super(); | ||
} | ||
|
||
attach(tunnel: Tunnel3D) { | ||
this._tunnel = tunnel; | ||
} | ||
|
||
detach() { | ||
this._tunnel = null; | ||
} | ||
|
||
getTunnel() { | ||
return this._tunnel; | ||
} | ||
|
||
setTunnelParams(params: Partial<AbstractTunnel3DParams>) { | ||
if (this._tunnel == null) return; | ||
Object.assign(this._tunnel, params); | ||
this._tunnel.update(); | ||
|
||
if (this._grout) { | ||
this._grout.update(); | ||
} | ||
} | ||
|
||
addGrout() { | ||
if (this._tunnel == null) return; | ||
const grout = new Grout3D(this._tunnel); | ||
this._grout = grout; | ||
this._tunnel.groutGroup.add(grout); | ||
return grout; | ||
} | ||
|
||
setGroutParams(params: Partial<AbstractGrout3DParams>) { | ||
if (this._grout == null) return; | ||
Object.assign(this._grout, params); | ||
this._grout.update(); | ||
} | ||
} |
Oops, something went wrong.