Refactoring, license, Box UI framework
This commit is contained in:
172
box.c
172
box.c
@@ -1,112 +1,136 @@
|
||||
|
||||
/*
|
||||
* Copyright (C) Artsiom D.
|
||||
* Copyright (C) shit-co.de
|
||||
*/
|
||||
|
||||
|
||||
#include <curses.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "box.h"
|
||||
#include "ui.h"
|
||||
#include "util.h"
|
||||
|
||||
static void draw_rectangle(size_t x, size_t y, size_t width, size_t height)
|
||||
|
||||
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(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, '+');
|
||||
size_t w = width - 1;
|
||||
size_t h = height - 1;
|
||||
|
||||
for (size_t i = 1; i < height - 1; i++) {
|
||||
mvaddch(y + i, x, '|');
|
||||
/* corners */
|
||||
|
||||
ui_char(x, y, '+');
|
||||
ui_char(x, y + h, '+');
|
||||
ui_char(x + w, y + h, '+');
|
||||
ui_char(x + w, y, '+');
|
||||
|
||||
/* vertical borders */
|
||||
|
||||
for (size_t i = 1; i < h; i++) {
|
||||
ui_char(x, y + i, '|');
|
||||
}
|
||||
|
||||
for (size_t i = 1; i < height - 1; i++) {
|
||||
mvaddch(y + i, x + width - 1, '|');
|
||||
for (size_t i = 1; i < h; i++) {
|
||||
ui_char(x + w, y + i, '|');
|
||||
}
|
||||
|
||||
for (size_t i = 1; i < width - 1; i++) {
|
||||
mvaddch(y, x + i, '-');
|
||||
/* horizontal borders */
|
||||
|
||||
for (size_t i = 1; i < w; i++) {
|
||||
ui_char(x + i, y, '-');
|
||||
}
|
||||
|
||||
for (size_t i = 1; i < width - 1; i++) {
|
||||
mvaddch(y + height - 1, x + i, '-');
|
||||
for (size_t i = 1; i < w; i++) {
|
||||
ui_char(x + i, y + h, '-');
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
static void
|
||||
draw_button(struct box *b, size_t x, size_t y, size_t width, size_t height)
|
||||
{
|
||||
ui_color_on(b->color);
|
||||
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, '?');
|
||||
}
|
||||
}
|
||||
ui_string(x + width / 2 - strlen(b->name) / 2, y + height / 2, b->name);
|
||||
ui_color_off(b->color);
|
||||
}
|
||||
|
||||
void draw_vertical(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)
|
||||
{
|
||||
die("Don't call me");
|
||||
}
|
||||
|
||||
|
||||
static 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;
|
||||
}
|
||||
total_fills += b->children[i]->fills;
|
||||
}
|
||||
|
||||
size_t fill_pixels = height - total_pixels;
|
||||
double pixels_per_fill = fill_pixels / total_fills;
|
||||
double pixels_per_fill = (double)height / total_fills;
|
||||
|
||||
double current_y = 0;
|
||||
double current_y = y;
|
||||
|
||||
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;
|
||||
}
|
||||
double height = child->fills * pixels_per_fill;
|
||||
draw_box(child, x, current_y, width, height);
|
||||
current_y += height;
|
||||
}
|
||||
}
|
||||
|
||||
void draw_box(struct box *b, size_t x, size_t y, size_t width, size_t height)
|
||||
|
||||
struct box *
|
||||
new_button_box(char *name)
|
||||
{
|
||||
struct box *button = malloc(sizeof(struct box));
|
||||
|
||||
button->type = BOX_BUTTON;
|
||||
button->name = name;
|
||||
button->fills = 1;
|
||||
button->color = UI_RED;
|
||||
|
||||
return button;
|
||||
}
|
||||
|
||||
|
||||
struct box *
|
||||
new_vertical_box(void)
|
||||
{
|
||||
struct box *box = emalloc(sizeof(struct box));
|
||||
|
||||
box->type = BOX_VERTICAL;
|
||||
box->fills = 1;
|
||||
box->color = UI_RED;
|
||||
box->children = emalloc(sizeof(struct box *) * 100);
|
||||
box->length = 0;
|
||||
|
||||
return box;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user