mirror of
https://github.com/nicbarker/clay.git
synced 2025-05-07 19:08:02 +00:00
Compare commits
6 Commits
101371ac37
...
55233479f4
Author | SHA1 | Date | |
---|---|---|---|
|
55233479f4 | ||
|
cd82ce6fcf | ||
|
814c9392c6 | ||
|
ea3e29be5c | ||
|
134beca09c | ||
|
b3cdf90d39 |
5
bindings/D/README.md
Normal file
5
bindings/D/README.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
### D Language Example
|
||||||
|
|
||||||
|
```
|
||||||
|
dmd main.d clay.c
|
||||||
|
```
|
2
bindings/D/clay.c
Normal file
2
bindings/D/clay.c
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
#define CLAY_IMPLEMENTATION
|
||||||
|
#include "../../clay.h"
|
88
bindings/D/main.d
Normal file
88
bindings/D/main.d
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
import clay;
|
||||||
|
|
||||||
|
import core.stdc.stdlib;
|
||||||
|
|
||||||
|
__gshared:
|
||||||
|
|
||||||
|
Clay_LayoutConfig layoutElement = { padding: {5} };
|
||||||
|
|
||||||
|
extern(C) void main()
|
||||||
|
{
|
||||||
|
ulong totalMemorySize = Clay_MinMemorySize();
|
||||||
|
Clay_Arena clayMemory = {
|
||||||
|
label: str("Clay Memory Arena"),
|
||||||
|
capacity: totalMemorySize,
|
||||||
|
memory: cast(char*)malloc(totalMemorySize)
|
||||||
|
};
|
||||||
|
|
||||||
|
Clay_Initialize(clayMemory, Clay_Dimensions(1024,768));
|
||||||
|
Clay_BeginLayout();
|
||||||
|
if (ClayBegin( Rectangle(color: Clay_Color(255,255,255,0)), Layout(layoutElement)))
|
||||||
|
{ }
|
||||||
|
ClayEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// helper functions
|
||||||
|
Clay_String str(string it)
|
||||||
|
{
|
||||||
|
return Clay_String(cast(int)it.length, it.ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ClayBegin(A...)(A configs)
|
||||||
|
{
|
||||||
|
Clay__OpenElement();
|
||||||
|
foreach(config; configs)
|
||||||
|
{
|
||||||
|
alias T = typeof(config);
|
||||||
|
static if (is(T == Clay_ElementId))
|
||||||
|
{
|
||||||
|
Clay__AttachId(config);
|
||||||
|
}
|
||||||
|
else static if(is(T == Clay_LayoutConfig*))
|
||||||
|
{
|
||||||
|
Clay__AttachLayoutConfig(config);
|
||||||
|
}
|
||||||
|
else static if(is(T == Clay_ElementConfig))
|
||||||
|
{
|
||||||
|
Clay__AttachElementConfig(config.config, config.type);
|
||||||
|
}
|
||||||
|
else static assert(0, "unsupported " ~ typeof(config).stringof);
|
||||||
|
}
|
||||||
|
|
||||||
|
Clay__ElementPostConfiguration();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClayEnd()
|
||||||
|
{
|
||||||
|
Clay__CloseElement();
|
||||||
|
}
|
||||||
|
|
||||||
|
Clay_ElementId Id(string label)
|
||||||
|
{
|
||||||
|
return Clay__HashString(str(label), 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
Clay_LayoutConfig* Layout(lazy Clay_Sizing sizing = Clay_Sizing.init)
|
||||||
|
{
|
||||||
|
Clay_LayoutConfig config;
|
||||||
|
config.sizing = sizing;
|
||||||
|
return Clay__StoreLayoutConfig(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
Clay_LayoutConfig* Layout(Clay_LayoutConfig config)
|
||||||
|
{
|
||||||
|
return Clay__StoreLayoutConfig(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
Clay_ElementConfig Rectangle(lazy Clay_Color color = Clay_Color.init)
|
||||||
|
{
|
||||||
|
Clay_RectangleElementConfig config;
|
||||||
|
config.color = color;
|
||||||
|
|
||||||
|
Clay_ElementConfig ret;
|
||||||
|
ret.type = Clay__ElementConfigType.CLAY__ELEMENT_CONFIG_TYPE_RECTANGLE;
|
||||||
|
ret.config.rectangleElementConfig = Clay__StoreRectangleElementConfig(config);
|
||||||
|
return ret;
|
||||||
|
}
|
43
clay.h
43
clay.h
@ -443,6 +443,13 @@ CLAY__TYPEDEF(Clay_ScrollContainerData, struct {
|
|||||||
bool found;
|
bool found;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
CLAY__TYPEDEF(Clay_ElementData, struct
|
||||||
|
{
|
||||||
|
Clay_BoundingBox boundingBox;
|
||||||
|
// Indicates whether an actual Element matched the provided ID or if the default struct was returned.
|
||||||
|
bool found;
|
||||||
|
});
|
||||||
|
|
||||||
CLAY__TYPEDEF(Clay_RenderCommandType, CLAY_PACKED_ENUM {
|
CLAY__TYPEDEF(Clay_RenderCommandType, CLAY_PACKED_ENUM {
|
||||||
CLAY_RENDER_COMMAND_TYPE_NONE,
|
CLAY_RENDER_COMMAND_TYPE_NONE,
|
||||||
CLAY_RENDER_COMMAND_TYPE_RECTANGLE,
|
CLAY_RENDER_COMMAND_TYPE_RECTANGLE,
|
||||||
@ -515,6 +522,7 @@ void Clay_BeginLayout(void);
|
|||||||
Clay_RenderCommandArray Clay_EndLayout(void);
|
Clay_RenderCommandArray Clay_EndLayout(void);
|
||||||
Clay_ElementId Clay_GetElementId(Clay_String idString);
|
Clay_ElementId Clay_GetElementId(Clay_String idString);
|
||||||
Clay_ElementId Clay_GetElementIdWithIndex(Clay_String idString, uint32_t index);
|
Clay_ElementId Clay_GetElementIdWithIndex(Clay_String idString, uint32_t index);
|
||||||
|
Clay_ElementData Clay_GetElementData (Clay_ElementId id);
|
||||||
bool Clay_Hovered(void);
|
bool Clay_Hovered(void);
|
||||||
void Clay_OnHover(void (*onHoverFunction)(Clay_ElementId elementId, Clay_PointerData pointerData, intptr_t userData), intptr_t userData);
|
void Clay_OnHover(void (*onHoverFunction)(Clay_ElementId elementId, Clay_PointerData pointerData, intptr_t userData), intptr_t userData);
|
||||||
bool Clay_PointerOver(Clay_ElementId elementId);
|
bool Clay_PointerOver(Clay_ElementId elementId);
|
||||||
@ -2109,9 +2117,9 @@ void Clay__InitializePersistentMemory(Clay_Context* context) {
|
|||||||
void Clay__CompressChildrenAlongAxis(bool xAxis, float totalSizeToDistribute, Clay__int32_tArray resizableContainerBuffer) {
|
void Clay__CompressChildrenAlongAxis(bool xAxis, float totalSizeToDistribute, Clay__int32_tArray resizableContainerBuffer) {
|
||||||
Clay_Context* context = Clay_GetCurrentContext();
|
Clay_Context* context = Clay_GetCurrentContext();
|
||||||
Clay__int32_tArray largestContainers = context->openClipElementStack;
|
Clay__int32_tArray largestContainers = context->openClipElementStack;
|
||||||
largestContainers.length = 0;
|
|
||||||
|
|
||||||
while (totalSizeToDistribute > 0.1) {
|
while (totalSizeToDistribute > 0.1) {
|
||||||
|
largestContainers.length = 0;
|
||||||
float largestSize = 0;
|
float largestSize = 0;
|
||||||
float targetSize = 0;
|
float targetSize = 0;
|
||||||
for (int32_t i = 0; i < resizableContainerBuffer.length; ++i) {
|
for (int32_t i = 0; i < resizableContainerBuffer.length; ++i) {
|
||||||
@ -2133,23 +2141,29 @@ void Clay__CompressChildrenAlongAxis(bool xAxis, float totalSizeToDistribute, Cl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (largestContainers.length == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
targetSize = CLAY__MAX(targetSize, (largestSize * largestContainers.length) - totalSizeToDistribute) / largestContainers.length;
|
targetSize = CLAY__MAX(targetSize, (largestSize * largestContainers.length) - totalSizeToDistribute) / largestContainers.length;
|
||||||
|
|
||||||
for (int32_t childOffset = 0; childOffset < largestContainers.length; childOffset++) {
|
for (int32_t childOffset = 0; childOffset < largestContainers.length; childOffset++) {
|
||||||
Clay_LayoutElement *childElement = Clay_LayoutElementArray_Get(&context->layoutElements, Clay__int32_tArray_Get(&largestContainers, childOffset));
|
int32_t childIndex = Clay__int32_tArray_Get(&largestContainers, childOffset);
|
||||||
|
Clay_LayoutElement *childElement = Clay_LayoutElementArray_Get(&context->layoutElements, childIndex);
|
||||||
float *childSize = xAxis ? &childElement->dimensions.width : &childElement->dimensions.height;
|
float *childSize = xAxis ? &childElement->dimensions.width : &childElement->dimensions.height;
|
||||||
float childMinSize = xAxis ? childElement->minDimensions.width : childElement->minDimensions.height;
|
float childMinSize = xAxis ? childElement->minDimensions.width : childElement->minDimensions.height;
|
||||||
float oldChildSize = *childSize;
|
float oldChildSize = *childSize;
|
||||||
*childSize = CLAY__MAX(childMinSize, targetSize);
|
*childSize = CLAY__MAX(childMinSize, targetSize);
|
||||||
totalSizeToDistribute -= (oldChildSize - *childSize);
|
totalSizeToDistribute -= (oldChildSize - *childSize);
|
||||||
if (*childSize == childMinSize) {
|
if (*childSize == childMinSize) {
|
||||||
Clay__int32_tArray_RemoveSwapback(&largestContainers, childOffset);
|
for (int32_t i = 0; i < resizableContainerBuffer.length; i++) {
|
||||||
childOffset--;
|
if (Clay__int32_tArray_Get(&resizableContainerBuffer, i) == childIndex) {
|
||||||
|
Clay__int32_tArray_RemoveSwapback(&resizableContainerBuffer, i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (largestContainers.length == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -4005,6 +4019,19 @@ Clay_ScrollContainerData Clay_GetScrollContainerData(Clay_ElementId id) {
|
|||||||
return CLAY__INIT(Clay_ScrollContainerData) CLAY__DEFAULT_STRUCT;
|
return CLAY__INIT(Clay_ScrollContainerData) CLAY__DEFAULT_STRUCT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CLAY_WASM_EXPORT("Clay_GetElementData")
|
||||||
|
Clay_ElementData Clay_GetElementData(Clay_ElementId id){
|
||||||
|
Clay_LayoutElementHashMapItem * item = Clay__GetHashMapItem(id.id);
|
||||||
|
if(item == &CLAY__LAYOUT_ELEMENT_HASH_MAP_ITEM_DEFAULT) {
|
||||||
|
return CLAY__INIT(Clay_ElementData) CLAY__DEFAULT_STRUCT;
|
||||||
|
}
|
||||||
|
|
||||||
|
return CLAY__INIT(Clay_ElementData){
|
||||||
|
.boundingBox = item->boundingBox,
|
||||||
|
.found = true
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
CLAY_WASM_EXPORT("Clay_SetDebugModeEnabled")
|
CLAY_WASM_EXPORT("Clay_SetDebugModeEnabled")
|
||||||
void Clay_SetDebugModeEnabled(bool enabled) {
|
void Clay_SetDebugModeEnabled(bool enabled) {
|
||||||
Clay_Context* context = Clay_GetCurrentContext();
|
Clay_Context* context = Clay_GetCurrentContext();
|
||||||
|
Loading…
Reference in New Issue
Block a user