I will ommit all the parent.parent.parents to get to right level. for this example.
First off, All Variables are stored in the data file. The user clicks on buttons in game that will increase or decrease either the Cell Size or the Number of Colums. Each Cell has an OnShow property that tells the Data File its number when shown.
We will use a 3 slot bag as an example (due to its odd number).
So when the game loads up we know 3 things.
CellSize (User Sets) default = 40
Colums (User Sets) default = 4
CellNum (Game Tells Us) default = 4
User says he wants 2 Colums, Cell size 40.
Our goal is to set the Windows X and Y to a value that fits these 3 cells. The arrangment will be handled by the volume page so we do not need to worry about location.
The X value is easy to figure out.
Code:
Page.X = (CellSize)*(Colums)
Page.X=(40)*(2)
So
Page.X = 80
Now the Y value is a bit more involved.
First we figure out the area that we need
Code:
Area=(CellNum)*(CellSize)*(CellSize)
Area=(3)*(40)*(40)
Area = 4800
Next figure out how many y pixels are needed.
Code:
Pixel.Y=(Area)/(Page.X)
Pixel.Y=(4800)/(80)
Pixel.Y = 60
Now to figure out how many rows that is (so no rows are cut in half)
Code:
Rows.Base=(Pixel.Y)/(CellSize)
Rows.Base=(60)/(40)
Rows.Base = 1.5
Well we can't have 1.5 rows, so lets round up to nearest number.
First we set minimum rows as 1
Now if there are more than 1 rows then we need at least 2 rows.
Code:
Conditional = (Rows.Base) '>' (1)
Rows.Round = (conditional ? '2' : (Rows.Round))
Conditional=True => Rows.Round=2
Same thing, if there are more than 2 rows then we need at least 3 rows.
Code:
Conditional = (Rows.Base) '>' (2)
Rows.Round = (conditional ? '3' : (Rows.Round))
Conditional=False => Rows.Round stays at 2.
This would continue for all 36 cells, end result, we just need 2.
Now to figure out what our Y setting should be
Code:
Page.Y = (Rows.Round)*(CellSize)
Page.Y=(2)*(40)
Page.Y=80
So X is 80, Y is 80, now to combine them into a size.
So last tricky bit of code. We need to set the size of the page to Page.x, Page.y
Code:
bag.size=(page.x) ## ',' ## (page.y)
bag.size=(80),(80)
bag.size=80,80
Done!
I also used conditional operators to prevent the user from setting colums to 0 or to a number higher than the number of cells.
Now I also have the data file update the volume page cell size. Now that the window has been resized and the cells in the volume page have been resized the volum page will put cells 0 and 1 on the first row, cell 2 on the 2nd row.
I set each real cells location equal to the dummy volumes location and now our cells are shaped in a 2x2 grid.