Skip to content

Instantly share code, notes, and snippets.

@amitkhare
Last active November 12, 2019 11:09
Show Gist options
  • Save amitkhare/6b8ac99a1ca68c6675369bb771ef9b66 to your computer and use it in GitHub Desktop.
Save amitkhare/6b8ac99a1ca68c6675369bb771ef9b66 to your computer and use it in GitHub Desktop.
#WASP3D, #Csharp : Get transform position for newly created items
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace amitkhare
{
public class Row
{
public float height { get; set; }
public float position { get; set; }
public int index { get; set; }
public float FontSize (){
return height + 10 ;
}
public float BoundingBoxHeight (){
return FontSize() + 1f;
}
}
class GenerateItemPositionOffset
{
public static Row row(float blockHeight, float totalRows, float rowSpacing, bool isUp = false){
float rowHeight = getRowHeight(blockHeight, totalRows, rowSpacing);
float rowPos = getPosition(rowHeight, rowSpacing, 0, blockHeight, isUp);
Row row = new Row();
row.height = rowHeight;
row.position = rowPos;
return row;
}
public static List<Row> rows(float blockHeight, float totalRows, float rowSpacing,int groupRowCount = 9, bool isUp = false){
List<Row> rows = new List<Row>();
float rowHeight = getRowHeight(blockHeight, totalRows, rowSpacing);
for (int rowIndex = 0; rowIndex < groupRowCount; rowIndex++)
{
float rowPos = getPosition(rowHeight, rowSpacing, rowIndex, blockHeight, isUp);
Row row = new Row();
row.index = rowIndex;
row.height = rowHeight;
row.position = rowPos;
rows.Add(row);
}
return rows;
}
public static List<Row> rowList(float blockHeight, float totalRows, float rowSpacing, bool isUp = false)
{
List<Row> rows = new List<Row>();
float rowHeight = getRowHeight(blockHeight, totalRows, rowSpacing);
for (int rowIndex = 0; rowIndex < totalRows; rowIndex++)
{
float rowPos = getPosition(rowHeight, rowSpacing, rowIndex, blockHeight, isUp);
Row row = new Row();
row.index = rowIndex;
row.height = rowHeight;
row.position = rowPos;
rows.Add(row);
}
return rows;
}
public static float getRowHeight(float blockHeight, float totalRows, float rowSpacing = 0f)
{
float rawRowHeight = blockHeight / totalRows;
float rowHeight = rawRowHeight - rowSpacing;
return rowHeight;
}
public static float getPosition(float rowHeight, float rowSpacing, int rowIndex, float blockHeight, bool isUp = false)
{
float rowOffset = rowHeight + rowSpacing;
float rowMiddleWithOffset = (rowHeight / 2) + rowSpacing;
float rowPosition = rowOffset * rowIndex;
float rowPositionWithInitialOffset = (isUp) ? blockHeight + rowPosition : blockHeight - rowPosition;
rowPositionWithInitialOffset = rowPositionWithInitialOffset - ((rowHeight+ rowSpacing )/ 2);
return rowPositionWithInitialOffset;
}
}
}
/*
// USAGE on Trigger
private void reCalculate (){
List<Row> rows = GenerateItemPositionOffset.rows(410, WInteger.segmentCount, 5, Rows_1.Children.Count);
WFloat.RowHeight = rows[0].height;
WFloat.RowBoundingHeight = rows[0].BoundingBoxHeight();
WFloat.RowFontSize = rows[0].FontSize();
int i = 0;
foreach(Group grpRow in Rows_1.Children) {
if(Rows_1.Children.Count == rows.Count){
grpRow.Translation = new Vector3(0f,rows[i].position,0f);
i++;
}
}
}
*/
@amitkhare
Copy link
Author

amitkhare commented Nov 11, 2019

usage

on trigger

private void generateListPosition() {
  List<amitkhare.Row>  rowsList = amitkhare.GenerateItemPositionOffset.rowList(blockHeight, totalRows, rowSpacing);
  foreach (amitkhare.Row row in rowsList) {
   // log($"height: {row.height}      position: {row.position}");
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment