The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
combo_drag.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 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 "global.hpp"
18 
19 #include "widgets/combo_drag.hpp"
20 
21 
22 
23 namespace gui {
24 
25  const float combo_drag::MIN_DRAG_DISTANCE = 10.0;
26  const float combo_drag::RETURN_SPEED = 25.0;
27 
29  , const std::vector<std::string>& items
31  : combo(v, items)
32  , drop_target(group, location())
33  , drag_target_(-1)
34  , old_drag_target_(-1)
35  , old_location_()
36  , mouse_x_(-1)
37  , mouse_y_(-1)
38  , drag_(NONE)
39  {
40  }
41 
43  {
45  drag_target_ = -1;
46  return old_drag_target_;
47  }
48 
49  void combo_drag::handle_move(const SDL_MouseMotionEvent& event)
50  {
51  if (drag_ == PRESSED)
52  {
56  }
57  const int diff_x = event.x - mouse_x_;
58  const int diff_y = event.y - mouse_y_;
59  if (drag_ == PRESSED_MOVE
60  && std::sqrt(static_cast<float>(diff_x*diff_x + diff_y*diff_y)) > MIN_DRAG_DISTANCE)
61  {
62  return;
63  }
64  drag_ = MOVED;
65  SDL_Rect loc = old_location_;
66  loc.x += diff_x;
67  loc.y += diff_y;
68 
69 
70  // Don't allow moving outside clip are
71 
72  if (clip_rect())
73  {
74  const SDL_Rect *clip = clip_rect();
75  if (loc.x < clip->x)
76  loc.x = clip->x;
77  if (loc.x + loc.w > clip->x + clip->w)
78  loc.x = clip->x + clip->w - loc.w;
79  if (loc.y < clip->y)
80  loc.y = clip->y;
81  if (loc.y + loc.h > clip->y + clip->h)
82  loc.y = clip->y + clip->h - loc.h;
83  }
84 
85  set_location(loc);
86  }
87 
89  {
90  if (drag_ == RETURN)
91  {
92  SDL_Rect loc = location();
93  int x_diff = loc.x - old_location_.x;
94  int y_diff = loc.y - old_location_.y;
95  const float length = std::sqrt(static_cast<float>(x_diff*x_diff + y_diff*y_diff));
96 
97  if (length > RETURN_SPEED)
98  {
99  loc.x -= static_cast<Sint16>(x_diff*(RETURN_SPEED/length));
100  loc.y -= static_cast<Sint16>(y_diff*(RETURN_SPEED/length));
101  set_location(loc);
102  }
103  else
104  {
105  drag_ = NONE;
107  }
108  }
109  }
110 
112  {
114  }
115 
116  void combo_drag::mouse_motion(const SDL_MouseMotionEvent& event)
117  {
118  if (drag_ == PRESSED
119  || drag_ == MOVED
120  || drag_ == PRESSED_MOVE)
121  {
122  handle_move(event);
123  } else {
124  button::mouse_motion(event);
125  }
126  }
127 
128  void combo_drag::mouse_up(const SDL_MouseButtonEvent& event)
129  {
130  if ((drag_ == PRESSED || drag_ == PRESSED_MOVE || drag_ == MOVED) && event.button == SDL_BUTTON_LEFT)
131  {
132  if (drag_ == PRESSED
133  || drag_ == PRESSED_MOVE)
134  {
135  free_mouse_lock();
136  drag_ = DROP_DOWN;
137  }
138  else if (drag_ == MOVED)
139  {
140  free_mouse_lock();
141  handle_drop();
142  drag_ = RETURN;
143  hide();
144  }
145  }
146  button::mouse_up(event);
147 
148  }
149 
150  void combo_drag::mouse_down(const SDL_MouseButtonEvent& event)
151  {
152  if (hit(event.x, event.y) && event.button == SDL_BUTTON_LEFT)
153  {
154  drag_ = PRESSED;
155  mouse_x_ = event.x;
156  mouse_y_ = event.y;
157  }
158  button::mouse_down(event);
159  }
160 
162  {
163  if (drag_ == DROP_DOWN)
164  {
165  drag_ = NONE;
167  }
168  }
169 
170 }
virtual void mouse_up(const SDL_MouseButtonEvent &event)
Definition: button.cpp:689
void aquire_mouse_lock()
Definition: widget.cpp:56
static const float MIN_DRAG_DISTANCE
Definition: combo_drag.hpp:62
virtual void mouse_motion(const SDL_MouseMotionEvent &event)
Definition: combo_drag.cpp:116
Definition: video.hpp:58
SDL_Rect old_location_
Definition: combo_drag.hpp:51
General purpose widgets.
static const float RETURN_SPEED
Definition: combo_drag.hpp:63
const std::vector< std::string > items
virtual void mouse_down(const SDL_MouseButtonEvent &event)
Definition: combo_drag.cpp:150
virtual void mouse_down(const SDL_MouseButtonEvent &event)
Definition: button.cpp:661
drag_state drag_
Definition: combo_drag.hpp:61
GLuint GLsizei GLsizei * length
Definition: glew.h:1793
virtual void process_event()
Definition: combo_drag.cpp:161
const SDL_Rect * clip_rect() const
Definition: widget.cpp:104
int handle_drop()
Called by widget object when droping happens.
Definition: drop_target.cpp:60
virtual void hide(bool value=true)
Definition: widget.cpp:162
const GLdouble * v
Definition: glew.h:1359
combo_drag(CVideo &v, const std::vector< std::string > &items, const drop_group_manager_ptr group)
Definition: combo_drag.cpp:28
void free_mouse_lock()
Definition: widget.cpp:63
int get_drop_target()
used to query if this object was dropped to a target
Definition: combo_drag.cpp:42
virtual void mouse_motion(const SDL_MouseMotionEvent &event)
Definition: button.cpp:627
Encapsulates the map of the game.
Definition: location.hpp:38
void handle_move(const SDL_MouseMotionEvent &event)
Definition: combo_drag.cpp:49
bool hit(int x, int y) const
Definition: button.cpp:575
GLboolean GLuint group
Definition: glew.h:2589
virtual void mouse_up(const SDL_MouseButtonEvent &event)
Definition: combo_drag.cpp:128
cl_event event
Definition: glew.h:3070
void make_drop_down_menu()
Definition: combo.cpp:74
virtual void set_location(SDL_Rect const &rect)
Definition: widget.cpp:85
SDL_Rect const & location() const
Definition: widget.cpp:144
virtual void process(events::pump_info &)
Implements return after drop.
Definition: combo_drag.cpp:88
Handles droping for drag able ui items.
Definition: drop_target.hpp:38