Framework, stijl en cutting-edge UI
This commit is contained in:
142
box.c
142
box.c
@@ -6,6 +6,8 @@
|
||||
|
||||
|
||||
#include <curses.h>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -15,17 +17,19 @@
|
||||
#include "util.h"
|
||||
|
||||
|
||||
static void draw_rectangle(size_t x, size_t y, size_t width, size_t height);
|
||||
static void draw_vertical(struct box *, size_t x, size_t y, size_t width, size_t height);
|
||||
static void draw_horizontal(struct box *, size_t x, size_t y, size_t width, size_t height);
|
||||
static void draw_button(struct box *, size_t x, size_t y, size_t width, size_t height);
|
||||
static void draw_rectangle(double x, double y, double width, double height);
|
||||
static void draw_chart(struct box *, double x, double y, double width, double height);
|
||||
static void draw_container(struct box *, double x, double y, double width, double height);
|
||||
static void draw_button(struct box *, double x, double y, double width, double height);
|
||||
|
||||
|
||||
static void
|
||||
draw_rectangle(size_t x, size_t y, size_t width, size_t height)
|
||||
draw_rectangle(double fx, double fy, double fwidth, double fheight)
|
||||
{
|
||||
size_t w = width - 1;
|
||||
size_t h = height - 1;
|
||||
size_t x = llround(fx);
|
||||
size_t y = llround(fy);
|
||||
size_t w = llround(fwidth) - 1;
|
||||
size_t h = llround(fheight) - 1;
|
||||
|
||||
/* corners */
|
||||
|
||||
@@ -57,7 +61,46 @@ draw_rectangle(size_t x, size_t y, size_t width, size_t height)
|
||||
|
||||
|
||||
static void
|
||||
draw_button(struct box *b, size_t x, size_t y, size_t width, size_t height)
|
||||
draw_chart(struct box *chart, double x, double y, double width, double height)
|
||||
{
|
||||
char c = '@';
|
||||
long int actual_diff;
|
||||
double diff, price;
|
||||
enum ui_color color;
|
||||
|
||||
draw_rectangle(x, y, width, height);
|
||||
|
||||
x += 1;
|
||||
y += 1;
|
||||
width -= 2;
|
||||
height -= 2;
|
||||
|
||||
price = height / 2;
|
||||
actual_diff = 0;
|
||||
|
||||
for (size_t i = 0; i < width; i++) {
|
||||
diff = (double)rand() / RAND_MAX - 0.6;
|
||||
actual_diff = (size_t)(height + y - price) - (size_t)(height + y - (price + diff));
|
||||
|
||||
if (actual_diff > 0) {
|
||||
color = UI_GREEN;
|
||||
} else if (actual_diff == 0) {
|
||||
color = UI_NONE;
|
||||
} else if (actual_diff < 0) {
|
||||
color = UI_RED;
|
||||
}
|
||||
|
||||
ui_color_on(color);
|
||||
ui_char(x + i, height + y - price, c);
|
||||
ui_color_off(color);
|
||||
|
||||
price += diff;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
draw_button(struct box *b, double x, double y, double width, double height)
|
||||
{
|
||||
ui_color_on(b->color);
|
||||
draw_rectangle(x, y, width, height);
|
||||
@@ -67,31 +110,54 @@ draw_button(struct box *b, size_t x, size_t y, size_t width, size_t height)
|
||||
|
||||
|
||||
static void
|
||||
draw_horizontal(struct box *b, size_t x, size_t y, size_t width, size_t height)
|
||||
draw_container(struct box *b, double x, double y, double width, double height)
|
||||
{
|
||||
die("Don't call me");
|
||||
}
|
||||
double current, length, total_fills, pixels_per_fill;
|
||||
struct box *child;
|
||||
|
||||
switch (b->type) {
|
||||
case BOX_VERTICAL:
|
||||
length = height;
|
||||
current = y;
|
||||
break;
|
||||
case BOX_HORIZONTAL:
|
||||
length = width;
|
||||
current = x;
|
||||
break;
|
||||
}
|
||||
|
||||
static void
|
||||
draw_vertical(struct box *b, size_t x, size_t y, size_t width, size_t height)
|
||||
{
|
||||
double total_fills = 0;
|
||||
|
||||
total_fills = 0;
|
||||
for (size_t i = 0; i < b->length; i++) {
|
||||
total_fills += b->children[i]->fills;
|
||||
}
|
||||
|
||||
double pixels_per_fill = (double)height / total_fills;
|
||||
|
||||
double current_y = y;
|
||||
pixels_per_fill = length / total_fills;
|
||||
|
||||
for (size_t i = 0; i < b->length; i++) {
|
||||
struct box *child = b->children[i];
|
||||
double height = child->fills * pixels_per_fill;
|
||||
draw_box(child, x, current_y, width, height);
|
||||
current_y += height;
|
||||
child = b->children[i];
|
||||
length = child->fills * pixels_per_fill;
|
||||
|
||||
switch (b->type) {
|
||||
case BOX_VERTICAL: draw_box(child, x, current, width, length); break;
|
||||
case BOX_HORIZONTAL: draw_box(child, current, y, length, height); break;
|
||||
}
|
||||
|
||||
current += length;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct box *
|
||||
new_chart_box(void)
|
||||
{
|
||||
struct box *chart = malloc(sizeof(struct box));
|
||||
|
||||
chart->type = BOX_CHART;
|
||||
chart->prices = emalloc(sizeof(double) * 100);
|
||||
chart->fills = 1;
|
||||
chart->color = UI_NONE;
|
||||
|
||||
return chart;
|
||||
}
|
||||
|
||||
|
||||
@@ -103,39 +169,53 @@ new_button_box(char *name)
|
||||
button->type = BOX_BUTTON;
|
||||
button->name = name;
|
||||
button->fills = 1;
|
||||
button->color = UI_RED;
|
||||
button->color = UI_NONE;
|
||||
|
||||
return button;
|
||||
}
|
||||
|
||||
|
||||
struct box *
|
||||
new_vertical_box(void)
|
||||
new_container_box(enum box_type type)
|
||||
{
|
||||
struct box *box = emalloc(sizeof(struct box));
|
||||
|
||||
box->type = BOX_VERTICAL;
|
||||
box->type = type;
|
||||
box->fills = 1;
|
||||
box->color = UI_RED;
|
||||
box->color = UI_NONE;
|
||||
box->children = emalloc(sizeof(struct box *) * 100);
|
||||
box->length = 0;
|
||||
|
||||
return box;
|
||||
}
|
||||
|
||||
struct box *
|
||||
new_horizontal_box(void)
|
||||
{
|
||||
return new_container_box(BOX_HORIZONTAL);
|
||||
}
|
||||
|
||||
|
||||
struct box *
|
||||
new_vertical_box(void)
|
||||
{
|
||||
return new_container_box(BOX_VERTICAL);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
draw_box(struct box *b, size_t x, size_t y, size_t width, size_t height)
|
||||
draw_box(struct box *b, double x, double y, double width, double height)
|
||||
{
|
||||
switch (b->type) {
|
||||
case BOX_BUTTON:
|
||||
draw_button(b, x, y, width, height);
|
||||
break;
|
||||
case BOX_VERTICAL:
|
||||
draw_vertical(b, x, y, width, height);
|
||||
case BOX_CHART:
|
||||
draw_chart(b, x, y, width, height);
|
||||
break;
|
||||
case BOX_HORIZONTAL:
|
||||
draw_horizontal(b, x, y, width, height);
|
||||
case BOX_VERTICAL:
|
||||
draw_container(b, x, y, width, height);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
19
box.h
19
box.h
@@ -14,6 +14,7 @@
|
||||
enum box_type {
|
||||
BOX_VERTICAL,
|
||||
BOX_HORIZONTAL,
|
||||
BOX_CHART,
|
||||
BOX_BUTTON,
|
||||
};
|
||||
|
||||
@@ -24,14 +25,20 @@ struct box {
|
||||
|
||||
union {
|
||||
/* button */
|
||||
struct {
|
||||
char *name;
|
||||
};
|
||||
|
||||
/* container boxes */
|
||||
/* chart and vertical/horizontal box */
|
||||
struct {
|
||||
struct box **children;
|
||||
/* chart and vertical/horizontal box */
|
||||
size_t length;
|
||||
|
||||
union {
|
||||
/* vertical/horizontal box */
|
||||
struct box **children;
|
||||
|
||||
/* chart */
|
||||
double *prices;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
@@ -40,5 +47,7 @@ struct box {
|
||||
struct box *new_button_box(char *name);
|
||||
struct box *new_vertical_box(void);
|
||||
struct box *new_horizontal_box(void);
|
||||
struct box *new_chart_box(void);
|
||||
|
||||
void draw_box(struct box *, size_t x, size_t y, size_t width, size_t height);
|
||||
|
||||
void draw_box(struct box *, double x, double y, double width, double height);
|
||||
|
||||
44
main.c
44
main.c
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <curses.h>
|
||||
|
||||
#include "box.h"
|
||||
#include "ui.h"
|
||||
@@ -15,18 +16,47 @@
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
struct box *parent;
|
||||
struct box *ui, *top, *bottom, *left, *right, *buy, *sell, *scam, *title, *desc;
|
||||
|
||||
ui_init();
|
||||
|
||||
parent = new_vertical_box();
|
||||
title = new_button_box("Bitcoin");
|
||||
desc = new_button_box("A crypto-scam :-)");
|
||||
|
||||
parent->length = 2;
|
||||
parent->children[0] = new_button_box("hello!");
|
||||
parent->children[0]->color = UI_BLUE;
|
||||
parent->children[1] = new_button_box("quit");
|
||||
top = new_vertical_box();
|
||||
top->length = 2;
|
||||
top->children[0] = title;
|
||||
top->children[1] = desc;
|
||||
|
||||
left = new_chart_box();
|
||||
left->fills = 5;
|
||||
|
||||
buy = new_button_box("buy");
|
||||
buy->color = UI_GREEN;
|
||||
sell = new_button_box("sell");
|
||||
sell->color = UI_RED;
|
||||
scam = new_button_box("scam");
|
||||
sell->color = UI_BLUE;
|
||||
|
||||
right = new_vertical_box();
|
||||
right->length = 3;
|
||||
right->children[0] = buy;
|
||||
right->children[1] = sell;
|
||||
right->children[2] = scam;
|
||||
|
||||
bottom = new_horizontal_box();
|
||||
bottom->fills = 3;
|
||||
bottom->length = 2;
|
||||
bottom->children[0] = left;
|
||||
bottom->children[1] = right;
|
||||
|
||||
ui = new_vertical_box();
|
||||
ui->length = 2;
|
||||
ui->children[0] = top;
|
||||
ui->children[1] = bottom;
|
||||
|
||||
draw_box(ui, 0, 0, ui_width(), 40);
|
||||
|
||||
draw_box(parent, 0, 0, 20, 30);
|
||||
ui_refresh();
|
||||
|
||||
sleep(300);
|
||||
|
||||
10
ui.c
10
ui.c
@@ -62,3 +62,13 @@ void ui_clear(void)
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
size_t ui_width(void)
|
||||
{
|
||||
return COLS;
|
||||
}
|
||||
|
||||
size_t ui_height(void)
|
||||
{
|
||||
return LINES;
|
||||
}
|
||||
|
||||
6
ui.h
6
ui.h
@@ -9,7 +9,8 @@
|
||||
|
||||
|
||||
enum ui_color {
|
||||
UI_RED = 1,
|
||||
UI_NONE,
|
||||
UI_RED,
|
||||
UI_GREEN,
|
||||
UI_BLUE,
|
||||
};
|
||||
@@ -24,5 +25,8 @@ void ui_color_off(enum ui_color);
|
||||
void ui_char(size_t x, size_t y, char c);
|
||||
void ui_string(size_t x, size_t y, char *s);
|
||||
|
||||
size_t ui_width(void);
|
||||
size_t ui_height(void);
|
||||
|
||||
void ui_refresh(void);
|
||||
void ui_clear(void);
|
||||
|
||||
Reference in New Issue
Block a user