From a65ca7af57feaf7a4ea73f7e071a72eb9b3a1e24 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 7 Nov 2015 10:39:46 +0100 Subject: Refactored Painter. --- plugingui/button.cc | 6 +- plugingui/checkbox.cc | 14 +- plugingui/combobox.cc | 4 +- plugingui/eventhandler.cc | 44 +-- plugingui/eventhandler.h | 6 +- plugingui/filebrowser.cc | 4 +- plugingui/font.cc | 6 +- plugingui/font.h | 11 +- plugingui/image.cc | 6 +- plugingui/image.h | 6 +- plugingui/knob.cc | 4 +- plugingui/label.cc | 2 +- plugingui/layout.h | 4 +- plugingui/led.cc | 2 +- plugingui/lineedit.cc | 4 +- plugingui/listbox.cc | 4 +- plugingui/listboxbasic.cc | 4 +- plugingui/listboxthin.cc | 4 +- plugingui/painter.cc | 761 ++++++++++++++++++++++------------------------ plugingui/painter.h | 89 +++--- plugingui/plugingui.cc | 2 +- plugingui/progressbar.cc | 6 +- plugingui/scrollbar.cc | 4 +- plugingui/slider.cc | 2 +- plugingui/verticalline.cc | 4 +- plugingui/widget.cc | 6 +- plugingui/window.cc | 9 +- 27 files changed, 496 insertions(+), 522 deletions(-) diff --git a/plugingui/button.cc b/plugingui/button.cc index be5f82e..9248242 100644 --- a/plugingui/button.cc +++ b/plugingui/button.cc @@ -106,7 +106,7 @@ void Button::buttonEvent(ButtonEvent *e) void Button::repaintEvent(RepaintEvent *e) { - Painter p(this); + Painter p(*this); p.clear(); @@ -119,10 +119,10 @@ void Button::repaintEvent(RepaintEvent *e) switch(draw_state) { case up: - p.drawBox(0, 0, &box_up, w, h); + p.drawBox(0, 0, box_up, w, h); break; case down: - p.drawBox(0, 0, &box_down, w, h); + p.drawBox(0, 0, box_down, w, h); break; } diff --git a/plugingui/checkbox.cc b/plugingui/checkbox.cc index 6d85615..b9468d3 100644 --- a/plugingui/checkbox.cc +++ b/plugingui/checkbox.cc @@ -83,32 +83,32 @@ void CheckBox::keyEvent(KeyEvent *e) void CheckBox::repaintEvent(RepaintEvent *e) { - Painter p(this); + Painter p(*this); p.clear(); if(state) { - p.drawImage(0, (knob.height() - bg_on.height()) / 2, &bg_on); + p.drawImage(0, (knob.height() - bg_on.height()) / 2, bg_on); if(middle) { - p.drawImage((bg_on.width() - knob.width()) / 2 + 1, 0, &knob); + p.drawImage((bg_on.width() - knob.width()) / 2 + 1, 0, knob); } else { - p.drawImage(bg_on.width() - 40 + 2, 0, &knob); + p.drawImage(bg_on.width() - 40 + 2, 0, knob); } } else { - p.drawImage(0, (knob.height() - bg_off.height()) / 2, &bg_off); + p.drawImage(0, (knob.height() - bg_off.height()) / 2, bg_off); if(middle) { - p.drawImage((bg_on.width() - knob.width()) / 2 + 1, 0, &knob); + p.drawImage((bg_on.width() - knob.width()) / 2 + 1, 0, knob); } else { - p.drawImage(0, 0, &knob); + p.drawImage(0, 0, knob); } } } diff --git a/plugingui/combobox.cc b/plugingui/combobox.cc index 4a6d2b5..6e6a9c8 100644 --- a/plugingui/combobox.cc +++ b/plugingui/combobox.cc @@ -115,7 +115,7 @@ static void drawArrow(Painter &p, int x, int y, int w, int h) void ComboBox::repaintEvent(RepaintEvent *e) { - Painter p(this); + Painter p(*this); p.clear(); @@ -128,7 +128,7 @@ void ComboBox::repaintEvent(RepaintEvent *e) return; } - p.drawBox(0, 0, &box, w, h); + p.drawBox(0, 0, box, w, h); p.setColour(Colour(183.0/255.0, 219.0/255.0 , 255.0/255.0, 1)); p.drawText(BORDER - 4 + 3, height()/2+5 + 1 + 1, font, _text); diff --git a/plugingui/eventhandler.cc b/plugingui/eventhandler.cc index e47cbf5..4b55ce5 100644 --- a/plugingui/eventhandler.cc +++ b/plugingui/eventhandler.cc @@ -31,7 +31,7 @@ namespace GUI { -EventHandler::EventHandler(NativeWindow *nativeWindow, Window *window) +EventHandler::EventHandler(NativeWindow& nativeWindow, Window& window) : window(window) , nativeWindow(nativeWindow) // , last_click(0) @@ -40,12 +40,12 @@ EventHandler::EventHandler(NativeWindow *nativeWindow, Window *window) bool EventHandler::hasEvent() { - return nativeWindow->hasEvent(); + return nativeWindow.hasEvent(); } Event *EventHandler::getNextEvent() { - return nativeWindow->getNextEvent(); + return nativeWindow.getNextEvent(); } void EventHandler::processEvents() @@ -63,16 +63,16 @@ void EventHandler::processEvents() switch(event->type()) { case Event::Repaint: - window->redraw(); + window.redraw(); break; case Event::Resize: { auto resizeEvent = static_cast(event); - if((resizeEvent->width != window->width()) || - (resizeEvent->height != window->height())) + if((resizeEvent->width != window.width()) || + (resizeEvent->height != window.height())) { - window->resized(resizeEvent->width, resizeEvent->height); + window.resized(resizeEvent->width, resizeEvent->height); } } break; @@ -81,8 +81,8 @@ void EventHandler::processEvents() { auto moveEvent = static_cast(event); - auto widget = window->find(moveEvent->x, moveEvent->y); - auto oldwidget = window->mouseFocus(); + auto widget = window.find(moveEvent->x, moveEvent->y); + auto oldwidget = window.mouseFocus(); if(widget != oldwidget) { // Send focus leave to oldwidget @@ -97,16 +97,16 @@ void EventHandler::processEvents() widget->mouseEnterEvent(); } - window->setMouseFocus(widget); + window.setMouseFocus(widget); } - if(window->buttonDownFocus()) + if(window.buttonDownFocus()) { - auto widget = window->buttonDownFocus(); + auto widget = window.buttonDownFocus(); moveEvent->x -= widget->windowX(); moveEvent->y -= widget->windowY(); - window->buttonDownFocus()->mouseMoveEvent(moveEvent); + window.buttonDownFocus()->mouseMoveEvent(moveEvent); break; } @@ -131,13 +131,13 @@ void EventHandler::processEvents() lastWasDoubleClick = buttonEvent->doubleclick; - auto widget = window->find(buttonEvent->x, buttonEvent->y); + auto widget = window.find(buttonEvent->x, buttonEvent->y); - if(window->buttonDownFocus()) + if(window.buttonDownFocus()) { if(buttonEvent->direction == ButtonEvent::Up) { - auto widget = window->buttonDownFocus(); + auto widget = window.buttonDownFocus(); buttonEvent->x -= widget->windowX(); buttonEvent->y -= widget->windowY(); @@ -146,7 +146,7 @@ void EventHandler::processEvents() } else // Event::Button::Down { - window->setButtonDownFocus(nullptr); + window.setButtonDownFocus(nullptr); } } @@ -160,12 +160,12 @@ void EventHandler::processEvents() if((buttonEvent->direction == ButtonEvent::Down) && widget->catchMouse()) { - window->setButtonDownFocus(widget); + window.setButtonDownFocus(widget); } if(widget->isFocusable()) { - window->setKeyboardFocus(widget); + window.setKeyboardFocus(widget); } } } @@ -175,7 +175,7 @@ void EventHandler::processEvents() { auto scrollEvent = static_cast(event); - auto widget = window->find(scrollEvent->x, scrollEvent->y); + auto widget = window.find(scrollEvent->x, scrollEvent->y); if(widget) { scrollEvent->x -= widget->windowX(); @@ -189,9 +189,9 @@ void EventHandler::processEvents() case Event::Key: { auto keyEvent = static_cast(event); - if(window->keyboardFocus()) + if(window.keyboardFocus()) { - window->keyboardFocus()->keyEvent(keyEvent); + window.keyboardFocus()->keyEvent(keyEvent); } } break; diff --git a/plugingui/eventhandler.h b/plugingui/eventhandler.h index b865cbb..8fa42e4 100644 --- a/plugingui/eventhandler.h +++ b/plugingui/eventhandler.h @@ -36,7 +36,7 @@ class Window; class EventHandler { public: - EventHandler(NativeWindow *nativeWindow, Window *window); + EventHandler(NativeWindow& nativeWindow, Window& window); //! \brief Process all evebts currently in the event queue. void processEvents(); @@ -51,8 +51,8 @@ public: Notifier<> closeNotifier; private: - Window *window; - NativeWindow *nativeWindow; + Window& window; + NativeWindow& nativeWindow; // Used to ignore mouse button release after a double click. bool lastWasDoubleClick; diff --git a/plugingui/filebrowser.cc b/plugingui/filebrowser.cc index 9635c94..6872a4e 100644 --- a/plugingui/filebrowser.cc +++ b/plugingui/filebrowser.cc @@ -131,8 +131,8 @@ void FileBrowser::resize(int w, int h) void FileBrowser::repaintEvent(RepaintEvent *e) { - Painter p(this); - p.drawImageStretched(0,0, &back, width(), height()); + Painter p(*this); + p.drawImageStretched(0,0, back, width(), height()); } void FileBrowser::listSelectionChanged() diff --git a/plugingui/font.cc b/plugingui/font.cc index 79e3ab9..8cc0a4f 100644 --- a/plugingui/font.cc +++ b/plugingui/font.cc @@ -72,7 +72,7 @@ Font::Font(const std::string& fontfile) characters[c - 1].width = characters[c].offset - characters[c - 1].offset - 1; } -size_t Font::textWidth(const std::string& text) +size_t Font::textWidth(const std::string& text) const { size_t len = 0; @@ -86,12 +86,12 @@ size_t Font::textWidth(const std::string& text) return len; } -size_t Font::textHeight(const std::string& text) +size_t Font::textHeight(const std::string& text) const { return img_font.height(); } -PixelBufferAlpha *Font::render(const std::string& text) +PixelBufferAlpha *Font::render(const std::string& text) const { int border = 1; PixelBufferAlpha *pb = diff --git a/plugingui/font.h b/plugingui/font.h index c7fe67e..d350481 100644 --- a/plugingui/font.h +++ b/plugingui/font.h @@ -38,10 +38,10 @@ class Font { public: Font(const std::string& fontfile = ":font.png"); - size_t textWidth(const std::string& text); - size_t textHeight(const std::string& text = ""); + size_t textWidth(const std::string& text) const; + size_t textHeight(const std::string& text = "") const; - PixelBufferAlpha *render(const std::string& text); + PixelBufferAlpha *render(const std::string& text) const; private: Image img_font; @@ -54,11 +54,6 @@ private: }; std::array characters; - -// std::map character_offset; -// std::map character_width; -// std::map character_pre_bias; -// std::map character_post_bias; }; } // GUI:: diff --git a/plugingui/image.cc b/plugingui/image.cc index f2dfb9c..6c16a69 100644 --- a/plugingui/image.cc +++ b/plugingui/image.cc @@ -92,17 +92,17 @@ void Image::load(const char* data, size_t size) } } -size_t Image::width() +size_t Image::width() const { return _width; } -size_t Image::height() +size_t Image::height() const { return _height; } -Colour Image::getPixel(size_t x, size_t y) +Colour Image::getPixel(size_t x, size_t y) const { if(x > _width || y > _height) { diff --git a/plugingui/image.h b/plugingui/image.h index 1a6e40a..791ade4 100644 --- a/plugingui/image.h +++ b/plugingui/image.h @@ -39,10 +39,10 @@ public: Image(const std::string& filename); ~Image(); - size_t width(); - size_t height(); + size_t width() const; + size_t height() const; - Colour getPixel(size_t x, size_t y); + Colour getPixel(size_t x, size_t y) const; private: void setError(int err); diff --git a/plugingui/knob.cc b/plugingui/knob.cc index 360766e..535448b 100644 --- a/plugingui/knob.cc +++ b/plugingui/knob.cc @@ -144,10 +144,10 @@ void Knob::repaintEvent(RepaintEvent *e) int center_x = width() / 2; int center_y = height() / 2; - Painter p(this); + Painter p(*this); p.clear(); - p.drawImageStretched(0, 0, &img_knob, diameter, diameter); + p.drawImageStretched(0, 0, img_knob, diameter, diameter); char buf[64]; sprintf(buf, "%.2f", currentValue * maximum); diff --git a/plugingui/label.cc b/plugingui/label.cc index 1b2ec14..7d82f1b 100644 --- a/plugingui/label.cc +++ b/plugingui/label.cc @@ -44,7 +44,7 @@ void Label::setText(std::string text) void Label::repaintEvent(GUI::RepaintEvent *e) { - Painter p(this); + Painter p(*this); p.clear(); diff --git a/plugingui/layout.h b/plugingui/layout.h index 9814f7e..72e4fc8 100644 --- a/plugingui/layout.h +++ b/plugingui/layout.h @@ -38,7 +38,7 @@ public: LayoutItem(); virtual ~LayoutItem(); - void setLayoutParent(Layout *parent); + void setLayoutParent(Layout* parent); virtual void resize(int width, int height) = 0; virtual void move(size_t x, size_t y) = 0; @@ -48,7 +48,7 @@ public: virtual size_t height() = 0; private: - Layout *parent; + Layout* parent; }; //! \brief Abtract Layout class. diff --git a/plugingui/led.cc b/plugingui/led.cc index f8c001c..1d844c3 100644 --- a/plugingui/led.cc +++ b/plugingui/led.cc @@ -50,7 +50,7 @@ void LED::repaintEvent(RepaintEvent *e) size_t h = height() - 1; size_t w = width() - 1; - Painter p(this); + Painter p(*this); float alpha = 0.9; switch(state) { case Red: diff --git a/plugingui/lineedit.cc b/plugingui/lineedit.cc index 12969ba..ff936f4 100644 --- a/plugingui/lineedit.cc +++ b/plugingui/lineedit.cc @@ -213,7 +213,7 @@ void LineEdit::keyEvent(KeyEvent *keyEvent) void LineEdit::repaintEvent(RepaintEvent *repaintEvent) { - Painter p(this); + Painter p(*this); p.clear(); @@ -224,7 +224,7 @@ void LineEdit::repaintEvent(RepaintEvent *repaintEvent) return; } - p.drawBox(0, 0, &box, w, h); + p.drawBox(0, 0, box, w, h); p.setColour(Colour(183.0/255.0, 219.0/255.0 , 255.0/255.0, 1)); diff --git a/plugingui/listbox.cc b/plugingui/listbox.cc index 22263b0..a573e0e 100644 --- a/plugingui/listbox.cc +++ b/plugingui/listbox.cc @@ -101,7 +101,7 @@ void ListBox::clearSelectedValue() void ListBox::repaintEvent(RepaintEvent *e) { - Painter p(this); + Painter p(*this); p.clear(); @@ -112,7 +112,7 @@ void ListBox::repaintEvent(RepaintEvent *e) return; } - p.drawBox(0, 0, &box, w, h); + p.drawBox(0, 0, box, w, h); } void ListBox::resize(int width, int height) diff --git a/plugingui/listboxbasic.cc b/plugingui/listboxbasic.cc index fa1601f..0d854ab 100644 --- a/plugingui/listboxbasic.cc +++ b/plugingui/listboxbasic.cc @@ -142,7 +142,7 @@ void ListBoxBasic::clearSelectedValue() void ListBoxBasic::repaintEvent(RepaintEvent *e) { - Painter p(this); + Painter p(*this); p.clear(); @@ -154,7 +154,7 @@ void ListBoxBasic::repaintEvent(RepaintEvent *e) return; } - p.drawImageStretched(0, 0, &bg_img, w, h); + p.drawImageStretched(0, 0, bg_img, w, h); p.setColour(Colour(183.0/255.0, 219.0/255.0 , 255.0/255.0, 1)); diff --git a/plugingui/listboxthin.cc b/plugingui/listboxthin.cc index 18cfc76..0120c50 100644 --- a/plugingui/listboxthin.cc +++ b/plugingui/listboxthin.cc @@ -96,7 +96,7 @@ std::string ListBoxThin::selectedValue() void ListBoxThin::repaintEvent(RepaintEvent *e) { - Painter p(this); + Painter p(*this); p.clear(); @@ -107,7 +107,7 @@ void ListBoxThin::repaintEvent(RepaintEvent *e) return; } - p.drawBox(0, 0, &box, w, h); + p.drawBox(0, 0, box, w, h); } void ListBoxThin::resize(int width, int height) diff --git a/plugingui/painter.cc b/plugingui/painter.cc index 842ff11..c1ab811 100644 --- a/plugingui/painter.cc +++ b/plugingui/painter.cc @@ -28,461 +28,444 @@ #include "window.h" -#include +#include -GUI::Painter::Painter(GUI::Widget *widget) -{ - this->widget = widget; - widget->window()->beginPaint(); - pixbuf = &widget->pixbuf; - colour = Colour(0, 0, 0, 0.5); -} +namespace GUI { -GUI::Painter::~Painter() +Painter::Painter(Widget& widget) + : widget(widget) { - widget->window()->endPaint(); - flush(); -} + if(widget.window()) + { + widget.window()->beginPaint(); + } -void GUI::Painter::setColour(Colour colour) -{ - this->colour = colour; + pixbuf = &widget.pixbuf; + colour = Colour(0, 0, 0, 0.5); } -void GUI::Painter::plot(int x, int y, double c) +Painter::~Painter() { - // plot the pixel at (x, y) with brightness c (where 0 ≤ c ≤ 1) - pixbuf->addPixel(x, y, - (unsigned char)(colour.red * 255.0), - (unsigned char)(colour.green * 255.0), - (unsigned char)(colour.blue * 255.0), - (unsigned char)(colour.alpha * 255 * c)); + if(widget.window()) + { + widget.window()->endPaint(); + } + flush(); } -#include -double GUI::Painter::ipart(double x) +void Painter::setColour(const Colour& colour) { - return floor(x); //integer part of x' + this->colour = colour; } - -double GUI::Painter::round(double x) -{ - return ipart(x + 0.5); -} - -double GUI::Painter::fpart(double x) -{ - return x - ipart(x);//'fractional part of x' -} - -double GUI::Painter::rfpart(double x) + +void Painter::plot(int x, int y, double c) { - return 1 - fpart(x); -} + // plot the pixel at (x, y) with brightness c (where 0 ≤ c ≤ 1) + pixbuf->addPixel(x, y, + (unsigned char)(colour.red * 255.0), + (unsigned char)(colour.green * 255.0), + (unsigned char)(colour.blue * 255.0), + (unsigned char)(colour.alpha * 255 * c)); +} -#define SWAP(x, y) { int tmp = x; x = y; y = tmp; } -void GUI::Painter::drawLine(int x0, int y0, int x1, int y1) +double Painter::fpart(double x) { - bool steep = abs(y1 - y0) > abs(x1 - x0); - - if(steep) { - SWAP(x0, y0); - SWAP(x1, y1); - } - if(x0 > x1) { - SWAP(x0, x1); - SWAP(y0, y1); - } - - double dx = x1 - x0; - double dy = y1 - y0; - double gradient = dy / dx; - - // Handle first endpoint: - double xend = round(x0); - double yend = y0 + gradient * (xend - x0); - //double xgap = rfpart(x0 + 0.5); - double xpxl1 = xend; //this will be used in the main loop - double ypxl1 = ipart(yend); - - if(steep) { - plot(ypxl1, xpxl1, 1); - //plot(ypxl1, xpxl1, rfpart(yend) * xgap); - //plot(ypxl1+1, xpxl1, fpart(yend) * xgap); - } else { - plot(xpxl1, ypxl1, 1); - //plot(xpxl1, ypxl1 , rfpart(yend) * xgap); - //plot(xpxl1, ypxl1+1, fpart(yend) * xgap); - } - - double intery = yend + gradient; // first y-intersection for the main loop - - // Handle second endpoint: - xend = round(x1); - yend = y1 + gradient * (xend - x1); - //xgap = fpart(x1 + 0.5); - double xpxl2 = xend; //this will be used in the main loop - double ypxl2 = ipart(yend); - - if(steep) { - plot(ypxl2, xpxl2, 1); - //plot(ypxl2 , xpxl2, rfpart(yend) * xgap); - //plot(ypxl2+1, xpxl2, fpart(yend) * xgap); - } else { - plot(xpxl2, ypxl2, 1); - //plot(xpxl2, ypxl2, rfpart(yend) * xgap); - //plot(xpxl2, ypxl2+1, fpart(yend) * xgap); - } - - // main loop - for(int x = xpxl1 + 1; x <= xpxl2 - 1; x++) { - if(steep) { - plot(ipart(intery) , x, rfpart(intery)); - plot(ipart(intery)+1, x, fpart(intery)); - } else { - plot(x, ipart (intery), rfpart(intery)); - plot(x, ipart (intery)+1, fpart(intery)); - } - intery += gradient; - } + return x - std::floor(x);// fractional part of x } -void GUI::Painter::drawRectangle(int x1, int y1, int x2, int y2) +double Painter::rfpart(double x) { - drawLine(x1, y1, x2 - 1, y1); - drawLine(x2, y1, x2, y2 - 1); - drawLine(x1 + 1, y2, x2, y2); - drawLine(x1, y1 + 1, x1, y2); + return 1 - fpart(x); // reverse fractional part of x } -void GUI::Painter::drawFilledRectangle(int x1, int y1, int x2, int y2) +void Painter::drawLine(int x0, int y0, int x1, int y1) { - for(int y = y1; y < y2; y++) { - drawLine(x1, y, x2, y); - } + bool steep = abs(y1 - y0) > abs(x1 - x0); + + if(steep) + { + std::swap(x0, y0); + std::swap(x1, y1); + } + if(x0 > x1) + { + std::swap(x0, x1); + std::swap(y0, y1); + } + + double dx = x1 - x0; + double dy = y1 - y0; + double gradient = dy / dx; + + // Handle first endpoint: + double xend = std::round(x0); + double yend = y0 + gradient * (xend - x0); + + double xpxl1 = xend; // this will be used in the main loop + double ypxl1 = std::floor(yend); + + if(steep) + { + plot(ypxl1, xpxl1, 1); + } + else + { + plot(xpxl1, ypxl1, 1); + } + + double intery = yend + gradient; // first y-intersection for the main loop + + // Handle second endpoint: + xend = std::round(x1); + yend = y1 + gradient * (xend - x1); + + double xpxl2 = xend; // this will be used in the main loop + double ypxl2 = std::floor(yend); + + if(steep) + { + plot(ypxl2, xpxl2, 1); + } + else + { + plot(xpxl2, ypxl2, 1); + } + + // main loop + for(int x = xpxl1 + 1; x <= xpxl2 - 1; ++x) + { + if(steep) + { + plot(std::floor(intery) , x, rfpart(intery)); + plot(std::floor(intery)+1, x, fpart(intery)); + } + else + { + plot(x, std::floor(intery), rfpart(intery)); + plot(x, std::floor(intery)+1, fpart(intery)); + } + intery += gradient; + } } -void GUI::Painter::clear() +void Painter::drawRectangle(int x1, int y1, int x2, int y2) { - for(int x = 0; x < (int)pixbuf->width; x++) { - for(int y = 0; y < (int)pixbuf->height; y++) { - pixbuf->setPixel(x, y, 0, 0, 0, 0); - } - } + drawLine(x1, y1, x2 - 1, y1); + drawLine(x2, y1, x2, y2 - 1); + drawLine(x1 + 1, y2, x2, y2); + drawLine(x1, y1 + 1, x1, y2); } -void GUI::Painter::drawText(int x0, int y0, GUI::Font &font, std::string text, - bool nocolour) +void Painter::drawFilledRectangle(int x1, int y1, int x2, int y2) { - PixelBufferAlpha *textbuf = font.render(text); - if(nocolour) { - for(size_t x = 0; x < textbuf->width; x++) { - for(size_t y = 0; y < textbuf->height; y++) { - unsigned char r,g,b,a; - textbuf->pixel(x, y, &r, &g, &b, &a); - pixbuf->addPixel(x + x0, y + y0 - textbuf->height, r,g,b,a); - } - } - } else { - for(size_t x = 0; x < textbuf->width; x++) { - for(size_t y = 0; y < textbuf->height; y++) { - unsigned char r,g,b,a; - textbuf->pixel(x, y, &r, &g, &b, &a); - pixbuf->addPixel(x + x0, y + y0 - textbuf->height, - colour.red * 255, - colour.green * 255, - colour.blue * 255, - colour.alpha * a); - } - } - } - - delete textbuf; + for(int y = y1; y < y2; ++y) + { + drawLine(x1, y, x2, y); + } } -#include -void GUI::Painter::drawPoint(int x, int y) +void Painter::clear() { - pixbuf->setPixel(x, y, - (unsigned char)(colour.red * 255.0), - (unsigned char)(colour.green * 255.0), - (unsigned char)(colour.blue * 255.0), - (unsigned char)(colour.alpha * 255.0)); + for(int x = 0; x < (int)pixbuf->width; ++x) + { + for(int y = 0; y < (int)pixbuf->height; ++y) + { + pixbuf->setPixel(x, y, 0, 0, 0, 0); + } + } } -#if 0 -static double distance(double r, double y) +void Painter::drawText(int x0, int y0, const Font &font, + const std::string& text, bool nocolour) { - double real_point = sqrt(pow(r, 2) - pow(y, 2)); - return ceil(real_point) - real_point; + PixelBufferAlpha* textbuf = font.render(text); + + if(nocolour) + { + for(size_t x = 0; x < textbuf->width; ++x) + { + for(size_t y = 0; y < textbuf->height; ++y) + { + unsigned char r, g, b, a; + textbuf->pixel(x, y, &r, &g, &b, &a); + pixbuf->addPixel(x + x0, y + y0 - textbuf->height, r, g, b, a); + } + } + } + else + { + for(size_t x = 0; x < textbuf->width; ++x) + { + for(size_t y = 0; y < textbuf->height; ++y) + { + unsigned char r,g,b,a; + textbuf->pixel(x, y, &r, &g, &b, &a); + pixbuf->addPixel(x + x0, y + y0 - textbuf->height, + colour.red * 255, + colour.green * 255, + colour.blue * 255, + colour.alpha * a); + } + } + } + + delete textbuf; } -double new_color(double i) { - return i * 127; +void Painter::drawPoint(int x, int y) +{ + pixbuf->setPixel(x, y, + (unsigned char)(colour.red * 255.0), + (unsigned char)(colour.green * 255.0), + (unsigned char)(colour.blue * 255.0), + (unsigned char)(colour.alpha * 255.0)); } -void GUI::Painter::drawCircle(int cx, int cy, double radius) +static void plot4points(Painter *p, int cx, int cy, int x, int y) { - // wu_circle($image, $r, $color, $offset_x = null, $offset_y = null) { - //$red = $color["red"]; - //$green = $color["green"]; - //$blue = $color["blue"]; - int offset_x = cx; - int offset_y = cy; - int x = radius; - // int xx = radius; - int y = -1; - // int yy = -1; - double t = 0; - //$color = imagecolorallocate($image, $red, $green, $blue); - while(x > y) { - y++; - double current_distance = distance(radius, y); - if(current_distance < t) { - x--; - } - - double trasparency = new_color(current_distance); - double alpha = trasparency; - double alpha2 = 127.0 - trasparency; - - double color = 1; - - plot(x + offset_x, y + offset_y, color); - plot(x + offset_x - 1, y + offset_y, alpha2 ); - plot(x + offset_x + 1, y + offset_y, alpha ); - - plot(y + offset_x, x + offset_y, color); - plot(y + offset_x, x + offset_y - 1, alpha2); - plot(y + offset_x, x + offset_y + 1, alpha); - - plot(offset_x - x , y + offset_y, color); - plot(offset_x - x + 1, y + offset_y, alpha2); - plot(offset_x - x - 1, y + offset_y, alpha); - - plot(offset_x - y, x + offset_y, color); - plot(offset_x - y, x + offset_y - 1, alpha2); - plot(offset_x - y, x + offset_y + 1, alpha); - - plot(x + offset_x, offset_y - y, color); - plot(x + offset_x - 1, offset_y - y, alpha2); - plot(x + offset_x + 1, offset_y - y, alpha); - - plot(y + offset_x, offset_y - x, color); - plot(y + offset_x, offset_y - x - 1, alpha); - plot(y + offset_x, offset_y - x + 1, alpha2); - - plot(offset_x - y, offset_y - x, color); - plot(offset_x - y, offset_y - x - 1, alpha); - plot(offset_x - y, offset_y - x + 1, alpha2); - - plot(offset_x - x, offset_y - y, color); - plot(offset_x - x - 1, offset_y - y, alpha); - plot(offset_x - x + 1, offset_y - y, alpha2); - - t = current_distance; - } + p->drawPoint(cx + x, cy + y); + if(x != 0) + { + p->drawPoint(cx - x, cy + y); + } + + if(y != 0) + { + p->drawPoint(cx + x, cy - y); + } + + if(x != 0 && y != 0) + { + p->drawPoint(cx - x, cy - y); + } } -#else -static void plot4points(GUI::Painter *p, int cx, int cy, int x, int y) + +void Painter::drawCircle(int cx, int cy, double radius) { - p->drawPoint(cx + x, cy + y); - if(x != 0) p->drawPoint(cx - x, cy + y); - if(y != 0) p->drawPoint(cx + x, cy - y); - if(x != 0 && y != 0) p->drawPoint(cx - x, cy - y); + int error = -radius; + int x = radius; + int y = 0; + + while(x >= y) + { + plot4points(this, cx, cy, x, y); + + if(x != y) + { + plot4points(this, cx, cy, y, x); + } + + error += y; + ++y; + error += y; + + if(error >= 0) + { + --x; + error -= x; + error -= x; + } + } } -void GUI::Painter::drawCircle(int cx, int cy, double radius) +static void plot4lines(Painter *p, int cx, int cy, int x, int y) { - int error = -radius; - int x = radius; - int y = 0; - - while(x >= y) { - plot4points(this, cx, cy, x, y); - if(x != y) plot4points(this, cx, cy, y, x); - - error += y; - ++y; - error += y; - - if(error >= 0) { - --x; - error -= x; - error -= x; - } - } + p->drawLine(cx + x, cy + y, cx - x, cy + y); + if(x != 0) + { + p->drawLine(cx - x, cy + y, cx + x, cy + y); + } + + if(y != 0) + { + p->drawLine(cx + x, cy - y, cx - x, cy - y); + } + + if(x != 0 && y != 0) + { + p->drawLine(cx - x, cy - y, cx + x, cy - y); + } } -#endif -static void plot4lines(GUI::Painter *p, int cx, int cy, int x, int y) +void Painter::drawFilledCircle(int cx, int cy, int radius) { - p->drawLine(cx + x, cy + y, cx - x, cy + y); - if(x != 0) p->drawLine(cx - x, cy + y, cx + x, cy + y); - if(y != 0) p->drawLine(cx + x, cy - y, cx - x, cy - y); - if(x != 0 && y != 0) p->drawLine(cx - x, cy - y, cx + x, cy - y); + int error = -radius; + int x = radius; + int y = 0; + + while(x >= y) + { + plot4lines(this, cx, cy, x, y); + + if(x != y) + { + plot4lines(this, cx, cy, y, x); + } + + error += y; + ++y; + error += y; + + if(error >= 0) + { + --x; + error -= x; + error -= x; + } + } } -void GUI::Painter::drawFilledCircle(int cx, int cy, int radius) +void Painter::drawImage(int x0, int y0, const Image& image) { - int error = -radius; - int x = radius; - int y = 0; - - while(x >= y) { - plot4lines(this, cx, cy, x, y); - if(x != y) plot4lines(this, cx, cy, y, x); - - error += y; - ++y; - error += y; - - if(error >= 0) { - --x; - error -= x; - error -= x; - } - } + size_t fw = image.width(); + size_t fh = image.height(); + + for(size_t y = 0; y < fh; ++y) + { + for(size_t x = 0; x < fw; ++x) + { + Colour c = image.getPixel(x, y); + pixbuf->addPixel(x0 + x, y0 + y, c); + } + } } -void GUI::Painter::drawImage(int x0, int y0, GUI::Image *image) +void Painter::drawImageStretched(int x0, int y0, const Image& image, + int w, int h) { - size_t fw = image->width(); - size_t fh = image->height(); - - for(size_t x = 0; x < fw; x++) { - for(size_t y = 0; y < fh; y++) { - GUI::Colour c = image->getPixel(x, y); - pixbuf->addPixel(x0 + x, y0 + y, c); - } - } + if((w < 1) || (h < 1)) + { + return; + } + + float fw = image.width(); + float fh = image.height(); + + for(int y = 0; y < h; ++y) + { + for(int x = 0; x < w; ++x) + { + int lx = ((float)x / (float)w) * fw; + int ly = ((float)y / (float)h) * fh; + Colour c = image.getPixel(lx, ly); + pixbuf->addPixel(x0 + x, y0 + y, c); + } + } } -void GUI::Painter::drawImageStretched(int x0, int y0, GUI::Image *image, - int w, int h) +void Painter::drawBox(int x, int y, const Box& box, int width, int height) { - if(w < 1 || h < 1) return; - - float fw = image->width(); - float fh = image->height(); - - for(int x = 0; x < w; x++) { - for(int y = 0; y < h; y++) { - int lx = ((float)x/(float)w)*fw; - int ly = ((float)y/(float)h)*fh; - GUI::Colour c = image->getPixel(lx, ly); - pixbuf->addPixel(x0 + x, y0 + y, c); - } - } + int dx = x; + int dy = y; + + // Top: + drawImage(dx, dy, *box.topLeft); + + dx += box.topLeft->width(); + if((dx < 0) || (dy < 0)) + { + return; + } + + drawImageStretched(dx, dy, *box.top, + width - box.topRight->width() - box.topLeft->width(), + box.top->height()); + + dx = x + width - box.topRight->width(); + if((dx < 0) || (dy < 0)) + { + return; + } + + drawImage(dx, dy, *box.topRight); + + // Center: + dy = y + box.topLeft->height(); + dx = x + box.left->width(); + if((dx < 0) || (dy < 0)) + { + return; + } + + drawImageStretched(dx, dy, *box.center, + width - box.left->width() - box.right->width(), + height - box.topLeft->height() - box.bottomLeft->height()); + + // Mid: + dx = x; + dy = y + box.topLeft->height(); + if((dx < 0) || (dy < 0)) + { + return; + } + + drawImageStretched(dx, dy, *box.left, box.left->width(), + height - box.topLeft->height() - box.bottomLeft->height()); + + dx = x + width - box.right->width(); + dy = y + box.topRight->height(); + if((dx < 0) || (dy < 0)) + { + return; + } + + drawImageStretched(dx, dy, *box.right, + box.right->width(), + height - box.topRight->height() - box.bottomRight->height()); + + // Bottom: + dx = x; + dy = y + height - box.bottomLeft->height(); + if((dx < 0) || (dy < 0)) + { + return; + } + + drawImage(dx, dy, *box.bottomLeft); + + dx += box.bottomLeft->width(); + if((dx < 0) || (dy < 0)) + { + return; + } + + drawImageStretched(dx, dy, *box.bottom, + width - box.bottomRight->width() - box.bottomLeft->width(), + box.bottom->height()); + + dx = x + width - box.bottomRight->width(); + if((dx < 0) || (dy < 0)) + { + return; + } + + drawImage(dx, dy, *box.bottomRight); } -void GUI::Painter::drawBox(int x, int y, Box *box, int width, int height) +void Painter::drawBar(int x, int y, const Bar& bar, int width, int height) { - int dx = x; - int dy = y; - - // Top: - - drawImage(dx, dy, box->topLeft); - - dx += box->topLeft->width(); - if(dx < 0 || dy < 0) return; - - drawImageStretched(dx, dy, box->top, - width - box->topRight->width() - box->topLeft->width(), - box->top->height()); - - dx = x + width - box->topRight->width(); - if(dx < 0 || dy < 0) return; + if(width < ((int)bar.left->width() + (int)bar.right->width() + 1)) + { + width = bar.left->width() + bar.right->width() + 1; + } - drawImage(dx, dy, box->topRight); + drawImageStretched(x, y, *bar.left, bar.left->width(), height); - // Center - dy = y + box->topLeft->height(); - dx = x + box->left->width(); - if(dx < 0 || dy < 0) return; + drawImageStretched(x + bar.left->width(), y, *bar.center, + width - bar.left->width() - bar.right->width(), height); - drawImageStretched(dx, dy, box->center, - width - box->left->width() - box->right->width(), - height - box->topLeft->height() - box->bottomLeft->height()); - - // Mid: - dx = x; - dy = y + box->topLeft->height(); - if(dx < 0 || dy < 0) return; - - drawImageStretched(dx, dy, box->left, box->left->width(), - height - box->topLeft->height() - box->bottomLeft->height()); - - dx = x + width - box->right->width(); - dy = y + box->topRight->height(); - if(dx < 0 || dy < 0) return; - - drawImageStretched(dx, dy, box->right, - box->right->width(), - height - box->topRight->height() - box->bottomRight->height()); - - // Bottom: - dx = x; - dy = y + height - box->bottomLeft->height(); - if(dx < 0 || dy < 0) return; - - drawImage(dx, dy, box->bottomLeft); - - dx += box->bottomLeft->width(); - if(dx < 0 || dy < 0) return; - - drawImageStretched(dx, dy, box->bottom, - width - box->bottomRight->width() - box->bottomLeft->width(), - box->bottom->height()); - - dx = x + width - box->bottomRight->width(); - if(dx < 0 || dy < 0) return; - - drawImage(dx, dy, box->bottomRight); -} - -void GUI::Painter::drawBar(int x, int y, Bar *bar, int width, int height) -{ - if(width < ((int)bar->left->width() + (int)bar->right->width() + 1)) { - width = bar->left->width() + bar->right->width() + 1; - } - drawImageStretched(x, y, - bar->left, - bar->left->width(), height); - - drawImageStretched(x + bar->left->width(), y, - bar->center, - width - bar->left->width() - bar->right->width(), height); - - drawImageStretched(x + width - bar->left->width(), y, - bar->right, - bar->right->width(), height); + drawImageStretched(x + width - bar.left->width(), y, *bar.right, + bar.right->width(), height); } -void GUI::Painter::flush() +void Painter::flush() { #ifdef X11 - // Send the "DrawLine" request to the server - //XFlush(gctx->display); + // Send the "DrawLine" request to the server + //XFlush(gctx->display); #endif/*X11*/ } -#ifdef TEST_PAINTER -//Additional dependency files -//deps: -//Required cflags (autoconf vars may be used) -//cflags: -//Required link options (autoconf vars may be used) -//libs: -#include "test.h" - -TEST_BEGIN; - -// TODO: Put some testcode here (see test.h for usable macros). - -TEST_END; - -#endif/*TEST_PAINTER*/ +} // GUI:: diff --git a/plugingui/painter.h b/plugingui/painter.h index 0a93f50..40747a7 100644 --- a/plugingui/painter.h +++ b/plugingui/painter.h @@ -24,8 +24,7 @@ * along with DrumGizmo; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#ifndef __DRUMGIZMO_PAINTER_H__ -#define __DRUMGIZMO_PAINTER_H__ +#pragma once #include @@ -39,59 +38,55 @@ namespace GUI { class Painter { public: - Painter(Widget *widget); - ~Painter(); + Painter(Widget& widget); + ~Painter(); - void flush(); + void flush(); - void setColour(Colour colour); + void setColour(const Colour& colour); - void drawLine(int x1, int y1, int x2, int y2); - void drawText(int x, int y, Font &font, std::string text, - bool nocolour = false); - void drawRectangle(int x1, int y1, int x2, int y2); - void drawFilledRectangle(int x1, int y1, int x2, int y2); - void drawPoint(int x, int y); - void drawCircle(int x, int y, double r); - void drawFilledCircle(int x, int y, int r); - void drawImage(int x, int y, Image *image); - void drawImageStretched(int x0, int y0, GUI::Image *image, - int width, int height); + void drawLine(int x1, int y1, int x2, int y2); + void drawText(int x, int y, const Font &font, const std::string& text, + bool nocolour = false); + void drawRectangle(int x1, int y1, int x2, int y2); + void drawFilledRectangle(int x1, int y1, int x2, int y2); + void drawPoint(int x, int y); + void drawCircle(int x, int y, double r); + void drawFilledCircle(int x, int y, int r); + void drawImage(int x, int y, const Image& image); + void drawImageStretched(int x, int y, const Image& image, + int width, int height); - typedef struct { - Image *topLeft; - Image *top; - Image *topRight; - Image *left; - Image *right; - Image *bottomLeft; - Image *bottom; - Image *bottomRight; - Image *center; - } Box; - void drawBox(int x, int y, Box *box, int width, int height); + typedef struct { + Image *topLeft; + Image *top; + Image *topRight; + Image *left; + Image *right; + Image *bottomLeft; + Image *bottom; + Image *bottomRight; + Image *center; + } Box; + void drawBox(int x, int y, const Box& box, int width, int height); - typedef struct { - Image *left; - Image *right; - Image *center; - } Bar; - void drawBar(int x, int y, Bar *bar, int width, int height); + typedef struct { + Image *left; + Image *right; + Image *center; + } Bar; + void drawBar(int x, int y, const Bar& bar, int width, int height); - void clear(); + void clear(); private: - void plot(int x, int y, double c); - double ipart(double x); - double round(double x); - double fpart(double x); - double rfpart(double x); + void plot(int x, int y, double c); + double fpart(double x); + double rfpart(double x); - Widget *widget; - PixelBufferAlpha *pixbuf; - Colour colour; + Widget& widget; + PixelBufferAlpha* pixbuf; + Colour colour; }; -}; - -#endif/*__DRUMGIZMO_PAINTER_H__*/ +} // GUI:: diff --git a/plugingui/plugingui.cc b/plugingui/plugingui.cc index deb1309..007a0ff 100644 --- a/plugingui/plugingui.cc +++ b/plugingui/plugingui.cc @@ -84,7 +84,7 @@ void PluginGUI::stopThread() void PluginGUI::handleMessage(Message *msg) { - Painter p(window);// Make sure we only redraw buffer one time. + Painter p(*window);// Make sure we only redraw buffer one time. switch(msg->type()) { case Message::LoadStatus: diff --git a/plugingui/progressbar.cc b/plugingui/progressbar.cc index 3b821bb..f834fc4 100644 --- a/plugingui/progressbar.cc +++ b/plugingui/progressbar.cc @@ -71,14 +71,14 @@ void GUI::ProgressBar::setProgress(float progress) void GUI::ProgressBar::repaintEvent(GUI::RepaintEvent *e) { - Painter p(this); + Painter p(*this); int max = width() * _progress; p.clear(); int brd = 4; - p.drawBar(0, 0, &bar_bg, width(), height()); + p.drawBar(0, 0, bar_bg, width(), height()); GUI::Painter::Bar *b = NULL; switch(state) { @@ -87,7 +87,7 @@ void GUI::ProgressBar::repaintEvent(GUI::RepaintEvent *e) case blue: b = &bar_blue; break; default: break; } - if(b) p.drawBar(brd, 0, b, max - 2*brd, height()); + if(b) p.drawBar(brd, 0, *b, max - 2*brd, height()); } #ifdef TEST_PROGRESSBAR diff --git a/plugingui/scrollbar.cc b/plugingui/scrollbar.cc index 3107987..c175720 100644 --- a/plugingui/scrollbar.cc +++ b/plugingui/scrollbar.cc @@ -107,11 +107,11 @@ static void drawArrow(GUI::Painter &p, int x, int y, int w, int h) void GUI::ScrollBar::repaintEvent(RepaintEvent *e) { - GUI::Painter p(this); + GUI::Painter p(*this); p.clear(); - p.drawImageStretched(0, 0, &bg_img, width(), height()); + p.drawImageStretched(0, 0, bg_img, width(), height()); p.setColour(GUI::Colour(183.0/255.0, 219.0/255.0 , 255.0/255.0, 1)); if(!max) return; diff --git a/plugingui/slider.cc b/plugingui/slider.cc index 311497a..4f56344 100644 --- a/plugingui/slider.cc +++ b/plugingui/slider.cc @@ -104,7 +104,7 @@ void GUI::Slider::repaintEvent(GUI::RepaintEvent *e) { //DEBUG(slider, "Slider::repaintEvent (%f)\n", val); - Painter p(this); + Painter p(*this); float alpha = 0.8; diff --git a/plugingui/verticalline.cc b/plugingui/verticalline.cc index 53d82a6..bbf0d28 100644 --- a/plugingui/verticalline.cc +++ b/plugingui/verticalline.cc @@ -36,6 +36,6 @@ GUI::VerticalLine::VerticalLine(GUI::Widget *parent) void GUI::VerticalLine::repaintEvent(RepaintEvent *e) { (void)e; - GUI::Painter p(this); - p.drawImageStretched(0, 0, &vline, width(), height()); + GUI::Painter p(*this); + p.drawImageStretched(0, 0, vline, width(), height()); } diff --git a/plugingui/widget.cc b/plugingui/widget.cc index 65d269b..6106f7c 100644 --- a/plugingui/widget.cc +++ b/plugingui/widget.cc @@ -35,15 +35,17 @@ namespace GUI { Widget::Widget(Widget *parent) : pixbuf(1, 1) + , parent(parent) + , _window(nullptr) { _width = _height = 10; - this->parent = parent; if(parent) { parent->addChild(this); _window = parent->window(); } + _width = _height = 0; _visible = true; } @@ -173,7 +175,7 @@ Window *Widget::window() void Widget::repaint_r(RepaintEvent *e) { - Painter p(this); // make sure pixbuf refcount is incremented. + Painter p(*this); // make sure pixbuf refcount is incremented. repaintEvent(e); diff --git a/plugingui/window.cc b/plugingui/window.cc index b9a1092..afbcb21 100644 --- a/plugingui/window.cc +++ b/plugingui/window.cc @@ -69,7 +69,7 @@ GUI::Window::Window() native = new NativeWindowPugl(this); #endif - eventhandler = new GUI::EventHandler(native, this); + eventhandler = new GUI::EventHandler(*native, *this); } GUI::Window::~Window() @@ -92,10 +92,9 @@ void GUI::Window::repaintEvent(GUI::RepaintEvent *e) { if(!visible()) return; - Painter p(this); - p.drawImageStretched(0,0, &back, width(), height()); - p.drawImage(width() - logo.width(), - height() - logo.height(), &logo); + Painter p(*this); + p.drawImageStretched(0,0, back, width(), height()); + p.drawImage(width() - logo.width(), height() - logo.height(), logo); } void GUI::Window::setFixedSize(int w, int h) -- cgit v1.2.3