Grid
This commit is contained in:
parent
933b515fad
commit
768c55af4a
2
cbuild
2
cbuild
@ -1 +1 @@
|
||||
Subproject commit e8e42424d329ef1e75fb827ddf85d55b4cd89169
|
||||
Subproject commit 9711d8fbb1bf947101f475c8cf918009a4d39110
|
||||
@ -41,7 +41,9 @@ case "$TASK" in
|
||||
# -flto applies more optimizations across object files
|
||||
# -flto=auto is needed to multithreaded copilation
|
||||
# -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"
|
||||
LINKER_ARGS="$CPP_ARGS"
|
||||
PRE_TASK_SCRIPT=tasks/pre_build.sh
|
||||
|
||||
2
kerep
2
kerep
@ -1 +1 @@
|
||||
Subproject commit 26b69f9b7bb7a816b8f048557efe0641428b9e80
|
||||
Subproject commit ededa875d6d1b5947e4516588489b1c8c64e940f
|
||||
@ -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
60
src/TUI/Grid.c
Normal 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;
|
||||
}
|
||||
|
||||
@ -5,9 +5,9 @@ void TextBlock_freeMembers(void* _self){
|
||||
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;
|
||||
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_drawBorder(renderer, self->base.border, area),_1,;);
|
||||
for(u16 i=0; i<area.w-2 && i<self->text.length; i++){
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
#include "tui_internal.h"
|
||||
|
||||
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
|
||||
@ -8,12 +10,10 @@ UIElement __UIElement_createDefault(ktid typeId, UIElement_draw_t drawFunc){
|
||||
return (UIElement){
|
||||
.type=ktDescriptor_get(typeId),
|
||||
.min_width=2,
|
||||
.max_width=UIElement_stretch,
|
||||
.min_height=2,
|
||||
.max_height=UIElement_stretch,
|
||||
.fgColor=kp_fgWhite,
|
||||
.bgColor=kp_bgBlack,
|
||||
.anchor=UIAnchor_Center,
|
||||
.width_scaling=UIElement_no_scaling,
|
||||
.height_scaling=UIElement_no_scaling,
|
||||
.color=kp_fgWhite|kp_bgBlack,
|
||||
.border={
|
||||
.left=UIBorder_Thin, .right=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)
|
||||
self->type->freeMembers(self);
|
||||
free(self);
|
||||
@ -36,12 +36,8 @@ UI_Maybe DrawingArea_validate(DrawingArea a){
|
||||
return MaybeNull;
|
||||
}
|
||||
|
||||
UI_Maybe UIElement_validate(UIElement* u, DrawingArea a){
|
||||
UI_Maybe UIElement_validate(UIElement_Ptr u, DrawingArea 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)
|
||||
UI_safethrow(UIError_InvalidHeight,;);
|
||||
if(u->min_width>a.w)
|
||||
|
||||
@ -7,6 +7,6 @@ void kt_initScolteTypes(){
|
||||
kt_register(DrawingArea);
|
||||
kt_register(UIBorder);
|
||||
kt_register(UIElement);
|
||||
kt_register(Canvas);
|
||||
kt_register(Grid);
|
||||
kt_register(TextBlock);
|
||||
}
|
||||
|
||||
@ -20,18 +20,6 @@ void kt_initScolteTypes();
|
||||
// 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,
|
||||
UIBorder_Hidden, /* blank space */
|
||||
UIBorder_Thin,
|
||||
@ -100,43 +88,46 @@ UI_THROWING_FUNC_DECL(Renderer_drawBorder(Renderer* renderer, UIBorder border, c
|
||||
//////////////////////////////////////
|
||||
|
||||
typedef struct UIElement UIElement;
|
||||
typedef UIElement* UIElementPtr;
|
||||
typedef UI_THROWING_FUNC_DECL((*UIElement_draw_t)(Renderer* renderer, UIElement* self, const DrawingArea area));
|
||||
typedef UIElement* UIElement_Ptr;
|
||||
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,
|
||||
ktDescriptor* type;
|
||||
u16 min_width;
|
||||
u16 max_width;
|
||||
u16 min_height;
|
||||
u16 max_height;
|
||||
kp_fgColor fgColor;
|
||||
kp_bgColor bgColor;
|
||||
UIAnchor anchor;
|
||||
u16 width_scaling;
|
||||
u16 height_scaling;
|
||||
kp_fmt color;
|
||||
UIBorder border;
|
||||
UIElement_draw_t draw;
|
||||
)
|
||||
|
||||
/// 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) \
|
||||
((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 //
|
||||
//////////////////////////////////////
|
||||
|
||||
////// Canvas //////
|
||||
////// Grid //////
|
||||
|
||||
STRUCT(Canvas,
|
||||
STRUCT(Grid,
|
||||
UIElement base;
|
||||
/* Autoarr(UIElementPtr) */
|
||||
Autoarr(Pointer)* children;
|
||||
u16 columns;
|
||||
u16 rows;
|
||||
UIElement_Ptr* ui_elements; /* UIElement[rows][columns] */
|
||||
)
|
||||
|
||||
Canvas* Canvas_create();
|
||||
void Canvas_addChild(Canvas* self, UIElement* child);
|
||||
Grid* Grid_create(u16 columns, u16 rows, UIElement_Ptr* ui_elements);
|
||||
UIElement_Ptr Grid_get(Grid* grid, u16 column, u16 row);
|
||||
void Grid_set(Grid* grid, u16 column, u16 row, UIElement_Ptr value);
|
||||
|
||||
|
||||
////// TextBlock //////
|
||||
|
||||
@ -8,7 +8,7 @@ extern "C" {
|
||||
|
||||
UIElement __UIElement_createDefault(ktid typeId, UIElement_draw_t drawFunc);
|
||||
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_v[4];
|
||||
|
||||
11
src/main.c
11
src/main.c
@ -1,5 +1,5 @@
|
||||
#include "../kerep/src/Filesystem/filesystem.h"
|
||||
#include "TUI/tui.h"
|
||||
#include "../kerep/src/Filesystem/filesystem.h"
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#if _WIN32 || _WIN64
|
||||
@ -43,13 +43,14 @@ i32 main(const i32 argc, const char* const* argv){
|
||||
fputs(text,stdout); */
|
||||
|
||||
Renderer* renderer=Renderer_create();
|
||||
Canvas* mainCanvas=Canvas_create();
|
||||
TextBlock* testTextBlock=TextBlock_create(string_fromCptr("some example text"));
|
||||
Canvas_addChild(mainCanvas, (UIElement*)testTextBlock);
|
||||
tryLast(UIElement_draw(renderer, (UIElement*)mainCanvas, ((DrawingArea){.x=10, .y=4, .h=7, .w=24})),_);
|
||||
Autoarr(UIElement_Ptr)* grid_content=Autoarr_create(UIElement_Ptr, 1, 64);
|
||||
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);
|
||||
|
||||
UIElement_destroy((UIElement*)mainCanvas);
|
||||
UIElement_destroy((UIElement_Ptr)mainGrid);
|
||||
Renderer_destroy(renderer);
|
||||
|
||||
kt_free();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user