diff options
| author | 2023-03-08 20:48:55 -0500 | |
|---|---|---|
| committer | 2023-06-03 00:05:39 -0700 | |
| commit | 0177e908e921a30b352868e2af8c86e37d16cc7c (patch) | |
| tree | e6e65f1eadfcb4c26e4619b564978ccdef8bb733 /src/android | |
| parent | android: Convert InputOverlay to Kotlin (diff) | |
| download | yuzu-0177e908e921a30b352868e2af8c86e37d16cc7c.tar.gz yuzu-0177e908e921a30b352868e2af8c86e37d16cc7c.tar.xz yuzu-0177e908e921a30b352868e2af8c86e37d16cc7c.zip | |
android: Convert InputOverlayDrawableButton to Kotlin
Diffstat (limited to 'src/android')
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableButton.java | 139 | ||||
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableButton.kt | 118 |
2 files changed, 118 insertions, 139 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableButton.java b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableButton.java deleted file mode 100644 index 16a4b96ad..000000000 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableButton.java +++ /dev/null | |||
| @@ -1,139 +0,0 @@ | |||
| 1 | /** | ||
| 2 | * Copyright 2013 Dolphin Emulator Project | ||
| 3 | * Licensed under GPLv2+ | ||
| 4 | * Refer to the license.txt file included. | ||
| 5 | */ | ||
| 6 | |||
| 7 | package org.yuzu.yuzu_emu.overlay; | ||
| 8 | |||
| 9 | import android.content.res.Resources; | ||
| 10 | import android.graphics.Bitmap; | ||
| 11 | import android.graphics.Canvas; | ||
| 12 | import android.graphics.Rect; | ||
| 13 | import android.graphics.drawable.BitmapDrawable; | ||
| 14 | import android.view.MotionEvent; | ||
| 15 | |||
| 16 | import org.yuzu.yuzu_emu.NativeLibrary.ButtonState; | ||
| 17 | |||
| 18 | |||
| 19 | /** | ||
| 20 | * Custom {@link BitmapDrawable} that is capable | ||
| 21 | * of storing it's own ID. | ||
| 22 | */ | ||
| 23 | public final class InputOverlayDrawableButton { | ||
| 24 | // The ID value what type of button this Drawable represents. | ||
| 25 | private int mButtonId; | ||
| 26 | |||
| 27 | // The ID value what motion event is tracking | ||
| 28 | private int mTrackId; | ||
| 29 | |||
| 30 | // The drawable position on the screen | ||
| 31 | private int mButtonPositionX, mButtonPositionY; | ||
| 32 | private int mWidth; | ||
| 33 | private int mHeight; | ||
| 34 | private BitmapDrawable mDefaultStateBitmap; | ||
| 35 | private BitmapDrawable mPressedStateBitmap; | ||
| 36 | private boolean mPressedState = false; | ||
| 37 | |||
| 38 | /** | ||
| 39 | * Constructor | ||
| 40 | * | ||
| 41 | * @param res {@link Resources} instance. | ||
| 42 | * @param defaultStateBitmap {@link Bitmap} to use with the default state Drawable. | ||
| 43 | * @param pressedStateBitmap {@link Bitmap} to use with the pressed state Drawable. | ||
| 44 | * @param buttonId Identifier for this type of button. | ||
| 45 | */ | ||
| 46 | public InputOverlayDrawableButton(Resources res, Bitmap defaultStateBitmap, | ||
| 47 | Bitmap pressedStateBitmap, int buttonId) { | ||
| 48 | mDefaultStateBitmap = new BitmapDrawable(res, defaultStateBitmap); | ||
| 49 | mPressedStateBitmap = new BitmapDrawable(res, pressedStateBitmap); | ||
| 50 | mButtonId = buttonId; | ||
| 51 | mTrackId = -1; | ||
| 52 | |||
| 53 | mWidth = mDefaultStateBitmap.getIntrinsicWidth(); | ||
| 54 | mHeight = mDefaultStateBitmap.getIntrinsicHeight(); | ||
| 55 | } | ||
| 56 | |||
| 57 | /** | ||
| 58 | * Updates button status based on the motion event. | ||
| 59 | * | ||
| 60 | * @return true if value was changed | ||
| 61 | */ | ||
| 62 | public boolean updateStatus(MotionEvent event) { | ||
| 63 | int pointerIndex = event.getActionIndex(); | ||
| 64 | int xPosition = (int) event.getX(pointerIndex); | ||
| 65 | int yPosition = (int) event.getY(pointerIndex); | ||
| 66 | int pointerId = event.getPointerId(pointerIndex); | ||
| 67 | int motion_event = event.getAction() & MotionEvent.ACTION_MASK; | ||
| 68 | boolean isActionDown = motion_event == MotionEvent.ACTION_DOWN || motion_event == MotionEvent.ACTION_POINTER_DOWN; | ||
| 69 | boolean isActionUp = motion_event == MotionEvent.ACTION_UP || motion_event == MotionEvent.ACTION_POINTER_UP; | ||
| 70 | boolean current_state = mPressedState; | ||
| 71 | |||
| 72 | if (isActionDown) { | ||
| 73 | if (!getBounds().contains(xPosition, yPosition)) { | ||
| 74 | return false; | ||
| 75 | } | ||
| 76 | mPressedState = true; | ||
| 77 | mTrackId = pointerId; | ||
| 78 | return true; | ||
| 79 | } | ||
| 80 | |||
| 81 | if (isActionUp) { | ||
| 82 | if (mTrackId != pointerId) { | ||
| 83 | return false; | ||
| 84 | } | ||
| 85 | mPressedState = false; | ||
| 86 | mTrackId = -1; | ||
| 87 | return true; | ||
| 88 | } | ||
| 89 | |||
| 90 | return false; | ||
| 91 | } | ||
| 92 | |||
| 93 | public void setPosition(int x, int y) { | ||
| 94 | mButtonPositionX = x; | ||
| 95 | mButtonPositionY = y; | ||
| 96 | } | ||
| 97 | |||
| 98 | public void draw(Canvas canvas) { | ||
| 99 | getCurrentStateBitmapDrawable().draw(canvas); | ||
| 100 | } | ||
| 101 | |||
| 102 | private BitmapDrawable getCurrentStateBitmapDrawable() { | ||
| 103 | return mPressedState ? mPressedStateBitmap : mDefaultStateBitmap; | ||
| 104 | } | ||
| 105 | |||
| 106 | public void setBounds(int left, int top, int right, int bottom) { | ||
| 107 | mDefaultStateBitmap.setBounds(left, top, right, bottom); | ||
| 108 | mPressedStateBitmap.setBounds(left, top, right, bottom); | ||
| 109 | } | ||
| 110 | |||
| 111 | /** | ||
| 112 | * Gets this InputOverlayDrawableButton's button ID. | ||
| 113 | * | ||
| 114 | * @return this InputOverlayDrawableButton's button ID. | ||
| 115 | */ | ||
| 116 | public int getId() { | ||
| 117 | return mButtonId; | ||
| 118 | } | ||
| 119 | |||
| 120 | public int getTrackId() { | ||
| 121 | return mTrackId; | ||
| 122 | } | ||
| 123 | |||
| 124 | public int getStatus() { | ||
| 125 | return mPressedState ? ButtonState.PRESSED : ButtonState.RELEASED; | ||
| 126 | } | ||
| 127 | |||
| 128 | private Rect getBounds() { | ||
| 129 | return mDefaultStateBitmap.getBounds(); | ||
| 130 | } | ||
| 131 | |||
| 132 | public int getWidth() { | ||
| 133 | return mWidth; | ||
| 134 | } | ||
| 135 | |||
| 136 | public int getHeight() { | ||
| 137 | return mHeight; | ||
| 138 | } | ||
| 139 | } | ||
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableButton.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableButton.kt new file mode 100644 index 000000000..819d9d6b2 --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableButton.kt | |||
| @@ -0,0 +1,118 @@ | |||
| 1 | /** | ||
| 2 | * Copyright 2013 Dolphin Emulator Project | ||
| 3 | * Licensed under GPLv2+ | ||
| 4 | * Refer to the license.txt file included. | ||
| 5 | */ | ||
| 6 | package org.yuzu.yuzu_emu.overlay | ||
| 7 | |||
| 8 | import android.content.res.Resources | ||
| 9 | import android.graphics.Bitmap | ||
| 10 | import android.graphics.Canvas | ||
| 11 | import android.graphics.Rect | ||
| 12 | import android.graphics.drawable.BitmapDrawable | ||
| 13 | import android.view.MotionEvent | ||
| 14 | import org.yuzu.yuzu_emu.NativeLibrary.ButtonState | ||
| 15 | |||
| 16 | /** | ||
| 17 | * Custom [BitmapDrawable] that is capable | ||
| 18 | * of storing it's own ID. | ||
| 19 | * | ||
| 20 | * @param res [Resources] instance. | ||
| 21 | * @param defaultStateBitmap [Bitmap] to use with the default state Drawable. | ||
| 22 | * @param pressedStateBitmap [Bitmap] to use with the pressed state Drawable. | ||
| 23 | * @param buttonId Identifier for this type of button. | ||
| 24 | */ | ||
| 25 | class InputOverlayDrawableButton( | ||
| 26 | res: Resources, | ||
| 27 | defaultStateBitmap: Bitmap, | ||
| 28 | pressedStateBitmap: Bitmap, | ||
| 29 | buttonId: Int | ||
| 30 | ) { | ||
| 31 | /** | ||
| 32 | * Gets this InputOverlayDrawableButton's button ID. | ||
| 33 | * | ||
| 34 | * @return this InputOverlayDrawableButton's button ID. | ||
| 35 | */ | ||
| 36 | // The ID value what type of button this Drawable represents. | ||
| 37 | val id: Int | ||
| 38 | |||
| 39 | // The ID value what motion event is tracking | ||
| 40 | var trackId: Int | ||
| 41 | |||
| 42 | // The drawable position on the screen | ||
| 43 | private var buttonPositionX = 0 | ||
| 44 | private var buttonPositionY = 0 | ||
| 45 | val width: Int | ||
| 46 | val height: Int | ||
| 47 | private val defaultStateBitmap: BitmapDrawable | ||
| 48 | private val pressedStateBitmap: BitmapDrawable | ||
| 49 | private var pressedState = false | ||
| 50 | |||
| 51 | init { | ||
| 52 | this.defaultStateBitmap = BitmapDrawable(res, defaultStateBitmap) | ||
| 53 | this.pressedStateBitmap = BitmapDrawable(res, pressedStateBitmap) | ||
| 54 | id = buttonId | ||
| 55 | trackId = -1 | ||
| 56 | width = this.defaultStateBitmap.intrinsicWidth | ||
| 57 | height = this.defaultStateBitmap.intrinsicHeight | ||
| 58 | } | ||
| 59 | |||
| 60 | /** | ||
| 61 | * Updates button status based on the motion event. | ||
| 62 | * | ||
| 63 | * @return true if value was changed | ||
| 64 | */ | ||
| 65 | fun updateStatus(event: MotionEvent): Boolean { | ||
| 66 | val pointerIndex = event.actionIndex | ||
| 67 | val xPosition = event.getX(pointerIndex).toInt() | ||
| 68 | val yPosition = event.getY(pointerIndex).toInt() | ||
| 69 | val pointerId = event.getPointerId(pointerIndex) | ||
| 70 | val motionEvent = event.action and MotionEvent.ACTION_MASK | ||
| 71 | val isActionDown = | ||
| 72 | motionEvent == MotionEvent.ACTION_DOWN || motionEvent == MotionEvent.ACTION_POINTER_DOWN | ||
| 73 | val isActionUp = | ||
| 74 | motionEvent == MotionEvent.ACTION_UP || motionEvent == MotionEvent.ACTION_POINTER_UP | ||
| 75 | |||
| 76 | if (isActionDown) { | ||
| 77 | if (!bounds.contains(xPosition, yPosition)) { | ||
| 78 | return false | ||
| 79 | } | ||
| 80 | pressedState = true | ||
| 81 | trackId = pointerId | ||
| 82 | return true | ||
| 83 | } | ||
| 84 | |||
| 85 | if (isActionUp) { | ||
| 86 | if (trackId != pointerId) { | ||
| 87 | return false | ||
| 88 | } | ||
| 89 | pressedState = false | ||
| 90 | trackId = -1 | ||
| 91 | return true | ||
| 92 | } | ||
| 93 | |||
| 94 | return false | ||
| 95 | } | ||
| 96 | |||
| 97 | fun setPosition(x: Int, y: Int) { | ||
| 98 | buttonPositionX = x | ||
| 99 | buttonPositionY = y | ||
| 100 | } | ||
| 101 | |||
| 102 | fun draw(canvas: Canvas?) { | ||
| 103 | currentStateBitmapDrawable.draw(canvas!!) | ||
| 104 | } | ||
| 105 | |||
| 106 | private val currentStateBitmapDrawable: BitmapDrawable | ||
| 107 | get() = if (pressedState) pressedStateBitmap else defaultStateBitmap | ||
| 108 | |||
| 109 | fun setBounds(left: Int, top: Int, right: Int, bottom: Int) { | ||
| 110 | defaultStateBitmap.setBounds(left, top, right, bottom) | ||
| 111 | pressedStateBitmap.setBounds(left, top, right, bottom) | ||
| 112 | } | ||
| 113 | |||
| 114 | val status: Int | ||
| 115 | get() = if (pressedState) ButtonState.PRESSED else ButtonState.RELEASED | ||
| 116 | private val bounds: Rect | ||
| 117 | get() = defaultStateBitmap.bounds | ||
| 118 | } | ||