modify gesturelibraries.h ,add includes

This commit is contained in:
侯歌 2024-07-29 18:25:17 +08:00
parent 2d0cc455f6
commit a550883e9f
4 changed files with 88 additions and 90 deletions

View File

@ -3,6 +3,7 @@
#include <string> #include <string>
#include <gesture/instance.h> #include <gesture/instance.h>
#include <gesture/gesturelibrary.h> #include <gesture/gesturelibrary.h>
#include <gesture/gestureutils.h>
namespace cdroid{ namespace cdroid{
class Context; class Context;
class GestureLibrary; class GestureLibrary;

View File

@ -4,13 +4,10 @@ namespace cdroid{
//private final AccelerateDecelerateInterpolator mInterpolator =new AccelerateDecelerateInterpolator(); //private final AccelerateDecelerateInterpolator mInterpolator =new AccelerateDecelerateInterpolator();
//private final FadeOutRunnable mFadingOut = new FadeOutRunnable(); //private final FadeOutRunnable mFadingOut = new FadeOutRunnable();
GestureOverlayView::GestureOverlayView(Context* context,const AttributeSet& attrs) { GestureOverlayView::GestureOverlayView(Context* context,const AttributeSet& attrs)
super(context, attrs, defStyleAttr, defStyleRes); :FrameLayout(context, attrs){
final TypedArray a = context.obtainStyledAttributes( /*mGestureStrokeWidth = a.getFloat(R.styleable.GestureOverlayView_gestureStrokeWidth,
attrs, R.styleable.GestureOverlayView, defStyleAttr, defStyleRes);
mGestureStrokeWidth = a.getFloat(R.styleable.GestureOverlayView_gestureStrokeWidth,
mGestureStrokeWidth); mGestureStrokeWidth);
mInvalidateExtraBorder = std::max(1, ((int) mGestureStrokeWidth) - 1); mInvalidateExtraBorder = std::max(1, ((int) mGestureStrokeWidth) - 1);
mCertainGestureColor = a.getColor(R.styleable.GestureOverlayView_gestureColor, mCertainGestureColor = a.getColor(R.styleable.GestureOverlayView_gestureColor,
@ -34,30 +31,29 @@ GestureOverlayView::GestureOverlayView(Context* context,const AttributeSet& attr
mInterceptEvents); mInterceptEvents);
mFadeEnabled = a.getBoolean(R.styleable.GestureOverlayView_fadeEnabled, mFadeEnabled = a.getBoolean(R.styleable.GestureOverlayView_fadeEnabled,
mFadeEnabled); mFadeEnabled);
mOrientation = a.getInt(R.styleable.GestureOverlayView_orientation, mOrientation); mOrientation = a.getInt(R.styleable.GestureOverlayView_orientation, mOrientation);*/
a.recycle();
init(); init();
} }
void GestureOverlayView::init() { void GestureOverlayView::init() {
setWillNotDraw(false); setWillNotDraw(false);
mFadingOut = std::bind(&GestureOverlayView::FadeOutProc,this);
final Paint gesturePaint = mGesturePaint; /*Paint gesturePaint = mGesturePaint;
gesturePaint.setAntiAlias(GESTURE_RENDERING_ANTIALIAS); gesturePaint.setAntiAlias(GESTURE_RENDERING_ANTIALIAS);
gesturePaint.setColor(mCertainGestureColor); gesturePaint.setColor(mCertainGestureColor);
gesturePaint.setStyle(Paint.Style.STROKE); gesturePaint.setStyle(Paint.Style.STROKE);
gesturePaint.setStrokeJoin(Paint.Join.ROUND); gesturePaint.setStrokeJoin(Paint.Join.ROUND);
gesturePaint.setStrokeCap(Paint.Cap.ROUND); gesturePaint.setStrokeCap(Paint.Cap.ROUND);
gesturePaint.setStrokeWidth(mGestureStrokeWidth); gesturePaint.setStrokeWidth(mGestureStrokeWidth);
gesturePaint.setDither(DITHER_FLAG); gesturePaint.setDither(DITHER_FLAG);*/
mCurrentColor = mCertainGestureColor; mCurrentColor = mCertainGestureColor;
setPaintAlpha(255); setPaintAlpha(255);
} }
std::vector<GesturePoint> GestureOverlayView::getCurrentStroke() const{ const std::vector<GesturePoint>& GestureOverlayView::getCurrentStroke() const{
return mStrokeBuffer; return mStrokeBuffer;
} }
@ -111,7 +107,7 @@ void GestureOverlayView::setGestureStrokeLengthThreshold(float gestureStrokeLeng
mGestureStrokeLengthThreshold = gestureStrokeLengthThreshold; mGestureStrokeLengthThreshold = gestureStrokeLengthThreshold;
} }
float GestureOverlayView::getGestureStrokeSquarenessTreshold() { float GestureOverlayView::getGestureStrokeSquarenessTreshold() const{
return mGestureStrokeSquarenessTreshold; return mGestureStrokeSquarenessTreshold;
} }
@ -143,7 +139,7 @@ void GestureOverlayView::setFadeEnabled(bool fadeEnabled) {
mFadeEnabled = fadeEnabled; mFadeEnabled = fadeEnabled;
} }
Gesture* GestureOverlayView::getGesture() { Gesture* GestureOverlayView::getGesture() const{
return mCurrentGesture; return mCurrentGesture;
} }
@ -155,7 +151,7 @@ void GestureOverlayView::setGesture(Gesture* gesture) {
setCurrentColor(mCertainGestureColor); setCurrentColor(mCertainGestureColor);
mCurrentGesture = gesture; mCurrentGesture = gesture;
final Path path = mCurrentGesture.toPath(); Path path = mCurrentGesture->toPath();
RectF bounds; RectF bounds;
path.computeBounds(bounds, true); path.computeBounds(bounds, true);
@ -273,8 +269,8 @@ void GestureOverlayView::clear(bool animated, bool fireActionPerformed, bool imm
setPaintAlpha(255); setPaintAlpha(255);
removeCallbacks(mFadingOut); removeCallbacks(mFadingOut);
mResetGesture = false; mResetGesture = false;
mFadingOut.fireActionPerformed = fireActionPerformed; mFireActionPerformed = fireActionPerformed;
mFadingOut.resetMultipleStrokes = false; mResetMultipleStrokes = false;
if (animated && mCurrentGesture) { if (animated && mCurrentGesture) {
mFadingAlpha = 1.0f; mFadingAlpha = 1.0f;
@ -289,16 +285,16 @@ void GestureOverlayView::clear(bool animated, bool fireActionPerformed, bool imm
mFadingHasStarted = false; mFadingHasStarted = false;
if (immediate) { if (immediate) {
mCurrentGesture = null; mCurrentGesture = nullptr;
mPath.rewind(); mPath.rewind();
invalidate(); invalidate();
} else if (fireActionPerformed) { } else if (fireActionPerformed) {
postDelayed(mFadingOut, mFadeOffset); postDelayed(mFadingOut, mFadeOffset);
} else if (mGestureStrokeType == GESTURE_STROKE_TYPE_MULTIPLE) { } else if (mGestureStrokeType == GESTURE_STROKE_TYPE_MULTIPLE) {
mFadingOut.resetMultipleStrokes = true; mResetMultipleStrokes = true;
postDelayed(mFadingOut, mFadeOffset); postDelayed(mFadingOut, mFadeOffset);
} else { } else {
mCurrentGesture = null; mCurrentGesture = nullptr;
mPath.rewind(); mPath.rewind();
invalidate(); invalidate();
} }
@ -311,7 +307,7 @@ void GestureOverlayView::cancelClearAnimation() {
mFadingHasStarted = false; mFadingHasStarted = false;
removeCallbacks(mFadingOut); removeCallbacks(mFadingOut);
mPath.rewind(); mPath.rewind();
mCurrentGesture = null; mCurrentGesture = nullptr;
} }
void GestureOverlayView::cancelGesture() { void GestureOverlayView::cancelGesture() {
@ -327,7 +323,7 @@ void GestureOverlayView::cancelGesture() {
std::vector<OnGestureListener>& listeners = mOnGestureListeners; std::vector<OnGestureListener>& listeners = mOnGestureListeners;
int count = listeners.size(); int count = listeners.size();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
listeners.get(i).onGestureCancelled(this, event); listeners.at(i).onGestureCancelled(*this, *event);
} }
event->recycle(); event->recycle();
@ -340,12 +336,12 @@ void GestureOverlayView::cancelGesture() {
std::vector<OnGesturingListener>& otherListeners = mOnGesturingListeners; std::vector<OnGesturingListener>& otherListeners = mOnGesturingListeners;
count = otherListeners.size(); count = otherListeners.size();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
otherListeners.at(i).onGesturingEnded(this); otherListeners.at(i).onGesturingEnded(*this);
} }
} }
void GestureOverlayView::onDetachedFromWindow() { void GestureOverlayView::onDetachedFromWindow() {
super.onDetachedFromWindow(); FrameLayout::onDetachedFromWindow();
cancelClearAnimation(); cancelClearAnimation();
} }
@ -414,7 +410,7 @@ void GestureOverlayView::touchDown(MotionEvent& event) {
if (mGestureStrokeType == GESTURE_STROKE_TYPE_SINGLE || mResetGesture) { if (mGestureStrokeType == GESTURE_STROKE_TYPE_SINGLE || mResetGesture) {
if (mHandleGestureActions) setCurrentColor(mUncertainGestureColor); if (mHandleGestureActions) setCurrentColor(mUncertainGestureColor);
mResetGesture = false; mResetGesture = false;
mCurrentGesture = null; mCurrentGesture = nullptr;
mPath.rewind(); mPath.rewind();
} else if (mCurrentGesture == nullptr || mCurrentGesture->getStrokesCount() == 0) { } else if (mCurrentGesture == nullptr || mCurrentGesture->getStrokesCount() == 0) {
if (mHandleGestureActions) setCurrentColor(mUncertainGestureColor); if (mHandleGestureActions) setCurrentColor(mUncertainGestureColor);
@ -430,7 +426,7 @@ void GestureOverlayView::touchDown(MotionEvent& event) {
removeCallbacks(mFadingOut); removeCallbacks(mFadingOut);
} }
if (mCurrentGesture == null) { if (mCurrentGesture == nullptr) {
mCurrentGesture = new Gesture(); mCurrentGesture = new Gesture();
} }
@ -447,7 +443,7 @@ void GestureOverlayView::touchDown(MotionEvent& event) {
std::vector<OnGestureListener>& listeners = mOnGestureListeners; std::vector<OnGestureListener>& listeners = mOnGestureListeners;
const int count = listeners.size(); const int count = listeners.size();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
listeners.at(i).onGestureStarted(this, event); listeners.at(i).onGestureStarted(*this, event);
} }
} }
@ -495,12 +491,12 @@ Rect GestureOverlayView::touchMove(MotionEvent& event) {
if (mTotalLength > mGestureStrokeLengthThreshold) { if (mTotalLength > mGestureStrokeLengthThreshold) {
OrientedBoundingBox* box = GestureUtils::computeOrientedBoundingBox(mStrokeBuffer); OrientedBoundingBox* box = GestureUtils::computeOrientedBoundingBox(mStrokeBuffer);
float angle = std::abs(box.orientation); float angle = std::abs(box->orientation);
if (angle > 90.f) { if (angle > 90.f) {
angle = 180.f - angle; angle = 180.f - angle;
} }
if (box.squareness > mGestureStrokeSquarenessTreshold || if (box->squareness > mGestureStrokeSquarenessTreshold ||
(mOrientation == ORIENTATION_VERTICAL ? (mOrientation == ORIENTATION_VERTICAL ?
angle < mGestureStrokeAngleThreshold : angle < mGestureStrokeAngleThreshold :
angle > mGestureStrokeAngleThreshold)) { angle > mGestureStrokeAngleThreshold)) {
@ -511,9 +507,10 @@ Rect GestureOverlayView::touchMove(MotionEvent& event) {
std::vector<OnGesturingListener>& listeners = mOnGesturingListeners; std::vector<OnGesturingListener>& listeners = mOnGesturingListeners;
const int count = listeners.size(); const int count = listeners.size();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
listeners.at(i).onGesturingStarted(this); listeners.at(i).onGesturingStarted(*this);
} }
} }
delete box;
} }
} }
@ -521,7 +518,7 @@ Rect GestureOverlayView::touchMove(MotionEvent& event) {
std::vector<OnGestureListener>& listeners = mOnGestureListeners; std::vector<OnGestureListener>& listeners = mOnGestureListeners;
const int count = listeners.size(); const int count = listeners.size();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
listeners.at(i).onGesture(this, event); listeners.at(i).onGesture(*this, event);
} }
} }
@ -541,7 +538,7 @@ void GestureOverlayView::touchUp(MotionEvent& event, bool cancel) {
std::vector<OnGestureListener>& listeners = mOnGestureListeners; std::vector<OnGestureListener>& listeners = mOnGestureListeners;
const int count = listeners.size(); const int count = listeners.size();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
listeners.at(i).onGestureEnded(this, event); listeners.at(i).onGestureEnded(*this, event);
} }
clear(mHandleGestureActions && mFadeEnabled, mHandleGestureActions && mIsGesturing, clear(mHandleGestureActions && mFadeEnabled, mHandleGestureActions && mIsGesturing,
@ -561,7 +558,7 @@ void GestureOverlayView::touchUp(MotionEvent& event, bool cancel) {
std::vector<OnGesturingListener>& listeners = mOnGesturingListeners; std::vector<OnGesturingListener>& listeners = mOnGesturingListeners;
const int count = listeners.size(); const int count = listeners.size();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
listeners.at(i).onGesturingEnded(this); listeners.at(i).onGesturingEnded(*this);
} }
} }
@ -570,7 +567,7 @@ void GestureOverlayView::cancelGesture(MotionEvent& event) {
std::vector<OnGestureListener>& listeners = mOnGestureListeners; std::vector<OnGestureListener>& listeners = mOnGestureListeners;
const int count = listeners.size(); const int count = listeners.size();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
listeners.at(i).onGestureCancelled(this, event); listeners.at(i).onGestureCancelled(*this, event);
} }
clear(false); clear(false);
@ -580,51 +577,49 @@ void GestureOverlayView::fireOnGesturePerformed() {
std::vector<OnGesturePerformedListener>& actionListeners = mOnGesturePerformedListeners; std::vector<OnGesturePerformedListener>& actionListeners = mOnGesturePerformedListeners;
const int count = actionListeners.size(); const int count = actionListeners.size();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
actionListeners.at(i).onGesturePerformed(GestureOverlayView.this, mCurrentGesture); actionListeners.at(i).onGesturePerformed(*this, *mCurrentGesture);
} }
} }
class FadeOutRunnable implements Runnable { void GestureOverlayView::FadeOutProc(){
bool fireActionPerformed; //bool fireActionPerformed;
bool resetMultipleStrokes; //bool resetMultipleStrokes;
void run() { if (mIsFadingOut) {
if (mIsFadingOut) { const long now = AnimationUtils::currentAnimationTimeMillis();
const long now = AnimationUtils::currentAnimationTimeMillis(); const long duration = now - mFadingStart;
const long duration = now - mFadingStart;
if (duration > mFadeDuration) { if (duration > mFadeDuration) {
if (fireActionPerformed) { if (mFireActionPerformed) {
fireOnGesturePerformed(); mFireOnGesturePerformed();
}
mPreviousWasGesturing = false;
mIsFadingOut = false;
mFadingHasStarted = false;
mPath.rewind();
mCurrentGesture = null;
setPaintAlpha(255);
} else {
mFadingHasStarted = true;
float interpolatedTime = std::max(0.0f, std::min(1.0f, duration / (float) mFadeDuration));
mFadingAlpha = 1.0f - mInterpolator.getInterpolation(interpolatedTime);
setPaintAlpha(int(255 * mFadingAlpha));
postDelayed(this, FADE_ANIMATION_RATE);
} }
} else if (resetMultipleStrokes) {
mResetGesture = true;
} else {
fireOnGesturePerformed();
mPreviousWasGesturing = false;
mIsFadingOut = false;
mFadingHasStarted = false; mFadingHasStarted = false;
mPath.rewind(); mPath.rewind();
mCurrentGesture = null; mCurrentGesture = nullptr;
mPreviousWasGesturing = false;
setPaintAlpha(255); setPaintAlpha(255);
} else {
mFadingHasStarted = true;
float interpolatedTime = std::max(0.0f, std::min(1.0f, duration / (float) mFadeDuration));
mFadingAlpha = 1.0f - mInterpolator->getInterpolation(interpolatedTime);
setPaintAlpha(int(255 * mFadingAlpha));
postDelayed(this, FADE_ANIMATION_RATE);
} }
} else if (mResetMultipleStrokes) {
mResetGesture = true;
} else {
fireOnGesturePerformed();
invalidate(); mFadingHasStarted = false;
mPath.rewind();
mCurrentGesture = nullptr;
mPreviousWasGesturing = false;
setPaintAlpha(255);
} }
invalidate();
} }
}/*endof namespace*/ }/*endof namespace*/
#endif #endif

View File

@ -1,5 +1,5 @@
#include <wiget/framelayout.h> #include <widget/framelayout.h>
#include <gesture/gesturelibraries.h>
namespace cdroid{ namespace cdroid{
class GestureOverlayView:public FrameLayout { class GestureOverlayView:public FrameLayout {
public: public:
@ -9,24 +9,23 @@ public:
static constexpr int ORIENTATION_HORIZONTAL = 0; static constexpr int ORIENTATION_HORIZONTAL = 0;
static constexpr int ORIENTATION_VERTICAL = 1; static constexpr int ORIENTATION_VERTICAL = 1;
public: public:
static interface OnGesturingListener { class OnGesturingListener:public EventSet {
void onGesturingStarted(GestureOverlayView overlay); public:
CallbackBase<void,GestureOverlayView&> onGesturingStarted;
void onGesturingEnded(GestureOverlayView overlay); CallbackBase<void,GestureOverlayView&> onGesturingEnded;
}; };
interface OnGestureListener { class OnGestureListener:public EventSet{
void onGestureStarted(GestureOverlayView overlay, MotionEvent event); public:
CallbackBase<void,GestureOverlayView&,MotionEvent&> onGestureStarted;
void onGesture(GestureOverlayView overlay, MotionEvent event); CallbackBase<void,GestureOverlayView&,MotionEvent&> onGesture;
CallbackBase<void,GestureOverlayView&,MotionEvent&> onGestureEnded;
void onGestureEnded(GestureOverlayView overlay, MotionEvent event); CallbackBase<void,GestureOverlayView&,MotionEvent&> onGestureCancelled;
void onGestureCancelled(GestureOverlayView overlay, MotionEvent event);
}; };
interface OnGesturePerformedListener { class OnGesturePerformedListener:public EventSet{
void onGesturePerformed(GestureOverlayView overlay, Gesture gesture); public:
CallbackBase<void,GestureOverlayView&, Gesture&> onGesturePerformed;
}; };
private: private:
static constexpr int FADE_ANIMATION_RATE = 16; static constexpr int FADE_ANIMATION_RATE = 16;
@ -38,6 +37,8 @@ private:
long mFadingStart; long mFadingStart;
bool mFadingHasStarted; bool mFadingHasStarted;
bool mFadeEnabled = true; bool mFadeEnabled = true;
bool mFireActionPerformed;
bool mResetMultipleStrokes;
int mCurrentColor; int mCurrentColor;
int mCertainGestureColor = 0xFFFFFF00; int mCertainGestureColor = 0xFFFFFF00;
@ -52,8 +53,8 @@ private:
int mOrientation = ORIENTATION_VERTICAL; int mOrientation = ORIENTATION_VERTICAL;
final Rect mInvalidRect = new Rect(); Rect mInvalidRect;
final Path mPath = new Path(); Path mPath;
bool mGestureVisible = true; bool mGestureVisible = true;
float mX; float mX;
@ -87,9 +88,10 @@ private:
float mFadingAlpha = 1.0f; float mFadingAlpha = 1.0f;
AccelerateDecelerateInterpolator*mInterpolator;// =new AccelerateDecelerateInterpolator(); AccelerateDecelerateInterpolator*mInterpolator;// =new AccelerateDecelerateInterpolator();
FadeOutRunnable mFadingOut;// = new FadeOutRunnable(); Runnable mFadingOut;// = new FadeOutRunnable();
private: private:
void init(); void init();
void FadeOutProc();
void setCurrentColor(int color); void setCurrentColor(int color);
void setPaintAlpha(int alpha); void setPaintAlpha(int alpha);
void clear(bool animated, bool fireActionPerformed, bool immediate); void clear(bool animated, bool fireActionPerformed, bool immediate);
@ -101,7 +103,7 @@ private:
void fireOnGesturePerformed(); void fireOnGesturePerformed();
protected: protected:
void onDetachedFromWindow()override; void onDetachedFromWindow()override;
publc: public:
GestureOverlayView(Context* context,const AttributeSet& attrs); GestureOverlayView(Context* context,const AttributeSet& attrs);
const std::vector<GesturePoint>& getCurrentStroke() const; const std::vector<GesturePoint>& getCurrentStroke() const;
@ -125,8 +127,8 @@ publc:
void setEventsInterceptionEnabled(bool enabled); void setEventsInterceptionEnabled(bool enabled);
bool isFadeEnabled()const; bool isFadeEnabled()const;
void setFadeEnabled(bool fadeEnabled); void setFadeEnabled(bool fadeEnabled);
Gesture getGesture()const; Gesture* getGesture()const;
void setGesture(Gesture gesture); void setGesture(Gesture* gesture);
Path getGesturePath(); Path getGesturePath();
Path getGesturePath(Path path); Path getGesturePath(Path path);
bool isGestureVisible()const; bool isGestureVisible()const;
@ -143,7 +145,7 @@ publc:
void removeOnGesturingListener(OnGesturingListener listener); void removeOnGesturingListener(OnGesturingListener listener);
void removeAllOnGesturingListeners(); void removeAllOnGesturingListeners();
bool isGesturing()const; bool isGesturing()const;
void draw(Canvas canvas); void draw(Canvas& canvas);
void clear(bool animated); void clear(bool animated);
void cancelClearAnimation(); void cancelClearAnimation();
void cancelGesture(); void cancelGesture();

View File

@ -9,8 +9,8 @@
namespace cdroid{ namespace cdroid{
class GestureStroke { class GestureStroke {
static constexpr float TOUCH_TOLERANCE = 3;
public: public:
static constexpr float TOUCH_TOLERANCE = 3;
RectF boundingBox; RectF boundingBox;
float length; float length;
std::vector<float> points; std::vector<float> points;