axmol/core/base/CCTouch.h

177 lines
5.2 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
https://axis-project.github.io/
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
2022-07-16 10:43:05 +08:00
#ifndef __AX_TOUCH_H__
#define __AX_TOUCH_H__
2019-11-23 20:27:39 +08:00
#include "base/CCRef.h"
2021-10-23 23:27:14 +08:00
#include "math/CCMath.h"
2019-11-23 20:27:39 +08:00
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
/**
* @addtogroup base
* @{
*/
/** @class Touch
* @brief Encapsulates the Touch information, such as touch point, id and so on,
and provides the methods that commonly used.
*/
2022-07-16 10:43:05 +08:00
class AX_DLL Touch : public Ref
2019-11-23 20:27:39 +08:00
{
public:
2021-12-25 10:04:45 +08:00
/**
2019-11-23 20:27:39 +08:00
* Dispatch mode, how the touches are dispatched.
* @js NA
*/
2021-12-25 10:04:45 +08:00
enum class DispatchMode
{
2019-11-23 20:27:39 +08:00
ALL_AT_ONCE, /** All at once. */
ONE_BY_ONE, /** One by one. */
};
/** Constructor.
* @js ctor
*/
2021-12-25 10:04:45 +08:00
Touch() : _id(0), _startPointCaptured(false), _curForce(0.f), _maxForce(0.f) {}
2019-11-23 20:27:39 +08:00
/** Returns the current touch location in OpenGL coordinates.
*
* @return The current touch location in OpenGL coordinates.
*/
Vec2 getLocation() const;
/** Returns the previous touch location in OpenGL coordinates.
*
* @return The previous touch location in OpenGL coordinates.
*/
Vec2 getPreviousLocation() const;
/** Returns the start touch location in OpenGL coordinates.
*
* @return The start touch location in OpenGL coordinates.
*/
Vec2 getStartLocation() const;
/** Returns the delta of 2 current touches locations in screen coordinates.
*
* @return The delta of 2 current touches locations in screen coordinates.
*/
Vec2 getDelta() const;
/** Returns the current touch location in screen coordinates.
*
* @return The current touch location in screen coordinates.
*/
Vec2 getLocationInView() const;
2021-12-25 10:04:45 +08:00
/** Returns the previous touch location in screen coordinates.
2019-11-23 20:27:39 +08:00
*
* @return The previous touch location in screen coordinates.
*/
Vec2 getPreviousLocationInView() const;
/** Returns the start touch location in screen coordinates.
*
* @return The start touch location in screen coordinates.
*/
Vec2 getStartLocationInView() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Set the touch information. It always used to monitor touch event.
*
* @param id A given id
* @param x A given x coordinate.
* @param y A given y coordinate.
*/
void setTouchInfo(int id, float x, float y)
{
2021-12-25 10:04:45 +08:00
_id = id;
2019-11-23 20:27:39 +08:00
_prevPoint = _point;
_point.x = x;
_point.y = y;
2021-12-25 10:04:45 +08:00
_curForce = 0.0f;
_maxForce = 0.0f;
2019-11-23 20:27:39 +08:00
if (!_startPointCaptured)
{
2021-12-25 10:04:45 +08:00
_startPoint = _point;
2019-11-23 20:27:39 +08:00
_startPointCaptured = true;
2021-12-25 10:04:45 +08:00
_prevPoint = _point;
2019-11-23 20:27:39 +08:00
}
}
/** Set the touch information. It always used to monitor touch event.
*
* @param id A given id
* @param x A given x coordinate.
* @param y A given y coordinate.
* @param force Current force for 3d touch.
* @param maxForce maximum possible force for 3d touch.
*/
void setTouchInfo(int id, float x, float y, float force, float maxForce)
{
2021-12-25 10:04:45 +08:00
_id = id;
2019-11-23 20:27:39 +08:00
_prevPoint = _point;
_point.x = x;
_point.y = y;
2021-12-25 10:04:45 +08:00
_curForce = force;
_maxForce = maxForce;
2019-11-23 20:27:39 +08:00
if (!_startPointCaptured)
{
2021-12-25 10:04:45 +08:00
_startPoint = _point;
2019-11-23 20:27:39 +08:00
_startPointCaptured = true;
2021-12-25 10:04:45 +08:00
_prevPoint = _point;
2019-11-23 20:27:39 +08:00
}
}
/** Get touch id.
* @js getId
* @lua getId
*
* @return The id of touch.
*/
2021-12-25 10:04:45 +08:00
int getID() const { return _id; }
2019-11-23 20:27:39 +08:00
/** Returns the current touch force for 3d touch.
*
* @return The current touch force for 3d touch.
*/
float getCurrentForce() const;
/** Returns the maximum touch force for 3d touch.
*
* @return The maximum touch force for 3d touch.
*/
float getMaxForce() const;
private:
int _id;
bool _startPointCaptured;
Vec2 _startPoint;
Vec2 _point;
Vec2 _prevPoint;
float _curForce;
float _maxForce;
};
// end of base group
/// @}
NS_AX_END
2019-11-23 20:27:39 +08:00
#endif // __PLATFORM_TOUCH_H__