started implementing dtsod parsing

This commit is contained in:
Timerix22 2023-05-22 04:06:55 +06:00
parent 03f9f889cc
commit 4e2376f251
21 changed files with 724 additions and 63 deletions

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
# build results # build results
bin/ bin/
obj/ obj/
src/generated/
*.log *.log
*.tmp *.tmp

2
kerep

@ -1 +1 @@
Subproject commit d7136055d92e90070b947f1662b3fa6981eecdc0 Subproject commit 27be5beacdfda586f518ab05de050775ce7b5f54

@ -1 +1 @@
Subproject commit 81df5940827fa4347d112d3f81c926ef3a6acb80 Subproject commit 1708415900b0d5caed72c5e0aab3c5039517ee41

141
src/TUI/Dtsod/tui_dtsod.c Normal file
View File

@ -0,0 +1,141 @@
#include "../tui_internal.h"
#include "tui_dtsod_internal.h"
#include "../../../kerep/src/Filesystem/filesystem.h"
//////////////////////////////////////
// UIDtsodFileModel //
//////////////////////////////////////
void UIDtsodFileModel_freeMembers(void* _fm){
UIDtsodFileModel* fm=_fm;
DtsodV24_free(fm->dtsod);
free(fm->dtsod_text);
free(fm->file_name);
}
kt_define(UIDtsodFileModel, UIDtsodFileModel_freeMembers, NULL);
Autoarr_define(UIDtsodFileModel, false);
//////////////////////////////////////
// UIDtsodParser //
//////////////////////////////////////
void _UIDtsodParser_freeMembers(void* _p){
UIDtsodParser* p=_p;
Autoarr_free(p->file_models, true);
if(!p->returned_context && p->context!=NULL){
UIContext_destroy(p->context);
}
}
void UIDtsodParser_destroy(UIDtsodParser* p){
_UIDtsodParser_freeMembers(p);
free(p);
}
kt_define(UIDtsodParser, _UIDtsodParser_freeMembers, NULL);
UIDtsodParser* UIDtsodParser_create(){
UIDtsodParser* parser=malloc(sizeof(UIDtsodParser));
parser->context=NULL;
parser->returned_context=false;
parser->file_models=Autoarr_create(UIDtsodFileModel, 64, 32);
return parser;
}
///@return UI_Maybe<void>
UI_Maybe UIDtsodParser_parseFile(UIDtsodParser* parser, char* file_path){
UI_try(file_open(file_path, FileOpenMode_Read), _m_file, ;);
FileHandle file=_m_file.value.VoidPtr;
char* file_content=NULL;
UI_try(file_readAll(file, &file_content), _m_bcount, ;)
UI_try(file_close(file), _m_1563, free(file_content));
UI_try(UIDtsodParser_parseText(parser, file_path, file_content), _6561, free(file_content));
return MaybeNull;
}
UI_Maybe UIDtsodParser_parseText(UIDtsodParser* parser, char* file_name_placeholder, char* text){
UI_try(DtsodV24_deserialize(text), _m_dtsod, ;);
UIDtsodFileModel fm={
.file_name=cptr_copy(file_name_placeholder),
.dtsod_text=text,
.dtsod=_m_dtsod.value.VoidPtr
};
Autoarr_add(parser->file_models, fm);
return MaybeNull;
}
void _UIContext_freeMembers(void* _u){
UIContext* u=_u;
Hashtable_free(u->ui_elements);
}
void UIContext_destroy(UIContext* u){
_UIContext_freeMembers(u);
free(u);
}
kt_define(UIContext, _UIContext_freeMembers, NULL);
UI_Maybe UIElement_deserialize(Dtsod* dtsod){
Unitype uni;
Dtsod_get_necessary(dtsod, "type");
UITDescriptor* ui_type=uni.VoidPtr;
UI_try(ui_type->deserialize(dtsod), _m_ui, ;);
return _m_ui;
}
UI_Maybe UIDtsodParser_constructUIContext(UIDtsodParser* parser){
if(parser->context == NULL){
parser->context=malloc(sizeof(UIContext));
parser->context->ui_elements=Hashtable_create();
}
Autoarr_foreach(parser->file_models, fm,
Hashtable_foreach(fm.dtsod, dtsod_elem,
if(cptr_compare(dtsod_elem.key, "tui_dtsod_version")){
}
else if(cptr_compare(dtsod_elem.key, "ui")) {
Autoarr_Unitype* ui_ar=dtsod_elem.value.VoidPtr;
Autoarr_foreach(ui_ar, ui_el_dtsod,
UI_try(UIElement_deserialize(ui_el_dtsod.VoidPtr),_m_ui,;);
UIElement_Ptr new_el=_m_ui.value.VoidPtr;
UI_try( UIContext_add(parser->context, new_el), _a76515, ;);
);
}
);
);
parser->returned_context=true;
return SUCCESS(UniHeapPtr(UIContext, parser->context));
}
UI_Maybe _UIContext_get(UIContext* context, char* name, ktid type_id){
Unitype val;
// check name
if(!Hashtable_tryGet(context->ui_elements, name, &val)){
UI_safethrow_msg(cptr_concat("can't get <", name, "> from context"), ;);
}
// check type
UIElement* ptr=val.VoidPtr;
if(val.typeId != type_id)
UI_safethrow_msg(cptr_concat(
"tried to get ",ktDescriptor_get(type_id)->name, " <",name,"> but it is of type ", ptr->ui_type->type->name
), ;);
return SUCCESS(val);
}
UI_Maybe UIContext_add(UIContext* context, _UIElement_Ptr _new_el){
UIElement_Ptr new_el=_new_el;
Unitype u=UniPtr(new_el->ui_type->type->id, new_el, true);
if(!Hashtable_tryAdd(context->ui_elements, new_el->name, u))
UI_safethrow_msg(cptr_concat("element with name <", new_el->name, "> already exists in context"), ;);
return MaybeNull;
}

81
src/TUI/Dtsod/tui_dtsod.h Normal file
View File

@ -0,0 +1,81 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../../../kerep/src/DtsodParser/DtsodV24.h"
#include "../UIError.h"
typedef Hashtable Dtsod;
typedef void* _UIElement_Ptr;
/*
HOW TO USE:
UIDtsodParser* p=UIDtsodParser_create();
UI_try(UIDtsodParser_parseFile("some_file.tui.dtsod"), _91624, UIDtsodParser_destroy(p));
UI_try(UIDtsodParser_parseFile("another_file.tui.dtsod"), _95927, UIDtsodParser_destroy(p));
UI_try(UIDtsodParser_constructUIContext(p), _m_ui_context, UIDtsodParser_destroy(p));
UIContext* ui_context=_m_ui_context.value.VoidPtr;
UIDtsodParser_destroy(p);
UIContext_get(ui_context, example_namespace, text_box, TextBlock, UIContext_destroy(ui_context));
example_namespace_text_box->text=string_fromCptr("text replaced");
*/
//////////////////////////////////////
// UIDtsodParser //
//////////////////////////////////////
typedef struct UIDtsodParser UIDtsodParser;
UIDtsodParser* UIDtsodParser_create();
void UIDtsodParser_destroy(UIDtsodParser* p);
///@return UI_Maybe<void>
UI_THROWING_FUNC_DECL(UIDtsodParser_parseFile(UIDtsodParser* parser, char* file_name));
///@return UI_Maybe<void>
UI_THROWING_FUNC_DECL(UIDtsodParser_parseText(UIDtsodParser* parser, char* file_name_placeholder, char* text));
///@return UI_Maybe<UIContext*>
UI_THROWING_FUNC_DECL(UIDtsodParser_constructUIContext(UIDtsodParser* parser));
//////////////////////////////////////
// UIContext //
//////////////////////////////////////
typedef struct UIContext UIContext;
void UIContext_destroy(UIContext* u);
///@return UI_Maybe<UIElement*>
UI_THROWING_FUNC_DECL(_UIContext_get(UIContext* context, char* name, ktid type_id));
#define UIContext_get(CONTEXT, NAMESPACE, NAME, TYPE, FREECALLS_ON_ERROR) \
UI_try( \
_UIContext_get(CONTEXT, #NAMESPACE "_" #NAME, ktid_name(TYPE)), \
_m_##NAMESPACE##_##NAME, \
FREECALLS_ON_ERROR); \
TYPE* NAMESPACE##_##NAME=_m_##NAMESPACE##_##NAME.value.VoidPtr;
///@return Maybe<void>
UI_THROWING_FUNC_DECL(UIContext_add(UIContext* context, _UIElement_Ptr new_el));
//////////////////////////////////////
// Deserialization //
//////////////////////////////////////
/// universal function for UIElement ancestors deserializing
/// @return Maybe<UIElement_Ptr>
UI_THROWING_FUNC_DECL( UIElement_deserialize(Dtsod* dtsod) );
#define Dtsod_get_necessary(dtsod, key) \
if(!Hashtable_tryGet(dtsod, key, &uni)) \
UI_safethrow(UIError_NullPtr, ;);
#define Dtsod_get_optional(dtsod, key) \
if(Hashtable_tryGet(dtsod, key, &uni))
#if __cplusplus
}
#endif

View File

@ -0,0 +1,21 @@
#pragma once
#include "tui_dtsod.h"
STRUCT(UIDtsodFileModel,
char* file_name;
char* dtsod_text;
Dtsod* dtsod;
)
Autoarr_declare(UIDtsodFileModel)
STRUCT(UIDtsodParser,
Autoarr(UIDtsodFileModel)* file_models;
UIContext* context;
bool returned_context;
)
STRUCT(UIContext,
Hashtable* ui_elements;
)

View File

@ -16,15 +16,6 @@ void Grid_set(Grid* grid, u16 column, u16 row, UIElement_Ptr value){
*_Grid_getPtr(grid, column, row)=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){ void Grid_freeMembers(void* _self){
Grid* self=(Grid*)_self; Grid* self=(Grid*)_self;
Grid_foreach(self, el, Grid_foreach(self, el,
@ -45,12 +36,36 @@ UI_Maybe Grid_draw(Renderer* renderer, UIElement_Ptr _self, const DrawingArea ar
return MaybeNull; return MaybeNull;
} }
kt_define(Grid, Grid_freeMembers, NULL); UI_Maybe Grid_deserialize(Dtsod* dtsod){
Grid gr;
Unitype uni;
Autoarr(UIElement_Ptr)* content=Autoarr_create(UIElement_Ptr, 64, 8);
Grid* Grid_create(u16 columns, u16 rows, UIElement_Ptr* ui_elements){ UI_try(UIElement_deserializeBase(dtsod, &gr.base), _91875, ;);
Dtsod_get_necessary(dtsod, "content"){
Autoarr(Unitype)* _content=uni.VoidPtr;
Autoarr_foreach(_content, _row,
Autoarr(Unitype)* row=_row.VoidPtr;
Autoarr_foreach(row, _elem_d,
Dtsod* elem_dtsod=_elem_d.VoidPtr;
UI_try(UIElement_deserialize(elem_dtsod), _m_uie, ;);
Autoarr_add(content, (UIElement_Ptr)_m_uie.value.VoidPtr);
)
);
}
Grid* ptr=malloc(sizeof(*ptr));
*ptr=gr;
return SUCCESS(UniHeapPtr(TextBlock, ptr));
}
uit_define(Grid, Grid_freeMembers, NULL, Grid_draw, Grid_deserialize);
Grid* Grid_create(char* name, u16 columns, u16 rows, UIElement_Ptr* ui_elements){
Grid* grid=malloc(sizeof(Grid)); Grid* grid=malloc(sizeof(Grid));
*grid=(Grid){ *grid=(Grid){
.base=__UIElement_createDefault(ktid_name(Grid), Grid_draw), .base=_UIElement_initBaseDefault(name, &UITDescriptor_Grid),
.columns=columns, .columns=columns,
.rows=rows, .rows=rows,
.ui_elements=ui_elements .ui_elements=ui_elements

View File

@ -18,11 +18,27 @@ UI_Maybe TextBlock_draw(Renderer* renderer, UIElement_Ptr _self, const DrawingAr
return MaybeNull; return MaybeNull;
} }
kt_define(TextBlock, TextBlock_freeMembers, NULL); UI_Maybe TextBlock_deserialize(Dtsod* dtsod){
TextBlock tb;
Unitype uni;
TextBlock* TextBlock_create(string text){ UI_try(UIElement_deserializeBase(dtsod, &tb.base), _8751, ;);
Dtsod_get_necessary(dtsod, "text"){
char* cptr=uni.VoidPtr;
tb.text=(string){.ptr=cptr, .length=cptr_length(cptr)};
}
TextBlock* ptr=malloc(sizeof(*ptr));
*ptr=tb;
return SUCCESS(UniHeapPtr(TextBlock, ptr));
}
uit_define(TextBlock, TextBlock_freeMembers, NULL,TextBlock_draw, TextBlock_deserialize);
TextBlock* TextBlock_create(char* name, string text){
TextBlock* textBlock=malloc(sizeof(TextBlock)); TextBlock* textBlock=malloc(sizeof(TextBlock));
textBlock->base=__UIElement_createDefault(ktid_name(TextBlock), TextBlock_draw); textBlock->base=_UIElement_initBaseDefault(name, &UITDescriptor_TextBlock);
textBlock->text=string_copy(text); textBlock->text=string_copy(text);
return textBlock; return textBlock;
} }

View File

@ -4,32 +4,30 @@ kt_define(UIElement, NULL, NULL);
Autoarr_define(UIElement_Ptr, true); Autoarr_define(UIElement_Ptr, true);
Array_define(UIElement); 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
UIElement __UIElement_createDefault(ktid typeId, UIElement_draw_t drawFunc){ inline UIElement _UIElement_initBaseDefault(char* name, UITDescriptor* uit){
return (UIElement){ return (UIElement){
.type=ktDescriptor_get(typeId), .name=name,
.ui_type=uit,
.min_width=2, .min_width=2,
.min_height=2, .min_height=2,
.width_scaling=UIElement_no_scaling, .width_scaling=UIElement_no_scaling,
.height_scaling=UIElement_no_scaling, .height_scaling=UIElement_no_scaling,
.color=kp_fgWhite|kp_bgBlack, .color=kp_bgBlack|kp_fgGray,
.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,
.color=kp_bgBlack|kp_fgGray .color=kp_bgBlack|kp_fgGray
}, },
.draw=drawFunc
}; };
} }
void UIElement_destroy(UIElement_Ptr self){ void UIElement_destroy(UIElement_Ptr self){
if(self->type->freeMembers) if(self->ui_type->type->freeMembers)
self->type->freeMembers(self); self->ui_type->type->freeMembers(self);
free(self); free(self);
} }
UI_Maybe DrawingArea_validate(DrawingArea a){ UI_Maybe DrawingArea_validate(DrawingArea a){
if(a.h<1) UI_safethrow(UIError_InvalidWidth,;); if(a.h<1) UI_safethrow(UIError_InvalidWidth,;);
if(a.w<1) UI_safethrow(UIError_InvalidHeight,;); if(a.w<1) UI_safethrow(UIError_InvalidHeight,;);
@ -44,3 +42,43 @@ UI_Maybe UIElement_validate(UIElement_Ptr u, DrawingArea a){
UI_safethrow(UIError_InvalidWidth,;); UI_safethrow(UIError_InvalidWidth,;);
return MaybeNull; return MaybeNull;
} }
UI_Maybe UIElement_deserializeBase(Dtsod* dtsod, UIElement* base){
Unitype uni;
Dtsod_get_necessary(dtsod, "name");
char* name=uni.VoidPtr;
Dtsod_get_necessary(dtsod, "type");
UITDescriptor* ui_type=uni.VoidPtr;
*base=_UIElement_initBaseDefault(name, ui_type);
Dtsod_get_optional(dtsod, "min_width")
base->min_width=uni.UInt64;
Dtsod_get_optional(dtsod, "min_height")
base->min_height=uni.UInt64;
Dtsod_get_optional(dtsod, "width_scaling")
base->width_scaling=uni.UInt64;
Dtsod_get_optional(dtsod, "height_scaling")
base->height_scaling=uni.UInt64;
Dtsod_get_optional(dtsod, "bg_color")
set_color(uni.VoidPtr, base->color);
Dtsod_get_optional(dtsod, "fg_color")
set_color(uni.VoidPtr, base->color);
base->border.color=base->color;
Dtsod_get_optional(dtsod, "border"){
Dtsod* border_dtsod=uni.VoidPtr;
Dtsod_get_optional(border_dtsod, "top")
set_border_thickness(uni.VoidPtr, base->border.top);
Dtsod_get_optional(border_dtsod, "bottom")
set_border_thickness(uni.VoidPtr, base->border.bottom);
Dtsod_get_optional(border_dtsod, "left")
set_border_thickness(uni.VoidPtr, base->border.left);
Dtsod_get_optional(border_dtsod, "right")
set_border_thickness(uni.VoidPtr, base->border.right);
Dtsod_get_optional(dtsod, "bg_color")
set_color(uni.VoidPtr, base->border.color);
Dtsod_get_optional(dtsod, "fg_color")
set_color(uni.VoidPtr, base->border.color);
}
return MaybeNull;
}

View File

@ -7,7 +7,8 @@ extern "C" {
#include "../../kerep/src/base/base.h" #include "../../kerep/src/base/base.h"
PACKED_ENUM(UIError, PACKED_ENUM(UIError,
UIError_Success=0, UIError_Success=ERR_IO_EOF+1,
UIError_NotImplemented,
UIError_NullPtr, UIError_NullPtr,
UIError_InvalidHeight, UIError_InvalidWidth, UIError_InvalidHeight, UIError_InvalidWidth,
UIError_InvalidX, UIError_InvalidY, UIError_InvalidX, UIError_InvalidY,

113
src/TUI/enums_getByName.c Normal file
View File

@ -0,0 +1,113 @@
#include "tui_internal.h"
#define enum_pair(name, value) { cptr_copy(name), UniUInt64(value) }
Hashtable* _colors_table=NULL;
void termcolor_table_init(){
_colors_table=Hashtable_create();
KVPair pairs[]={
enum_pair("black", kp_bgBlack|kp_fgBlack),
enum_pair("white", kp_bgWhite|kp_fgWhite),
enum_pair("gray", kp_bgGray|kp_fgGray),
enum_pair("red", kp_bgRed|kp_fgRed),
enum_pair("green", kp_bgGreen|kp_fgGreen),
enum_pair("yellow", kp_bgYellow|kp_fgYellow),
enum_pair("blue", kp_bgBlue|kp_fgBlue),
enum_pair("magenta", kp_bgMagenta|kp_fgMagenta),
enum_pair("cyan", kp_bgCyan|kp_fgCyan),
enum_pair("gray_dark", kp_bgGrayD|kp_fgGrayD),
enum_pair("red_dark", kp_bgRedD|kp_fgRedD),
enum_pair("green_dark", kp_bgGreenD|kp_fgGreenD),
enum_pair("yellow_dark", kp_bgYellowD|kp_fgYellowD),
enum_pair("blue_dark", kp_bgBlueD|kp_fgBlueD),
enum_pair("magenta_dark", kp_bgMagentaD|kp_fgMagentaD),
enum_pair("cyan_dark", kp_bgCyanD|kp_fgCyanD),
enum_pair("dark_gray", kp_bgGrayD|kp_fgGrayD),
enum_pair("dark_red", kp_bgRedD|kp_fgRedD),
enum_pair("dark_green", kp_bgGreenD|kp_fgGreenD),
enum_pair("dark_yellow", kp_bgYellowD|kp_fgYellowD),
enum_pair("dark_blue", kp_bgBlueD|kp_fgBlueD),
enum_pair("dark_magenta", kp_bgMagentaD|kp_fgMagentaD),
enum_pair("dark_cyan", kp_bgCyanD|kp_fgCyanD),
enum_pair("gray_d", kp_bgGrayD|kp_fgGrayD),
enum_pair("red_d", kp_bgRedD|kp_fgRedD),
enum_pair("green_d", kp_bgGreenD|kp_fgGreenD),
enum_pair("yellow_d", kp_bgYellowD|kp_fgYellowD),
enum_pair("blue_d", kp_bgBlueD|kp_fgBlueD),
enum_pair("magenta_d", kp_bgMagentaD|kp_fgMagentaD),
enum_pair("cyan_d", kp_bgCyanD|kp_fgCyanD),
enum_pair("d_gray", kp_bgGrayD|kp_fgGrayD),
enum_pair("d_red", kp_bgRedD|kp_fgRedD),
enum_pair("d_green", kp_bgGreenD|kp_fgGreenD),
enum_pair("d_yellow", kp_bgYellowD|kp_fgYellowD),
enum_pair("d_blue", kp_bgBlueD|kp_fgBlueD),
enum_pair("d_magenta", kp_bgMagentaD|kp_fgMagentaD),
enum_pair("d_cyan", kp_bgCyanD|kp_fgCyanD),
enum_pair("gray_d", kp_bgGrayD|kp_fgGrayD),
enum_pair("red_d", kp_bgRedD|kp_fgRedD),
enum_pair("green_d", kp_bgGreenD|kp_fgGreenD),
enum_pair("yellow_d", kp_bgYellowD|kp_fgYellowD),
enum_pair("blue_d", kp_bgBlueD|kp_fgBlueD),
enum_pair("magenta_d", kp_bgMagentaD|kp_fgMagentaD),
enum_pair("cyan_d", kp_bgCyanD|kp_fgCyanD),
enum_pair("dgray", kp_bgGrayD|kp_fgGrayD),
enum_pair("dred", kp_bgRedD|kp_fgRedD),
enum_pair("dgreen", kp_bgGreenD|kp_fgGreenD),
enum_pair("dyellow", kp_bgYellowD|kp_fgYellowD),
enum_pair("dblue", kp_bgBlueD|kp_fgBlueD),
enum_pair("dmagenta", kp_bgMagentaD|kp_fgMagentaD),
enum_pair("dcyan", kp_bgCyanD|kp_fgCyanD),
};
Hashtable_addMany(_colors_table, pairs, sizeof(pairs)/sizeof(KVPair));
}
int termcolor_getByName(char* color_name){
char* color_name_lower=cptr_toLower(color_name);
Unitype uni;
if(!Hashtable_tryGet(_colors_table, color_name_lower, &uni))
return -1;
return uni.UInt64;
}
Hashtable* _UIBorderThickness_table=NULL;
void UIBorderThickness_table_init(){
_UIBorderThickness_table=Hashtable_create();
KVPair pairs[]={
enum_pair("hidden", UIBorder_Hidden),
enum_pair("0", UIBorder_Hidden),
enum_pair("thin", UIBorder_Thin),
enum_pair("0.5", UIBorder_Thin),
enum_pair("thick", UIBorder_Thick),
enum_pair("1", UIBorder_Thick),
enum_pair("double", UIBorder_Double),
enum_pair("2", UIBorder_Double),
enum_pair("noborder", UiBorder_NoBorder),
enum_pair("no", UiBorder_NoBorder)
};
Hashtable_addMany(_UIBorderThickness_table, pairs, sizeof(pairs)/sizeof(KVPair));
}
int UIBorderThickness_getByName(char* name){
char* name_lower=cptr_toLower(name);
Unitype uni;
if(!Hashtable_tryGet(_UIBorderThickness_table, name_lower, &uni))
return -1;
return uni.UInt64;
}
void UI_enum_tables_init(){
termcolor_table_init();
UIBorderThickness_table_init();
}
void UI_enum_tables_free(){
Hashtable_free(_colors_table);
Hashtable_free(_UIBorderThickness_table);
}

View File

@ -1,18 +1,79 @@
#include "tui.h" #include "tui_internal.h"
#include "Dtsod/tui_dtsod_internal.h"
kt_define(DrawingArea, NULL, NULL); kt_define(DrawingArea, NULL, NULL);
kt_define(UIBorder, NULL, NULL); kt_define(UIBorder, NULL, NULL);
kt_define(UITDescriptor, NULL, NULL);
Hashtable* __uit_hashtable=NULL;
u32 __kt_id_first=-1;
Autoarr(Pointer)* __uit_temp_arr=NULL;
UITDescriptor** __uit_descriptors=NULL;
/// call this between kt_beginInit() and kt_endInit()
void kt_beginInitTUI(){
__uit_hashtable=Hashtable_create();
__kt_id_first=ktid_last;
__uit_temp_arr=Autoarr_create(Pointer, 32, 32);
}
/// call this between kt_beginInit() and kt_endInit()
void kt_endInitTUI(){
__uit_descriptors=(UITDescriptor**)Autoarr_toArray(__uit_temp_arr);
Autoarr_free(__uit_temp_arr, true);
}
void kt_freeTUI(){
free(__uit_descriptors);
Hashtable_free(__uit_hashtable);
}
void __uit_register(ktDescriptor* kt, UITDescriptor* uit){
uit->type=kt;
Hashtable_add(__uit_hashtable, cptr_copy(kt->name), UniStackPtr(UITDescriptor, uit));
}
UITDescriptor* UITDescriptor_getById(ktid id){
if(id > ktid_last)
throw(ERR_WRONGINDEX);
if(__uit_descriptors==NULL)
throw(ERR_NULLPTR);
return __uit_descriptors[id - __kt_id_first];
}
///@return NULL if not found
UITDescriptor* UITDescriptor_getByName(char* name){
Unitype val=Hashtable_get(__uit_hashtable, name);
return val.VoidPtr;
}
void kt_initScolteTypes(){ void kt_initScolteTypes(){
// term // term.h
kt_register(TerminalSize); kt_register(TerminalSize);
kt_register(TermCharInfo); kt_register(TermCharInfo);
// tui // tui.h
kt_register(UITDescriptor)
kt_register(DrawingArea); kt_register(DrawingArea);
kt_register(UIBorder); kt_register(UIBorder);
kt_register(FrameBuffer); kt_register(FrameBuffer);
kt_register(Renderer); kt_register(Renderer);
kt_register(UIElement); kt_register(UIElement);
kt_register(Grid); uit_register(Grid);
kt_register(TextBlock); uit_register(TextBlock);
// tui_dtsod.h
kt_register(UIDtsodFileModel);
kt_register(UIDtsodParser);
kt_register(UIContext);
}
/// call this between kt_beginInitTUI() and kt_endInitTUI()
void Scolte_init(){
kt_initScolteTypes();
UI_enum_tables_init();
}
// call this before kt_free()
void Scolte_free(){
UI_enum_tables_free();
kt_freeTUI();
} }

View File

@ -8,12 +8,54 @@ extern "C" {
#include "../../kerep/src/Array/Array.h" #include "../../kerep/src/Array/Array.h"
#include "../term/term.h" #include "../term/term.h"
#include "../encoding/encoding.h" #include "../encoding/encoding.h"
#include "Dtsod/tui_dtsod.h"
#include "UIError.h" #include "UIError.h"
//////////////////////////////////////
// Prototypes //
//////////////////////////////////////
/// initializes type descriptors for this project's types typedef struct UIElement UIElement;
/// call this function between kt_beginInit() and kt_endInit() typedef UIElement* UIElement_Ptr;
void kt_initScolteTypes(); typedef struct Renderer Renderer;
typedef struct DrawingArea DrawingArea;
///@return Maybe<UIElement_Ptr>
typedef UI_THROWING_FUNC_DECL( (*UIElement_draw_t)(Renderer* renderer, UIElement_Ptr self, const DrawingArea area) );
///@return Maybe<UIElement_Ptr>
typedef UI_THROWING_FUNC_DECL( (*UIElement_deserialize_t)(Dtsod* dtsod) );
//////////////////////////////////////
// UI Type System //
//////////////////////////////////////
STRUCT(UITDescriptor,
ktDescriptor* type;
UIElement_draw_t draw;
UIElement_deserialize_t deserialize;
)
#define uit_declare(TYPE) extern UITDescriptor UITDescriptor_##TYPE;
#define uit_define(TYPE, FREE_F, TOSTRING_F, DRAW_F, DESERIALIZE_F) kt_define(TYPE, FREE_F, TOSTRING_F) \
UITDescriptor UITDescriptor_##TYPE={ \
.draw=DRAW_F, \
.deserialize=DESERIALIZE_F \
};
/// call this between kt_beginInit() and kt_endInit()
void kt_beginInitTUI();
/// call this between kt_beginInitTUI() and kt_endInitTUI()
void Scolte_init();
/// call this between kt_beginInit() and kt_endInit()
void kt_endInitTUI();
// call this before kt_free()
void Scolte_free();
UITDescriptor* UITDescriptor_getById(ktid i);
UITDescriptor* UITDescriptor_getByName(char* name);
////////////////////////////////////// //////////////////////////////////////
// Enums // // Enums //
@ -27,6 +69,7 @@ PACKED_ENUM(UIBorderThickness,
UiBorder_NoBorder /* no space */ UiBorder_NoBorder /* no space */
) )
////////////////////////////////////// //////////////////////////////////////
// Small structs // // Small structs //
////////////////////////////////////// //////////////////////////////////////
@ -74,34 +117,32 @@ UI_THROWING_FUNC_DECL(Renderer_drawLineX(Renderer* renderer, TermCharInfo tci, u
UI_THROWING_FUNC_DECL(Renderer_drawLineY(Renderer* renderer, TermCharInfo tci, u16 x, u16 y, u16 length)); UI_THROWING_FUNC_DECL(Renderer_drawLineY(Renderer* renderer, TermCharInfo tci, u16 x, u16 y, u16 length));
UI_THROWING_FUNC_DECL(Renderer_drawBorder(Renderer* renderer, UIBorder border, const DrawingArea area)); UI_THROWING_FUNC_DECL(Renderer_drawBorder(Renderer* renderer, UIBorder border, const DrawingArea area));
////////////////////////////////////// //////////////////////////////////////
// UIElement abstract struct // // UIElement abstract struct //
////////////////////////////////////// //////////////////////////////////////
typedef struct UIElement UIElement;
typedef UIElement* UIElement_Ptr;
typedef UI_THROWING_FUNC_DECL((*UIElement_draw_t)(Renderer* renderer, UIElement_Ptr self, const DrawingArea area));
#define UIElement_no_scaling (u16)0
STRUCT(UIElement, STRUCT(UIElement,
ktDescriptor* type; UITDescriptor* ui_type;
char* name;
u16 min_width; u16 min_width;
u16 min_height; u16 min_height;
u16 width_scaling; u16 width_scaling;
u16 height_scaling; u16 height_scaling;
termcolor color; termcolor color;
UIBorder border; UIBorder border;
UIElement_draw_t draw;
) )
Autoarr_declare(UIElement_Ptr)
Array_declare(UIElement)
#define UIElement_no_scaling (u16)0
/// proper way to free UIElement and all its members /// proper way to free UIElement and all its members
void UIElement_destroy(UIElement_Ptr self); void UIElement_destroy(UIElement_Ptr self);
#define UIElement_draw(RENDERER, UIE_PTR, PLACE_RECT) \
((UIElement_Ptr)UIE_PTR)->draw(RENDERER, UIE_PTR, PLACE_RECT)
Autoarr_declare(UIElement_Ptr) #define UIElement_draw(RENDERER, UIE_PTR, PLACE_RECT) \
Array_declare(UIElement) (UIE_PTR)->ui_type->draw(RENDERER, UIE_PTR, PLACE_RECT)
////////////////////////////////////// //////////////////////////////////////
// UIElement derivatives // // UIElement derivatives //
@ -115,11 +156,20 @@ STRUCT(Grid,
u16 rows; u16 rows;
UIElement_Ptr* ui_elements; /* UIElement[rows][columns] */ UIElement_Ptr* ui_elements; /* UIElement[rows][columns] */
) )
uit_declare(Grid);
Grid* Grid_create(u16 columns, u16 rows, UIElement_Ptr* ui_elements); Grid* Grid_create(char* name, u16 columns, u16 rows, UIElement_Ptr* ui_elements);
UIElement_Ptr Grid_get(Grid* grid, u16 column, u16 row); UIElement_Ptr Grid_get(Grid* grid, u16 column, u16 row);
void Grid_set(Grid* grid, u16 column, u16 row, UIElement_Ptr value); void Grid_set(Grid* grid, u16 column, u16 row, UIElement_Ptr 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; } \
} \
} \
}
////// TextBlock ////// ////// TextBlock //////
@ -127,9 +177,10 @@ STRUCT(TextBlock,
UIElement base; UIElement base;
string text; string text;
) )
uit_declare(TextBlock);
/// creates a TextBlock with a copy of text /// creates a TextBlock with a copy of text
TextBlock* TextBlock_create(string text); TextBlock* TextBlock_create(char* name, string text);
#if __cplusplus #if __cplusplus
} }

View File

@ -6,9 +6,12 @@ extern "C" {
#include "tui.h" #include "tui.h"
UIElement __UIElement_createDefault(ktid typeId, UIElement_draw_t drawFunc); extern UIElement _UIElement_initBaseDefault(char* name, UITDescriptor* type);
UI_THROWING_FUNC_DECL(DrawingArea_validate(DrawingArea a));
UI_THROWING_FUNC_DECL(UIElement_validate(UIElement_Ptr u, DrawingArea a)); UI_THROWING_FUNC_DECL( DrawingArea_validate(DrawingArea a) );
UI_THROWING_FUNC_DECL( UIElement_validate(UIElement_Ptr u, DrawingArea a) );
UI_THROWING_FUNC_DECL( UIElement_deserializeBase(Dtsod* dtsod, UIElement* base) );
extern termchar UIBorder_char_h[4]; extern termchar UIBorder_char_h[4];
extern termchar UIBorder_char_v[4]; extern termchar UIBorder_char_v[4];
@ -17,6 +20,31 @@ extern termchar UIBorder_char_rt[4][4];
extern termchar UIBorder_char_rb[4][4]; extern termchar UIBorder_char_rb[4][4];
extern termchar UIBorder_char_lb[4][4]; extern termchar UIBorder_char_lb[4][4];
/// extended kt_register
void __uit_register(ktDescriptor* kt, UITDescriptor* uit);
#define uit_register(TYPE) kt_register(TYPE); __uit_register(&ktDescriptor_##TYPE, &UITDescriptor_##TYPE)
void UI_enum_tables_init();
void UI_enum_tables_free();
int termcolor_getByName(char* color_name);
int UIBorderThickness_getByName(char* name);
#define set_color(name, var){ \
int color=termcolor_getByName(name); \
if(color==-1) \
UI_safethrow(cptr_concat("incorrect color name: ", name), ;); \
var|=(termcolor)color; \
}
#define set_border_thickness(name, var){ \
int th=UIBorderThickness_getByName(name); \
if(th==-1) \
UI_safethrow(cptr_concat("incorrect UIBorderThickness name: ", name), ;); \
var=(UIBorderThickness)th; \
}
#if __cplusplus #if __cplusplus
} }
#endif #endif

View File

@ -1,4 +1,5 @@
#include "TUI/tui.h" #include "TUI/tui.h"
#include "TUI/Dtsod/tui_dtsod.h"
#include "../kerep/src/Filesystem/filesystem.h" #include "../kerep/src/Filesystem/filesystem.h"
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
@ -6,15 +7,36 @@
#include <windows.h> #include <windows.h>
#endif #endif
Maybe tryReadFile(char* filePath){ #define EMBEDDED_RESOURCE_POSTFIX view
if(!file_exists(filePath)) #include "generated/view.h"
kprintf("file doesnt exist, creating new\n");
try(file_open(filePath, FileOpenMode_ReadAppend), _m_file,;); // Maybe tryReadFile(char* filePath){
FileHandle file=_m_file.value.VoidPtr; // if(!file_exists(filePath))
char* fileContent=NULL; // kprintf("file doesnt exist, creating new\n");
try(file_readAll(file, &fileContent), _m_bcount,;) // try(file_open(filePath, FileOpenMode_ReadAppend), _m_file,;);
try(file_close(file),_m_,;); // FileHandle file=_m_file.value.VoidPtr;
return SUCCESS(UniHeapPtr(char,fileContent)); // char* fileContent=NULL;
// try(file_readAll(file, &fileContent), _m_bcount,;)
// try(file_close(file),_m_,;);
// return SUCCESS(UniHeapPtr(char,fileContent));
// }
UI_THROWING_FUNC_DECL(test_example_ui_build());
UI_Maybe test_example_ui_build() {
// ui from dtsod build example
UIDtsodParser* p=UIDtsodParser_create();
for(int i=0; i<EmbeddedResourceFile_table_view_count; i++){
EmbeddedResourceFile rs=EmbeddedResourceFile_table_view[i];
UI_try(UIDtsodParser_parseText(p, rs.path, rs.data),
_91624, UIDtsodParser_destroy(p));
}
UI_try(UIDtsodParser_constructUIContext(p), _m_ui_context, UIDtsodParser_destroy(p));
UIContext* ui_context=_m_ui_context.value.VoidPtr;
UIDtsodParser_destroy(p);
UIContext_get(ui_context, example_namespace, text_box, TextBlock, UIContext_destroy(ui_context));
example_namespace_text_box->text=string_fromCptr("text replaced");
return MaybeNull;
} }
i32 main(const i32 argc, const char* const* argv){ i32 main(const i32 argc, const char* const* argv){
@ -33,7 +55,9 @@ i32 main(const i32 argc, const char* const* argv){
// kerep type system // kerep type system
kt_beginInit(); kt_beginInit();
kt_initKerepTypes(); kt_initKerepTypes();
kt_initScolteTypes(); kt_beginInitTUI();
Scolte_init();
kt_endInitTUI();
kt_endInit(); kt_endInit();
// print command line arguments // print command line arguments
@ -50,10 +74,10 @@ i32 main(const i32 argc, const char* const* argv){
// render ui // render ui
Renderer* renderer=Renderer_create(); Renderer* renderer=Renderer_create();
TextBlock* testTextBlock=TextBlock_create(string_fromCptr("some example text")); TextBlock* testTextBlock=TextBlock_create("TextBlock1", string_fromCptr("some example text"));
Autoarr(UIElement_Ptr)* grid_content=Autoarr_create(UIElement_Ptr, 1, 64); Autoarr(UIElement_Ptr)* grid_content=Autoarr_create(UIElement_Ptr, 1, 64);
Autoarr_add(grid_content, (UIElement_Ptr)testTextBlock); Autoarr_add(grid_content, (UIElement_Ptr)testTextBlock);
Grid* mainGrid=Grid_create(1,1, Autoarr_toArray(grid_content)); Grid* mainGrid=Grid_create("MainGrid", 1,1, Autoarr_toArray(grid_content));
Autoarr_free(grid_content, true); Autoarr_free(grid_content, true);
tryLast(UIElement_draw(renderer, (UIElement_Ptr)mainGrid, ((DrawingArea){.x=10, .y=4, .h=7, .w=24})),_); 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);
@ -63,7 +87,10 @@ i32 main(const i32 argc, const char* const* argv){
// TODO signal( SIGWINCH, redraw ); // TODO signal( SIGWINCH, redraw );
// tryLast(test_example_ui_build(), _6981751);
// exit // exit
Scolte_free();
kt_free(); kt_free();
term_resetColors(); term_resetColors();
term_cursorHide(false); term_cursorHide(false);

17
tasks/build_view.sh Normal file
View File

@ -0,0 +1,17 @@
#!/bin/bash
set -eo pipefail
mkdir -p "obj"
clean_dir "obj/objects"
mkdir -p "src"
mkdir -p "src/generated"
cd resource_embedder
./build.sh
cd ..
embedder_args="-o src/generated/view.h"
for guifile in $(find "view" -name '*.tui.dtsod'); do
embedder_args="$embedder_args -i $guifile"
done
resource_embedder/resource_embedder $embedder_args

View File

@ -1,4 +1,6 @@
#!/usr/bin/bash #!/usr/bin/bash
set -eo pipefail
for tmpfile in $(ls -a | grep -e '\.rebuild.*\.tmp'); do for tmpfile in $(ls -a | grep -e '\.rebuild.*\.tmp'); do
try_delete_dir_or_file "$tmpfile" try_delete_dir_or_file "$tmpfile"
done done
@ -8,3 +10,5 @@ for submodule in kerep utf8proc; do
make clean make clean
cd .. cd ..
done done
try_delete_dir_or_file src/generated

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
set -eo pipefail
# if $lib_project.a doesn't exist or rebuild_* task was executed, builds static lib # if $lib_project.a doesn't exist or rebuild_* task was executed, builds static lib
function handle_static_dependency { function handle_static_dependency {
@ -23,3 +24,5 @@ function handle_static_dependency {
handle_static_dependency kerep "$DEPS_BUILD_TASK" kerep/bin/kerep.a handle_static_dependency kerep "$DEPS_BUILD_TASK" kerep/bin/kerep.a
handle_static_dependency utf8proc libutf8proc.a utf8proc/libutf8proc.a handle_static_dependency utf8proc libutf8proc.a utf8proc/libutf8proc.a
source tasks/build_view.sh

View File

@ -1,4 +1,6 @@
#!/bin/bash #!/bin/bash
set -eo pipefail
source cbuild/colors.sh source cbuild/colors.sh
source cbuild/functions.sh source cbuild/functions.sh
touch ".rebuild_$1.tmp" touch ".rebuild_$1.tmp"

31
view/example.tui.dtsod Normal file
View File

@ -0,0 +1,31 @@
TUI_version: 1;
namespace: "example_namespace"
$ui: {
name: "UIElement",
type: "UIElement",
min_width: 4,
max_width: 4,
width_scaling: 0,
height_scaling: 0,
bg_color: "black",
fg_color: "white",
border: {
top: "hidden",
bottom: "no",
left: "thick",
right: "double",
fg_color: "dark_cyan",
bg_color: "gray";
};
};
$ui: [
name: "Grid",
type: "Grid",
content: [
// column
[ "@UIElement" ], // row
[ "@namespace.element1" ], // row
];
];

10
view/main.tui.dtsod Normal file
View File

@ -0,0 +1,10 @@
TUI_version: 1;
namespace: "main";
grid: [
name: "Grid",
type: "Grid",
// column
[ "@namespace.element0" ], // row
[ "@namespace.element1" ], // row
];