<commit_before>// Copyright © 2011 <NAME> <<EMAIL>> unless otherwise noted

define(['engine/util/Event', 'engine/client/Widget'], function (Event, Widget) {

	inherits(Viewport, Widget);
	function Viewport(client, opt /* x, y, width, height */) {
		Widget.call(this, client, opt);
		this.game = this.client.game;
		this.autoScrollRegion = 100;
		this.autoScrollMultiplier = 2.5;
		this.viewX = 0;
		this.viewY = 0;
		this.viewZoom = 1;
		this.lastMouseX = 0;
		this.lastMouseY = 0;
		this.autoScrollX = 0;
		this.autoScrollY = 0;

		this._areaSelectionActive = false;
		this._areaSelectionStartX = 0;
		this._areaSelectionStartY = 0;
		this._areaSelectionEndX = 0;
		this._areaSelectionEndY = 0;

		Event.register('didLoadAsset', this._didLoadAsset, this);
	}

	Viewport.prototype._didLoadAsset = function () {
		if (this.game.map) {
			this.viewX = this.game.fieldWidth / 2;
			this.viewY = this.game.fieldHeight / 2;
			return Event.STOP;
		}
	};

	Viewport.prototype.draw = function (gl) {
		// To make autoscrolling work, this should be called as the first thing
		// in the overridden draw method.
		if (this.autoScrollX || this.autoScrollY) {
			var factor = this.client.msecsSinceDrawn / 1000;
			this.translate(this.autoScrollX * factor, this.autoScrollY * factor);
		}
	};

	Viewport.prototype.screenToWorld = function (x, y) {
		return [x * this.viewZoom + this.viewX - this.width / 2 * this.viewZoom << 10,
				y * this.viewZoom + this.viewY - this.height / 2 * this.viewZoom << 10];
	};

	Viewport.prototype._autoScrollDimension = function (mousePos, viewportPos, viewportSize) {
		if (mousePos > viewportPos && mousePos < viewportPos + this.autoScrollRegion) {
			return (mousePos - viewportPos - this.autoScrollRegion) * this.autoScrollMultiplier;
		} else if (mousePos > viewportPos + viewportSize - this.autoScrollRegion
				&& mousePos < viewportPos + viewportSize) {
			return (mousePos - viewportPos - viewportSize + this.autoScrollRegion) * this.autoScrollMultiplier;
		} else {
			return 0;
		}
	};

	Viewport.prototype.handleClick = function (x, y) {
		var target = this.screenToWorld(x, y);
		for (var idx in this.game.actors) {
			var actor = this.game.actors[idx];
			if (actor.isSelectable() && actor.clickTest(target[0], target[1], this.client)) {
				this.client.setSelection([actor]);
				return;
			}
		}
		for (var idx in this.client.selectedActors) {
			var actor = this.client.selectedActors[idx];
			if (actor.validateMove && actor.validateMove(this.game.localPlayer, target[0], target[1])) {
				actor.issueMove(target[0], target[1]);
			}
		}
	};

	Viewport.prototype.handleMouseMove = function (x, y) {
		// Save the most recent position of the mouse to use in firing etc.
		// FIXME: Handle changes to this when zooming or scrolling. Maybe do the
		// conversion right before the value needs to be used.
		var target = this.screenToWorld(x, y);
		this.lastMouseX = target[0];
		this.lastMouseY = target[1];
		// If the mouse pointer is near the boundary of the viewport, scroll
		// the viewport automatically.
		if (this.autoScrollRegion > 0) {
			this.autoScrollX = this._autoScrollDimension(x, this.x, this.width);
			this.autoScrollY = this._autoScrollDimension(y, this.y, this.height);
		}
	};

	Viewport.prototype.handleDragStart = function (x, y) {
		this._areaSelectionStartX = x;
		this._areaSelectionStartY = y;
		this._areaSelectionActive = true;

		// Cancel automatic scrolling, because it doesn't play well with area
		// selection yet.
		this.autoScrollX = 0;
		this.autoScrollY = 0;
	};

	Viewport.prototype.handleDragMove = function (x, y) {
		this._areaSelectionEndX = x;
		this._areaSelectionEndY = y;
	};

	Viewport.prototype.handleDragDone = function (x, y) {
		// FIXME: This might not work depending on which direction the drag happens
		this.client.setSelection(this.getActorsInsideScreenRect(
			this._areaSelectionStartX, this._areaSelectionStartY,
			x, y
		));
		this._areaSelectionActive = false;
	};

	Viewport.prototype.handleMouseOut = function () {
		// Stop automatic scrolling if the mouse leaves the viewport
		this.autoScrollX = 0;
		this.autoScrollY = 0;
	};

	Viewport.prototype.handleKeyPress = function (key) {
		switch (key) {
			case 'e':
				this.zoomBy(0.5);
				break;<commit_msg>Make area selection work regardless of drag direction<commit_after>// Copyright © 2011 <NAME> <<EMAIL>> unless otherwise noted

define(['engine/util/Event', 'engine/client/Widget'], function (Event, Widget) {

	inherits(Viewport, Widget);
	function Viewport(client, opt /* x, y, width, height */) {
		Widget.call(this, client, opt);
		this.game = this.client.game;
		this.autoScrollRegion = 100;
		this.autoScrollMultiplier = 2.5;
		this.viewX = 0;
		this.viewY = 0;
		this.viewZoom = 1;
		this.lastMouseX = 0;
		this.lastMouseY = 0;
		this.autoScrollX = 0;
		this.autoScrollY = 0;

		this._areaSelectionActive = false;
		this._areaSelectionStartX = 0;
		this._areaSelectionStartY = 0;
		this._areaSelectionEndX = 0;
		this._areaSelectionEndY = 0;

		Event.register('didLoadAsset', this._didLoadAsset, this);
	}

	Viewport.prototype._didLoadAsset = function () {
		if (this.game.map) {
			this.viewX = this.game.fieldWidth / 2;
			this.viewY = this.game.fieldHeight / 2;
			return Event.STOP;
		}
	};

	Viewport.prototype.draw = function (gl) {
		// To make autoscrolling work, this should be called as the first thing
		// in the overridden draw method.
		if (this.autoScrollX || this.autoScrollY) {
			var factor = this.client.msecsSinceDrawn / 1000;
			this.translate(this.autoScrollX * factor, this.autoScrollY * factor);
		}
	};

	Viewport.prototype.screenToWorld = function (x, y) {
		return [x * this.viewZoom + this.viewX - this.width / 2 * this.viewZoom << 10,
				y * this.viewZoom + this.viewY - this.height / 2 * this.viewZoom << 10];
	};

	Viewport.prototype._autoScrollDimension = function (mousePos, viewportPos, viewportSize) {
		if (mousePos > viewportPos && mousePos < viewportPos + this.autoScrollRegion) {
			return (mousePos - viewportPos - this.autoScrollRegion) * this.autoScrollMultiplier;
		} else if (mousePos > viewportPos + viewportSize - this.autoScrollRegion
				&& mousePos < viewportPos + viewportSize) {
			return (mousePos - viewportPos - viewportSize + this.autoScrollRegion) * this.autoScrollMultiplier;
		} else {
			return 0;
		}
	};

	Viewport.prototype.handleClick = function (x, y) {
		var target = this.screenToWorld(x, y);
		for (var idx in this.game.actors) {
			var actor = this.game.actors[idx];
			if (actor.isSelectable() && actor.clickTest(target[0], target[1], this.client)) {
				this.client.setSelection([actor]);
				return;
			}
		}
		for (var idx in this.client.selectedActors) {
			var actor = this.client.selectedActors[idx];
			if (actor.validateMove && actor.validateMove(this.game.localPlayer, target[0], target[1])) {
				actor.issueMove(target[0], target[1]);
			}
		}
	};

	Viewport.prototype.handleMouseMove = function (x, y) {
		// Save the most recent position of the mouse to use in firing etc.
		// FIXME: Handle changes to this when zooming or scrolling. Maybe do the
		// conversion right before the value needs to be used.
		var target = this.screenToWorld(x, y);
		this.lastMouseX = target[0];
		this.lastMouseY = target[1];
		// If the mouse pointer is near the boundary of the viewport, scroll
		// the viewport automatically.
		if (this.autoScrollRegion > 0) {
			this.autoScrollX = this._autoScrollDimension(x, this.x, this.width);
			this.autoScrollY = this._autoScrollDimension(y, this.y, this.height);
		}
	};

	Viewport.prototype.handleDragStart = function (x, y) {
		this._areaSelectionStartX = x;
		this._areaSelectionStartY = y;
		this._areaSelectionActive = true;

		// Cancel automatic scrolling, because it doesn't play well with area
		// selection yet.
		this.autoScrollX = 0;
		this.autoScrollY = 0;
	};

	Viewport.prototype.handleDragMove = function (x, y) {
		this._areaSelectionEndX = x;
		this._areaSelectionEndY = y;
	};

	Viewport.prototype.handleDragDone = function (x, y) {
		var sx = this._areaSelectionStartX;
		var sy = this._areaSelectionStartY;
		this.client.setSelection(this.getActorsInsideScreenRect(
			(sx <= x ? sx : x),
			(sy <= y ? sy : y),
			(sx <= x ? x : sx),
			(sy <= y ? y : sy)
		));
		this._areaSelectionActive = false;
	};

	Viewport.prototype.handleMouseOut = function () {
		// Stop automatic scrolling if the mouse leaves the viewport
		this.autoScrollX = 0;
		this.autoScrollY = 0;
	};

	Viewport.prototype.handleKeyPress = function (key) {
		switch (key) {
			case 'e':
				this.zoomBy(0.5);
				break;
{'fraction_non_alphanumeric': 0.12558193668528864, 'fraction_numerical': 0.0111731843575419, 'mean_word_length': 3.6625067824199675, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 15, 'https://': 0, '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': '4528096', 'n_tokens_mistral': 3179, 'n_tokens_neox': 2833, 'n_words': 704}