michael@0: /* michael@0: * Copyright (C) 2012 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #ifndef _ANDROIDFW_VELOCITY_CONTROL_H michael@0: #define _ANDROIDFW_VELOCITY_CONTROL_H michael@0: michael@0: #include "Input.h" michael@0: #include "VelocityTracker.h" michael@0: #include michael@0: michael@0: namespace android { michael@0: michael@0: /* michael@0: * Specifies parameters that govern pointer or wheel acceleration. michael@0: */ michael@0: struct VelocityControlParameters { michael@0: // A scale factor that is multiplied with the raw velocity deltas michael@0: // prior to applying any other velocity control factors. The scale michael@0: // factor should be used to adapt the input device resolution michael@0: // (eg. counts per inch) to the output device resolution (eg. pixels per inch). michael@0: // michael@0: // Must be a positive value. michael@0: // Default is 1.0 (no scaling). michael@0: float scale; michael@0: michael@0: // The scaled speed at which acceleration begins to be applied. michael@0: // This value establishes the upper bound of a low speed regime for michael@0: // small precise motions that are performed without any acceleration. michael@0: // michael@0: // Must be a non-negative value. michael@0: // Default is 0.0 (no low threshold). michael@0: float lowThreshold; michael@0: michael@0: // The scaled speed at which maximum acceleration is applied. michael@0: // The difference between highThreshold and lowThreshold controls michael@0: // the range of speeds over which the acceleration factor is interpolated. michael@0: // The wider the range, the smoother the acceleration. michael@0: // michael@0: // Must be a non-negative value greater than or equal to lowThreshold. michael@0: // Default is 0.0 (no high threshold). michael@0: float highThreshold; michael@0: michael@0: // The acceleration factor. michael@0: // When the speed is above the low speed threshold, the velocity will scaled michael@0: // by an interpolated value between 1.0 and this amount. michael@0: // michael@0: // Must be a positive greater than or equal to 1.0. michael@0: // Default is 1.0 (no acceleration). michael@0: float acceleration; michael@0: michael@0: VelocityControlParameters() : michael@0: scale(1.0f), lowThreshold(0.0f), highThreshold(0.0f), acceleration(1.0f) { michael@0: } michael@0: michael@0: VelocityControlParameters(float scale, float lowThreshold, michael@0: float highThreshold, float acceleration) : michael@0: scale(scale), lowThreshold(lowThreshold), michael@0: highThreshold(highThreshold), acceleration(acceleration) { michael@0: } michael@0: }; michael@0: michael@0: /* michael@0: * Implements mouse pointer and wheel speed control and acceleration. michael@0: */ michael@0: class VelocityControl { michael@0: public: michael@0: VelocityControl(); michael@0: michael@0: /* Sets the various parameters. */ michael@0: void setParameters(const VelocityControlParameters& parameters); michael@0: michael@0: /* Resets the current movement counters to zero. michael@0: * This has the effect of nullifying any acceleration. */ michael@0: void reset(); michael@0: michael@0: /* Translates a raw movement delta into an appropriately michael@0: * scaled / accelerated delta based on the current velocity. */ michael@0: void move(nsecs_t eventTime, float* deltaX, float* deltaY); michael@0: michael@0: private: michael@0: // If no movements are received within this amount of time, michael@0: // we assume the movement has stopped and reset the movement counters. michael@0: static const nsecs_t STOP_TIME = 500 * 1000000; // 500 ms michael@0: michael@0: VelocityControlParameters mParameters; michael@0: michael@0: nsecs_t mLastMovementTime; michael@0: VelocityTracker::Position mRawPosition; michael@0: VelocityTracker mVelocityTracker; michael@0: }; michael@0: michael@0: } // namespace android michael@0: michael@0: #endif // _ANDROIDFW_VELOCITY_CONTROL_H