From 500f1f18616b127ab997fb9df846c59446425c1d Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Sat, 3 Jun 2023 15:01:49 -0400 Subject: [PATCH] Android: Convert InputOverlayDrawableButton to Kotlin --- .../overlay/InputOverlayDrawableButton.java | 144 ------------------ .../overlay/InputOverlayDrawableButton.kt | 97 ++++++++++++ 2 files changed, 97 insertions(+), 144 deletions(-) delete mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableButton.java create mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableButton.kt diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableButton.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableButton.java deleted file mode 100644 index d997da94bf..0000000000 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableButton.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright 2013 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 InputOverlayDrawableButton -{ - // The legacy ID identifying what type of button this Drawable represents. - private int mLegacyId; - private int mControl; - private int mTrackId; - private int mPreviousTouchX, mPreviousTouchY; - private int mControlPositionX, mControlPositionY; - private int mWidth; - private int mHeight; - private BitmapDrawable mDefaultStateBitmap; - private BitmapDrawable mPressedStateBitmap; - private boolean mPressedState = false; - - /** - * Constructor - * - * @param res {@link Resources} instance. - * @param defaultStateBitmap {@link Bitmap} to use with the default state Drawable. - * @param pressedStateBitmap {@link Bitmap} to use with the pressed state Drawable. - * @param legacyId Legacy identifier (ButtonType) for this type of button. - * @param control Control ID for this type of button. - */ - public InputOverlayDrawableButton(Resources res, Bitmap defaultStateBitmap, - Bitmap pressedStateBitmap, int legacyId, int control) - { - mTrackId = -1; - mDefaultStateBitmap = new BitmapDrawable(res, defaultStateBitmap); - mPressedStateBitmap = new BitmapDrawable(res, pressedStateBitmap); - mLegacyId = legacyId; - mControl = control; - - mWidth = mDefaultStateBitmap.getIntrinsicWidth(); - mHeight = mDefaultStateBitmap.getIntrinsicHeight(); - } - - /** - * Gets this InputOverlayDrawableButton's legacy button ID. - */ - public int getLegacyId() - { - return mLegacyId; - } - - public int getControl() - { - return mControl; - } - - 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 draw(Canvas canvas) - { - getCurrentStateBitmapDrawable().draw(canvas); - } - - private BitmapDrawable getCurrentStateBitmapDrawable() - { - return mPressedState ? mPressedStateBitmap : mDefaultStateBitmap; - } - - public void setBounds(int left, int top, int right, int bottom) - { - mDefaultStateBitmap.setBounds(left, top, right, bottom); - mPressedStateBitmap.setBounds(left, top, right, bottom); - } - - public void setOpacity(int value) - { - mDefaultStateBitmap.setAlpha(value); - mPressedStateBitmap.setAlpha(value); - } - - public Rect getBounds() - { - return mDefaultStateBitmap.getBounds(); - } - - public int getWidth() - { - return mWidth; - } - - public int getHeight() - { - return mHeight; - } - - public void setPressedState(boolean isPressed) - { - mPressedState = isPressed; - } -} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableButton.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableButton.kt new file mode 100644 index 0000000000..220eb836d5 --- /dev/null +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableButton.kt @@ -0,0 +1,97 @@ +// 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] to use with the default state Drawable. + * @param pressedStateBitmap [Bitmap] to use with the pressed state Drawable. + * @param legacyId Legacy identifier (ButtonType) for this type of button. + * @param control Control ID for this type of button. + */ +class InputOverlayDrawableButton( + res: Resources, + defaultStateBitmap: Bitmap, + pressedStateBitmap: Bitmap, + val legacyId: Int, + val control: Int +) { + 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 pressedStateBitmap: BitmapDrawable + private var pressedState = false + + init { + this.defaultStateBitmap = BitmapDrawable(res, defaultStateBitmap) + this.pressedStateBitmap = BitmapDrawable(res, pressedStateBitmap) + width = this.defaultStateBitmap.intrinsicWidth + height = this.defaultStateBitmap.intrinsicHeight + } + + 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 draw(canvas: Canvas) { + currentStateBitmapDrawable.draw(canvas) + } + + private val currentStateBitmapDrawable: BitmapDrawable + get() = if (pressedState) pressedStateBitmap else defaultStateBitmap + + fun setBounds(left: Int, top: Int, right: Int, bottom: Int) { + defaultStateBitmap.setBounds(left, top, right, bottom) + pressedStateBitmap.setBounds(left, top, right, bottom) + } + + fun setOpacity(value: Int) { + defaultStateBitmap.alpha = value + pressedStateBitmap.alpha = value + } + + val bounds: Rect + get() = defaultStateBitmap.bounds + + fun setPressedState(isPressed: Boolean) { + pressedState = isPressed + } +}