From 54ba7723952c39df82dbad2347e35e086a5a3ab0 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Wed, 15 Feb 2023 21:54:42 +0600 Subject: [PATCH] began implementing structure functions --- .vscode/launch.json | 2 +- kerep | 2 +- src/Canvas.c | 31 +++++++++++++ src/TextBlock.c | 21 +++++++++ src/UIElement.c | 29 ++++++++++++ src/main.c | 37 +++++++++++---- src/tui.c | 9 ++++ src/tui.h | 109 +++++++++++++++++++++++++++++++++----------- 8 files changed, 203 insertions(+), 37 deletions(-) create mode 100644 src/Canvas.c create mode 100644 src/TextBlock.c create mode 100644 src/UIElement.c diff --git a/.vscode/launch.json b/.vscode/launch.json index e1515ba..8c85847 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -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", diff --git a/kerep b/kerep index dbf2c27..5391862 160000 --- a/kerep +++ b/kerep @@ -1 +1 @@ -Subproject commit dbf2c2772c832bbb6f8f56b9741a21722daa01a2 +Subproject commit 5391862d029e64fa41c888a0db3ee83979a3954f diff --git a/src/Canvas.c b/src/Canvas.c new file mode 100644 index 0000000..d236c43 --- /dev/null +++ b/src/Canvas.c @@ -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); +} diff --git a/src/TextBlock.c b/src/TextBlock.c new file mode 100644 index 0000000..b0c15cd --- /dev/null +++ b/src/TextBlock.c @@ -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; +} diff --git a/src/UIElement.c b/src/UIElement.c new file mode 100644 index 0000000..e2a6a6c --- /dev/null +++ b/src/UIElement.c @@ -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); +} diff --git a/src/main.c b/src/main.c index 8549f91..a48cbd7 100644 --- a/src/main.c +++ b/src/main.c @@ -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; i1 ? 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; } diff --git a/src/tui.c b/src/tui.c index e4c60b4..58c5471 100644 --- a/src/tui.c +++ b/src/tui.c @@ -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); +} diff --git a/src/tui.h b/src/tui.h index 5d15d07..8f247cb 100644 --- a/src/tui.h +++ b/src/tui.h @@ -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