Android Open Source - TreeFrogEngine Ouya Game Controller






From Project

Back to project page TreeFrogEngine.

License

The source code is released under:

MIT License

If you think the Android project TreeFrogEngine 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.discretesoftworks.OUYAframework;
//from w  w  w  .  j a va2  s . c om
import tv.ouya.console.api.OuyaController;
import android.graphics.PointF;
import android.view.KeyEvent;
import android.view.MotionEvent;

import com.discretesoftworks.framework.GameRenderer;
import com.discretesoftworks.networking.NetworkGameController;

public abstract class OuyaGameController extends NetworkGameController{

  public static final float deadzone = .28f;
  
  private static PointF[] stickValues = { new PointF(0,0),
                      new PointF(0,0),
                      new PointF(0,0), 
                      new PointF(0,0) };
  
  public static final int LEFT_STICK = 1;
  public static final int RIGHT_STICK = 2;
  
  @Override
  public void update(float deltaTime){
    OuyaController.startOfFrame();
    GameRenderer.s_instance.update(deltaTime);
    super.update(deltaTime);
  }
  
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
        return OuyaController.onKeyDown(keyCode, event);
  }

  @Override
  public boolean onKeyUp(int keyCode, KeyEvent event) {
    return OuyaController.onKeyUp(keyCode, event);
  }

  @Override
  public boolean onGenericMotionEvent(MotionEvent event) {
    return OuyaController.onGenericMotionEvent(event);
  }
  
  public static PointF getRawStickValues(OuyaController c, int stick){
    float dx = 0;
    float dy = 0;
    if (stick == LEFT_STICK){
      dx = c.getAxisValue(OuyaController.AXIS_LS_X);
      dy = c.getAxisValue(OuyaController.AXIS_LS_Y);
    }
    else {
      dx = c.getAxisValue(OuyaController.AXIS_RS_X);
      dy = c.getAxisValue(OuyaController.AXIS_RS_Y);
    }
    stickValues[c.getPlayerNum()].x = dx;
    stickValues[c.getPlayerNum()].y = dy;
    return stickValues[c.getPlayerNum()];
  }
  
  public static PointF getStickValues(OuyaController c, int stick){
    float dx = 0;
    float dy = 0;
    if (stick == LEFT_STICK){
      dx = c.getAxisValue(OuyaController.AXIS_LS_X);
      dy = c.getAxisValue(OuyaController.AXIS_LS_Y);
    }
    else {
      dx = c.getAxisValue(OuyaController.AXIS_RS_X);
      dy = c.getAxisValue(OuyaController.AXIS_RS_Y);
    }
    
    stickValues[c.getPlayerNum()].x = dx;
    stickValues[c.getPlayerNum()].y = dy;
    
    /*
    float mag = (float)Math.sqrt(dx*dx + dy*dy);
    stickValues[c.getPlayerNum()].x = dx/mag;
    stickValues[c.getPlayerNum()].y = dy/mag;
    
    stickValues[c.getPlayerNum()].x *= ((Math.abs(dx) - deadzone) / (1f - deadzone));
    stickValues[c.getPlayerNum()].y *= ((Math.abs(dy) - deadzone) / (1f - deadzone));
    */
    return stickValues[c.getPlayerNum()];
  }
  
  public static boolean stickInDeadzone(OuyaController c, int stick){
    PointF stickM = getRawStickValues(c,stick);
    float dx = stickM.x;
    float dy = stickM.y;
    if (Math.abs(dx) > deadzone || Math.abs(dy) > deadzone || getRawStickMagnitude(c,stick) > deadzone)
      return false;
    return true;
  }
  
  public static float getRawStickMagnitude(OuyaController c, int stick){
    PointF stickM = getRawStickValues(c,stick);
    float dx = stickM.x;
    float dy = stickM.y;
    float mag = (float)Math.sqrt(dx*dx + dy*dy);
    if (mag <= 1)
      return mag;
    return 1;
  }
  
  public static float getStickMagnitude(OuyaController c, int stick){
    PointF stickM = getStickValues(c,stick);
    float dx = stickM.x;
    float dy = stickM.y;
    float mag = (float)Math.sqrt(dx*dx + dy*dy);
    if (mag <= 1)
      return mag;
    return 1;
  }
  
  public static float getStickDirection(OuyaController c, int stick){
    PointF stickM = getStickValues(c,stick);
    float axisX = stickM.x;
    float axisY = stickM.y;
    float dir = (float) Math.toDegrees( Math.atan2(-axisX, axisY) );
    return dir;
  }
  
  public static float getInvertedStickDirection(OuyaController c, int stick){
    PointF stickM = getStickValues(c,stick);
    float axisX = stickM.x;
    float axisY = -stickM.y;
    float dir = (float) Math.toDegrees( Math.atan2(-axisX, axisY) );
    return dir;
  }
  
}




Java Source Code List

com.discretesoftworks.OUYAframework.OuyaGameController.java
com.discretesoftworks.OUYAframework.OuyaGame.java
com.discretesoftworks.TestGame.Floor.java
com.discretesoftworks.TestGame.Player.java
com.discretesoftworks.TestGame.Pointer.java
com.discretesoftworks.TestGame.TestController.java
com.discretesoftworks.TestGame.TestGame.java
com.discretesoftworks.framework.AndroidGame.java
com.discretesoftworks.framework.Assets.java
com.discretesoftworks.framework.Directional.java
com.discretesoftworks.framework.GameController.java
com.discretesoftworks.framework.GameFont.java
com.discretesoftworks.framework.GameObject.java
com.discretesoftworks.framework.GameRenderer.java
com.discretesoftworks.framework.GriddedObject.java
com.discretesoftworks.framework.ModelLoader.java
com.discretesoftworks.framework.MovingObject.java
com.discretesoftworks.framework.MyGLRenderer.java
com.discretesoftworks.framework.MyGLSurfaceView.java
com.discretesoftworks.framework.NumericalMatrix.java
com.discretesoftworks.framework.Pair.java
com.discretesoftworks.framework.Path.java
com.discretesoftworks.framework.Pool.java
com.discretesoftworks.framework.RaggedDictionary.java
com.discretesoftworks.framework.RenderModel.java
com.discretesoftworks.framework.Sprite.java
com.discretesoftworks.framework.Terrain.java
com.discretesoftworks.framework.TextDisplay.java
com.discretesoftworks.framework.TextHolder.java
com.discretesoftworks.framework.View.java
com.discretesoftworks.networking.NetworkGameController.java
com.discretesoftworks.networking.Writer.java