-
Notifications
You must be signed in to change notification settings - Fork 345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Renderers/Terminal] Feature/terminal renderer #168
Open
phyxl
wants to merge
6
commits into
nicbarker:main
Choose a base branch
from
phyxl:feature/terminal-renderer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
77d3fc4
implemented ncurses termninal renderer
phyxl 286c075
Merge branch 'main' into feature/terminal-renderer
phyxl b0b0e15
clean up some files
phyxl 244395e
rename to ncurses to follow convention, clean up checked in files
phyxl c8341dc
fix typo, remove generated Makefile, correct CMakeLists
phyxl d7104ef
Merge branch 'main' into feature/terminal-renderer
phyxl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
examples/ncurses-sidebar-scrolling-container/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
Large diffs are not rendered by default.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
examples/ncurses-sidebar-scrolling-container/multi-compilation-unit.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 })); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
//#include <stdio.h> | ||
#include <curses.h> | ||
#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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its probably not a good idea to add the author of the functions, git blame already provides this feature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would, but I didn't use your branch, so it will show that I was the author instead. I want to make sure you get whatever credit you want for that.