merge neutrino-mp progressbar code, (C) martii

This commit is contained in:
[CST] Focus
2014-07-17 15:15:43 +04:00
parent 7cee5d3b2e
commit 416646bbff
23 changed files with 623 additions and 271 deletions

View File

@@ -123,11 +123,8 @@ void CSignalBar::initVarSigBar()
void CSignalBar::initSBarScale()
{
//create scale object if required
if (sb_scale == NULL){
if (sb_scale == NULL)
sb_scale = new CProgressBar();
//we want colored scale!
sb_scale->setBlink();
}
//move and set dimensions
int scale_y = (sb_item_height/2 - sb_scale_height/2);

View File

@@ -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 &shy, 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;
}

View File

@@ -43,7 +43,6 @@
frame_col > general frame color of progressbar, set 0 for no frame
shadowbar_col color > shadow behind progressbar, set 0 for no shadow
paintZero > optional, if set to true and value = 0, then paints a diagonal line instead of active bar as symbolic for a zero value
*/
#ifndef __CC_PROGRESSBAR_H__
@@ -80,86 +79,80 @@ class CProgressBar : public CComponentsItem
///start position of activ/passiv area
int pb_x, pb_y;
bool pb_blink, pb_invert, pb_bl_changed;
///causes a diagonal line as a sign for 0 value instead of an empty bar
bool pb_paint_zero;
///to evaluate values, these will be convert to the graph
int pb_value, pb_max_value;
///paint simple version of progressbar with simple color modifications
void paintSimple();
///paint version of progressbar with color and advanced display modifications
void paintAdvanced();
///painting of activ/passive bars via shape object
void paintShapes(int &shx, int &shy, int &shw, int &shh, fb_pixel_t &col);
int *pb_design, *pb_gradient;
int pb_type;
void initDimensions();
///paints graph
void paintProgress(bool do_save_bg = CC_SAVE_SCREEN_NO);
static inline unsigned int make16color(__u32 rgb){return 0xFF000000 | rgb;};
public:
///parameters:
///x_pos, y_pos, w, h: position and dimension in pixel
///blinkenlights: true if you want code to follow progressbar_color. needed, no default.
///w, h: width / height of bar. Can later be set with paintProgressbar.
///r, g, b: percentage of the bar where red/green/yellow is used, only used if blinkenlights (colored) == true.
///inv: false => red on the left side, true: red on right side.
///r, g, b: percentage of the bar where red/green/yellow is used, only used for colored designs
///active_col, passive_col: sets colors for displayed values, activ_col means the the displayed progress
///color_frame, color_body, color_shadow: colores of progressbar for frame, body and shadow, Note: color of frame is ineffective on fr_thickness = 0
CProgressBar( const int x_pos = 0, const int y_pos = 0,
const int w = -1, const int h = -1,
fb_pixel_t color_frame = 0, fb_pixel_t color_body = COL_MENUCONTENT_PLUS_0, fb_pixel_t color_shadow = COL_MENUCONTENTDARK_PLUS_0,
const fb_pixel_t active_col = COL_INFOBAR_PLUS_7, const fb_pixel_t passive_col = COL_INFOBAR_PLUS_3,
const bool blinkenlights = false,
const int r = 40, const int g = 100, const int b =70,
const bool inv = false,
CComponentsForm *parent = NULL);
///set up to display available values
void setValue(const int val){ pb_value = val;};
void setMaxValue(const int max_val){pb_max_value = max_val;};
void setValue(const int val){ pb_value = val;}
//return current value
int getValue(void) { return pb_value; }
void setMaxValue(const int max_val){pb_max_value = max_val;}
///set up booth values to display at once
void setValues(const int val, const int max_val){pb_value = val; pb_max_value = max_val;};
///causes painting a diagonal line if value = 0, Note: ineffective in colored mode
void setZeroLine(bool paint_zero_line = true){pb_paint_zero = paint_zero_line;};
void setValues(const int val, const int max_val){pb_value = val; pb_max_value = max_val;}
///setters for status colors
void setActiveColor(fb_pixel_t active_color) {pb_active_col = active_color;};
void setPassiveColor(fb_pixel_t passive_color) {pb_passive_col = passive_color;};
void setActiveColor(fb_pixel_t active_color) {pb_active_col = active_color;}
void setPassiveColor(fb_pixel_t passive_color) {pb_passive_col = passive_color;}
///set up booth status colors at once
void setStatusColors(fb_pixel_t active_color, fb_pixel_t passive_color) {pb_passive_col = passive_color; pb_active_col = active_color;};
void setStatusColors(fb_pixel_t active_color, fb_pixel_t passive_color) {pb_passive_col = passive_color; pb_active_col = active_color;}
///invert: false => red on the left side, true: red on right side.
void setInvert(bool inverted = true){pb_invert = inverted;};
///blinkenlights: true (default) if you want code to follow progressbar_color.
void setBlink(bool blinkenlights = true){pb_blink = blinkenlights;};
///r, g, b: percentage of the bar where red/green/yellow is used, only used if blinkenlights (colored) == true.
void setRgb(const int r, const int g, const int b){pb_red = r; pb_green = g; pb_yellow = b;};
///r, g, b: percentage of the bar where red/green/yellow is used, only used for colored designs
void setRgb(const int r, const int g, const int b){pb_red = r; pb_green = g; pb_yellow = b;}
///x, y, width, height, value, max_value: set most wanted parameters at once
void setProgress(const int x_pos, const int y_pos,
const int w, const int h,
const int val, const int max_val){x=x_pos; y=y_pos; width=w; height=h; pb_value=val; pb_max_value=max_val;};
const int val, const int max_val){x=x_pos; y=y_pos; width=w; height=h; pb_value=val; pb_max_value=max_val;}
///force update on next paint
void reset() { pb_last_width = -1; }
void paint(bool do_save_bg = CC_SAVE_SCREEN_NO);
enum pb_color_t {
PB_MATRIX = 0, /* 0 */
PB_LINES_V, /* 1 */
PB_LINES_H, /* 2 */
PB_COLOR /* 3 */
PB_OFF = -2, /* -2 */
PB_MONO, /* -1 */
PB_MATRIX, /* 0 */
PB_LINES_V, /* 1 */
PB_LINES_H, /* 2 */
PB_COLOR, /* 3 */
};
enum pb_type_t {
PB_REDLEFT = 0,
PB_REDRIGHT,
PB_TIMESCALE,
};
void setType(pb_type_t type);
//set design (overides g_settings.progressbar_design)
void setDesign(int &design) { pb_design = &design; }
//set gradient (overides g_settings.progressbar_gradient)
void setGradient(int &gradient) { pb_gradient = &gradient; }
};
#endif /* __CC_PROGRESSBAR_H__ */