xref: /glogg/src/viewtools.cpp (revision ecd5e5f04f693586ee5ae569c38bac80c0547cd0)
10b05c6eaSNicolas Bonnefon /*
20b05c6eaSNicolas Bonnefon  * Copyright (C) 2015 Nicolas Bonnefon and other contributors
30b05c6eaSNicolas Bonnefon  *
40b05c6eaSNicolas Bonnefon  * This file is part of glogg.
50b05c6eaSNicolas Bonnefon  *
60b05c6eaSNicolas Bonnefon  * glogg is free software: you can redistribute it and/or modify
70b05c6eaSNicolas Bonnefon  * it under the terms of the GNU General Public License as published by
80b05c6eaSNicolas Bonnefon  * the Free Software Foundation, either version 3 of the License, or
90b05c6eaSNicolas Bonnefon  * (at your option) any later version.
100b05c6eaSNicolas Bonnefon  *
110b05c6eaSNicolas Bonnefon  * glogg is distributed in the hope that it will be useful,
120b05c6eaSNicolas Bonnefon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
130b05c6eaSNicolas Bonnefon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
140b05c6eaSNicolas Bonnefon  * GNU General Public License for more details.
150b05c6eaSNicolas Bonnefon  *
160b05c6eaSNicolas Bonnefon  * You should have received a copy of the GNU General Public License
170b05c6eaSNicolas Bonnefon  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
180b05c6eaSNicolas Bonnefon  */
190b05c6eaSNicolas Bonnefon 
200b05c6eaSNicolas Bonnefon #include "viewtools.h"
210b05c6eaSNicolas Bonnefon 
220b05c6eaSNicolas Bonnefon #include "log.h"
230b05c6eaSNicolas Bonnefon 
240b05c6eaSNicolas Bonnefon /* ElasticHook */
250b05c6eaSNicolas Bonnefon 
260b05c6eaSNicolas Bonnefon void ElasticHook::move( int value )
270b05c6eaSNicolas Bonnefon {
28819e4a32SNicolas Bonnefon     static constexpr int MAX_POSITION = 2000;
29819e4a32SNicolas Bonnefon 
300b05c6eaSNicolas Bonnefon     if ( timer_id_ == 0 )
310b05c6eaSNicolas Bonnefon         timer_id_ = startTimer( TIMER_PERIOD_MS );
320b05c6eaSNicolas Bonnefon 
33a9448ba0SNicolas Bonnefon     int resistance = 0;
34819e4a32SNicolas Bonnefon     if ( !held_ && ( position_ * value > 0 ) ) // value and resistance have the same sign
35*ecd5e5f0SNicolas Bonnefon         resistance = position_ / 8;
36a9448ba0SNicolas Bonnefon 
37819e4a32SNicolas Bonnefon     position_ = std::min( position_ + ( value - resistance ), MAX_POSITION );
380b05c6eaSNicolas Bonnefon 
39819e4a32SNicolas Bonnefon     if ( !held_ && ( std::chrono::duration_cast<std::chrono::milliseconds>
40819e4a32SNicolas Bonnefon             ( std::chrono::steady_clock::now() - last_update_ ).count() > TIMER_PERIOD_MS ) )
41a9448ba0SNicolas Bonnefon         decreasePosition();
42a9448ba0SNicolas Bonnefon 
43ac6602a5SNicolas Bonnefon     if ( ( ! hooked_ ) && position_ >= hook_threshold_ ) {
44ac6602a5SNicolas Bonnefon         position_ -= hook_threshold_;
45ac6602a5SNicolas Bonnefon         hooked_ = true;
46ac6602a5SNicolas Bonnefon         emit hooked( true );
47ac6602a5SNicolas Bonnefon     }
48ac6602a5SNicolas Bonnefon     else if ( hooked_ && position_ <= - hook_threshold_ ) {
49ac6602a5SNicolas Bonnefon         position_ += hook_threshold_;
50ac6602a5SNicolas Bonnefon         hooked_ = false;
51ac6602a5SNicolas Bonnefon         emit hooked( false );
52ac6602a5SNicolas Bonnefon     }
53ac6602a5SNicolas Bonnefon 
54ac6602a5SNicolas Bonnefon     if ( position_ < 0 && !isHooked() )
55ac6602a5SNicolas Bonnefon         position_ = 0;
56ac6602a5SNicolas Bonnefon 
57a9448ba0SNicolas Bonnefon     last_update_ = std::chrono::steady_clock::now();
58a9448ba0SNicolas Bonnefon 
590b05c6eaSNicolas Bonnefon     LOG( logDEBUG ) << "ElasticHook::move: new value " << position_;
600b05c6eaSNicolas Bonnefon 
610b05c6eaSNicolas Bonnefon     emit lengthChanged();
620b05c6eaSNicolas Bonnefon }
630b05c6eaSNicolas Bonnefon 
640b05c6eaSNicolas Bonnefon void ElasticHook::timerEvent( QTimerEvent* event )
650b05c6eaSNicolas Bonnefon {
66819e4a32SNicolas Bonnefon     if ( !held_ && ( std::chrono::duration_cast<std::chrono::milliseconds>
67819e4a32SNicolas Bonnefon             ( std::chrono::steady_clock::now() - last_update_ ).count() > TIMER_PERIOD_MS ) ) {
68a9448ba0SNicolas Bonnefon         decreasePosition();
69a9448ba0SNicolas Bonnefon         last_update_ = std::chrono::steady_clock::now();
70a9448ba0SNicolas Bonnefon     }
71a9448ba0SNicolas Bonnefon }
720b05c6eaSNicolas Bonnefon 
73a9448ba0SNicolas Bonnefon void ElasticHook::decreasePosition()
74a9448ba0SNicolas Bonnefon {
75a9448ba0SNicolas Bonnefon     static constexpr int PROP_RATIO = 10;
76a9448ba0SNicolas Bonnefon 
77a9448ba0SNicolas Bonnefon     // position_ -= DECREASE_RATE + ( ( position_/SQUARE_RATIO ) * ( position_/SQUARE_RATIO ) );
78ac6602a5SNicolas Bonnefon     if ( std::abs( position_ ) < DECREASE_RATE ) {
790b05c6eaSNicolas Bonnefon         position_ = 0;
800b05c6eaSNicolas Bonnefon         killTimer( timer_id_ );
810b05c6eaSNicolas Bonnefon         timer_id_ = 0;
820b05c6eaSNicolas Bonnefon     }
83ac6602a5SNicolas Bonnefon     else if ( position_ > 0 )
84ac6602a5SNicolas Bonnefon         position_ -= DECREASE_RATE + ( position_/PROP_RATIO );
85ac6602a5SNicolas Bonnefon     else if ( position_ < 0 )
86ac6602a5SNicolas Bonnefon         position_ += DECREASE_RATE - ( position_/PROP_RATIO );
87ac6602a5SNicolas Bonnefon 
880b05c6eaSNicolas Bonnefon     LOG( logDEBUG ) << "ElasticHook::timerEvent: new value " << position_;
890b05c6eaSNicolas Bonnefon 
900b05c6eaSNicolas Bonnefon     emit lengthChanged();
910b05c6eaSNicolas Bonnefon }
92