From d521dd29b038472aa1d2269def96fdf6663cf1b0 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 7 Mar 2020 21:46:19 +0100 Subject: WIP: Experimental highlight stretching. --- plugingui/drumkittab.cc | 30 ++++++++++++-------- plugingui/knob.cc | 2 +- plugingui/painter.cc | 73 ++++++++++++++++++++++++++++++++++++++++++++++++- plugingui/painter.h | 24 ++++++++++++++-- 4 files changed, 114 insertions(+), 15 deletions(-) (limited to 'plugingui') diff --git a/plugingui/drumkittab.cc b/plugingui/drumkittab.cc index 701fcff..7be0562 100644 --- a/plugingui/drumkittab.cc +++ b/plugingui/drumkittab.cc @@ -80,7 +80,7 @@ void DrumkitTab::resize(std::size_t width, std::size_t height) *drumkit_image, drumkit_image->width() * drumkit_scale, drumkit_image->height() * drumkit_scale, - Filter::Linear); + Filter::Nearest); } velocity_label.move(10, height-velocity_label.height()-5); @@ -101,7 +101,7 @@ void DrumkitTab::buttonEvent(ButtonEvent* buttonEvent) *map_image, drumkit_image->width() * drumkit_scale, drumkit_image->height() * drumkit_scale, - Filter::Linear); + Filter::Nearest); shows_overlay = true; redraw(); return; @@ -115,7 +115,7 @@ void DrumkitTab::buttonEvent(ButtonEvent* buttonEvent) *drumkit_image, drumkit_image->width() * drumkit_scale, drumkit_image->height() * drumkit_scale, - Filter::Linear); + Filter::Nearest); highlightInstrument(current_index); @@ -145,14 +145,14 @@ void DrumkitTab::buttonEvent(ButtonEvent* buttonEvent) *drumkit_image, drumkit_image->width() * drumkit_scale, drumkit_image->height() * drumkit_scale, - Filter::Linear); + Filter::Nearest); if(shows_overlay) { painter.drawImageStretched(drumkit_image_x, drumkit_image_y, *map_image, drumkit_image->width() * drumkit_scale, drumkit_image->height() * drumkit_scale, - Filter::Linear); + Filter::Nearest); } highlightInstrument(current_index); redraw(); @@ -189,7 +189,7 @@ void DrumkitTab::mouseMoveEvent(MouseMoveEvent* mouseMoveEvent) *drumkit_image, drumkit_image->width() * drumkit_scale, drumkit_image->height() * drumkit_scale, - Filter::Linear); + Filter::Nearest); if(shows_overlay) { @@ -197,7 +197,7 @@ void DrumkitTab::mouseMoveEvent(MouseMoveEvent* mouseMoveEvent) *map_image, drumkit_image->width() * drumkit_scale, drumkit_image->height() * drumkit_scale, - Filter::Linear); + Filter::Nearest); } highlightInstrument(index); @@ -215,7 +215,7 @@ void DrumkitTab::mouseLeaveEvent() *drumkit_image, drumkit_image->width() * drumkit_scale, drumkit_image->height() * drumkit_scale, - Filter::Linear); + Filter::Nearest); shows_overlay = false; redraw(); @@ -250,9 +250,17 @@ void DrumkitTab::highlightInstrument(int index) Painter painter(*this); auto const& colour = colours[index]; //Colour colour(1.0f, 1.0f, 0.0f); - auto const& positions = colour_index_to_positions[index]; - painter.draw(positions.begin(), positions.end(), - drumkit_image_x, drumkit_image_y, colour, drumkit_scale); + //auto const& positions = colour_index_to_positions[index]; +// painter.draw(positions.begin(), positions.end(), +// drumkit_image_x, drumkit_image_y, colour, drumkit_scale); + + painter.drawRestrictedImageStretched(drumkit_image_x, drumkit_image_y, + colour, + *map_image, + drumkit_image->width() * drumkit_scale, + drumkit_image->height() * drumkit_scale, + Filter::Nearest); + shows_instrument_overlay = true; } else diff --git a/plugingui/knob.cc b/plugingui/knob.cc index 25200a7..d61abac 100644 --- a/plugingui/knob.cc +++ b/plugingui/knob.cc @@ -204,7 +204,7 @@ void Knob::repaintEvent(RepaintEvent* repaintEvent) sprintf(buf, "%.2f", current_value * range + minimum); } p.drawText(center_x - font.textWidth(buf) / 2 + 1, - center_y + font.textHeight(buf) / 2 + 1, font, buf); + center_y + font.textHeight(buf) / 2 + 1, font, buf); } // Make it start from 20% and stop at 80% diff --git a/plugingui/painter.cc b/plugingui/painter.cc index ba1e621..cc7c66e 100644 --- a/plugingui/painter.cc +++ b/plugingui/painter.cc @@ -452,7 +452,7 @@ void Painter::drawRestrictedImage(int x0, int y0, assert(x0 + x < pixbuf.width); assert(y0 + y < pixbuf.height); - if(c == restriction_colour) + if(c == restriction_colour && c.alpha() > 0) { pixbuf.setPixel(x0 + x, y0 + y, c); } @@ -460,6 +460,77 @@ void Painter::drawRestrictedImage(int x0, int y0, } } +void Painter::drawRestrictedImageStretched(int x0, int y0, + const Colour& restriction_colour, + const Drawable& image, + int width, int height, Filter filter) +{ + float fw = image.width(); + float fh = image.height(); + + // Make sure we don't try to draw outside the pixbuf. + if(width > (int)(pixbuf.width - x0)) + { + width = pixbuf.width - x0; + } + + if(height > (int)(pixbuf.height - y0)) + { + height = pixbuf.height - y0; + } + + if((width < 1) || (height < 1)) + { + return; + } + + switch(filter) + { + case Filter::Nearest: + for(int y = -1 * std::min(0, y0); y < height; ++y) + { + for(int x = -1 * std::min(0, x0); x < width; ++x) + { + int lx = ((float)x / (float)width) * fw; + int ly = ((float)y / (float)height) * fh; + auto& c = image.getPixel(lx, ly); + if(c == restriction_colour && c.alpha() > 0) + { + pixbuf.addPixel(x0 + x, y0 + y, restriction_colour); + } + } + } + break; + + case Filter::Linear: + for(int y = -1 * std::min(0, y0); y < height; ++y) + { + for(int x = -1 * std::min(0, x0); x < width; ++x) + { + float lx = ((float)x / (float)width) * fw; + float ly = ((float)y / (float)height) * fh; + float px = lx - std::floor(lx); + float py = ly - std::floor(ly); + //printf("px: %f, py: %f\n", px, py); + // 1,1 2,1 + // 1,2 2,2 + auto c11 = image.getPixel(std::floor(lx), std::floor(ly)); + auto c12 = image.getPixel(std::floor(lx), std::ceil(ly)); + auto c21 = image.getPixel(std::ceil(lx), std::floor(ly)); + auto c22 = image.getPixel(std::ceil(lx), std::ceil(ly)); + auto c1 = c11 * (1 - px) + c21 * px; + auto c2 = c12 * (1 - px) + c22 * px; + auto c = c1 * (1 - py) + c2 * py; + if(c == restriction_colour && c.alpha() > 0) + { + pixbuf.addPixel(x0 + x, y0 + y, restriction_colour); + } + } + } + break; + } +} + void Painter::drawImageStretched(int x0, int y0, const Drawable& image, int width, int height, Filter filter) { diff --git a/plugingui/painter.h b/plugingui/painter.h index 3bdbe2d..a1ea6f1 100644 --- a/plugingui/painter.h +++ b/plugingui/painter.h @@ -27,6 +27,7 @@ #pragma once #include +#include #include "colour.h" #include "pixelbuffer.h" @@ -64,6 +65,10 @@ public: void drawImage(int x, int y, const Drawable& image); void drawRestrictedImage(int x0, int y0, const Colour& restriction_colour, const Drawable& image); + void drawRestrictedImageStretched(int x0, int y0, + const Colour& restriction_colour, + const Drawable& image, + int width, int height, Filter filter); void drawImageStretched(int x, int y, const Drawable& image, int width, int height, Filter filter = Filter::Nearest); @@ -101,9 +106,24 @@ private: template void Painter::draw(Iterator begin, Iterator end, int x_offset, int y_offset, Colour const& colour, double scale) { - for (auto it = begin; it != end; ++it) + if(scale > 1) { - pixbuf.addPixel(x_offset + it->x * scale, y_offset + it->y * scale, colour); + auto tmp = this->colour; + setColour(colour); + for (auto it = begin; it != end; ++it) + { + drawFilledCircle(x_offset + it->x * scale, y_offset + it->y * scale, + std::ceil(scale)); + } + setColour(tmp); + } + else + { + for (auto it = begin; it != end; ++it) + { + pixbuf.addPixel(x_offset + it->x * scale, y_offset + it->y * scale, + colour); + } } } -- cgit v1.2.3