Android Open Source - HexNanoController_Android Button






From Project

Back to project page HexNanoController_Android.

License

The source code is released under:

Code license GNU GPL v2 http://www.gnu.org/licenses/gpl.html Content license CC BY-NC-SA 4.0 http://creativecommons.org/licenses/by-nc-sa/4.0/

If you think the Android project HexNanoController_Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.hexairbot.hexmini.ui;
//  w ww  .ja  va  2s .com
import javax.microedition.khronos.opengles.GL10;

import android.content.res.Resources;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;

import com.hexairbot.hexmini.ui.gl.GLSprite;

public class Button extends Sprite
{
  protected GLSprite spriteNormal;
  protected GLSprite spritePressed;

  protected boolean isPressed;
  private boolean isInitialized;
  
  private OnClickListener clickListener;
  private int pointerId; // Id of the pointer that has touched the control
  
  
  public Button(Resources resources, int normalBitmapId, int pressedBitmapId, Align align) 
  {
    super(align);
    
    spriteNormal = new GLSprite(resources, normalBitmapId);
    spritePressed = new GLSprite(resources, pressedBitmapId);
    isInitialized = false;
  }


  public void init(GL10 gl, int program) 
  {
    spriteNormal.init(gl, program);
    spritePressed.init(gl, program);

  }

  
  public void surfaceChanged(GL10 gl, int width, int height) 
  {
    spriteNormal.onSurfaceChanged(gl, width, height);
    spritePressed.onSurfaceChanged(gl, width, height);
    
    super.surfaceChanged(gl, width, height);
    
    isInitialized = true;
  }

  
  public void surfaceChanged(Canvas canvas) 
  {
    super.surfaceChanged(canvas);
    
    isInitialized = true;
  }

  
  public void draw(GL10 gl) 
  {
    if (bounds != null && visible) {
      if (isPressed) {
        spritePressed.onDraw(gl,  bounds.left, surfaceHeight - bounds.top - spritePressed.height);
      } else {
        spriteNormal.onDraw(gl,  bounds.left, surfaceHeight - bounds.top - spriteNormal.height);
      }
    }
  }

  
  public void draw(Canvas canvas) 
  {
    if (bounds != null  && visible) {
      if (isPressed) {
        spritePressed.onDraw(canvas, bounds.left, bounds.top);
      } else {
        spriteNormal.onDraw(canvas,  bounds.left, bounds.top);
      }
    }
  }

  
  @Override
    protected void onAlphaChanged(float newAlpha)
    {
      spriteNormal.alpha = newAlpha;
        spritePressed.alpha = newAlpha;
    }


  public void setOnClickListener(OnClickListener listener)
  {
    this.clickListener = listener;
  }
  
  
  public void setImages(Resources resources, int normal, int pressed)
  {
    spriteNormal = new GLSprite(resources, normal);
    spritePressed = new GLSprite(resources, pressed);
    
    isInitialized = false;
  }
  
  
  public boolean onTouchEvent(View v, MotionEvent event) 
  {
    int action = event.getAction();
    int actionCode = action & MotionEvent.ACTION_MASK;
    int actionIndex = event.getActionIndex();

    switch (actionCode) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_DOWN: {
      
        for (int i=0; i<event.getPointerCount(); ++i) {
          int x = (int)event.getX(i);
          int y = (int)event.getY(i);
          
          if (bounds != null && bounds.contains(x, y)) {
            if (enabled && visible) {
                pointerId = event.getPointerId(i);
                isPressed = true;
                
                return true;
            } else if (!enabled && visible) {
                return true;
            }
          }
        }
      
      break;
    }
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP: 
      if (isPressed) {
          for (int i=0; i<event.getPointerCount(); ++i) {
            if (bounds != null && bounds.contains((int)event.getX(i), 
                                (int)event.getY(i))
                                && pointerId == event.getPointerId(i)) {
              
              if (clickListener != null && enabled && visible) {
                clickListener.onClick(null);
              }
              
              isPressed = false;
              pointerId = 0;
              return true;
            }
          }
      }
      
      break;
      
    case MotionEvent.ACTION_MOVE:
        if (isPressed) {
            for (int i=0; i<event.getPointerCount(); ++i) {
                if (pointerId == event.getPointerId(i)) {
                    int x = (int) event.getX(actionIndex);
                    int y = (int) event.getY(actionIndex);
                    
                    if (!bounds.contains(x, y)) {
                        isPressed = false;
                        pointerId = 0;
                    }
                }
            }
        }
        break;
    }
    
    return false;
  }


  public boolean isInitialized()
  {
    return isInitialized;    
  }  

  
  @Override
  public void setViewAndProjectionMatrices(float[] vMatrix, float[] projMatrix) {
    spriteNormal.setViewAndProjectionMatrices(vMatrix, projMatrix);
    spritePressed.setViewAndProjectionMatrices(vMatrix, projMatrix);
  }


  @Override
  public int getWidth()
  {
    return spriteNormal.width;
  }


  @Override
  public int getHeight() 
  {
    return spriteNormal.height;
  }


    @Override
    public void freeResources()
    {
        spriteNormal.freeResources();
        spritePressed.freeResources();
    }


  @Override
  public void setNeedsUpdate() {
    isInitialized = false;
  }
}




Java Source Code List

.FileHelper.java
.Input.java
.Output.java
.Serializable.java
com.hexairbot.hexmini.HelpActivity.java
com.hexairbot.hexmini.HexMiniApplication.java
com.hexairbot.hexmini.HudActivity.java
com.hexairbot.hexmini.HudViewControllerDelegate.java
com.hexairbot.hexmini.HudViewController.java
com.hexairbot.hexmini.SettingsDialogDelegate.java
com.hexairbot.hexmini.SettingsDialog.java
com.hexairbot.hexmini.SettingsViewControllerDelegate.java
com.hexairbot.hexmini.SettingsViewController.java
com.hexairbot.hexmini.ViewController.java
com.hexairbot.hexmini.adapter.SettingsViewAdapter.java
com.hexairbot.hexmini.ble.BleConnectinManagerDelegate.java
com.hexairbot.hexmini.ble.BleConnectinManager.java
com.hexairbot.hexmini.ble.BluetoothLeService.java
com.hexairbot.hexmini.gestures.EnhancedGestureDetector.java
com.hexairbot.hexmini.modal.ApplicationSettings.java
com.hexairbot.hexmini.modal.Channel.java
com.hexairbot.hexmini.modal.OSDCommon.java
com.hexairbot.hexmini.modal.Transmitter.java
com.hexairbot.hexmini.sensors.DeviceOrientationChangeDelegate.java
com.hexairbot.hexmini.sensors.DeviceOrientationManager.java
com.hexairbot.hexmini.sensors.DeviceSensorManagerWrapper.java
com.hexairbot.hexmini.sensors.SensorManagerWrapper.java
com.hexairbot.hexmini.services.ConnectStateManager.java
com.hexairbot.hexmini.services.IpcControlService.java
com.hexairbot.hexmini.services.IpcProxy.java
com.hexairbot.hexmini.services.NavData.java
com.hexairbot.hexmini.services.OnIpcConnectChangedListener.java
com.hexairbot.hexmini.services.VIConfig.java
com.hexairbot.hexmini.ui.Button.java
com.hexairbot.hexmini.ui.Image.java
com.hexairbot.hexmini.ui.Indicator.java
com.hexairbot.hexmini.ui.Sprite.java
com.hexairbot.hexmini.ui.Text.java
com.hexairbot.hexmini.ui.ToggleButton.java
com.hexairbot.hexmini.ui.UIRenderer.java
com.hexairbot.hexmini.ui.control.CustomSeekBar.java
com.hexairbot.hexmini.ui.control.ViewPagerIndicator.java
com.hexairbot.hexmini.ui.gl.GLSprite.java
com.hexairbot.hexmini.ui.joystick.AcceleratorJoystick.java
com.hexairbot.hexmini.ui.joystick.AnalogueJoystick.java
com.hexairbot.hexmini.ui.joystick.JoystickBase.java
com.hexairbot.hexmini.ui.joystick.JoystickFactory.java
com.hexairbot.hexmini.ui.joystick.JoystickListener.java
com.hexairbot.hexmini.util.DebugHandler.java
com.hexairbot.hexmini.util.FontUtils.java
com.hexairbot.hexmini.util.SystemUiHiderBase.java
com.hexairbot.hexmini.util.SystemUiHiderHoneycomb.java
com.hexairbot.hexmini.util.SystemUiHider.java
com.hexairbot.hexmini.util.SystemUtil.java
com.hexairbot.hexmini.util.TextureUtils.java
fix.android.opengl.GLES20.java