Community-based box design. Remaking ReactJS, Vue, Angular, etc.
This commit is contained in:
117
box.c
Normal file
117
box.c
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user