Built basic UI Grid.
This commit is contained in:
118
src/ui/grid.c
Normal file
118
src/ui/grid.c
Normal file
@ -0,0 +1,118 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "grid.h"
|
||||
|
||||
void gridInit(grid_t *grid) {
|
||||
grid->breakpointCount = 0x00;
|
||||
grid->childCount = 0x00;
|
||||
grid->width = 0;
|
||||
grid->height = 0;
|
||||
grid->x = 0;
|
||||
grid->y = 0;
|
||||
}
|
||||
|
||||
uint8_t gridAddBreakpoint(
|
||||
grid_t *grid, float width,
|
||||
uint8_t rows, uint8_t columns,
|
||||
float gutterX, float gutterY
|
||||
) {
|
||||
uint8_t i;
|
||||
i = grid->breakpointCount;
|
||||
grid->grids[i].rows = rows;
|
||||
grid->grids[i].columns = columns;
|
||||
grid->grids[i].gutterX = gutterX;
|
||||
grid->grids[i].gutterY = gutterY;
|
||||
grid->breakpoints[i].width = width;
|
||||
grid->breakpointCount++;
|
||||
return i;
|
||||
}
|
||||
|
||||
gridchild_t * gridAddChild(grid_t *grid) {
|
||||
gridchild_t *child = grid->children + grid->childCount;
|
||||
child->defCount = 1;
|
||||
child->defs[0].columns = 1;
|
||||
child->defs[0].rows = 1;
|
||||
child->defs[0].x = 0;
|
||||
child->defs[0].y = 0;
|
||||
grid->childCount++;
|
||||
return child;
|
||||
}
|
||||
|
||||
void gridSetSize(grid_t *grid,
|
||||
float screenWidth, float screenHeight,
|
||||
float width, float height,
|
||||
float x, float y
|
||||
) {
|
||||
uint8_t i, breakpoint;
|
||||
gridchild_t *item;
|
||||
gridchilddef_t *itemdef;
|
||||
griddef_t griddef;
|
||||
float sizeCol, sizeRow, gx, gy, gw, gh;
|
||||
|
||||
// Need to resize?
|
||||
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 = 0xFF;
|
||||
for(i = 0; i < grid->breakpointCount; i++) {
|
||||
if(grid->breakpoints[i].width < screenWidth) continue;
|
||||
breakpoint = i;
|
||||
}
|
||||
if(breakpoint == 0xFF) breakpoint = grid->breakpointCount - 1;
|
||||
griddef = grid->grids[breakpoint];
|
||||
|
||||
// Determine the size of a single column
|
||||
sizeCol = (width - (
|
||||
griddef.gutterX * (griddef.columns - 1)
|
||||
)) / griddef.columns;
|
||||
|
||||
sizeRow = (height - (
|
||||
griddef.gutterY * (griddef.rows - 1)
|
||||
)) / griddef.rows;
|
||||
|
||||
// Resize children
|
||||
for(i = 0; i < grid->childCount; i++) {
|
||||
// Get the item and the definition.
|
||||
item = grid->children + i;
|
||||
itemdef = item->defs + (
|
||||
breakpoint >= item->defCount ? item->defCount-1 : breakpoint
|
||||
);
|
||||
|
||||
// Get the local X/Y
|
||||
gx = (sizeCol * itemdef->x) + (griddef.gutterX * itemdef->x);
|
||||
gy = (sizeRow * itemdef->y) + (griddef.gutterY * itemdef->y);
|
||||
|
||||
// Get the width/height
|
||||
gw = (
|
||||
(itemdef->columns * sizeCol) +
|
||||
(mathMax(itemdef->columns - 1, 0) * griddef.gutterX)
|
||||
);
|
||||
gh = (
|
||||
(itemdef->rows * sizeRow) +
|
||||
(mathMax(itemdef->rows - 1, 0) * griddef.gutterY)
|
||||
);
|
||||
|
||||
item->onResize(
|
||||
grid->user, i,
|
||||
screenWidth, screenHeight,
|
||||
x + gx,y + gy,
|
||||
gw, gh
|
||||
);
|
||||
}
|
||||
}
|
77
src/ui/grid.h
Normal file
77
src/ui/grid.h
Normal file
@ -0,0 +1,77 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <dawn/dawn.h>
|
||||
|
||||
#define BREAKPOINT_COUNT 4
|
||||
|
||||
#define GRID_CHILD_COUNT 32
|
||||
|
||||
typedef struct {
|
||||
float width;
|
||||
} breakpoint_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t rows;
|
||||
uint8_t columns;
|
||||
float gutterX;
|
||||
float gutterY;
|
||||
} griddef_t;
|
||||
|
||||
typedef void gridchildresizecallback_t(
|
||||
void *user, int32_t i,
|
||||
float screenWidth, float screenHeight,
|
||||
float x, float y,
|
||||
float width, float height
|
||||
);
|
||||
|
||||
typedef struct {
|
||||
uint8_t columns;
|
||||
uint8_t rows;
|
||||
uint8_t x;
|
||||
uint8_t y;
|
||||
} gridchilddef_t;
|
||||
|
||||
typedef struct {
|
||||
gridchilddef_t defs[BREAKPOINT_COUNT];
|
||||
uint8_t defCount;
|
||||
gridchildresizecallback_t *onResize;
|
||||
} gridchild_t;
|
||||
|
||||
typedef struct {
|
||||
griddef_t grids[BREAKPOINT_COUNT];
|
||||
breakpoint_t breakpoints[BREAKPOINT_COUNT];
|
||||
gridchild_t children[GRID_CHILD_COUNT];
|
||||
uint8_t childCount;
|
||||
|
||||
float width;
|
||||
float height;
|
||||
float x;
|
||||
float y;
|
||||
|
||||
uint8_t breakpointCount;
|
||||
void *user;
|
||||
} grid_t;
|
||||
|
||||
|
||||
void gridInit(grid_t *grid);
|
||||
|
||||
uint8_t gridAddBreakpoint(
|
||||
grid_t *grid, float width,
|
||||
uint8_t rows, uint8_t columns,
|
||||
float gutterX, float gutterY
|
||||
);
|
||||
|
||||
gridchild_t * gridAddChild(grid_t *grid);
|
||||
|
||||
|
||||
void gridSetSize(grid_t *grid,
|
||||
float screenWidth, float screenHeight,
|
||||
float width, float height,
|
||||
float x, float y
|
||||
);
|
Reference in New Issue
Block a user