I decided that using 4 SPACES is better than mixing with tabs.
This commit is contained in:
237
box.c
237
box.c
@@ -23,199 +23,206 @@ static void draw_container(struct box *, double x, double y, double width, doubl
|
|||||||
static void draw_button(struct box *, double x, double y, double width, double height);
|
static void draw_button(struct box *, double x, double y, double width, double height);
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
append_box(struct box *parent, struct box *child)
|
||||||
|
{
|
||||||
|
parent->children[parent->length++] = child;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
draw_rectangle(double fx, double fy, double fwidth, double fheight)
|
draw_rectangle(double fx, double fy, double fwidth, double fheight)
|
||||||
{
|
{
|
||||||
size_t x = llround(fx);
|
size_t x = llround(fx);
|
||||||
size_t y = llround(fy);
|
size_t y = llround(fy);
|
||||||
size_t w = llround(fwidth) - 1;
|
size_t w = llround(fwidth) - 1;
|
||||||
size_t h = llround(fheight) - 1;
|
size_t h = llround(fheight) - 1;
|
||||||
|
|
||||||
/* corners */
|
/* corners */
|
||||||
|
|
||||||
ui_char(x, y, '+');
|
ui_char(x, y, '+');
|
||||||
ui_char(x, y + h, '+');
|
ui_char(x, y + h, '+');
|
||||||
ui_char(x + w, y + h, '+');
|
ui_char(x + w, y + h, '+');
|
||||||
ui_char(x + w, y, '+');
|
ui_char(x + w, y, '+');
|
||||||
|
|
||||||
/* vertical borders */
|
/* vertical borders */
|
||||||
|
|
||||||
for (size_t i = 1; i < h; i++) {
|
for (size_t i = 1; i < h; i++) {
|
||||||
ui_char(x, y + i, '|');
|
ui_char(x, y + i, '|');
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 1; i < h; i++) {
|
for (size_t i = 1; i < h; i++) {
|
||||||
ui_char(x + w, y + i, '|');
|
ui_char(x + w, y + i, '|');
|
||||||
}
|
}
|
||||||
|
|
||||||
/* horizontal borders */
|
/* horizontal borders */
|
||||||
|
|
||||||
for (size_t i = 1; i < w; i++) {
|
for (size_t i = 1; i < w; i++) {
|
||||||
ui_char(x + i, y, '-');
|
ui_char(x + i, y, '-');
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 1; i < w; i++) {
|
for (size_t i = 1; i < w; i++) {
|
||||||
ui_char(x + i, y + h, '-');
|
ui_char(x + i, y + h, '-');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
draw_chart(struct box *chart, double x, double y, double width, double height)
|
draw_chart(struct box *chart, double x, double y, double width, double height)
|
||||||
{
|
{
|
||||||
char c = '@';
|
char c = '@';
|
||||||
long int actual_diff;
|
long int actual_diff;
|
||||||
double diff, price;
|
double diff, price;
|
||||||
enum ui_color color;
|
enum ui_color color;
|
||||||
|
|
||||||
draw_rectangle(x, y, width, height);
|
draw_rectangle(x, y, width, height);
|
||||||
|
|
||||||
x += 1;
|
x += 1;
|
||||||
y += 1;
|
y += 1;
|
||||||
width -= 2;
|
width -= 2;
|
||||||
height -= 2;
|
height -= 2;
|
||||||
|
|
||||||
price = height / 2;
|
price = height / 2;
|
||||||
actual_diff = 0;
|
actual_diff = 0;
|
||||||
|
|
||||||
for (size_t i = 0; i < width; i++) {
|
for (size_t i = 0; i < width; i++) {
|
||||||
diff = (double)rand() / RAND_MAX - 0.6;
|
diff = (double)rand() / RAND_MAX - 0.6;
|
||||||
actual_diff = (size_t)(height + y - price) - (size_t)(height + y - (price + diff));
|
actual_diff = (size_t)(height + y - price) - (size_t)(height + y - (price + diff));
|
||||||
|
|
||||||
if (actual_diff > 0) {
|
if (actual_diff > 0) {
|
||||||
color = UI_GREEN;
|
color = UI_GREEN;
|
||||||
} else if (actual_diff == 0) {
|
} else if (actual_diff == 0) {
|
||||||
color = UI_NONE;
|
color = UI_NONE;
|
||||||
} else if (actual_diff < 0) {
|
} else if (actual_diff < 0) {
|
||||||
color = UI_RED;
|
color = UI_RED;
|
||||||
}
|
}
|
||||||
|
|
||||||
ui_color_on(color);
|
ui_color_on(color);
|
||||||
ui_char(x + i, height + y - price, c);
|
ui_char(x + i, height + y - price, c);
|
||||||
ui_color_off(color);
|
ui_color_off(color);
|
||||||
|
|
||||||
price += diff;
|
price += diff;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
draw_button(struct box *b, double x, double y, double width, double height)
|
draw_button(struct box *b, double x, double y, double width, double height)
|
||||||
{
|
{
|
||||||
ui_color_on(b->color);
|
ui_color_on(b->color);
|
||||||
draw_rectangle(x, y, width, height);
|
draw_rectangle(x, y, width, height);
|
||||||
ui_string(x + width / 2 - strlen(b->name) / 2, y + height / 2, b->name);
|
ui_string(x + width / 2 - strlen(b->name) / 2, y + height / 2, b->name);
|
||||||
ui_color_off(b->color);
|
ui_color_off(b->color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
draw_container(struct box *b, double x, double y, double width, double height)
|
draw_container(struct box *b, double x, double y, double width, double height)
|
||||||
{
|
{
|
||||||
double current, length, total_fills, pixels_per_fill;
|
double current, length, total_fills, pixels_per_fill;
|
||||||
struct box *child;
|
struct box *child;
|
||||||
|
|
||||||
switch (b->type) {
|
switch (b->type) {
|
||||||
case BOX_VERTICAL:
|
case BOX_VERTICAL:
|
||||||
length = height;
|
length = height;
|
||||||
current = y;
|
current = y;
|
||||||
break;
|
break;
|
||||||
case BOX_HORIZONTAL:
|
case BOX_HORIZONTAL:
|
||||||
length = width;
|
length = width;
|
||||||
current = x;
|
current = x;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
total_fills = 0;
|
total_fills = 0;
|
||||||
for (size_t i = 0; i < b->length; i++) {
|
for (size_t i = 0; i < b->length; i++) {
|
||||||
total_fills += b->children[i]->fills;
|
total_fills += b->children[i]->fills;
|
||||||
}
|
}
|
||||||
|
|
||||||
pixels_per_fill = length / total_fills;
|
pixels_per_fill = length / total_fills;
|
||||||
|
|
||||||
for (size_t i = 0; i < b->length; i++) {
|
for (size_t i = 0; i < b->length; i++) {
|
||||||
child = b->children[i];
|
child = b->children[i];
|
||||||
length = child->fills * pixels_per_fill;
|
length = child->fills * pixels_per_fill;
|
||||||
|
|
||||||
switch (b->type) {
|
switch (b->type) {
|
||||||
case BOX_VERTICAL: draw_box(child, x, current, width, length); break;
|
case BOX_VERTICAL: draw_box(child, x, current, width, length); break;
|
||||||
case BOX_HORIZONTAL: draw_box(child, current, y, length, height); break;
|
case BOX_HORIZONTAL: draw_box(child, current, y, length, height); break;
|
||||||
}
|
}
|
||||||
|
|
||||||
current += length;
|
current += length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct box *
|
struct box *
|
||||||
new_chart_box(void)
|
new_chart_box(void)
|
||||||
{
|
{
|
||||||
struct box *chart = malloc(sizeof(struct box));
|
struct box *chart = malloc(sizeof(struct box));
|
||||||
|
|
||||||
chart->type = BOX_CHART;
|
chart->type = BOX_CHART;
|
||||||
chart->prices = emalloc(sizeof(double) * 100);
|
chart->prices = emalloc(sizeof(double) * 100);
|
||||||
chart->fills = 1;
|
chart->fills = 1;
|
||||||
chart->color = UI_NONE;
|
chart->color = UI_NONE;
|
||||||
|
|
||||||
return chart;
|
return chart;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct box *
|
struct box *
|
||||||
new_button_box(char *name)
|
new_button_box(char *name)
|
||||||
{
|
{
|
||||||
struct box *button = malloc(sizeof(struct box));
|
struct box *button = malloc(sizeof(struct box));
|
||||||
|
|
||||||
button->type = BOX_BUTTON;
|
button->type = BOX_BUTTON;
|
||||||
button->name = name;
|
button->name = name;
|
||||||
button->fills = 1;
|
button->fills = 1;
|
||||||
button->color = UI_NONE;
|
button->color = UI_NONE;
|
||||||
|
|
||||||
return button;
|
return button;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct box *
|
struct box *
|
||||||
new_container_box(enum box_type type)
|
new_container_box(enum box_type type)
|
||||||
{
|
{
|
||||||
struct box *box = emalloc(sizeof(struct box));
|
struct box *box = emalloc(sizeof(struct box));
|
||||||
|
|
||||||
box->type = type;
|
box->type = type;
|
||||||
box->fills = 1;
|
box->fills = 1;
|
||||||
box->color = UI_NONE;
|
box->color = UI_NONE;
|
||||||
box->children = emalloc(sizeof(struct box *) * 100);
|
box->children = emalloc(sizeof(struct box *) * 100);
|
||||||
box->length = 0;
|
box->length = 0;
|
||||||
|
|
||||||
return box;
|
return box;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct box *
|
struct box *
|
||||||
new_horizontal_box(void)
|
new_horizontal_box(void)
|
||||||
{
|
{
|
||||||
return new_container_box(BOX_HORIZONTAL);
|
return new_container_box(BOX_HORIZONTAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct box *
|
struct box *
|
||||||
new_vertical_box(void)
|
new_vertical_box(void)
|
||||||
{
|
{
|
||||||
return new_container_box(BOX_VERTICAL);
|
return new_container_box(BOX_VERTICAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
draw_box(struct box *b, double x, double y, double width, double height)
|
draw_box(struct box *b, double x, double y, double width, double height)
|
||||||
{
|
{
|
||||||
switch (b->type) {
|
switch (b->type) {
|
||||||
case BOX_BUTTON:
|
case BOX_BUTTON:
|
||||||
draw_button(b, x, y, width, height);
|
draw_button(b, x, y, width, height);
|
||||||
break;
|
break;
|
||||||
case BOX_CHART:
|
case BOX_CHART:
|
||||||
draw_chart(b, x, y, width, height);
|
draw_chart(b, x, y, width, height);
|
||||||
break;
|
break;
|
||||||
case BOX_HORIZONTAL:
|
case BOX_HORIZONTAL:
|
||||||
case BOX_VERTICAL:
|
case BOX_VERTICAL:
|
||||||
draw_container(b, x, y, width, height);
|
draw_container(b, x, y, width, height);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
27
box.h
27
box.h
@@ -24,22 +24,22 @@ struct box {
|
|||||||
double fills;
|
double fills;
|
||||||
|
|
||||||
union {
|
union {
|
||||||
/* button */
|
/* button */
|
||||||
char *name;
|
char *name;
|
||||||
|
|
||||||
/* chart and vertical/horizontal box */
|
/* chart and vertical/horizontal box */
|
||||||
struct {
|
struct {
|
||||||
/* chart and vertical/horizontal box */
|
/* chart and vertical/horizontal box */
|
||||||
size_t length;
|
size_t length;
|
||||||
|
|
||||||
union {
|
union {
|
||||||
/* vertical/horizontal box */
|
/* vertical/horizontal box */
|
||||||
struct box **children;
|
struct box **children;
|
||||||
|
|
||||||
/* chart */
|
/* chart */
|
||||||
double *prices;
|
double *prices;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -49,5 +49,6 @@ struct box *new_vertical_box(void);
|
|||||||
struct box *new_horizontal_box(void);
|
struct box *new_horizontal_box(void);
|
||||||
struct box *new_chart_box(void);
|
struct box *new_chart_box(void);
|
||||||
|
|
||||||
|
void append_box(struct box *parent, struct box *child);
|
||||||
|
|
||||||
void draw_box(struct box *, double x, double y, double width, double height);
|
void draw_box(struct box *, double x, double y, double width, double height);
|
||||||
|
|||||||
66
main.c
66
main.c
@@ -16,50 +16,46 @@
|
|||||||
int
|
int
|
||||||
main(void)
|
main(void)
|
||||||
{
|
{
|
||||||
struct box *ui, *top, *bottom, *left, *right, *buy, *sell, *scam, *title, *desc;
|
struct box *ui, *top, *bottom, *left, *right, *buy, *sell, *scam, *title, *desc;
|
||||||
|
|
||||||
ui_init();
|
ui_init();
|
||||||
|
|
||||||
title = new_button_box("Bitcoin");
|
title = new_button_box("Bitcoin");
|
||||||
desc = new_button_box("A crypto-scam :-)");
|
desc = new_button_box("A crypto-scam :-)");
|
||||||
|
|
||||||
top = new_vertical_box();
|
top = new_vertical_box();
|
||||||
top->length = 2;
|
append_box(top, title);
|
||||||
top->children[0] = title;
|
append_box(top, desc);
|
||||||
top->children[1] = desc;
|
|
||||||
|
|
||||||
left = new_chart_box();
|
left = new_chart_box();
|
||||||
left->fills = 5;
|
left->fills = 5;
|
||||||
|
|
||||||
buy = new_button_box("buy");
|
buy = new_button_box("buy");
|
||||||
buy->color = UI_GREEN;
|
buy->color = UI_GREEN;
|
||||||
sell = new_button_box("sell");
|
sell = new_button_box("sell");
|
||||||
sell->color = UI_RED;
|
sell->color = UI_RED;
|
||||||
scam = new_button_box("scam");
|
scam = new_button_box("scam");
|
||||||
sell->color = UI_BLUE;
|
sell->color = UI_BLUE;
|
||||||
|
|
||||||
right = new_vertical_box();
|
right = new_vertical_box();
|
||||||
right->length = 3;
|
append_box(right, buy);
|
||||||
right->children[0] = buy;
|
append_box(right, sell);
|
||||||
right->children[1] = sell;
|
append_box(right, scam);
|
||||||
right->children[2] = scam;
|
|
||||||
|
|
||||||
bottom = new_horizontal_box();
|
bottom = new_horizontal_box();
|
||||||
bottom->fills = 3;
|
bottom->fills = 3;
|
||||||
bottom->length = 2;
|
append_box(bottom, left);
|
||||||
bottom->children[0] = left;
|
append_box(bottom, right);
|
||||||
bottom->children[1] = right;
|
|
||||||
|
|
||||||
ui = new_vertical_box();
|
ui = new_vertical_box();
|
||||||
ui->length = 2;
|
append_box(ui, top);
|
||||||
ui->children[0] = top;
|
append_box(ui, bottom);
|
||||||
ui->children[1] = bottom;
|
|
||||||
|
|
||||||
draw_box(ui, 0, 0, ui_width(), 40);
|
draw_box(ui, 0, 0, ui_width()-1, ui_height()-1);
|
||||||
|
|
||||||
ui_refresh();
|
ui_refresh();
|
||||||
|
|
||||||
sleep(300);
|
sleep(300);
|
||||||
|
|
||||||
ui_end();
|
ui_end();
|
||||||
}
|
}
|
||||||
|
|||||||
29
ui.c
29
ui.c
@@ -11,26 +11,27 @@
|
|||||||
#include "ui.h"
|
#include "ui.h"
|
||||||
|
|
||||||
|
|
||||||
void ui_init(void)
|
void
|
||||||
|
ui_init(void)
|
||||||
{
|
{
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
initscr();
|
initscr();
|
||||||
start_color();
|
start_color();
|
||||||
init_pair(UI_GREEN, COLOR_GREEN, COLOR_BLACK);
|
init_pair(UI_GREEN, COLOR_GREEN, COLOR_BLACK);
|
||||||
init_pair(UI_RED, COLOR_RED, COLOR_BLACK);
|
init_pair(UI_RED, COLOR_RED, COLOR_BLACK);
|
||||||
init_pair(UI_BLUE, COLOR_BLUE, COLOR_BLACK);
|
init_pair(UI_BLUE, COLOR_BLUE, COLOR_BLACK);
|
||||||
cbreak();
|
cbreak();
|
||||||
noecho();
|
noecho();
|
||||||
nonl();
|
nonl();
|
||||||
intrflush(stdscr, FALSE);
|
intrflush(stdscr, FALSE);
|
||||||
keypad(stdscr, TRUE);
|
keypad(stdscr, TRUE);
|
||||||
clear();
|
clear();
|
||||||
refresh();
|
refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ui_end()
|
void ui_end()
|
||||||
{
|
{
|
||||||
endwin();
|
endwin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ui_string(size_t x, size_t y, char *s)
|
void ui_string(size_t x, size_t y, char *s)
|
||||||
|
|||||||
Reference in New Issue
Block a user