widgets/drop_target.cpp

Go to the documentation of this file.
00001 /* $Id: drop_target.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2008 - 2012 by David White <dave@whitevine.net>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #define GETTEXT_DOMAIN "wesnoth-lib"
00017 
00018 #include "widgets/drop_target.hpp"
00019 
00020 #include <boost/bind.hpp>
00021 
00022 namespace gui {
00023 
00024     drop_target::drop_groups drop_target::groups_;
00025     drop_target::target_id drop_target::next_id_;
00026     drop_target_group drop_target::next_group_ = 0;
00027 
00028     int drop_target::next_free_id(const drop_target_group& group) const
00029     {
00030         target_id::iterator group_id = next_id_.insert(std::make_pair(group, 0)).first;
00031         return group_id->second++;
00032     }
00033 
00034     drop_target::drop_target(const drop_group_manager_ptr group, const SDL_Rect& loc) : loc_(loc), id_(next_free_id(group->get_group_id())), group_(group)
00035     {
00036         groups_.insert(std::make_pair(group_->get_group_id(), this));
00037     }
00038 
00039     bool drop_target::is_this_id(const int id) const
00040     {
00041         return id_ == id;
00042     }
00043 
00044     drop_target::drop_groups::iterator drop_target::find_this() const
00045     {
00046         return std::find_if(groups_.lower_bound(group_->get_group_id()),
00047                 groups_.upper_bound(group_->get_group_id()),
00048                 boost::bind(&drop_target::is_this_id,boost::bind(&drop_groups::value_type::second,_1),id_));
00049     }
00050 
00051     drop_target::~drop_target()
00052     {
00053         groups_.erase(find_this());
00054     }
00055 
00056     int drop_target::get_id() const
00057     {
00058         return id_;
00059     }
00060 
00061     int drop_target::handle_drop()
00062     {
00063         drop_groups::iterator end = groups_.upper_bound(group_->get_group_id());
00064         drop_target::drop_groups::iterator itor
00065             = std::find_if(groups_.lower_bound(group_->get_group_id()),
00066                     end,
00067                     boost::bind(&drop_target::hit_rect,
00068                         boost::bind(&drop_groups::value_type::second,_1)
00069                         ,boost::cref(loc_), id_));
00070 
00071         if (itor == end)
00072             return -1;
00073 
00074         return itor->second->get_id();
00075     }
00076 
00077     void drop_target::delete_group(const drop_target_group id)
00078     {
00079         next_id_.erase(id);
00080         groups_.erase(id);
00081     }
00082 
00083     drop_target_group drop_target::create_group()
00084     {
00085         return next_group_++;
00086     }
00087 
00088     bool drop_target::hit_rect(const SDL_Rect& hit_loc, const int not_id) const
00089     {
00090         if (id_ == not_id)
00091             return false;
00092         int this_right = loc_.x + loc_.w;
00093         int this_lower = loc_.y + loc_.h;
00094 
00095         int hit_right = hit_loc.x + hit_loc.w;
00096         int hit_lower = hit_loc.y + hit_loc.h;
00097 
00098         // Is it inside in x direction?
00099         return (this_right > hit_loc.x
00100                 && loc_.x < hit_right
00101         // Is it inside in y direction?
00102                 && this_lower > hit_loc.y
00103                 && loc_.y < hit_lower);
00104 
00105     }
00106 
00107     drop_group_manager::drop_group_manager() : group_id_(drop_target::create_group())
00108     {
00109     }
00110 
00111     drop_group_manager::~drop_group_manager()
00112     {
00113         drop_target::delete_group(group_id_);
00114     }
00115 
00116     drop_target_group drop_group_manager::get_group_id() const
00117     {
00118         return group_id_;
00119     }
00120 }
00121 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Thu May 24 2012 01:02:58 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs