[Android] Overlay now works during emulation.
This commit is contained in:
parent
c24dfe559b
commit
d1834b3058
|
@ -15,6 +15,42 @@ import android.view.Surface;
|
||||||
*/
|
*/
|
||||||
public final class NativeLibrary
|
public final class NativeLibrary
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Button type for use in onTouchEvent
|
||||||
|
*/
|
||||||
|
public static final class ButtonType
|
||||||
|
{
|
||||||
|
public static final int BUTTON_A = 0;
|
||||||
|
public static final int BUTTON_B = 1;
|
||||||
|
public static final int BUTTON_START = 2;
|
||||||
|
public static final int BUTTON_X = 3;
|
||||||
|
public static final int BUTTON_Y = 4;
|
||||||
|
public static final int BUTTON_Z = 5;
|
||||||
|
public static final int BUTTON_UP = 6;
|
||||||
|
public static final int BUTTON_DOWN = 7;
|
||||||
|
public static final int BUTTON_LEFT = 8;
|
||||||
|
public static final int BUTTON_RIGHT = 9;
|
||||||
|
public static final int STICK_MAIN_UP = 10;
|
||||||
|
public static final int STICK_MAIN_DOWN = 11;
|
||||||
|
public static final int STICK_MAIN_LEFT = 12;
|
||||||
|
public static final int STICK_MAIN_RIGHT = 13;
|
||||||
|
public static final int STICK_C_UP = 14;
|
||||||
|
public static final int STICK_C_DOWN = 15;
|
||||||
|
public static final int STICK_C_LEFT = 16;
|
||||||
|
public static final int STICK_C_RIGHT = 17;
|
||||||
|
public static final int TRIGGER_L = 18;
|
||||||
|
public static final int TRIGGER_R = 19;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Button states
|
||||||
|
*/
|
||||||
|
public class ButtonState
|
||||||
|
{
|
||||||
|
public static final int RELEASED = 0;
|
||||||
|
public static final int PRESSED = 1;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles touch events.
|
* Handles touch events.
|
||||||
*
|
*
|
||||||
|
|
|
@ -9,13 +9,18 @@ package org.dolphinemu.dolphinemu.emulation.overlay;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.dolphinemu.dolphinemu.NativeLibrary;
|
||||||
|
import org.dolphinemu.dolphinemu.NativeLibrary.ButtonState;
|
||||||
|
import org.dolphinemu.dolphinemu.NativeLibrary.ButtonType;
|
||||||
import org.dolphinemu.dolphinemu.R;
|
import org.dolphinemu.dolphinemu.R;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.BitmapFactory;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
import android.graphics.drawable.BitmapDrawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
|
@ -29,7 +34,7 @@ import android.view.View.OnTouchListener;
|
||||||
*/
|
*/
|
||||||
public final class InputOverlay extends SurfaceView implements OnTouchListener
|
public final class InputOverlay extends SurfaceView implements OnTouchListener
|
||||||
{
|
{
|
||||||
private final Set<BitmapDrawable> overlayItems = new HashSet<BitmapDrawable>();
|
private final Set<InputOverlayDrawable> overlayItems = new HashSet<InputOverlayDrawable>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
|
@ -42,9 +47,12 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
|
|
||||||
// Add all the overlay items to the HashSet.
|
// Add all the overlay items to the HashSet.
|
||||||
overlayItems.add(initializeOverlayDrawable(context, R.drawable.button_a));
|
overlayItems.add(initializeOverlayDrawable(context, R.drawable.button_a, ButtonType.BUTTON_A));
|
||||||
overlayItems.add(initializeOverlayDrawable(context, R.drawable.button_b));
|
overlayItems.add(initializeOverlayDrawable(context, R.drawable.button_b, ButtonType.BUTTON_B));
|
||||||
overlayItems.add(initializeOverlayDrawable(context, R.drawable.button_start));
|
overlayItems.add(initializeOverlayDrawable(context, R.drawable.button_start, ButtonType.BUTTON_START));
|
||||||
|
|
||||||
|
// Set the on touch listener.
|
||||||
|
setOnTouchListener(this);
|
||||||
|
|
||||||
// Force draw
|
// Force draw
|
||||||
setWillNotDraw(false);
|
setWillNotDraw(false);
|
||||||
|
@ -56,16 +64,35 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
|
||||||
@Override
|
@Override
|
||||||
public boolean onTouch(View v, MotionEvent event)
|
public boolean onTouch(View v, MotionEvent event)
|
||||||
{
|
{
|
||||||
switch (event.getAction())
|
// Determine the button state to apply based on the MotionEvent action flag.
|
||||||
|
int buttonState = (event.getAction() == MotionEvent.ACTION_DOWN) ? ButtonState.PRESSED : ButtonState.RELEASED;
|
||||||
|
|
||||||
|
for (InputOverlayDrawable item : overlayItems)
|
||||||
{
|
{
|
||||||
case MotionEvent.ACTION_DOWN:
|
// Check if there was a touch within the bounds of a drawable.
|
||||||
|
if (item.getBounds().contains((int)event.getX(), (int)event.getY()))
|
||||||
{
|
{
|
||||||
// TODO: Handle down presses.
|
switch (item.getId())
|
||||||
return true;
|
{
|
||||||
|
case ButtonType.BUTTON_A:
|
||||||
|
NativeLibrary.onTouchEvent(ButtonType.BUTTON_A, buttonState);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ButtonType.BUTTON_B:
|
||||||
|
NativeLibrary.onTouchEvent(ButtonType.BUTTON_B, buttonState);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ButtonType.BUTTON_START:
|
||||||
|
NativeLibrary.onTouchEvent(ButtonType.BUTTON_START, buttonState);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -74,14 +101,14 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
|
||||||
super.onDraw(canvas);
|
super.onDraw(canvas);
|
||||||
|
|
||||||
// Draw all overlay items.
|
// Draw all overlay items.
|
||||||
for (BitmapDrawable item : overlayItems)
|
for (InputOverlayDrawable item : overlayItems)
|
||||||
{
|
{
|
||||||
item.draw(canvas);
|
item.draw(canvas);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a drawable, given by resId, with all of the
|
* Initializes an InputOverlayDrawable, given by resId, with all of the
|
||||||
* parameters set for it to be properly shown on the InputOverlay.
|
* parameters set for it to be properly shown on the InputOverlay.
|
||||||
* <p>
|
* <p>
|
||||||
* This works due to the way the X and Y coordinates are stored within
|
* This works due to the way the X and Y coordinates are stored within
|
||||||
|
@ -101,46 +128,48 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
|
||||||
* </ul>
|
* </ul>
|
||||||
* <p>
|
* <p>
|
||||||
* Technically no modifications should need to be performed on the returned
|
* Technically no modifications should need to be performed on the returned
|
||||||
* BitmapDrawable. Simply add it to the HashSet of overlay items and wait
|
* InputOverlayDrawable. Simply add it to the HashSet of overlay items and wait
|
||||||
* for Android to call the onDraw method.
|
* for Android to call the onDraw method.
|
||||||
*
|
*
|
||||||
* @param context The current {@link Context}.
|
* @param context The current {@link Context}.
|
||||||
* @param resId The resource ID of the {@link BitmapDrawable} to get.
|
* @param resId The resource ID of the {@link Drawable} to get the {@link Bitmap} of.
|
||||||
|
* @param buttonId Identifier for determining what type of button the initialized InputOverlayDrawable represents.
|
||||||
*
|
*
|
||||||
* @return A {@link BitmapDrawable} with the correct drawing bounds set.
|
* @return An {@link InputOverlayDrawable} with the correct drawing bounds set.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
private static BitmapDrawable initializeOverlayDrawable(Context context, int resId)
|
private static InputOverlayDrawable initializeOverlayDrawable(Context context, int resId, int buttonId)
|
||||||
{
|
{
|
||||||
// Resources handle for fetching the drawable, etc.
|
// Resources handle for fetching the initial Drawable resource.
|
||||||
final Resources res = context.getResources();
|
final Resources res = context.getResources();
|
||||||
|
|
||||||
// SharedPreference to retrieve the X and Y coordinates for the drawable.
|
// SharedPreference to retrieve the X and Y coordinates for the InputOverlayDrawable.
|
||||||
final SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(context);
|
final SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||||
|
|
||||||
// Get the desired drawable.
|
// Initialize the InputOverlayDrawable.
|
||||||
BitmapDrawable drawable = (BitmapDrawable) res.getDrawable(resId);
|
final Bitmap bitmap = BitmapFactory.decodeResource(res, resId);
|
||||||
|
final InputOverlayDrawable overlayDrawable = new InputOverlayDrawable(res, bitmap, buttonId);
|
||||||
|
|
||||||
// String ID of the drawable. This is what is passed into SharedPreferences
|
// String ID of the Drawable. This is what is passed into SharedPreferences
|
||||||
// to check whether or not a value has been set.
|
// to check whether or not a value has been set.
|
||||||
String drawableId = res.getResourceEntryName(resId);
|
final String drawableId = res.getResourceEntryName(resId);
|
||||||
|
|
||||||
// The X and Y coordinates of the drawable on the InputOverlay.
|
// The X and Y coordinates of the InputOverlayDrawable on the InputOverlay.
|
||||||
// These were set in the input overlay configuration menu.
|
// These were set in the input overlay configuration menu.
|
||||||
int drawableX = (int) sPrefs.getFloat(drawableId+"-X", 0f);
|
int drawableX = (int) sPrefs.getFloat(drawableId+"-X", 0f);
|
||||||
int drawableY = (int) sPrefs.getFloat(drawableId+"-Y", 0f);
|
int drawableY = (int) sPrefs.getFloat(drawableId+"-Y", 0f);
|
||||||
|
|
||||||
// Intrinsic width and height of the drawable.
|
// Intrinsic width and height of the InputOverlayDrawable.
|
||||||
// For any who may not know, intrinsic width/height
|
// For any who may not know, intrinsic width/height
|
||||||
// are the original unmodified width and height of the image.
|
// are the original unmodified width and height of the image.
|
||||||
int intrinWidth = drawable.getIntrinsicWidth();
|
int intrinWidth = overlayDrawable.getIntrinsicWidth();
|
||||||
int intrinHeight = drawable.getIntrinsicHeight();
|
int intrinHeight = overlayDrawable.getIntrinsicHeight();
|
||||||
|
|
||||||
// Now set the bounds for the drawable.
|
// Now set the bounds for the InputOverlayDrawable.
|
||||||
// This will dictate where on the screen (and the what the size) of the drawable will be.
|
// This will dictate where on the screen (and the what the size) the InputOverlayDrawable will be.
|
||||||
drawable.setBounds(drawableX, drawableY, drawableX+intrinWidth, drawableY+intrinHeight);
|
overlayDrawable.setBounds(drawableX, drawableY, drawableX+intrinWidth, drawableY+intrinHeight);
|
||||||
|
|
||||||
return drawable;
|
return overlayDrawable;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
/**
|
||||||
|
* Copyright 2013 Dolphin Emulator Project
|
||||||
|
* Licensed under GPLv2
|
||||||
|
* Refer to the license.txt file included.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.dolphinemu.dolphinemu.emulation.overlay;
|
||||||
|
|
||||||
|
import android.content.res.Resources;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.drawable.BitmapDrawable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom {@link BitmapDrawable} that is capable
|
||||||
|
* of storing it's own ID.
|
||||||
|
*/
|
||||||
|
public class InputOverlayDrawable extends BitmapDrawable
|
||||||
|
{
|
||||||
|
// The ID identifying what type of button this Drawable represents.
|
||||||
|
private int buttonType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param res {@link Resources} instance.
|
||||||
|
* @param bitmap {@link Bitmap} to use with this Drawable.
|
||||||
|
* @param buttonType Identifier for this type of button.
|
||||||
|
*/
|
||||||
|
public InputOverlayDrawable(Resources res, Bitmap bitmap, int buttonType)
|
||||||
|
{
|
||||||
|
super(res, bitmap);
|
||||||
|
|
||||||
|
this.buttonType = buttonType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets this InputOverlayDrawable's button ID.
|
||||||
|
*
|
||||||
|
* @return this InputOverlayDrawable's button ID.
|
||||||
|
*/
|
||||||
|
public int getId()
|
||||||
|
{
|
||||||
|
return buttonType;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue