GUI2/Canvas: allow the default draw routine (shape drawing) to be overridden

Previously widgets that wanted to implement custom drawing behavior overrode styled_widget::impl_draw_background
or impl_draw_foreground. Those functions in the base class simply called the canvas render methods. Some widgets
such as the minimap ignored that rendered the contents on their own.

This new method allows rendering directly to the canvas's texture, meaning all the caching and sizing is already
handled by the canvas and doesn't need to be done by the widget; everything's always the right size and redrawn
when necessary.
This commit is contained in:
Charles Dang 2017-07-24 17:54:33 +11:00
parent 6c09c2b269
commit ae106b5091
2 changed files with 20 additions and 4 deletions

View file

@ -1359,6 +1359,7 @@ void text_shape::draw(
canvas::canvas()
: shapes_()
, drawn_shapes_()
, draw_func_(nullptr)
, blur_depth_(0)
, w_(0)
, h_(0)
@ -1374,6 +1375,7 @@ canvas::canvas()
canvas::canvas(canvas&& c)
: shapes_(std::move(c.shapes_))
, drawn_shapes_(std::move(c.drawn_shapes_))
, draw_func_(c.draw_func_)
, blur_depth_(c.blur_depth_)
, w_(c.w_)
, h_(c.h_)
@ -1442,11 +1444,15 @@ void canvas::draw(const bool force)
SDL_RenderClear(renderer_); // TODO: move to its own wrapper.
// Draw items.
for(auto& shape : shapes_) {
lg::scope_logger inner_scope_logging_object__(log_gui_draw, "Canvas: draw shape.");
// Draw items. TODO: make this cleaner
if(draw_func_ != nullptr) {
draw_func_(texture_);
} else {
for(auto& shape : shapes_) {
lg::scope_logger inner_scope_logging_object__(log_gui_draw, "Canvas: draw shape.");
shape->draw(w_, h_, renderer_, variables_);
shape->draw(w_, h_, renderer_, variables_);
}
}
// TODO: re-enable

View file

@ -93,6 +93,8 @@ public:
~canvas();
using draw_func_t = std::function<void(texture&)>;
/**
* Draws the canvas.
*
@ -109,6 +111,11 @@ public:
*/
void render();
void set_draw_function(draw_func_t func)
{
draw_func_ = func;
}
/**
* Sets the config.
*
@ -179,6 +186,9 @@ private:
* the cache needs to be invalidated. */
std::vector<shape_ptr> drawn_shapes_;
/** Optional custom drawing function. */
draw_func_t draw_func_;
/**
* The depth of the blur to use in the pre committing.
*