From 0222bc1f905c7bdd69bc28a9769ae720bbc11d31 Mon Sep 17 00:00:00 2001 From: Artsiom Dzenisiuk Date: Thu, 16 Oct 2025 20:22:32 +0200 Subject: [PATCH] Community-based box design. Remaking ReactJS, Vue, Angular, etc. --- box.c | 117 ++++++++++++++++++++++++++++++++++++++++++ box.h | 54 ++++++++++++++++++++ main.c | 157 +++++++++++++++++++++++++++++++++++++-------------------- 3 files changed, 273 insertions(+), 55 deletions(-) create mode 100644 box.c create mode 100644 box.h diff --git a/box.c b/box.c new file mode 100644 index 0000000..083d141 --- /dev/null +++ b/box.c @@ -0,0 +1,117 @@ +#include "box.h" + +static void draw_rectangle(size_t x, size_t y, size_t width, size_t height) +{ + mvaddch(y, x, '+'); + mvaddch(y + height - 1, x, '+'); + mvaddch(y + height - 1, x + width - 1, '+'); + mvaddch(y, x + width - 1, '+'); + + for (size_t i = 1; i < height - 1; i++) { + mvaddch(y + i, x, '|'); + } + + for (size_t i = 1; i < height - 1; i++) { + mvaddch(y + i, x + width - 1, '|'); + } + + for (size_t i = 1; i < width - 1; i++) { + mvaddch(y, x + i, '-'); + } + + for (size_t i = 1; i < width - 1; i++) { + mvaddch(y + height - 1, x + i, '-'); + } +} + +void draw_button(struct box *b) +{ + draw_rectangle(x, y, b->width, b->height); + + if (b->highlighted) { + attron(A_UNDERLINE); + } + + attron(COLOR_PAIR(b->color)); + + mvaddstr(y + b->height / 2, x + b->width / 2 - strlen(b->name) / 2, b->name); + + if (b->highlighted) { + attroff(A_UNDERLINE); + } + + attroff(COLOR_PAIR(b->color)); +} + +void draw_chart(struct box *b, size_t x, size_t y, size_t width, size_t height) +{ + draw_rectangle(x, y, width, height); + + size_t chart_width = width - 2; + size_t chart_height = height - 2; + size_t chart_x = x + 1; + size_t chart_y = y + 1; + + for (size_t i = 0; i < chart->length; i++) { + for (size_t j = 0; j < chart->prices[i]; j++) { + mvaddch(chart_height - (chart_y + j), chart_x * i, '?'); + } + } +} + +void draw_vertical(struct box *b, size_t x, size_t y, size_t width, size_t height) +{ + size_t total_pixels = 0; + double total_fills = 0; + + for (size_t i = 0; i < b->length; i++) { + struct box *child = b->children[i]; + + switch (child.length.type) { + case LENGTH_PIXEL: + total_pixels += child.length.pixels; + break; + case LENGTH_FILL: + total_fills += child.length.fills; + break; + } + } + + size_t fill_pixels = height - total_pixels; + double pixels_per_fill = fill_pixels / total_fills; + + double current_y = 0; + + for (size_t i = 0; i < b->length; i++) { + struct box *child = b->children[i]; + + double width; + + switch (child.length.type) { + case LENGTH_PIXEL: + width = child.length.pixels; + break; + case LENGTH_FILL: + width = child.length.fills * pixels_per_fill; + break; + } + } +} + +void draw_box(struct box *b, size_t x, size_t y, size_t width, size_t height) +{ + switch (b->type) { + case BOX_BUTTON: + draw_button(b, x, y, width, height); + break; + case BOX_CHART: + draw_chart(b, x, y, width, height); + break; + case BOX_VERTICAL: + draw_vertical(b, x, y, width, height); + break; + case BOX_HORIZONTAL: + draw_horizontal(b, x, y, width, height); + break; + } +} diff --git a/box.h b/box.h new file mode 100644 index 0000000..bea1485 --- /dev/null +++ b/box.h @@ -0,0 +1,54 @@ +#pragma once + +#include "chart.h" + +enum box_type { + BOX_VERTICAL, + BOX_HORIZONTAL, + BOX_CHART, + BOX_BUTTON, +}; + +enum length_type { + LENGTH_FILL, + LENGTH_PIXEL, +}; + +struct length { + enum length_type type; + union { + double fill; + double pixel; + }; +}; + +struct box { + enum box_type; + + size_t width; + size_t height; + + enum color color; + + union { + struct chart *chart; + + /* button */ + struct { + char *name; + }; + + /* container boxes */ + struct { + struct box *children; + size_t length; + }; + }; +}; + +box *new_chart_box(struct box *, size_t width, size_t height); +box *new_button_box(struct button *, size_t width, size_t height); +box *new_vertical_box(size_t width, size_t height); +box *new_horizontal_box(size_t width, size_t height); + +void draw_box(struct box *); diff --git a/main.c b/main.c index c6aa264..3b670e8 100644 --- a/main.c +++ b/main.c @@ -6,17 +6,17 @@ #include #include -struct chart { - size_t length; - double *prices; -}; - struct coin { const char *name; const char *description; struct chart chart; }; +struct chart { + double *prices; + size_t length; +}; + struct chart new_chart(size_t length) { double *prices = malloc(length * sizeof(double)); @@ -40,23 +40,21 @@ enum game_state { GAME_END, }; -struct button { - char *name; - size_t width; - size_t height; - - struct button *next; +enum color { + color_red, + color_green, + color_blue, }; -struct button *new_button(char *name, size_t width, size_t height) +struct button *new_button(char *name, enum color color, size_t width, size_t height) { struct button *button = malloc(sizeof(struct button)); - button->name = name; button->width = width; button->height = height; button->next = NULL; - + button->color = color; + button->highlighted = false; return button; } @@ -67,62 +65,86 @@ struct game { struct button *button; }; -void draw_box(size_t x, size_t y, size_t width, size_t height) -{ - mvaddch(y, x, '+'); - mvaddch(y + height - 1, x, '+'); - mvaddch(y + height - 1, x + width - 1, '+'); - mvaddch(y, x + width - 1, '+'); - - for (size_t i = 1; i < height - 1; i++) { - mvaddch(y + i, x, '|'); - } - - for (size_t i = 1; i < height - 1; i++) { - mvaddch(y + i, x + width - 1, '|'); - } - - for (size_t i = 1; i < width - 1; i++) { - mvaddch(y, x + i, '-'); - } - - for (size_t i = 1; i < width - 1; i++) { - mvaddch(y + height - 1, x + i, '-'); - } -} - void draw_button(struct button *b, size_t x, size_t y) { draw_box(x, y, b->width, b->height); + + if (b->highlighted) { + attron(A_UNDERLINE); + } + + attron(COLOR_PAIR(b->color)); + mvaddstr(y + b->height / 2, x + b->width / 2 - strlen(b->name) / 2, b->name); + + if (b->highlighted) { + attroff(A_UNDERLINE); + } + + attroff(COLOR_PAIR(b->color)); } void draw_chart(struct chart *chart, size_t x, size_t y) { - draw_box(x - 1, y - 1, (chart->length * 2) + 8, 23); + const size_t height = 30; + const size_t width = chart->length * 2 + 1; + + draw_box(x, y, width, height + 1); + for (size_t i = 0; i < chart->length; i++) { for (size_t j = 0; j < chart->prices[i]; j++) { - mvaddch(30 - (y + j), x + (i * 2), '?'); + mvaddch(30 - (y + j), x + (i * 2) + 1, '?'); } } - struct button *buy = new_button("buy", 20, 5); - struct button *sell = new_button("sell", 20, 5); - struct button *scam = new_button("scam", 20, 5); + const size_t button_width = (double)(width) / 3; - attron(COLOR_PAIR(1)); - draw_button(buy, 4, 28); - attroff(COLOR_PAIR(1)); + struct button *buy = new_button("buy", color_red, button_width, 5); + struct button *sell = new_button("sell", color_blue, button_width, 5); + struct button *scam = new_button("scam", color_green, button_width, 5); - attron(COLOR_PAIR(2)); - draw_button(sell, 28, 28); - attroff(COLOR_PAIR(2)); + buy->next = sell; + sell->next = scam; + scam->next = buy; - attron(COLOR_PAIR(3)); - draw_button(scam, 52, 28); - attroff(COLOR_PAIR(3)); + buy->highlighted = true; + + draw_button(buy, x, y + height + 1); + draw_button(sell, x + button_width, y + height + 1); + draw_button(scam, x + button_width * 2, y + height + 1); } +enum box_type { + BOX_TYPE_BUTTON, + BOX_TYPE_CHART, + BOX_TYPE_HORIZONTAL, + BOX_TYPE_VERTICAL, +}; + +struct box { + enum box_type type; + size_t width; + size_t height; + enum color color; + + union { + /* chart */ + struct { + double *prices; + size_t length; + }; + + /* button */ + char *name; + + /* horizontal/vertical box */ + struct { + struct box *children; + size_t length; + }; + } +}; + void draw_game(struct game *game) { clear(); @@ -132,14 +154,39 @@ void draw_game(struct game *game) refresh(); } +struct box *new_chart_box() + +struct box *new_container_box(enum box_type type) +{ + struct box *box = malloc(sizeof(box)); + box->width = 0; + box->height = 0; + box->type = type; + box->children = malloc(sizeof(box) * 100); + box->length = 0; + return box; +} + +struct box *new_vertical_box(void) +{ + return new_container_box(BOX_TYPE_VERTICAL); +} + +struct box *new_horizontal_box(void) +{ + return new_container_box(BOX_TYPE_HORIZONTAL); +} + +struct box *new + int main(void) { srand(time(NULL)); initscr(); start_color(); - init_pair(1, COLOR_GREEN, COLOR_BLACK); - init_pair(2, COLOR_RED, COLOR_BLACK); - init_pair(3, COLOR_BLUE, COLOR_BLACK); + init_pair(color_green, COLOR_GREEN, COLOR_BLACK); + init_pair(color_red, COLOR_RED, COLOR_BLACK); + init_pair(color_blue, COLOR_BLUE, COLOR_BLACK); cbreak(); noecho(); nonl();