diff --git a/Source/Android/res/layout/input_overlay_config_layout.xml b/Source/Android/res/layout/input_overlay_config_layout.xml index 29b4f268b8..fc4b1bcf15 100644 --- a/Source/Android/res/layout/input_overlay_config_layout.xml +++ b/Source/Android/res/layout/input_overlay_config_layout.xml @@ -5,7 +5,7 @@ android:layout_height="fill_parent" > \ No newline at end of file diff --git a/Source/Android/src/org/dolphinemu/dolphinemu/emulation/overlay/InputOverlay.java b/Source/Android/src/org/dolphinemu/dolphinemu/emulation/overlay/InputOverlay.java index c2272fdc5f..d681a3c85c 100644 --- a/Source/Android/src/org/dolphinemu/dolphinemu/emulation/overlay/InputOverlay.java +++ b/Source/Android/src/org/dolphinemu/dolphinemu/emulation/overlay/InputOverlay.java @@ -3,8 +3,14 @@ package org.dolphinemu.dolphinemu.emulation.overlay; import java.util.HashSet; import java.util.Set; +import org.dolphinemu.dolphinemu.R; + import android.content.Context; +import android.content.SharedPreferences; +import android.content.res.Resources; import android.graphics.Canvas; +import android.graphics.drawable.BitmapDrawable; +import android.preference.PreferenceManager; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.SurfaceView; @@ -17,7 +23,7 @@ import android.view.View.OnTouchListener; */ public final class InputOverlay extends SurfaceView implements OnTouchListener { - private final Set overlayItems = new HashSet(); + private final Set overlayItems = new HashSet(); /** * Constructor @@ -29,6 +35,11 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener { super(context, attrs); + // Add all the overlay items to the HashSet. + overlayItems.add(initializeOverlayDrawable(context, R.drawable.button_a)); + overlayItems.add(initializeOverlayDrawable(context, R.drawable.button_b)); + overlayItems.add(initializeOverlayDrawable(context, R.drawable.button_start)); + // Force draw setWillNotDraw(false); @@ -56,9 +67,74 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener { super.onDraw(canvas); - for (InputOverlayItem item : overlayItems) + // Draw all overlay items. + for (BitmapDrawable item : overlayItems) { item.draw(canvas); } } + + /** + * Initializes a drawable, given by resId, with all of the + * parameters set for it to be properly shown on the InputOverlay. + *

+ * This works due to the way the X and Y coordinates are stored within + * the {@link SharedPreferences}. + *

+ * In the input overlay configuration menu, + * once a touch event begins and then ends (ie. Organizing the buttons to one's own liking for the overlay). + * the X and Y coordinates of the button at the END of its touch event + * (when you remove your finger/stylus from the touchscreen) are then stored + * within a SharedPreferences instance so that those values can be retrieved here. + *

+ * This has a few benefits over the conventional way of storing the values + * (ie. within the Dolphin ini file). + *

    + *
  • No native calls
  • + *
  • Keeps Android-only values inside the Android environment
  • + *
+ *

+ * Technically no modifications should need to be performed on the returned + * BitmapDrawable. Simply add it to the HashSet of overlay items and wait + * for Android to call the onDraw method. + * + * @param context The current {@link Context}. + * @param resId The resource ID of the {@link BitmapDrawable} to get. + * + * @return A {@link BitmapDrawable} with the correct drawing bounds set. + * + */ + private static BitmapDrawable initializeOverlayDrawable(Context context, int resId) + { + // Resources handle for fetching the drawable, etc. + final Resources res = context.getResources(); + + // SharedPreference to retrieve the X and Y coordinates for the drawable. + final SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(context); + + // Get the desired drawable. + BitmapDrawable drawable = (BitmapDrawable) res.getDrawable(resId); + + // String ID of the drawable. This is what is passed into SharedPreferences + // to check whether or not a value has been set. + String drawableId = res.getResourceEntryName(resId); + + // The X and Y coordinates of the drawable on the InputOverlay. + // These were set in the input overlay configuration menu. + int drawableX = (int) sPrefs.getFloat(drawableId+"-X", 0f); + int drawableY = (int) sPrefs.getFloat(drawableId+"-Y", 0f); + + // Intrinsic width and height of the drawable. + // For any who may not know, intrinsic width/height + // are the original unmodified width and height of the image. + int intrinWidth = drawable.getIntrinsicWidth(); + int intrinHeight = drawable.getIntrinsicHeight(); + + // Now set the bounds for the drawable. + // This will dictate where on the screen (and the what the size) of the drawable will be. + drawable.setBounds(drawableX, drawableY, drawableX+intrinWidth, drawableY+intrinHeight); + + return drawable; + } + } diff --git a/Source/Android/src/org/dolphinemu/dolphinemu/emulation/overlay/InputOverlayItem.java b/Source/Android/src/org/dolphinemu/dolphinemu/emulation/overlay/InputOverlayItem.java deleted file mode 100644 index a45578b263..0000000000 --- a/Source/Android/src/org/dolphinemu/dolphinemu/emulation/overlay/InputOverlayItem.java +++ /dev/null @@ -1,107 +0,0 @@ -package org.dolphinemu.dolphinemu.emulation.overlay; - -import android.content.res.Resources; -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Rect; -import android.graphics.drawable.BitmapDrawable; - -/** - * Represents a drawable image for the {@link InputOverlay} - */ -public final class InputOverlayItem -{ - // The image as a BitmapDrawable - private BitmapDrawable drawable; - - // Width and height of the underlying image. - private int width; - private int height; - - // X and Y coordinates to display this item at. - private int x; - private int y; - - // Image scale factor. - private float scaleFactor = 1.0f; - - // Rectangle that we draw this item to. - private Rect drawRect; - - /** - * Constructor. - * - * @param res Reference to the app resources for fetching display metrics. - * @param resId Resource ID of the {@link BitmapDrawable} to encapsulate. - */ - public InputOverlayItem(Resources res, int resId) - { - // Idiot-proof the constructor. - if (res == null) - throw new IllegalArgumentException("res cannot be null"); - - // Everything is valid, decode the filename as a bitmap. - drawable = (BitmapDrawable) res.getDrawable(resId); - Bitmap image = drawable.getBitmap(); - - // Set width/height - width = image.getWidth(); - height = image.getHeight(); - - // Initialize rectangle to zero width, height, x, and y. - drawRect = new Rect(); - } - - /** - * Constructor - * - * @param res Reference to the app resources for fetching display metrics. - * @param resId Resource ID of the {@link BitmapDrawable} to encapsulate. - * @param x X coordinate on the screen to place the control. - * @param y Y coordinate on the screen to place the control. - */ - public InputOverlayItem(Resources res, int resId, int x, int y) - { - this(res, resId); - - setPosition(x, y); - } - - /** - * Sets the position of this item on the screen. - * - * @param x New x-coordinate for this image. - * @param y New y-coordinate for this image. - */ - public void setPosition(int x, int y) - { - this.x = x; - this.y = y; - - drawRect.set(x, y, x + (int)(width * scaleFactor), y + (int)(height * scaleFactor)); - drawable.setBounds(drawRect); - } - - /** - * Sets a new scaling factor for the current image. - * - * @param scaleFactor The new scaling factor. Note that 1.0 is normal size. - */ - public void setScaleFactor(float scaleFactor) - { - this.scaleFactor = scaleFactor; - - // Adjust for the new scale factor. - setPosition(x, y); - } - - /** - * Draws this item to a given canvas. - * - * @param canvas The canvas to draw this item to. - */ - public void draw(Canvas canvas) - { - drawable.draw(canvas); - } -} diff --git a/Source/Android/src/org/dolphinemu/dolphinemu/settings/input/InputOverlayConfigButton.java b/Source/Android/src/org/dolphinemu/dolphinemu/settings/input/InputOverlayConfigButton.java index cca596e85c..39ac957234 100644 --- a/Source/Android/src/org/dolphinemu/dolphinemu/settings/input/InputOverlayConfigButton.java +++ b/Source/Android/src/org/dolphinemu/dolphinemu/settings/input/InputOverlayConfigButton.java @@ -7,6 +7,8 @@ package org.dolphinemu.dolphinemu.settings.input; import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; @@ -19,6 +21,9 @@ import android.widget.Button; */ public final class InputOverlayConfigButton extends Button implements OnTouchListener { + // SharedPreferences instance that the button positions are cached to. + private final SharedPreferences sharedPrefs; + /** * Constructor * @@ -31,6 +36,9 @@ public final class InputOverlayConfigButton extends Button implements OnTouchLis // Set the button as its own OnTouchListener. setOnTouchListener(this); + + // Get the SharedPreferences instance. + sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); } @Override @@ -45,6 +53,20 @@ public final class InputOverlayConfigButton extends Button implements OnTouchLis setY(getY() + event.getY()); return true; } + + // Whenever the press event has ended + // is when we save all of the information. + case MotionEvent.ACTION_UP: + { + // String ID of this button. + String buttonId = getResources().getResourceEntryName(getId()); + + // Add the current X and Y positions of this button into SharedPreferences. + SharedPreferences.Editor editor = sharedPrefs.edit(); + editor.putFloat(buttonId+"-X", getX()); + editor.putFloat(buttonId+"-Y", getY()); + editor.commit(); + } } return false;