The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
drop_target.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
17 #include "widgets/drop_target.hpp"
18 
19 #include "utils/functional.hpp"
20 
21 namespace gui {
22 
26 
28  {
29  target_id::iterator group_id = next_id_.insert(std::make_pair(group, 0)).first;
30  return group_id->second++;
31  }
32 
33  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)
34  {
35  groups_.insert(std::make_pair(group_->get_group_id(), this));
36  }
37 
38  bool drop_target::is_this_id(const int id) const
39  {
40  return id_ == id;
41  }
42 
44  {
45  return std::find_if(groups_.lower_bound(group_->get_group_id()),
46  groups_.upper_bound(group_->get_group_id()),
47  std::bind(&drop_target::is_this_id,std::bind(&drop_groups::value_type::second,_1),id_));
48  }
49 
51  {
52  groups_.erase(find_this());
53  }
54 
55  int drop_target::get_id() const
56  {
57  return id_;
58  }
59 
61  {
62  drop_groups::iterator end = groups_.upper_bound(group_->get_group_id());
64  = std::find_if(groups_.lower_bound(group_->get_group_id()),
65  end,
66  std::bind(&drop_target::hit_rect,
67  std::bind(&drop_groups::value_type::second,_1),
68  std::cref(loc_), id_));
69 
70  if (itor == end)
71  return -1;
72 
73  return itor->second->get_id();
74  }
75 
77  {
78  next_id_.erase(id);
79  groups_.erase(id);
80  }
81 
83  {
84  return next_group_++;
85  }
86 
87  bool drop_target::hit_rect(const SDL_Rect& hit_loc, const int not_id) const
88  {
89  if (id_ == not_id)
90  return false;
91  int this_right = loc_.x + loc_.w;
92  int this_lower = loc_.y + loc_.h;
93 
94  int hit_right = hit_loc.x + hit_loc.w;
95  int hit_lower = hit_loc.y + hit_loc.h;
96 
97  // Is it inside in x direction?
98  return (this_right > hit_loc.x
99  && loc_.x < hit_right
100  // Is it inside in y direction?
101  && this_lower > hit_loc.y
102  && loc_.y < hit_lower);
103 
104  }
105 
107  {
108  }
109 
111  {
113  }
114 
116  {
117  return group_id_;
118  }
119 }
120 
bool hit_rect(const SDL_Rect &hit_loc, const int not_id) const
Definition: drop_target.cpp:87
drop_target_group get_group_id() const
std::string id_
Definition: formula.cpp:636
static target_id next_id_
Definition: drop_target.hpp:42
drop_target(const drop_group_manager_ptr group, const SDL_Rect &loc)
Registers drop target and saves reference to location.
Definition: drop_target.cpp:33
static drop_groups groups_
Definition: drop_target.hpp:41
General purpose widgets.
map_location loc_
static drop_target_group create_group()
Definition: drop_target.cpp:82
GLuint GLuint end
Definition: glew.h:1221
std::multimap< drop_target_group, drop_target * > drop_groups
Definition: drop_target.hpp:39
int drop_target_group
Definition: drop_target.hpp:29
GLuint id
Definition: glew.h:1647
int handle_drop()
Called by widget object when droping happens.
Definition: drop_target.cpp:60
drop_groups::iterator find_this() const
Definition: drop_target.cpp:43
bool is_this_id(const int id) const
Checks if id matches id for this object.
Definition: drop_target.cpp:38
const drop_target_group group_id_
Definition: drop_target.hpp:96
const SDL_Rect & loc_
Definition: drop_target.hpp:49
drop_group_manager_ptr group_
Definition: drop_target.hpp:51
std::map< std::string, tfilter >::iterator itor
Definition: filter.cpp:199
static drop_target_group next_group_
Definition: drop_target.hpp:43
int get_id() const
Definition: drop_target.cpp:55
GLboolean GLuint group
Definition: glew.h:2589
std::map< drop_target_group, int > target_id
Definition: drop_target.hpp:40
Handles droping for drag able ui items.
Definition: drop_target.hpp:38
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
int next_free_id(const drop_target_group &group) const
Definition: drop_target.cpp:27
static void delete_group(const drop_target_group id)
Definition: drop_target.cpp:76