@@ -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
183193void 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
188218namespace {
0 commit comments