Source: resources/RenderTexture.js

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

const __ = {
	private: Symbol('private'),
}

/**
 * @class RenderTexture
 * The render texture resource.
 * @memberof THING
 * @extends THING.BaseStaticTexture
 * @public
 */
class RenderTexture extends BaseStaticTexture {

	constructor(param = {}) {
		super(param);

		this[__.private] = {};
		let _private = this[__.private];

		_private.size = Utils.parseArray(param['size'], [1, 1]);

		// Complete
		this.onNotifyComplete();
	}

	// #region Overrides

	onDispose() {
		let _private = this[__.private];

		_private.resource = null;

		super.onDispose();
	}

	onCreateTextureResource() {
		let _private = this[__.private];

		let renderTexture = Utils.createObject('RenderTextureResource');
		renderTexture.setSize(_private.size);

		return renderTexture;
	}

	// #endregion

	/**
	 * Copy from image.
	 * @param {THING.RenderTexture} source The image.
	 * @returns {THING.RenderTexture}
	 * @public
	 */
	copy(source) {
		super.copy(source);

		let _private = this[__.private];

		_private.size = source.size;

		return this;
	}

	/**
	 * Clone image.
	 * @returns {THING.RenderTexture}
	 * @public
	 */
	clone() {
		let resource = new RenderTexture();
		resource.copy(this);

		return resource;
	}

	/**
	 * Download file as image resource in PNG file format.
	 * @param {String} fileName The file name.
	 * @example
	 * renderTexture.download('myScreenshot');
	 * @public
	 */
	download(fileName) {
		let _private = this[__.private];

		let size = _private.size;
		let imageWidth = size[0];
		let imageHeight = size[1];

		let image = Utils.saveAsImage(imageWidth, imageHeight, this.pixelBuffer);
		Utils.saveAsFile(fileName + '.png', image, 'png');
	}

	// #region Accessors

	/**
	 * Get/Set size.
	 * @type {Array<Number>}
	 * @public
	 */
	get size() {
		let _private = this[__.private];

		return _private.size.slice(0);
	}
	set size(value) {
		let _private = this[__.private];

		_private.size = value.slice(0);

		this.getTextureResource().setSize(_private.size);
	}

	/**
	 * Get pixel buffer in RGBA color format.
	 * @type {Uint8Array}
	 * @public
	 */
	get pixelBuffer() {
		return this.getTextureResource().getPixelBuffer();
	}

	// #endregion

	/**
	 * Check class type.
	 * @type {Boolean}
	 */
	get isRenderTexture() {
		return true;
	}

	// #endregion

}

export { RenderTexture }