diff --git a/CMakeLists.txt b/CMakeLists.txt index 10a49b89..a3308f9e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ 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/ncurses-sidebar-scrolling-container") endif() add_subdirectory("examples/introducing-clay-video-demo") add_subdirectory("examples/SDL2-video-demo") diff --git a/examples/ncurses-sidebar-scrolling-container/CMakeLists.txt b/examples/ncurses-sidebar-scrolling-container/CMakeLists.txt new file mode 100644 index 00000000..009e69c1 --- /dev/null +++ b/examples/ncurses-sidebar-scrolling-container/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.27) +project(clay_examples_textui_sidebar_scrolling_container C) +set(CMAKE_C_STANDARD 99) + +add_executable(clay_examples_textui_sidebar_scrolling_container main.c multi-compilation-unit.c) + +target_compile_options(clay_examples_textui_sidebar_scrolling_container PUBLIC) +target_include_directories(clay_examples_textui_sidebar_scrolling_container PUBLIC .) + +target_link_libraries(clay_examples_textui_sidebar_scrolling_container PUBLIC ncurses) +set(CMAKE_CXX_FLAGS_DEBUG "-Wall -Werror -DCLAY_DEBUG") +set(CMAKE_CXX_FLAGS_RELEASE "-O3") + +add_custom_command( + TARGET clay_examples_textui_sidebar_scrolling_container POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory + ${CMAKE_CURRENT_SOURCE_DIR}/resources + ${CMAKE_CURRENT_BINARY_DIR}/resources) diff --git a/examples/ncurses-sidebar-scrolling-container/Readme.md b/examples/ncurses-sidebar-scrolling-container/Readme.md new file mode 100644 index 00000000..5271e5d2 --- /dev/null +++ b/examples/ncurses-sidebar-scrolling-container/Readme.md @@ -0,0 +1,39 @@ +# textui/console renderer example + +## Introduction +This renderer example utilizes ncurses and a mostly implemented library which converts the clay draw commands into ncurses commands. +A console rendering is very limited, and all coordinates are simply rounded via integer division to the coordinate system of a terminal. + +## What works +- Rectangles draw approximately correctly with the right color +- Text wraps, and is well placed +- Clicking a location on a scroll bar moves the scrolled area +- Debug mode highlights on click +- Window resize and layout reaction + +## What doesn't work +- Image rendering +- Sub character rectangles +- Text fonts and colors +- Clay extensions +- Clay scissor mode + +## Reasonable expectations +This renderer is intended to allow for a nearly dependency free implementation of Clay, and nearly all linux distributions have ncurses installed. +This renderer will allow for quick visualization of layouts. There is no graphics acceleration though this example is quite responsive. +The unit of a terminal is a character, not a pixel, so there is only so much detail that can be rendered. The default character size defined in the renderer is 5x8 pixels. + +## What could work +- More precise rendering of borders and rectangles, this could be implemented with box-drawing characters: https://en.wikipedia.org/wiki/Box-drawing_characters +- The mouse wheel on some terminals +- Images with an additional dependency on something like kitty: https://sw.kovidgoyal.net/kitty/graphics-protocol/ +- Better text coloring by matching the background + +## Setup +You'll have to setup an event loop using getch(), and pay attention to the various init functions in main.c +Then it's the regular Clay layout definition and calls to the renderer function. + +## Conclusion +The truth is that if you're willing to learn ncurses and how it handles windows you can get a better terminal experience without Clay. +This example shows that for quick terminal apps, Clay could be extremely effective for organizing a user interface in the terminal. +I hope that this renderer helps many people build fun and cool projects by lowering the barrier to a framwork. diff --git a/examples/ncurses-sidebar-scrolling-container/main.c b/examples/ncurses-sidebar-scrolling-container/main.c new file mode 100644 index 00000000..3321762d --- /dev/null +++ b/examples/ncurses-sidebar-scrolling-container/main.c @@ -0,0 +1,287 @@ +#include +#define CLAY_IMPLEMENTATION +#include "../../renderers/ncurses/clay_renderer_ncurses.c" +#include + +const uint32_t FONT_ID_BODY_24 = 0; +const uint32_t FONT_ID_BODY_16 = 1; +#define COLOR_ORANGE (Clay_Color) {225, 138, 50, 255} +#define COLOR_BLUE (Clay_Color) {111, 173, 162, 255} + +//Texture2D profilePicture; +//#define RAYLIB_VECTOR2_TO_CLAY_VECTOR2(vector) (Clay_Vector2) { .x = vector.x, .y = vector.y } + +Clay_String profileText = CLAY_STRING("Profile Page one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen"); +Clay_TextElementConfig headerTextConfig = (Clay_TextElementConfig) { .fontId = 1, .fontSize = 16, .textColor = {0,0,0,255} }; + +void HandleHeaderButtonInteraction(Clay_ElementId elementId, Clay_PointerData pointerData, intptr_t userData) { + if (pointerData.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) { + // Do some click handling + } +} + +// Examples of re-usable "Components" +void RenderHeaderButton(Clay_String text) { + CLAY(CLAY_LAYOUT({ .padding = {16, 8} }), + CLAY_RECTANGLE({ .color = Clay_Hovered() ? COLOR_BLUE : COLOR_ORANGE }), + Clay_OnHover(HandleHeaderButtonInteraction, 1)) { + CLAY_TEXT(text, CLAY_TEXT_CONFIG(headerTextConfig)); + } +} + +Clay_LayoutConfig dropdownTextItemLayout = (Clay_LayoutConfig) { .padding = {8, 4} }; +Clay_RectangleElementConfig dropdownRectangleConfig = (Clay_RectangleElementConfig) { .color = {180, 180, 180, 255} }; +Clay_TextElementConfig dropdownTextElementConfig = (Clay_TextElementConfig) { .fontSize = 24, .textColor = {255,255,255,255} }; + +void RenderDropdownTextItem(int index) { + CLAY(CLAY_IDI("ScrollContainerItem", index), CLAY_LAYOUT(dropdownTextItemLayout), CLAY_RECTANGLE(dropdownRectangleConfig)) { + CLAY_TEXT(CLAY_STRING("I'm a text field in a scroll container."), &dropdownTextElementConfig); + } +} + +Clay_RenderCommandArray CreateLayout() { + Clay_BeginLayout(); + CLAY(CLAY_ID("OuterContainer"), CLAY_LAYOUT({ .sizing = { .width = CLAY_SIZING_GROW(), .height = CLAY_SIZING_GROW() }, .padding = { 16, 16 }, .childGap = 16 }), CLAY_RECTANGLE({ .color = {200, 200, 200, 255} })) { + CLAY(CLAY_ID("SideBar"), CLAY_LAYOUT({ .layoutDirection = CLAY_TOP_TO_BOTTOM, .sizing = { .width = CLAY_SIZING_FIXED(300), .height = CLAY_SIZING_GROW() }, .padding = {16, 16}, .childGap = 16 }), CLAY_RECTANGLE({ .color = {150, 150, 255, 255} })) { + CLAY(CLAY_ID("ProfilePictureOuter"), CLAY_LAYOUT({ .sizing = { .width = CLAY_SIZING_GROW() }, .padding = { 8, 8 }, .childGap = 8, .childAlignment = { .y = CLAY_ALIGN_Y_CENTER } }), CLAY_RECTANGLE({ .color = {130, 130, 255, 255} })) { + CLAY(CLAY_ID("ProfilePicture"), CLAY_LAYOUT({ .sizing = { .width = CLAY_SIZING_FIXED(60), .height = CLAY_SIZING_FIXED(60) } })) {} + //TODO CLAY_TEXT(profileText, CLAY_TEXT_CONFIG({ .fontSize = 24, .textColor = {0, 0, 0, 255} })); + } + CLAY(CLAY_ID("SidebarBlob1"), CLAY_LAYOUT({ .sizing = { .width = CLAY_SIZING_GROW(), .height = CLAY_SIZING_FIXED(50) }}), CLAY_RECTANGLE({ .color = {110, 110, 255, 255} })) {} + CLAY(CLAY_ID("SidebarBlob2"), CLAY_LAYOUT({ .sizing = { .width = CLAY_SIZING_GROW(), .height = CLAY_SIZING_FIXED(50) }}), CLAY_RECTANGLE({ .color = {110, 110, 255, 255} })) {} + CLAY(CLAY_ID("SidebarBlob3"), CLAY_LAYOUT({ .sizing = { .width = CLAY_SIZING_GROW(), .height = CLAY_SIZING_FIXED(50) }}), CLAY_RECTANGLE({ .color = {110, 110, 255, 255} })) {} + CLAY(CLAY_ID("SidebarBlob4"), CLAY_LAYOUT({ .sizing = { .width = CLAY_SIZING_GROW(), .height = CLAY_SIZING_FIXED(50) }}), CLAY_RECTANGLE({ .color = {110, 110, 255, 255} })) {} + } + + CLAY(CLAY_ID("RightPanel"), CLAY_LAYOUT({ .layoutDirection = CLAY_TOP_TO_BOTTOM, .sizing = { .width = CLAY_SIZING_GROW(), .height = CLAY_SIZING_GROW() }, .childGap = 16 })) { + CLAY(CLAY_ID("HeaderBar"), CLAY_LAYOUT({ .sizing = { .width = CLAY_SIZING_GROW() }, .childAlignment = { .x = CLAY_ALIGN_X_RIGHT }, .padding = {8, 8}, .childGap = 8 }), CLAY_RECTANGLE({ .color = {180, 180, 180, 255} })) { + RenderHeaderButton(CLAY_STRING("Header Item 1")); + RenderHeaderButton(CLAY_STRING("Header Item 2")); + RenderHeaderButton(CLAY_STRING("Header Item 3")); + } + CLAY(CLAY_ID("MainContent"), + CLAY_SCROLL({ .vertical = true }), + CLAY_LAYOUT({ .layoutDirection = CLAY_TOP_TO_BOTTOM, .padding = {16, 16}, .childGap = 16, .sizing = { CLAY_SIZING_GROW() } }), + CLAY_RECTANGLE({ .color = {200, 200, 255, 255} })) + { + CLAY(CLAY_ID("FloatingContainer"), + CLAY_LAYOUT({ .sizing = { .width = CLAY_SIZING_FIXED(300), .height = CLAY_SIZING_FIXED(300) }, .padding = { 16, 16 }}), + CLAY_FLOATING({ .zIndex = 1, .attachment = { CLAY_ATTACH_POINT_CENTER_TOP, CLAY_ATTACH_POINT_CENTER_TOP }, .offset = {0, -16} }), + CLAY_BORDER_OUTSIDE({ .color = {80, 80, 80, 255}, .width = 2 }), + CLAY_RECTANGLE({ .color = {140,80, 200, 200 }}) + ) { + CLAY_TEXT(CLAY_STRING("I'm an inline floating container."), CLAY_TEXT_CONFIG({ .fontSize = 24, .textColor = {255,255,255,255} })); + } + + CLAY_TEXT(CLAY_STRING("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt."), + CLAY_TEXT_CONFIG({ .fontId = FONT_ID_BODY_24, .fontSize = 24, .textColor = {0,0,0,255} })); + + CLAY(CLAY_ID("Photos2"), CLAY_LAYOUT({ .childGap = 16, .padding = { 16, 16 }}), CLAY_RECTANGLE({ .color = {180, 180, 220, 255} })) { + CLAY(CLAY_ID("Picture4"), CLAY_LAYOUT({ .sizing = { .width = CLAY_SIZING_FIXED(120), .height = CLAY_SIZING_FIXED(120) }})) {} + CLAY(CLAY_ID("Picture5"), CLAY_LAYOUT({ .sizing = { .width = CLAY_SIZING_FIXED(120), .height = CLAY_SIZING_FIXED(120) }})) {} + CLAY(CLAY_ID("Picture6"), CLAY_LAYOUT({ .sizing = { .width = CLAY_SIZING_FIXED(120), .height = CLAY_SIZING_FIXED(120) }})) {} + } + + CLAY_TEXT(CLAY_STRING("Faucibus purus in massa tempor nec. Nec ullamcorper sit amet risus nullam eget felis eget nunc. Diam vulputate ut pharetra sit amet aliquam id diam. Lacus suspendisse faucibus interdum posuere lorem. A diam sollicitudin tempor id. Amet massa vitae tortor condimentum lacinia. Aliquet nibh praesent tristique magna."), + CLAY_TEXT_CONFIG({ .fontSize = 24, .lineHeight = 60, .textColor = {0,0,0,255} })); + + CLAY_TEXT(CLAY_STRING("Suspendisse in est ante in nibh. Amet venenatis urna cursus eget nunc scelerisque viverra. Elementum sagittis vitae et leo duis ut diam quam nulla. Enim nulla aliquet porttitor lacus. Pellentesque habitant morbi tristique senectus et. Facilisi nullam vehicula ipsum a arcu cursus vitae.\nSem fringilla ut morbi tincidunt. Euismod quis viverra nibh cras pulvinar mattis nunc sed. Velit sed ullamcorper morbi tincidunt ornare massa. Varius quam quisque id diam vel quam. Nulla pellentesque dignissim enim sit amet venenatis. Enim lobortis scelerisque fermentum dui faucibus in. Pretium viverra suspendisse potenti nullam ac tortor vitae. Lectus vestibulum mattis ullamcorper velit sed. Eget mauris pharetra et ultrices neque ornare aenean euismod elementum. Habitant morbi tristique senectus et. Integer vitae justo eget magna fermentum iaculis eu. Semper quis lectus nulla at volutpat diam. Enim praesent elementum facilisis leo. Massa vitae tortor condimentum lacinia quis vel."), + CLAY_TEXT_CONFIG({ .fontSize = 24, .textColor = {0,0,0,255} })); + + CLAY(CLAY_ID("Photos"), CLAY_LAYOUT({ .sizing = { .width = CLAY_SIZING_GROW() }, .childAlignment = { .x = CLAY_ALIGN_X_CENTER, .y = CLAY_ALIGN_Y_CENTER }, .childGap = 16, .padding = {16, 16} }), CLAY_RECTANGLE({ .color = {180, 180, 220, 255} })) { + CLAY(CLAY_ID("Picture2"), CLAY_LAYOUT({ .sizing = { .width = CLAY_SIZING_FIXED(120), .height = CLAY_SIZING_FIXED(120) }})) {} + CLAY(CLAY_ID("Picture1"), CLAY_LAYOUT({ .childAlignment = { .x = CLAY_ALIGN_X_CENTER }, .layoutDirection = CLAY_TOP_TO_BOTTOM, .padding = {8, 8} }), CLAY_RECTANGLE({ .color = {170, 170, 220, 255} })) { + CLAY(CLAY_ID("ProfilePicture2"), CLAY_LAYOUT({ .sizing = { .width = CLAY_SIZING_FIXED(60), .height = CLAY_SIZING_FIXED(60) }})) {} + CLAY_TEXT(CLAY_STRING("Image caption below"), CLAY_TEXT_CONFIG({ .fontSize = 24, .textColor = {0,0,0,255} })); + } + CLAY(CLAY_ID("Picture3"), CLAY_LAYOUT({ .sizing = { .width = CLAY_SIZING_FIXED(120), .height = CLAY_SIZING_FIXED(120) }})) {} + } + + CLAY_TEXT(CLAY_STRING("Amet cursus sit amet dictum sit amet justo donec. Et malesuada fames ac turpis egestas maecenas. A lacus vestibulum sed arcu non odio euismod lacinia. Gravida neque convallis a cras. Dui nunc mattis enim ut tellus elementum sagittis vitae et. Orci sagittis eu volutpat odio facilisis mauris. Neque gravida in fermentum et sollicitudin ac orci. Ultrices dui sapien eget mi proin sed libero. Euismod quis viverra nibh cras pulvinar mattis. Diam volutpat commodo sed egestas egestas. In fermentum posuere urna nec tincidunt praesent semper. Integer eget aliquet nibh praesent tristique magna.\nId cursus metus aliquam eleifend mi in. Sed pulvinar proin gravida hendrerit lectus a. Etiam tempor orci eu lobortis elementum nibh tellus. Nullam vehicula ipsum a arcu cursus vitae. Elit scelerisque mauris pellentesque pulvinar pellentesque habitant morbi tristique senectus. Condimentum lacinia quis vel eros donec ac odio. Mattis pellentesque id nibh tortor id aliquet lectus. Turpis egestas integer eget aliquet nibh praesent tristique. Porttitor massa id neque aliquam vestibulum morbi. Mauris commodo quis imperdiet massa tincidunt nunc pulvinar sapien et. Nunc scelerisque viverra mauris in aliquam sem fringilla. Suspendisse ultrices gravida dictum fusce ut placerat orci nulla.\nLacus laoreet non curabitur gravida arcu ac tortor dignissim. Urna nec tincidunt praesent semper feugiat nibh sed pulvinar. Tristique senectus et netus et malesuada fames ac. Nunc aliquet bibendum enim facilisis gravida. Egestas maecenas pharetra convallis posuere morbi leo urna molestie. Sapien nec sagittis aliquam malesuada bibendum arcu vitae elementum curabitur. Ac turpis egestas maecenas pharetra convallis posuere morbi leo urna. Viverra vitae congue eu consequat. Aliquet enim tortor at auctor urna. Ornare massa eget egestas purus viverra accumsan in nisl nisi. Elit pellentesque habitant morbi tristique senectus et netus et malesuada.\nSuspendisse ultrices gravida dictum fusce ut placerat orci nulla pellentesque. Lobortis feugiat vivamus at augue eget arcu. Vitae justo eget magna fermentum iaculis eu. Gravida rutrum quisque non tellus orci. Ipsum faucibus vitae aliquet nec. Nullam non nisi est sit amet. Nunc consequat interdum varius sit amet mattis vulputate enim. Sem fringilla ut morbi tincidunt augue interdum. Vitae purus faucibus ornare suspendisse. Massa tincidunt nunc pulvinar sapien et. Fringilla ut morbi tincidunt augue interdum velit euismod in. Donec massa sapien faucibus et. Est placerat in egestas erat imperdiet. Gravida rutrum quisque non tellus. Morbi non arcu risus quis varius quam quisque id diam. Habitant morbi tristique senectus et netus et malesuada fames ac. Eget lorem dolor sed viverra.\nOrnare massa eget egestas purus viverra. Varius vel pharetra vel turpis nunc eget lorem. Consectetur purus ut faucibus pulvinar elementum. Placerat in egestas erat imperdiet sed euismod nisi. Interdum velit euismod in pellentesque massa placerat duis ultricies lacus. Aliquam nulla facilisi cras fermentum odio eu. Est pellentesque elit ullamcorper dignissim cras tincidunt. Nunc sed id semper risus in hendrerit gravida rutrum. A pellentesque sit amet porttitor eget dolor morbi. Pellentesque habitant morbi tristique senectus et netus et malesuada fames. Nisl nunc mi ipsum faucibus vitae aliquet nec ullamcorper. Sed id semper risus in hendrerit gravida. Tincidunt praesent semper feugiat nibh. Aliquet lectus proin nibh nisl condimentum id venenatis a. Enim sit amet venenatis urna cursus eget. In egestas erat imperdiet sed euismod nisi porta lorem mollis. Lacinia quis vel eros donec ac odio tempor orci. Donec pretium vulputate sapien nec sagittis aliquam malesuada bibendum arcu. Erat pellentesque adipiscing commodo elit at.\nEgestas sed sed risus pretium quam vulputate. Vitae congue mauris rhoncus aenean vel elit scelerisque mauris pellentesque. Aliquam malesuada bibendum arcu vitae elementum. Congue mauris rhoncus aenean vel elit scelerisque mauris. Pellentesque dignissim enim sit amet venenatis urna cursus. Et malesuada fames ac turpis egestas sed tempus urna. Vel fringilla est ullamcorper eget nulla facilisi etiam dignissim. Nibh cras pulvinar mattis nunc sed blandit libero. Fringilla est ullamcorper eget nulla facilisi etiam dignissim. Aenean euismod elementum nisi quis eleifend quam adipiscing vitae proin. Mauris pharetra et ultrices neque ornare aenean euismod elementum. Ornare quam viverra orci sagittis eu. Odio ut sem nulla pharetra diam sit amet nisl suscipit. Ornare lectus sit amet est. Ullamcorper sit amet risus nullam eget. Tincidunt lobortis feugiat vivamus at augue eget arcu dictum.\nUrna nec tincidunt praesent semper feugiat nibh. Ut venenatis tellus in metus vulputate eu scelerisque felis. Cursus risus at ultrices mi tempus. In pellentesque massa placerat duis ultricies lacus sed turpis. Platea dictumst quisque sagittis purus. Cras adipiscing enim eu turpis egestas. Egestas sed tempus urna et pharetra pharetra. Netus et malesuada fames ac turpis egestas integer eget aliquet. Ac turpis egestas sed tempus. Sed lectus vestibulum mattis ullamcorper velit sed. Ante metus dictum at tempor commodo ullamcorper a. Augue neque gravida in fermentum et sollicitudin ac. Praesent semper feugiat nibh sed pulvinar proin gravida. Metus aliquam eleifend mi in nulla posuere sollicitudin aliquam ultrices. Neque gravida in fermentum et sollicitudin ac orci phasellus egestas.\nRidiculus mus mauris vitae ultricies. Morbi quis commodo odio aenean. Duis ultricies lacus sed turpis. Non pulvinar neque laoreet suspendisse interdum consectetur. Scelerisque eleifend donec pretium vulputate sapien nec sagittis aliquam. Volutpat est velit egestas dui id ornare arcu odio ut. Viverra tellus in hac habitasse platea dictumst vestibulum rhoncus est. Vestibulum lectus mauris ultrices eros. Sed blandit libero volutpat sed cras ornare. Id leo in vitae turpis massa sed elementum tempus. Gravida dictum fusce ut placerat orci nulla pellentesque. Pretium quam vulputate dignissim suspendisse in. Nisl suscipit adipiscing bibendum est ultricies integer quis auctor. Risus viverra adipiscing at in tellus. Turpis nunc eget lorem dolor sed viverra ipsum. Senectus et netus et malesuada fames ac. Habitasse platea dictumst vestibulum rhoncus est. Nunc sed id semper risus in hendrerit gravida. Felis eget velit aliquet sagittis id. Eget felis eget nunc lobortis.\nMaecenas pharetra convallis posuere morbi leo. Maecenas volutpat blandit aliquam etiam. A condimentum vitae sapien pellentesque habitant morbi tristique senectus et. Pulvinar mattis nunc sed blandit libero volutpat sed. Feugiat in ante metus dictum at tempor commodo ullamcorper. Vel pharetra vel turpis nunc eget lorem dolor. Est placerat in egestas erat imperdiet sed euismod. Quisque non tellus orci ac auctor augue mauris augue. Placerat vestibulum lectus mauris ultrices eros in cursus turpis. Enim nunc faucibus a pellentesque sit. Adipiscing vitae proin sagittis nisl. Iaculis at erat pellentesque adipiscing commodo elit at imperdiet. Aliquam sem fringilla ut morbi.\nArcu odio ut sem nulla pharetra diam sit amet nisl. Non diam phasellus vestibulum lorem sed. At erat pellentesque adipiscing commodo elit at. Lacus luctus accumsan tortor posuere ac ut consequat. Et malesuada fames ac turpis egestas integer. Tristique magna sit amet purus. A condimentum vitae sapien pellentesque habitant. Quis varius quam quisque id diam vel quam. Est ullamcorper eget nulla facilisi etiam dignissim diam quis. Augue interdum velit euismod in pellentesque massa. Elit scelerisque mauris pellentesque pulvinar pellentesque habitant. Vulputate eu scelerisque felis imperdiet. Nibh tellus molestie nunc non blandit massa. Velit euismod in pellentesque massa placerat. Sed cras ornare arcu dui. Ut sem viverra aliquet eget sit. Eu lobortis elementum nibh tellus molestie nunc non. Blandit libero volutpat sed cras ornare arcu dui vivamus.\nSit amet aliquam id diam maecenas. Amet risus nullam eget felis eget nunc lobortis mattis aliquam. Magna sit amet purus gravida. Egestas purus viverra accumsan in nisl nisi. Leo duis ut diam quam. Ante metus dictum at tempor commodo ullamcorper. Ac turpis egestas integer eget. Fames ac turpis egestas integer eget aliquet nibh. Sem integer vitae justo eget magna fermentum. Semper auctor neque vitae tempus quam pellentesque nec nam aliquam. Vestibulum mattis ullamcorper velit sed. Consectetur adipiscing elit duis tristique sollicitudin nibh. Massa id neque aliquam vestibulum morbi blandit cursus risus.\nCursus sit amet dictum sit amet justo donec enim diam. Egestas erat imperdiet sed euismod. Nullam vehicula ipsum a arcu cursus vitae congue mauris. Habitasse platea dictumst vestibulum rhoncus est pellentesque elit. Duis ultricies lacus sed turpis tincidunt id aliquet risus feugiat. Faucibus ornare suspendisse sed nisi lacus sed viverra. Pretium fusce id velit ut tortor pretium viverra. Fermentum odio eu feugiat pretium nibh ipsum consequat nisl vel. Senectus et netus et malesuada. Tellus pellentesque eu tincidunt tortor aliquam. Aenean sed adipiscing diam donec adipiscing tristique risus nec feugiat. Quis vel eros donec ac odio. Id interdum velit laoreet id donec ultrices tincidunt.\nMassa id neque aliquam vestibulum morbi blandit cursus risus at. Enim tortor at auctor urna nunc id cursus metus. Lorem ipsum dolor sit amet consectetur. At quis risus sed vulputate odio. Facilisis mauris sit amet massa vitae tortor condimentum lacinia quis. Et malesuada fames ac turpis egestas maecenas. Bibendum arcu vitae elementum curabitur vitae nunc sed velit dignissim. Viverra orci sagittis eu volutpat odio facilisis mauris. Adipiscing bibendum est ultricies integer quis auctor elit sed. Neque viverra justo nec ultrices dui sapien. Elementum nibh tellus molestie nunc non blandit massa enim. Euismod elementum nisi quis eleifend quam adipiscing vitae proin sagittis. Faucibus ornare suspendisse sed nisi. Quis viverra nibh cras pulvinar mattis nunc sed blandit. Tristique senectus et netus et. Magnis dis parturient montes nascetur ridiculus mus.\nDolor magna eget est lorem ipsum dolor. Nibh sit amet commodo nulla. Donec pretium vulputate sapien nec sagittis aliquam malesuada. Cras adipiscing enim eu turpis egestas pretium. Cras ornare arcu dui vivamus arcu felis bibendum ut tristique. Mus mauris vitae ultricies leo integer. In nulla posuere sollicitudin aliquam ultrices sagittis orci. Quis hendrerit dolor magna eget. Nisl tincidunt eget nullam non. Vitae congue eu consequat ac felis donec et odio. Vivamus at augue eget arcu dictum varius duis at. Ornare quam viverra orci sagittis.\nErat nam at lectus urna duis convallis. Massa placerat duis ultricies lacus sed turpis tincidunt id aliquet. Est ullamcorper eget nulla facilisi etiam dignissim diam. Arcu vitae elementum curabitur vitae nunc sed velit dignissim sodales. Tortor vitae purus faucibus ornare suspendisse sed nisi lacus. Neque viverra justo nec ultrices dui sapien eget mi proin. Viverra accumsan in nisl nisi scelerisque eu ultrices. Consequat interdum varius sit amet mattis. In aliquam sem fringilla ut morbi. Eget arcu dictum varius duis at. Nulla aliquet porttitor lacus luctus accumsan tortor posuere. Arcu bibendum at varius vel pharetra vel turpis. Hac habitasse platea dictumst quisque sagittis purus sit amet. Sapien eget mi proin sed libero enim sed. Quam elementum pulvinar etiam non quam lacus suspendisse faucibus interdum. Semper viverra nam libero justo. Fusce ut placerat orci nulla pellentesque dignissim enim sit amet. Et malesuada fames ac turpis egestas maecenas pharetra convallis posuere.\nTurpis egestas sed tempus urna et pharetra pharetra massa. Gravida in fermentum et sollicitudin ac orci phasellus. Ornare suspendisse sed nisi lacus sed viverra tellus in. Fames ac turpis egestas maecenas pharetra convallis posuere. Mi proin sed libero enim sed faucibus turpis. Sit amet mauris commodo quis imperdiet massa tincidunt nunc. Ut etiam sit amet nisl purus in mollis nunc. Habitasse platea dictumst quisque sagittis purus sit amet volutpat consequat. Eget aliquet nibh praesent tristique magna. Sit amet est placerat in egestas erat. Commodo sed egestas egestas fringilla. Enim nulla aliquet porttitor lacus luctus accumsan tortor posuere ac. Et molestie ac feugiat sed lectus vestibulum mattis ullamcorper. Dignissim convallis aenean et tortor at risus viverra. Morbi blandit cursus risus at ultrices mi. Ac turpis egestas integer eget aliquet nibh praesent tristique magna.\nVolutpat sed cras ornare arcu dui. Egestas erat imperdiet sed euismod nisi porta lorem mollis aliquam. Viverra justo nec ultrices dui sapien. Amet risus nullam eget felis eget nunc lobortis. Metus aliquam eleifend mi in. Ut eu sem integer vitae. Auctor elit sed vulputate mi sit amet. Nisl nisi scelerisque eu ultrices. Dictum fusce ut placerat orci nulla. Pellentesque habitant morbi tristique senectus et. Auctor elit sed vulputate mi sit. Tincidunt arcu non sodales neque. Mi in nulla posuere sollicitudin aliquam. Morbi non arcu risus quis varius quam quisque id diam. Cras adipiscing enim eu turpis egestas pretium aenean pharetra magna. At auctor urna nunc id cursus metus aliquam. Mauris a diam maecenas sed enim ut sem viverra. Nunc scelerisque viverra mauris in. In iaculis nunc sed augue lacus viverra vitae congue eu. Volutpat blandit aliquam etiam erat velit scelerisque in dictum non."), + CLAY_TEXT_CONFIG({ .fontSize = 24, .textColor = {0,0,0,255} })); + } + } + + CLAY(CLAY_ID("Blob4Floating2"), CLAY_FLOATING({ .zIndex = 1, .parentId = Clay_GetElementId(CLAY_STRING("SidebarBlob4")).id })) { + CLAY(CLAY_ID("ScrollContainer"), CLAY_LAYOUT({ .sizing = { .height = CLAY_SIZING_FIXED(200) }, .childGap = 2 }), CLAY_SCROLL({ .vertical = true })) { + CLAY(CLAY_ID("FloatingContainer2"), CLAY_LAYOUT({ }), CLAY_FLOATING({ .zIndex = 1 })) { + CLAY(CLAY_ID("FloatingContainerInner"), CLAY_LAYOUT({ .sizing = { .width = CLAY_SIZING_FIXED(300), .height = CLAY_SIZING_FIXED(300) }, .padding = {16, 16} }), CLAY_RECTANGLE({ .color = {140,80, 200, 200} })) { + CLAY_TEXT(CLAY_STRING("I'm an inline floating container."), CLAY_TEXT_CONFIG({ .fontSize = 24, .textColor = {255,255,255,255} })); + } + } + CLAY(CLAY_ID("ScrollContainerInner"), CLAY_LAYOUT({ .layoutDirection = CLAY_TOP_TO_BOTTOM }), CLAY_RECTANGLE({ .color = {160, 160, 160, 255} })) { + for (int i = 0; i < 100; i++) { + RenderDropdownTextItem(i); + } + } + } + } + Clay_ScrollContainerData scrollData = Clay_GetScrollContainerData(Clay_GetElementId(CLAY_STRING("MainContent"))); + if (scrollData.found) { + CLAY(CLAY_ID("ScrollBar"), + CLAY_FLOATING({ + .offset = { .y = -(scrollData.scrollPosition->y / scrollData.contentDimensions.height) * scrollData.scrollContainerDimensions.height }, + .zIndex = 1, + .parentId = Clay_GetElementId(CLAY_STRING("MainContent")).id, + .attachment = {.element = CLAY_ATTACH_POINT_RIGHT_TOP, .parent = CLAY_ATTACH_POINT_RIGHT_TOP} + }) + ) { + CLAY(CLAY_ID("ScrollBarButton"), + CLAY_LAYOUT({ .sizing = {CLAY_SIZING_FIXED(12), CLAY_SIZING_FIXED((scrollData.scrollContainerDimensions.height / scrollData.contentDimensions.height) * scrollData.scrollContainerDimensions.height) }}), + CLAY_RECTANGLE({ .cornerRadius = {6}, .color = Clay_PointerOver(Clay__HashString(CLAY_STRING("ScrollBar"), 0, 0)) ? (Clay_Color){100, 100, 140, 150} : (Clay_Color){120, 120, 160, 150} }) + ) {} + } + } + } + return Clay_EndLayout(); +} + +typedef struct +{ + Clay_Vector2 clickOrigin; + Clay_Vector2 positionOrigin; + bool mouseDown; +} ScrollbarData; + +ScrollbarData scrollbarData = (ScrollbarData) {}; + +bool debugEnabled = false; + +bool UpdateDrawFrame(WINDOW* win, int ch) +{ + int x = 0; + int y = 0; + getmaxyx(win, y, x); + MEVENT event; + int mouse_x = 0; + int mouse_y = 0; + int scroll_y = 0; + bool mouse_1_pressed = false; + if (ch == KEY_MOUSE) { + if (getmouse(&event) == OK) { + if (event.bstate & BUTTON1_PRESSED) { + mvwprintw(win, 1, 0, "Left mouse button at %d, %d", event.x, event.y); + mouse_x = event.x; + mouse_y = event.y; + mouse_1_pressed = true; + wrefresh(win); + } + if (event.bstate & BUTTON1_RELEASED) { + mvwprintw(win, 1, 0, "Left mouse button at %d, %d", event.x, event.y); + mouse_x = event.x; + mouse_y = event.y; + mouse_1_pressed = false; + wrefresh(win); + } + } + } + if (ch == 'D') { + debugEnabled = !debugEnabled; + Clay_SetDebugModeEnabled(debugEnabled); + } + //---------------------------------------------------------------------------------- + // Handle scroll containers + Clay_Vector2 mousePosition = {mouse_x*HPIXELS_PER_CHAR, mouse_y*VPIXELS_PER_CHAR}; //event is not valid unless there was a mouse event + Clay_SetPointerState(mousePosition, mouse_1_pressed && !scrollbarData.mouseDown); + Clay_SetLayoutDimensions((Clay_Dimensions) { (float)x*HPIXELS_PER_CHAR, (float)y*VPIXELS_PER_CHAR }); + if (!(mouse_1_pressed)) { + scrollbarData.mouseDown = false; + } + + if (mouse_1_pressed && !scrollbarData.mouseDown && Clay_PointerOver(Clay__HashString(CLAY_STRING("ScrollBar"), 0, 0))) { + Clay_ScrollContainerData scrollContainerData = Clay_GetScrollContainerData(Clay__HashString(CLAY_STRING("MainContent"), 0, 0)); + scrollbarData.clickOrigin = mousePosition; + scrollbarData.positionOrigin = *scrollContainerData.scrollPosition; + scrollbarData.mouseDown = true; + } else if (scrollbarData.mouseDown) { + Clay_ScrollContainerData scrollContainerData = Clay_GetScrollContainerData(Clay__HashString(CLAY_STRING("MainContent"), 0, 0)); + if (scrollContainerData.contentDimensions.height > 0) { + Clay_Vector2 ratio = (Clay_Vector2) { + scrollContainerData.contentDimensions.width / scrollContainerData.scrollContainerDimensions.width, + scrollContainerData.contentDimensions.height / scrollContainerData.scrollContainerDimensions.height, + }; + if (scrollContainerData.config.vertical) { + scrollContainerData.scrollPosition->y = scrollbarData.positionOrigin.y + (scrollbarData.clickOrigin.y - mousePosition.y) * ratio.y; + } + if (scrollContainerData.config.horizontal) { + scrollContainerData.scrollPosition->x = scrollbarData.positionOrigin.x + (scrollbarData.clickOrigin.x - mousePosition.x) * ratio.x; + } + } + } + + Clay_UpdateScrollContainers(true, (Clay_Vector2) {0, scroll_y}, 20); + // Generate the auto layout for rendering + //double currentTime = GetTime(); + Clay_RenderCommandArray renderCommands = CreateLayout(); + //printf("layout time: %f microseconds\n", (GetTime() - currentTime) * 1000 * 1000); + // RENDERING --------------------------------- +// currentTime = GetTime(); + Clay_ncurses_Render(win, renderCommands); + + attr_on(color_set(0,0),0); + mvwprintw(win, 1, 0, "Left mouse button at %d, %d", event.x, event.y); + mvwprintw(win, 2, 0, "mouse mask: %d", event.bstate); + mvwprintw(win, 3, 0, "input ch: %d", ch); + mvwprintw(win, 4, 0, "scroll_y: %d", scroll_y); + refresh();//update the screen/window + //endwin(); + return true; +// printf("render time: %f ms\n", (GetTime() - currentTime) * 1000); + + //---------------------------------------------------------------------------------- +} + +bool reinitializeClay = false; + +void HandleClayErrors(Clay_ErrorData errorData) { + printf("%s", errorData.errorText.chars); + if (errorData.errorType == CLAY_ERROR_TYPE_ELEMENTS_CAPACITY_EXCEEDED) { + reinitializeClay = true; + Clay_SetMaxElementCount(Clay__maxElementCount * 2); + } else if (errorData.errorType == CLAY_ERROR_TYPE_TEXT_MEASUREMENT_CAPACITY_EXCEEDED) { + reinitializeClay = true; + Clay_SetMaxMeasureTextCacheWordCount(Clay__maxMeasureTextCacheWordCount * 2); + } +} + +int main(void) { + WINDOW * win = initscr(); + keypad(win, true); + cbreak(); + noecho(); + start_color(); + mmask_t mask, oldmask; + mask = BUTTON1_PRESSED | BUTTON_SHIFT | BUTTON2_CLICKED | REPORT_MOUSE_POSITION; + mousemask(mask, &oldmask); // Assigns new mask and saves previous one + mouseinterval(0); // Set minimum time between mouse events + + //declare x and y dimensions + int x = 0; + int y = 0; + getmaxyx(win, y, x); + uint64_t totalMemorySize = Clay_MinMemorySize(); + Clay_Arena clayMemory = Clay_CreateArenaWithCapacityAndMemory(totalMemorySize, malloc(totalMemorySize)); + Clay_SetMeasureTextFunction(ncurses_MeasureText);//clay segfaults if this isn't defined, which is a bummer. + Clay_Initialize(clayMemory, (Clay_Dimensions) { (float)x*HPIXELS_PER_CHAR, (float)y*VPIXELS_PER_CHAR }, (Clay_ErrorHandler) { HandleClayErrors }); + + //Clay_Raylib_Initialize(1024, 768, "Clay - Raylib Renderer Example", FLAG_VSYNC_HINT | FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_HIGHDPI | FLAG_MSAA_4X_HINT); + //profilePicture = LoadTextureFromImage(LoadImage("resources/profile-picture.png")); + + //-------------------------------------------------------------------------------------- + + // Main game loop + int running = true; + while (running) // Detect window close button or ESC key + { + if (reinitializeClay) { + Clay_SetMaxElementCount(8192); + totalMemorySize = Clay_MinMemorySize(); + clayMemory = Clay_CreateArenaWithCapacityAndMemory(totalMemorySize, malloc(totalMemorySize)); + Clay_Initialize(clayMemory, (Clay_Dimensions) { (float)x, (float)y }, (Clay_ErrorHandler) { HandleClayErrors }); + reinitializeClay = false; + } + int ch = getch(); + running = UpdateDrawFrame(win, ch); + } + return 0; +} diff --git a/examples/ncurses-sidebar-scrolling-container/multi-compilation-unit.c b/examples/ncurses-sidebar-scrolling-container/multi-compilation-unit.c new file mode 100644 index 00000000..2c1a4737 --- /dev/null +++ b/examples/ncurses-sidebar-scrolling-container/multi-compilation-unit.c @@ -0,0 +1,9 @@ +#include "../../clay.h" + +// NOTE: This file only exists to make sure that clay works when included in multiple translation units. + +void SatisfyCompiler() { + CLAY(CLAY_ID("SatisfyCompiler"), CLAY_LAYOUT({})) { + CLAY_TEXT(CLAY_STRING("Test"), CLAY_TEXT_CONFIG({ .fontId = 0, .fontSize = 24 })); + } +} \ No newline at end of file diff --git a/renderers/ncurses/clay_renderer_ncurses.c b/renderers/ncurses/clay_renderer_ncurses.c new file mode 100644 index 00000000..ab1b8772 --- /dev/null +++ b/renderers/ncurses/clay_renderer_ncurses.c @@ -0,0 +1,129 @@ +//#include +#include +#define CLAY_IMPLEMENTATION +#include "../../clay.h" + +#define HPIXELS_PER_CHAR 5 //these are used to convert between Clay pixel space and terminal character locations +#define VPIXELS_PER_CHAR 8 + +void Clay_ncurses_Render(WINDOW * win, Clay_RenderCommandArray renderCommands); + +void Clay_ncurses_Render(WINDOW * win, Clay_RenderCommandArray renderCommands){ + short color_pair = 1; //increment on use, 0 is reserved + short color = 10; //get passed reserved colors + //maybe keep a list of Clay colors and only init a new color if required. + //clear the screen/window + clear();//sets cursor to 0,0 + for(int i = 0; i < renderCommands.length; i++){ + //handle every command + switch (renderCommands.internalArray[i].commandType){ + case CLAY_RENDER_COMMAND_TYPE_NONE: + continue; + case CLAY_RENDER_COMMAND_TYPE_RECTANGLE: + Clay_RectangleElementConfig *rectangle_config = renderCommands.internalArray[i].config.rectangleElementConfig; + init_color(color, rectangle_config->color.r, rectangle_config->color.g, rectangle_config->color.b); + init_pair(color_pair, color, color); + attr_on(color_set(color_pair,0),0); + for(int j = 0; j < renderCommands.internalArray[i].boundingBox.height/VPIXELS_PER_CHAR; j++){ + mvhline(renderCommands.internalArray[i].boundingBox.y/VPIXELS_PER_CHAR + j, renderCommands.internalArray[i].boundingBox.x/HPIXELS_PER_CHAR, '#', renderCommands.internalArray[i].boundingBox.width/HPIXELS_PER_CHAR); + } + attr_off(color_set(color_pair,0),0); + color_pair++; + color++; + //TODO render radius corners + break; + case CLAY_RENDER_COMMAND_TYPE_BORDER: + Clay_BorderElementConfig *border_config = renderCommands.internalArray[i].config.borderElementConfig; + //just get a border on there for now + if(border_config->top.width > 0){ + init_color(color, border_config->top.color.r, border_config->top.color.g, border_config->top.color.b); + init_pair(color_pair, color, COLOR_CYAN);//TODO get color at target location and init pair with that background + attr_on(color_set(color_pair,0),0); + mvhline(renderCommands.internalArray[i].boundingBox.y/VPIXELS_PER_CHAR, renderCommands.internalArray[i].boundingBox.x/HPIXELS_PER_CHAR + 1, '-', renderCommands.internalArray[i].boundingBox.width/HPIXELS_PER_CHAR - 2); + attr_off(color_set(color_pair,0),0); + color_pair++; //can we just check of the color requested is already there? + color++; + } + if(border_config->bottom.width > 0){ + init_color(color, border_config->bottom.color.r, border_config->bottom.color.g, border_config->bottom.color.b); + init_pair(color_pair, color, COLOR_CYAN);//TODO get color at target location and init pair with that background + attr_on(color_set(color_pair,0),0); + mvhline(renderCommands.internalArray[i].boundingBox.y/VPIXELS_PER_CHAR + renderCommands.internalArray[i].boundingBox.height/VPIXELS_PER_CHAR, renderCommands.internalArray[i].boundingBox.x/HPIXELS_PER_CHAR + 1, '-', renderCommands.internalArray[i].boundingBox.width/HPIXELS_PER_CHAR - 2); + attr_off(color_set(color_pair,0),0); + color_pair++; + color++; + } + if(border_config->left.width > 0){ + init_color(color, border_config->left.color.r, border_config->left.color.g, border_config->left.color.b); + init_pair(color_pair, color, COLOR_CYAN);//TODO get color at target location and init pair with that background + attr_on(color_set(color_pair,0),0); + mvvline(renderCommands.internalArray[i].boundingBox.y/VPIXELS_PER_CHAR + 1, renderCommands.internalArray[i].boundingBox.x/HPIXELS_PER_CHAR, '|', renderCommands.internalArray[i].boundingBox.height/VPIXELS_PER_CHAR - 1); + attr_off(color_set(color_pair,0),0); + color_pair++; + color++; + } + if(border_config->right.width > 0){ + init_color(color, border_config->right.color.r, border_config->right.color.g, border_config->right.color.b); + init_pair(color_pair, color, COLOR_CYAN);//TODO get color at target location and init pair with that background + attr_on(color_set(color_pair,0),0); + mvvline(renderCommands.internalArray[i].boundingBox.y/VPIXELS_PER_CHAR + 1, renderCommands.internalArray[i].boundingBox.x/HPIXELS_PER_CHAR + renderCommands.internalArray[i].boundingBox.width/HPIXELS_PER_CHAR - 1, '|', renderCommands.internalArray[i].boundingBox.height/VPIXELS_PER_CHAR - 1); + attr_off(color_set(color_pair,0),0); + color_pair++; + color++; + } + break; + case CLAY_RENDER_COMMAND_TYPE_TEXT: + Clay_TextElementConfig *text_config = renderCommands.internalArray[i].config.textElementConfig; + attr_on(color_set(0,0),0); + int x = renderCommands.internalArray[i].boundingBox.x/HPIXELS_PER_CHAR; + int y = renderCommands.internalArray[i].boundingBox.y/VPIXELS_PER_CHAR; //text is referenced from bottom corner? + int w = renderCommands.internalArray[i].boundingBox.width/HPIXELS_PER_CHAR; + int h = renderCommands.internalArray[i].boundingBox.height/VPIXELS_PER_CHAR; + int line = 0; + int column = 0; + for (int k = 0; k < renderCommands.internalArray[i].text.length; k++) { + if (column >= w) { + column = 0; + line += 1; + } + mvaddch(y + line, x + column, renderCommands.internalArray[i].text.chars[k]); + column += 1; + } + break; + case CLAY_RENDER_COMMAND_TYPE_IMAGE: + case CLAY_RENDER_COMMAND_TYPE_SCISSOR_START: + case CLAY_RENDER_COMMAND_TYPE_SCISSOR_END: + case CLAY_RENDER_COMMAND_TYPE_CUSTOM: + default: continue; + } + } + attr_on(color_set(0,0),0); + mvwprintw(win, 0, 0, "Number of color pairs used: %i", color_pair); + refresh();//update the screen/window +} + + +//written by EmmanuelMess: https://github.com/nicbarker/clay/pull/91/commits/7ce74ba46c01f32e4517032e9da76bf54ecf7a43 +static inline Clay_Dimensions ncurses_MeasureText(Clay_String *text, Clay_TextElementConfig *config) { + Clay_Dimensions textSize = { 0 }; + float maxTextWidth = 0.0f; + float lineTextWidth = 0; + float textHeight = 1; + + for (int i = 0; i < text->length; ++i) + { + if (text->chars[i] == '\n') { + maxTextWidth = maxTextWidth > lineTextWidth ? maxTextWidth : lineTextWidth; + lineTextWidth = 0; + textHeight++; + continue; + } + lineTextWidth++; + } + maxTextWidth = maxTextWidth > lineTextWidth ? maxTextWidth : lineTextWidth; + + textSize.width = maxTextWidth*HPIXELS_PER_CHAR; + textSize.height = textHeight*VPIXELS_PER_CHAR; + + return textSize; +}