From cec1d9ea562e3d52c98f1219db5e186943f2f0d6 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sun, 12 Feb 2017 11:07:22 +0100 Subject: Refactor/introduce widget and windiow redraw/dirty mechanism to eradicate unnecessary rendering passes during event handling. --- plugingui/widget.cc | 97 ++++++++++++++++++++++++----------------------------- 1 file changed, 44 insertions(+), 53 deletions(-) (limited to 'plugingui/widget.cc') diff --git a/plugingui/widget.cc b/plugingui/widget.cc index 9b8b173..239c233 100644 --- a/plugingui/widget.cc +++ b/plugingui/widget.cc @@ -31,7 +31,8 @@ #include "painter.h" #include "window.h" -namespace GUI { +namespace GUI +{ Widget::Widget(Widget* parent) : parent(parent) @@ -67,15 +68,21 @@ void Widget::setVisible(bool visible) if(visible) { - repaintEvent(nullptr); + redraw(); } } -bool Widget::visible() +bool Widget::visible() const { return _visible; } +void Widget::redraw() +{ + dirty = true; + window()->needsRedraw(); +} + void Widget::addChild(Widget* widget) { children.push_back(widget); @@ -124,33 +131,39 @@ void Widget::resize(std::size_t width, std::size_t height) _width = width; _height = height; pixbuf.realloc(width, height); - repaintEvent(nullptr); + redraw(); sizeChangeNotifier(width, height); } void Widget::move(int x, int y) { + if((_x == x) && + (_y == y)) + { + return; + } + _x = x; _y = y; positionChangeNotifier(x, y); } -int Widget::x() +int Widget::x() const { return _x; } -int Widget::y() +int Widget::y() const { return _y; } -std::size_t Widget::width() +std::size_t Widget::width() const { return _width; } -std::size_t Widget::height() +std::size_t Widget::height() const { return _height; } @@ -160,44 +173,6 @@ PixelBufferAlpha& Widget::GetPixelBuffer() return pixbuf; } -void Widget::beginPaint() -{ - if(_window) - { - _window->beginPaint(); - } -} - -void Widget::endPaint() -{ - if(_window) - { - _window->endPaint(); - } -} - -size_t Widget::windowX() -{ - size_t window_x = x(); - if(parent) - { - window_x += parent->windowX(); - } - - return window_x; -} - -size_t Widget::windowY() -{ - size_t window_y = y(); - if(parent) - { - window_y += parent->windowY(); - } - - return window_y; -} - ImageCache& Widget::getImageCache() { assert(parent); @@ -231,8 +206,14 @@ std::vector Widget::getPixelBuffers() { std::vector pixelBuffers; - pixbuf.x = windowX(); - pixbuf.y = windowY(); + pixbuf.x = translateToWindowX(); + pixbuf.y = translateToWindowY(); + + if(dirty) + { + repaintEvent(nullptr); + dirty = false; + } pixelBuffers.push_back(&pixbuf); @@ -254,16 +235,26 @@ bool Widget::hasKeyboardFocus() return window()->keyboardFocus() == this; } -void Widget::repaintChildren(RepaintEvent* repaintEvent) +std::size_t Widget::translateToWindowX() { - Painter p(*this); // make sure pixbuf refcount is incremented. + size_t window_x = x(); + if(parent) + { + window_x += parent->translateToWindowX(); + } - this->repaintEvent(repaintEvent); + return window_x; +} - for(auto child : children) +std::size_t Widget::translateToWindowY() +{ + size_t window_y = y(); + if(parent) { - child->repaintChildren(repaintEvent); + window_y += parent->translateToWindowY(); } + + return window_y; } } // GUI:: -- cgit v1.2.3