The Battle for Wesnoth  1.17.0-dev
scrollarea.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004 - 2021
3  by Guillaume Melquiond <guillaume.melquiond@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
18 #include "widgets/scrollarea.hpp"
19 #include "sdl/rect.hpp"
20 #include "video.hpp"
21 
22 
23 namespace gui {
24 
25 scrollarea::scrollarea(CVideo &video, const bool auto_join)
26  : widget(video, auto_join), scrollbar_(video),
27  old_position_(0), recursive_(false), shown_scrollbar_(false),
28  shown_size_(0), full_size_(0), swipe_dy_(0)
29 {
30  scrollbar_.hide(true);
31 }
32 
34 {
35  return shown_size_ < full_size_;
36 }
37 
39 {
41  h.push_back(&scrollbar_);
42  return h;
43 }
44 
45 void scrollarea::update_location(const SDL_Rect& rect)
46 {
47  SDL_Rect r = rect;
49  if (shown_scrollbar_) {
50  int w = r.w - scrollbar_.width();
51  r.x += w;
52  r.w -= w;
54  r.x -= w;
55  r.w = w;
56  }
57 
58  if (!hidden())
61 }
62 
64 {
65  if (recursive_)
66  return;
67  recursive_ = true;
69  bg_restore();
70  bg_cancel();
72  }
73  recursive_ = false;
74 }
75 
76 void scrollarea::hide(bool value)
77 {
78  widget::hide(value);
79  if (shown_scrollbar_)
80  scrollbar_.hide(value);
81 }
82 
83 unsigned scrollarea::get_position() const
84 {
85  return scrollbar_.get_position();
86 }
87 
89 {
91 }
92 
93 void scrollarea::set_position(unsigned pos)
94 {
96 }
97 
98 void scrollarea::adjust_position(unsigned pos)
99 {
101 }
102 
104 {
106 }
107 
109 {
111  shown_size_ = h;
112  test_scrollbar();
113 }
114 
116 {
118  full_size_ = h;
119  test_scrollbar();
120 }
121 
123 {
125 }
126 
128 {
129  int grip_position = scrollbar_.get_position();
130  if (grip_position == old_position_)
131  return;
132  old_position_ = grip_position;
133  scroll(grip_position);
134 }
135 
137 {
138  SDL_Rect r = location();
139  if (shown_scrollbar_)
140  r.w -= scrollbar_.width();
141  return r;
142 }
143 
145 {
146  return scrollbar_.width();
147 }
148 
149 void scrollarea::handle_event(const SDL_Event& event)
150 {
152 
153  if (mouse_locked() || hidden())
154  return;
155 
156  if (event.type == SDL_MOUSEWHEEL) {
157  const SDL_MouseWheelEvent &ev = event.wheel;
158  int x, y;
159  SDL_GetMouseState(&x, &y);
160  if (sdl::point_in_rect(x, y, inner_location())) {
161  if (ev.y > 0) {
163  } else if (ev.y < 0) {
165  }
166  }
167  }
168 
169  if (event.type == SDL_FINGERUP) {
170  swipe_dy_ = 0;
171  }
172 
173  if (event.type == SDL_FINGERDOWN || event.type == SDL_FINGERMOTION) {
174  SDL_Rect r = video().screen_area();
175  auto tx = static_cast<int>(event.tfinger.x * r.w);
176  auto ty = static_cast<int>(event.tfinger.y * r.h);
177  auto dy = static_cast<int>(event.tfinger.dy * r.h);
178 
179  if (event.type == SDL_FINGERDOWN) {
180  swipe_dy_ = 0;
181  swipe_origin_.x = tx;
182  swipe_origin_.y = ty;
183  }
184 
185  if (event.type == SDL_FINGERMOTION) {
186 
187  swipe_dy_ += dy;
188  if (scrollbar_.get_max_position() == 0) {
189  return;
190  }
191 
192  int scrollbar_step = scrollbar_.height() / scrollbar_.get_max_position();
193  if (scrollbar_step <= 0) {
194  return;
195  }
196 
198  && abs(swipe_dy_) >= scrollbar_step)
199  {
200  unsigned int pos = std::max(
201  static_cast<int>(scrollbar_.get_position() - swipe_dy_ / scrollbar_step),
202  0);
204  swipe_dy_ %= scrollbar_step;
205  }
206  }
207  }
208 
209 }
210 
211 } // 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:108
void adjust_position(unsigned pos)
Definition: scrollarea.cpp:98
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:190
bool hidden() const
Definition: widget.cpp:188
unsigned get_position() const
Determine where the scrollbar is.
Definition: scrollbar.cpp:66
unsigned scrollbar_width() const
Definition: scrollarea.cpp:144
virtual sdl_handler_vector handler_members()
Definition: scrollarea.cpp:38
Definition: video.hpp:32
struct gui::scrollarea::@28 swipe_origin_
unsigned get_max_position() const
Definition: scrollbar.cpp:71
General purpose widgets.
virtual void update_location(const SDL_Rect &rect)
Definition: scrollarea.cpp:45
#define h
void set_scroll_rate(unsigned r)
Definition: scrollarea.cpp:122
void test_scrollbar()
Definition: scrollarea.cpp:63
void move_position(int dep)
Definition: scrollarea.cpp:103
void scroll_down()
Scrolls down one step.
Definition: scrollbar.cpp:135
virtual void scroll(unsigned int pos)=0
void set_full_size(unsigned h)
Definition: scrollarea.cpp:115
bool has_scrollbar() const
Definition: scrollarea.cpp:33
virtual void handle_event(const SDL_Event &)
Definition: widget.cpp:330
void set_position(unsigned pos)
Definition: scrollarea.cpp:93
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:76
bool point_in_rect(int x, int y, const SDL_Rect &rect)
Tests whether a point is inside a rectangle.
Definition: rect.cpp:23
void adjust_position(unsigned pos)
Ensure the viewport contains the position.
Definition: scrollbar.cpp:86
unsigned full_size_
Definition: scrollarea.hpp:61
scrollbar scrollbar_
Definition: scrollarea.hpp:57
virtual void handle_event(const SDL_Event &event)
Definition: scrollarea.cpp:149
SDL_Rect inner_location() const
Definition: scrollarea.cpp:136
unsigned get_max_position() const
Definition: scrollarea.cpp:88
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:140
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:76
CVideo & video() const
Definition: widget.hpp:84
unsigned shown_size_
Definition: scrollarea.hpp:60
Contains the SDL_Rect helper code.
void set_shown_size(unsigned h)
Set the relative size of the grip.
Definition: scrollbar.cpp:103
void set_full_size(unsigned h)
Set the relative size of the scrollbar.
Definition: scrollbar.cpp:117
void set_scroll_rate(unsigned r)
Set scroll rate.
Definition: scrollbar.cpp:130
scrollarea(CVideo &video, bool auto_join=true)
Create a zone with automatic handling of scrollbar.
Definition: scrollarea.cpp:25
SDL_Rect screen_area(bool as_pixels=true) const
Returns the current window renderer area, either in pixels or screen coordinates. ...
Definition: video.cpp:271
void move_position(int dep)
Move the scrollbar.
Definition: scrollbar.cpp:94
virtual void process_event()
Definition: scrollarea.cpp:127
unsigned get_position() const
Definition: scrollarea.cpp:83