More display-class refactoring; lift the code for manipulating buttons.

This commit is contained in:
Eric S. Raymond 2007-06-23 16:19:45 +00:00
parent f879503fe3
commit 4a92710834
2 changed files with 51 additions and 53 deletions

View file

@ -333,6 +333,50 @@ void map_display::screenshot()
SDL_SaveBMP(screen_.getSurface().get(), name.c_str());
}
gui::button* map_display::find_button(const std::string& id)
{
for (size_t i = 0; i < buttons_.size(); ++i) {
if(buttons_[i].id() == id) {
return &buttons_[i];
}
}
return NULL;
}
void map_display::create_buttons()
{
std::vector<gui::button> work;
const std::vector<theme::menu>& buttons = theme_.menus();
for(std::vector<theme::menu>::const_iterator i = buttons.begin(); i != buttons.end(); ++i) {
gui::button b(screen_,i->title(),string_to_button_type(i->type()),i->image());
b.set_id(i->get_id());
const SDL_Rect& loc = i->location(screen_area());
b.set_location(loc.x,loc.y);
if (!i->tooltip().empty()){
tooltips::add_tooltip(loc, i->tooltip());
}
if(rects_overlap(b.location(),map_area())) {
b.set_volatile(true);
}
gui::button* b_prev = find_button(b.id());
if(b_prev) b.enable(b_prev->enabled());
work.push_back(b);
}
buttons_.swap(work);
}
gui::button::TYPE map_display::string_to_button_type(std::string type)
{
gui::button::TYPE res = gui::button::TYPE_PRESS;
if (type == "checkbox") { res = gui::button::TYPE_CHECK; }
else if (type == "image") { res = gui::button::TYPE_IMAGE; }
return res;
}
// Methods for superclass aware of units go here
std::map<gamemap::location,fixed_t> display::debugHighlights_;
@ -2469,16 +2513,6 @@ void display::debug_highlight(const gamemap::location& loc, fixed_t amount)
debugHighlights_[loc] += amount;
}
gui::button* display::find_button(const std::string& id)
{
for (size_t i = 0; i < buttons_.size(); ++i) {
if(buttons_[i].id() == id) {
return &buttons_[i];
}
}
return NULL;
}
const theme::menu* display::menu_pressed()
{
@ -2516,40 +2550,6 @@ void display::begin_game()
invalidate_all();
}
void display::create_buttons()
{
std::vector<gui::button> work;
const std::vector<theme::menu>& buttons = theme_.menus();
for(std::vector<theme::menu>::const_iterator i = buttons.begin(); i != buttons.end(); ++i) {
gui::button b(screen_,i->title(),string_to_button_type(i->type()),i->image());
b.set_id(i->get_id());
const SDL_Rect& loc = i->location(screen_area());
b.set_location(loc.x,loc.y);
if (!i->tooltip().empty()){
tooltips::add_tooltip(loc, i->tooltip());
}
if(rects_overlap(b.location(),map_area())) {
b.set_volatile(true);
}
gui::button* b_prev = find_button(b.id());
if(b_prev) b.enable(b_prev->enabled());
work.push_back(b);
}
buttons_.swap(work);
}
gui::button::TYPE display::string_to_button_type(std::string type)
{
gui::button::TYPE res = gui::button::TYPE_PRESS;
if (type == "checkbox") { res = gui::button::TYPE_CHECK; }
else if (type == "image") { res = gui::button::TYPE_IMAGE; }
return res;
}
namespace {
const int chat_message_border = 5;
const int chat_message_x = 10;

View file

@ -136,12 +136,19 @@ public:
//function to make a screenshot and save it in a default location
void screenshot();
theme& get_theme() { return theme_; }
gui::button* find_button(const std::string& id);
gui::button::TYPE string_to_button_type(std::string type);
void create_buttons();
protected:
CVideo& screen_;
const gamemap& map_;
int xpos_, ypos_;
theme theme_;
int zoom_;
// Not set by the initializer
std::vector<gui::button> buttons_;
};
class display : public map_display
@ -272,9 +279,6 @@ private:
//composes and draws the terrains on a tile
void draw_terrain_on_tile(const gamemap::location& loc, image::TYPE image_type, ADJACENT_TERRAIN_TYPE type);
gui::button::TYPE string_to_button_type(std::string type);
// event raised when the map is being scrolled
mutable events::generic_event _scroll_event;
@ -391,9 +395,6 @@ public:
bool team_valid() const { return currentTeam_ < teams_.size(); }
const std::string current_team_name() const;
theme& get_theme() { return theme_; }
gui::button* find_button(const std::string& id);
const theme::menu* menu_pressed();
//finds the menu which has a given item in it, and enables or disables it.
@ -548,9 +549,6 @@ private:
terrain_builder builder_;
void create_buttons();
std::vector<gui::button> buttons_;
bool first_turn_, in_game_;
std::set<std::string> observers_;