summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Narr the Reg2023-02-13 13:15:57 -0600
committerGravatar bunnei2023-06-03 00:05:30 -0700
commit3be891ea6faffb10d34eb780f5650d35d7a50bc3 (patch)
treeacdc26495063bc757aaafed03568293adef0341f /src
parentandroid: Clean dpad overlay (diff)
downloadyuzu-3be891ea6faffb10d34eb780f5650d35d7a50bc3.tar.gz
yuzu-3be891ea6faffb10d34eb780f5650d35d7a50bc3.tar.xz
yuzu-3be891ea6faffb10d34eb780f5650d35d7a50bc3.zip
android: Clean joystick overlay
Diffstat (limited to 'src')
-rw-r--r--src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.java8
-rw-r--r--src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlay.java27
-rw-r--r--src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableJoystick.java231
3 files changed, 131 insertions, 135 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.java b/src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.java
index dbbc339ec..2ab3a834a 100644
--- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.java
+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.java
@@ -638,6 +638,14 @@ public final class NativeLibrary {
638 } 638 }
639 639
640 /** 640 /**
641 * Stick type for use in onTouchEvent
642 */
643 public static final class StickType {
644 public static final int STICK_L = 0;
645 public static final int STICK_R = 1;
646 }
647
648 /**
641 * Button states 649 * Button states
642 */ 650 */
643 public static final class ButtonState { 651 public static final class ButtonState {
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlay.java b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlay.java
index c316a63b1..6b51a596f 100644
--- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlay.java
+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlay.java
@@ -26,8 +26,8 @@ import android.view.View;
26import android.view.View.OnTouchListener; 26import android.view.View.OnTouchListener;
27 27
28import org.yuzu.yuzu_emu.NativeLibrary; 28import org.yuzu.yuzu_emu.NativeLibrary;
29import org.yuzu.yuzu_emu.NativeLibrary.ButtonState;
30import org.yuzu.yuzu_emu.NativeLibrary.ButtonType; 29import org.yuzu.yuzu_emu.NativeLibrary.ButtonType;
30import org.yuzu.yuzu_emu.NativeLibrary.StickType;
31import org.yuzu.yuzu_emu.R; 31import org.yuzu.yuzu_emu.R;
32import org.yuzu.yuzu_emu.utils.EmulationMenuSettings; 32import org.yuzu.yuzu_emu.utils.EmulationMenuSettings;
33 33
@@ -271,10 +271,11 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener {
271 * @param defaultResInner Resource ID for the default inner image of the joystick (the one you actually move around). 271 * @param defaultResInner Resource ID for the default inner image of the joystick (the one you actually move around).
272 * @param pressedResInner Resource ID for the pressed inner image of the joystick. 272 * @param pressedResInner Resource ID for the pressed inner image of the joystick.
273 * @param joystick Identifier for which joystick this is. 273 * @param joystick Identifier for which joystick this is.
274 * @param button Identifier for which joystick button this is.
274 * @return the initialized {@link InputOverlayDrawableJoystick}. 275 * @return the initialized {@link InputOverlayDrawableJoystick}.
275 */ 276 */
276 private static InputOverlayDrawableJoystick initializeOverlayJoystick(Context context, 277 private static InputOverlayDrawableJoystick initializeOverlayJoystick(Context context,
277 int resOuter, int defaultResInner, int pressedResInner, int joystick, String orientation) { 278 int resOuter, int defaultResInner, int pressedResInner, int joystick, int button, String orientation) {
278 // Resources handle for fetching the initial Drawable resource. 279 // Resources handle for fetching the initial Drawable resource.
279 final Resources res = context.getResources(); 280 final Resources res = context.getResources();
280 281
@@ -294,8 +295,8 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener {
294 295
295 // The X and Y coordinates of the InputOverlayDrawableButton on the InputOverlay. 296 // The X and Y coordinates of the InputOverlayDrawableButton on the InputOverlay.
296 // These were set in the input overlay configuration menu. 297 // These were set in the input overlay configuration menu.
297 int drawableX = (int) sPrefs.getFloat(joystick + orientation + "-X", 0f); 298 int drawableX = (int) sPrefs.getFloat(button + orientation + "-X", 0f);
298 int drawableY = (int) sPrefs.getFloat(joystick + orientation + "-Y", 0f); 299 int drawableY = (int) sPrefs.getFloat(button + orientation + "-Y", 0f);
299 300
300 float outerScale = 1.3f; 301 float outerScale = 1.3f;
301 302
@@ -309,7 +310,7 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener {
309 final InputOverlayDrawableJoystick overlayDrawable 310 final InputOverlayDrawableJoystick overlayDrawable
310 = new InputOverlayDrawableJoystick(res, bitmapOuter, 311 = new InputOverlayDrawableJoystick(res, bitmapOuter,
311 bitmapInnerDefault, bitmapInnerPressed, 312 bitmapInnerDefault, bitmapInnerPressed,
312 outerRect, innerRect, joystick); 313 outerRect, innerRect, joystick, button);
313 314
314 // Need to set the image's position 315 // Need to set the image's position
315 overlayDrawable.setPosition(drawableX, drawableY); 316 overlayDrawable.setPosition(drawableX, drawableY);
@@ -386,12 +387,12 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener {
386 } 387 }
387 388
388 for (InputOverlayDrawableJoystick joystick : overlayJoysticks) { 389 for (InputOverlayDrawableJoystick joystick : overlayJoysticks) {
389 joystick.TrackEvent(event); 390 if (!joystick.updateStatus(event)) {
390 int axisID = joystick.getId(); 391 continue;
391 float[] axises = joystick.getAxisValues(); 392 }
392 393 int axisID = joystick.getJoystickId();
393 NativeLibrary 394 NativeLibrary.onGamePadMoveEvent(NativeLibrary.TouchScreenDevice, axisID, joystick.getXAxis(), joystick.getYAxis());
394 .onGamePadMoveEvent(NativeLibrary.TouchScreenDevice, axisID, axises[0], axises[1]); 395 NativeLibrary.onGamePadEvent(NativeLibrary.TouchScreenDevice, joystick.getButtonId(), joystick.getButtonStatus());
395 } 396 }
396 397
397 invalidate(); 398 invalidate();
@@ -455,11 +456,11 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener {
455 if (mPreferences.getBoolean("buttonToggle11", true)) { 456 if (mPreferences.getBoolean("buttonToggle11", true)) {
456 overlayJoysticks.add(initializeOverlayJoystick(getContext(), R.drawable.stick_main_range, 457 overlayJoysticks.add(initializeOverlayJoystick(getContext(), R.drawable.stick_main_range,
457 R.drawable.stick_main, R.drawable.stick_main_pressed, 458 R.drawable.stick_main, R.drawable.stick_main_pressed,
458 ButtonType.STICK_L, orientation)); 459 StickType.STICK_L, ButtonType.STICK_L, orientation));
459 } 460 }
460 if (mPreferences.getBoolean("buttonToggle12", true)) { 461 if (mPreferences.getBoolean("buttonToggle12", true)) {
461 overlayJoysticks.add(initializeOverlayJoystick(getContext(), R.drawable.stick_main_range, 462 overlayJoysticks.add(initializeOverlayJoystick(getContext(), R.drawable.stick_main_range,
462 R.drawable.stick_main, R.drawable.stick_main_pressed, ButtonType.STICK_R, orientation)); 463 R.drawable.stick_main, R.drawable.stick_main_pressed, StickType.STICK_R, ButtonType.STICK_R, orientation));
463 } 464 }
464 if (mPreferences.getBoolean("buttonToggle13", true)) { 465 if (mPreferences.getBoolean("buttonToggle13", true)) {
465 overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.button_a, 466 overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.button_a,
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableJoystick.java b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableJoystick.java
index 03d38862b..4a0b9fd86 100644
--- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableJoystick.java
+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableJoystick.java
@@ -13,6 +13,7 @@ import android.graphics.Rect;
13import android.graphics.drawable.BitmapDrawable; 13import android.graphics.drawable.BitmapDrawable;
14import android.view.MotionEvent; 14import android.view.MotionEvent;
15 15
16import org.yuzu.yuzu_emu.NativeLibrary;
16import org.yuzu.yuzu_emu.NativeLibrary.ButtonType; 17import org.yuzu.yuzu_emu.NativeLibrary.ButtonType;
17import org.yuzu.yuzu_emu.utils.EmulationMenuSettings; 18import org.yuzu.yuzu_emu.utils.EmulationMenuSettings;
18 19
@@ -21,12 +22,15 @@ import org.yuzu.yuzu_emu.utils.EmulationMenuSettings;
21 * of storing it's own ID. 22 * of storing it's own ID.
22 */ 23 */
23public final class InputOverlayDrawableJoystick { 24public final class InputOverlayDrawableJoystick {
24 private final int[] axisIDs = {0, 0, 0, 0}; 25 // The ID value what type of joystick this Drawable represents.
25 private final float[] axises = {0f, 0f}; 26 private int mJoystickId;
26 private int trackId = -1; 27 // The ID value what type of button this Drawable represents.
27 private int mJoystickType; 28 private int mButtonId;
29 // The ID value what motion event is tracking
30 private int mTrackId = -1;
31 private float mXAxis;
32 private float mYAxis;
28 private int mControlPositionX, mControlPositionY; 33 private int mControlPositionX, mControlPositionY;
29 private int mPreviousTouchX, mPreviousTouchY;
30 private int mWidth; 34 private int mWidth;
31 private int mHeight; 35 private int mHeight;
32 private Rect mVirtBounds; 36 private Rect mVirtBounds;
@@ -50,12 +54,9 @@ public final class InputOverlayDrawableJoystick {
50 */ 54 */
51 public InputOverlayDrawableJoystick(Resources res, Bitmap bitmapOuter, 55 public InputOverlayDrawableJoystick(Resources res, Bitmap bitmapOuter,
52 Bitmap bitmapInnerDefault, Bitmap bitmapInnerPressed, 56 Bitmap bitmapInnerDefault, Bitmap bitmapInnerPressed,
53 Rect rectOuter, Rect rectInner, int joystick) { 57 Rect rectOuter, Rect rectInner, int joystick, int button) {
54 axisIDs[0] = joystick + 1; // Up 58 mJoystickId = joystick;
55 axisIDs[1] = joystick + 2; // Down 59 mButtonId = button;
56 axisIDs[2] = joystick + 3; // Left
57 axisIDs[3] = joystick + 4; // Right
58 mJoystickType = joystick;
59 60
60 mOuterBitmap = new BitmapDrawable(res, bitmapOuter); 61 mOuterBitmap = new BitmapDrawable(res, bitmapOuter);
61 mDefaultStateInnerBitmap = new BitmapDrawable(res, bitmapInnerDefault); 62 mDefaultStateInnerBitmap = new BitmapDrawable(res, bitmapInnerDefault);
@@ -74,131 +75,90 @@ public final class InputOverlayDrawableJoystick {
74 SetInnerBounds(); 75 SetInnerBounds();
75 } 76 }
76 77
77 /**
78 * Gets this InputOverlayDrawableJoystick's button ID.
79 *
80 * @return this InputOverlayDrawableJoystick's button ID.
81 */
82 public int getId() {
83 return mJoystickType;
84 }
85
86 public void draw(Canvas canvas) { 78 public void draw(Canvas canvas) {
87 mOuterBitmap.draw(canvas); 79 mOuterBitmap.draw(canvas);
88 getCurrentStateBitmapDrawable().draw(canvas); 80 getCurrentStateBitmapDrawable().draw(canvas);
89 mBoundsBoxBitmap.draw(canvas); 81 mBoundsBoxBitmap.draw(canvas);
90 } 82 }
91 83
92 public void TrackEvent(MotionEvent event) { 84 public boolean updateStatus(MotionEvent event) {
93 int pointerIndex = event.getActionIndex(); 85 int pointerIndex = event.getActionIndex();
86 int xPosition = (int) event.getX(pointerIndex);
87 int yPosition = (int) event.getY(pointerIndex);
88 int pointerId = event.getPointerId(pointerIndex);
89 int motion_event = event.getAction() & MotionEvent.ACTION_MASK;
90 boolean isActionDown = motion_event == MotionEvent.ACTION_DOWN || motion_event == MotionEvent.ACTION_POINTER_DOWN;
91 boolean isActionUp = motion_event == MotionEvent.ACTION_UP || motion_event == MotionEvent.ACTION_POINTER_UP;
92
93 if (isActionDown) {
94 if (!getBounds().contains(xPosition, yPosition)) {
95 return false;
96 }
97 mPressedState = true;
98 mOuterBitmap.setAlpha(0);
99 mBoundsBoxBitmap.setAlpha(255);
100 if (EmulationMenuSettings.getJoystickRelCenter()) {
101 getVirtBounds().offset(xPosition - getVirtBounds().centerX(),
102 yPosition - getVirtBounds().centerY());
103 }
104 mBoundsBoxBitmap.setBounds(getVirtBounds());
105 mTrackId = pointerId;
106 }
94 107
95 switch (event.getAction() & MotionEvent.ACTION_MASK) { 108 if (isActionUp) {
96 case MotionEvent.ACTION_DOWN: 109 if (mTrackId != pointerId) {
97 case MotionEvent.ACTION_POINTER_DOWN: 110 return false;
98 if (getBounds().contains((int) event.getX(pointerIndex), (int) event.getY(pointerIndex))) { 111 }
99 mPressedState = true; 112 mPressedState = false;
100 mOuterBitmap.setAlpha(0); 113 mXAxis = 0.0f;
101 mBoundsBoxBitmap.setAlpha(255); 114 mYAxis = 0.0f;
102 if (EmulationMenuSettings.getJoystickRelCenter()) { 115 mOuterBitmap.setAlpha(255);
103 getVirtBounds().offset((int) event.getX(pointerIndex) - getVirtBounds().centerX(), 116 mBoundsBoxBitmap.setAlpha(0);
104 (int) event.getY(pointerIndex) - getVirtBounds().centerY()); 117 setVirtBounds(new Rect(mOrigBounds.left, mOrigBounds.top, mOrigBounds.right,
105 } 118 mOrigBounds.bottom));
106 mBoundsBoxBitmap.setBounds(getVirtBounds()); 119 setBounds(new Rect(mOrigBounds.left, mOrigBounds.top, mOrigBounds.right,
107 trackId = event.getPointerId(pointerIndex); 120 mOrigBounds.bottom));
108 } 121 SetInnerBounds();
109 break; 122 mTrackId = -1;
110 case MotionEvent.ACTION_UP: 123 return true;
111 case MotionEvent.ACTION_POINTER_UP:
112 if (trackId == event.getPointerId(pointerIndex)) {
113 mPressedState = false;
114 axises[0] = axises[1] = 0.0f;
115 mOuterBitmap.setAlpha(255);
116 mBoundsBoxBitmap.setAlpha(0);
117 setVirtBounds(new Rect(mOrigBounds.left, mOrigBounds.top, mOrigBounds.right,
118 mOrigBounds.bottom));
119 setBounds(new Rect(mOrigBounds.left, mOrigBounds.top, mOrigBounds.right,
120 mOrigBounds.bottom));
121 SetInnerBounds();
122 trackId = -1;
123 }
124 break;
125 } 124 }
126 125
127 if (trackId == -1) 126 if (mTrackId == -1)
128 return; 127 return false;
129 128
130 for (int i = 0; i < event.getPointerCount(); i++) { 129 for (int i = 0; i < event.getPointerCount(); i++) {
131 if (trackId == event.getPointerId(i)) { 130 if (mTrackId != event.getPointerId(i)) {
132 float touchX = event.getX(i); 131 continue;
133 float touchY = event.getY(i);
134 float maxY = getVirtBounds().bottom;
135 float maxX = getVirtBounds().right;
136 touchX -= getVirtBounds().centerX();
137 maxX -= getVirtBounds().centerX();
138 touchY -= getVirtBounds().centerY();
139 maxY -= getVirtBounds().centerY();
140 final float AxisX = touchX / maxX;
141 final float AxisY = touchY / maxY;
142
143 // Clamp the circle pad input to a circle
144 final float angle = (float) Math.atan2(AxisY, AxisX);
145 float radius = (float) Math.sqrt(AxisX * AxisX + AxisY * AxisY);
146 if (radius > 1.0f) {
147 radius = 1.0f;
148 }
149 axises[0] = ((float) Math.cos(angle) * radius);
150 axises[1] = ((float) Math.sin(angle) * radius);
151 SetInnerBounds();
152 } 132 }
133 float touchX = event.getX(i);
134 float touchY = event.getY(i);
135 float maxY = getVirtBounds().bottom;
136 float maxX = getVirtBounds().right;
137 touchX -= getVirtBounds().centerX();
138 maxX -= getVirtBounds().centerX();
139 touchY -= getVirtBounds().centerY();
140 maxY -= getVirtBounds().centerY();
141 final float AxisX = touchX / maxX;
142 final float AxisY = touchY / maxY;
143
144 // Clamp the circle pad input to a circle
145 final float angle = (float) Math.atan2(AxisY, AxisX);
146 float radius = (float) Math.sqrt(AxisX * AxisX + AxisY * AxisY);
147 if (radius > 1.0f) {
148 radius = 1.0f;
149 }
150 mXAxis = ((float) Math.cos(angle) * radius);
151 mYAxis = ((float) Math.sin(angle) * radius);
152 SetInnerBounds();
153 return true;
153 } 154 }
154 }
155
156 public boolean onConfigureTouch(MotionEvent event) {
157 int pointerIndex = event.getActionIndex();
158 int fingerPositionX = (int) event.getX(pointerIndex);
159 int fingerPositionY = (int) event.getY(pointerIndex);
160
161 int scale = 1;
162
163 switch (event.getAction()) {
164 case MotionEvent.ACTION_DOWN:
165 mPreviousTouchX = fingerPositionX;
166 mPreviousTouchY = fingerPositionY;
167 break;
168 case MotionEvent.ACTION_MOVE:
169 int deltaX = fingerPositionX - mPreviousTouchX;
170 int deltaY = fingerPositionY - mPreviousTouchY;
171 mControlPositionX += deltaX;
172 mControlPositionY += deltaY;
173 setBounds(new Rect(mControlPositionX, mControlPositionY,
174 mOuterBitmap.getIntrinsicWidth() / scale + mControlPositionX,
175 mOuterBitmap.getIntrinsicHeight() / scale + mControlPositionY));
176 setVirtBounds(new Rect(mControlPositionX, mControlPositionY,
177 mOuterBitmap.getIntrinsicWidth() / scale + mControlPositionX,
178 mOuterBitmap.getIntrinsicHeight() / scale + mControlPositionY));
179 SetInnerBounds();
180 setOrigBounds(new Rect(new Rect(mControlPositionX, mControlPositionY,
181 mOuterBitmap.getIntrinsicWidth() / scale + mControlPositionX,
182 mOuterBitmap.getIntrinsicHeight() / scale + mControlPositionY)));
183 mPreviousTouchX = fingerPositionX;
184 mPreviousTouchY = fingerPositionY;
185 break;
186 }
187 return true;
188 }
189
190
191 public float[] getAxisValues() {
192 return axises;
193 }
194 155
195 public int[] getAxisIDs() { 156 return false;
196 return axisIDs;
197 } 157 }
198 158
199 private void SetInnerBounds() { 159 private void SetInnerBounds() {
200 int X = getVirtBounds().centerX() + (int) ((axises[0]) * (getVirtBounds().width() / 2)); 160 int X = getVirtBounds().centerX() + (int) ((mXAxis) * (getVirtBounds().width() / 2));
201 int Y = getVirtBounds().centerY() + (int) ((axises[1]) * (getVirtBounds().height() / 2)); 161 int Y = getVirtBounds().centerY() + (int) ((mYAxis) * (getVirtBounds().height() / 2));
202 162
203 if (X > getVirtBounds().centerX() + (getVirtBounds().width() / 2)) 163 if (X > getVirtBounds().centerX() + (getVirtBounds().width() / 2))
204 X = getVirtBounds().centerX() + (getVirtBounds().width() / 2); 164 X = getVirtBounds().centerX() + (getVirtBounds().width() / 2);
@@ -224,6 +184,37 @@ public final class InputOverlayDrawableJoystick {
224 return mPressedState ? mPressedStateInnerBitmap : mDefaultStateInnerBitmap; 184 return mPressedState ? mPressedStateInnerBitmap : mDefaultStateInnerBitmap;
225 } 185 }
226 186
187 /**
188 * Gets this InputOverlayDrawableJoystick's button ID.
189 *
190 * @return this InputOverlayDrawableJoystick's button ID.
191 */
192 public int getJoystickId() {
193 return mJoystickId;
194 }
195
196 public float getXAxis() {
197 return mXAxis;
198 }
199
200 public float getYAxis() {
201 // Nintendo joysticks have y axis inverted
202 return -mYAxis;
203 }
204
205 public int getButtonId() {
206 return mButtonId;
207 }
208
209 public int getTrackId() {
210 return mTrackId;
211 }
212
213 public int getButtonStatus() {
214 // TODO: Add button support
215 return NativeLibrary.ButtonState.RELEASED;
216 }
217
227 public Rect getBounds() { 218 public Rect getBounds() {
228 return mOuterBitmap.getBounds(); 219 return mOuterBitmap.getBounds();
229 } 220 }
@@ -232,10 +223,6 @@ public final class InputOverlayDrawableJoystick {
232 mOuterBitmap.setBounds(bounds); 223 mOuterBitmap.setBounds(bounds);
233 } 224 }
234 225
235 private void setOrigBounds(Rect bounds) {
236 mOrigBounds = bounds;
237 }
238
239 private Rect getVirtBounds() { 226 private Rect getVirtBounds() {
240 return mVirtBounds; 227 return mVirtBounds;
241 } 228 }