Skip to content

Commit ea33d55

Browse files
committed
stuff
1 parent 84e4451 commit ea33d55

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

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

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,28 @@ ruis::event_status flickable::on_mouse_button(const mouse_button_event& event)
7777
}
7878
case state::dragging:
7979
{
80+
utki::assert(false, SL);
8081
utki::assert(event.action == button_action::release, SL);
81-
this->cur_state = state::idle;
82-
83-
// TODO: start updating
82+
this->cur_state = state::inertial_scrolling;
8483

85-
auto vel = this->calculate_touch_velocity();
86-
std::cout << "touch release, vel = " << vel << std::endl;
84+
this->velocity = this->calculate_touch_velocity();
85+
std::cout << "touch release, vel = " << this->velocity << std::endl;
8786

8887
this->touch_history.clear();
8988

89+
this->context.get().updater.get().start(utki::make_shared_from(static_cast<updateable&>(*this)));
90+
9091
return event_status::consumed;
9192
}
93+
case state::inertial_scrolling:
94+
utki::assert(event.action == button_action::press, SL);
95+
96+
this->cur_state = state::dragging;
97+
this->prev_touch_point = event.pos;
98+
99+
this->context.get().updater.get().stop(*this);
100+
101+
return event_status::consumed;
92102
}
93103
}
94104

@@ -182,7 +192,27 @@ ruis::event_status flickable::on_mouse_move(const mouse_move_event& event)
182192

183193
void flickable::update(uint32_t dt_ms)
184194
{
185-
// TODO:
195+
utki::assert(this->cur_state == state::inertial_scrolling, SL);
196+
197+
auto scrolled_by = this->flickable_scroll_by(-this->velocity * dt_ms);
198+
199+
using std::copysign;
200+
auto velocity_sign = this->velocity.comp_op([](const auto& e){return copysign(real(1), e);});
201+
202+
auto prev_velocity = this->velocity;
203+
204+
this->velocity -= velocity_sign * this->friction * dt_ms;
205+
206+
for(auto [prev, cur, scrolled] : utki::views::zip(prev_velocity, this->velocity, scrolled_by)){
207+
using std::signbit;
208+
if(signbit(prev) != signbit(cur) || scrolled == 0){
209+
cur = ruis::real(0);
210+
}
211+
}
212+
213+
if(this->velocity.is_zero()){
214+
this->context.get().updater.get().stop(*this);
215+
}
186216
}
187217

188218
namespace {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ class flickable :
6363

6464
ruis::vec2 calculate_touch_velocity_for_at_least_3_points_using_ols_method();
6565

66-
66+
// inertial scrolling state
67+
ruis::vec2 velocity;
68+
ruis::real friction = this->context.get().units.pp_to_px(0.1);
6769

6870
public:
6971
event_status on_mouse_button(const mouse_button_event& event) override;

0 commit comments

Comments
 (0)