Loading...
Searching...
No Matches
view.c
Go to the documentation of this file.
#include "arc/console/view.h"
#include "key.h"
#include "arc/console/element.h"
#include "arc/std/bool.h"
#include "arc/std/errno.h"
#include "arc/std/vector.h"
#include "arc/std/string.h"
#include <locale.h>
#include <ncurses.h>
#include <stdlib.h>
uint8_t arc_ncurses_win_size = 0;
struct ARC_ConsoleView {
WINDOW *window;
ARC_Rect bounds;
ARC_Bool echo;
ARC_Vector *elements;
};
void ARC_ConsoleView_Create(ARC_ConsoleView **view, ARC_Rect bounds){
if(arc_ncurses_win_size == ~(uint8_t)0){
arc_errno = ARC_ERRNO_OVERFLOW;
ARC_DEBUG_ERR("ARC_NCurses_Create(ncurses), max num of ARC_NCurses have been created, consider making arc_ncurses_win_size a uint32_t to increase the max");
*view = NULL;
return;
}
//if this is the first ncurses, init ncurses
if(arc_ncurses_win_size == 0){
setlocale(LC_ALL, "");
initscr();
//start_color();
cbreak();
keypad(stdscr, TRUE);
refresh();
}
*view = (ARC_ConsoleView *)malloc(sizeof(ARC_ConsoleView));
ARC_Rect viewBounds = { 0, 0, COLS, LINES };
if(bounds.w != 0 && bounds.h != 0){
viewBounds = bounds;
}
(*view)->window = newwin(viewBounds.h, viewBounds.w, viewBounds.y, viewBounds.x);
(*view)->bounds = viewBounds;
keypad((*view)->window, TRUE);
noecho();
(*view)->echo = false;
ARC_Vector_Create(&(*view)->elements);
wrefresh((*view)->window);
arc_ncurses_win_size++;
}
void ARC_ConsoleView_Destroy(ARC_ConsoleView *ncurses){
arc_ncurses_win_size--;
ARC_Vector_Destroy(ncurses->elements);
delwin(ncurses->window);
free(ncurses);
if(arc_ncurses_win_size == 0){
endwin();
}
}
void ARC_ConsoleView_AddElement(ARC_ConsoleView *view, ARC_ConsoleElement *element){
ARC_Vector_Add(view->elements, (void *)element);
}
void ARC_ConsoleView_RemoveElement(ARC_ConsoleView *view, uint32_t index){
ARC_Vector_RemoveIndex(view->elements, index);
}
void ARC_ConsoleView_Clear(ARC_ConsoleView *view){
wclear(view->window);
wrefresh(view->window);
}
void ARC_ConsoleView_RenderCharAt(ARC_ConsoleView *view, char character, ARC_Point pos){
mvwprintw(view->window, pos.y, pos.x, "%c", character);
wrefresh(view->window);
}
void ARC_ConsoleView_RenderWCharAt(ARC_ConsoleView *view, wchar_t character, ARC_Point pos){
mvwprintw(view->window, pos.y, pos.x, "%lc", character);
wrefresh(view->window);
}
void ARC_ConsoleView_RenderKeyAt(ARC_ConsoleView *view, ARC_ConsoleKey key, ARC_Point pos){
mvwprintw(view->window, pos.y, pos.x, "%c", (char)key.key);
wrefresh(view->window);
}
void ARC_ConsoleView_RenderUint32At(ARC_ConsoleView *view, uint32_t uint32, ARC_Point pos){
mvwprintw(view->window, pos.y, pos.x, "%d", uint32);
wrefresh(view->window);
}
void ARC_ConsoleView_RenderStringAt(ARC_ConsoleView *view, ARC_String *text, ARC_Point pos){
mvwprintw(view->window, pos.y, pos.x, "%s", text->data);
wrefresh(view->window);
}
void ARC_ConsoleView_RenderCStringWithStrlenAt(ARC_ConsoleView *view, char *cstr, ARC_Point pos){
mvwprintw(view->window, pos.y, pos.x, "%s", cstr);
wrefresh(view->window);
}
void ARC_ConsoleView_RenderRect(ARC_ConsoleView *view, ARC_Rect bounds){
//render corners
ARC_ConsoleView_RenderWCharAt(view, L'┌', (ARC_Point){ bounds.x, bounds.y });
ARC_ConsoleView_RenderWCharAt(view, L'└', (ARC_Point){ bounds.x, (bounds.h - 1) + bounds.y });
ARC_ConsoleView_RenderWCharAt(view, L'┐', (ARC_Point){ (bounds.w - 1) + bounds.x, bounds.y });
ARC_ConsoleView_RenderWCharAt(view, L'┘', (ARC_Point){ (bounds.w - 1) + bounds.x, (bounds.h - 1) + bounds.y });
//render virticle lines
for(int32_t x = 1; x < bounds.w - 1; x++){
ARC_ConsoleView_RenderWCharAt(view, L'─', (ARC_Point){ bounds.x + x, bounds.y });
ARC_ConsoleView_RenderWCharAt(view, L'─', (ARC_Point){ bounds.x + x, (bounds.h - 1) + bounds.y });
}
//render horizontal lines
for(int32_t y = 1; y < bounds.h - 1; y++){
ARC_ConsoleView_RenderWCharAt(view, L'│', (ARC_Point){ bounds.x, bounds.y + y });
ARC_ConsoleView_RenderWCharAt(view, L'│', (ARC_Point){ (bounds.w - 1) + bounds.x, bounds.y + y });
}
}
void ARC_ConsoleView_RenderElements(ARC_ConsoleView *view){
for(uint32_t i = 0; i < ARC_Vector_Size(view->elements); i++){
ARC_ConsoleElement *element = (ARC_ConsoleElement *)ARC_Vector_Get(view->elements, i);
element->renderFn(view, element);
wrefresh(view->window);
}
}
ARC_Rect ARC_ConsoleView_GetBounds(ARC_ConsoleView *view){
return view->bounds;
}
ARC_ConsoleElement *ARC_ConsoleView_GetElement(ARC_ConsoleView *view, uint32_t index){
return (ARC_ConsoleElement *)ARC_Vector_Get(view->elements, index);
}
char ARC_ConsoleView_GetChar(ARC_ConsoleView *view){
return wgetch(view->window);
}
char ARC_ConsoleView_GetCharAt(ARC_ConsoleView *view, ARC_Point pos){
return mvwgetch(view->window, pos.y, pos.x);
}
ARC_ConsoleKey ARC_ConsoleView_GetConsoleKeyAt(ARC_ConsoleView *view, ARC_Point pos){
return (ARC_ConsoleKey){ mvwgetch(view->window, pos.y, pos.x) };
}
ARC_ConsoleKey *ARC_ConsoleView_GetCreateConsoleKeyAt(ARC_ConsoleView *view, ARC_Point pos){
ARC_ConsoleKey *key;
ARC_ConsoleKey_Create(&key, NULL);
key->key = mvwgetch(view->window, pos.y, pos.x);
return key;
}
ARC_String *ARC_ConsoleView_GetStringInput(ARC_ConsoleView *view, ARC_Point pos, ARC_ConsoleView_OverrideCharInputFn *overrideCharInputFn, void *userdata){
noecho();
uint32_t cstringSize = view->bounds.w - pos.x;
char cstring[view->bounds.w - pos.x];
ARC_ConsoleKey temp = ARC_ConsoleView_GetConsoleKeyAt(view, pos);
uint32_t index = 0;
while(temp.key != '\n'){
//store the last size to be able to clear efficeintly
uint32_t lastSize = index;
//if override function exists and it overrode the current char
if(overrideCharInputFn != NULL && (*overrideCharInputFn)(&temp, cstring, &index, cstringSize, userdata)){
for(uint32_t i = 0; i < lastSize; i++){
ARC_ConsoleView_RenderCharAt(view, ' ', (ARC_Point){ pos.x + i, pos.y });
}
for(uint32_t i = 0; i < index; i++){
ARC_ConsoleView_RenderCharAt(view, cstring[i], (ARC_Point){ pos.x + i, pos.y });
}
temp = ARC_ConsoleView_GetConsoleKeyAt(view, (ARC_Point){ pos.x + index, pos.y });
continue;
}
if(temp.key == KEY_BACKSPACE || temp.key == KEY_DC || temp.key == 127){
if(index == 0){
temp = ARC_ConsoleView_GetConsoleKeyAt(view, (ARC_Point){ pos.x + index, pos.y });
continue;
}
index--;
ARC_ConsoleView_RenderCharAt(view, ' ', (ARC_Point){ pos.x + index, pos.y });
cstring[index] = '\0';
temp = ARC_ConsoleView_GetConsoleKeyAt(view, (ARC_Point){ pos.x + index, pos.y });
continue;
}
if(index < (view->bounds.w - 1) - pos.x){
ARC_ConsoleView_RenderCharAt(view, (char)(temp.key), (ARC_Point){ pos.x + index, pos.y });
cstring[index] = (char)(temp.key);
index++;
}
temp = ARC_ConsoleView_GetConsoleKeyAt(view, (ARC_Point){ pos.x + index, pos.y });
}
if(view->echo){
echo();
}
if(index == 0){
return NULL;
}
ARC_String *string;
ARC_String_Create(&string, cstring, index);
return string;
}
void ARC_ConsoleView_SetCursorVisibility(ARC_ConsoleView *view, uint8_t visibility){
switch(visibility){
case ARC_CONSOLE_VIEW_CURSOR_HIDDEN:
curs_set(0);
break;
case ARC_CONSOLE_VIEW_CURSOR_VISIBLE:
curs_set(1);
break;
}
}
void ARC_ConsoleView_SetBorder(ARC_ConsoleView *view, uint32_t border){
switch(border){
case ARC_CONSOLE_VIEW_BORDER_NONE:
wborder(view->window, ' ', ' ', ' ',' ',' ',' ',' ',' ');
break;
case ARC_CONSOLE_VIEW_BORDER_DEFAULT:
box(view->window, 0, 0);
break;
}
wrefresh(view->window);
}
void ARC_ConsoleView_SetAttribute(ARC_ConsoleView *view, uint32_t attribute){
switch(attribute){
case ARC_CONSOLE_VIEW_ATTRIBUTE_NONE:
wattroff(view->window, A_REVERSE);
break;
case ARC_CONSOLE_VIEW_ATTRIBUTE_REVERSE:
wattron(view->window, A_REVERSE);
break;
}
}
bool.h
ARC_Bool
#define ARC_Bool
Definition bool.h:10
ARC_ConsoleView_RenderStringAt
void ARC_ConsoleView_RenderStringAt(ARC_ConsoleView *view, ARC_String *text, ARC_Point pos)
Definition view.c:109
ARC_ConsoleView_GetChar
char ARC_ConsoleView_GetChar(ARC_ConsoleView *view)
gets a char from the view
Definition view.c:155
arc_ncurses_win_size
uint8_t arc_ncurses_win_size
Definition view.c:13
ARC_ConsoleView_RenderCStringWithStrlenAt
void ARC_ConsoleView_RenderCStringWithStrlenAt(ARC_ConsoleView *view, char *cstr, ARC_Point pos)
Definition view.c:114
ARC_ConsoleView_GetStringInput
ARC_String * ARC_ConsoleView_GetStringInput(ARC_ConsoleView *view, ARC_Point pos, ARC_ConsoleView_OverrideCharInputFn *overrideCharInputFn, void *userdata)
gets a ARC_String from the view at a position
Definition view.c:174
ARC_ConsoleView_RenderElements
void ARC_ConsoleView_RenderElements(ARC_ConsoleView *view)
Definition view.c:139
ARC_ConsoleView_RenderKeyAt
void ARC_ConsoleView_RenderKeyAt(ARC_ConsoleView *view, ARC_ConsoleKey key, ARC_Point pos)
Definition view.c:99
ARC_ConsoleView_GetBounds
ARC_Rect ARC_ConsoleView_GetBounds(ARC_ConsoleView *view)
gets the bounds of an ARC_ConsoleView
Definition view.c:147
ARC_ConsoleView_RenderUint32At
void ARC_ConsoleView_RenderUint32At(ARC_ConsoleView *view, uint32_t uint32, ARC_Point pos)
Definition view.c:104
ARC_ConsoleView_Destroy
void ARC_ConsoleView_Destroy(ARC_ConsoleView *ncurses)
destroys ARC_ConsoleView type
Definition view.c:63
ARC_ConsoleView_Create
void ARC_ConsoleView_Create(ARC_ConsoleView **view, ARC_Rect bounds)
creates ARC_ConsoleView type
Definition view.c:23
ARC_ConsoleView_RenderCharAt
void ARC_ConsoleView_RenderCharAt(ARC_ConsoleView *view, char character, ARC_Point pos)
Definition view.c:89
ARC_ConsoleView_GetConsoleKeyAt
ARC_ConsoleKey ARC_ConsoleView_GetConsoleKeyAt(ARC_ConsoleView *view, ARC_Point pos)
gets a console key from the view at a position
Definition view.c:163
ARC_ConsoleView_SetBorder
void ARC_ConsoleView_SetBorder(ARC_ConsoleView *view, uint32_t border)
sets a border on the ARC_ConsoleView
Definition view.c:246
ARC_ConsoleView_RenderWCharAt
void ARC_ConsoleView_RenderWCharAt(ARC_ConsoleView *view, wchar_t character, ARC_Point pos)
Definition view.c:94
ARC_ConsoleView_GetCreateConsoleKeyAt
ARC_ConsoleKey * ARC_ConsoleView_GetCreateConsoleKeyAt(ARC_ConsoleView *view, ARC_Point pos)
gets and creates a console key from the view at a position
Definition view.c:167
ARC_ConsoleView_RemoveElement
void ARC_ConsoleView_RemoveElement(ARC_ConsoleView *view, uint32_t index)
Definition view.c:80
ARC_ConsoleView_GetCharAt
char ARC_ConsoleView_GetCharAt(ARC_ConsoleView *view, ARC_Point pos)
gets a char from the view at a position
Definition view.c:159
ARC_ConsoleView_SetCursorVisibility
void ARC_ConsoleView_SetCursorVisibility(ARC_ConsoleView *view, uint8_t visibility)
sets a visibility of the cursor with an ARC_ConsoleView
Definition view.c:235
ARC_ConsoleView_AddElement
void ARC_ConsoleView_AddElement(ARC_ConsoleView *view, ARC_ConsoleElement *element)
adds an ARC_ConsoleElement to the ARC_ConsoleView type
Definition view.c:76
ARC_ConsoleView_GetElement
ARC_ConsoleElement * ARC_ConsoleView_GetElement(ARC_ConsoleView *view, uint32_t index)
adds an ARC_ConsoleElement to the ARC_ConsoleView type
Definition view.c:151
ARC_ConsoleView_SetAttribute
void ARC_ConsoleView_SetAttribute(ARC_ConsoleView *view, uint32_t attribute)
sets a border on the ARC_ConsoleView
Definition view.c:259
ARC_ConsoleView_RenderRect
void ARC_ConsoleView_RenderRect(ARC_ConsoleView *view, ARC_Rect bounds)
Definition view.c:119
ARC_ConsoleView_Clear
void ARC_ConsoleView_Clear(ARC_ConsoleView *view)
Definition view.c:84
view.h
ARC_CONSOLE_VIEW_BORDER_NONE
#define ARC_CONSOLE_VIEW_BORDER_NONE
border options
Definition view.h:238
ARC_CONSOLE_VIEW_CURSOR_HIDDEN
#define ARC_CONSOLE_VIEW_CURSOR_HIDDEN
mouse options
Definition view.h:224
ARC_CONSOLE_VIEW_CURSOR_VISIBLE
#define ARC_CONSOLE_VIEW_CURSOR_VISIBLE
Definition view.h:225
ARC_CONSOLE_VIEW_BORDER_DEFAULT
#define ARC_CONSOLE_VIEW_BORDER_DEFAULT
Definition view.h:239
ARC_CONSOLE_VIEW_ATTRIBUTE_REVERSE
#define ARC_CONSOLE_VIEW_ATTRIBUTE_REVERSE
Definition view.h:253
ARC_CONSOLE_VIEW_ATTRIBUTE_NONE
#define ARC_CONSOLE_VIEW_ATTRIBUTE_NONE
border options
Definition view.h:252
ARC_ConsoleView_OverrideCharInputFn
ARC_Bool(* ARC_ConsoleView_OverrideCharInputFn)(ARC_ConsoleKey *key, char *inputCStr, uint32_t *inputSize, uint32_t maxInputSize, void *userdata)
callback to check char being read in and override functionality
Definition view.h:209
element.h
errno.h
arc_errno
int32_t arc_errno
Definition errno.c:5
ARC_ERRNO_OVERFLOW
#define ARC_ERRNO_OVERFLOW
Definition errno.h:10
ARC_ConsoleKey_Create
void ARC_ConsoleKey_Create(ARC_ConsoleKey **consoleKey, ARC_ConsoleKey_Key *key)
Definition key.c:7
key.h
string.h
ARC_String_Create
void ARC_String_Create(ARC_String **string, char *data, uint64_t length)
creates ARC_String type
Definition string.c:9
ARC_ConsoleElement
Definition element.h:27
ARC_ConsoleElement::renderFn
ARC_ConsoleElement_RenderFn renderFn
Definition element.h:34
ARC_ConsoleKeyType
Definition key.h:11
ARC_ConsoleKeyType::key
int32_t key
Definition key.h:12
ARC_ConsoleView
Definition view.c:15
ARC_ConsoleView::elements
ARC_Vector * elements
Definition view.c:20
ARC_ConsoleView::echo
ARC_Bool echo
Definition view.c:18
ARC_ConsoleView::bounds
ARC_Rect bounds
Definition view.c:17
ARC_ConsoleView::window
WINDOW * window
Definition view.c:16
ARC_Point
Definition point.h:10
ARC_Point::y
int32_t y
Definition point.h:12
ARC_Point::x
int32_t x
Definition point.h:11
ARC_Rect
Definition rectangle.h:12
ARC_Rect::x
int32_t x
Definition rectangle.h:13
ARC_Rect::w
int32_t w
Definition rectangle.h:15
ARC_Rect::y
int32_t y
Definition rectangle.h:14
ARC_Rect::h
int32_t h
Definition rectangle.h:16
ARC_String
substring position within a string
Definition string.h:14
ARC_String::data
char * data
Definition string.h:15
ARC_Vector
a private dynamic array type
Definition vector.c:12
vector.h
ARC_Vector_Get
void * ARC_Vector_Get(ARC_Vector *vector, uint32_t index)
gets an item from an ARC_Vector at a position index
Definition vector.c:153
ARC_Vector_Add
void ARC_Vector_Add(ARC_Vector *vector, void *data)
adds an item to an ARC_Vector
Definition vector.c:70
ARC_Vector_Destroy
void ARC_Vector_Destroy(ARC_Vector *vector)
destroys an ARC_Vector
Definition vector.c:54
ARC_Vector_RemoveIndex
void ARC_Vector_RemoveIndex(ARC_Vector *vector, uint32_t index)
removes an item from an ARC_Vector at an index
Definition vector.c:110
ARC_Vector_Create
void ARC_Vector_Create(ARC_Vector **vector, ARC_Vector_CompareDataFn *compareDataFn, ARC_Vector_DestroyDataFn *destroyDataFn)
creates an ARC_Vector which is an "expandable" array
Definition vector.c:31