<commit_before>"use strict";
/**
 * Since only a single constructor is being exported as module.exports this comment isn't documented.
 * The class and module are the same thing, the contructor comment takes precedence.
 * @module InverseGridContext
 */

var babylon = require("babylonjs");
var hexToRgb = require("../HexToRGB.js");
/**
 * This context (item factory + manager) creates a visible grid for the board.
 * This particular grid is composed of semi-transparent hexagons.
 * Instead of inifinitely tiling the plain, only a finite number are shown that fade in and out as the view moves
 * @constructor
 * @param {external:cartesian-hexagonal} hexDimensions - The DTO defining the hex <--> cartesian relation
 */
module.exports = function InverseGridContext(hexDimensions, board, color) {
  //Protect the constructor from being called as a normal method
  if (!(this instanceof InverseGridContext)) {
    return new InverseGridContext(hexDimensions, board, color);
  }
  var context = this;
  context.hexDimensions = hexDimensions;
  this.board = board;
  this.scene = board.scene;
  this.color = hexToRgb(color);

  let positionArray = createPositionArray(hexDimensions);

  var nb = positionArray.length; // nb of hexagons
  // custom position function for SPS creation
  var myPositionFunction = (particle, i) => {
    particle.position.x = positionArray[i].x; //(Math.random() - 0.5) * fact;
    particle.position.y = positionArray[i].y; //(Math.random() - 0.5) * fact;
    particle.position.z = 0; //(Math.random() - 0.5) * fact;
    //particle.rotation.x = Math.random() * 3.15;
    //particle.rotation.y = Math.random() * 3.15;
    particle.rotation.z = Math.PI / 2;
    particle.color = new babylon.Color4(
      this.color.r / 256,
      this.color.g / 256,
      this.color.b / 256,
      0.5
    );
  };

  var hexagon = babylon.MeshBuilder.CreateDisc(
    "t",
    {
      radius: hexDimensions.hexagon_half_wide_width - 2,
      tessellation: 6,
      sideOrientation: babylon.Mesh.DOUBLESIDE
    },
    this.scene
  );

  // SPS creation : Immutable {updatable: false}
  var SPS = new babylon.SolidParticleSystem("SPS", this.scene, {
    updatable: false,
    pickable: false
  });
  SPS.addShape(hexagon, nb, { positionFunction: myPositionFunction });
  var mesh = SPS.buildMesh();
  mesh.hasVertexAlpha = true;
  hexagon.rotation.z = Math.PI / 2;
  hexagon.color = new babylon.Color4(
    this.color.r / 256,
    this.color.g / 256,
    this.color.b / 256,
    0.5
  );
  this.gridParent = mesh;
  hexagon.dispose();
  this.board.addListener("pan", e => {
    //Convert the middle point to U, V
    var hexCoordinates = this.hexDimensions.getReferencePoint(
      e.middleX,
      e.middleY
    );

    //Find the center of the hex in cartesian co-ordinates
    var centerHexPixelCoordinates = this.hexDimensions.getPixelCoordinates(
      hexCoordinates.u,
      hexCoordinates.v
    );

    //Center our grid there
    context.gridParent.position.x = centerHexPixelCoordinates.x;
    context.gridParent.position.y = centerHexPixelCoordinates.y;
  });
};

/**
 * Creates a full grid from the single mesh
 * @private
 */
var createPositionArray = function(hexDimensions) {
  var positionArray = [];
  var pixelCoordinates;

  //For every hex, place an instance of the original mesh. The symbol fills in 3 of the 6 lines, the other 3 being shared with an adjacent hex

  //Make a hexagonal grid of hexagons since it is approximately circular.<commit_msg>Transparent transition<commit_after>"use strict";
/**
 * Since only a single constructor is being exported as module.exports this comment isn't documented.
 * The class and module are the same thing, the contructor comment takes precedence.
 * @module InverseGridContext
 */

var babylon = require("babylonjs");
var hexToRgb = require("../HexToRGB.js");
/**
 * This context (item factory + manager) creates a visible grid for the board.
 * This particular grid is composed of semi-transparent hexagons.
 * Instead of inifinitely tiling the plain, only a finite number are shown that fade in and out as the view moves
 * @constructor
 * @param {external:cartesian-hexagonal} hexDimensions - The DTO defining the hex <--> cartesian relation
 */
module.exports = function InverseGridContext(hexDimensions, board, color) {
  //Protect the constructor from being called as a normal method
  if (!(this instanceof InverseGridContext)) {
    return new InverseGridContext(hexDimensions, board, color);
  }
  var context = this;
  context.hexDimensions = hexDimensions;
  this.board = board;
  this.scene = board.scene;
  this.color = hexToRgb(color);
  	this.middleX = 0;
  	this.middleY = 0;
  let positionArray = createPositionArray(hexDimensions);

  var nb = positionArray.length; // nb of hexagons
  // custom position function for SPS creation
  var myPositionFunction = (particle, i) => {
    particle.position.x = positionArray[i].x; //(Math.random() - 0.5) * fact;
    particle.position.y = positionArray[i].y; //(Math.random() - 0.5) * fact;
    particle.position.z = 0; //(Math.random() - 0.5) * fact;
    //particle.rotation.x = Math.random() * 3.15;
    //particle.rotation.y = Math.random() * 3.15;
    particle.rotation.z = Math.PI / 2;

  };

  var hexagon = babylon.MeshBuilder.CreateDisc(
    "t",
    {
      radius: hexDimensions.hexagon_half_wide_width - 2,
      tessellation: 6,
      sideOrientation: babylon.Mesh.DOUBLESIDE
    },
    this.scene
  );

  // SPS creation : Immutable {updatable: false}
  var SPS = new babylon.SolidParticleSystem("SPS", this.scene, {
    updatable: true,
    pickable: false
  });
  SPS.addShape(hexagon, nb);
  
    SPS.initParticles = function () {
	  for (var p = 0; p < SPS.nbParticles; p++) {
		  myPositionFunction(SPS.particles[p], p);
		    SPS.particles[p].rotation.z = Math.PI / 2;
	  }
  };
  SPS.updateParticle = particle => {
  	let distanceFromViewPoint = Math.sqrt(Math.pow(particle.position.x - this.middleX, 2) + Math.pow(particle.position.y - this.middleY, 2));
  	let alpha = .5;
  	if (distanceFromViewPoint > 500 && distanceFromViewPoint < 1000) {
  		alpha = (500 - distanceFromViewPoint)/1000 + .5;
  	} else if (distanceFromViewPoint >= 1000) {
  		alpha = 0;
  	}
  	particle.color = new babylon.Color4(
  	    this.color.r / 256,
    this.color.g / 256,
    this.color.b / 256,
    alpha
  	);
  };
  var mesh = SPS.buildMesh();
  mesh.hasVertexAlpha = true;
  SPS.initParticles();
  SPS.setParticles();
  this.gridParent = mesh;
  hexagon.dispose();
  this.board.addListener("pan", e => {

    //Convert the middle point to U, V
    var hexCoordinates = this.hexDimensions.getReferencePoint(
      e.middleX,
      e.middleY
    );

    //Find the center of the hex in cartesian co-ordinates
    var centerHexPixelCoordinates = this.hexDimensions.getPixelCoordinates(
      hexCoordinates.u,
      hexCoordinates.v
    );

    //Center our grid there
    context.gridParent.position.x = centerHexPixelCoordinates.x;
    context.gridParent.position.y = centerHexPixelCoordinates.y;
    
    //Put the relative middleX and middleY onto the board for figuring transparency
  	this.middleX = e.middleX - centerHexPixelCoordinates.x;
  	this.middleY = e.middleY - centerHexPixelCoordinates.y;
    SPS.setParticles();
  });
};

/**
 * Creates a full grid from the single mesh
 * @private
 */
var createPositionArray = function(hexDimensions) {
  var positionArray = [];
  var pixelCoordinates;

  //For every hex, place an instance of the original mesh. The symbol fills in 3 of the 6 lines, the other 3 being shared with an adjacent hex

  //Make a hexagonal grid of hexagons since it is approximately circular.
{'fraction_non_alphanumeric': 0.11356219814645607, 'fraction_numerical': 0.013053126223730583, 'mean_word_length': 3.244875346260388, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, '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': '3085260', 'n_tokens_mistral': 2509, 'n_tokens_neox': 2373, 'n_words': 690}