The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
drop_target.hpp
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 #ifndef DROP_TARGET_H_INCLUDED
15 #define DROP_TARGET_H_INCLUDED
16 
17 #include <SDL.h>
18 
19 #include <map>
20 
21 #include <boost/shared_ptr.hpp>
22 #include <boost/noncopyable.hpp>
23 
24 namespace gui {
25 
26  class drop_target;
28 
29  typedef int drop_target_group;
32 
33  /**
34  * Handles droping for drag able ui items.
35  * Widget class just has to inherit from drop_target,
36  * call constructor and handle_drop.
37  **/
38  class drop_target : public boost::noncopyable {
39  typedef std::multimap<drop_target_group, drop_target*> drop_groups;
40  typedef std::map<drop_target_group, int> target_id;
41  static drop_groups groups_;
42  static target_id next_id_;
43  static drop_target_group next_group_;
44 
45  int next_free_id(const drop_target_group& group) const;
46  bool hit_rect(const SDL_Rect& hit_loc, const int not_id) const;
48 
49  const SDL_Rect& loc_;
50  const int id_;
51  drop_group_manager_ptr group_;
52 
53  static drop_target_group create_group();
54  static void delete_group(const drop_target_group id);
55 
56  protected:
57  /**
58  * Used to check if static storages are empty.
59  * Only for testing.
60  **/
61  static bool empty() { return groups_.empty() && next_id_.empty(); }
62  /**
63  * Called by widget object when droping happens.
64  *
65  * Droping over multiple widget objects in same group
66  * is undefined.
67  *
68  * @return: id which widget was hit when droping
69  **/
70  int handle_drop();
71 
72  public:
73  /**
74  * Registers drop target and saves reference to location.
75  **/
76  drop_target(const drop_group_manager_ptr group, const SDL_Rect& loc);
77  ~drop_target();
78 
79  int get_id() const;
80  /**
81  * Checks if id matches id for this object.
82  * Used by for_each/boost:bind
83  **/
84  bool is_this_id(const int id) const;
85 
86  friend class drop_group_manager;
87 
88  };
89 
90  /**
91  * Used to create and destroy drop groups.
92  * To create drop_target widgets one has to have
93  * drop_group_manager stored in drop_group_manager_ptr.
94  **/
95  class drop_group_manager : public boost::noncopyable {
96  const drop_target_group group_id_;
97  public:
100 
101  drop_target_group get_group_id() const;
102  };
103 
104 }
105 #endif
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
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.
static drop_target_group create_group()
Definition: drop_target.cpp:82
std::multimap< drop_target_group, drop_target * > drop_groups
Definition: drop_target.hpp:39
int drop_target_group
Definition: drop_target.hpp:29
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
static bool empty()
Used to check if static storages are empty.
Definition: drop_target.hpp:61
boost::shared_ptr< drop_group_manager > drop_group_manager_ptr
Definition: drop_target.hpp:30
boost::shared_ptr< drop_target > drop_target_ptr
Definition: drop_target.hpp:26
static drop_target_group next_group_
Definition: drop_target.hpp:43
Used to create and destroy drop groups.
Definition: drop_target.hpp:95
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