This commit is contained in:
Timerix22 2023-05-19 19:54:52 +06:00
parent 933b515fad
commit 768c55af4a
11 changed files with 103 additions and 84 deletions

2
cbuild

@ -1 +1 @@
Subproject commit e8e42424d329ef1e75fb827ddf85d55b4cd89169 Subproject commit 9711d8fbb1bf947101f475c8cf918009a4d39110

View File

@ -41,7 +41,9 @@ case "$TASK" in
# -flto applies more optimizations across object files # -flto applies more optimizations across object files
# -flto=auto is needed to multithreaded copilation # -flto=auto is needed to multithreaded copilation
# -fuse-linker-plugin is required to use static libs with lto, it strips away all # -fuse-linker-plugin is required to use static libs with lto, it strips away all
C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-use -fprofile-prefix-path=$(realpath $OBJDIR)/objects" # -fdata-sections -ffunction-sections -Wl,--gc-sections removes unused code
C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-use -fprofile-prefix-path=$(realpath $OBJDIR)/objects \
-fdata-sections -ffunction-sections -Wl,--gc-sections"
CPP_ARGS="$C_ARGS" CPP_ARGS="$C_ARGS"
LINKER_ARGS="$CPP_ARGS" LINKER_ARGS="$CPP_ARGS"
PRE_TASK_SCRIPT=tasks/pre_build.sh PRE_TASK_SCRIPT=tasks/pre_build.sh

2
kerep

@ -1 +1 @@
Subproject commit 26b69f9b7bb7a816b8f048557efe0641428b9e80 Subproject commit ededa875d6d1b5947e4516588489b1c8c64e940f

View File

@ -1,31 +0,0 @@
#include "tui_internal.h"
void Canvas_freeMembers(void* _self){
Canvas* self=(Canvas*)_self;
Autoarr_foreach(self->children, ch,
UIElement_destroy((UIElement*)ch));
Autoarr_freeWithoutMembers(self->children, true);
}
UI_Maybe Canvas_draw(Renderer* renderer, UIElement* _self, const DrawingArea area){
Canvas* self=(Canvas*)_self;
Autoarr_foreach(self->children, ch, ({
if(ch==NULL)
UI_safethrow(UIError_NullPtr,;);
UI_try(UIElement_draw(renderer, (UIElement*)ch, area),_,;);
}));
return MaybeNull;
}
kt_define(Canvas, Canvas_freeMembers, NULL);
Canvas* Canvas_create(){
Canvas* canvas=malloc(sizeof(Canvas));
canvas->base=__UIElement_createDefault(ktid_name(Canvas), Canvas_draw);
canvas->children=Autoarr_create(Pointer, 32, 64);
return canvas;
}
void Canvas_addChild(Canvas* self, UIElement* child){
Autoarr_add(self->children, child);
}

60
src/TUI/Grid.c Normal file
View File

@ -0,0 +1,60 @@
#include "tui_internal.h"
static inline UIElement_Ptr* _Grid_getPtr(Grid* grid, u16 column, u16 row){
if(row >= grid->rows)
throw(ERR_WRONGINDEX);
if(column >= grid->columns)
throw(ERR_WRONGINDEX);
return grid->ui_elements + grid->columns*row + column;
}
UIElement_Ptr Grid_get(Grid* grid, u16 column, u16 row){
return *_Grid_getPtr(grid, column, row);
}
void Grid_set(Grid* grid, u16 column, u16 row, UIElement_Ptr value){
*_Grid_getPtr(grid, column, row)=value;
}
#define Grid_foreach(GRID_PTR, ELEM_VAR_NAME, CODE...){ \
for(u16 _g_r = 0; _g_r < GRID_PTR->rows; _g_r++){ \
for(u16 _g_c = 0; _g_c < GRID_PTR->columns; _g_c++){ \
UIElement_Ptr ELEM_VAR_NAME = Grid_get(GRID_PTR, _g_c, _g_r); \
{ CODE } \
} \
} \
}
void Grid_freeMembers(void* _self){
Grid* self=(Grid*)_self;
Grid_foreach(self, el,
if(el==NULL) // todo UI_throw
throw(ERR_NULLPTR);
UIElement_destroy(el);
);
free(self->ui_elements);
}
UI_Maybe Grid_draw(Renderer* renderer, UIElement_Ptr _self, const DrawingArea area){
Grid* self=(Grid*)_self;
Grid_foreach(self, el,
if(el==NULL)
UI_safethrow(UIError_NullPtr,;);
UI_try(UIElement_draw(renderer, el, area),_,;);
);
return MaybeNull;
}
kt_define(Grid, Grid_freeMembers, NULL);
Grid* Grid_create(u16 columns, u16 rows, UIElement_Ptr* ui_elements){
Grid* grid=malloc(sizeof(Grid));
*grid=(Grid){
.base=__UIElement_createDefault(ktid_name(Grid), Grid_draw),
.columns=columns,
.rows=rows,
.ui_elements=ui_elements
};
return grid;
}

View File

@ -5,9 +5,9 @@ void TextBlock_freeMembers(void* _self){
free(self->text.ptr); free(self->text.ptr);
} }
UI_Maybe TextBlock_draw(Renderer* renderer, UIElement* _self, const DrawingArea area){ UI_Maybe TextBlock_draw(Renderer* renderer, UIElement_Ptr _self, const DrawingArea area){
TextBlock* self=(TextBlock*)_self; TextBlock* self=(TextBlock*)_self;
UI_try(UIElement_validate((UIElement*)self, area),_0,;); UI_try(UIElement_validate((UIElement_Ptr)self, area),_0,;);
UI_try(Renderer_fill(renderer, TCI(TERMCHAR(' '), kp_bgBlack|kp_fgGray), area),_2,;); UI_try(Renderer_fill(renderer, TCI(TERMCHAR(' '), kp_bgBlack|kp_fgGray), area),_2,;);
UI_try(Renderer_drawBorder(renderer, self->base.border, area),_1,;); UI_try(Renderer_drawBorder(renderer, self->base.border, area),_1,;);
for(u16 i=0; i<area.w-2 && i<self->text.length; i++){ for(u16 i=0; i<area.w-2 && i<self->text.length; i++){

View File

@ -1,6 +1,8 @@
#include "tui_internal.h" #include "tui_internal.h"
kt_define(UIElement, NULL, NULL); kt_define(UIElement, NULL, NULL);
Autoarr_define(UIElement_Ptr, true);
Array_define(UIElement);
// ktid is needed to check if it is uninitilized by calling ktDescriptor_get // ktid is needed to check if it is uninitilized by calling ktDescriptor_get
@ -8,12 +10,10 @@ UIElement __UIElement_createDefault(ktid typeId, UIElement_draw_t drawFunc){
return (UIElement){ return (UIElement){
.type=ktDescriptor_get(typeId), .type=ktDescriptor_get(typeId),
.min_width=2, .min_width=2,
.max_width=UIElement_stretch,
.min_height=2, .min_height=2,
.max_height=UIElement_stretch, .width_scaling=UIElement_no_scaling,
.fgColor=kp_fgWhite, .height_scaling=UIElement_no_scaling,
.bgColor=kp_bgBlack, .color=kp_fgWhite|kp_bgBlack,
.anchor=UIAnchor_Center,
.border={ .border={
.left=UIBorder_Thin, .right=UIBorder_Thin, .left=UIBorder_Thin, .right=UIBorder_Thin,
.top=UIBorder_Thin, .bottom=UIBorder_Thin, .top=UIBorder_Thin, .bottom=UIBorder_Thin,
@ -23,7 +23,7 @@ UIElement __UIElement_createDefault(ktid typeId, UIElement_draw_t drawFunc){
}; };
} }
void UIElement_destroy(UIElement* self){ void UIElement_destroy(UIElement_Ptr self){
if(self->type->freeMembers) if(self->type->freeMembers)
self->type->freeMembers(self); self->type->freeMembers(self);
free(self); free(self);
@ -36,12 +36,8 @@ UI_Maybe DrawingArea_validate(DrawingArea a){
return MaybeNull; return MaybeNull;
} }
UI_Maybe UIElement_validate(UIElement* u, DrawingArea a){ UI_Maybe UIElement_validate(UIElement_Ptr u, DrawingArea a){
UI_try(DrawingArea_validate(a),_,;); UI_try(DrawingArea_validate(a),_,;);
if(u->max_height<u->min_height)
UI_safethrow(UIError_InvalidHeight,;);
if(u->max_width<u->min_width)
UI_safethrow(UIError_InvalidWidth,;);
if(u->min_height>a.h) if(u->min_height>a.h)
UI_safethrow(UIError_InvalidHeight,;); UI_safethrow(UIError_InvalidHeight,;);
if(u->min_width>a.w) if(u->min_width>a.w)

View File

@ -7,6 +7,6 @@ void kt_initScolteTypes(){
kt_register(DrawingArea); kt_register(DrawingArea);
kt_register(UIBorder); kt_register(UIBorder);
kt_register(UIElement); kt_register(UIElement);
kt_register(Canvas); kt_register(Grid);
kt_register(TextBlock); kt_register(TextBlock);
} }

View File

@ -20,18 +20,6 @@ void kt_initScolteTypes();
// Enums // // Enums //
////////////////////////////////////// //////////////////////////////////////
PACKED_ENUM(UIAnchor,
UIAnchor_Center=0,
UIAnchor_Left=1,
UIAnchor_Right=2,
UIAnchor_Top=4,
UIAnchor_Bottom=8,
UIAnchor_RightTop=UIAnchor_Right|UIAnchor_Top,
UIAnchor_RightBottom=UIAnchor_Right|UIAnchor_Bottom,
UIAnchor_LeftTop=UIAnchor_Left|UIAnchor_Top,
UIAnchor_LeftBottom=UIAnchor_Left|UIAnchor_Bottom
)
PACKED_ENUM(UIBorderThickness, PACKED_ENUM(UIBorderThickness,
UIBorder_Hidden, /* blank space */ UIBorder_Hidden, /* blank space */
UIBorder_Thin, UIBorder_Thin,
@ -100,43 +88,46 @@ UI_THROWING_FUNC_DECL(Renderer_drawBorder(Renderer* renderer, UIBorder border, c
////////////////////////////////////// //////////////////////////////////////
typedef struct UIElement UIElement; typedef struct UIElement UIElement;
typedef UIElement* UIElementPtr; typedef UIElement* UIElement_Ptr;
typedef UI_THROWING_FUNC_DECL((*UIElement_draw_t)(Renderer* renderer, UIElement* self, const DrawingArea area)); typedef UI_THROWING_FUNC_DECL((*UIElement_draw_t)(Renderer* renderer, UIElement_Ptr self, const DrawingArea area));
#define UIElement_stretch (u16)-1 #define UIElement_no_scaling (u16)0
STRUCT(UIElement, STRUCT(UIElement,
ktDescriptor* type; ktDescriptor* type;
u16 min_width; u16 min_width;
u16 max_width;
u16 min_height; u16 min_height;
u16 max_height; u16 width_scaling;
kp_fgColor fgColor; u16 height_scaling;
kp_bgColor bgColor; kp_fmt color;
UIAnchor anchor;
UIBorder border; UIBorder border;
UIElement_draw_t draw; UIElement_draw_t draw;
) )
/// proper way to free UIElement and all its members /// proper way to free UIElement and all its members
void UIElement_destroy(UIElement* self); void UIElement_destroy(UIElement_Ptr self);
#define UIElement_draw(RENDERER, UIE_PTR, PLACE_RECT) \ #define UIElement_draw(RENDERER, UIE_PTR, PLACE_RECT) \
((UIElement*)UIE_PTR)->draw(RENDERER, UIE_PTR, PLACE_RECT) ((UIElement_Ptr)UIE_PTR)->draw(RENDERER, UIE_PTR, PLACE_RECT)
Autoarr_declare(UIElement_Ptr)
Array_declare(UIElement)
////////////////////////////////////// //////////////////////////////////////
// UIElement derivatives // // UIElement derivatives //
////////////////////////////////////// //////////////////////////////////////
////// Canvas ////// ////// Grid //////
STRUCT(Canvas, STRUCT(Grid,
UIElement base; UIElement base;
/* Autoarr(UIElementPtr) */ u16 columns;
Autoarr(Pointer)* children; u16 rows;
UIElement_Ptr* ui_elements; /* UIElement[rows][columns] */
) )
Canvas* Canvas_create(); Grid* Grid_create(u16 columns, u16 rows, UIElement_Ptr* ui_elements);
void Canvas_addChild(Canvas* self, UIElement* child); UIElement_Ptr Grid_get(Grid* grid, u16 column, u16 row);
void Grid_set(Grid* grid, u16 column, u16 row, UIElement_Ptr value);
////// TextBlock ////// ////// TextBlock //////

View File

@ -8,7 +8,7 @@ extern "C" {
UIElement __UIElement_createDefault(ktid typeId, UIElement_draw_t drawFunc); UIElement __UIElement_createDefault(ktid typeId, UIElement_draw_t drawFunc);
UI_THROWING_FUNC_DECL(DrawingArea_validate(DrawingArea a)); UI_THROWING_FUNC_DECL(DrawingArea_validate(DrawingArea a));
UI_THROWING_FUNC_DECL(UIElement_validate(UIElement* u, DrawingArea a)); UI_THROWING_FUNC_DECL(UIElement_validate(UIElement_Ptr u, DrawingArea a));
extern termchar UIBorder_char_h[4]; extern termchar UIBorder_char_h[4];
extern termchar UIBorder_char_v[4]; extern termchar UIBorder_char_v[4];

View File

@ -1,5 +1,5 @@
#include "../kerep/src/Filesystem/filesystem.h"
#include "TUI/tui.h" #include "TUI/tui.h"
#include "../kerep/src/Filesystem/filesystem.h"
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#if _WIN32 || _WIN64 #if _WIN32 || _WIN64
@ -37,19 +37,20 @@ i32 main(const i32 argc, const char* const* argv){
kprintf(" %s", argv[i]); kprintf(" %s", argv[i]);
kprintf("\n"); kprintf("\n");
/*char* filePath= argc>1 ? argv[argc-1] : "new_file.txt"; /* char* filePath= argc>1 ? argv[argc-1] : "new_file.txt";
tryLast(tryReadFile(filePath), _m_text); tryLast(tryReadFile(filePath), _m_text);
char* text=(char*)_m_text.value.VoidPtr; char* text=(char*)_m_text.value.VoidPtr;
fputs(text,stdout);*/ fputs(text,stdout); */
Renderer* renderer=Renderer_create(); Renderer* renderer=Renderer_create();
Canvas* mainCanvas=Canvas_create();
TextBlock* testTextBlock=TextBlock_create(string_fromCptr("some example text")); TextBlock* testTextBlock=TextBlock_create(string_fromCptr("some example text"));
Canvas_addChild(mainCanvas, (UIElement*)testTextBlock); Autoarr(UIElement_Ptr)* grid_content=Autoarr_create(UIElement_Ptr, 1, 64);
tryLast(UIElement_draw(renderer, (UIElement*)mainCanvas, ((DrawingArea){.x=10, .y=4, .h=7, .w=24})),_); Autoarr_add(grid_content, (UIElement_Ptr)testTextBlock);
Grid* mainGrid=Grid_create(1,1, Autoarr_toArray(grid_content));
tryLast(UIElement_draw(renderer, (UIElement_Ptr)mainGrid, ((DrawingArea){.x=10, .y=4, .h=7, .w=24})),_);
tryLast(Renderer_drawFrame(renderer),_2); tryLast(Renderer_drawFrame(renderer),_2);
UIElement_destroy((UIElement*)mainCanvas); UIElement_destroy((UIElement_Ptr)mainGrid);
Renderer_destroy(renderer); Renderer_destroy(renderer);
kt_free(); kt_free();