Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
42 kB
4
Indexable
Never
package com.epson.mobilephone.creative.variety.collageprint.view;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Canvas;
import android.graphics.PointF;
import android.graphics.RectF;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ViewTreeObserver;

import androidx.appcompat.widget.AppCompatImageView;

import com.epson.mobilephone.creative.variety.collageprint.draw.CollagePrintDrawEngine;
import com.epson.mobilephone.creative.variety.collageprint.draw.DrawFrame;
import com.epson.mobilephone.creative.variety.collageprint.draw.DrawImage;
import com.epson.mobilephone.creative.variety.collageprint.draw.DrawObject;
import com.epson.mobilephone.creative.variety.collageprint.draw.DrawPhoto;
import com.epson.mobilephone.creative.variety.collageprint.draw.DrawStamp;
import com.epson.mobilephone.creative.variety.collageprint.draw.DrawText;

public class CollagePrintLayoutView extends AppCompatImageView {
    String LOGTAG;
    private float mAngleDrawDegree;
    private float mAngleMoveDegree;
    private RectF mCanvasRect;
    private PointF mCenterPoint;
    private DrawPhoto mDragDrawPhoto;
    private CollagePrintDrawEngine mDrawEngineAokage;
    private EDIT_MODE mEditMode;
    private EDIT_STATE mEditState;
    private OnEventListener mEventListener;
    private GestureDetector mGestureDetector;
    private boolean mHitAngleZoom;
    private float mMovX;
    private float mMovY;
    private int mOrientation;
    private float mPointerDegree;
    private float mPointerDistance;
    private float mPointerUpDegree;
    private ScaleGestureDetector mScaleGestureDetector;
    private final GestureDetector.SimpleOnGestureListener mSimpleOnGestureListener;
    private final ScaleGestureDetector.SimpleOnScaleGestureListener mSimpleOnScaleGestureListener;
    private int mTouchCount;
    private PointF mTouchPointMove1a;
    private PointF mTouchPointMove1b;
    private PointF mTouchPointMove2a;
    private PointF mTouchPointMove2b;
    private Vibrator mVibrator;
    private float mZoonMoveDistance;

    private enum EDIT_MODE {
        EM_EDIT,
        EM_PREVIEW
    }

    private enum EDIT_STATE {
        ES_IDOL,
        ES_SELECTED_FRAME_PHOTO,
        ES_SELECTED_DRAW_OBJECT,
        ES_SELECTED_SCALE_OBJECT,
        ES_DRAG
    }

    public interface OnEventListener {
        void onNotifyDeselect();

        void onNotifyDeviceOrientation(int i);

        void onNotifyMoveEnd();

        void onNotifyMoving();

        void onNotifySelectFrame(int i);

        void onNotifySelectImage();

        void onNotifySelectPhoto(int i);

        void onNotifySelectStamp();

        void onNotifySelectText();

        void onNotifySwapFramePhoto(int i, int i2);
    }

    private float adjustR90(float f) {
        float f2 = f + 90.0f;
        float f3 = 0.0f;
        if (f2 < 360.0f) {
            if (f2 >= 270.0f) {
                f3 = 270.0f;
            } else if (f2 >= 180.0f) {
                f3 = 180.0f;
            } else if (f2 >= 90.0f) {
                f3 = 90.0f;
            }
        }
        return f3 - f;
    }


    private float getAdjustDegree(float f, float f2, float f3) {
        if (f3 > 0.0f) {
            if (f > 270.0f && f < 360.0f && f2 > 0.0f && f2 < 90.0f) {
                return 0.0f;
            }
            if (f <= 0.0f || f >= 90.0f || f2 <= 90.0f || f2 >= 180.0f) {
                if (f <= 90.0f || f >= 180.0f || f2 <= 180.0f || f2 >= 270.0f) {
                    if (f > 180.0f) {
                        if (f < 270.0f) {
                            if (f2 > 270.0f) {
                            }
                        }
                    }
                    return -1.0f;
                }
                return 180.0f;
            }
            return 90.0f;
        }
        if (f > 0.0f && f < 90.0f && f2 > 270.0f && f2 < 360.0f) {
            return 0.0f;
        }
        if (f <= 270.0f || f >= 360.0f || f2 <= 180.0f || f2 >= 270.0f) {
            if (f <= 180.0f || f >= 270.0f || f2 <= 90.0f || f2 >= 180.0f) {
                if (f > 90.0f) {
                    if (f < 180.0f) {
                        if (f2 > 0.0f) {
                        }
                    }
                }
                return -1.0f;
            }
            return 180.0f;
        }
        return 270.0f;
    }

    private float getMoveDegree(float f, float f2, float f3) {
        float f4 = f + (f3 - f2);
        return f4 < 0.0f ? f4 + 360.0f : f4 >= 360.0f ? f4 - 360.0f : f4;
    }

    public void setSelectedObjectReedit() {
    }

    public void setOnEventListener(OnEventListener onEventListener) {
        this.mEventListener = onEventListener;
    }

    public CollagePrintLayoutView(Context context) {
        super(context);
        this.LOGTAG = "CollagePrintLayoutView";
        this.mSimpleOnGestureListener = new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onDoubleTap(MotionEvent motionEvent) {
                if (CollagePrintLayoutView.this.mDrawEngineAokage.isSelectedPhoto() != null) {
                    CollagePrintLayoutView.this.mDrawEngineAokage.resetSelectedPhoto();
                    CollagePrintLayoutView.this.invalidate();
                }
                return super.onDoubleTap(motionEvent);
            }

            @Override
            public boolean onDoubleTapEvent(MotionEvent motionEvent) {
                return super.onDoubleTapEvent(motionEvent);
            }

            @Override
            public boolean onDown(MotionEvent motionEvent) {
                return super.onDown(motionEvent);
            }

            @Override
            public void onLongPress(MotionEvent motionEvent) {
                if (CollagePrintLayoutView.this.mDragDrawPhoto == null && CollagePrintLayoutView.this.mTouchCount < 2 && CollagePrintLayoutView.this.mEditState.equals(EDIT_STATE.ES_SELECTED_FRAME_PHOTO)) {
                    CollagePrintLayoutView.this.mDragDrawPhoto = CollagePrintLayoutView.this.mDrawEngineAokage.getDragDrawPhoto();
                    if (CollagePrintLayoutView.this.mDragDrawPhoto != null) {
                        if (CollagePrintLayoutView.this.mVibrator != null) {
                            CollagePrintLayoutView.this.mVibrator.vibrate(100L);
                        }
                        CollagePrintLayoutView.this.mMovX = motionEvent.getX();
                        CollagePrintLayoutView.this.mMovY = motionEvent.getY();
                        CollagePrintLayoutView.this.mDrawEngineAokage.resetAllObject();
                        CollagePrintLayoutView.this.invalidate();
                    }
                }
                super.onLongPress(motionEvent);
            }

            @Override
            public void onShowPress(MotionEvent motionEvent) {
                super.onShowPress(motionEvent);
            }

            @Override
            public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
                boolean z;
                boolean z2;
                boolean z3;
                boolean z4 = false;
                float x = motionEvent.getX(0);
                float y = motionEvent.getY(0);
                DrawObject selectDrawObject = CollagePrintLayoutView.this.mDrawEngineAokage.getSelectDrawObject();
                DrawFrame selectDrawFrame = CollagePrintLayoutView.this.mDrawEngineAokage.getSelectDrawFrame();
                DrawObject containsDrawObject = CollagePrintLayoutView.this.mDrawEngineAokage.containsDrawObject(x, y);
                DrawFrame containsDrawFrame = CollagePrintLayoutView.this.mDrawEngineAokage.containsDrawFrame(x, y);
                if (selectDrawObject == null && selectDrawFrame == null) {
                    z3 = true;
                    z = containsDrawObject != null;
                    if (containsDrawFrame != null) {
                        int photoIndex = CollagePrintLayoutView.this.mDrawEngineAokage.getPhotoIndex(containsDrawFrame);
                        z2 = photoIndex != -1;
                        if (photoIndex != -1) {
                            z3 = false;
                        }
                    } else {
                        z3 = false;
                        z2 = false;
                    }
                } else {
                    CollagePrintDrawEngine collagePrintDrawEngine = CollagePrintLayoutView.this.mDrawEngineAokage;
                    if (selectDrawObject == null) {
                        selectDrawObject = selectDrawFrame;
                    }
                    boolean containsObject = collagePrintDrawEngine.containsObject(selectDrawObject, x, y);
                    z = false;
                    z2 = false;
                    z4 = containsObject;
                    z3 = false;
                }
                if (!z4 && !z) {
                    if (z2) {
                        CollagePrintLayoutView.this.selectPhotoNotifySelect(containsDrawFrame);
                    } else if (z3) {
                        CollagePrintLayoutView.this.selectFrameNotifySelectPhoto(containsDrawFrame.getIndex());
                    } else {
                        CollagePrintLayoutView.this.resetObjectNotifyDeselect();
                    }
                }
                return super.onSingleTapConfirmed(motionEvent);
            }

            @Override
            public boolean onSingleTapUp(MotionEvent motionEvent) {
                return super.onSingleTapUp(motionEvent);
            }

            @Override
            public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
                return super.onFling(motionEvent, motionEvent2, f, f2);
            }

            @Override
            public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
                return super.onScroll(motionEvent, motionEvent2, f, f2);
            }
        };
        this.mSimpleOnScaleGestureListener = new ScaleGestureDetector.SimpleOnScaleGestureListener() {
            @Override
            public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) {
                CollagePrintLayoutView.this.setScale(scaleGestureDetector.getScaleFactor());
                return super.onScaleBegin(scaleGestureDetector);
            }

            @Override
            public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
                CollagePrintLayoutView.this.setScale(scaleGestureDetector.getScaleFactor());
                return true;
            }

            @Override
            public void onScaleEnd(ScaleGestureDetector scaleGestureDetector) {
                super.onScaleEnd(scaleGestureDetector);
                CollagePrintLayoutView.this.setScale(scaleGestureDetector.getScaleFactor());
            }
        };
        init(context);
    }

    public CollagePrintLayoutView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.LOGTAG = "CollagePrintLayoutView";
        this.mSimpleOnGestureListener = new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onDoubleTap(MotionEvent motionEvent) {
                if (CollagePrintLayoutView.this.mDrawEngineAokage.isSelectedPhoto() != null) {
                    CollagePrintLayoutView.this.mDrawEngineAokage.resetSelectedPhoto();
                    CollagePrintLayoutView.this.invalidate();
                }
                return super.onDoubleTap(motionEvent);
            }

            @Override
            public boolean onDoubleTapEvent(MotionEvent motionEvent) {
                return super.onDoubleTapEvent(motionEvent);
            }

            @Override
            public boolean onDown(MotionEvent motionEvent) {
                return super.onDown(motionEvent);
            }

            @Override
            public void onLongPress(MotionEvent motionEvent) {
                if (CollagePrintLayoutView.this.mDragDrawPhoto == null && CollagePrintLayoutView.this.mTouchCount < 2 && CollagePrintLayoutView.this.mEditState.equals(EDIT_STATE.ES_SELECTED_FRAME_PHOTO)) {
                    CollagePrintLayoutView.this.mDragDrawPhoto = CollagePrintLayoutView.this.mDrawEngineAokage.getDragDrawPhoto();
                    if (CollagePrintLayoutView.this.mDragDrawPhoto != null) {
                        if (CollagePrintLayoutView.this.mVibrator != null) {
                            CollagePrintLayoutView.this.mVibrator.vibrate(100L);
                        }
                        CollagePrintLayoutView.this.mMovX = motionEvent.getX();
                        CollagePrintLayoutView.this.mMovY = motionEvent.getY();
                        CollagePrintLayoutView.this.mDrawEngineAokage.resetAllObject();
                        CollagePrintLayoutView.this.invalidate();
                    }
                }
                super.onLongPress(motionEvent);
            }

            @Override
            public void onShowPress(MotionEvent motionEvent) {
                super.onShowPress(motionEvent);
            }

            @Override
            public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
                boolean z;
                boolean z2;
                boolean z3;
                boolean z4 = false;
                float x = motionEvent.getX(0);
                float y = motionEvent.getY(0);
                DrawObject selectDrawObject = CollagePrintLayoutView.this.mDrawEngineAokage.getSelectDrawObject();
                DrawFrame selectDrawFrame = CollagePrintLayoutView.this.mDrawEngineAokage.getSelectDrawFrame();
                DrawObject containsDrawObject = CollagePrintLayoutView.this.mDrawEngineAokage.containsDrawObject(x, y);
                DrawFrame containsDrawFrame = CollagePrintLayoutView.this.mDrawEngineAokage.containsDrawFrame(x, y);
                if (selectDrawObject == null && selectDrawFrame == null) {
                    z3 = true;
                    z = containsDrawObject != null;
                    if (containsDrawFrame != null) {
                        int photoIndex = CollagePrintLayoutView.this.mDrawEngineAokage.getPhotoIndex(containsDrawFrame);
                        z2 = photoIndex != -1;
                        if (photoIndex != -1) {
                            z3 = false;
                        }
                    } else {
                        z3 = false;
                        z2 = false;
                    }
                } else {
                    CollagePrintDrawEngine collagePrintDrawEngine = CollagePrintLayoutView.this.mDrawEngineAokage;
                    if (selectDrawObject == null) {
                        selectDrawObject = selectDrawFrame;
                    }
                    boolean containsObject = collagePrintDrawEngine.containsObject(selectDrawObject, x, y);
                    z = false;
                    z2 = false;
                    z4 = containsObject;
                    z3 = false;
                }
                if (!z4 && !z) {
                    if (z2) {
                        CollagePrintLayoutView.this.selectPhotoNotifySelect(containsDrawFrame);
                    } else if (z3) {
                        CollagePrintLayoutView.this.selectFrameNotifySelectPhoto(containsDrawFrame.getIndex());
                    } else {
                        CollagePrintLayoutView.this.resetObjectNotifyDeselect();
                    }
                }
                return super.onSingleTapConfirmed(motionEvent);
            }

            @Override
            public boolean onSingleTapUp(MotionEvent motionEvent) {
                return super.onSingleTapUp(motionEvent);
            }

            @Override
            public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
                return super.onFling(motionEvent, motionEvent2, f, f2);
            }

            @Override
            public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
                return super.onScroll(motionEvent, motionEvent2, f, f2);
            }
        };
        this.mSimpleOnScaleGestureListener = new ScaleGestureDetector.SimpleOnScaleGestureListener() { // from class: com.epson.mobilephone.creative.variety.collageprint.view.CollagePrintLayoutView.3
            @Override
            public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) {
                CollagePrintLayoutView.this.setScale(scaleGestureDetector.getScaleFactor());
                return super.onScaleBegin(scaleGestureDetector);
            }

            @Override
            public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
                CollagePrintLayoutView.this.setScale(scaleGestureDetector.getScaleFactor());
                return true;
            }

            @Override
            public void onScaleEnd(ScaleGestureDetector scaleGestureDetector) {
                super.onScaleEnd(scaleGestureDetector);
                CollagePrintLayoutView.this.setScale(scaleGestureDetector.getScaleFactor());
            }
        };
        init(context);
    }

    public CollagePrintLayoutView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.LOGTAG = "CollagePrintLayoutView";
        this.mSimpleOnGestureListener = new GestureDetector.SimpleOnGestureListener() { // from class: com.epson.mobilephone.creative.variety.collageprint.view.CollagePrintLayoutView.2
            @Override
            public boolean onDoubleTap(MotionEvent motionEvent) {
                if (CollagePrintLayoutView.this.mDrawEngineAokage.isSelectedPhoto() != null) {
                    CollagePrintLayoutView.this.mDrawEngineAokage.resetSelectedPhoto();
                    CollagePrintLayoutView.this.invalidate();
                }
                return super.onDoubleTap(motionEvent);
            }

            @Override
            public boolean onDoubleTapEvent(MotionEvent motionEvent) {
                return super.onDoubleTapEvent(motionEvent);
            }

            @Override
            public boolean onDown(MotionEvent motionEvent) {
                return super.onDown(motionEvent);
            }

            @Override
            public void onLongPress(MotionEvent motionEvent) {
                if (CollagePrintLayoutView.this.mDragDrawPhoto == null && CollagePrintLayoutView.this.mTouchCount < 2 && CollagePrintLayoutView.this.mEditState.equals(EDIT_STATE.ES_SELECTED_FRAME_PHOTO)) {
                    CollagePrintLayoutView.this.mDragDrawPhoto = CollagePrintLayoutView.this.mDrawEngineAokage.getDragDrawPhoto();
                    if (CollagePrintLayoutView.this.mDragDrawPhoto != null) {
                        if (CollagePrintLayoutView.this.mVibrator != null) {
                            CollagePrintLayoutView.this.mVibrator.vibrate(100L);
                        }
                        CollagePrintLayoutView.this.mMovX = motionEvent.getX();
                        CollagePrintLayoutView.this.mMovY = motionEvent.getY();
                        CollagePrintLayoutView.this.mDrawEngineAokage.resetAllObject();
                        CollagePrintLayoutView.this.invalidate();
                    }
                }
                super.onLongPress(motionEvent);
            }

            @Override
            public void onShowPress(MotionEvent motionEvent) {
                super.onShowPress(motionEvent);
            }

            @Override
            public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
                boolean z;
                boolean z2;
                boolean z3;
                boolean z4 = false;
                float x = motionEvent.getX(0);
                float y = motionEvent.getY(0);
                DrawObject selectDrawObject = CollagePrintLayoutView.this.mDrawEngineAokage.getSelectDrawObject();
                DrawFrame selectDrawFrame = CollagePrintLayoutView.this.mDrawEngineAokage.getSelectDrawFrame();
                DrawObject containsDrawObject = CollagePrintLayoutView.this.mDrawEngineAokage.containsDrawObject(x, y);
                DrawFrame containsDrawFrame = CollagePrintLayoutView.this.mDrawEngineAokage.containsDrawFrame(x, y);
                if (selectDrawObject == null && selectDrawFrame == null) {
                    z3 = true;
                    z = containsDrawObject != null;
                    if (containsDrawFrame != null) {
                        int photoIndex = CollagePrintLayoutView.this.mDrawEngineAokage.getPhotoIndex(containsDrawFrame);
                        z2 = photoIndex != -1;
                        if (photoIndex != -1) {
                            z3 = false;
                        }
                    } else {
                        z3 = false;
                        z2 = false;
                    }
                } else {
                    CollagePrintDrawEngine collagePrintDrawEngine = CollagePrintLayoutView.this.mDrawEngineAokage;
                    if (selectDrawObject == null) {
                        selectDrawObject = selectDrawFrame;
                    }
                    boolean containsObject = collagePrintDrawEngine.containsObject(selectDrawObject, x, y);
                    z = false;
                    z2 = false;
                    z4 = containsObject;
                    z3 = false;
                }
                if (!z4 && !z) {
                    if (z2) {
                        CollagePrintLayoutView.this.selectPhotoNotifySelect(containsDrawFrame);
                    } else if (z3) {
                        CollagePrintLayoutView.this.selectFrameNotifySelectPhoto(containsDrawFrame.getIndex());
                    } else {
                        CollagePrintLayoutView.this.resetObjectNotifyDeselect();
                    }
                }
                return super.onSingleTapConfirmed(motionEvent);
            }

            @Override
            public boolean onSingleTapUp(MotionEvent motionEvent) {
                return super.onSingleTapUp(motionEvent);
            }

            @Override
            public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
                return super.onFling(motionEvent, motionEvent2, f, f2);
            }

            @Override
            public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
                return super.onScroll(motionEvent, motionEvent2, f, f2);
            }
        };
        this.mSimpleOnScaleGestureListener = new ScaleGestureDetector.SimpleOnScaleGestureListener() {
            @Override
            public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) {
                CollagePrintLayoutView.this.setScale(scaleGestureDetector.getScaleFactor());
                return super.onScaleBegin(scaleGestureDetector);
            }

            @Override
            public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
                CollagePrintLayoutView.this.setScale(scaleGestureDetector.getScaleFactor());
                return true;
            }

            @Override
            public void onScaleEnd(ScaleGestureDetector scaleGestureDetector) {
                super.onScaleEnd(scaleGestureDetector);
                CollagePrintLayoutView.this.setScale(scaleGestureDetector.getScaleFactor());
            }
        };
        init(context);
    }

    private void init(Context context) {
        this.mDrawEngineAokage = null;
        this.mCanvasRect = new RectF(0.0f, 0.0f, 0.0f, 0.0f);
        this.mTouchPointMove1a = new PointF(0.0f, 0.0f);
        this.mTouchPointMove2a = new PointF(0.0f, 0.0f);
        this.mTouchPointMove1b = new PointF(0.0f, 0.0f);
        this.mTouchPointMove2b = new PointF(0.0f, 0.0f);
        this.mGestureDetector = new GestureDetector(context, this.mSimpleOnGestureListener);
        this.mScaleGestureDetector = new ScaleGestureDetector(context, this.mSimpleOnScaleGestureListener);
        this.mEditState = EDIT_STATE.ES_IDOL;
        this.mEditMode = EDIT_MODE.EM_PREVIEW;
        this.mHitAngleZoom = false;
        this.mCenterPoint = new PointF(0.0f, 0.0f);
        this.mDragDrawPhoto = null;
        this.mOrientation = 0;
        if (!isInEditMode()) {
            this.mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        }
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                CollagePrintLayoutView.this.mCanvasRect.set(0.0f, 0.0f, CollagePrintLayoutView.this.getWidth(), CollagePrintLayoutView.this.getHeight());
                CollagePrintLayoutView.this.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    }

    public void setEditEnable(boolean z) {
        this.mEditMode = z ? EDIT_MODE.EM_EDIT : EDIT_MODE.EM_PREVIEW;
    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (this.mEditMode == EDIT_MODE.EM_EDIT) {
            if (this.mDragDrawPhoto != null) {
                return touchEventDragObject(motionEvent);
            }
            return touchEventDrawObject(motionEvent);
        }
        return super.onTouchEvent(motionEvent);
    }

    private boolean touchEventDragObject(MotionEvent motionEvent) {
        int pointerCount = motionEvent.getPointerCount();
        int action = motionEvent.getAction() & 255;
        if (pointerCount == 1) {
            float x = motionEvent.getX();
            float y = motionEvent.getY();
            if (action == 2) {
                this.mDragDrawPhoto.setMovedPoint(x - this.mMovX, y - this.mMovY);
                this.mMovX = x;
                this.mMovY = y;
                invalidate();
            }
            if (action == 1 || action == 3) {
                int index = this.mDragDrawPhoto.getIndex();
                DrawFrame containsDrawFrame = this.mDrawEngineAokage.containsDrawFrame(x, y);
                int index2 = containsDrawFrame != null ? containsDrawFrame.getIndex() : 0;
                if (index > 0 && index2 > 0 && index != index2 && this.mEventListener != null) {
                    this.mEventListener.onNotifySwapFramePhoto(index, index2);
                }
                this.mDragDrawPhoto.resetDragDrawPhoto();
                this.mDragDrawPhoto = null;
                this.mEditState = EDIT_STATE.ES_IDOL;
                if (this.mEventListener != null) {
                    this.mEventListener.onNotifyMoveEnd();
                    this.mEventListener.onNotifyDeselect();
                }
                invalidate();
            }
        }
        return true;
    }



    //-----------------------------------------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------------------------------------

    private boolean touchEventDrawObject(MotionEvent motionEvent) {
        int pointerCount = motionEvent.getPointerCount();
        int action = motionEvent.getActionMasked();

        if (pointerCount == 1) {
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    mMovX = motionEvent.getX();
                    mMovY = motionEvent.getY();
                    mHitAngleZoom = false;
                    mTouchCount = 1;

                    if (mEditState.equals(EDIT_STATE.ES_IDOL)) {
                        mTouchPointMove1a.set(mMovX, mMovY);
                        DrawObject selectedObject = mDrawEngineAokage.containsSelectDrawObject(mMovX, mMovY);

                        if (selectedObject != null) {
                            mHitAngleZoom = selectedObject.isHitAngleZoomHandle(mMovX, mMovY);
                            mEditState = EDIT_STATE.ES_SELECTED_DRAW_OBJECT;
                            mDrawEngineAokage.updateStateDrawObject();

                            if (mEventListener != null) {
                                if (selectedObject instanceof DrawText) {
                                    mEventListener.onNotifySelectText();
                                } else if (selectedObject instanceof DrawStamp) {
                                    mEventListener.onNotifySelectStamp();
                                } else if (selectedObject instanceof DrawImage) {
                                    mEventListener.onNotifySelectImage();
                                }
                            }

                            if (mHitAngleZoom) {
                                mCenterPoint = selectedObject.getCenter();
                                mAngleMoveDegree = calcDegree(mCenterPoint, mMovX, mMovY);
                                mZoonMoveDistance = calcDistance(mCenterPoint, mMovX, mMovY);
                            }

                            invalidate();
                        } else {
                            DrawFrame selectedFrame = mDrawEngineAokage.isSelectedFrame()
                                    ? mDrawEngineAokage.getSelectDrawFrame()
                                    : mDrawEngineAokage.containsSelectDrawFrame(mMovX, mMovY);

                            if (selectedFrame != null) {
                                DrawPhoto drawPhoto = mDrawEngineAokage.getDrawPhoto(selectedFrame.getIndex());

                                if (drawPhoto != null && drawPhoto.isPhoto()) {
                                    mHitAngleZoom = drawPhoto.isHitAngleZoomHandle(mMovX, mMovY);
                                    mEditState = EDIT_STATE.ES_SELECTED_FRAME_PHOTO;

                                    if (!mHitAngleZoom) {
                                        mDrawEngineAokage.setSelectDrawFrame(selectedFrame);
                                        mDrawEngineAokage.updateStateDrawFrame();
                                    } else {
                                        mCenterPoint = drawPhoto.getCenter();
                                        mAngleMoveDegree = calcDegree(mCenterPoint, mMovX, mMovY);
                                        mZoonMoveDistance = calcDistance(mCenterPoint, mMovX, mMovY);
                                    }

                                    if (mEventListener != null) {
                                        mEventListener.onNotifySelectPhoto(selectedFrame.getIndex());
                                    }

                                    invalidate();
                                }
                            }
                        }
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
                    mMovX = motionEvent.getX();
                    mMovY = motionEvent.getY();

                    if (mHitAngleZoom) {
                        float currentDegree = calcDegree(mCenterPoint, mMovX, mMovY);
                        float degreeChange = currentDegree - mAngleMoveDegree;
                        float moveDegree = getMoveDegree(mAngleDrawDegree, mAngleMoveDegree, currentDegree);

                        if (setAngle(degreeChange)) {
                            mAngleMoveDegree = currentDegree;
                            mAngleDrawDegree = moveDegree;
                        }

                        float currentDistance = calcDistance(mCenterPoint, mMovX, mMovY);
                        if (setScale(currentDistance / mZoonMoveDistance)) {
                            mZoonMoveDistance = currentDistance;
                        }

                        invalidate();
                    } else {
                        float dx = mMovX - mTouchPointMove1a.x;
                        float dy = mMovY - mTouchPointMove1a.y;

                        if (mEditState.equals(EDIT_STATE.ES_SELECTED_DRAW_OBJECT)) {
                            if (setMoveObject(dx, dy)) {
                                mTouchPointMove1a.set(mMovX, mMovY);
                            }
                        } else if (mEditState.equals(EDIT_STATE.ES_SELECTED_FRAME_PHOTO)) {
                            if (setMovePhoto(dx, dy)) {
                                mTouchPointMove1a.set(mMovX, mMovY);
                            }
                        }
                    }
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    mHitAngleZoom = false;
                    mEditState = EDIT_STATE.ES_IDOL;
                    mTouchCount = 0;

                    if (mEventListener != null) {
                        mEventListener.onNotifyMoveEnd();
                    }
                    break;
            }

            mGestureDetector.onTouchEvent(motionEvent);
        } else if (pointerCount == 2) {
            switch (action) {
                case MotionEvent.ACTION_POINTER_DOWN:
                    mTouchCount = 2;
                    mTouchPointMove2a.set(motionEvent.getX(1), motionEvent.getY(1));
                    break;
                case MotionEvent.ACTION_MOVE:
                    mTouchPointMove1b.set(motionEvent.getX(0), motionEvent.getY(0));
                    mTouchPointMove2b.set(motionEvent.getX(1), motionEvent.getY(1));

                    float currentDegree = calcDegree(mTouchPointMove1b.x, mTouchPointMove1b.y, mTouchPointMove2b.x, mTouchPointMove2b.y);
                    float previousDegree = calcDegree(mTouchPointMove1a.x, mTouchPointMove1a.y, mTouchPointMove2a.x, mTouchPointMove2a.y);
                    float degreeChange = currentDegree - previousDegree;

                    boolean updated = false;

                    if (mEditState.equals(EDIT_STATE.ES_SELECTED_DRAW_OBJECT)) {
                        updated = mDrawEngineAokage.setAngleDrawObject(degreeChange);
                    } else if (mEditState.equals(EDIT_STATE.ES_SELECTED_FRAME_PHOTO)) {
                        updated = mDrawEngineAokage.setAngleFramePhoto(degreeChange);
                    }

                    if (updated) {
                        mTouchPointMove1a.set(mTouchPointMove1b);
                        mTouchPointMove2a.set(mTouchPointMove2b);
                        invalidate();

                        if (mEventListener != null) {
                            mEventListener.onNotifyMoving();
                        }
                    }
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    mEditState = EDIT_STATE.ES_IDOL;
                    mTouchCount = 0;
                    break;
            }

            mScaleGestureDetector.onTouchEvent(motionEvent);
        }

        return true;
    }


    //-----------------------------------------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------------------------------------------------------

    @Override
    protected void onSizeChanged(int i, int i2, int i3, int i4) {
        super.onSizeChanged(i, i2, i3, i4);
    }

    @Override
    public void onWindowFocusChanged(boolean z) {
        super.onWindowFocusChanged(z);
    }

    @Override
    protected void onConfigurationChanged(Configuration configuration) {
        super.onConfigurationChanged(configuration);
        if (this.mDrawEngineAokage == null || this.mOrientation == configuration.orientation) {
            return;
        }
        this.mOrientation = configuration.orientation;
        this.mCanvasRect.set(0.0f, 0.0f, getWidth(), getHeight());
        if (this.mEventListener != null) {
            this.mEventListener.onNotifyDeviceOrientation(this.mOrientation);
        }
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (this.mCanvasRect.isEmpty() || this.mDrawEngineAokage == null) {
            return;
        }
        this.mDrawEngineAokage.drawLayoutCanvas(canvas);
        if (this.mDragDrawPhoto != null) {
            this.mDragDrawPhoto.drawDrag(canvas);
        }
    }

    public void resetObjectNotifyDeselect() {
        this.mDrawEngineAokage.resetAllObject();
        if (this.mEventListener != null) {
            this.mEventListener.onNotifyDeselect();
        }
        invalidate();
    }

    public void selectPhotoNotifySelect(DrawFrame drawFrame) {
        this.mDrawEngineAokage.setSelectDrawFrame(drawFrame);
        if (this.mEventListener != null) {
            this.mEventListener.onNotifySelectPhoto(drawFrame.getIndex());
        }
        invalidate();
    }

    public void selectFrameNotifySelectPhoto(int i) {
        if (this.mEventListener != null) {
            this.mEventListener.onNotifySelectFrame(i);
        }
    }

    private void resetObject() {
        this.mDrawEngineAokage.resetAllObject();
        invalidate();
    }

    private boolean setMoveObject(float f, float f2) {
        if (!this.mDrawEngineAokage.moveDrawObject(f, f2) || this.mEventListener == null) {
            return false;
        }
        this.mEventListener.onNotifyMoving();
        invalidate();
        return true;
    }

    private boolean setMovePhoto(float f, float f2) {
        if (!this.mDrawEngineAokage.moveFramePhoto(f, f2) || this.mEventListener == null) {
            return false;
        }
        this.mEventListener.onNotifyMoving();
        invalidate();
        return true;
    }

    public boolean setScale(float f) {
        if (this.mEditState.equals(EDIT_STATE.ES_SELECTED_DRAW_OBJECT)) {
            if (this.mDrawEngineAokage.setScaleDrawObject(this.mDrawEngineAokage.getScaleDrawObject() * f) && this.mEventListener != null) {
                this.mEventListener.onNotifyMoving();
                invalidate();
                return true;
            }
        } else if (this.mEditState.equals(EDIT_STATE.ES_SELECTED_FRAME_PHOTO)) {
            if (this.mDrawEngineAokage.setScaleFramePhoto(this.mDrawEngineAokage.getScaleFramePhoto() * f) && this.mEventListener != null) {
                this.mEventListener.onNotifyMoving();
                invalidate();
                return true;
            }
        }
        return false;
    }

    private boolean setAngle(float f) {
        if (this.mEditState.equals(EDIT_STATE.ES_SELECTED_DRAW_OBJECT)) {
            if (this.mDrawEngineAokage.setAngleDrawObject(f) && this.mEventListener != null) {
                this.mEventListener.onNotifyMoving();
                invalidate();
                return true;
            }
        } else if (this.mEditState.equals(EDIT_STATE.ES_SELECTED_FRAME_PHOTO) && this.mDrawEngineAokage.setAngleFramePhoto(f) && this.mEventListener != null) {
            this.mEventListener.onNotifyMoving();
            invalidate();
            return true;
        }
        return false;
    }

    private void setEditAngle90() {
        if (this.mDrawEngineAokage.getSelectDrawObject() != null) {
            this.mDrawEngineAokage.setEditAngleDrawObject(adjustR90(this.mDrawEngineAokage.getAngleDrawObject()));
        } else if (this.mDrawEngineAokage.getSelectDrawPhoto() != null) {
            this.mDrawEngineAokage.setEditAngleFramePhoto(adjustR90(this.mDrawEngineAokage.getAngleFramePhoto()));
        }
        invalidate();
    }

    private float calcDegree(PointF pointF, float f, float f2) {
        return calcDegree(pointF.x, pointF.y, f, f2);
    }

    private float calcDegree(float f, float f2, float f3, float f4) {
        float degrees = (float) Math.toDegrees(Math.atan2(f4 - f2, f3 - f));
        if (degrees < 0.0f) {
            degrees += 360.0f;
        }
        return degrees >= 360.0f ? degrees - 360.0f : degrees;
    }
    private float calcDistance(PointF pointF, float f, float f2) {
        return calcDistance(pointF.x, pointF.y, f, f2);
    }

    private float calcDistance(float f, float f2, float f3, float f4) {
        float f5 = f3 - f;
        float f6 = f4 - f2;
        return (float) Math.sqrt((f5 * f5) + (f6 * f6));
    }

    public void setLayoutDrawEngine(CollagePrintDrawEngine collagePrintDrawEngine) {
        this.mDrawEngineAokage = collagePrintDrawEngine;
    }

    public CollagePrintDrawEngine getLayoutDrawEngine() {
        return this.mDrawEngineAokage;
    }

    public void setSelectedObjectR90() {
        setEditAngle90();
    }

    public void setSelectedObjectRemove() {
        if (this.mDrawEngineAokage.getSelectDrawObject() != null) {
            this.mDrawEngineAokage.removeSelectDrawObject();
        } else if (this.mDrawEngineAokage.getSelectDrawPhoto() != null) {
            this.mDrawEngineAokage.removeSelectDrawPhoto();
        }
        invalidate();
    }
}
Leave a Comment