{"size": 1701, "ext": "js", "max_stars_repo_path": "scripts/core/graphics/Screen.js", "max_stars_repo_name": "bmstu-iu9/utp2020-4-2d-game", "max_stars_repo_stars_event_min_datetime": "2020-07-22T00:30:02.000Z", "max_stars_repo_stars_event_max_datetime": "2020-08-10T09:04:30.000Z", "max_issues_repo_path": "scripts/core/graphics/Screen.js", "max_issues_repo_name": "bmstu-iu9/utp2020-4-2d-game", "max_issues_repo_issues_event_min_datetime": "2020-07-11T10:08:16.000Z", "max_issues_repo_issues_event_max_datetime": "2020-07-16T05:02:50.000Z", "max_forks_repo_path": "scripts/core/graphics/Screen.js", "max_forks_repo_name": "bmstu-iu9/utp2020-4-2d-game", "max_forks_repo_forks_event_min_datetime": "2021-01-27T21:53:40.000Z", "max_forks_repo_forks_event_max_datetime": "2021-01-27T21:53:40.000Z"}
{"max_stars_count": 2, "max_issues_count": 1, "max_forks_count": 1, "avg_line_length": 20.743902439, "max_line_length": 103, "alphanum_fraction": 0.6619635509}
import Vector2d from '../mathematics/Vector2d.js';
import Rect from './Rect.js';

export default class Screen {
	/**
	 * @type {HTMLCanvasElement}
	 */
	static canvas = null;

	/**
	 * @type {Vector2d}
	 */
	static size;

	/**
	 * @type {Vector2d}
	 */
	static unprocessedSize;

	/**
	 * @param {Vector2d} size Новый размер экрана.
	 */
	static setSize(size) {
		if (!(size instanceof Vector2d)) {
			throw new TypeError('invalid parameter "size". Expected an instance of Vector2d class.');
		}

		if (size.x <= 0) {
			throw new RangeError('width must be greater than 0.');
		}

		if (size.y <= 0) {
			throw new RangeError('height must be greater than 0.');
		}

		this.unprocessedSize = size;
	}

	/**
	 * @return {Rect} Возвращает прямоугольник, который содержит позицию и размер элемента canvas.
	 */
	static getCanvasRect() {
		if (this.canvas == null) {
			throw new Error('screen is not initialized.');
		}

		return new Rect(
			this.canvas.offsetLeft,
			this.canvas.offsetTop,
			this.canvas.clientWidth,
			this.canvas.clientHeight,
		);
	}

	static process() {
		if (this.unprocessedSize != null) {
			this.size = this.unprocessedSize;
			this.unprocessedSize = null;
			this.canvas.width = this.size.x;
			this.canvas.height = this.size.y;
		}
	}

	/**
	 * @param {HTMLCanvasElement} canvas
	 */
	static initialize(canvas) {
		if (!(canvas instanceof HTMLCanvasElement)) {
			throw new TypeError('invalid parameter "canvas". Expected an instance of HTMLCanvasElement class.');
		}

		this.canvas = canvas;
		this.size = new Vector2d(this.canvas.clientWidth, this.canvas.clientHeight);
	}

	static destroy() {
		this.canvas = null;
		this.unprocessedSize = null;
		this.size = null;
	}
}
