[Android] Documentation and some cleanup.

This commit is contained in:
Lioncash 2013-11-15 12:26:17 -05:00
parent 1e90a838f2
commit 07765aa6f0
4 changed files with 72 additions and 69 deletions

View File

@ -45,7 +45,7 @@ public final class NativeLibrary
/**
* Button states
*/
public class ButtonState
public static final class ButtonState
{
public static final int RELEASED = 0;
public static final int PRESSED = 1;
@ -60,12 +60,13 @@ public final class NativeLibrary
public static native void onTouchEvent(int Button, int Action);
/**
* Handles touch events.
* Handles axis-related touch events.
*
* @param Button Key code identifying which button was pressed.
* @param Action Mask for the action being performed.
* @param Axis Axis ID for the type of axis being altered. (Example: Main stick up, down, left, right, etc).
* @param force How 'far down' the joystick is pushed down. 0.0f indicates center (or no force),
* 1.0f indicates max force (or joystick pushed all the way down in any arbitrary direction).
*/
public static native void onTouchAxisEvent(int Axis, float loc);
public static native void onTouchAxisEvent(int Axis, float force);
/**
* Handles button press events for a gamepad.

View File

@ -13,9 +13,7 @@ import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
@ -26,21 +24,16 @@ import org.dolphinemu.dolphinemu.R;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
/**
* Draws the interactive input overlay on top of the
* {@link NativeGLSurfaceView} that is rendering emulation.
*/
public final class InputOverlay extends SurfaceView implements OnTouchListener, Runnable
public final class InputOverlay extends SurfaceView implements OnTouchListener
{
private final Set<InputOverlayDrawableButton> overlayButtons = new HashSet<InputOverlayDrawableButton>();
private final Set<InputOverlayDrawableJoystick> overlayJoysticks = new HashSet<InputOverlayDrawableJoystick>();
Semaphore _sema;
SurfaceHolder surfaceHolder;
Thread thread = null;
/**
* Constructor
*
@ -68,56 +61,40 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener,
// Request focus for the overlay so it has priority on presses.
requestFocus();
_sema = new Semaphore(0);
}
surfaceHolder = getHolder();
surfaceHolder.setFormat(PixelFormat.TRANSPARENT);
thread = new Thread(this);
thread.start();
}
private boolean Draw()
{
if(surfaceHolder.getSurface().isValid()){
// Draw everything
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
for (InputOverlayDrawableButton item : overlayButtons)
item.draw(canvas);
for (InputOverlayDrawableJoystick item : overlayJoysticks)
item.Draw(canvas);
surfaceHolder.unlockCanvasAndPost(canvas);
return true;
}
return false;
}
@Override
public void run() {
boolean didFirstPost = false;
while(!didFirstPost)
if (Draw())
didFirstPost = true;
while(true){
try
{
_sema.acquire();
Draw();
} catch (InterruptedException ex) {}
public void draw(Canvas canvas)
{
super.onDraw(canvas);
for (InputOverlayDrawableButton button : overlayButtons)
{
button.draw(canvas);
}
for (InputOverlayDrawableJoystick joystick: overlayJoysticks)
{
joystick.draw(canvas);
}
}
@Override
public boolean onTouch(View v, MotionEvent event)
{
// Determine the button state to apply based on the MotionEvent action flag.
// TODO: This will not work when Axis support is added. Make sure to refactor this when that time comes
// The reason it won't work is that when moving an axis, you would get the event MotionEvent.ACTION_MOVE.
//
// TODO: Refactor this so we detect either Axis movements or button presses so we don't run two loops all the time.
//
int buttonState = (event.getAction() == MotionEvent.ACTION_DOWN) ? ButtonState.PRESSED : ButtonState.RELEASED;
for (InputOverlayDrawableButton item : overlayButtons)
for (InputOverlayDrawableButton button : overlayButtons)
{
// Check if there was a touch within the bounds of a drawable.
if (item.getBounds().contains((int)event.getX(), (int)event.getY()))
if (button.getBounds().contains((int)event.getX(), (int)event.getY()))
{
switch (item.getId())
switch (button.getId())
{
case ButtonType.BUTTON_A:
NativeLibrary.onTouchEvent(ButtonType.BUTTON_A, buttonState);
@ -136,16 +113,18 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener,
}
}
}
for (InputOverlayDrawableJoystick item : overlayJoysticks)
for (InputOverlayDrawableJoystick joystick : overlayJoysticks)
{
item.TrackEvent(event);
int[] axisIDs = item.getAxisIDs();
float[] axises = item.getAxisValues();
joystick.TrackEvent(event);
int[] axisIDs = joystick.getAxisIDs();
float[] axises = joystick.getAxisValues();
for (int a = 0; a < 4; ++a)
{
NativeLibrary.onTouchAxisEvent(axisIDs[a], axises[a]);
}
}
_sema.release();
return true;
}
@ -214,20 +193,37 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener,
return overlayDrawable;
}
/**
* Initializes an {@link InputOverlayDrawableJoystick}
*
* @param context The current {@link Context}
* @param resOuter Resource ID for the outer image of the joystick (the static image that shows the circular bounds).
* @param resInner Resource ID for the inner image of the joystick (the one you actually move around).
* @param axisUp Identifier for this type of axis.
* @param axisDown Identifier for this type of axis.
* @param axisLeft Identifier for this type of axis.
* @param axisRight Identifier for this type of axis.
*
* @return the initialized {@link InputOverlayDrawableJoystick}.
*/
private static InputOverlayDrawableJoystick initializeOverlayJoystick(Context context, int resOuter, int resInner, int axisUp, int axisDown, int axisLeft, int axisRight)
{
// Resources handle for fetching the initial Drawable resource.
final Resources res = context.getResources();
// SharedPreference to retrieve the X and Y coordinates for the InputOverlayDrawableButton.
// SharedPreference to retrieve the X and Y coordinates for the InputOverlayDrawableJoystick.
final SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(context);
// Initialize the InputOverlayDrawableButton.
// Initialize the InputOverlayDrawableJoystick.
final Bitmap bitmapOuter = BitmapFactory.decodeResource(res, resOuter);
final Bitmap bitmapInner = BitmapFactory.decodeResource(res, resInner);
// Now set the bounds for the InputOverlayDrawableButton.
// This will dictate where on the screen (and the what the size) the InputOverlayDrawableButton will be.
// TODO: Load coordinates for the drawable from the SharedPreference keys
// made from the overlay configuration window in the settings.
// Now set the bounds for the InputOverlayDrawableJoystick.
// This will dictate where on the screen (and the what the size) the InputOverlayDrawableJoystick will be.
int outerSize = bitmapOuter.getWidth() + 256;
int X = 0;
int Y = 0;

View File

@ -14,7 +14,7 @@ import android.graphics.drawable.BitmapDrawable;
* Custom {@link BitmapDrawable} that is capable
* of storing it's own ID.
*/
public class InputOverlayDrawableButton extends BitmapDrawable
public final class InputOverlayDrawableButton extends BitmapDrawable
{
// The ID identifying what type of button this Drawable represents.
private int buttonType;

View File

@ -14,22 +14,21 @@ import android.graphics.drawable.BitmapDrawable;
import android.view.MotionEvent;
/**
* Custom {@link android.graphics.drawable.BitmapDrawable} that is capable
* Custom {@link BitmapDrawable} that is capable
* of storing it's own ID.
*/
public class InputOverlayDrawableJoystick extends BitmapDrawable
public final class InputOverlayDrawableJoystick extends BitmapDrawable
{
// The ID identifying what type of button this Drawable represents.
private int axisIDs[] = {0, 0, 0, 0};
private float axises[] = {0f, 0f};
private final int[] axisIDs = {0, 0, 0, 0};
private final float[] axises = {0f, 0f};
private final BitmapDrawable ringInner;
private int trackid = -1;
private BitmapDrawable ringInner;
/**
* Constructor
*
* @param res {@link android.content.res.Resources} instance.
* @param bitmapOuter {@link android.graphics.Bitmap} to use with this Drawable.
* @param res {@link Resources} instance.
* @param bitmapOuter {@link Bitmap} to use with this Drawable.
* @param axisUp Identifier for this type of axis.
* @param axisDown Identifier for this type of axis.
* @param axisLeft Identifier for this type of axis.
@ -51,11 +50,15 @@ public class InputOverlayDrawableJoystick extends BitmapDrawable
this.axisIDs[2] = axisLeft;
this.axisIDs[3] = axisRight;
}
public void Draw(Canvas canvas)
@Override
public void draw(Canvas canvas)
{
this.draw(canvas);
super.draw(canvas);
ringInner.draw(canvas);
}
public void TrackEvent(MotionEvent event)
{
int pointerIndex = event.getActionIndex();
@ -95,6 +98,7 @@ public class InputOverlayDrawableJoystick extends BitmapDrawable
SetInnerBounds();
}
public float[] getAxisValues()
{
float[] joyaxises = new float[4];
@ -120,10 +124,12 @@ public class InputOverlayDrawableJoystick extends BitmapDrawable
}
return joyaxises;
}
public int[] getAxisIDs()
{
return axisIDs;
}
private void SetInnerBounds()
{
float floatX = this.getBounds().centerX();