summaryrefslogtreecommitdiff
path: root/src/android
diff options
context:
space:
mode:
authorGravatar Charles Lombardo2023-03-08 20:49:24 -0500
committerGravatar bunnei2023-06-03 00:05:39 -0700
commitd85678a80fe75b2b8c85d37b8ccd52972696fe9b (patch)
treead485cf03ecab5abb2210b479c7ba7d41545a0c3 /src/android
parentandroid: Convert InputOverlayDrawableButton to Kotlin (diff)
downloadyuzu-d85678a80fe75b2b8c85d37b8ccd52972696fe9b.tar.gz
yuzu-d85678a80fe75b2b8c85d37b8ccd52972696fe9b.tar.xz
yuzu-d85678a80fe75b2b8c85d37b8ccd52972696fe9b.zip
android: Convert InputOverlayDrawableDpad to Kotlin
Diffstat (limited to 'src/android')
-rw-r--r--src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableDpad.java276
-rw-r--r--src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableDpad.kt232
2 files changed, 232 insertions, 276 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableDpad.java b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableDpad.java
deleted file mode 100644
index 1d10b1e65..000000000
--- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableDpad.java
+++ /dev/null
@@ -1,276 +0,0 @@
1/**
2 * Copyright 2016 Dolphin Emulator Project
3 * Licensed under GPLv2+
4 * Refer to the license.txt file included.
5 */
6
7package org.yuzu.yuzu_emu.overlay;
8
9import android.content.res.Resources;
10import android.graphics.Bitmap;
11import android.graphics.Canvas;
12import android.graphics.Rect;
13import android.graphics.drawable.BitmapDrawable;
14import android.view.MotionEvent;
15
16import org.yuzu.yuzu_emu.NativeLibrary.ButtonState;
17
18/**
19 * Custom {@link BitmapDrawable} that is capable
20 * of storing it's own ID.
21 */
22public final class InputOverlayDrawableDpad {
23 public static final float VIRT_AXIS_DEADZONE = 0.5f;
24 // The ID identifying what type of button this Drawable represents.
25 private int mUpButtonId;
26 private int mDownButtonId;
27 private int mLeftButtonId;
28 private int mRightButtonId;
29 private int mTrackId;
30 private int mControlPositionX, mControlPositionY;
31 private int mWidth;
32 private int mHeight;
33 private BitmapDrawable mDefaultStateBitmap;
34 private BitmapDrawable mPressedOneDirectionStateBitmap;
35 private BitmapDrawable mPressedTwoDirectionsStateBitmap;
36 private boolean mUpButtonState;
37 private boolean mDownButtonState;
38 private boolean mLeftButtonState;
39 private boolean mRightButtonState;
40
41 /**
42 * Constructor
43 *
44 * @param res {@link Resources} instance.
45 * @param defaultStateBitmap {@link Bitmap} of the default state.
46 * @param pressedOneDirectionStateBitmap {@link Bitmap} of the pressed state in one direction.
47 * @param pressedTwoDirectionsStateBitmap {@link Bitmap} of the pressed state in two direction.
48 * @param buttonUp Identifier for the up button.
49 * @param buttonDown Identifier for the down button.
50 * @param buttonLeft Identifier for the left button.
51 * @param buttonRight Identifier for the right button.
52 */
53 public InputOverlayDrawableDpad(Resources res,
54 Bitmap defaultStateBitmap,
55 Bitmap pressedOneDirectionStateBitmap,
56 Bitmap pressedTwoDirectionsStateBitmap,
57 int buttonUp, int buttonDown,
58 int buttonLeft, int buttonRight) {
59 mDefaultStateBitmap = new BitmapDrawable(res, defaultStateBitmap);
60 mPressedOneDirectionStateBitmap = new BitmapDrawable(res, pressedOneDirectionStateBitmap);
61 mPressedTwoDirectionsStateBitmap = new BitmapDrawable(res, pressedTwoDirectionsStateBitmap);
62
63 mWidth = mDefaultStateBitmap.getIntrinsicWidth();
64 mHeight = mDefaultStateBitmap.getIntrinsicHeight();
65
66 mUpButtonId = buttonUp;
67 mDownButtonId = buttonDown;
68 mLeftButtonId = buttonLeft;
69 mRightButtonId = buttonRight;
70
71 mTrackId = -1;
72 }
73
74 public boolean updateStatus(MotionEvent event, boolean dpad_slide) {
75 int pointerIndex = event.getActionIndex();
76 int xPosition = (int) event.getX(pointerIndex);
77 int yPosition = (int) event.getY(pointerIndex);
78 int pointerId = event.getPointerId(pointerIndex);
79 int motion_event = event.getAction() & MotionEvent.ACTION_MASK;
80 boolean isActionDown = motion_event == MotionEvent.ACTION_DOWN || motion_event == MotionEvent.ACTION_POINTER_DOWN;
81 boolean isActionUp = motion_event == MotionEvent.ACTION_UP || motion_event == MotionEvent.ACTION_POINTER_UP;
82
83 if (isActionDown) {
84 if (!getBounds().contains(xPosition, yPosition)) {
85 return false;
86 }
87 mTrackId = pointerId;
88 }
89
90 if (isActionUp) {
91 if (mTrackId != pointerId) {
92 return false;
93 }
94 mTrackId = -1;
95 mUpButtonState = false;
96 mDownButtonState = false;
97 mLeftButtonState = false;
98 mRightButtonState = false;
99 return true;
100 }
101
102 if (mTrackId == -1) {
103 return false;
104 }
105
106 if (!dpad_slide && !isActionDown) {
107 return false;
108 }
109
110 for (int i = 0; i < event.getPointerCount(); i++) {
111 if (mTrackId != event.getPointerId(i)) {
112 continue;
113 }
114 float touchX = event.getX(i);
115 float touchY = event.getY(i);
116 float maxY = getBounds().bottom;
117 float maxX = getBounds().right;
118 touchX -= getBounds().centerX();
119 maxX -= getBounds().centerX();
120 touchY -= getBounds().centerY();
121 maxY -= getBounds().centerY();
122 final float AxisX = touchX / maxX;
123 final float AxisY = touchY / maxY;
124 final boolean up_state = mUpButtonState;
125 final boolean down_state = mDownButtonState;
126 final boolean left_state = mLeftButtonState;
127 final boolean right_state = mRightButtonState;
128
129 mUpButtonState = AxisY < -InputOverlayDrawableDpad.VIRT_AXIS_DEADZONE;
130 mDownButtonState = AxisY > InputOverlayDrawableDpad.VIRT_AXIS_DEADZONE;
131 mLeftButtonState = AxisX < -InputOverlayDrawableDpad.VIRT_AXIS_DEADZONE;
132 mRightButtonState = AxisX > InputOverlayDrawableDpad.VIRT_AXIS_DEADZONE;
133 return up_state != mUpButtonState || down_state != mDownButtonState || left_state != mLeftButtonState || right_state != mRightButtonState;
134 }
135
136 return false;
137 }
138
139 public void draw(Canvas canvas) {
140 int px = mControlPositionX + (getWidth() / 2);
141 int py = mControlPositionY + (getHeight() / 2);
142
143 // Pressed up
144 if (mUpButtonState && !mLeftButtonState && !mRightButtonState) {
145 mPressedOneDirectionStateBitmap.draw(canvas);
146 return;
147 }
148
149 // Pressed down
150 if (mDownButtonState && !mLeftButtonState && !mRightButtonState) {
151 canvas.save();
152 canvas.rotate(180, px, py);
153 mPressedOneDirectionStateBitmap.draw(canvas);
154 canvas.restore();
155 return;
156 }
157
158 // Pressed left
159 if (mLeftButtonState && !mUpButtonState && !mDownButtonState) {
160 canvas.save();
161 canvas.rotate(270, px, py);
162 mPressedOneDirectionStateBitmap.draw(canvas);
163 canvas.restore();
164 return;
165 }
166
167 // Pressed right
168 if (mRightButtonState && !mUpButtonState && !mDownButtonState) {
169 canvas.save();
170 canvas.rotate(90, px, py);
171 mPressedOneDirectionStateBitmap.draw(canvas);
172 canvas.restore();
173 return;
174 }
175
176 // Pressed up left
177 if (mUpButtonState && mLeftButtonState && !mRightButtonState) {
178 mPressedTwoDirectionsStateBitmap.draw(canvas);
179 return;
180 }
181
182 // Pressed up right
183 if (mUpButtonState && !mLeftButtonState && mRightButtonState) {
184 canvas.save();
185 canvas.rotate(90, px, py);
186 mPressedTwoDirectionsStateBitmap.draw(canvas);
187 canvas.restore();
188 return;
189 }
190
191 // Pressed down left
192 if (mDownButtonState && mLeftButtonState && !mRightButtonState) {
193 canvas.save();
194 canvas.rotate(270, px, py);
195 mPressedTwoDirectionsStateBitmap.draw(canvas);
196 canvas.restore();
197 return;
198 }
199
200 // Pressed down right
201 if (mDownButtonState && !mLeftButtonState && mRightButtonState) {
202 canvas.save();
203 canvas.rotate(180, px, py);
204 mPressedTwoDirectionsStateBitmap.draw(canvas);
205 canvas.restore();
206 return;
207 }
208
209 // Not pressed
210 mDefaultStateBitmap.draw(canvas);
211 }
212
213 /**
214 * Gets one of the InputOverlayDrawableDpad's button IDs.
215 *
216 * @return the requested InputOverlayDrawableDpad's button ID.
217 */
218 public int getUpId() {
219 return mUpButtonId;
220 }
221
222 public int getDownId() {
223 return mDownButtonId;
224 }
225
226 public int getLeftId() {
227 return mLeftButtonId;
228 }
229
230 public int getRightId() {
231 return mRightButtonId;
232 }
233
234 public int getTrackId() {
235 return mTrackId;
236 }
237
238 public int getUpStatus() {
239 return mUpButtonState ? ButtonState.PRESSED : ButtonState.RELEASED;
240 }
241
242 public int getDownStatus() {
243 return mDownButtonState ? ButtonState.PRESSED : ButtonState.RELEASED;
244 }
245
246 public int getLeftStatus() {
247 return mLeftButtonState ? ButtonState.PRESSED : ButtonState.RELEASED;
248 }
249
250 public int getRightStatus() {
251 return mRightButtonState ? ButtonState.PRESSED : ButtonState.RELEASED;
252 }
253
254 public void setPosition(int x, int y) {
255 mControlPositionX = x;
256 mControlPositionY = y;
257 }
258
259 public void setBounds(int left, int top, int right, int bottom) {
260 mDefaultStateBitmap.setBounds(left, top, right, bottom);
261 mPressedOneDirectionStateBitmap.setBounds(left, top, right, bottom);
262 mPressedTwoDirectionsStateBitmap.setBounds(left, top, right, bottom);
263 }
264
265 public Rect getBounds() {
266 return mDefaultStateBitmap.getBounds();
267 }
268
269 public int getWidth() {
270 return mWidth;
271 }
272
273 public int getHeight() {
274 return mHeight;
275 }
276}
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableDpad.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableDpad.kt
new file mode 100644
index 000000000..55e99d465
--- /dev/null
+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableDpad.kt
@@ -0,0 +1,232 @@
1package org.yuzu.yuzu_emu.overlay
2
3import android.content.res.Resources
4import android.graphics.Bitmap
5import android.graphics.Canvas
6import android.graphics.Rect
7import android.graphics.drawable.BitmapDrawable
8import android.view.MotionEvent
9import org.yuzu.yuzu_emu.NativeLibrary.ButtonState
10
11/**
12 * Custom [BitmapDrawable] that is capable
13 * of storing it's own ID.
14 *
15 * @param res [Resources] instance.
16 * @param defaultStateBitmap [Bitmap] of the default state.
17 * @param pressedOneDirectionStateBitmap [Bitmap] of the pressed state in one direction.
18 * @param pressedTwoDirectionsStateBitmap [Bitmap] of the pressed state in two direction.
19 * @param buttonUp Identifier for the up button.
20 * @param buttonDown Identifier for the down button.
21 * @param buttonLeft Identifier for the left button.
22 * @param buttonRight Identifier for the right button.
23 */
24class InputOverlayDrawableDpad(
25 res: Resources,
26 defaultStateBitmap: Bitmap,
27 pressedOneDirectionStateBitmap: Bitmap,
28 pressedTwoDirectionsStateBitmap: Bitmap,
29 buttonUp: Int,
30 buttonDown: Int,
31 buttonLeft: Int,
32 buttonRight: Int
33) {
34 /**
35 * Gets one of the InputOverlayDrawableDpad's button IDs.
36 *
37 * @return the requested InputOverlayDrawableDpad's button ID.
38 */
39 // The ID identifying what type of button this Drawable represents.
40 val upId: Int
41 val downId: Int
42 val leftId: Int
43 val rightId: Int
44 var trackId: Int
45 private var controlPositionX = 0
46 private var controlPositionY = 0
47 val width: Int
48 val height: Int
49 private val defaultStateBitmap: BitmapDrawable
50 private val pressedOneDirectionStateBitmap: BitmapDrawable
51 private val pressedTwoDirectionsStateBitmap: BitmapDrawable
52 private var upButtonState = false
53 private var downButtonState = false
54 private var leftButtonState = false
55 private var rightButtonState = false
56
57 init {
58 this.defaultStateBitmap = BitmapDrawable(res, defaultStateBitmap)
59 this.pressedOneDirectionStateBitmap = BitmapDrawable(res, pressedOneDirectionStateBitmap)
60 this.pressedTwoDirectionsStateBitmap = BitmapDrawable(res, pressedTwoDirectionsStateBitmap)
61 width = this.defaultStateBitmap.intrinsicWidth
62 height = this.defaultStateBitmap.intrinsicHeight
63 upId = buttonUp
64 downId = buttonDown
65 leftId = buttonLeft
66 rightId = buttonRight
67 trackId = -1
68 }
69
70 fun updateStatus(event: MotionEvent, dpad_slide: Boolean): Boolean {
71 val pointerIndex = event.actionIndex
72 val xPosition = event.getX(pointerIndex).toInt()
73 val yPosition = event.getY(pointerIndex).toInt()
74 val pointerId = event.getPointerId(pointerIndex)
75 val motionEvent = event.action and MotionEvent.ACTION_MASK
76 val isActionDown =
77 motionEvent == MotionEvent.ACTION_DOWN || motionEvent == MotionEvent.ACTION_POINTER_DOWN
78 val isActionUp =
79 motionEvent == MotionEvent.ACTION_UP || motionEvent == MotionEvent.ACTION_POINTER_UP
80 if (isActionDown) {
81 if (!bounds.contains(xPosition, yPosition)) {
82 return false
83 }
84 trackId = pointerId
85 }
86 if (isActionUp) {
87 if (trackId != pointerId) {
88 return false
89 }
90 trackId = -1
91 upButtonState = false
92 downButtonState = false
93 leftButtonState = false
94 rightButtonState = false
95 return true
96 }
97 if (trackId == -1) {
98 return false
99 }
100 if (!dpad_slide && !isActionDown) {
101 return false
102 }
103 for (i in 0 until event.pointerCount) {
104 if (trackId != event.getPointerId(i)) {
105 continue
106 }
107
108 var touchX = event.getX(i)
109 var touchY = event.getY(i)
110 var maxY = bounds.bottom.toFloat()
111 var maxX = bounds.right.toFloat()
112 touchX -= bounds.centerX().toFloat()
113 maxX -= bounds.centerX().toFloat()
114 touchY -= bounds.centerY().toFloat()
115 maxY -= bounds.centerY().toFloat()
116 val axisX = touchX / maxX
117 val axisY = touchY / maxY
118 val oldUpState = upButtonState
119 val oldDownState = downButtonState
120 val oldLeftState = leftButtonState
121 val oldRightState = rightButtonState
122
123 upButtonState = axisY < -VIRT_AXIS_DEADZONE
124 downButtonState = axisY > VIRT_AXIS_DEADZONE
125 leftButtonState = axisX < -VIRT_AXIS_DEADZONE
126 rightButtonState = axisX > VIRT_AXIS_DEADZONE
127 return oldUpState != upButtonState || oldDownState != downButtonState || oldLeftState != leftButtonState || oldRightState != rightButtonState
128 }
129 return false
130 }
131
132 fun draw(canvas: Canvas) {
133 val px = controlPositionX + width / 2
134 val py = controlPositionY + height / 2
135
136 // Pressed up
137 if (upButtonState && !leftButtonState && !rightButtonState) {
138 pressedOneDirectionStateBitmap.draw(canvas)
139 return
140 }
141
142 // Pressed down
143 if (downButtonState && !leftButtonState && !rightButtonState) {
144 canvas.save()
145 canvas.rotate(180f, px.toFloat(), py.toFloat())
146 pressedOneDirectionStateBitmap.draw(canvas)
147 canvas.restore()
148 return
149 }
150
151 // Pressed left
152 if (leftButtonState && !upButtonState && !downButtonState) {
153 canvas.save()
154 canvas.rotate(270f, px.toFloat(), py.toFloat())
155 pressedOneDirectionStateBitmap.draw(canvas)
156 canvas.restore()
157 return
158 }
159
160 // Pressed right
161 if (rightButtonState && !upButtonState && !downButtonState) {
162 canvas.save()
163 canvas.rotate(90f, px.toFloat(), py.toFloat())
164 pressedOneDirectionStateBitmap.draw(canvas)
165 canvas.restore()
166 return
167 }
168
169 // Pressed up left
170 if (upButtonState && leftButtonState && !rightButtonState) {
171 pressedTwoDirectionsStateBitmap.draw(canvas)
172 return
173 }
174
175 // Pressed up right
176 if (upButtonState && !leftButtonState && rightButtonState) {
177 canvas.save()
178 canvas.rotate(90f, px.toFloat(), py.toFloat())
179 pressedTwoDirectionsStateBitmap.draw(canvas)
180 canvas.restore()
181 return
182 }
183
184 // Pressed down right
185 if (downButtonState && !leftButtonState && rightButtonState) {
186 canvas.save()
187 canvas.rotate(180f, px.toFloat(), py.toFloat())
188 pressedTwoDirectionsStateBitmap.draw(canvas)
189 canvas.restore()
190 return
191 }
192
193 // Pressed down left
194 if (downButtonState && leftButtonState && !rightButtonState) {
195 canvas.save()
196 canvas.rotate(270f, px.toFloat(), py.toFloat())
197 pressedTwoDirectionsStateBitmap.draw(canvas)
198 canvas.restore()
199 return
200 }
201
202 // Not pressed
203 defaultStateBitmap.draw(canvas)
204 }
205
206 val upStatus: Int
207 get() = if (upButtonState) ButtonState.PRESSED else ButtonState.RELEASED
208 val downStatus: Int
209 get() = if (downButtonState) ButtonState.PRESSED else ButtonState.RELEASED
210 val leftStatus: Int
211 get() = if (leftButtonState) ButtonState.PRESSED else ButtonState.RELEASED
212 val rightStatus: Int
213 get() = if (rightButtonState) ButtonState.PRESSED else ButtonState.RELEASED
214
215 fun setPosition(x: Int, y: Int) {
216 controlPositionX = x
217 controlPositionY = y
218 }
219
220 fun setBounds(left: Int, top: Int, right: Int, bottom: Int) {
221 defaultStateBitmap.setBounds(left, top, right, bottom)
222 pressedOneDirectionStateBitmap.setBounds(left, top, right, bottom)
223 pressedTwoDirectionsStateBitmap.setBounds(left, top, right, bottom)
224 }
225
226 val bounds: Rect
227 get() = defaultStateBitmap.bounds
228
229 companion object {
230 const val VIRT_AXIS_DEADZONE = 0.5f
231 }
232}