diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 74 | ||||
-rw-r--r-- | src/ui/color.c | 11 | ||||
-rw-r--r-- | src/ui/color.h | 26 | ||||
-rw-r--r-- | src/ui/tui.c | 47 | ||||
-rw-r--r-- | src/ui/tui.h | 13 |
5 files changed, 80 insertions, 91 deletions
@@ -2,75 +2,35 @@ #include <stdio.h> #include <unistd.h> +#include "ui/color.h" #include "ui/tui.h" -bool is_clicked = false; - -void on_button_click(const MOUSE_ACTION *mouse_action) { - is_clicked = !is_clicked; -} - -WIDGET *ui_build0(TUI *tui) { - if (is_clicked) { - char frame[20 + 4 + 1]; - const uint64_t fps = 1000000000 / tui->last_frame; - sprintf(frame, "%ldfps\n", fps); - return tui_make_box( - MAX_WIDTH, MAX_HEIGHT, - tui_make_column(tui_make_widget_array( - tui_make_box(0, 12, NULL, COLOR_NO_COLOR), - tui_make_row(tui_make_widget_array( - tui_make_box(50, 0, NULL, COLOR_NO_COLOR), - tui_make_box( - 20, 3, - tui_make_column(tui_make_widget_array( - tui_make_text(frame, COLOR_BLUE), - tui_make_button(tui_make_text("Back", COLOR_RED), - on_button_click))), - COLOR_WHITE))))), - COLOR_MAGENTA); - } else { - return tui_make_box( - MAX_WIDTH, MAX_HEIGHT, - tui_make_column(tui_make_widget_array( - tui_make_box(0, 12, NULL, COLOR_NO_COLOR), - tui_make_row(tui_make_widget_array( - tui_make_box(50, 0, NULL, COLOR_NO_COLOR), - tui_make_button( - tui_make_box(MIN_WIDTH, MIN_HEIGHT, - tui_make_text("\nClick here\n", COLOR_BLUE), - COLOR_WHITE), - on_button_click))))), - COLOR_MAGENTA); - } -} - WIDGET *search_box() { return tui_make_padding( - tui_make_box( - MAX_WIDTH, 1, - tui_make_center(tui_make_row(tui_make_widget_array( - tui_make_box(MAX_WIDTH, 1, NULL, COLOR_BLUE), - tui_make_box(10, 1, - tui_make_center(tui_make_text("Search", COLOR_BLUE)), - COLOR_GREEN) - ))), - COLOR_NO_COLOR), + tui_make_box(MAX_WIDTH, 1, + tui_make_center(tui_make_row(tui_make_widget_array( + tui_make_box(MAX_WIDTH, 1, NULL, color_init(0xFF42414D)), + tui_make_box(10, 1, + tui_make_center(tui_make_text( + "Search", color_init(0xFF000000))), + color_init(0xFFC4C3C9))))), + COLOR_NO_COLOR), 1, 1, 10, 10); } +WIDGET *search_header() {} + WIDGET *ui_build(TUI *tui) { return tui_make_box( MAX_WIDTH, MAX_HEIGHT, tui_make_column(tui_make_widget_array( search_box(), - tui_make_row(tui_make_widget_array(tui_make_center(tui_make_button( - tui_make_box( - MIN_WIDTH, 3, - tui_make_center(tui_make_text("Click here", COLOR_BLUE)), - COLOR_WHITE), - on_button_click)))))), - COLOR_MAGENTA); + tui_make_row(tui_make_widget_array(tui_make_center( + tui_make_box(MIN_WIDTH, 3, + tui_make_center(tui_make_text( + "Hi here", color_init(0xFF0000FF))), + color_init(0xFFFFFFFF))))))), + color_init(0xFF2B2A33)); } int main() { diff --git a/src/ui/color.c b/src/ui/color.c new file mode 100644 index 0000000..624bad9 --- /dev/null +++ b/src/ui/color.c @@ -0,0 +1,11 @@ +#include "color.h" + +COLOR color_init(uint32_t value) { return *(COLOR*)&value; } + +bool color_equals(COLOR a, COLOR b) { + return a.a == b.a && a.r == b.r && a.g == b.g && a.b == b.b; +} + +bool color_not_equals(COLOR a, COLOR b) { + return a.a != b.a || a.r != b.r || a.g != b.g || a.b != b.b; +} diff --git a/src/ui/color.h b/src/ui/color.h new file mode 100644 index 0000000..43005a6 --- /dev/null +++ b/src/ui/color.h @@ -0,0 +1,26 @@ +#pragma once + +#include <stdint.h> + +typedef struct COLOR { + uint8_t b, g, r, a; +} COLOR; + +#define color_hex(value) \ + (uint8_t)((uint32_t)value >> 3), \ + (uint8_t)((uint32_t)value >> 2), \ + (uint8_t)((uint32_t)value >> 1), \ + (uint8_t)((uint32_t)value >> 0) \ + +constexpr COLOR COLOR_NO_COLOR = { + .a = 0, + .r = 0, + .g = 0, + .b = 0, +}; + + +extern COLOR color_init(uint32_t value); + +extern bool color_equals(COLOR a,COLOR b); +extern bool color_not_equals(COLOR a,COLOR b); diff --git a/src/ui/tui.c b/src/ui/tui.c index 02e3079..62eb585 100644 --- a/src/ui/tui.c +++ b/src/ui/tui.c @@ -9,6 +9,8 @@ #include <time.h> #include <unistd.h> +#include "ui/color.h" + const int MAX_WIDTH = -1; const int MAX_HEIGHT = -1; @@ -141,7 +143,7 @@ void _tui_set_cell_char(TUI *tui, int x, int y, char c) { } void _tui_set_cell_color(TUI *tui, int x, int y, COLOR color) { - if (color == COLOR_NO_COLOR) { + if (color_equals(color, COLOR_NO_COLOR)) { return; } tui->cells[_tui_get_cell_index(tui, x, y)].color = color; @@ -149,7 +151,7 @@ void _tui_set_cell_color(TUI *tui, int x, int y, COLOR color) { void _tui_set_cell_background_color(TUI *tui, int x, int y, COLOR background_color) { - if (background_color == COLOR_NO_COLOR) { + if (color_equals(background_color, COLOR_NO_COLOR)) { return; } tui->cells[_tui_get_cell_index(tui, x, y)].background_color = @@ -158,11 +160,11 @@ void _tui_set_cell_background_color(TUI *tui, int x, int y, void _tui_set_cell_background_color_if_not_set(TUI *tui, int x, int y, COLOR background_color) { - if (background_color == COLOR_NO_COLOR) { + if (color_equals(background_color, COLOR_NO_COLOR)) { return; } TERMINAL_CELL *cell = &tui->cells[_tui_get_cell_index(tui, x, y)]; - if (cell->background_color == COLOR_NO_COLOR) { + if (color_equals(cell->background_color, COLOR_NO_COLOR)) { cell->background_color = background_color; } } @@ -182,8 +184,9 @@ void tui_handle_mouse_action(TUI *tui, const MOUSE_ACTION *mouse_action) { } } +/* int tui_change_terminal_text_color(COLOR color) { - if (color == COLOR_NO_COLOR) { + if (color_equals(color, COLOR_NO_COLOR)) { return 0; } else if (color == COLOR_RESET) { return printf("\033[%dm", COLOR_RESET); @@ -199,6 +202,7 @@ int tui_change_terminal_background_color(COLOR color) { } return printf("\033[%dm", color + 40); } +*/ bool handle_input(TUI *tui) { unsigned char buff[6]; @@ -616,7 +620,7 @@ bool _tui_is_max_width(const WIDGET *widget) { return true; } else if (metadata->width == MIN_WIDTH) { return _tui_is_max_width(metadata->child); - }else{ + } else { return false; } } @@ -632,14 +636,16 @@ bool _tui_is_max_width(const WIDGET *widget) { void _tui_move_to_start_in_str(char *str) { strcpy(str, "\033[;H"); } +/* int _tui_get_background_color_ascii(COLOR color) { - if (color == COLOR_NO_COLOR) { + if (color_equals(color, COLOR_NO_COLOR)) { return 0; } else if (color == COLOR_RESET) { return printf("\033[%dm", COLOR_RESET); } return printf("\033[%dm", color + 40); } +*/ void _tui_draw_cells_to_terminal(TUI *tui) { const size_t size_of_cell = 5 + 5 + sizeof(char) + 5; @@ -648,7 +654,7 @@ void _tui_draw_cells_to_terminal(TUI *tui) { _tui_move_to_start_in_str(str); - char cell_str[5]; + char cell_str[20]; COLOR last_color = COLOR_NO_COLOR; COLOR last_background_color = COLOR_NO_COLOR; @@ -656,24 +662,21 @@ void _tui_draw_cells_to_terminal(TUI *tui) { for (size_t i = 0; i < tui->cells_length; ++i) { const TERMINAL_CELL cell = tui->cells[i]; - if (last_color != cell.color || - last_background_color != cell.background_color) { - sprintf(cell_str, "\033[%dm", COLOR_RESET); + if (color_not_equals(last_color, cell.color) || + color_not_equals(last_background_color, cell.background_color)) { + sprintf(cell_str, "\033[0m"); strcat(str, cell_str); last_color = cell.color; last_background_color = cell.background_color; - if (cell.color == COLOR_RESET || cell.color == COLOR_NO_COLOR) { - sprintf(cell_str, "\033[%dm", COLOR_RESET); - } else { - sprintf(cell_str, "\033[%dm", cell.color + 30); + if (color_not_equals(cell.color, COLOR_NO_COLOR)) { + sprintf(cell_str, "\033[38;2;%d;%d;%dm", cell.color.r, cell.color.g, + cell.color.b); } strcat(str, cell_str); - if (cell.background_color == COLOR_RESET || - cell.background_color == COLOR_NO_COLOR) { - sprintf(cell_str, "\033[%dm", COLOR_RESET); - } else { - sprintf(cell_str, "\033[%dm", cell.background_color + 40); + if (color_not_equals(cell.background_color, COLOR_NO_COLOR)) { + sprintf(cell_str, "\033[48;2;%d;%d;%dm", cell.background_color.r, + cell.background_color.g, cell.background_color.b); } strcat(str, cell_str); } @@ -718,7 +721,7 @@ bool tui_widget_eqauls(const WIDGET *restrict left, case WIDGET_TYPE_TEXT: { const TEXT_METADATA *left_data = left->metadata; const TEXT_METADATA *right_data = right->metadata; - return left_data->color == right_data->color && + return color_equals(left_data->color, right_data->color) && strcmp(left_data->text, right_data->text) == 0; } case WIDGET_TYPE_BUTTON: { @@ -742,7 +745,7 @@ bool tui_widget_eqauls(const WIDGET *restrict left, const BOX_METADATA *right_data = right->metadata; return left_data->width == right_data->width && left_data->height == right_data->height && - left_data->color == right_data->color && + color_equals(left_data->color, right_data->color) && tui_widget_eqauls(left_data->child, right_data->child); } case WIDGET_TYPE_CENTER: { diff --git a/src/ui/tui.h b/src/ui/tui.h index eda55ac..8678804 100644 --- a/src/ui/tui.h +++ b/src/ui/tui.h @@ -4,6 +4,7 @@ #include <stdlib.h> #include <sys/ioctl.h> #include <termios.h> +#include <ui/color.h> extern const int MAX_WIDTH; extern const int MAX_HEIGHT; @@ -35,18 +36,6 @@ typedef enum bool : uint8_t { false = 0, true = 1 } bool; #endif #endif -typedef enum COLOR { - COLOR_NO_COLOR = -1, - COLOR_RESET = 0, - COLOR_RED = 1, - COLOR_GREEN = 2, - COLOR_YELLOW = 3, - COLOR_BLUE = 4, - COLOR_MAGENTA = 5, - COLOR_CYAN = 6, - COLOR_WHITE = 7 -} COLOR; - typedef struct TERMINAL_CELL { char c; COLOR color; |