The Battle for Wesnoth  1.15.2+dev
scrollarea.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004 - 2018 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
3  Part of the Battle for Wesnoth Project https://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 /** @file */
16 
17 #define GETTEXT_DOMAIN "wesnoth-lib"
18 
19 #include "widgets/scrollarea.hpp"
20 #include "sdl/rect.hpp"
21 #include "video.hpp"
22 
23 
24 namespace gui {
25 
26 scrollarea::scrollarea(CVideo &video, const bool auto_join)
27  : widget(video, auto_join), scrollbar_(video),
28  old_position_(0), recursive_(false), shown_scrollbar_(false),
29  shown_size_(0), full_size_(0), swipe_dy_(0)
30 {
31  scrollbar_.hide(true);
32 }
33 
35 {
37 }
38 
40 {
42  h.push_back(&scrollbar_);
43  return h;
44 }
45 
46 void scrollarea::update_location(const SDL_Rect& rect)
47 {
48  SDL_Rect r = rect;
50  if (shown_scrollbar_) {
51  int w = r.w - scrollbar_.width();
52  r.x += w;
53  r.w -= w;
55  r.x -= w;
56  r.w = w;
57  }
58 
59  if (!hidden())
62 }
63 
65 {
66  if (recursive_)
67  return;
68  recursive_ = true;
70  bg_restore();
71  bg_cancel();
73  }
74  recursive_ = false;
75 }
76 
77 void scrollarea::hide(bool value)
78 {
79  widget::hide(value);
80  if (shown_scrollbar_)
81  scrollbar_.hide(value);
82 }
83 
84 unsigned scrollarea::get_position() const
85 {
86  return scrollbar_.get_position();
87 }
88 
90 {
92 }
93 
94 void scrollarea::set_position(unsigned pos)
95 {
97 }
98 
99 void scrollarea::adjust_position(unsigned pos)
100 {
102 }
103 
105 {
107 }
108 
110 {
112  shown_size_ = h;
113  test_scrollbar();
114 }
115 
117 {
119  full_size_ = h;
120  test_scrollbar();
121 }
122 
124 {
126 }
127 
129 {
130  int grip_position = scrollbar_.get_position();
131  if (grip_position == old_position_)
132  return;
133  old_position_ = grip_position;
134  scroll(grip_position);
135 }
136 
138 {
139  SDL_Rect r = location();
140  if (shown_scrollbar_)
141  r.w -= scrollbar_.width();
142  return r;
143 }
144 
146 {
147  return scrollbar_.width();
148 }
149 
150 void scrollarea::handle_event(const SDL_Event& event)
151 {
153 
154  if (mouse_locked() || hidden())
155  return;
156 
157  if (event.type == SDL_MOUSEWHEEL) {
158  const SDL_MouseWheelEvent &ev = event.wheel;
159  int x, y;
160  SDL_GetMouseState(&x, &y);
161  if (sdl::point_in_rect(x, y, inner_location())) {
162  if (ev.y > 0) {
164  } else if (ev.y < 0) {
166  }
167  }
168  }
169 
170  if (event.type == SDL_FINGERUP) {
171  swipe_dy_ = 0;
172  }
173 
174  if (event.type == SDL_FINGERDOWN || event.type == SDL_FINGERMOTION) {
175  SDL_Rect r = video().screen_area();
176  auto tx = static_cast<int>(event.tfinger.x * r.w);
177  auto ty = static_cast<int>(event.tfinger.y * r.h);
178  auto dy = static_cast<int>(event.tfinger.dy * r.h);
179 
180  if (event.type == SDL_FINGERDOWN) {
181  swipe_dy_ = 0;
182  swipe_origin_.x = tx;
183  swipe_origin_.y = ty;
184  }
185 
186  if (event.type == SDL_FINGERMOTION) {
187 
188  swipe_dy_ += dy;
189  if (scrollbar_.get_max_position() == 0) {
190  return;
191  }
192 
193  int scrollbar_step = scrollbar_.height() / scrollbar_.get_max_position();
194  if (scrollbar_step <= 0) {
195  return;
196  }
197 
199  && abs(swipe_dy_) >= scrollbar_step)
200  {
201  unsigned int pos = std::max(
202  static_cast<int>(scrollbar_.get_position() - swipe_dy_ / scrollbar_step),
203  0);
205  swipe_dy_ %= scrollbar_step;
206  }
207  }
208  }
209 
210 }
211 
212 } // end namespace gui
void bg_cancel()
Definition: widget.cpp:67
virtual void set_inner_location(const SDL_Rect &rect)=0
void set_shown_size(unsigned h)
Definition: scrollarea.cpp:109
void adjust_position(unsigned pos)
Definition: scrollarea.cpp:99
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:189
bool hidden() const
Definition: widget.cpp:188
unsigned get_position() const
Determine where the scrollbar is.
Definition: scrollbar.cpp:105
unsigned scrollbar_width() const
Definition: scrollarea.cpp:145
virtual sdl_handler_vector handler_members()
Definition: scrollarea.cpp:39
Definition: video.hpp:31
unsigned get_max_position() const
Definition: scrollbar.cpp:110
General purpose widgets.
virtual void update_location(const SDL_Rect &rect)
Definition: scrollarea.cpp:46
#define h
void set_scroll_rate(unsigned r)
Definition: scrollarea.cpp:123
void test_scrollbar()
Definition: scrollarea.cpp:64
void move_position(int dep)
Definition: scrollarea.cpp:104
-file util.hpp
void scroll_down()
Scrolls down one step.
Definition: scrollbar.cpp:188
virtual void scroll(unsigned int pos)=0
void set_full_size(unsigned h)
Definition: scrollarea.cpp:116
bool has_scrollbar() const
Definition: scrollarea.cpp:34
virtual void handle_event(const SDL_Event &)
Definition: widget.cpp:330
void set_position(unsigned pos)
Definition: scrollarea.cpp:94
virtual void hide(bool value=true)
Definition: widget.cpp:152
struct gui::scrollarea::@22 swipe_origin_
const SDL_Rect & location() const
Definition: widget.cpp:134
virtual void hide(bool value=true)
Definition: scrollbar.cpp:98
virtual void set_location(const SDL_Rect &rect)
Definition: widget.cpp:75
void set_position(unsigned pos)
Manually update the scrollbar.
Definition: scrollbar.cpp:115
bool point_in_rect(int x, int y, const SDL_Rect &rect)
Tests whether a point is inside a rectangle.
Definition: rect.cpp:22
void adjust_position(unsigned pos)
Ensure the viewport contains the position.
Definition: scrollbar.cpp:127
unsigned full_size_
Definition: scrollarea.hpp:62
scrollbar scrollbar_
Definition: scrollarea.hpp:58
virtual void handle_event(const SDL_Event &event)
Definition: scrollarea.cpp:150
SDL_Rect inner_location() const
Definition: scrollarea.cpp:137
unsigned get_max_position() const
Definition: scrollarea.cpp:89
int width() const
Definition: widget.cpp:124
int w
int height() const
Definition: widget.cpp:129
void scroll_up()
Scrolls up one step.
Definition: scrollbar.cpp:193
void bg_restore() const
Definition: widget.cpp:241
bool mouse_locked() const
Definition: widget.cpp:62
virtual void hide(bool value=true)
Definition: scrollarea.cpp:77
CVideo & video() const
Definition: widget.hpp:83
unsigned shown_size_
Definition: scrollarea.hpp:61
Contains the SDL_Rect helper code.
void set_shown_size(unsigned h)
Set the relative size of the grip.
Definition: scrollbar.cpp:144
bool is_valid_height(int height) const
Return true if the scrollbar has a valid size.
Definition: scrollbar.cpp:177
void set_full_size(unsigned h)
Set the relative size of the scrollbar.
Definition: scrollbar.cpp:158
void set_scroll_rate(unsigned r)
Set scroll rate.
Definition: scrollbar.cpp:172
scrollarea(CVideo &video, bool auto_join=true)
Create a zone with automatic handling of scrollbar.
Definition: scrollarea.cpp:26
SDL_Rect screen_area(bool as_pixels=true) const
Returns the current window renderer area, either in pixels or screen coordinates. ...
Definition: video.cpp:286
void move_position(int dep)
Move the scrollbar.
Definition: scrollbar.cpp:135
virtual void process_event()
Definition: scrollarea.cpp:128
unsigned get_position() const
Definition: scrollarea.cpp:84