android: Use the center of the object and reduce draw calls

This commit is contained in:
german77 2023-03-04 21:42:26 -06:00 committed by bunnei
parent 7dd02363a3
commit bde568c3c5
8 changed files with 76 additions and 59 deletions

View file

@ -12,6 +12,7 @@ import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.TextView;
@ -164,6 +165,9 @@ public final class EmulationActivity extends AppCompatActivity {
}
private void enableFullscreenImmersive() {
getWindow().getAttributes().layoutInDisplayCutoutMode=
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
// It would be nice to use IMMERSIVE_STICKY, but that doesn't show the toolbar.
mDecorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |

View file

@ -186,7 +186,7 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener,
scale = 0.38f;
break;
default:
scale = 0.40f;
scale = 0.43f;
break;
}
@ -215,10 +215,10 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener,
// Now set the bounds for the InputOverlayDrawableButton.
// This will dictate where on the screen (and the what the size) the InputOverlayDrawableButton will be.
overlayDrawable.setBounds(drawableX, drawableY, drawableX + width, drawableY + height);
overlayDrawable.setBounds(drawableX - (width / 2), drawableY - (height / 2), drawableX + (width / 2), drawableY + (height / 2));
// Need to set the image's position
overlayDrawable.setPosition(drawableX, drawableY);
overlayDrawable.setPosition(drawableX - (width / 2), drawableY - (height / 2));
return overlayDrawable;
}
@ -278,10 +278,10 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener,
// Now set the bounds for the InputOverlayDrawableDpad.
// This will dictate where on the screen (and the what the size) the InputOverlayDrawableDpad will be.
overlayDrawable.setBounds(drawableX, drawableY, drawableX + width, drawableY + height);
overlayDrawable.setBounds(drawableX - (width / 2), drawableY - (height / 2), drawableX + (width / 2), drawableY + (height / 2));
// Need to set the image's position
overlayDrawable.setPosition(drawableX, drawableY);
overlayDrawable.setPosition(drawableX - (width / 2), drawableY - (height / 2));
return overlayDrawable;
}
@ -306,7 +306,7 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener,
final SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(context);
// Decide scale based on user preference
float scale = 0.35f;
float scale = 0.40f;
scale *= (sPrefs.getInt("controlScale", 50) + 50);
scale /= 100;
@ -320,12 +320,12 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener,
int drawableX = (int) sPrefs.getFloat(button + orientation + "-X", 0f);
int drawableY = (int) sPrefs.getFloat(button + orientation + "-Y", 0f);
float outerScale = 1.3f;
float outerScale = 1.66f;
// 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();
Rect outerRect = new Rect(drawableX, drawableY, drawableX + outerSize, drawableY + outerSize);
Rect outerRect = new Rect(drawableX - (outerSize / 2), drawableY - (outerSize / 2), drawableX + (outerSize / 2), drawableY + (outerSize / 2));
Rect innerRect = new Rect(0, 0, (int) (outerSize / outerScale), (int) (outerSize / outerScale));
// Send the drawableId to the joystick so it can be referenced when saving control position.
@ -362,12 +362,13 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener,
if (isInEditMode()) {
return onTouchWhileEditing(event);
}
boolean should_update_view = false;
for (InputOverlayDrawableButton button : overlayButtons) {
if (!button.updateStatus(event)) {
continue;
}
NativeLibrary.onGamePadButtonEvent(NativeLibrary.Player1Device, button.getId(), button.getStatus());
should_update_view = true;
}
for (InputOverlayDrawableDpad dpad : overlayDpads) {
@ -378,6 +379,7 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener,
NativeLibrary.onGamePadButtonEvent(NativeLibrary.Player1Device, dpad.getDownId(), dpad.getDownStatus());
NativeLibrary.onGamePadButtonEvent(NativeLibrary.Player1Device, dpad.getLeftId(), dpad.getLeftStatus());
NativeLibrary.onGamePadButtonEvent(NativeLibrary.Player1Device, dpad.getRightId(), dpad.getRightStatus());
should_update_view = true;
}
for (InputOverlayDrawableJoystick joystick : overlayJoysticks) {
@ -387,6 +389,11 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener,
int axisID = joystick.getJoystickId();
NativeLibrary.onGamePadJoystickEvent(NativeLibrary.Player1Device, axisID, joystick.getXAxis(), joystick.getYAxis());
NativeLibrary.onGamePadButtonEvent(NativeLibrary.Player1Device, joystick.getButtonId(), joystick.getButtonStatus());
should_update_view = true;
}
if (should_update_view) {
invalidate();
}
if (!mPreferences.getBoolean("isTouchEnabled", true)) {
@ -420,8 +427,6 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener,
NativeLibrary.onTouchReleased(pointerId);
}
invalidate();
return true;
}
@ -536,11 +541,11 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener,
overlayJoysticks.add(initializeOverlayJoystick(getContext(), R.drawable.joystick_range,
R.drawable.joystick, R.drawable.joystick_depressed, StickType.STICK_R, ButtonType.STICK_R, orientation));
}
if (mPreferences.getBoolean("buttonToggle13", true)) {
if (mPreferences.getBoolean("buttonToggle13", false)) {
overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.facebutton_home,
R.drawable.facebutton_home_depressed, ButtonType.BUTTON_HOME, orientation));
}
if (mPreferences.getBoolean("buttonToggle14", true)) {
if (mPreferences.getBoolean("buttonToggle14", false)) {
overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.facebutton_screenshot,
R.drawable.facebutton_screenshot_depressed, ButtonType.BUTTON_CAPTURE, orientation));
}
@ -580,7 +585,7 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener,
if (!mPreferences.getBoolean("OverlayInit", false)) {
defaultOverlayLandscape();
}
resetButtonPlacement();
SharedPreferences.Editor sPrefsEditor = mPreferences.edit();
sPrefsEditor.putBoolean("OverlayInit", true);
sPrefsEditor.apply();
@ -596,7 +601,7 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener,
// Get screen size
Display display = ((Activity) getContext()).getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
display.getRealMetrics(outMetrics);
float maxX = outMetrics.heightPixels;
float maxY = outMetrics.widthPixels;
// Height and width changes depending on orientation. Use the larger value for height.
@ -605,6 +610,7 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener,
maxX = maxY;
maxY = tmp;
}
Resources res = getResources();
// Each value is a percent from max X/Y stored as an int. Have to bring that value down
@ -621,8 +627,8 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener,
sPrefsEditor.putFloat(ButtonType.TRIGGER_ZL + "-Y", (((float) res.getInteger(R.integer.SWITCH_TRIGGER_ZL_Y) / 1000) * maxY));
sPrefsEditor.putFloat(ButtonType.TRIGGER_ZR + "-X", (((float) res.getInteger(R.integer.SWITCH_TRIGGER_ZR_X) / 1000) * maxX));
sPrefsEditor.putFloat(ButtonType.TRIGGER_ZR + "-Y", (((float) res.getInteger(R.integer.SWITCH_TRIGGER_ZR_Y) / 1000) * maxY));
sPrefsEditor.putFloat(ButtonType.DPAD_UP + "-X", (((float) res.getInteger(R.integer.SWITCH_BUTTON_UP_X) / 1000) * maxX));
sPrefsEditor.putFloat(ButtonType.DPAD_UP + "-Y", (((float) res.getInteger(R.integer.SWITCH_BUTTON_UP_Y) / 1000) * maxY));
sPrefsEditor.putFloat(ButtonType.DPAD_UP + "-X", (((float) res.getInteger(R.integer.SWITCH_BUTTON_DPAD_X) / 1000) * maxX));
sPrefsEditor.putFloat(ButtonType.DPAD_UP + "-Y", (((float) res.getInteger(R.integer.SWITCH_BUTTON_DPAD_Y) / 1000) * maxY));
sPrefsEditor.putFloat(ButtonType.TRIGGER_L + "-X", (((float) res.getInteger(R.integer.SWITCH_TRIGGER_L_X) / 1000) * maxX));
sPrefsEditor.putFloat(ButtonType.TRIGGER_L + "-Y", (((float) res.getInteger(R.integer.SWITCH_TRIGGER_L_Y) / 1000) * maxY));
sPrefsEditor.putFloat(ButtonType.TRIGGER_R + "-X", (((float) res.getInteger(R.integer.SWITCH_TRIGGER_R_X) / 1000) * maxX));

View file

@ -67,6 +67,7 @@ public final class InputOverlayDrawableButton {
int motion_event = event.getAction() & MotionEvent.ACTION_MASK;
boolean isActionDown = motion_event == MotionEvent.ACTION_DOWN || motion_event == MotionEvent.ACTION_POINTER_DOWN;
boolean isActionUp = motion_event == MotionEvent.ACTION_UP || motion_event == MotionEvent.ACTION_POINTER_UP;
boolean current_state = mPressedState;
if (isActionDown) {
if (!getBounds().contains(xPosition, yPosition)) {

View file

@ -121,12 +121,16 @@ public final class InputOverlayDrawableDpad {
maxY -= getBounds().centerY();
final float AxisX = touchX / maxX;
final float AxisY = touchY / maxY;
final boolean up_state = mUpButtonState;
final boolean down_state = mDownButtonState;
final boolean left_state = mLeftButtonState;
final boolean right_state = mRightButtonState;
mUpButtonState = AxisY < -InputOverlayDrawableDpad.VIRT_AXIS_DEADZONE;
mDownButtonState = AxisY > InputOverlayDrawableDpad.VIRT_AXIS_DEADZONE;
mLeftButtonState = AxisX < -InputOverlayDrawableDpad.VIRT_AXIS_DEADZONE;
mRightButtonState = AxisX > InputOverlayDrawableDpad.VIRT_AXIS_DEADZONE;
return true;
return up_state != mUpButtonState || down_state != mDownButtonState || left_state != mLeftButtonState || right_state != mRightButtonState;
}
return false;

View file

@ -140,6 +140,8 @@ public final class InputOverlayDrawableJoystick {
maxY -= getVirtBounds().centerY();
final float AxisX = touchX / maxX;
final float AxisY = touchY / maxY;
final float oldXAxis = mXAxis;
final float oldYAxis = mYAxis;
// Clamp the circle pad input to a circle
final float angle = (float) Math.atan2(AxisY, AxisX);
@ -150,7 +152,7 @@ public final class InputOverlayDrawableJoystick {
mXAxis = ((float) Math.cos(angle) * radius);
mYAxis = ((float) Math.sin(angle) * radius);
SetInnerBounds();
return true;
return oldXAxis != mXAxis && oldYAxis != mYAxis;
}
return false;

View file

@ -20,17 +20,17 @@
</aapt:attr>
</path>
<path android:fillAlpha="0.6"
android:pathData="M18.74,64.84A132.8,132.8 0,1 0,200.8 18.74,132.8 132.8,0 0,0 18.74,64.84ZM230,190.72A113.12,113.12 0,1 1,190.72 35.64,113.12 113.12,0 0,1 230,190.72Z" android:strokeAlpha="0.6">
android:pathData="m18.72,64.82a132.8,132.8 0,1 0,182.06 -46.1,132.8 132.8,0 0,0 -182.06,46.1zM229.98,190.7a113.12,113.12 0,1 1,-39.28 -155.08,113.12 113.12,0 0,1 39.28,155.08z" android:strokeAlpha="0.6">
<aapt:attr name="android:fillColor">
<gradient android:centerX="132.84" android:centerY="132.73"
android:gradientRadius="132.8" android:type="radial">
<gradient android:centerX="132.82" android:centerY="132.7"
android:gradientRadius="141.24" android:type="radial">
<item android:color="#FF969696" android:offset="0"/>
<item android:color="#FF949494" android:offset="0.45"/>
<item android:color="#FF8D8D8D" android:offset="0.61"/>
<item android:color="#FF828282" android:offset="0.72"/>
<item android:color="#FF717171" android:offset="0.82"/>
<item android:color="#FF5B5B5B" android:offset="0.9"/>
<item android:color="#FF404040" android:offset="0.97"/>
<item android:color="#FF949494" android:offset="0.8"/>
<item android:color="#FF8D8D8D" android:offset="0.84"/>
<item android:color="#FF828282" android:offset="0.87"/>
<item android:color="#FF717171" android:offset="0.9"/>
<item android:color="#FF5B5B5B" android:offset="0.94"/>
<item android:color="#FF404040" android:offset="0.98"/>
<item android:color="#FF303030" android:offset="1"/>
</gradient>
</aapt:attr>

View file

@ -23,7 +23,7 @@
<TextView
android:id="@+id/show_fps_text"
android:layout_marginStart="8dp"
android:layout_marginStart="18dp"
android:layout_marginTop="2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

View file

@ -4,35 +4,35 @@
<integer name="game_grid_columns">1</integer>
<!-- Default SWITCH landscape layout -->
<integer name="SWITCH_BUTTON_A_X">930</integer>
<integer name="SWITCH_BUTTON_A_Y">610</integer>
<integer name="SWITCH_BUTTON_B_X">870</integer>
<integer name="SWITCH_BUTTON_B_Y">720</integer>
<integer name="SWITCH_BUTTON_X_X">870</integer>
<integer name="SWITCH_BUTTON_X_Y">500</integer>
<integer name="SWITCH_BUTTON_Y_X">810</integer>
<integer name="SWITCH_BUTTON_Y_Y">610</integer>
<integer name="SWITCH_STICK_L_X">170</integer>
<integer name="SWITCH_STICK_L_Y">740</integer>
<integer name="SWITCH_STICK_R_X">715</integer>
<integer name="SWITCH_STICK_R_Y">740</integer>
<integer name="SWITCH_TRIGGER_L_X">13</integer>
<integer name="SWITCH_TRIGGER_L_Y">125</integer>
<integer name="SWITCH_TRIGGER_R_X">895</integer>
<integer name="SWITCH_TRIGGER_R_Y">125</integer>
<integer name="SWITCH_TRIGGER_ZL_X">13</integer>
<integer name="SWITCH_TRIGGER_ZL_Y">0</integer>
<integer name="SWITCH_TRIGGER_ZR_X">895</integer>
<integer name="SWITCH_TRIGGER_ZR_Y">0</integer>
<integer name="SWITCH_BUTTON_MINUS_X">440</integer>
<integer name="SWITCH_BUTTON_MINUS_Y">850</integer>
<integer name="SWITCH_BUTTON_PLUS_X">520</integer>
<integer name="SWITCH_BUTTON_PLUS_Y">850</integer>
<integer name="SWITCH_BUTTON_HOME_X">600</integer>
<integer name="SWITCH_BUTTON_HOME_Y">890</integer>
<integer name="SWITCH_BUTTON_CAPTURE_X">360</integer>
<integer name="SWITCH_BUTTON_CAPTURE_Y">890</integer>
<integer name="SWITCH_BUTTON_UP_X">35</integer>
<integer name="SWITCH_BUTTON_UP_Y">480</integer>
<integer name="SWITCH_BUTTON_A_X">760</integer>
<integer name="SWITCH_BUTTON_A_Y">810</integer>
<integer name="SWITCH_BUTTON_B_X">710</integer>
<integer name="SWITCH_BUTTON_B_Y">920</integer>
<integer name="SWITCH_BUTTON_X_X">710</integer>
<integer name="SWITCH_BUTTON_X_Y">700</integer>
<integer name="SWITCH_BUTTON_Y_X">660</integer>
<integer name="SWITCH_BUTTON_Y_Y">810</integer>
<integer name="SWITCH_STICK_L_X">100</integer>
<integer name="SWITCH_STICK_L_Y">670</integer>
<integer name="SWITCH_STICK_R_X">900</integer>
<integer name="SWITCH_STICK_R_Y">670</integer>
<integer name="SWITCH_TRIGGER_L_X">70</integer>
<integer name="SWITCH_TRIGGER_L_Y">220</integer>
<integer name="SWITCH_TRIGGER_R_X">930</integer>
<integer name="SWITCH_TRIGGER_R_Y">220</integer>
<integer name="SWITCH_TRIGGER_ZL_X">70</integer>
<integer name="SWITCH_TRIGGER_ZL_Y">90</integer>
<integer name="SWITCH_TRIGGER_ZR_X">930</integer>
<integer name="SWITCH_TRIGGER_ZR_Y">90</integer>
<integer name="SWITCH_BUTTON_MINUS_X">460</integer>
<integer name="SWITCH_BUTTON_MINUS_Y">955</integer>
<integer name="SWITCH_BUTTON_PLUS_X">540</integer>
<integer name="SWITCH_BUTTON_PLUS_Y">955</integer>
<integer name="SWITCH_BUTTON_HOME_X">620</integer>
<integer name="SWITCH_BUTTON_HOME_Y">960</integer>
<integer name="SWITCH_BUTTON_CAPTURE_X">380</integer>
<integer name="SWITCH_BUTTON_CAPTURE_Y">960</integer>
<integer name="SWITCH_BUTTON_DPAD_X">260</integer>
<integer name="SWITCH_BUTTON_DPAD_Y">810</integer>
</resources>