mirror of
https://github.com/nicbarker/clay.git
synced 2025-05-07 02:48:04 +00:00
Compare commits
11 Commits
7be346692d
...
56c83a4718
Author | SHA1 | Date | |
---|---|---|---|
|
56c83a4718 | ||
|
cd82ce6fcf | ||
|
814c9392c6 | ||
|
356724ed20 | ||
|
337fc1dc92 | ||
|
9af4b6384d | ||
|
faf2f926b8 | ||
|
5fea1e748d | ||
|
13402c62ab | ||
|
9b8fd94170 | ||
|
b2e7eb2ee6 |
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,5 +1,5 @@
|
||||
cmake-build-debug/
|
||||
cmake-build-release/
|
||||
cmake-build-*/
|
||||
build/
|
||||
.DS_Store
|
||||
.idea/
|
||||
node_modules/
|
||||
|
@ -9,6 +9,9 @@ add_subdirectory("examples/raylib-sidebar-scrolling-container")
|
||||
# add_subdirectory("examples/cairo-pdf-rendering") Some issue with github actions populating cairo, disable for now
|
||||
if(NOT MSVC)
|
||||
add_subdirectory("examples/clay-official-website")
|
||||
add_subdirectory("examples/introducing-clay-video-demo")
|
||||
add_subdirectory("examples/SDL2-video-demo")
|
||||
add_subdirectory("examples/minimal-imgui")
|
||||
endif()
|
||||
add_subdirectory("examples/introducing-clay-video-demo")
|
||||
add_subdirectory("examples/SDL2-video-demo")
|
||||
|
43
clay.h
43
clay.h
@ -443,6 +443,13 @@ CLAY__TYPEDEF(Clay_ScrollContainerData, struct {
|
||||
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_RENDER_COMMAND_TYPE_NONE,
|
||||
CLAY_RENDER_COMMAND_TYPE_RECTANGLE,
|
||||
@ -515,6 +522,7 @@ void Clay_BeginLayout(void);
|
||||
Clay_RenderCommandArray Clay_EndLayout(void);
|
||||
Clay_ElementId Clay_GetElementId(Clay_String idString);
|
||||
Clay_ElementId Clay_GetElementIdWithIndex(Clay_String idString, uint32_t index);
|
||||
Clay_ElementData Clay_GetElementData (Clay_ElementId id);
|
||||
bool Clay_Hovered(void);
|
||||
void Clay_OnHover(void (*onHoverFunction)(Clay_ElementId elementId, Clay_PointerData pointerData, intptr_t userData), intptr_t userData);
|
||||
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) {
|
||||
Clay_Context* context = Clay_GetCurrentContext();
|
||||
Clay__int32_tArray largestContainers = context->openClipElementStack;
|
||||
largestContainers.length = 0;
|
||||
|
||||
while (totalSizeToDistribute > 0.1) {
|
||||
largestContainers.length = 0;
|
||||
float largestSize = 0;
|
||||
float targetSize = 0;
|
||||
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;
|
||||
|
||||
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 childMinSize = xAxis ? childElement->minDimensions.width : childElement->minDimensions.height;
|
||||
float oldChildSize = *childSize;
|
||||
*childSize = CLAY__MAX(childMinSize, targetSize);
|
||||
totalSizeToDistribute -= (oldChildSize - *childSize);
|
||||
if (*childSize == childMinSize) {
|
||||
Clay__int32_tArray_RemoveSwapback(&largestContainers, childOffset);
|
||||
childOffset--;
|
||||
for (int32_t i = 0; i < resizableContainerBuffer.length; i++) {
|
||||
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;
|
||||
}
|
||||
|
||||
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")
|
||||
void Clay_SetDebugModeEnabled(bool enabled) {
|
||||
Clay_Context* context = Clay_GetCurrentContext();
|
||||
|
50
examples/minimal-imgui/CMakeLists.txt
Normal file
50
examples/minimal-imgui/CMakeLists.txt
Normal file
@ -0,0 +1,50 @@
|
||||
cmake_minimum_required(VERSION 3.27)
|
||||
project(minimal_imgui)
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
|
||||
|
||||
#add_subdirectory(3rd_party)
|
||||
|
||||
include(FetchContent)
|
||||
|
||||
FetchContent_Declare(
|
||||
sokol
|
||||
GIT_REPOSITORY "https://github.com/floooh/sokol.git"
|
||||
GIT_COMMIT "789d97071d17cbab4e3835a0b0b8b379e98c114f"
|
||||
GIT_PROGRESS TRUE
|
||||
GIT_SHALLOW TRUE
|
||||
)
|
||||
|
||||
FetchContent_Declare(
|
||||
imgui
|
||||
GIT_REPOSITORY "https://github.com/ocornut/imgui.git"
|
||||
GIT_TAG "v1.91.6"
|
||||
GIT_PROGRESS TRUE
|
||||
GIT_SHALLOW TRUE
|
||||
)
|
||||
|
||||
FetchContent_MakeAvailable(sokol imgui)
|
||||
|
||||
add_executable(minimal_imgui
|
||||
main.cpp
|
||||
${imgui_SOURCE_DIR}/imgui.cpp
|
||||
${imgui_SOURCE_DIR}/imgui_demo.cpp
|
||||
${imgui_SOURCE_DIR}/imgui_draw.cpp
|
||||
${imgui_SOURCE_DIR}/imgui_tables.cpp
|
||||
${imgui_SOURCE_DIR}/imgui_widgets.cpp
|
||||
)
|
||||
|
||||
target_compile_options(minimal_imgui PUBLIC)
|
||||
target_include_directories(minimal_imgui PUBLIC .)
|
||||
|
||||
#set(CMAKE_CXX_FLAGS_DEBUG "-DCLAY_DEBUG")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
|
||||
target_include_directories(minimal_imgui PUBLIC ${sokol_SOURCE_DIR} ${imgui_SOURCE_DIR})
|
||||
if(WIN32)
|
||||
target_link_libraries(minimal_imgui PUBLIC kernel32 user32 shell32 gdi32)
|
||||
elseif(APPLE)
|
||||
target_link_libraries(minimal_imgui PUBLIC "-framework Cocoa" "-framework QuartzCore" "-framework OpenGL")
|
||||
elseif(UNIX AND NOT APPLE)
|
||||
target_link_libraries(minimal_imgui PUBLIC X11 Xi Xcursor GL dl pthread m)
|
||||
endif()
|
196
examples/minimal-imgui/main.cpp
Normal file
196
examples/minimal-imgui/main.cpp
Normal file
@ -0,0 +1,196 @@
|
||||
#define CLAY_IMPLEMENTATION
|
||||
#include "../../clay.h"
|
||||
|
||||
#define SOKOL_IMPL
|
||||
#define SOKOL_NO_ENTRY
|
||||
#define SOKOL_GLCORE
|
||||
#include "sokol_app.h"
|
||||
#include "sokol_gfx.h"
|
||||
#include "sokol_glue.h"
|
||||
#include "sokol_log.h"
|
||||
|
||||
#define IMGUI_DEFINE_MATH_OPERATORS
|
||||
#include "imgui.h"
|
||||
#define SOKOL_IMGUI_IMPL
|
||||
#include "util/sokol_imgui.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define SCREEN_WIDTH 800
|
||||
#define SCREEN_HEIGHT 600
|
||||
|
||||
sg_pass_action pass_action = {};
|
||||
|
||||
static const uint32_t FONT_ID_BODY_24 = 0;
|
||||
static const Clay_Color COLOR_ORANGE = (Clay_Color) {225, 138, 50, 255};
|
||||
static const Clay_Color COLOR_BLUE = (Clay_Color) {111, 173, 162, 255};
|
||||
static const Clay_Color COLOR_LIGHT = (Clay_Color) {224, 215, 210, 255};
|
||||
|
||||
void init();
|
||||
void frame();
|
||||
void cleanup();
|
||||
void input(const sapp_event* event);
|
||||
|
||||
static void Label(Clay_String text) {
|
||||
CLAY(CLAY_LAYOUT({ .padding = {16, 8} }),
|
||||
CLAY_RECTANGLE({ .color = Clay_Hovered() ? COLOR_BLUE : COLOR_ORANGE })
|
||||
) {
|
||||
CLAY_TEXT(text, CLAY_TEXT_CONFIG({
|
||||
.textColor = { 255, 255, 255, 255 },
|
||||
.fontId = FONT_ID_BODY_24,
|
||||
.fontSize = 24,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
static Clay_RenderCommandArray CreateLayout() {
|
||||
Clay_BeginLayout();
|
||||
CLAY(CLAY_ID("MainContent"),
|
||||
CLAY_LAYOUT({
|
||||
.sizing = {
|
||||
.width = CLAY_SIZING_GROW(),
|
||||
.height = CLAY_SIZING_GROW(),
|
||||
},
|
||||
.childAlignment = {
|
||||
.x = CLAY_ALIGN_X_CENTER,
|
||||
.y = CLAY_ALIGN_Y_CENTER,
|
||||
}
|
||||
}),
|
||||
CLAY_RECTANGLE({
|
||||
.color = COLOR_LIGHT,
|
||||
})
|
||||
) {
|
||||
Label(CLAY_STRING("Hello, World!"));
|
||||
}
|
||||
return Clay_EndLayout();
|
||||
}
|
||||
|
||||
Clay_Dimensions measureText(Clay_String *text, Clay_TextElementConfig *config);
|
||||
void render(Clay_RenderCommandArray renderCommands);
|
||||
|
||||
int main(int argc, char** args) {
|
||||
//-----------------------------------------------------------------------
|
||||
// Setup Clay
|
||||
//-----------------------------------------------------------------------
|
||||
uint64_t totalMemorySize = Clay_MinMemorySize();
|
||||
Clay_Arena clayMemory = (Clay_Arena) {
|
||||
.label = CLAY_STRING("Clay Memory Arena"),
|
||||
.capacity = totalMemorySize,
|
||||
.memory = (char*)malloc(totalMemorySize),
|
||||
};
|
||||
|
||||
Clay_SetMeasureTextFunction(measureText);
|
||||
|
||||
Clay_Initialize(clayMemory, (Clay_Dimensions) { SCREEN_WIDTH, SCREEN_HEIGHT });
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
// Setup Sokol
|
||||
//-----------------------------------------------------------------------
|
||||
sapp_desc desc = {0};
|
||||
desc.init_cb = init;
|
||||
desc.frame_cb = frame;
|
||||
desc.cleanup_cb = cleanup,
|
||||
desc.event_cb = input,
|
||||
desc.width = SCREEN_WIDTH,
|
||||
desc.height = SCREEN_HEIGHT,
|
||||
desc.window_title = "sokol + puredoom",
|
||||
desc.icon.sokol_default = true,
|
||||
desc.logger.func = slog_func;
|
||||
sapp_run(&desc);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void init() {
|
||||
sg_desc desc = {0};
|
||||
desc.environment = sglue_environment();
|
||||
desc.logger.func = slog_func;
|
||||
sg_setup(&desc);
|
||||
|
||||
pass_action.colors[0] = (sg_color_attachment_action){ .load_action=SG_LOADACTION_CLEAR, .clear_value={0.2f, 0.1f, 0.3f, 1.0f} };
|
||||
|
||||
simgui_desc_t simgui_desc = {0};
|
||||
simgui_setup(&simgui_desc);
|
||||
}
|
||||
|
||||
void frame() {
|
||||
// const double dt = sapp_frame_duration();
|
||||
|
||||
const int width = sapp_width();
|
||||
const int height = sapp_height();
|
||||
simgui_new_frame({ width, height, sapp_frame_duration(), sapp_dpi_scale() });
|
||||
|
||||
// imgui
|
||||
{
|
||||
// ImGui::ShowDemoWindow();
|
||||
ImGui::SetNextWindowSize(ImVec2{SCREEN_WIDTH, SCREEN_HEIGHT});
|
||||
ImGui::SetNextWindowPos(ImVec2{0, 0});
|
||||
ImGui::Begin("Text rendering", NULL, ImGuiWindowFlags_NoBackground|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoScrollbar|ImGuiWindowFlags_NoSavedSettings);
|
||||
|
||||
Clay_RenderCommandArray renderCommands = CreateLayout();
|
||||
render(renderCommands);
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
sg_begin_pass({ .action = pass_action, .swapchain = sglue_swapchain() });
|
||||
simgui_render();
|
||||
sg_end_pass();
|
||||
sg_commit();
|
||||
}
|
||||
|
||||
void cleanup() {
|
||||
simgui_shutdown();
|
||||
sg_shutdown();
|
||||
}
|
||||
|
||||
void input(const sapp_event* event) {
|
||||
simgui_handle_event(event);
|
||||
}
|
||||
|
||||
Clay_Dimensions measureText(Clay_String *text, Clay_TextElementConfig *config)
|
||||
{
|
||||
ImVec2 size = ImGui::CalcTextSize(text->chars, nullptr);
|
||||
return (Clay_Dimensions) {
|
||||
.width = size.x,
|
||||
.height = size.y,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
void render(Clay_RenderCommandArray renderCommands)
|
||||
{
|
||||
ImDrawList* draw_list = ImGui::GetWindowDrawList();
|
||||
ImVec2 p = ImGui::GetCursorScreenPos();
|
||||
|
||||
for (uint32_t i = 0; i < renderCommands.length; i++)
|
||||
{
|
||||
Clay_RenderCommand *renderCommand = Clay_RenderCommandArray_Get(&renderCommands, i);
|
||||
Clay_BoundingBox boundingBox = renderCommand->boundingBox;
|
||||
switch (renderCommand->commandType)
|
||||
{
|
||||
case CLAY_RENDER_COMMAND_TYPE_RECTANGLE: {
|
||||
Clay_RectangleElementConfig *config = renderCommand->config.rectangleElementConfig;
|
||||
Clay_Color color = config->color;
|
||||
|
||||
draw_list->AddRectFilled(p+ImVec2(boundingBox.x, boundingBox.y), p+ImVec2(boundingBox.x+boundingBox.width, boundingBox.y+boundingBox.height), ImColor(color.r/255.0f, color.g/255.0f, color.b/255.0f, color.a/255.0f));
|
||||
|
||||
break;
|
||||
}
|
||||
case CLAY_RENDER_COMMAND_TYPE_TEXT: {
|
||||
Clay_TextElementConfig *config = renderCommand->config.textElementConfig;
|
||||
|
||||
draw_list->AddText(p+ImVec2(boundingBox.x, boundingBox.y), ImColor(config->textColor.r/255.0f, config->textColor.g/255.0f, config->textColor.b/255.0f, config->textColor.a/255.0f), renderCommand->text.chars);
|
||||
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
fprintf(stderr, "Error: unhandled render command: %d\n", renderCommand->commandType);
|
||||
#ifdef CLAY_OVERFLOW_TRAP
|
||||
raise(SIGTRAP);
|
||||
#endif
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user