began implementing structure functions
This commit is contained in:
parent
167785a410
commit
54ba772395
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@ -5,7 +5,7 @@
|
|||||||
"name": "(gdb) Debug",
|
"name": "(gdb) Debug",
|
||||||
"type": "cppdbg",
|
"type": "cppdbg",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${workspaceFolder}/bin/scolte.exe",
|
"program": "${workspaceFolder}/bin/scolte",
|
||||||
"preLaunchTask": "build_exec_dbg",
|
"preLaunchTask": "build_exec_dbg",
|
||||||
"stopAtEntry": false,
|
"stopAtEntry": false,
|
||||||
"cwd": "${workspaceFolder}/bin",
|
"cwd": "${workspaceFolder}/bin",
|
||||||
|
|||||||
2
kerep
2
kerep
@ -1 +1 @@
|
|||||||
Subproject commit dbf2c2772c832bbb6f8f56b9741a21722daa01a2
|
Subproject commit 5391862d029e64fa41c888a0db3ee83979a3954f
|
||||||
31
src/Canvas.c
Normal file
31
src/Canvas.c
Normal 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
21
src/TextBlock.c
Normal 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
29
src/UIElement.c
Normal 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);
|
||||||
|
}
|
||||||
37
src/main.c
37
src/main.c
@ -1,17 +1,38 @@
|
|||||||
#include "../kerep/src/Filesystem/filesystem.h"
|
#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){
|
i32 main(const i32 argc, const char* const* argv){
|
||||||
|
kt_beginInit();
|
||||||
|
kt_initKerepTypes();
|
||||||
|
kt_initScolteTypes();
|
||||||
|
kt_endInit();
|
||||||
|
|
||||||
kprintf("\e[37margs:");
|
kprintf("\e[37margs:");
|
||||||
for(i32 i=0; i<argc; i++)
|
for(i32 i=0; i<argc; i++)
|
||||||
kprintf(" %s", argv[i]);
|
kprintf(" %s", argv[i]);
|
||||||
kprintf("\n");
|
kprintf("\n");
|
||||||
if(argc==1)
|
|
||||||
return 0;
|
/*char* filePath= argc>1 ? argv[argc-1] : "new_file.txt";
|
||||||
char* filePath=argv[argc-1];
|
tryLast(tryReadFile(filePath), _m_text);
|
||||||
if(!file_exists(filePath))
|
char* text=(char*)_m_text.value.VoidPtr;
|
||||||
kprintf("file doesnt exist, creating new\n");
|
fputs(text,stdout);*/
|
||||||
tryLast(file_open(filePath, FileOpenMode_ReadWrite), _m_file);
|
|
||||||
File* file=_m_file.value.VoidPtr;
|
Canvas* mainCanvas=Canvas_create();
|
||||||
tryLast(file_close(file),_m);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,12 @@
|
|||||||
#include "tui.h"
|
#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
109
src/tui.h
@ -1,36 +1,52 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "../kerep/src/base/base.h"
|
#include "../kerep/src/base/base.h"
|
||||||
#include "../kerep/src/String/string.h"
|
#include "../kerep/src/String/string.h"
|
||||||
#include "../kerep/src/kprint/kprint_colors.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,
|
PACKED_ENUM(UIAnchor,
|
||||||
UI_Center=0,
|
UIAnchor_Center=0,
|
||||||
UI_Left=1,
|
UIAnchor_Left=1,
|
||||||
UI_Right=2,
|
UIAnchor_Right=2,
|
||||||
UI_Top=4,
|
UIAnchor_Top=4,
|
||||||
UI_Bottom=8,
|
UIAnchor_Bottom=8,
|
||||||
UI_RightTop=UI_Right|UI_Top,
|
UIAnchor_RightTop=UIAnchor_Right|UIAnchor_Top,
|
||||||
UI_RightBottom=UI_Right|UI_Bottom,
|
UIAnchor_RightBottom=UIAnchor_Right|UIAnchor_Bottom,
|
||||||
UI_LeftTop=UI_Left|UI_Top,
|
UIAnchor_LeftTop=UIAnchor_Left|UIAnchor_Top,
|
||||||
UI_LeftBottom=UI_Left|UI_Bottom
|
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,
|
PACKED_ENUM(UIBorderType,
|
||||||
UIBorderType_NoBorder,
|
UIBorder_NoBorder,
|
||||||
UIBorderType_Thin,
|
UIBorder_Thin,
|
||||||
UIBorderType_Thick,
|
UIBorder_Thick,
|
||||||
UIBorderType_Double
|
UIBorder_Double
|
||||||
|
)
|
||||||
|
|
||||||
|
//////////////////////////////////////
|
||||||
|
// Small structs //
|
||||||
|
//////////////////////////////////////
|
||||||
|
|
||||||
|
/// Rectangle
|
||||||
|
STRUCT(Rect,
|
||||||
|
/* right-top corner */
|
||||||
|
u16 x; u16 y;
|
||||||
|
u16 w; u16 h;
|
||||||
)
|
)
|
||||||
|
|
||||||
STRUCT(UIBorderParams,
|
STRUCT(UIBorderParams,
|
||||||
@ -40,7 +56,18 @@ STRUCT(UIBorderParams,
|
|||||||
UIBorderType bottom;
|
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,
|
STRUCT(UIElement,
|
||||||
|
ktDescriptor* type;
|
||||||
u16 min_width;
|
u16 min_width;
|
||||||
u16 max_width;
|
u16 max_width;
|
||||||
u16 min_height;
|
u16 min_height;
|
||||||
@ -52,9 +79,37 @@ STRUCT(UIElement,
|
|||||||
UIElement_draw_t draw;
|
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;
|
UIElement base;
|
||||||
string text;
|
string text;
|
||||||
)
|
)
|
||||||
|
|
||||||
void UIElement_draw(UIElement uie);
|
/// creates a TextBlock with a copy of text
|
||||||
|
TextBlock* TextBlock_create(string text);
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user