<commit_before>'use strict';
const Atom = require('./atom.js');

class Eye {
	constructor(client, id) {
		this.client = client;
		this.id = id;
		this.planes = new Map();
		this.atoms = new Set();
		this.last_planes = new WeakMap();
		if(client.eyes[id])
			throw new Error(`duplicate plane of id ${id}`);
		client.eyes[id] = this;

		for(let atom of client.atoms) {
			if(atom.eye_id == id) {
				atom.eye = this;
				this.atoms.add(atom);
			}
		}

		this.origin = {x:0, y:0};
	}

	draw(timestamp) {
		if(!this.canvas)
			return;
		let ctx = this.canvas.getContext('2d');
		ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
		for(let atom of this.atoms) {
			atom.on_render_tick(timestamp);
			let last_plane = this.last_planes.get(atom);
			let plane = atom.get_plane();
			if(last_plane != plane) {
				if(last_plane)
					last_plane.atoms.delete(atom);
				if(plane)
					plane.atoms.add(atom);
				this.last_planes.set(atom, plane);
			}
		}
		for(let plane of [...this.planes.values()].sort((a, b) => {return a.z_index - b.z_index;})) {
			plane.draw(ctx, timestamp);
		}
	}
	get_world_draw_pos(x, y, timestamp) {
		let {dispx, dispy} = (this.origin && this.origin.get_displacement && this.origin.get_displacement(timestamp)) || {dispx:0,dispy:0};
		return [(x-dispx+7)*32, -(y-dispy-7)*32];
	}
	create_click_handlers() {
		this.canvas.addEventListener("mousedown", this.handle_mousedown.bind(this));
	}
	get_mouse_target(e) {
		var rect = e.target.getBoundingClientRect();
		var clickX = (e.clientX - rect.left) / rect.width * e.target.width;
		var clickY = (e.clientY - rect.top) / rect.height * e.target.height;
		// Iterate through the atoms from top to bottom.
		var clickedAtom;
		let timestamp = performance.now();
		for(let plane of [...this.planes.values()].sort((a, b) => {return b.z_index - a.z_index;})) {
			if(plane.no_click)
				continue;
			let [originx, originy] = plane.calculate_origin(timestamp);
			let [offsetx, offsety] = plane.calculate_composite_offset(timestamp);
			for(let atom of [...plane.atoms].sort((a,b) => {return Atom.atom_comparator(b,a);})) {
				if(atom.mouse_opacity == undefined) {
					atom.mouse_opacity = 1;
				}
				if(atom.mouse_opacity == 0)
					continue;
				var {dispx, dispy} = atom.get_displacement(timestamp);
				let [scrx, scry] = [Math.round((dispx-originx)*32+offsetx), Math.round(plane.canvas.height-(dispy-originy)*32-32+offsety)];

				var localX = (clickX - scrx)/32;
				var localY = 1-(clickY - scry)/32;
				[localX, localY] = atom.get_transform(timestamp).inverse().multiply([localX - 0.5, localY - 0.5]);
				localX += 0.5; localY += 0.5;
				var bounds = atom.get_bounds(timestamp);
				if(bounds && localX >= bounds.x && localX < bounds.width && localY >= bounds.y && localY < bounds.height) {
					if(atom.mouse_opacity == 2 || atom.is_mouse_over(localX, localY, timestamp)) {
						clickedAtom = atom;
						break;
					}
				}
			}
		}
		if(!clickedAtom)
			return;
		console.log(clickedAtom.icon_state);
		return {"atom":clickedAtom,"x":localX,"y":localY, "ctrlKey": e.ctrlKey, "shiftKey": e.shiftKey, "altKey": e.altKey, "button": e.button};
	}

	handle_mousedown(e) {
		var start_meta = this.get_mouse_target(e);
		var start_time = performance.now();
		var mouseup = (e2) => {
			if(e2.button != e.button)
				return;
			document.removeEventListener("mouseup", mouseup);
			var end_time = performance.now();
			var end_meta = this.get_mouse_target(e2);
			console.log(end_time - start_time);
			if(end_time - start_time < 200 || end_meta.atom == start_meta.atom) {
				if(this.client.connection)
					this.client.connection.send(JSON.stringify({"click_on":Object.assign({}, start_meta, {atom: start_meta && start_meta.atom && start_meta.atom.network_id})}));
				return;
			}
			this.client.connection.send(JSON.stringify({
				"drag": {
					from: Object.assign({}, start_meta, {atom: start_meta && start_meta.atom && start_meta.atom.network_id}),
					to: Object.assign({}, end_meta, {atom: end_meta && end_meta.atom && end_meta.atom.network_id})
				}
			}));<commit_msg>Fixes lower planes having priority when it comes to click events<commit_after>'use strict';
const Atom = require('./atom.js');

class Eye {
	constructor(client, id) {
		this.client = client;
		this.id = id;
		this.planes = new Map();
		this.atoms = new Set();
		this.last_planes = new WeakMap();
		if(client.eyes[id])
			throw new Error(`duplicate plane of id ${id}`);
		client.eyes[id] = this;

		for(let atom of client.atoms) {
			if(atom.eye_id == id) {
				atom.eye = this;
				this.atoms.add(atom);
			}
		}

		this.origin = {x:0, y:0};
	}

	draw(timestamp) {
		if(!this.canvas)
			return;
		let ctx = this.canvas.getContext('2d');
		ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
		for(let atom of this.atoms) {
			atom.on_render_tick(timestamp);
			let last_plane = this.last_planes.get(atom);
			let plane = atom.get_plane();
			if(last_plane != plane) {
				if(last_plane)
					last_plane.atoms.delete(atom);
				if(plane)
					plane.atoms.add(atom);
				this.last_planes.set(atom, plane);
			}
		}
		for(let plane of [...this.planes.values()].sort((a, b) => {return a.z_index - b.z_index;})) {
			plane.draw(ctx, timestamp);
		}
	}
	get_world_draw_pos(x, y, timestamp) {
		let {dispx, dispy} = (this.origin && this.origin.get_displacement && this.origin.get_displacement(timestamp)) || {dispx:0,dispy:0};
		return [(x-dispx+7)*32, -(y-dispy-7)*32];
	}
	create_click_handlers() {
		this.canvas.addEventListener("mousedown", this.handle_mousedown.bind(this));
	}
	get_mouse_target(e) {
		var rect = e.target.getBoundingClientRect();
		var clickX = (e.clientX - rect.left) / rect.width * e.target.width;
		var clickY = (e.clientY - rect.top) / rect.height * e.target.height;
		// Iterate through the atoms from top to bottom.
		var clickedAtom;
		let timestamp = performance.now();
		for(let plane of [...this.planes.values()].sort((a, b) => {return b.z_index - a.z_index;})) {
			if(plane.no_click)
				continue;
			let [originx, originy] = plane.calculate_origin(timestamp);
			let [offsetx, offsety] = plane.calculate_composite_offset(timestamp);
			for(let atom of [...plane.atoms].sort((a,b) => {return Atom.atom_comparator(b,a);})) {
				if(atom.mouse_opacity == undefined) {
					atom.mouse_opacity = 1;
				}
				if(atom.mouse_opacity == 0)
					continue;
				var {dispx, dispy} = atom.get_displacement(timestamp);
				let [scrx, scry] = [Math.round((dispx-originx)*32+offsetx), Math.round(plane.canvas.height-(dispy-originy)*32-32+offsety)];

				var localX = (clickX - scrx)/32;
				var localY = 1-(clickY - scry)/32;
				[localX, localY] = atom.get_transform(timestamp).inverse().multiply([localX - 0.5, localY - 0.5]);
				localX += 0.5; localY += 0.5;
				var bounds = atom.get_bounds(timestamp);
				if(bounds && localX >= bounds.x && localX < bounds.width && localY >= bounds.y && localY < bounds.height) {
					if(atom.mouse_opacity == 2 || atom.is_mouse_over(localX, localY, timestamp)) {
						clickedAtom = atom;
						break;
					}
				}
			}
			if(clickedAtom)
				break;
		}
		if(!clickedAtom)
			return;
		console.log(clickedAtom.icon_state);
		return {"atom":clickedAtom,"x":localX,"y":localY, "ctrlKey": e.ctrlKey, "shiftKey": e.shiftKey, "altKey": e.altKey, "button": e.button};
	}

	handle_mousedown(e) {
		var start_meta = this.get_mouse_target(e);
		var start_time = performance.now();
		var mouseup = (e2) => {
			if(e2.button != e.button)
				return;
			document.removeEventListener("mouseup", mouseup);
			var end_time = performance.now();
			var end_meta = this.get_mouse_target(e2);
			console.log(end_time - start_time);
			if(end_time - start_time < 200 || end_meta.atom == start_meta.atom) {
				if(this.client.connection)
					this.client.connection.send(JSON.stringify({"click_on":Object.assign({}, start_meta, {atom: start_meta && start_meta.atom && start_meta.atom.network_id})}));
				return;
			}
			this.client.connection.send(JSON.stringify({
				"drag": {
					from: Object.assign({}, start_meta, {atom: start_meta && start_meta.atom && start_meta.atom.network_id}),
					to: Object.assign({}, end_meta, {atom: end_meta && end_meta.atom && end_meta.atom.network_id})
				}
			}));
{'fraction_non_alphanumeric': 0.1711119230300816, 'fraction_numerical': 0.009986603336986968, 'mean_word_length': 3.6872146118721463, 'pattern_counts': {'":': 18, '<': 9, '<?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': '6840600', 'n_tokens_mistral': 3494, 'n_tokens_neox': 3024, 'n_words': 554}