began implementing structure functions

This commit is contained in:
Timerix22 2023-02-15 21:54:42 +06:00
parent 167785a410
commit 54ba772395
8 changed files with 203 additions and 37 deletions

2
.vscode/launch.json vendored
View File

@ -5,7 +5,7 @@
"name": "(gdb) Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/scolte.exe",
"program": "${workspaceFolder}/bin/scolte",
"preLaunchTask": "build_exec_dbg",
"stopAtEntry": false,
"cwd": "${workspaceFolder}/bin",

2
kerep

@ -1 +1 @@
Subproject commit dbf2c2772c832bbb6f8f56b9741a21722daa01a2
Subproject commit 5391862d029e64fa41c888a0db3ee83979a3954f

31
src/Canvas.c Normal file
View File

@ -0,0 +1,31 @@
#include "tui.h"
void Canvas_freeMembers(void* _self){
Canvas* self=(Canvas*)_self;
Autoarr_foreach(self->children, ch,
UIElement_destroy((UIElement*)ch));
Autoarr_freeWithoutMembers(self->children, true);
}
void Canvas_draw(UIElement* _self, Rect place){
Canvas* self=(Canvas*)_self;
Autoarr_foreach(self->children, ch, ({
UIElement_draw((UIElement*)ch,place);
fputc('\n', stdout);
}));
}
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){
if (child == NULL)
throw(ERR_NULLPTR);
Autoarr_add(self->children, child);
}

21
src/TextBlock.c Normal file
View File

@ -0,0 +1,21 @@
#include "tui.h"
void TextBlock_freeMembers(void* _self){
TextBlock* self=(TextBlock*)_self;
free(self->text.ptr);
}
void TextBlock_draw(UIElement* _self, Rect place){
TextBlock* self=(TextBlock*)_self;
fputs(self->text.ptr, stdout);
}
kt_define(TextBlock, TextBlock_freeMembers, NULL);
TextBlock* TextBlock_create(string text){
TextBlock* TextBlock=malloc(sizeof(TextBlock));
TextBlock->base=__UIElement_createDefault(ktid_name(TextBlock), TextBlock_draw);
TextBlock->text=string_copy(text);
return TextBlock;
}

29
src/UIElement.c Normal file
View File

@ -0,0 +1,29 @@
#include "tui.h"
kt_define(UIElement, NULL, NULL);
// ktid is needed to check if it is uninitilized by calling ktDescriptor_get
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,
.borders={
.left=UIBorder_Thin, .right=UIBorder_Thin,
.top=UIBorder_Thick, .bottom=UIBorder_Thin
},
.draw=drawFunc
};
}
void UIElement_destroy(UIElement* self){
if(self->type->freeMembers)
self->type->freeMembers(self);
free(self);
}

View File

@ -1,17 +1,38 @@
#include "../kerep/src/Filesystem/filesystem.h"
#include "tui.h"
Maybe tryReadFile(char* filePath){
if(!file_exists(filePath))
kprintf("file doesnt exist, creating new\n");
try(file_open(filePath, FileOpenMode_ReadAppend), _m_file,;);
File* file=_m_file.value.VoidPtr;
char* fileContent=NULL;
try(file_readAll(file, &fileContent), _m_bcount,;)
try(file_close(file),_m_,;);
return SUCCESS(UniHeapPtr(char,fileContent));
}
i32 main(const i32 argc, const char* const* argv){
kt_beginInit();
kt_initKerepTypes();
kt_initScolteTypes();
kt_endInit();
kprintf("\e[37margs:");
for(i32 i=0; i<argc; i++)
kprintf(" %s", argv[i]);
kprintf("\n");
if(argc==1)
return 0;
char* filePath=argv[argc-1];
if(!file_exists(filePath))
kprintf("file doesnt exist, creating new\n");
tryLast(file_open(filePath, FileOpenMode_ReadWrite), _m_file);
File* file=_m_file.value.VoidPtr;
tryLast(file_close(file),_m);
/*char* filePath= argc>1 ? argv[argc-1] : "new_file.txt";
tryLast(tryReadFile(filePath), _m_text);
char* text=(char*)_m_text.value.VoidPtr;
fputs(text,stdout);*/
Canvas* mainCanvas=Canvas_create();
TextBlock* testTextBlock=TextBlock_create(string_fromCptr("some example text"));
Canvas_addChild(mainCanvas, (UIElement*)testTextBlock);
UIElement_draw(mainCanvas, ((Rect){.x=-1, .y=-1, .h=-1, .w=-1}));
UIElement_destroy((UIElement*)mainCanvas);
kt_free();
return 0;
}

View File

@ -1,3 +1,12 @@
#include "tui.h"
kt_define(Rect, NULL, NULL);
kt_define(UIBorderParams, NULL, NULL);
void kt_initScolteTypes(){
kt_register(Rect);
kt_register(UIBorderParams);
kt_register(UIElement);
kt_register(Canvas);
kt_register(TextBlock);
}

109
src/tui.h
View File

@ -1,36 +1,52 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../kerep/src/base/base.h"
#include "../kerep/src/String/string.h"
#include "../kerep/src/kprint/kprint_colors.h"
#include "../kerep/src/Array/Array.h"
/// initializes type descriptors for this project's types
/// call this function between kt_beginInit() and kt_endInit()
void kt_initScolteTypes();
//////////////////////////////////////
// Enums //
//////////////////////////////////////
PACKED_ENUM(UIAnchor,
UI_Center=0,
UI_Left=1,
UI_Right=2,
UI_Top=4,
UI_Bottom=8,
UI_RightTop=UI_Right|UI_Top,
UI_RightBottom=UI_Right|UI_Bottom,
UI_LeftTop=UI_Left|UI_Top,
UI_LeftBottom=UI_Left|UI_Bottom
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
)
typedef struct UIElement;
#define UIElement_stretch (u16)-1
STRUCT(Rect, // Rectangle
// right-top corner
u16 x; u16 y;
u16 w; u16 h;
)
typedef void (*UIElement_draw_t)(UIElement,Rect);
PACKED_ENUM(UIBorderType,
UIBorderType_NoBorder,
UIBorderType_Thin,
UIBorderType_Thick,
UIBorderType_Double
UIBorder_NoBorder,
UIBorder_Thin,
UIBorder_Thick,
UIBorder_Double
)
//////////////////////////////////////
// Small structs //
//////////////////////////////////////
/// Rectangle
STRUCT(Rect,
/* right-top corner */
u16 x; u16 y;
u16 w; u16 h;
)
STRUCT(UIBorderParams,
@ -40,7 +56,18 @@ STRUCT(UIBorderParams,
UIBorderType bottom;
)
//////////////////////////////////////
// UIElement abstract struct //
//////////////////////////////////////
typedef struct UIElement UIElement;
typedef UIElement* UIElementPtr;
typedef void (*UIElement_draw_t)(UIElement* self,Rect place);
#define UIElement_stretch (u16)-1
STRUCT(UIElement,
ktDescriptor* type;
u16 min_width;
u16 max_width;
u16 min_height;
@ -52,9 +79,37 @@ STRUCT(UIElement,
UIElement_draw_t draw;
)
STRUCT(TextBox,
UIElement __UIElement_createDefault(ktid typeId, UIElement_draw_t drawFunc);
/// proper way to free UIElement and all its members
void UIElement_destroy(UIElement* self);
#define UIElement_draw(UIE_PTR, PLACE_RECT) ((UIElement*)UIE_PTR)->draw((UIElement*)UIE_PTR, PLACE_RECT)
//////////////////////////////////////
// UIElement derivatives //
//////////////////////////////////////
////// Canvas //////
STRUCT(Canvas,
UIElement base;
/* Autoarr(UIElementPtr) */
Autoarr(Pointer)* children;
)
Canvas* Canvas_create();
void Canvas_addChild(Canvas* self, UIElement* child);
////// TextBlock //////
STRUCT(TextBlock,
UIElement base;
string text;
)
void UIElement_draw(UIElement uie);
/// creates a TextBlock with a copy of text
TextBlock* TextBlock_create(string text);
#if __cplusplus
}
#endif