The Battle for Wesnoth  1.15.12+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 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
17 #include "widgets/scrollarea.hpp"
18 #include "sdl/rect.hpp"
19 #include "video.hpp"
20 
21 
22 namespace gui {
23 
24 scrollarea::scrollarea(CVideo &video, const bool auto_join)
25  : widget(video, auto_join), scrollbar_(video),
26  old_position_(0), recursive_(false), shown_scrollbar_(false),
27  shown_size_(0), full_size_(0), swipe_dy_(0)
28 {
29  scrollbar_.hide(true);
30 }
31 
33 {
34  return shown_size_ < full_size_;
35 }
36 
38 {
40  h.push_back(&scrollbar_);
41  return h;
42 }
43 
44 void scrollarea::update_location(const SDL_Rect& rect)
45 {
46  SDL_Rect r = rect;
48  if (shown_scrollbar_) {
49  int w = r.w - scrollbar_.width();
50  r.x += w;
51  r.w -= w;
53  r.x -= w;
54  r.w = w;
55  }
56 
57  if (!hidden())
60 }
61 
63 {
64  if (recursive_)
65  return;
66  recursive_ = true;
68  bg_restore();
69  bg_cancel();
71  }
72  recursive_ = false;
73 }
74 
75 void scrollarea::hide(bool value)
76 {
77  widget::hide(value);
78  if (shown_scrollbar_)
79  scrollbar_.hide(value);
80 }
81 
82 unsigned scrollarea::get_position() const
83 {
84  return scrollbar_.get_position();
85 }
86 
88 {
90 }
91 
92 void scrollarea::set_position(unsigned pos)
93 {
95 }
96 
97 void scrollarea::adjust_position(unsigned pos)
98 {
100 }
101 
103 {
105 }
106 
108 {
110  shown_size_ = h;
111  test_scrollbar();
112 }
113 
115 {
117  full_size_ = h;
118  test_scrollbar();
119 }
120 
122 {
124 }
125 
127 {
128  int grip_position = scrollbar_.get_position();
129  if (grip_position == old_position_)
130  return;
131  old_position_ = grip_position;
132  scroll(grip_position);
133 }
134 
136 {
137  SDL_Rect r = location();
138  if (shown_scrollbar_)
139  r.w -= scrollbar_.width();
140  return r;
141 }
142 
144 {
145  return scrollbar_.width();
146 }
147 
148 void scrollarea::handle_event(const SDL_Event& event)
149 {
151 
152  if (mouse_locked() || hidden())
153  return;
154 
155  if (event.type == SDL_MOUSEWHEEL) {
156  const SDL_MouseWheelEvent &ev = event.wheel;
157  int x, y;
158  SDL_GetMouseState(&x, &y);
159  if (sdl::point_in_rect(x, y, inner_location())) {
160  if (ev.y > 0) {
162  } else if (ev.y < 0) {
164  }
165  }
166  }
167 
168  if (event.type == SDL_FINGERUP) {
169  swipe_dy_ = 0;
170  }
171 
172  if (event.type == SDL_FINGERDOWN || event.type == SDL_FINGERMOTION) {
173  SDL_Rect r = video().screen_area();
174  auto tx = static_cast<int>(event.tfinger.x * r.w);
175  auto ty = static_cast<int>(event.tfinger.y * r.h);
176  auto dy = static_cast<int>(event.tfinger.dy * r.h);
177 
178  if (event.type == SDL_FINGERDOWN) {
179  swipe_dy_ = 0;
180  swipe_origin_.x = tx;
181  swipe_origin_.y = ty;
182  }
183 
184  if (event.type == SDL_FINGERMOTION) {
185 
186  swipe_dy_ += dy;
187  if (scrollbar_.get_max_position() == 0) {
188  return;
189  }
190 
191  int scrollbar_step = scrollbar_.height() / scrollbar_.get_max_position();
192  if (scrollbar_step <= 0) {
193  return;
194  }
195 
197  && abs(swipe_dy_) >= scrollbar_step)
198  {
199  unsigned int pos = std::max(
200  static_cast<int>(scrollbar_.get_position() - swipe_dy_ / scrollbar_step),
201  0);
203  swipe_dy_ %= scrollbar_step;
204  }
205  }
206  }
207 
208 }
209 
210 } // 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:107
void adjust_position(unsigned pos)
Definition: scrollarea.cpp:97
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:65
unsigned scrollbar_width() const
Definition: scrollarea.cpp:143
virtual sdl_handler_vector handler_members()
Definition: scrollarea.cpp:37
Definition: video.hpp:31
unsigned get_max_position() const
Definition: scrollbar.cpp:70
General purpose widgets.
virtual void update_location(const SDL_Rect &rect)
Definition: scrollarea.cpp:44
#define h
void set_scroll_rate(unsigned r)
Definition: scrollarea.cpp:121
void test_scrollbar()
Definition: scrollarea.cpp:62
void move_position(int dep)
Definition: scrollarea.cpp:102
struct gui::scrollarea::@29 swipe_origin_
void scroll_down()
Scrolls down one step.
Definition: scrollbar.cpp:134
virtual void scroll(unsigned int pos)=0
void set_full_size(unsigned h)
Definition: scrollarea.cpp:114
bool has_scrollbar() const
Definition: scrollarea.cpp:32
virtual void handle_event(const SDL_Event &)
Definition: widget.cpp:330
void set_position(unsigned pos)
Definition: scrollarea.cpp:92
virtual void hide(bool value=true)
Definition: widget.cpp:152
const SDL_Rect & location() const
Definition: widget.cpp:134
virtual void set_location(const SDL_Rect &rect)
Definition: widget.cpp:75
void set_position(unsigned pos)
Manually update the scrollbar.
Definition: scrollbar.cpp:75
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:85
unsigned full_size_
Definition: scrollarea.hpp:60
scrollbar scrollbar_
Definition: scrollarea.hpp:56
virtual void handle_event(const SDL_Event &event)
Definition: scrollarea.cpp:148
SDL_Rect inner_location() const
Definition: scrollarea.cpp:135
unsigned get_max_position() const
Definition: scrollarea.cpp:87
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:139
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:75
CVideo & video() const
Definition: widget.hpp:83
unsigned shown_size_
Definition: scrollarea.hpp:59
Contains the SDL_Rect helper code.
void set_shown_size(unsigned h)
Set the relative size of the grip.
Definition: scrollbar.cpp:102
void set_full_size(unsigned h)
Set the relative size of the scrollbar.
Definition: scrollbar.cpp:116
void set_scroll_rate(unsigned r)
Set scroll rate.
Definition: scrollbar.cpp:129
scrollarea(CVideo &video, bool auto_join=true)
Create a zone with automatic handling of scrollbar.
Definition: scrollarea.cpp:24
SDL_Rect screen_area(bool as_pixels=true) const
Returns the current window renderer area, either in pixels or screen coordinates. ...
Definition: video.cpp:270
void move_position(int dep)
Move the scrollbar.
Definition: scrollbar.cpp:93
virtual void process_event()
Definition: scrollarea.cpp:126
unsigned get_position() const
Definition: scrollarea.cpp:82