Skip to content

Commit

Permalink
Add more comprehensive check for invalid button image loading (improves
Browse files Browse the repository at this point in the history
0babe82)

Also extends to button overlays
  • Loading branch information
Vultraz committed Feb 29, 2016
1 parent 0babe82 commit 5349552
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
8 changes: 2 additions & 6 deletions src/display.cpp
Expand Up @@ -898,9 +898,7 @@ void display::layout_buttons()
b->set_location(loc);
b->set_measurements(0,0);
b->set_label(i->title());
if(!i->image().empty()) {
b->set_image(i->image());
}
b->set_image(i->image());
b->set_volatile(
sdl::rects_overlap(b->location(),map_outside_area()));
}
Expand All @@ -915,9 +913,7 @@ void display::layout_buttons()
b->set_location(loc);
b->set_measurements(0,0);
b->set_label(i->title());
if(!i->image().empty()) {
b->set_image(i->image());
}
b->set_image(i->image());
b->set_volatile(
sdl::rects_overlap(b->location(),map_outside_area()));
}
Expand Down
12 changes: 10 additions & 2 deletions src/widgets/button.cpp
Expand Up @@ -577,17 +577,25 @@ bool button::hit(int x, int y) const
return sdl::point_in_rect(x,y,location());
}

static bool not_image(const std::string& str) { return !str.empty() && str[0] != IMAGE_PREFIX; }
static bool is_valid_image(const std::string& str) { return !str.empty() && str[0] != IMAGE_PREFIX; }

void button::set_image(const std::string& image_file)
{
if(!is_valid_image(image_file)) {
return;
}

button_image_name_ = "buttons/" + image_file;
load_images();
set_dirty();
}

void button::set_overlay(const std::string& image_file)
{
if(!is_valid_image(image_file)) {
return;
}

button_overlay_image_name_ = image_file;
load_images();
set_dirty();
Expand All @@ -600,7 +608,7 @@ void button::set_label(const std::string& val)
//if we have a list of items, use the first one that isn't an image
if (std::find(label_text_.begin(), label_text_.end(), COLUMN_SEPARATOR) != label_text_.end()) {
const std::vector<std::string>& items = utils::split(label_text_, COLUMN_SEPARATOR);
const std::vector<std::string>::const_iterator i = std::find_if(items.begin(),items.end(),not_image);
const std::vector<std::string>::const_iterator i = std::find_if(items.begin(),items.end(),is_valid_image);
if(i != items.end()) {
label_text_ = *i;
}
Expand Down

0 comments on commit 5349552

Please sign in to comment.