TouchDisplay.java :  » UnTagged » eagle-library » eagle » android » device » Android Open Source

Android Open Source » UnTagged » eagle library 
eagle library » eagle » android » device » TouchDisplay.java
/**
 *
 * @author eagle.sakura
 * @version 2009/11/19 : 
 */
package eagle.android.device;

import eagle.util.EagleUtil;
import android.graphics.Point;
import android.view.MotionEvent;

/**
 * @author eagle.sakura
 * @version 2009/11/19 : 
 * @version 2010/07/20 : 
 */
public class TouchDisplay {
    protected TouchPoint[] touchPoints;

    /**
     * <BR>
     * 
     *
     * @author eagle.sakura
     * @version 2009/11/19 : 
     */
    public TouchDisplay() {
        this(1);
    }

    /**
     *
     * @author eagle.sakura
     * @param nums
     * @version 2010/07/20 : 
     */
    protected TouchDisplay(int nums) {
        touchPoints = new TouchPoint[nums];
        for (int i = 0; i < nums; ++i) {
            touchPoints[i] = new TouchPoint(i);
        }
    }

    /**
     * 
     *
     * @author eagle.sakura
     * @param index
     * @return
     * @version 2010/07/20 : 
     */
    public TouchPoint getTouchPoint(int index) {
        return touchPoints[index];
    }

    /**
     * 
     *
     * @author eagle.sakura
     * @param me
     * @version 2009/11/19 : 
     */
    public boolean onTouchEvent(MotionEvent me) {
        TouchPoint tp = touchPoints[0];

        switch (me.getAction()) {
        case MotionEvent.ACTION_DOWN:
            return tp.onActionDown(me.getX(), me.getY());
        case MotionEvent.ACTION_MOVE:
            return tp.onActionMove(me.getX(), me.getY());
        case MotionEvent.ACTION_UP:
            return tp.onActionUp(me.getX(), me.getY());
        case MotionEvent.ACTION_OUTSIDE:
            return tp.onActionOutside(me.getX(), me.getY());
        }

        return true;
    }

    /**
     * 
     *
     * @author eagle.sakura
     * @return
     * @version 2009/11/19 : 
     */
    public int getDrugVectorX() {
        return touchPoints[0].getDrugVectorX();
    }

    /**
     * 
     *
     * @author eagle.sakura
     * @return
     * @version 2009/12/06 : 
     */
    public int getTouchPosX() {
        return touchPoints[0].getTouchPosX();
    }

    /**
     * 
     *
     * @author eagle.sakura
     * @return
     * @version 2009/12/06 : 
     */
    public int getTouchPosY() {
        return touchPoints[0].getTouchPosY();
    }

    /**
     * 
     *
     * @author eagle.sakura
     * @return
     * @version 2009/11/19 : 
     */
    public int getDrugVectorY() {
        return touchPoints[0].getDrugVectorY();
    }

    /**
     * 
     *
     * @author eagle.sakura
     * @return
     * @version 2009/11/19 : 
     */
    public boolean isTouch() {
        return touchPoints[0].isTouch();
    }

    /**
     * 
     *
     * @author eagle.sakura
     * @return
     * @version 2010/05/21 : 
     */
    public boolean isRelease() {
        return touchPoints[0].isRelease();
    }

    /**
     * 
     *
     * @author eagle.sakura
     * @return
     * @version 2010/05/21 : 
     */
    public boolean isReleaseOnce() {
        return touchPoints[0].isReleaseOnce();
    }

    /**
     * 
     *
     * @author eagle.sakura
     * @return
     * @version 2009/11/19 : 
     */
    public boolean isTouchOnce() {
        return touchPoints[0].isTouchOnce();
    }

    /**
     * 
     *
     * @author eagle.sakura
     * @version 2009/11/19 : 
     */
    public void update() {
        for (TouchPoint tp : touchPoints) {
            tp.update();
        }
    }

    /**
     * @author eagle.sakura
     * @version 2010/07/19 : 
     */
    public class TouchPoint {
        /**
         * 
         */
        private int attribute = 0x0;
        /**
         * 
         */
        private int attrOld = 0x0;
        /**
         * 
         */
        private int attrNow = 0x0;
        /**
         * 
         */
        @SuppressWarnings("all")
        private int touchTimeMs = 0;
        /**
         * 
         */
        private long touchStartTime = 0;
        /**
         * 
         */
        private Point touchPos = new Point();
        /**
         * 
         */
        private Point releasePos = new Point();

        /**
         * 
         */
        private static final int eAttrTouch = 1 << 0;

        @SuppressWarnings("all")
        private int id = -1;

        /**
         * 
         *
         * @author eagle.sakura
         * @param id
         * @version 2010/07/19 : 
         */
        public TouchPoint(int id) {
            this.id = id;
        }

        /**
         * 
         *
         * @author eagle.sakura
         * @param me
         * @version 2010/07/19 : 
         */
        protected boolean onActionDown(float x, float y) {
            touchPos.x = (int) x;
            touchPos.y = (int) y;
            releasePos.x = (int) x;
            releasePos.y = (int) y;

            touchStartTime = System.currentTimeMillis();
            attribute = EagleUtil.setFlag(attribute, eAttrTouch, true);
            return true;
        }

        /**
         * 
         *
         * @author eagle.sakura
         * @param me
         * @version 2010/07/19 : 
         */
        protected boolean onActionMove(float x, float y) {
            releasePos.x = (int) x;
            releasePos.y = (int) y;
            touchTimeMs = (int) (System.currentTimeMillis() - touchStartTime);
            attribute = EagleUtil.setFlag(attribute, eAttrTouch, true);
            return true;
        }

        /**
         * 
         *
         * @author eagle.sakura
         * @param me
         * @return
         * @version 2010/07/19 : 
         */
        protected boolean onActionUp(float x, float y) {
            releasePos.x = (int) x;
            releasePos.y = (int) y;
            touchTimeMs = (int) (System.currentTimeMillis() - touchStartTime);
            attribute = EagleUtil.setFlag(attribute, eAttrTouch, false);
            return true;
        }

        /**
         * 
         *
         * @author eagle.sakura
         * @param me
         * @return
         * @version 2010/07/19 : 
         */
        protected boolean onActionOutside(float x, float y) {
            releasePos.x = (int) x;
            releasePos.y = (int) y;
            touchTimeMs = (int) (System.currentTimeMillis() - touchStartTime);
            attribute = EagleUtil.setFlag(attribute, eAttrTouch, false);
            return true;
        }

        /**
         * 
         *
         * @author eagle.sakura
         * @return
         * @version 2009/11/19 : 
         */
        public int getDrugVectorX() {
            return releasePos.x - touchPos.x;
        }

        /**
         * 
         *
         * @author eagle.sakura
         * @return
         * @version 2009/12/06 : 
         */
        public int getTouchPosX() {
            return touchPos.x;
        }

        /**
         * 
         *
         * @author eagle.sakura
         * @return
         * @version 2009/12/06 : 
         */
        public int getTouchPosY() {
            return touchPos.y;
        }

        /**
         * 
         *
         * @author eagle.sakura
         * @return
         * @version 2010/07/20 : 
         */
        public int getCurrentX() {
            return releasePos.x;
        }

        /**
         * 
         *
         * @author eagle.sakura
         * @return
         * @version 2010/07/20 : 
         */
        public int getCurrentY() {
            return releasePos.y;
        }

        /**
         * 
         *
         * @author eagle.sakura
         * @param x
         * @param y
         * @return
         * @version 2010/07/20 : 
         */
        public float getLength(int x, int y) {
            int lx = releasePos.x - x, ly = releasePos.y - y;
            return (float) Math.sqrt((double) (lx * lx + ly * ly));
        }

        /**
         * 
         *
         * @author eagle.sakura
         * @return
         * @version 2009/11/19 : 
         */
        public int getDrugVectorY() {
            return releasePos.y - touchPos.y;
        }

        /**
         * 
         *
         * @author eagle.sakura
         * @return
         * @version 2009/11/19 : 
         */
        public boolean isTouch() {
            return EagleUtil.isFlagOn(attrNow, eAttrTouch);
        }

        /**
         * 
         *
         * @author eagle.sakura
         * @return
         * @version 2010/05/21 : 
         */
        public boolean isRelease() {
            return !EagleUtil.isFlagOn(attrNow, eAttrTouch);
        }

        /**
         * 
         *
         * @author eagle.sakura
         * @return
         * @version 2010/05/21 : 
         */
        public boolean isReleaseOnce() {
            if (!EagleUtil.isFlagOn(attrNow, eAttrTouch) && EagleUtil.isFlagOn(attrOld, eAttrTouch)) {
                return true;
            }
            return false;
        }

        /**
         * 
         *
         * @author eagle.sakura
         * @return
         * @version 2009/11/19 : 
         */
        public boolean isTouchOnce() {
            if (EagleUtil.isFlagOn(attrNow, eAttrTouch) && !EagleUtil.isFlagOn(attrOld, eAttrTouch)) {
                return true;
            }
            return false;
        }

        /**
         * 
         *
         * @author eagle.sakura
         * @version 2009/11/19 : 
         */
        protected void update() {
            attrOld = attrNow;
            attrNow = attribute;
        }
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.