Added grid cell alignment
This commit is contained in:
@ -33,7 +33,12 @@ align_t alignmentGet(
|
||||
float childWidth, float childHeight,
|
||||
float childX, float childY
|
||||
) {
|
||||
align_t out;
|
||||
align_t out = {
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.width = 0,
|
||||
.height = 0
|
||||
};
|
||||
|
||||
if(alignX & ALIGN_SIZE_RATIO) {
|
||||
// Work out Y size and alignment first, then we base off that.
|
||||
|
106
src/ui/grid.c
106
src/ui/grid.c
@ -74,58 +74,114 @@ void gridSetSize(grid_t *grid,
|
||||
float x, float y
|
||||
) {
|
||||
uint8_t i, breakpoint;
|
||||
gridchild_t *child;
|
||||
gridchildbreakpoint_t *childbp;
|
||||
gridbreakpoint_t *gridbp;
|
||||
float sizeCol, sizeRow, gx, gy, gw, gh;
|
||||
|
||||
// Need to resize?
|
||||
if(grid->width == width && grid->height == height) {
|
||||
if(
|
||||
grid->width == width && grid->height == height &&
|
||||
grid->x == x && grid->y == y
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Update properties
|
||||
grid->width = width;
|
||||
grid->height = height;
|
||||
grid->x = x;
|
||||
grid->y = y;
|
||||
|
||||
// Determine breakpoint
|
||||
breakpoint = gridGetBreakpoint(grid, screenWidth);
|
||||
gridbp = grid->breakpoints + breakpoint;
|
||||
grid->breakpointCurrent = breakpoint;
|
||||
|
||||
// Determine the size of a single column/row
|
||||
sizeCol = (width - (gridbp->gutterX * (gridbp->columns-1))) / gridbp->columns;
|
||||
sizeRow = (height - (gridbp->gutterY * (gridbp->rows - 1))) / gridbp->rows;
|
||||
|
||||
if(grid->onResize == NULL) return;
|
||||
|
||||
sizeCol = -1;
|
||||
sizeRow = -1;
|
||||
|
||||
// Resize children
|
||||
for(i = 0; i < grid->childCount; i++) {
|
||||
// Get the item and the definition.
|
||||
child = grid->children + i;
|
||||
|
||||
childbp = gridChildGetBreakpoint(child, breakpoint);
|
||||
|
||||
// Get the local X/Y
|
||||
gx = (sizeCol * childbp->x) + (gridbp->gutterX * childbp->x);
|
||||
gy = (sizeRow * childbp->y) + (gridbp->gutterY * childbp->y);
|
||||
|
||||
// Get the width/height
|
||||
gw = (
|
||||
(childbp->columns * sizeCol) +
|
||||
(mathMax(childbp->columns - 1, 0) * gridbp->gutterX)
|
||||
);
|
||||
gh = (
|
||||
(childbp->rows * sizeRow) +
|
||||
(mathMax(childbp->rows - 1, 0) * gridbp->gutterY)
|
||||
gridGetChildSize(
|
||||
grid, breakpoint, grid->children + i,
|
||||
&sizeCol, &sizeRow, &gx, &gy, &gw, &gh
|
||||
);
|
||||
|
||||
// Fire the resize event.
|
||||
grid->onResize(
|
||||
grid->user, i, childbp,
|
||||
grid->user, i, gridChildGetBreakpoint(grid->children + i, breakpoint),
|
||||
screenWidth, screenHeight,
|
||||
x + gx, y + gy,
|
||||
gw, gh
|
||||
gx, gy, gw, gh
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void gridGetChildSize(
|
||||
grid_t *grid, uint8_t breakpoint, gridchild_t *child,
|
||||
float *sizeCol, float *sizeRow,
|
||||
float *x, float *y, float *width, float *height
|
||||
) {
|
||||
gridchildbreakpoint_t *childbp;
|
||||
gridbreakpoint_t *gridbp;
|
||||
|
||||
gridbp = grid->breakpoints + breakpoint;
|
||||
|
||||
if(*sizeCol < 0) {
|
||||
*sizeCol = (
|
||||
grid->width - (gridbp->gutterX * (gridbp->columns-1))
|
||||
) / gridbp->columns;
|
||||
}
|
||||
|
||||
if(*sizeRow < 0) {
|
||||
*sizeRow = (
|
||||
grid->height - (gridbp->gutterY * (gridbp->rows - 1))
|
||||
) / gridbp->rows;
|
||||
}
|
||||
|
||||
childbp = gridChildGetBreakpoint(child, breakpoint);
|
||||
|
||||
*x = (*sizeCol * childbp->x) + (gridbp->gutterX * childbp->x) + grid->x;
|
||||
*y = (*sizeRow * childbp->y) + (gridbp->gutterY * childbp->y) + grid->y;
|
||||
|
||||
*width = (
|
||||
(childbp->columns * *sizeCol) +
|
||||
(mathMax(childbp->columns - 1, 0) * gridbp->gutterX)
|
||||
);
|
||||
*height = (
|
||||
(childbp->rows * *sizeRow) +
|
||||
(mathMax(childbp->rows - 1, 0) * gridbp->gutterY)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
align_t gridAlignChild(
|
||||
grid_t *grid, uint8_t breakpoint, gridchild_t *child,
|
||||
float *sizeCol, float *sizeRow,
|
||||
uint8_t alignX, uint8_t alignY,
|
||||
float width, float height
|
||||
) {
|
||||
float gridWidth, gridHeight, gridX, gridY;
|
||||
align_t alignment;
|
||||
|
||||
// Get the child size.
|
||||
gridGetChildSize(
|
||||
grid, breakpoint, child, sizeCol, sizeRow,
|
||||
&gridX, &gridY, &gridWidth, &gridHeight
|
||||
);
|
||||
|
||||
if(width == -1) width = gridWidth;
|
||||
if(height == -1) height = gridHeight;
|
||||
|
||||
// Align the child
|
||||
alignment = alignmentGet(
|
||||
alignX, alignY, gridWidth, gridHeight, width, height, -1, -1
|
||||
);
|
||||
|
||||
alignment.x += gridX;
|
||||
alignment.y += gridY;
|
||||
|
||||
return alignment;
|
||||
}
|
@ -7,6 +7,7 @@
|
||||
|
||||
#pragma once
|
||||
#include <dawn/dawn.h>
|
||||
#include "align.h"
|
||||
|
||||
/**
|
||||
* Initialize a grid system.
|
||||
@ -83,11 +84,51 @@ gridchildbreakpoint_t * gridChildGetBreakpoint(gridchild_t *child, uint8_t bp);
|
||||
* @param screenHeight Current screen height.
|
||||
* @param width Width of the grid itself, useful for nested grids.
|
||||
* @param height Height of the grid itself, useful for nested grids.
|
||||
* @param x X position of this grid (to offset children by).
|
||||
* @param y Y position of this grid (to offset children by).
|
||||
* @param x X position of this grid (to offset children by). Only for events.
|
||||
* @param y Y position of this grid (to offset children by). Only for events.
|
||||
*/
|
||||
void gridSetSize(grid_t *grid,
|
||||
float screenWidth, float screenHeight,
|
||||
float width, float height,
|
||||
float x, float y
|
||||
);
|
||||
|
||||
/**
|
||||
* Get the size of a grid child.
|
||||
*
|
||||
* @param grid Grid to get from.
|
||||
* @param breakpoint Breakpoint index to get.
|
||||
* @param child Grid child to get for.
|
||||
* @param sizeCol Pointer to a float that will/already holds the column size.
|
||||
* @param sizeRow Poitner to a float that will/aready holds the row size.
|
||||
* @param x Pointer to the output X position.
|
||||
* @param y Pointer to the output Y position.
|
||||
* @param width Pointer to the output width.
|
||||
* @param height Pointer to the output height.
|
||||
*/
|
||||
void gridGetChildSize(
|
||||
grid_t *grid, uint8_t breakpoint, gridchild_t *child,
|
||||
float *sizeCol, float *sizeRow,
|
||||
float *x, float *y, float *width, float *height
|
||||
);
|
||||
|
||||
/**
|
||||
* Align a grid item child.
|
||||
*
|
||||
* @param grid Grid to align within.
|
||||
* @param breakpoint Breakpoint index.
|
||||
* @param child Grid child.
|
||||
* @param sizeCol Pointer to a float that will/already holds the column size.
|
||||
* @param sizeRow Poitner to a float that will/aready holds the row size.
|
||||
* @param alignX Alignment options for the X axis.
|
||||
* @param alignY Alignment options for the Y axis.
|
||||
* @param width Width of the child item to be aligned.
|
||||
* @param height Height of the child item to be aligned.
|
||||
* @return The alignment within the grid cell.
|
||||
*/
|
||||
align_t gridAlignChild(
|
||||
grid_t *grid, uint8_t breakpoint, gridchild_t *child,
|
||||
float *sizeCol, float *sizeRow,
|
||||
uint8_t alignX, uint8_t alignY,
|
||||
float width, float height
|
||||
);
|
Reference in New Issue
Block a user