13
0

working canvas meter code

This commit is contained in:
Paul Davis 2016-09-25 10:50:03 -05:00
parent 7a0e2ceec0
commit e1fb7fe9fc
2 changed files with 62 additions and 51 deletions

View File

@ -28,12 +28,13 @@
#include <glibmm.h> #include <glibmm.h>
#include <gtkmm2ext/utils.h> #include <gtkmm2ext/utils.h>
#include <gtkmm2ext/rgb_macros.h>
#include "canvas/canvas.h" #include "canvas/canvas.h"
#include "meter.h" #include "canvas/utils.h"
#include "canvas/colors.h"
#define UINT_TO_RGB(u,r,g,b) { (*(r)) = ((u)>>16)&0xff; (*(g)) = ((u)>>8)&0xff; (*(b)) = (u)&0xff; } #include "meter.h"
#define UINT_TO_RGBA(u,r,g,b,a) { UINT_TO_RGB(((u)>>8),r,g,b); (*(a)) = (u)&0xff; }
using namespace Glib; using namespace Glib;
using namespace Gtkmm2ext; using namespace Gtkmm2ext;
@ -112,13 +113,13 @@ Meter::Meter (Item* parent, long hold, unsigned long dimen, Orientation o, int l
if (orientation == Vertical) { if (orientation == Vertical) {
pixheight = len; pixheight = len;
pixwidth = dimen; pixwidth = dimen;
fgpattern = request_vertical_meter(pixwidth + 2, pixheight + 2, _clr, _stp, _styleflags); fgpattern = vertical_meter_pattern (pixwidth + 2, pixheight + 2, _clr, _stp, _styleflags);
bgpattern = request_vertical_background (pixwidth + 2, pixheight + 2, _bgc, false); bgpattern = vertical_background (pixwidth + 2, pixheight + 2, _bgc, false);
} else { } else {
pixheight = dimen; pixheight = dimen;
pixwidth = len; pixwidth = len;
fgpattern = request_horizontal_meter(pixwidth + 2, pixheight + 2, _clr, _stp, _styleflags); fgpattern = horizontal_meter_pattern (pixwidth + 2, pixheight + 2, _clr, _stp, _styleflags);
bgpattern = request_horizontal_background (pixwidth + 2, pixheight + 2, _bgc, false); bgpattern = horizontal_background (pixwidth + 2, pixheight + 2, _bgc, false);
} }
pixrect.width = pixwidth; pixrect.width = pixwidth;
@ -347,7 +348,7 @@ Meter::generate_meter_background (int width, int height, int *clr, bool shade, b
} }
Cairo::RefPtr<Cairo::Pattern> Cairo::RefPtr<Cairo::Pattern>
Meter::request_vertical_meter (int width, int height, int *clr, float *stp, int styleflags) Meter::vertical_meter_pattern (int width, int height, int *clr, float *stp, int styleflags)
{ {
height = max(height, min_pattern_metric_size); height = max(height, min_pattern_metric_size);
height = min(height, max_pattern_metric_size); height = min(height, max_pattern_metric_size);
@ -364,15 +365,14 @@ Meter::request_vertical_meter (int width, int height, int *clr, float *stp, int
} }
// TODO flush pattern cache if it gets too large // TODO flush pattern cache if it gets too large
Cairo::RefPtr<Cairo::Pattern> p = generate_meter_pattern ( Cairo::RefPtr<Cairo::Pattern> p = generate_meter_pattern (width, height, clr, stp, styleflags, false);
width, height, clr, stp, styleflags, false);
vm_pattern_cache[key] = p; vm_pattern_cache[key] = p;
return p; return p;
} }
Cairo::RefPtr<Cairo::Pattern> Cairo::RefPtr<Cairo::Pattern>
Meter::request_vertical_background (int width, int height, int *bgc, bool shade) Meter::vertical_background (int width, int height, int *bgc, bool shade)
{ {
height = max(height, min_pattern_metric_size); height = max(height, min_pattern_metric_size);
height = min(height, max_pattern_metric_size); height = min(height, max_pattern_metric_size);
@ -380,20 +380,20 @@ Meter::request_vertical_background (int width, int height, int *bgc, bool shade)
const PatternBgMapKey key (width, height, bgc[0], bgc[1], shade); const PatternBgMapKey key (width, height, bgc[0], bgc[1], shade);
PatternBgMap::iterator i; PatternBgMap::iterator i;
if ((i = vb_pattern_cache.find (key)) != vb_pattern_cache.end()) { if ((i = vb_pattern_cache.find (key)) != vb_pattern_cache.end()) {
return i->second; return i->second;
} }
// TODO flush pattern cache if it gets too large // TODO flush pattern cache if it gets too large
Cairo::RefPtr<Cairo::Pattern> p = generate_meter_background ( Cairo::RefPtr<Cairo::Pattern> p = generate_meter_background (width, height, bgc, shade, false);
width, height, bgc, shade, false);
vb_pattern_cache[key] = p; vb_pattern_cache[key] = p;
return p; return p;
} }
Cairo::RefPtr<Cairo::Pattern> Cairo::RefPtr<Cairo::Pattern>
Meter::request_horizontal_meter (int width, int height, int *clr, float *stp, int styleflags) Meter::horizontal_meter_pattern (int width, int height, int *clr, float *stp, int styleflags)
{ {
width = max(width, min_pattern_metric_size); width = max(width, min_pattern_metric_size);
width = min(width, max_pattern_metric_size); width = min(width, max_pattern_metric_size);
@ -410,16 +410,14 @@ Meter::request_horizontal_meter (int width, int height, int *clr, float *stp, in
} }
// TODO flush pattern cache if it gets too large // TODO flush pattern cache if it gets too large
Cairo::RefPtr<Cairo::Pattern> p = generate_meter_pattern ( Cairo::RefPtr<Cairo::Pattern> p = generate_meter_pattern (height, width, clr, stp, styleflags, true);
height, width, clr, stp, styleflags, true);
hm_pattern_cache[key] = p; hm_pattern_cache[key] = p;
return p; return p;
} }
Cairo::RefPtr<Cairo::Pattern> Cairo::RefPtr<Cairo::Pattern>
Meter::request_horizontal_background( Meter::horizontal_background (int width, int height, int *bgc, bool shade)
int width, int height, int *bgc, bool shade)
{ {
width = max(width, min_pattern_metric_size); width = max(width, min_pattern_metric_size);
width = min(width, max_pattern_metric_size); width = min(width, max_pattern_metric_size);
@ -432,8 +430,7 @@ Meter::request_horizontal_background(
} }
// TODO flush pattern cache if it gets too large // TODO flush pattern cache if it gets too large
Cairo::RefPtr<Cairo::Pattern> p = generate_meter_background ( Cairo::RefPtr<Cairo::Pattern> p = generate_meter_background (height, width, bgc, shade, true);
height, width, bgc, shade, true);
hb_pattern_cache[key] = p; hb_pattern_cache[key] = p;
@ -493,6 +490,11 @@ Meter::vertical_expose (ArdourCanvas::Rect const & area, Cairo::RefPtr<Cairo::Co
background.width = pixrect.width; background.width = pixrect.width;
background.height = pixheight - top_of_meter; background.height = pixheight - top_of_meter;
/* translate so that item coordinates match window coordinates */
Duple origin (0, 0);
origin = item_to_window (origin);
context->translate (origin.x, origin.y);
Cairo::RefPtr<Cairo::Region> r1 = Cairo::Region::create (area_r); Cairo::RefPtr<Cairo::Region> r1 = Cairo::Region::create (area_r);
r1->intersect (background); r1->intersect (background);
@ -542,6 +544,8 @@ Meter::vertical_expose (ArdourCanvas::Rect const & area, Cairo::RefPtr<Cairo::Co
last_peak_rect.width = 0; last_peak_rect.width = 0;
last_peak_rect.height = 0; last_peak_rect.height = 0;
} }
context->translate (-origin.x, -origin.y);
} }
void void
@ -551,48 +555,61 @@ Meter::horizontal_expose (ArdourCanvas::Rect const & area, Cairo::RefPtr<Cairo::
Cairo::RectangleInt background; Cairo::RectangleInt background;
Cairo::RectangleInt area_r; Cairo::RectangleInt area_r;
/* convert expose area back to item coordinate space */
Rect area2 = window_to_item (area); Rect area2 = window_to_item (area);
/* create a Cairo object so that we can use intersect and Region */
area_r.x = area2.x0; area_r.x = area2.x0;
area_r.y = area2.y0; area_r.y = area2.y0;
area_r.width = area2.width(); area_r.width = area2.width();
area_r.height = area2.height(); area_r.height = area2.height();
/* draw the edge (rounded corners) */
context->set_source_rgb (0, 0, 0); // black context->set_source_rgb (0, 0, 0); // black
rounded_rectangle (context, 0, 0, pixwidth + 2, pixheight + 2, 2); rounded_rectangle (context, 0, 0, pixwidth + 2, pixheight + 2, 2);
context->stroke (); context->stroke ();
/* horizontal meter extends from left to right. Compute the right edge */
right_of_meter = (gint) floor (pixwidth * current_level); right_of_meter = (gint) floor (pixwidth * current_level);
/* reset the height & origin of the rect that needs to show the pixbuf /* reset the width the rect that needs to show the pattern of the meter */
*/
pixrect.width = right_of_meter; pixrect.width = right_of_meter;
/* compute a rect for the part of the meter that is all background */
background.x = 1 + right_of_meter; background.x = 1 + right_of_meter;
background.y = 1; background.y = 1;
background.width = pixwidth - right_of_meter; background.width = pixwidth - right_of_meter;
background.height = pixheight; background.height = pixheight;
Cairo::RefPtr<Cairo::Region> r1 = Cairo::Region::create (area_r); /* translate so that item coordinates match window coordinates */
r1->intersect (background); Duple origin (0, 0);
origin = item_to_window (origin);
context->translate (origin.x, origin.y);
if (!r1->empty()) { Cairo::RefPtr<Cairo::Region> r;
Cairo::RectangleInt i (r1->get_extents ());
context->rectangle (i.x, i.y, i.width, i.height); r = Cairo::Region::create (area_r);
r->intersect (background);
if (!r->empty()) {
/* draw the background part */
Cairo::RectangleInt i (r->get_extents ());
context->set_source (bgpattern); context->set_source (bgpattern);
context->rectangle (i.x, i.y, i.width, i.height);
context->fill (); context->fill ();
} }
Cairo::RefPtr<Cairo::Region> r2 = Cairo::Region::create (area_r); r = Cairo::Region::create (area_r);
r2->intersect (pixrect); r->intersect (pixrect);
if (!r2->empty()) { if (!r->empty()) {
// draw the part of the meter image that we need. the area we draw is bounded "in reverse" (top->bottom) // draw the part of the meter image that we need.
Cairo::RectangleInt i (r2->get_extents ()); Cairo::RectangleInt i (r->get_extents ());
cerr << "h-render-fg: " << i.x << ", " << i.y << ' ' << i.width << " + " << i.height << endl; Duple d (i.x, i.y);
context->rectangle (i.x, i.y, i.width, i.height);
context->set_source (fgpattern); context->set_source (fgpattern);
context->rectangle (i.x, i.y, i.width, i.height);
context->fill (); context->fill ();
} }
@ -622,6 +639,8 @@ Meter::horizontal_expose (ArdourCanvas::Rect const & area, Cairo::RefPtr<Cairo::
last_peak_rect.width = 0; last_peak_rect.width = 0;
last_peak_rect.height = 0; last_peak_rect.height = 0;
} }
context->translate (-origin.x, -origin.y);
} }
void void
@ -827,9 +846,9 @@ Meter::set_highlight (bool onoff)
} }
highlight = onoff; highlight = onoff;
if (orientation == Vertical) { if (orientation == Vertical) {
bgpattern = request_vertical_background (pixwidth + 2, pixheight + 2, highlight ? _bgh : _bgc, highlight); bgpattern = vertical_background (pixwidth + 2, pixheight + 2, highlight ? _bgh : _bgc, highlight);
} else { } else {
bgpattern = request_horizontal_background (pixwidth + 2, pixheight + 2, highlight ? _bgh : _bgc, highlight); bgpattern = horizontal_background (pixwidth + 2, pixheight + 2, highlight ? _bgh : _bgc, highlight);
} }
redraw (); redraw ();
} }

View File

@ -87,8 +87,6 @@ class Meter : public ArdourCanvas::Item {
Orientation orientation; Orientation orientation;
mutable Cairo::RectangleInt pixrect; mutable Cairo::RectangleInt pixrect;
mutable Cairo::RectangleInt last_peak_rect; mutable Cairo::RectangleInt last_peak_rect;
gint request_width;
gint request_height;
unsigned long hold_cnt; unsigned long hold_cnt;
unsigned long hold_state; unsigned long hold_state;
bool bright_hold; bool bright_hold;
@ -105,19 +103,13 @@ class Meter : public ArdourCanvas::Item {
static bool no_rgba_overlay; static bool no_rgba_overlay;
static Cairo::RefPtr<Cairo::Pattern> generate_meter_pattern ( static Cairo::RefPtr<Cairo::Pattern> generate_meter_pattern (int, int, int *, float *, int, bool);
int, int, int *, float *, int, bool); static Cairo::RefPtr<Cairo::Pattern> vertical_meter_pattern (int, int, int *, float *, int);
static Cairo::RefPtr<Cairo::Pattern> request_vertical_meter ( static Cairo::RefPtr<Cairo::Pattern> horizontal_meter_pattern (int, int, int *, float *, int);
int, int, int *, float *, int);
static Cairo::RefPtr<Cairo::Pattern> request_horizontal_meter (
int, int, int *, float *, int);
static Cairo::RefPtr<Cairo::Pattern> generate_meter_background ( static Cairo::RefPtr<Cairo::Pattern> generate_meter_background (int, int, int *, bool, bool);
int, int, int *, bool, bool); static Cairo::RefPtr<Cairo::Pattern> vertical_background (int, int, int *, bool);
static Cairo::RefPtr<Cairo::Pattern> request_vertical_background ( static Cairo::RefPtr<Cairo::Pattern> horizontal_background (int, int, int *, bool);
int, int, int *, bool);
static Cairo::RefPtr<Cairo::Pattern> request_horizontal_background (
int, int, int *, bool);
struct Pattern10MapKey { struct Pattern10MapKey {
Pattern10MapKey ( Pattern10MapKey (