2018-11-22 16:24:17 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2020-12-27 00:41:04 +00:00
|
|
|
* Copyright (C) 2013-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2018-11-22 16:24:17 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2023-04-19 11:51:26 +00:00
|
|
|
* @file zoom_controller.h
|
2018-11-22 16:24:17 +00:00
|
|
|
* @brief ZOOM_CONTROLLER class definition.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __ZOOM_CONTROLLER_H
|
|
|
|
#define __ZOOM_CONTROLLER_H
|
|
|
|
|
2023-09-26 11:40:17 +00:00
|
|
|
#include <gal/gal.h>
|
2018-11-22 17:01:59 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <memory>
|
2018-11-22 16:24:17 +00:00
|
|
|
|
|
|
|
namespace KIGFX
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Handle the response of the zoom scale to external inputs.
|
2018-11-22 16:24:17 +00:00
|
|
|
*/
|
2023-09-26 11:40:17 +00:00
|
|
|
class GAL_API ZOOM_CONTROLLER
|
2018-11-22 16:24:17 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~ZOOM_CONTROLLER() = default;
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Get the scale factor produced by a given mousewheel rotation.
|
|
|
|
*
|
|
|
|
* @param aRotation rotation of the mouse wheel (this comes from
|
|
|
|
* wxMouseEvent::GetWheelRotation()).
|
|
|
|
* @return the scale factor to scroll by.
|
2018-11-22 16:24:17 +00:00
|
|
|
*/
|
|
|
|
virtual double GetScaleForRotation( int aRotation ) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class that zooms faster if scroll events happen very close together.
|
|
|
|
*/
|
2023-09-26 11:40:17 +00:00
|
|
|
class GAL_API ACCELERATING_ZOOM_CONTROLLER : public ZOOM_CONTROLLER
|
2018-11-22 16:24:17 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-12-27 00:41:04 +00:00
|
|
|
///< The type of the acceleration timeout.
|
2018-11-22 17:01:59 +00:00
|
|
|
using TIMEOUT = std::chrono::milliseconds;
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< The clock used for the timestamp (guaranteed to be monotonic).
|
2018-11-22 17:01:59 +00:00
|
|
|
using CLOCK = std::chrono::steady_clock;
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< The type of the time stamps.
|
2018-11-22 17:01:59 +00:00
|
|
|
using TIME_PT = std::chrono::time_point<CLOCK>;
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< The default timeout, after which a another scroll will not be accelerated.
|
2018-11-22 17:01:59 +00:00
|
|
|
static constexpr TIMEOUT DEFAULT_TIMEOUT = std::chrono::milliseconds( 500 );
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< The default minimum step factor for accelerating controller.
|
2020-05-17 00:19:48 +00:00
|
|
|
static constexpr double DEFAULT_ACCELERATION_SCALE = 5.0;
|
|
|
|
|
2018-11-22 17:01:59 +00:00
|
|
|
/*
|
2020-12-27 00:41:04 +00:00
|
|
|
* A class interface that provides timestamps for events.
|
2018-11-22 17:01:59 +00:00
|
|
|
*/
|
|
|
|
class TIMESTAMP_PROVIDER
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~TIMESTAMP_PROVIDER() = default;
|
|
|
|
|
|
|
|
/*
|
2020-12-27 00:41:04 +00:00
|
|
|
* @return the timestamp at the current time.
|
2018-11-22 17:01:59 +00:00
|
|
|
*/
|
|
|
|
virtual TIME_PT GetTimestamp() = 0;
|
|
|
|
};
|
|
|
|
|
2018-11-22 16:24:17 +00:00
|
|
|
/**
|
2018-11-22 17:01:59 +00:00
|
|
|
* @param aAccTimeout the timeout - if a scroll happens within this timeframe,
|
2018-11-22 16:24:17 +00:00
|
|
|
* the zoom will be faster
|
2020-12-27 00:41:04 +00:00
|
|
|
* @param aTimestampProv a provider for timestamps. If null, a default will be provided,
|
|
|
|
* which is the main steady_clock (this is probably what you want
|
|
|
|
* for real usage)
|
2018-11-22 16:24:17 +00:00
|
|
|
*/
|
2020-05-17 00:19:48 +00:00
|
|
|
ACCELERATING_ZOOM_CONTROLLER( double aScale = DEFAULT_ACCELERATION_SCALE,
|
|
|
|
const TIMEOUT& aAccTimeout = DEFAULT_TIMEOUT,
|
|
|
|
TIMESTAMP_PROVIDER* aTimestampProv = nullptr );
|
2018-11-22 16:24:17 +00:00
|
|
|
|
|
|
|
double GetScaleForRotation( int aRotation ) override;
|
|
|
|
|
2018-11-22 17:01:59 +00:00
|
|
|
TIMEOUT GetTimeout() const
|
|
|
|
{
|
|
|
|
return m_accTimeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetTimeout( const TIMEOUT& aNewTimeout )
|
|
|
|
{
|
|
|
|
m_accTimeout = aNewTimeout;
|
|
|
|
}
|
|
|
|
|
2018-11-22 16:24:17 +00:00
|
|
|
private:
|
2020-12-27 00:41:04 +00:00
|
|
|
///< The timestamp provider to use (might be provided externally).
|
2018-11-22 17:01:59 +00:00
|
|
|
TIMESTAMP_PROVIDER* m_timestampProv;
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Any provider owned by this class (the default one, if used).
|
2018-11-22 17:01:59 +00:00
|
|
|
std::unique_ptr<TIMESTAMP_PROVIDER> m_ownTimestampProv;
|
2018-11-22 16:24:17 +00:00
|
|
|
|
2022-11-04 16:28:32 +00:00
|
|
|
///< The timestamp of the previous event.
|
|
|
|
TIME_PT m_prevTimestamp;
|
2020-12-27 00:41:04 +00:00
|
|
|
|
|
|
|
///< The timeout value.
|
2018-11-22 17:01:59 +00:00
|
|
|
TIMEOUT m_accTimeout;
|
2020-05-17 00:19:48 +00:00
|
|
|
|
2022-11-04 16:28:32 +00:00
|
|
|
/// Previous rotation was positive.
|
|
|
|
bool m_prevRotationPositive = false;
|
|
|
|
|
2020-05-17 00:19:48 +00:00
|
|
|
/// A multiplier for the minimum zoom step size
|
|
|
|
double m_scale;
|
2018-11-22 16:24:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-19 11:51:26 +00:00
|
|
|
* A CONSTANT_ZOOM_CONTROLLER that zooms by a fixed factor based only on the magnitude of the scroll
|
2020-12-27 00:41:04 +00:00
|
|
|
* wheel rotation.
|
2018-11-22 16:24:17 +00:00
|
|
|
*/
|
2023-09-26 11:40:17 +00:00
|
|
|
class GAL_API CONSTANT_ZOOM_CONTROLLER : public ZOOM_CONTROLLER
|
2018-11-22 16:24:17 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* @param aScale a scaling parameter that adjusts the magnitude of the scroll. This factor
|
|
|
|
* might be dependent on the platform for comfort.
|
2018-11-22 16:24:17 +00:00
|
|
|
*/
|
|
|
|
CONSTANT_ZOOM_CONTROLLER( double aScale );
|
|
|
|
|
|
|
|
double GetScaleForRotation( int aRotation ) override;
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< A suitable (magic) scale factor for GTK3 systems.
|
2019-02-19 14:03:19 +00:00
|
|
|
static constexpr double GTK3_SCALE = 0.002;
|
2018-11-22 16:24:17 +00:00
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< A suitable (magic) scale factor for Mac systems.
|
2018-11-22 16:24:17 +00:00
|
|
|
static constexpr double MAC_SCALE = 0.01;
|
|
|
|
|
2021-03-21 23:46:23 +00:00
|
|
|
/// A suitable (magic) scale factor for Windows systems.
|
|
|
|
static constexpr double MSW_SCALE = 0.005;
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Multiplier for manual scale ssetting.
|
2020-05-17 00:19:48 +00:00
|
|
|
static constexpr double MANUAL_SCALE_FACTOR = 0.001;
|
|
|
|
|
2018-11-22 16:24:17 +00:00
|
|
|
private:
|
2020-12-27 00:41:04 +00:00
|
|
|
///< The scale factor set by the constructor.
|
2018-11-22 16:24:17 +00:00
|
|
|
double m_scale;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace KIGFX
|
|
|
|
|
|
|
|
#endif
|