Source: objects/Box.js

import { Utils } from '../common/Utils';
import { BaseModelObject3D } from './BaseModelObject3D';

function _getParam(width, height, depth, param) {
	if (Utils.isObject(width)) {
		return width;
	}

	if (Utils.isObject(height)) {
		return height;
	}

	if (Utils.isObject(depth)) {
		return depth;
	}

	return param;
}

function _buildParam(width, height, depth, param) {
	param = _getParam(width, height, depth, param);

	param['body'] = param['body'] || {};
	param['body']['localScale'] = param['body']['localScale'] || [
		Utils.parseFloat(width, Utils.parseFloat(param['width'], 1)),
		Utils.parseFloat(height, Utils.parseFloat(param['height'], 1)),
		Utils.parseFloat(depth, Utils.parseFloat(param['depth'], 1)),
	];

	return param;
}

/**
 * @class Box
 * The box object.
 * @memberof THING
 * @extends THING.BaseModelObject3D
 * @public
 */
class Box extends BaseModelObject3D {

	static defaultTagArray = ['Geometry'];

	/**
	 * The box object in scene.
	 * @param {Number} width The width, default is 1.
	 * @param {Number} height The height, default is 1.
	 * @param {Number} depth The depth, default is 1.
	 * @param {Object} param The initial parameters.
	 */
	constructor(width, height, depth, param = {}) {
		super(_buildParam(width, height, depth, param));
	}

	// #region Accessor

	/**
	 * Get/Set the width.
	 * @type {Number}
	 * @example
	 * let box = new THING.Box();
	 * // @expect(box.width == 1)
	 * box.width = 10;
	 * // @expect(box.width == 10)
	 */
	get width() {
		return this.body.scale[0];
	}
	set width(value) {
		this._processBodyWithoutPivot((body) => {
			let scale = body.scale;
			body.scale = [value, scale[1], scale[2]];
		});
	}

	/**
	 * Get/Set the height.
	 * @type {Number}
	 * @example
	 * let box = new THING.Box();
	 * // @expect(box.height == 1)
	 * box.height = 10;
	 * // @expect(box.height == 10)
	 */
	get height() {
		return this.body.scale[1];
	}
	set height(value) {
		this._processBodyWithoutPivot((body) => {
			let scale = body.scale;
			body.scale = [scale[0], value, scale[2]];
		});
	}

	/**
	 * Get/Set the depth.
	 * @type {Number}
	 * @example
	 * let box = new THING.Box();
	 * // @expect(box.depth == 1)
	 * box.depth = 10;
	 * // @expect(box.depth == 10)
	 */
	get depth() {
		return this.body.scale[2];
	}
	set depth(value) {
		this._processBodyWithoutPivot((body) => {
			let scale = body.scale;
			body.scale = [scale[0], scale[1], value];
		});
	}

	// #endregion

	/**
	 * Check whether it's box object.
	 * @type {Boolean}
	 * @example
	 * let box = new THING.Box();
	 * let ret = box.isBox;
	 * // @expect(ret == true)
	 */
	get isBox() {
		return true;
	}

}

export { Box }