TouchArea.java :  » UnTagged » hentaymaster » GameUtils » Android Open Source

Android Open Source » UnTagged » hentaymaster 
hentaymaster » GameUtils » TouchArea.java
package GameUtils;

import android.view.MotionEvent;

public class TouchArea 
{
  //states
  public final static int k_STATE_OFF   = 0;
  public final static int k_STATE_WAIT   = 1;
  public final static int k_STATE_TOUCH   = 2;
  public final static int k_STATE_DRAG   = 3;
  
  private int m_rectx0, m_recty0, m_rectx1, m_recty1, m_state;
  
  //------------------------------------------------------------------------------
  // constructor
  //------------------------------------------------------------------------------
  public TouchArea() 
  {
    m_rectx0 = m_recty0 =  m_rectx1 = m_recty1 = 0;
    m_state = k_STATE_OFF;
  }
  
  //------------------------------------------------------------------------------
  // check if it's enabled
  //------------------------------------------------------------------------------
  public boolean IsEnabled () { return m_state != k_STATE_OFF; }
  
  //------------------------------------------------------------------------------
  // Enable the touch area
  //  @param x, y, w, h  position and size of the touch area
  //------------------------------------------------------------------------------
  public void Enable(int x, int y, int w, int h)
  {
    m_rectx0 = x;
    m_recty0 = y;
    m_rectx1 = x + w;
    m_recty1 = y + h;
    m_state  = k_STATE_WAIT;
  }
  
  //------------------------------------------------------------------------------
  // Disable the touch area
  //------------------------------------------------------------------------------
  public void Disable()
  {
    m_state = k_STATE_OFF;
  }
  
  //------------------------------------------------------------------------------
  // process on touch event
  //------------------------------------------------------------------------------
  public void OnTouchEvent (int action, int x, int y)
  {
    if((x >= m_rectx0) && (x <= m_rectx1) && (y >= m_recty0) && (y <= m_recty1))
    {
      if(action == MotionEvent.ACTION_DOWN)
      {
        m_state = k_STATE_TOUCH;
      }
      else if(m_state != k_STATE_WAIT)
      {
        if(action == MotionEvent.ACTION_MOVE)
        {
          m_state = k_STATE_DRAG;
        }
        if((action == MotionEvent.ACTION_CANCEL) || (action == MotionEvent.ACTION_UP))
        {
          m_state = k_STATE_WAIT;
        }
      }
    }
  }
}
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.