Skip to content

Commit f4d8c72

Browse files
committed
flickable: inertial scrolling
1 parent cdc501b commit f4d8c72

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

src/ruis/util/units.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class units
119119

120120
/**
121121
* @brief Convert millimeters to pixels (dots).
122+
* The resulting value is rounded.
122123
* @param mm - value in millimeters.
123124
* @return Value in pixels.
124125
*/
@@ -130,6 +131,7 @@ class units
130131

131132
/**
132133
* @brief Convert perception pixels to pixels.
134+
* The resulting value is rounded.
133135
* @param pp - value in perception pixels.
134136
* @return Value in pixels.
135137
*/

src/ruis/widget/base/touch/flickable.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ ruis::event_status flickable::on_mouse_button(const mouse_button_event& event)
8181
utki::assert(event.action == button_action::release, SL);
8282
this->cur_state = state::inertial_scrolling;
8383

84-
this->velocity = this->calculate_touch_velocity_px_per_ms();
85-
std::cout << "touch release, vel = " << this->velocity << std::endl;
84+
this->velocity_px_per_ms = this->calculate_touch_velocity_px_per_ms();
85+
// std::cout << "touch release, vel = " << this->velocity_px_per_ms << std::endl;
8686

8787
this->touch_history.clear();
8888

@@ -194,24 +194,30 @@ void flickable::update(uint32_t dt_ms)
194194
{
195195
utki::assert(this->cur_state == state::inertial_scrolling, SL);
196196

197-
auto scrolled_by = this->flickable_scroll_by(-this->velocity * dt_ms);
197+
auto scrolled_by = this->flickable_scroll_by(-this->velocity_px_per_ms * ruis::real(dt_ms));
198198

199199
using std::copysign;
200-
auto velocity_sign = this->velocity.comp_op([](const auto& e){return copysign(real(1), e);});
200+
auto velocity_sign = this->velocity_px_per_ms.comp_op([](const auto& e){return copysign(real(1), e);});
201201

202-
auto prev_velocity = this->velocity;
202+
auto prev_velocity_px_per_ms = this->velocity_px_per_ms;
203203

204-
this->velocity -= velocity_sign * this->friction * dt_ms;
204+
// std::cout << "velocity_sign = " << velocity_sign << ", this->friction = " << this->friction << ", dt_ms = " << dt_ms << std::endl;
205+
auto dv = velocity_sign * this->friction * ruis::real(dt_ms);
206+
// std::cout << "dv = " << dv << std::endl;
207+
this->velocity_px_per_ms -= dv;
205208

206-
for(auto [prev, cur, scrolled] : utki::views::zip(prev_velocity, this->velocity, scrolled_by)){
209+
// std::cout << "this->velocity_px_per_ms = " << this->velocity_px_per_ms << std::endl;
210+
211+
for(auto [prev, cur, scrolled_px] : utki::views::zip(prev_velocity_px_per_ms, this->velocity_px_per_ms, scrolled_by)){
207212
using std::signbit;
208-
if(signbit(prev) != signbit(cur) || scrolled == 0){
213+
if(signbit(prev) != signbit(cur) || scrolled_px == 0){
209214
cur = ruis::real(0);
210215
}
211216
}
212217

213-
if(this->velocity.is_zero()){
218+
if(this->velocity_px_per_ms.is_zero()){
214219
this->context.get().updater.get().stop(*this);
220+
this->cur_state = state::idle;
215221
}
216222
}
217223

src/ruis/widget/base/touch/flickable.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class flickable :
3333
{
3434
constexpr static auto scroll_threshold_pp = 5;
3535

36-
// TODO: convert from scroll_threshold_pp in on_reload()?
36+
// TODO: convert from scroll_threshold_pp if dots per pp changes
3737
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers, "constant value")
3838
real scroll_threshold_px = 10;
3939

@@ -63,8 +63,8 @@ class flickable :
6363
ruis::vec2 calculate_touch_velocity_for_at_least_3_points_using_ols_method_px_per_ms();
6464

6565
// inertial scrolling state
66-
ruis::vec2 velocity;
67-
ruis::real friction = this->context.get().units.pp_to_px(0.1);
66+
ruis::real friction = this->context.get().units.dots_per_pp() * 0.005; // TODO: update if dots per pp changes
67+
ruis::vec2 velocity_px_per_ms;
6868

6969
public:
7070
event_status on_mouse_button(const mouse_button_event& event) override;

0 commit comments

Comments
 (0)