From c299569f9d007062fa32441f3c3dd19e25924084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Wed, 25 Sep 2019 18:55:05 +0200 Subject: [PATCH] Android DPad Direction --- .../touchcontrols/ControlsBackgroundView.kt | 30 +++++++++++++++++++ .../metallic/chiaki/touchcontrols/DPadView.kt | 28 +++++++++++++++-- .../src/main/res/drawable/control_dpad.xml | 8 ++--- .../app/src/main/res/layout/activity_main.xml | 8 +++++ .../src/main/res/layout/activity_stream.xml | 20 +++++++++++++ 5 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 android/app/src/main/java/com/metallic/chiaki/touchcontrols/ControlsBackgroundView.kt diff --git a/android/app/src/main/java/com/metallic/chiaki/touchcontrols/ControlsBackgroundView.kt b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/ControlsBackgroundView.kt new file mode 100644 index 0000000..2d5b8b1 --- /dev/null +++ b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/ControlsBackgroundView.kt @@ -0,0 +1,30 @@ +/* + * This file is part of Chiaki. + * + * Chiaki is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Chiaki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ + +package com.metallic.chiaki.touchcontrols + +import android.content.Context +import android.util.AttributeSet +import android.view.MotionEvent +import android.view.View + +class ControlsBackgroundView @JvmOverloads constructor( + context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 +) : View(context, attrs, defStyleAttr) +{ + override fun onTouchEvent(event: MotionEvent) = true +} \ No newline at end of file diff --git a/android/app/src/main/java/com/metallic/chiaki/touchcontrols/DPadView.kt b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/DPadView.kt index 73f6ee3..ee502a5 100644 --- a/android/app/src/main/java/com/metallic/chiaki/touchcontrols/DPadView.kt +++ b/android/app/src/main/java/com/metallic/chiaki/touchcontrols/DPadView.kt @@ -18,31 +18,53 @@ package com.metallic.chiaki.touchcontrols import android.content.Context +import android.graphics.BlendMode import android.graphics.Canvas +import android.graphics.PorterDuff +import android.graphics.drawable.VectorDrawable import android.util.AttributeSet import android.util.Log import android.view.MotionEvent import android.view.View import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat import com.metallic.chiaki.R +import kotlin.math.abs class DPadView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr) { + enum class Direction { LEFT, RIGHT, UP, DOWN } + private val colorPrimary = resources.getColor(R.color.control_primary, null) private val dpadDrawable = VectorDrawableCompat.create(resources, R.drawable.control_dpad, null) - override fun onDraw(canvas: Canvas) { + override fun onDraw(canvas: Canvas) + { super.onDraw(canvas) dpadDrawable?.setBounds(0, 0, width, height) - dpadDrawable?.setTint(colorPrimary) + //dpadDrawable?.setTint(colorPrimary) + //dpadDrawable?.setTintBlendMode(BlendMode.SRC_ATOP) + //dpadDrawable?.setTintMode(PorterDuff.Mode.SRC_ATOP) dpadDrawable?.draw(canvas) } + fun directionForPosition(x: Float, y: Float): Direction + { + val dx = x - width * 0.5f + val dy = y - height * 0.5f + return when + { + dx > abs(dy) -> Direction.RIGHT + dx <= -abs(dy) -> Direction.LEFT + dy > abs(dx) -> Direction.DOWN + else /*dy <= -abs(dx)*/ -> Direction.UP + } + } + override fun onTouchEvent(event: MotionEvent): Boolean { - Log.i("DPadView", "onTouchEvent $event") + Log.i("DPadView", "direction: ${directionForPosition(event.x, event.y)}") return true } diff --git a/android/app/src/main/res/drawable/control_dpad.xml b/android/app/src/main/res/drawable/control_dpad.xml index 7d1dc15..6f44fb5 100644 --- a/android/app/src/main/res/drawable/control_dpad.xml +++ b/android/app/src/main/res/drawable/control_dpad.xml @@ -7,28 +7,28 @@ android:pathData="M45.244,0L45.244,36.263L62.162,53.181l11.144,0L90.223,36.263L90.223,0Z" android:strokeAlpha="1" android:strokeWidth="30.92149353" - android:fillColor="#000000" + android:fillColor="@color/control_primary" android:strokeColor="#00000000" android:fillAlpha="1"/> diff --git a/android/app/src/main/res/layout/activity_main.xml b/android/app/src/main/res/layout/activity_main.xml index 8a23077..c9516b3 100644 --- a/android/app/src/main/res/layout/activity_main.xml +++ b/android/app/src/main/res/layout/activity_main.xml @@ -59,4 +59,12 @@ app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/activity_stream.xml b/android/app/src/main/res/layout/activity_stream.xml index 4004b06..7a25c1b 100644 --- a/android/app/src/main/res/layout/activity_stream.xml +++ b/android/app/src/main/res/layout/activity_stream.xml @@ -17,4 +17,24 @@ android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent"/> + + + + + + \ No newline at end of file