mirror of
https://github.com/tuxbox-neutrino/neutrino.git
synced 2025-09-02 10:21:10 +02:00
merge neutrino-mp progressbar code, (C) martii
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <math.h>
|
||||
#include <global.h>
|
||||
#include <neutrino.h>
|
||||
|
||||
@@ -42,9 +43,7 @@
|
||||
CProgressBar::CProgressBar( const int x_pos, const int y_pos, const int w, const int h,
|
||||
fb_pixel_t color_frame, fb_pixel_t color_body, fb_pixel_t color_shadow,
|
||||
const fb_pixel_t active_col, const fb_pixel_t passive_col,
|
||||
const bool blinkenlights,
|
||||
const int r, const int g, const int b,
|
||||
const bool inv,
|
||||
CComponentsForm *parent)
|
||||
{
|
||||
//CComponentsItem
|
||||
@@ -60,19 +59,19 @@ CProgressBar::CProgressBar( const int x_pos, const int y_pos, const int w, const
|
||||
col_body = color_body;
|
||||
col_shadow = color_shadow;
|
||||
|
||||
pb_blink = blinkenlights;
|
||||
pb_invert = inv;
|
||||
pb_red = r;
|
||||
pb_green = g;
|
||||
pb_yellow = b;
|
||||
pb_active_col = active_col;
|
||||
pb_passive_col = passive_col;
|
||||
|
||||
pb_bl_changed = g_settings.progressbar_color;
|
||||
pb_design = &g_settings.progressbar_design;
|
||||
pb_gradient = &g_settings.progressbar_gradient;
|
||||
pb_type = PB_REDLEFT;
|
||||
|
||||
pb_last_width = -1;
|
||||
pb_value = 0;
|
||||
pb_max_value = 0;
|
||||
pb_paint_zero = false;
|
||||
|
||||
// init start positions x/y active bar
|
||||
pb_x = x + fr_thickness;
|
||||
@@ -119,143 +118,301 @@ void CProgressBar::initDimensions()
|
||||
col_frame = pb_active_col;
|
||||
}
|
||||
|
||||
void CProgressBar::paintShapes(int &shx, int ­, int &shw, int &shh, fb_pixel_t &col)
|
||||
{
|
||||
CComponentsShapeSquare shape(shx, shy, shw, shh, NULL, false);
|
||||
shape.setColorBody(col);
|
||||
shape.allowPaint(cc_allow_paint);
|
||||
shape.paint(false);
|
||||
}
|
||||
class CProgressBarCache;
|
||||
static std::vector<CProgressBarCache *> pbCache;
|
||||
|
||||
|
||||
void CProgressBar::paintSimple()
|
||||
class CProgressBarCache
|
||||
{
|
||||
// progress value
|
||||
if (pb_active_width != pb_last_width){
|
||||
if (pb_active_width)
|
||||
paintShapes(pb_x, pb_y, pb_active_width, pb_height, pb_active_col); // active bar
|
||||
paintShapes(pb_start_x_passive, pb_y, pb_passive_width, pb_height, pb_passive_col); // passive bar
|
||||
}
|
||||
|
||||
if (pb_paint_zero && pb_value == 0) //TODO: use shape cc-item, not available for lines yet
|
||||
frameBuffer->paintLine(pb_x , pb_y, pb_x+width-3, pb_y+height-3, pb_active_col); // zero line
|
||||
}
|
||||
private:
|
||||
// keys to lookup:
|
||||
int pb_height, pb_width;
|
||||
int pb_active_col, pb_passive_col;
|
||||
int design;
|
||||
bool pb_invert, gradient;
|
||||
int pb_red, pb_yellow, pb_green;
|
||||
|
||||
void CProgressBar::paintAdvanced()
|
||||
{
|
||||
int itemw = ITEMW, itemh = ITEMW, pointx = POINT, pointy = POINT;
|
||||
if(g_settings.progressbar_color){
|
||||
switch ((pb_color_t)g_settings.progressbar_design){
|
||||
default:
|
||||
case PB_MATRIX: // ::::: matrix
|
||||
break;
|
||||
case PB_LINES_V: // ||||| vert. lines
|
||||
itemh = pb_height;
|
||||
pointy = pb_height;
|
||||
break;
|
||||
case PB_LINES_H: // ===== horiz. lines
|
||||
itemw = POINT;
|
||||
break;
|
||||
case PB_COLOR: // filled color
|
||||
itemw = POINT;
|
||||
itemh = pb_height;
|
||||
pointy = pb_height;
|
||||
break;
|
||||
int yoff;
|
||||
|
||||
fb_pixel_t *active, *passive;
|
||||
|
||||
static inline unsigned int make16color(__u32 rgb){return 0xFF000000 | rgb;};
|
||||
void paintBoxRel(fb_pixel_t *b, int x, int y, int dx, int dy, fb_pixel_t col);
|
||||
void applyGradient(fb_pixel_t *buf);
|
||||
void createBitmaps();
|
||||
|
||||
CProgressBarCache(int _height, int _width, int _pb_active_col, int _pb_passive_col, int _design, bool _invert, bool _gradient, int _red, int _yellow, int _green)
|
||||
: pb_height(_height), pb_width(_width), pb_active_col(_pb_active_col), pb_passive_col(_pb_passive_col), design(_design), pb_invert(_invert), gradient(_gradient),
|
||||
pb_red(_red), pb_yellow(_yellow), pb_green(_green), yoff(0)
|
||||
{
|
||||
if (pbCache.size() > 10)
|
||||
clear();
|
||||
createBitmaps();
|
||||
}
|
||||
void clear();
|
||||
public:
|
||||
void paint(int x, int y, int pb_active_width, int pb_passive_width);
|
||||
static CProgressBarCache *lookup(int _height, int _width, int _pb_active_col, int _pb_passive_col, int _design, bool _invert, bool _gradient, int _red, int _yellow, int _green);
|
||||
};
|
||||
|
||||
void CProgressBarCache::clear()
|
||||
{
|
||||
for (std::vector<CProgressBarCache *>::iterator it = pbCache.begin(); it != pbCache.end(); ++it) {
|
||||
if ((*it)->active)
|
||||
free((*it)->active);
|
||||
if ((*it)->passive)
|
||||
free((*it)->passive);
|
||||
}
|
||||
|
||||
pbCache.clear();
|
||||
}
|
||||
|
||||
CProgressBarCache *CProgressBarCache::lookup(int _height, int _width, int _pb_active_col, int _pb_passive_col, int _design, bool _invert, bool _gradient, int _red, int _yellow, int _green)
|
||||
{
|
||||
// sanitize
|
||||
if (_design == CProgressBar::PB_MONO)
|
||||
_red = _yellow = _green = 0;
|
||||
|
||||
// lookup
|
||||
std::vector<CProgressBarCache *>::iterator it = pbCache.begin();
|
||||
for (; it != pbCache.end() && ((*it)->pb_height != _height || (*it)->pb_width != _width ||
|
||||
(*it)->pb_active_col != _pb_active_col || (*it)->pb_passive_col != _pb_passive_col ||
|
||||
(*it)->design != _design || (*it)->pb_invert != _invert || (*it)->gradient != _gradient ||
|
||||
(*it)->pb_red != _red || (*it)->pb_yellow != _yellow || (*it)->pb_green != _green); ++it);
|
||||
if (it != pbCache.end())
|
||||
return *it;
|
||||
|
||||
CProgressBarCache *pbc = new CProgressBarCache(_height, _width, _pb_active_col, _pb_passive_col, _design, _invert, _gradient, _red, _yellow, _green);
|
||||
pbCache.push_back(pbc);
|
||||
return pbc;
|
||||
}
|
||||
|
||||
void CProgressBarCache::paint(int x, int y, int pb_active_width, int pb_passive_width)
|
||||
{
|
||||
y += yoff;
|
||||
static CFrameBuffer *frameBuffer = CFrameBuffer::getInstance();
|
||||
unsigned int stride = frameBuffer->getStride() / sizeof(fb_pixel_t);
|
||||
fb_pixel_t *p = frameBuffer->getFrameBufferPointer() + y * stride + x;
|
||||
int off = stride - pb_width;
|
||||
if (pb_active_width > pb_width)
|
||||
pb_active_width = pb_width;
|
||||
if (pb_active_width + pb_passive_width != pb_width)
|
||||
pb_passive_width = pb_width - pb_active_width;
|
||||
fb_pixel_t *ap = active;
|
||||
fb_pixel_t *pp = passive;
|
||||
for (int h = 0; h < pb_height; h++) {
|
||||
int w = 0;
|
||||
for (; w < pb_active_width; w++, p++, ap++)
|
||||
if (*ap)
|
||||
*p = *ap;
|
||||
pp += pb_active_width;
|
||||
for (; w < pb_width; w++, p++, pp++)
|
||||
if (*pp)
|
||||
*p = *pp;
|
||||
ap += pb_passive_width;
|
||||
p += off;
|
||||
}
|
||||
}
|
||||
|
||||
void CProgressBarCache::paintBoxRel(fb_pixel_t *b, int x, int y, int dx, int dy, fb_pixel_t col)
|
||||
{
|
||||
if (x < 0) {
|
||||
dx -= x;
|
||||
x = 0;
|
||||
}
|
||||
if (y < 0) {
|
||||
dy -= y;
|
||||
y = 0;
|
||||
}
|
||||
if (x + dx > pb_width)
|
||||
dx = pb_width - x;
|
||||
if (y + dy > pb_height)
|
||||
dy = pb_height - y;
|
||||
if (dx < 1 || dy < 1)
|
||||
return;
|
||||
b += pb_width * y + x;
|
||||
fb_pixel_t *e = b + pb_width * (dy - 1) + dx;
|
||||
int off = pb_width - dx;
|
||||
while (b < e) {
|
||||
fb_pixel_t *ex = b + dx;
|
||||
while (b < ex)
|
||||
*b++ = col;
|
||||
b += off;
|
||||
}
|
||||
}
|
||||
|
||||
void CProgressBarCache::createBitmaps()
|
||||
{
|
||||
active = (fb_pixel_t *) calloc(1, pb_width * pb_height * sizeof(fb_pixel_t));
|
||||
if (!active)
|
||||
return;
|
||||
passive = (fb_pixel_t *) calloc(1, pb_width * pb_height * sizeof(fb_pixel_t));
|
||||
if (!passive) {
|
||||
free(active);
|
||||
return;
|
||||
}
|
||||
|
||||
int itemw = ITEMW, itemh = ITEMW, pointx = POINT, pointy = POINT;
|
||||
switch (design){
|
||||
default:
|
||||
case CProgressBar::PB_MONO: // monochrome
|
||||
paintBoxRel(active, 0, 0, pb_width, pb_height, pb_active_col );
|
||||
paintBoxRel(passive, 0, 0, pb_width, pb_height, pb_passive_col);
|
||||
if (gradient) {
|
||||
applyGradient(active);
|
||||
applyGradient(passive);
|
||||
}
|
||||
return;
|
||||
case CProgressBar::PB_MATRIX: // ::::: matrix
|
||||
break;
|
||||
case CProgressBar::PB_LINES_V: // ||||| vert. lines
|
||||
itemh = pb_height;
|
||||
pointy = pb_height;
|
||||
break;
|
||||
case CProgressBar::PB_LINES_H: // ===== horiz. lines
|
||||
itemw = POINT;
|
||||
break;
|
||||
case CProgressBar::PB_COLOR: // filled color
|
||||
itemw = 1;
|
||||
itemh = pb_height;
|
||||
pointy = pb_height;
|
||||
break;
|
||||
}
|
||||
|
||||
const int spc = itemh - pointy; /* space between horizontal lines / points */
|
||||
int hcnt = (pb_height + spc) / itemh; /* how many POINTs is the bar high */
|
||||
int yoff = (pb_height + spc - itemh * hcnt) / 2;
|
||||
|
||||
//printf("height: %d itemh: %d hcnt: %d yoff: %d spc: %d\n", height, itemh, hcnt, yoff, spc);
|
||||
yoff = (pb_height + spc - itemh * hcnt) / 2;
|
||||
|
||||
int i = 0;
|
||||
|
||||
int sh_x = 0;
|
||||
/* red, yellow, green are given in percent */
|
||||
int rd = pb_red * pb_max_width / (100 * itemw); /* how many POINTs red */
|
||||
int yw = pb_yellow * pb_max_width / (100 * itemw); /* how many POINTs yellow */
|
||||
int gn = pb_green * pb_max_width / (100 * itemw); /* how many POINTs green */
|
||||
int rd = pb_red * pb_width / (100 * itemw); /* how many POINTs red */
|
||||
int yw = pb_yellow * pb_width / (100 * itemw); /* how many POINTs yellow */
|
||||
int gn = pb_green * pb_width / (100 * itemw); /* how many POINTs green */
|
||||
|
||||
int maxi = pb_active_width / itemw; /* how many POINTs is the active bar */
|
||||
int total = pb_max_width / itemw; /* total number of POINTs */
|
||||
// fixup rounding errors
|
||||
while ((rd + yw + gn) * itemw < pb_width) {
|
||||
if (gn)
|
||||
gn++;
|
||||
if (yw && ((rd + yw + gn) * itemw < pb_width))
|
||||
yw++;
|
||||
if (rd && ((rd + yw + gn) * itemw < pb_width))
|
||||
rd++;
|
||||
}
|
||||
|
||||
uint32_t rgb;
|
||||
fb_pixel_t color;
|
||||
yw += rd;
|
||||
gn += yw;
|
||||
|
||||
if (pb_active_width != pb_last_width) {
|
||||
int i, j;
|
||||
const int py = pb_y + yoff;
|
||||
if (pb_active_width > pb_last_width) {
|
||||
int step, off;
|
||||
int b = 0;
|
||||
uint32_t diff = 0;
|
||||
for (i = 0; (i < rd) && (i < maxi); i++) {
|
||||
diff = i * 255 / rd;
|
||||
if (pb_invert)
|
||||
rgb = GREEN + (diff << 16); // adding red
|
||||
else
|
||||
rgb = RED + (diff << 8); // adding green
|
||||
color = make16color(rgb);
|
||||
for (j = 0; j < hcnt; j++) {
|
||||
int sh_x = pb_x + i * itemw;
|
||||
int sh_y = py + j * itemh;
|
||||
//paintShapes(sh_x, sh_y, pointx, pointy, color);
|
||||
frameBuffer->paintBoxRel(sh_x, sh_y, pointx, pointy, color);
|
||||
}
|
||||
}
|
||||
step = yw - rd - 1;
|
||||
if (step < 1)
|
||||
step = 1;
|
||||
for (; (i < yw) && (i < maxi); i++) {
|
||||
diff = b++ * 255 / step / 2;
|
||||
if (pb_invert)
|
||||
rgb = YELLOW - (diff << 8); // removing green
|
||||
else
|
||||
rgb = YELLOW - (diff << 16); // removing red
|
||||
color = make16color(rgb);
|
||||
for (j = 0; j < hcnt; j++) {
|
||||
int sh_x = pb_x + i * itemw;
|
||||
int sh_y = py + j * itemh;
|
||||
//paintShapes(sh_x, sh_y, pointx, pointy, color);
|
||||
frameBuffer->paintBoxRel(sh_x, sh_y, pointx, pointy, color);
|
||||
}
|
||||
}
|
||||
off = diff;
|
||||
b = 0;
|
||||
step = gn - yw - 1;
|
||||
if (step < 1)
|
||||
step = 1;
|
||||
for (; (i < gn) && (i < maxi); i++) {
|
||||
diff = b++ * 255 / step / 2 + off;
|
||||
if (pb_invert)
|
||||
rgb = YELLOW - (diff << 8); // removing green
|
||||
else
|
||||
rgb = YELLOW - (diff << 16); // removing red
|
||||
color = make16color(rgb);
|
||||
for (j = 0; j < hcnt; j++) {
|
||||
int sh_x = pb_x + i * itemw;
|
||||
int sh_y = py + j * itemh;
|
||||
//paintShapes(sh_x, sh_y, pointx, pointy, color);
|
||||
frameBuffer->paintBoxRel(sh_x, sh_y, pointx, pointy, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
for(i = maxi; i < total; i++) {
|
||||
for (j = 0; j < hcnt; j++) {
|
||||
int sh_x = pb_x + i * itemw;
|
||||
int sh_y = py + j * itemh;
|
||||
//paintShapes(sh_x, sh_y, pointx, pointy, pb_passive_col); //fill passive
|
||||
frameBuffer->paintBoxRel(sh_x, sh_y, pointx, pointy, pb_passive_col);
|
||||
uint32_t rgb, diff = 0;
|
||||
int step, b = 0;
|
||||
|
||||
for (i = 0; i < rd; i++, sh_x += itemw) {
|
||||
diff = i * 255 / rd;
|
||||
rgb = RED + (diff << 8); // adding green
|
||||
fb_pixel_t color = make16color(rgb);
|
||||
int sh_y = 0;
|
||||
for (int j = 0; j < hcnt; j++, sh_y += itemh)
|
||||
paintBoxRel(active, sh_x, sh_y, pointx, pointy, color);
|
||||
}
|
||||
step = yw - rd - 1;
|
||||
if (step < 1)
|
||||
step = 1;
|
||||
for (; i < yw; i++, sh_x += itemw) {
|
||||
diff = b++ * 255 / step / 2;
|
||||
rgb = YELLOW - (diff << 16); // removing red
|
||||
fb_pixel_t color = make16color(rgb);
|
||||
int sh_y = 0;
|
||||
for (int j = 0; j < hcnt; j++, sh_y += itemh)
|
||||
paintBoxRel(active, sh_x, sh_y, pointx, pointy, color);
|
||||
}
|
||||
int off = diff;
|
||||
b = 0;
|
||||
step = gn - yw - 1;
|
||||
if (step < 1)
|
||||
step = 1;
|
||||
for (; i < gn; i++, sh_x += itemw) {
|
||||
diff = b++ * 255 / step / 2 + off;
|
||||
rgb = YELLOW - (diff << 16); // removing red
|
||||
fb_pixel_t color = make16color(rgb);
|
||||
int sh_y = 0;
|
||||
for (int j = 0; j < hcnt; j++, sh_y += itemh)
|
||||
paintBoxRel(active, sh_x, sh_y, pointx, pointy, color);
|
||||
}
|
||||
if (pb_invert) {
|
||||
for (int l = 0; l < pb_height; l++) {
|
||||
fb_pixel_t *as = active + l * pb_width;
|
||||
fb_pixel_t *ae = as + pb_width - 1;
|
||||
for (; as < ae; as++, ae--) {
|
||||
fb_pixel_t t = *as;
|
||||
*as = *ae;
|
||||
*ae = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (gradient)
|
||||
applyGradient(active);
|
||||
|
||||
fb_pixel_t *a = active, *p = passive;
|
||||
fb_pixel_t *end = a + pb_width * pb_height;
|
||||
for (; a < end; a++, p++) {
|
||||
fb_pixel_t q = *a;
|
||||
unsigned int gray = ((q & 0xff) + ((q >> 8) & 0xff) + ((q >> 16) & 0xff)) / 3;
|
||||
q >>= 24;
|
||||
q <<= 8;
|
||||
q |= gray;
|
||||
q <<= 8;
|
||||
q |= gray;
|
||||
q <<= 8;
|
||||
q |= gray;
|
||||
*p = q;
|
||||
}
|
||||
}
|
||||
|
||||
void CProgressBarCache::applyGradient(fb_pixel_t *b)
|
||||
{
|
||||
for (int y = 0; y < pb_height; y++) {
|
||||
int _o = y * pb_width;
|
||||
fb_pixel_t last_old = 0;
|
||||
fb_pixel_t last_new = 0;
|
||||
for (int _x = pb_width - 1; _x > -1; _x--) {
|
||||
fb_pixel_t &v = *(b + _o + _x);
|
||||
if (v != last_old) {
|
||||
last_old = v;
|
||||
double s = sin((y + .5) * M_PI / pb_height) * .8 + .2;
|
||||
float fr = ((last_old >> 16) & 0xff) * s + 0.5;
|
||||
float fg = ((last_old >> 8) & 0xff) * s + 0.5;
|
||||
float fb = ((last_old ) & 0xff) * s + 0.5;
|
||||
last_new = (last_old & 0xFF000000)
|
||||
| ((unsigned int)fr << 16)
|
||||
| ((unsigned int)fg << 8)
|
||||
| ((unsigned int)fb );
|
||||
}
|
||||
v = last_new;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CProgressBar::paintProgress(bool do_save_bg)
|
||||
{
|
||||
if(pb_bl_changed != g_settings.progressbar_color) {
|
||||
pb_bl_changed = g_settings.progressbar_color;
|
||||
reset();
|
||||
if (*pb_design == PB_OFF) {
|
||||
paintInit(false);
|
||||
return;
|
||||
}
|
||||
if (pb_type == PB_TIMESCALE)
|
||||
setRgb(g_settings.progressbar_timescale_red, g_settings.progressbar_timescale_green, g_settings.progressbar_timescale_yellow);
|
||||
|
||||
if (!pb_red && !pb_yellow && !pb_green)
|
||||
pb_green = 1;
|
||||
int sum = pb_red + pb_yellow + pb_green;
|
||||
pb_red *= 100;
|
||||
pb_yellow *= 100;
|
||||
pb_green *= 100;
|
||||
pb_red /= sum;
|
||||
pb_yellow /= sum;
|
||||
pb_green /= sum;
|
||||
|
||||
if (*pb_gradient)
|
||||
setFrameThickness(0);
|
||||
|
||||
initDimensions();
|
||||
|
||||
@@ -264,10 +421,13 @@ void CProgressBar::paintProgress(bool do_save_bg)
|
||||
paintInit(do_save_bg);
|
||||
|
||||
//progress
|
||||
if (!pb_blink || !g_settings.progressbar_color)
|
||||
paintSimple();
|
||||
else
|
||||
paintAdvanced();
|
||||
bool pb_invert = (pb_type == PB_REDRIGHT) || ((pb_type == PB_TIMESCALE) && g_settings.progressbar_timescale_invert);
|
||||
if (pb_active_width != pb_last_width) {
|
||||
CProgressBarCache *pbc = CProgressBarCache::lookup(pb_height, pb_max_width, pb_active_col, pb_passive_col, *pb_design, pb_invert, *pb_gradient, pb_red, pb_yellow, pb_green);
|
||||
if (pbc)
|
||||
pbc->paint(pb_x, pb_y, pb_active_width, pb_passive_width);
|
||||
is_painted = true;
|
||||
}
|
||||
|
||||
if (is_painted)
|
||||
pb_last_width = pb_active_width;
|
||||
@@ -276,6 +436,10 @@ void CProgressBar::paintProgress(bool do_save_bg)
|
||||
|
||||
void CProgressBar::paint(bool do_save_bg)
|
||||
{
|
||||
|
||||
paintProgress(do_save_bg);
|
||||
}
|
||||
|
||||
void CProgressBar::setType(pb_type_t type)
|
||||
{
|
||||
pb_type = type;
|
||||
}
|
||||
|
Reference in New Issue
Block a user