//
// LEDWallGrid - This script will create a grid of squares for an LED wall.
//
//  Flip Phillips
//  Darren Alexander (from FidoLED)
//
//  Winter 2024
// 
//  (c) Darren Alexander 2015
//  (c) Flip Phillips 2024 but released under the MIT license
//

#target photoshop

app.bringToFront();

$.localize = true;

// try {
// }
// catch (e) {
// }

//
// the things to set
//

var canvasWidth = 1920;
var canvasHeight = 1080;

var ModuleW = 5;
var ModuleH = 4;
var ColumnOffset = 0;

var pixelpitchwide = 88;
var pixelpitchhigh = 88;

var guides = false;
var randomColors = true;
var numbering = true;

// the things we compute

var width = ModuleW * pixelpitchwide;
var height = ModuleH * pixelpitchhigh;

var defaultRulerUnits = preferences.rulerUnits; preferences.rulerUnits = Units.PIXELS;
var doc = documents.add(canvasWidth, canvasHeight, 72, "LED Grids", NewDocumentMode.RGB, DocumentFill.TRANSPARENT, 1.0);  

//
// make guides
//
function makeGuide(pixelOffSet, orientation) {
  var id8 = charIDToTypeID("Mk  ");
  var desc4 = new ActionDescriptor();
  var id9 = charIDToTypeID("Nw  ");
  var desc5 = new ActionDescriptor();
  var id10 = charIDToTypeID("Pstn");
  var id11 = charIDToTypeID("#Rlt");

  desc5.putUnitDouble(id10, id11, pixelOffSet); // integer

  var id12 = charIDToTypeID("Ornt");
  var id13 = charIDToTypeID("Ornt");
  var id14 = charIDToTypeID(orientation);
  desc5.putEnumerated(id12, id13, id14);
  var id15 = charIDToTypeID("Gd  ");

  desc4.putObject(id9, id15, desc5);

  executeAction(id8, desc4, DialogModes.NO);
}

if (guides) {
  for (i = 0; i <= ModuleW; i++) {
    makeGuide(pixelpitchwide * i, "Vrtc");
  }

  for (i = 0; i <= ModuleH; i++) {
    makeGuide(pixelpitchhigh * i, "Hrzn");
  }
}

//
// make boxes
//

// row colum globals?
var c = 0;
var r = 0;
var colourcycle = 0;

function makegrids(ppitch, colourcycle) { 
  for (s = 0; s <= ModuleH - 1; s++) { // this loop created a filled in column - now I need to move over 1 module and do more!
    var Colour1 = new SolidColor;
    var Colour2 = new SolidColor;

    if (randomColors) {
      Colour1.rgb.red = Math.round(Math.random() * 255);
      Colour1.rgb.green = Math.round(Math.random() * 255);
      Colour1.rgb.blue = Math.round(Math.random() * 255);

      Colour2.rgb.red = Math.round(Math.random() * 255);
      Colour2.rgb.green = Math.round(Math.random() * 255);
      Colour2.rgb.blue = Math.round(Math.random() * 255);
    }
    else {
      Colour1.rgb.red = 255;
      Colour1.rgb.green = 68;
      Colour1.rgb.blue = 34;

      Colour2.rgb.red = 0;
      Colour2.rgb.green = 170;
      Colour2.rgb.blue = 238;
    }

    var X1 = Number(ppitch) //Number((z*ModuleW)*pixelpitchwide) // point 1 of the selection
    var Y1 = Number(r * pixelpitchhigh) //Number((z*ModuleH)*pixelpitchhigh) // point 1 of the selection
    var X2 = Number(X1 + pixelpitchwide) // point 2 of the selection
    var Y2 = Number(r * pixelpitchhigh) //Number((z*ModuleH)*pixelpitchhigh) // point 2 of the selection
    var X3 = Number(X1 + pixelpitchwide) // point 3 of the selection
    var Y3 = Number(Y1 + pixelpitchhigh) // Point 3 of the selection
    var X4 = Number(ppitch) //Number((z*ModuleW)*pixelpitchwide) // point 4 of the selection
    var Y4 = Number(Y1 + pixelpitchhigh) // Point 4 of the selection
    var selectionBounds = [[X1, Y1], [X2, Y2], [X3, Y3], [X4, Y4]];

    curDoc.selection.select(selectionBounds, SelectionType.REPLACE, 0, false);

    if (colourcycle == 0) {
      curDoc.selection.fill(Colour1);
      var colourcycle = 1;
    }
    else {
      curDoc.selection.fill(Colour2);
      var colourcycle = 0;
    }

    curDoc.selection.deselect();
    r = r + 1;
  }
}

var curDoc = activeDocument;
var newLayer = curDoc.artLayers.add();
newLayer.name = "TheGrids";

for (t = 0; t <= ModuleW - 1; t++) {  //this is the loop for doing the column
  var ppitch = (t * pixelpitchwide);
  makegrids(ppitch, colourcycle);
  r = 0;

  if (colourcycle == 0) { // I need to flip the colours over at the start of a new column
    var colourcycle = 1;
  }
  else {
    var colourcycle = 0;
  }
}

//
// numbering
//

var textpitchx = 0;
var textpitchy = 0;

function addtext(columnnumber) {

  // suppress all dialogs
  app.displayDialogs = DialogModes.NO;

  var AD = activeDocument;

  var TextLayer = AD.artLayers.add();
  TextLayer.kind = LayerKind.TEXT;

  var txtRef = TextLayer.textItem;
  txtRef.color = new SolidColor(255, 255, 255); // Set text color to white
  txtRef.strokeColor = new SolidColor(0, 0, 0); // Set stroke color to black
  txtRef.strokeWidth = 1; // Set stroke width 
  txtRef.createOutline(); // Create outline of the text
  txtRef.font = "Trebuchet MS";
  txtRef.antiAliasMethod = AntiAlias.SHARP;
  txtRef.justification = Justification.CENTER;
  //txtRef.anchorposition = AnchorPosition.MIDDLECENTER;
  txtRef.position = Array((textpitchx + (pixelpitchwide / 2)), (textpitchy + (pixelpitchhigh / 1.4)));  //20,46
  txtRef.size = (pixelpitchwide / 2.0); 
  txtRef.contents = columnnumber + 1;

}

var nn = 0;

for (a = 1; a <= ModuleH; a++) { 
  for (q = 0; q <= ModuleW - 1; q++) {
    addtext(nn + ColumnOffset);
    textpitchx = ((q + 1) * pixelpitchwide); //this moves thetext along the columns
    nn = nn + 1;
  }

  textpitchx = 0;
  textpitchy = ((a) * pixelpitchhigh);
  q = 0;
}

// ok here I want to hide the background layer and the grid layer and flatten the text layers
curDoc.activeLayer = curDoc.layers.TheGrids;
curDoc.activeLayer.visible = 0;
curDoc.activeLayer = curDoc.layers[curDoc.layers.length - 3]; // this makes the 3rd Layer active
curDoc.mergeVisibleLayers();

curDoc.activeLayer = curDoc.layers.TheGrids;
curDoc.activeLayer.visible = 1;