Android: Use button IDs to save screen layout

Using the drawable caused problems such as not being able to have multiple joysticks.
This commit is contained in:
Sean Maas 2016-09-21 17:53:14 -04:00
parent 74290e873a
commit c24a22e30f
3 changed files with 23 additions and 34 deletions

View File

@ -202,7 +202,7 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
if (mButtonBeingConfigured == button) if (mButtonBeingConfigured == button)
{ {
//Persist button position by saving new place. //Persist button position by saving new place.
saveControlPosition(mButtonBeingConfigured.getSharedPrefsId(), mButtonBeingConfigured.getBounds().left, mButtonBeingConfigured.getBounds().top); saveControlPosition(mButtonBeingConfigured.getId(), mButtonBeingConfigured.getBounds().left, mButtonBeingConfigured.getBounds().top);
mButtonBeingConfigured = null; mButtonBeingConfigured = null;
} }
break; break;
@ -233,7 +233,7 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
case MotionEvent.ACTION_POINTER_UP: case MotionEvent.ACTION_POINTER_UP:
if (mJoystickBeingConfigured != null) if (mJoystickBeingConfigured != null)
{ {
saveControlPosition(mJoystickBeingConfigured.getSharedPrefsId(), mJoystickBeingConfigured.getBounds().left, mJoystickBeingConfigured.getBounds().top); saveControlPosition(mJoystickBeingConfigured.getId(), mJoystickBeingConfigured.getBounds().left, mJoystickBeingConfigured.getBounds().top);
mJoystickBeingConfigured = null; mJoystickBeingConfigured = null;
} }
break; break;
@ -245,7 +245,7 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
return true; return true;
} }
private void saveControlPosition(String sharedPrefsId, int x, int y) private void saveControlPosition(int sharedPrefsId, int x, int y)
{ {
final SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); final SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor sPrefsEditor = sPrefs.edit(); SharedPreferences.Editor sPrefsEditor = sPrefs.edit();
@ -318,15 +318,12 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
// Initialize the InputOverlayDrawableButton. // Initialize the InputOverlayDrawableButton.
final Bitmap bitmap = resizeBitmap(context, BitmapFactory.decodeResource(res, resId), scale); final Bitmap bitmap = resizeBitmap(context, BitmapFactory.decodeResource(res, resId), scale);
// String ID of the Drawable. This is what is passed into SharedPreferences final InputOverlayDrawableButton overlayDrawable = new InputOverlayDrawableButton(res, bitmap, buttonId);
// to check whether or not a value has been set. Send to button so it can be referenced.
final String drawableId = res.getResourceEntryName(resId);
final InputOverlayDrawableButton overlayDrawable = new InputOverlayDrawableButton(res, bitmap, buttonId, drawableId);
// The X and Y coordinates of the InputOverlayDrawableButton on the InputOverlay. // The X and Y coordinates of the InputOverlayDrawableButton 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(buttonId+"-X", 0f);
int drawableY = (int) sPrefs.getFloat(drawableId+"-Y", 0f); int drawableY = (int) sPrefs.getFloat(buttonId+"-Y", 0f);
// Intrinsic width and height of the InputOverlayDrawableButton. // Intrinsic width and height of the InputOverlayDrawableButton.
// For any who may not know, intrinsic width/height // For any who may not know, intrinsic width/height
@ -369,14 +366,10 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
final Bitmap bitmapOuter = resizeBitmap(context, BitmapFactory.decodeResource(res, resOuter), 0.30f * overlaySize); final Bitmap bitmapOuter = resizeBitmap(context, BitmapFactory.decodeResource(res, resOuter), 0.30f * overlaySize);
final Bitmap bitmapInner = BitmapFactory.decodeResource(res, resInner); final Bitmap bitmapInner = BitmapFactory.decodeResource(res, resInner);
// String ID of the Drawable. This is what is passed into SharedPreferences
// to check whether or not a value has been set.
final String drawableId = res.getResourceEntryName(resOuter);
// The X and Y coordinates of the InputOverlayDrawableButton on the InputOverlay. // The X and Y coordinates of the InputOverlayDrawableButton 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(joystick+"-X", 0f);
int drawableY = (int) sPrefs.getFloat(drawableId+"-Y", 0f); int drawableY = (int) sPrefs.getFloat(joystick+"-Y", 0f);
// Now set the bounds for the InputOverlayDrawableJoystick. // Now set the bounds for the InputOverlayDrawableJoystick.
// This will dictate where on the screen (and the what the size) the InputOverlayDrawableJoystick will be. // This will dictate where on the screen (and the what the size) the InputOverlayDrawableJoystick will be.
@ -389,7 +382,7 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
= new InputOverlayDrawableJoystick(res, = new InputOverlayDrawableJoystick(res,
bitmapOuter, bitmapInner, bitmapOuter, bitmapInner,
outerRect, innerRect, outerRect, innerRect,
joystick, drawableId); joystick);
// Need to set the image's position // Need to set the image's position
overlayDrawable.setPosition(drawableX, drawableY); overlayDrawable.setPosition(drawableX, drawableY);

View File

@ -23,7 +23,6 @@ public final class InputOverlayDrawableButton extends BitmapDrawable
private int mButtonType; private int mButtonType;
private int mPreviousTouchX, mPreviousTouchY; private int mPreviousTouchX, mPreviousTouchY;
private int mControlPositionX, mControlPositionY; private int mControlPositionX, mControlPositionY;
private String mSharedPrefsId;
/** /**
* Constructor * Constructor
@ -31,13 +30,11 @@ public final class InputOverlayDrawableButton extends BitmapDrawable
* @param res {@link Resources} instance. * @param res {@link Resources} instance.
* @param bitmap {@link Bitmap} to use with this Drawable. * @param bitmap {@link Bitmap} to use with this Drawable.
* @param buttonType Identifier for this type of button. * @param buttonType Identifier for this type of button.
* @param sharedPrefsId Identifier for getting X and Y control positions from Shared Preferences.
*/ */
public InputOverlayDrawableButton(Resources res, Bitmap bitmap, int buttonType, String sharedPrefsId) public InputOverlayDrawableButton(Resources res, Bitmap bitmap, int buttonType)
{ {
super(res, bitmap); super(res, bitmap);
mButtonType = buttonType; mButtonType = buttonType;
mSharedPrefsId = sharedPrefsId;
} }
/** /**
@ -73,11 +70,6 @@ public final class InputOverlayDrawableButton extends BitmapDrawable
return true; return true;
} }
public String getSharedPrefsId()
{
return mSharedPrefsId;
}
public void setPosition(int x, int y) public void setPosition(int x, int y)
{ {
mControlPositionX = x; mControlPositionX = x;

View File

@ -24,7 +24,7 @@ public final class InputOverlayDrawableJoystick extends BitmapDrawable
private final float[] axises = {0f, 0f}; private final float[] axises = {0f, 0f};
private final BitmapDrawable ringInner; private final BitmapDrawable ringInner;
private int trackId = -1; private int trackId = -1;
private String mSharedPrefsId; private int mJoystickType;
private int mControlPositionX, mControlPositionY; private int mControlPositionX, mControlPositionY;
private int mPreviousTouchX, mPreviousTouchY; private int mPreviousTouchX, mPreviousTouchY;
@ -37,12 +37,11 @@ public final class InputOverlayDrawableJoystick extends BitmapDrawable
* @param rectOuter {@link Rect} which represents the outer joystick bounds. * @param rectOuter {@link Rect} which represents the outer joystick bounds.
* @param rectInner {@link Rect} which represents the inner joystick bounds. * @param rectInner {@link Rect} which represents the inner joystick bounds.
* @param joystick Identifier for which joystick this is. * @param joystick Identifier for which joystick this is.
* @param sharedPrefsId Identifier for getting X and Y control positions from Shared Preferences.
*/ */
public InputOverlayDrawableJoystick(Resources res, public InputOverlayDrawableJoystick(Resources res,
Bitmap bitmapOuter, Bitmap bitmapInner, Bitmap bitmapOuter, Bitmap bitmapInner,
Rect rectOuter, Rect rectInner, Rect rectOuter, Rect rectInner,
int joystick, String sharedPrefsId) int joystick)
{ {
super(res, bitmapOuter); super(res, bitmapOuter);
this.setBounds(rectOuter); this.setBounds(rectOuter);
@ -54,7 +53,17 @@ public final class InputOverlayDrawableJoystick extends BitmapDrawable
this.axisIDs[1] = joystick + 2; this.axisIDs[1] = joystick + 2;
this.axisIDs[2] = joystick + 3; this.axisIDs[2] = joystick + 3;
this.axisIDs[3] = joystick + 4; this.axisIDs[3] = joystick + 4;
mSharedPrefsId = sharedPrefsId; mJoystickType = joystick;
}
/**
* Gets this InputOverlayDrawableJoystick's button ID.
*
* @return this InputOverlayDrawableJoystick's button ID.
*/
public int getId()
{
return mJoystickType;
} }
@Override @Override
@ -168,11 +177,6 @@ public final class InputOverlayDrawableJoystick extends BitmapDrawable
ringInner.invalidateSelf(); ringInner.invalidateSelf();
} }
public String getSharedPrefsId()
{
return mSharedPrefsId;
}
public void setPosition(int x, int y) public void setPosition(int x, int y)
{ {
mControlPositionX = x; mControlPositionX = x;