Community-based box design. Remaking ReactJS, Vue, Angular, etc.

This commit is contained in:
2025-10-16 20:22:32 +02:00
parent 90fc37b6f4
commit 0222bc1f90
3 changed files with 273 additions and 55 deletions

117
box.c Normal file
View File

@@ -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;
}
}

54
box.h Normal file
View File

@@ -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 *);

157
main.c
View File

@@ -6,17 +6,17 @@
#include <math.h> #include <math.h>
#include <time.h> #include <time.h>
struct chart {
size_t length;
double *prices;
};
struct coin { struct coin {
const char *name; const char *name;
const char *description; const char *description;
struct chart chart; struct chart chart;
}; };
struct chart {
double *prices;
size_t length;
};
struct chart new_chart(size_t length) struct chart new_chart(size_t length)
{ {
double *prices = malloc(length * sizeof(double)); double *prices = malloc(length * sizeof(double));
@@ -40,23 +40,21 @@ enum game_state {
GAME_END, GAME_END,
}; };
struct button { enum color {
char *name; color_red,
size_t width; color_green,
size_t height; color_blue,
struct button *next;
}; };
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)); struct button *button = malloc(sizeof(struct button));
button->name = name; button->name = name;
button->width = width; button->width = width;
button->height = height; button->height = height;
button->next = NULL; button->next = NULL;
button->color = color;
button->highlighted = false;
return button; return button;
} }
@@ -67,62 +65,86 @@ struct game {
struct button *button; 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) void draw_button(struct button *b, size_t x, size_t y)
{ {
draw_box(x, y, b->width, b->height); 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); 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) 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 i = 0; i < chart->length; i++) {
for (size_t j = 0; j < chart->prices[i]; j++) { 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); const size_t button_width = (double)(width) / 3;
struct button *sell = new_button("sell", 20, 5);
struct button *scam = new_button("scam", 20, 5);
attron(COLOR_PAIR(1)); struct button *buy = new_button("buy", color_red, button_width, 5);
draw_button(buy, 4, 28); struct button *sell = new_button("sell", color_blue, button_width, 5);
attroff(COLOR_PAIR(1)); struct button *scam = new_button("scam", color_green, button_width, 5);
attron(COLOR_PAIR(2)); buy->next = sell;
draw_button(sell, 28, 28); sell->next = scam;
attroff(COLOR_PAIR(2)); scam->next = buy;
attron(COLOR_PAIR(3)); buy->highlighted = true;
draw_button(scam, 52, 28);
attroff(COLOR_PAIR(3)); 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) void draw_game(struct game *game)
{ {
clear(); clear();
@@ -132,14 +154,39 @@ void draw_game(struct game *game)
refresh(); 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) int main(void)
{ {
srand(time(NULL)); srand(time(NULL));
initscr(); initscr();
start_color(); start_color();
init_pair(1, COLOR_GREEN, COLOR_BLACK); init_pair(color_green, COLOR_GREEN, COLOR_BLACK);
init_pair(2, COLOR_RED, COLOR_BLACK); init_pair(color_red, COLOR_RED, COLOR_BLACK);
init_pair(3, COLOR_BLUE, COLOR_BLACK); init_pair(color_blue, COLOR_BLUE, COLOR_BLACK);
cbreak(); cbreak();
noecho(); noecho();
nonl(); nonl();