From fb60b708f52c47e829673e4d0e227a9a90fc6a01 Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Sat, 3 Jun 2023 15:02:02 -0400 Subject: [PATCH] Android: Convert InputOverlayDrawableDpad to Kotlin --- .../overlay/InputOverlayDrawableDpad.java | 211 ------------------ .../overlay/InputOverlayDrawableDpad.kt | 189 ++++++++++++++++ 2 files changed, 189 insertions(+), 211 deletions(-) delete mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableDpad.java create mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableDpad.kt diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableDpad.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableDpad.java deleted file mode 100644 index 6542609ca0..0000000000 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableDpad.java +++ /dev/null @@ -1,211 +0,0 @@ -/* - * Copyright 2016 Dolphin Emulator Project - * SPDX-License-Identifier: GPL-2.0-or-later - */ - -package org.dolphinemu.dolphinemu.overlay; - -import android.content.res.Resources; -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Rect; -import android.graphics.drawable.BitmapDrawable; -import android.view.MotionEvent; - -/** - * Custom {@link BitmapDrawable} that is capable - * of storing it's own ID. - */ -public final class InputOverlayDrawableDpad -{ - // The legacy ID identifying what type of button this Drawable represents. - private int mLegacyId; - private int[] mControls = new int[4]; - private int mTrackId; - private int mPreviousTouchX, mPreviousTouchY; - private int mControlPositionX, mControlPositionY; - private int mWidth; - private int mHeight; - private BitmapDrawable mDefaultStateBitmap; - private BitmapDrawable mPressedOneDirectionStateBitmap; - private BitmapDrawable mPressedTwoDirectionsStateBitmap; - private int mPressState = STATE_DEFAULT; - - public static final int STATE_DEFAULT = 0; - public static final int STATE_PRESSED_UP = 1; - public static final int STATE_PRESSED_DOWN = 2; - public static final int STATE_PRESSED_LEFT = 3; - public static final int STATE_PRESSED_RIGHT = 4; - public static final int STATE_PRESSED_UP_LEFT = 5; - public static final int STATE_PRESSED_UP_RIGHT = 6; - public static final int STATE_PRESSED_DOWN_LEFT = 7; - public static final int STATE_PRESSED_DOWN_RIGHT = 8; - - /** - * Constructor - * - * @param res {@link Resources} instance. - * @param defaultStateBitmap {@link Bitmap} of the default state. - * @param pressedOneDirectionStateBitmap {@link Bitmap} of the pressed state in one direction. - * @param pressedTwoDirectionsStateBitmap {@link Bitmap} of the pressed state in two direction. - * @param legacyId Legacy identifier (ButtonType) for the up button. - * @param upControl Control identifier for the up button. - * @param downControl Control identifier for the down button. - * @param leftControl Control identifier for the left button. - * @param rightControl Control identifier for the right button. - */ - public InputOverlayDrawableDpad(Resources res, Bitmap defaultStateBitmap, - Bitmap pressedOneDirectionStateBitmap, Bitmap pressedTwoDirectionsStateBitmap, - int legacyId, int upControl, int downControl, int leftControl, int rightControl) - { - mTrackId = -1; - mDefaultStateBitmap = new BitmapDrawable(res, defaultStateBitmap); - mPressedOneDirectionStateBitmap = new BitmapDrawable(res, pressedOneDirectionStateBitmap); - mPressedTwoDirectionsStateBitmap = new BitmapDrawable(res, pressedTwoDirectionsStateBitmap); - - mWidth = mDefaultStateBitmap.getIntrinsicWidth(); - mHeight = mDefaultStateBitmap.getIntrinsicHeight(); - - mLegacyId = legacyId; - mControls[0] = upControl; - mControls[1] = downControl; - mControls[2] = leftControl; - mControls[3] = rightControl; - } - - public void draw(Canvas canvas) - { - int px = mControlPositionX + (getWidth() / 2); - int py = mControlPositionY + (getHeight() / 2); - switch (mPressState) - { - case STATE_DEFAULT: - mDefaultStateBitmap.draw(canvas); - break; - case STATE_PRESSED_UP: - mPressedOneDirectionStateBitmap.draw(canvas); - break; - case STATE_PRESSED_RIGHT: - canvas.save(); - canvas.rotate(90, px, py); - mPressedOneDirectionStateBitmap.draw(canvas); - canvas.restore(); - break; - case STATE_PRESSED_DOWN: - canvas.save(); - canvas.rotate(180, px, py); - mPressedOneDirectionStateBitmap.draw(canvas); - canvas.restore(); - break; - case STATE_PRESSED_LEFT: - canvas.save(); - canvas.rotate(270, px, py); - mPressedOneDirectionStateBitmap.draw(canvas); - canvas.restore(); - break; - case STATE_PRESSED_UP_LEFT: - mPressedTwoDirectionsStateBitmap.draw(canvas); - break; - case STATE_PRESSED_UP_RIGHT: - canvas.save(); - canvas.rotate(90, px, py); - mPressedTwoDirectionsStateBitmap.draw(canvas); - canvas.restore(); - break; - case STATE_PRESSED_DOWN_RIGHT: - canvas.save(); - canvas.rotate(180, px, py); - mPressedTwoDirectionsStateBitmap.draw(canvas); - canvas.restore(); - break; - case STATE_PRESSED_DOWN_LEFT: - canvas.save(); - canvas.rotate(270, px, py); - mPressedTwoDirectionsStateBitmap.draw(canvas); - canvas.restore(); - break; - } - } - - public int getLegacyId() - { - return mLegacyId; - } - - /** - * Gets one of the InputOverlayDrawableDpad's control IDs. - */ - public int getControl(int direction) - { - return mControls[direction]; - } - - public void setTrackId(int trackId) - { - mTrackId = trackId; - } - - public int getTrackId() - { - return mTrackId; - } - - public void onConfigureTouch(MotionEvent event) - { - switch (event.getAction()) - { - case MotionEvent.ACTION_DOWN: - mPreviousTouchX = (int) event.getX(); - mPreviousTouchY = (int) event.getY(); - break; - case MotionEvent.ACTION_MOVE: - mControlPositionX += (int) event.getX() - mPreviousTouchX; - mControlPositionY += (int) event.getY() - mPreviousTouchY; - setBounds(mControlPositionX, mControlPositionY, getWidth() + mControlPositionX, - getHeight() + mControlPositionY); - mPreviousTouchX = (int) event.getX(); - mPreviousTouchY = (int) event.getY(); - break; - } - } - - public void setPosition(int x, int y) - { - mControlPositionX = x; - mControlPositionY = y; - } - - public void setBounds(int left, int top, int right, int bottom) - { - mDefaultStateBitmap.setBounds(left, top, right, bottom); - mPressedOneDirectionStateBitmap.setBounds(left, top, right, bottom); - mPressedTwoDirectionsStateBitmap.setBounds(left, top, right, bottom); - } - - public void setOpacity(int value) - { - mDefaultStateBitmap.setAlpha(value); - mPressedOneDirectionStateBitmap.setAlpha(value); - mPressedTwoDirectionsStateBitmap.setAlpha(value); - } - - public Rect getBounds() - { - return mDefaultStateBitmap.getBounds(); - } - - public int getWidth() - { - return mWidth; - } - - public int getHeight() - { - return mHeight; - } - - public void setState(int pressState) - { - mPressState = pressState; - } -} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableDpad.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableDpad.kt new file mode 100644 index 0000000000..2b53046a1e --- /dev/null +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableDpad.kt @@ -0,0 +1,189 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.dolphinemu.dolphinemu.overlay + +import android.content.res.Resources +import android.graphics.Bitmap +import android.graphics.Canvas +import android.graphics.Rect +import android.graphics.drawable.BitmapDrawable +import android.view.MotionEvent + +/** + * Custom [BitmapDrawable] that is capable + * of storing it's own ID. + * + * @param res [Resources] instance. + * @param defaultStateBitmap [Bitmap] of the default state. + * @param pressedOneDirectionStateBitmap [Bitmap] of the pressed state in one direction. + * @param pressedTwoDirectionsStateBitmap [Bitmap] of the pressed state in two direction. + * @param legacyId Legacy identifier (ButtonType) for the up button. + * @param upControl Control identifier for the up button. + * @param downControl Control identifier for the down button. + * @param leftControl Control identifier for the left button. + * @param rightControl Control identifier for the right button. + */ +class InputOverlayDrawableDpad( + res: Resources, + defaultStateBitmap: Bitmap, + pressedOneDirectionStateBitmap: Bitmap, + pressedTwoDirectionsStateBitmap: Bitmap, + val legacyId: Int, + upControl: Int, + downControl: Int, + leftControl: Int, + rightControl: Int +) { + private val controls = IntArray(4) + var trackId: Int = -1 + private var previousTouchX = 0 + private var previousTouchY = 0 + private var controlPositionX = 0 + private var controlPositionY = 0 + val width: Int + val height: Int + private val defaultStateBitmap: BitmapDrawable + private val pressedOneDirectionStateBitmap: BitmapDrawable + private val pressedTwoDirectionsStateBitmap: BitmapDrawable + private var pressState = STATE_DEFAULT + + init { + this.defaultStateBitmap = BitmapDrawable(res, defaultStateBitmap) + this.pressedOneDirectionStateBitmap = BitmapDrawable(res, pressedOneDirectionStateBitmap) + this.pressedTwoDirectionsStateBitmap = BitmapDrawable(res, pressedTwoDirectionsStateBitmap) + + width = this.defaultStateBitmap.intrinsicWidth + height = this.defaultStateBitmap.intrinsicHeight + + controls[0] = upControl + controls[1] = downControl + controls[2] = leftControl + controls[3] = rightControl + } + + fun draw(canvas: Canvas) { + val px = controlPositionX + width / 2 + val py = controlPositionY + height / 2 + when (pressState) { + STATE_DEFAULT -> defaultStateBitmap.draw(canvas) + STATE_PRESSED_UP -> pressedOneDirectionStateBitmap.draw(canvas) + STATE_PRESSED_RIGHT -> { + canvas.apply { + save() + rotate(90f, px.toFloat(), py.toFloat()) + pressedOneDirectionStateBitmap.draw(canvas) + restore() + } + } + + STATE_PRESSED_DOWN -> { + canvas.apply { + save() + rotate(180f, px.toFloat(), py.toFloat()) + pressedOneDirectionStateBitmap.draw(canvas) + restore() + } + } + + STATE_PRESSED_LEFT -> { + canvas.apply { + save() + rotate(270f, px.toFloat(), py.toFloat()) + pressedOneDirectionStateBitmap.draw(canvas) + restore() + } + } + + STATE_PRESSED_UP_LEFT -> pressedTwoDirectionsStateBitmap.draw(canvas) + STATE_PRESSED_UP_RIGHT -> { + canvas.apply { + save() + rotate(90f, px.toFloat(), py.toFloat()) + pressedTwoDirectionsStateBitmap.draw(canvas) + restore() + } + } + + STATE_PRESSED_DOWN_RIGHT -> { + canvas.apply { + save() + rotate(180f, px.toFloat(), py.toFloat()) + pressedTwoDirectionsStateBitmap.draw(canvas) + restore() + } + } + + STATE_PRESSED_DOWN_LEFT -> { + canvas.apply { + save() + rotate(270f, px.toFloat(), py.toFloat()) + pressedTwoDirectionsStateBitmap.draw(canvas) + restore() + } + } + } + } + + /** + * Gets one of the InputOverlayDrawableDpad's control IDs. + */ + fun getControl(direction: Int): Int { + return controls[direction] + } + + fun onConfigureTouch(event: MotionEvent) { + when (event.action) { + MotionEvent.ACTION_DOWN -> { + previousTouchX = event.x.toInt() + previousTouchY = event.y.toInt() + } + + MotionEvent.ACTION_MOVE -> { + controlPositionX += event.x.toInt() - previousTouchX + controlPositionY += event.y.toInt() - previousTouchY + setBounds( + controlPositionX, controlPositionY, width + controlPositionX, + height + controlPositionY + ) + previousTouchX = event.x.toInt() + previousTouchY = event.y.toInt() + } + } + } + + fun setPosition(x: Int, y: Int) { + controlPositionX = x + controlPositionY = y + } + + fun setBounds(left: Int, top: Int, right: Int, bottom: Int) { + defaultStateBitmap.setBounds(left, top, right, bottom) + pressedOneDirectionStateBitmap.setBounds(left, top, right, bottom) + pressedTwoDirectionsStateBitmap.setBounds(left, top, right, bottom) + } + + fun setOpacity(value: Int) { + defaultStateBitmap.alpha = value + pressedOneDirectionStateBitmap.alpha = value + pressedTwoDirectionsStateBitmap.alpha = value + } + + val bounds: Rect + get() = defaultStateBitmap.bounds + + fun setState(pressState: Int) { + this.pressState = pressState + } + + companion object { + const val STATE_DEFAULT = 0 + const val STATE_PRESSED_UP = 1 + const val STATE_PRESSED_DOWN = 2 + const val STATE_PRESSED_LEFT = 3 + const val STATE_PRESSED_RIGHT = 4 + const val STATE_PRESSED_UP_LEFT = 5 + const val STATE_PRESSED_UP_RIGHT = 6 + const val STATE_PRESSED_DOWN_LEFT = 7 + const val STATE_PRESSED_DOWN_RIGHT = 8 + } +}