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 { 280b05c6eaSNicolas Bonnefon if ( timer_id_ == 0 ) 290b05c6eaSNicolas Bonnefon timer_id_ = startTimer( TIMER_PERIOD_MS ); 300b05c6eaSNicolas Bonnefon 31*a9448ba0SNicolas Bonnefon int resistance = 0; 32*a9448ba0SNicolas Bonnefon if ( value > 0 ) 33*a9448ba0SNicolas Bonnefon resistance = position_ / 12; 34*a9448ba0SNicolas Bonnefon 35*a9448ba0SNicolas Bonnefon position_ += std::max( 0, value - resistance ); 360b05c6eaSNicolas Bonnefon if ( position_ <= 0 ) 370b05c6eaSNicolas Bonnefon position_ = 0; 380b05c6eaSNicolas Bonnefon 39*a9448ba0SNicolas Bonnefon if ( std::chrono::duration_cast<std::chrono::milliseconds> 40*a9448ba0SNicolas Bonnefon ( std::chrono::steady_clock::now() - last_update_ ).count() > TIMER_PERIOD_MS ) 41*a9448ba0SNicolas Bonnefon decreasePosition(); 42*a9448ba0SNicolas Bonnefon 43*a9448ba0SNicolas Bonnefon last_update_ = std::chrono::steady_clock::now(); 44*a9448ba0SNicolas Bonnefon 450b05c6eaSNicolas Bonnefon LOG( logDEBUG ) << "ElasticHook::move: new value " << position_; 460b05c6eaSNicolas Bonnefon 470b05c6eaSNicolas Bonnefon emit lengthChanged(); 480b05c6eaSNicolas Bonnefon } 490b05c6eaSNicolas Bonnefon 500b05c6eaSNicolas Bonnefon void ElasticHook::timerEvent( QTimerEvent* event ) 510b05c6eaSNicolas Bonnefon { 52*a9448ba0SNicolas Bonnefon if ( std::chrono::duration_cast<std::chrono::milliseconds> 53*a9448ba0SNicolas Bonnefon ( std::chrono::steady_clock::now() - last_update_ ).count() > TIMER_PERIOD_MS ) { 54*a9448ba0SNicolas Bonnefon decreasePosition(); 55*a9448ba0SNicolas Bonnefon last_update_ = std::chrono::steady_clock::now(); 56*a9448ba0SNicolas Bonnefon } 57*a9448ba0SNicolas Bonnefon } 580b05c6eaSNicolas Bonnefon 59*a9448ba0SNicolas Bonnefon void ElasticHook::decreasePosition() 60*a9448ba0SNicolas Bonnefon { 61*a9448ba0SNicolas Bonnefon static constexpr int PROP_RATIO = 10; 62*a9448ba0SNicolas Bonnefon 63*a9448ba0SNicolas Bonnefon // position_ -= DECREASE_RATE + ( ( position_/SQUARE_RATIO ) * ( position_/SQUARE_RATIO ) ); 64*a9448ba0SNicolas Bonnefon position_ -= DECREASE_RATE + ( position_/PROP_RATIO ); 650b05c6eaSNicolas Bonnefon 660b05c6eaSNicolas Bonnefon if ( position_ <= 0 ) { 670b05c6eaSNicolas Bonnefon position_ = 0; 680b05c6eaSNicolas Bonnefon killTimer( timer_id_ ); 690b05c6eaSNicolas Bonnefon timer_id_ = 0; 700b05c6eaSNicolas Bonnefon } 710b05c6eaSNicolas Bonnefon LOG( logDEBUG ) << "ElasticHook::timerEvent: new value " << position_; 720b05c6eaSNicolas Bonnefon 730b05c6eaSNicolas Bonnefon emit lengthChanged(); 740b05c6eaSNicolas Bonnefon } 75