<commit_before>// control/mouse - mouse input and capture handling
// Part of Stardazed
// (c) 2015-2017 by <NAME> - @zenmumbler
// https://github.com/stardazed/stardazed

/// <reference path="../tools/dom.ts" />

namespace sd.control {

	export interface Mouse {
		buttonState(index: number): ButtonState;
		down(index: number): boolean;
		pressed(index: number): boolean;
		halfTransitions(index: number): number;
		resetHalfTransitions(): void;

		readonly position: Float2;
		readonly positionDelta: Float2;
		readonly wheelDelta: Float2;
	}

	class MouseImpl implements Mouse {
		constructor() {
		}

		buttonState(_index: number): ButtonState {
			return {
				down: false,
				halfTransitionCount: 0
			};
		}

		down(_index: number) {
			return false;
		}

		pressed(_index: number) {
			return false;
		}

		halfTransitions(_index: number) {
			return 0;
		}

		resetHalfTransitions() {

		}

		get position() {
			return [0, 0];
		}

		get positionDelta() {
			return [0, 0];
		}

		get wheelDelta() {
			return [0, 0];
		}
	}<commit_msg>feat(mouse): full basic mouse state<commit_after>// control/mouse - mouse input and capture handling
// Part of Stardazed
// (c) 2015-2017 by <NAME> - @zenmumbler
// https://github.com/stardazed/stardazed

/// <reference path="../tools/dom.ts" />

namespace sd.control {

	export interface Mouse {
		buttonState(index: number): ButtonState;
		down(index: number): boolean;
		pressed(index: number): boolean;
		released(index: number): boolean;
		halfTransitions(index: number): number;
		reset(): void;
		resetPerFrameData(): void;

		readonly position: Float2;
		readonly positionDelta: Float2;
		readonly wheelDelta: Float2;
	}

	class MouseImpl implements Mouse {
		private buttonData_: container.FixedMultiArray;
		private downBase_: Uint8Array;
		private halfTransBase_: Uint8Array;
		private readonly position_: number[];
		private readonly positionDelta_: number[];
		private readonly wheelDelta_: number[];

		constructor() {
			const fields: container.MABField[] = [
				{ type: UInt8, count: 1 },  // down
				{ type: UInt8, count: 1 },  // halfTransitionCount
			];
			this.buttonData_ = new container.FixedMultiArray(16, fields);
			this.downBase_ = this.buttonData_.indexedFieldView(0) as Uint8Array;
			this.halfTransBase_ = this.buttonData_.indexedFieldView(1) as Uint8Array;

			this.position_ = [0, 0];
			this.positionDelta_ = [0, 0];
			this.wheelDelta_ = [0, 0];

			window.addEventListener("wheel", evt => {
				this.wheelDelta_[0] = evt.deltaX;
				this.wheelDelta_[1] = evt.deltaY;
				evt.preventDefault();
				evt.stopPropagation();
			});
		
			window.addEventListener("mousedown", evt => {
				this.downBase_[evt.button] = 1;
				++this.halfTransBase_[evt.button];
				evt.preventDefault();
			});
		
			window.addEventListener("mouseup", evt => {
				this.downBase_[evt.button] = 0;
				++this.halfTransBase_[evt.button];
				evt.preventDefault();
			});
		
			window.addEventListener("contextmenu", evt => {
				evt.preventDefault();
				evt.stopPropagation();
			});

			window.addEventListener("mousemove", evt => {
				if (document.pointerLockElement) {
					this.positionDelta_[0] = evt.movementX;
					this.positionDelta_[1] = evt.movementY;
				}
				else {
					this.positionDelta_[0] = evt.clientX - this.position_[0];
					this.positionDelta_[1] = evt.clientY - this.position_[1];
					this.position_[0] = evt.clientX;
					this.position_[1] = evt.clientY;
				}
			});
		}

		buttonState(index: number): ButtonState {
			return {
				down: !!this.downBase_[index],
				halfTransitionCount: this.halfTransBase_[index]
			};
		}

		down(index: number) {
			return !!this.downBase_[index];
		}

		pressed(index: number) {
			return this.downBase_[index] ? (this.halfTransBase_[index] > 0) : false;
		}

		released(index: number): boolean {
			return !this.downBase_[index] ? (this.halfTransBase_[index] > 0) : false;
		}

		halfTransitions(index: number) {
			return this.halfTransBase_[index];
		}

		reset() {
			this.buttonData_.clear();
			vec2.set(this.position_, 0, 0);
			vec2.set(this.positionDelta_, 0, 0);
			vec2.set(this.wheelDelta_, 0, 0);
		}

		resetPerFrameData() {
			container.fill(this.halfTransBase_, 0, this.halfTransBase_.length);
			this.positionDelta_[0] = 0;
			this.positionDelta_[1] = 0;
			this.wheelDelta_[0] = 0;
			this.wheelDelta_[1] = 0;
		}

		get position() {
			return this.position_.slice();
		}

		get positionDelta() {
			return this.positionDelta_.slice();
		}

		get wheelDelta() {
			return this.wheelDelta_.slice();
		}
	}
{'fraction_non_alphanumeric': 0.14200923279841723, 'fraction_numerical': 0.017586282699494395, 'mean_word_length': 3.527363184079602, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 14, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7619157', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1501, 'n_words': 311}