Skip to content

Commit

Permalink
GUI2/Canvas: allow the default draw routine (shape drawing) to be ove…
Browse files Browse the repository at this point in the history
…rridden

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.
  • Loading branch information
Vultraz committed Mar 8, 2018
1 parent 55c5fe2 commit ee0705c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/gui/core/canvas.cpp
Expand Up @@ -1358,6 +1358,7 @@ void text_shape::draw(

canvas::canvas()
: shapes_()
, draw_func_(nullptr)
, blur_depth_(0)
, w_(0)
, h_(0)
Expand All @@ -1372,6 +1373,7 @@ canvas::canvas()

canvas::canvas(canvas&& c)
: shapes_(std::move(c.shapes_))
, draw_func_(c.draw_func_)
, blur_depth_(c.blur_depth_)
, w_(c.w_)
, h_(c.h_)
Expand Down Expand Up @@ -1440,11 +1442,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
Expand Down
10 changes: 10 additions & 0 deletions src/gui/core/canvas.hpp
Expand Up @@ -93,6 +93,8 @@ class canvas

~canvas();

using draw_func_t = std::function<void(texture&)>;

/**
* Draws the canvas.
*
Expand All @@ -109,6 +111,11 @@ class canvas
*/
void render();

void set_draw_function(draw_func_t func)
{
draw_func_ = func;
}

/**
* Sets the config.
*
Expand Down Expand Up @@ -179,6 +186,9 @@ class canvas
* 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.
*
Expand Down

0 comments on commit ee0705c

Please sign in to comment.