Android DPad Direction

This commit is contained in:
Florian Märkl 2019-09-25 18:55:05 +02:00
commit c299569f9d
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
5 changed files with 87 additions and 7 deletions

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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
}

View file

@ -18,31 +18,53 @@
package com.metallic.chiaki.touchcontrols package com.metallic.chiaki.touchcontrols
import android.content.Context import android.content.Context
import android.graphics.BlendMode
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.PorterDuff
import android.graphics.drawable.VectorDrawable
import android.util.AttributeSet import android.util.AttributeSet
import android.util.Log import android.util.Log
import android.view.MotionEvent import android.view.MotionEvent
import android.view.View import android.view.View
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat
import com.metallic.chiaki.R import com.metallic.chiaki.R
import kotlin.math.abs
class DPadView @JvmOverloads constructor( class DPadView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) ) : View(context, attrs, defStyleAttr)
{ {
enum class Direction { LEFT, RIGHT, UP, DOWN }
private val colorPrimary = resources.getColor(R.color.control_primary, null) private val colorPrimary = resources.getColor(R.color.control_primary, null)
private val dpadDrawable = VectorDrawableCompat.create(resources, R.drawable.control_dpad, 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) super.onDraw(canvas)
dpadDrawable?.setBounds(0, 0, width, height) 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) 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 override fun onTouchEvent(event: MotionEvent): Boolean
{ {
Log.i("DPadView", "onTouchEvent $event") Log.i("DPadView", "direction: ${directionForPosition(event.x, event.y)}")
return true return true
} }

View file

@ -7,28 +7,28 @@
android:pathData="M45.244,0L45.244,36.263L62.162,53.181l11.144,0L90.223,36.263L90.223,0Z" android:pathData="M45.244,0L45.244,36.263L62.162,53.181l11.144,0L90.223,36.263L90.223,0Z"
android:strokeAlpha="1" android:strokeAlpha="1"
android:strokeWidth="30.92149353" android:strokeWidth="30.92149353"
android:fillColor="#000000" android:fillColor="@color/control_primary"
android:strokeColor="#00000000" android:strokeColor="#00000000"
android:fillAlpha="1"/> android:fillAlpha="1"/>
<path <path
android:pathData="M0,45.244L0,90.223L36.263,90.223L53.181,73.305L53.181,62.162L36.263,45.244Z" android:pathData="M0,45.244L0,90.223L36.263,90.223L53.181,73.305L53.181,62.162L36.263,45.244Z"
android:strokeAlpha="1" android:strokeAlpha="1"
android:strokeWidth="30.92149353" android:strokeWidth="30.92149353"
android:fillColor="#000000" android:fillColor="@color/control_primary"
android:strokeColor="#00000000" android:strokeColor="#00000000"
android:fillAlpha="1"/> android:fillAlpha="1"/>
<path <path
android:pathData="M99.203,45.244 L82.285,62.162l0,11.144L99.203,90.223L135.467,90.223L135.467,45.244Z" android:pathData="M99.203,45.244 L82.285,62.162l0,11.144L99.203,90.223L135.467,90.223L135.467,45.244Z"
android:strokeAlpha="1" android:strokeAlpha="1"
android:strokeWidth="30.92149353" android:strokeWidth="30.92149353"
android:fillColor="#000000" android:fillColor="@color/control_primary"
android:strokeColor="#00000000" android:strokeColor="#00000000"
android:fillAlpha="1"/> android:fillAlpha="1"/>
<path <path
android:pathData="M62.162,82.285 L45.244,99.203L45.244,135.467L90.223,135.467L90.223,99.203L73.305,82.285Z" android:pathData="M62.162,82.285 L45.244,99.203L45.244,135.467L90.223,135.467L90.223,99.203L73.305,82.285Z"
android:strokeAlpha="1" android:strokeAlpha="1"
android:strokeWidth="30.92149353" android:strokeWidth="30.92149353"
android:fillColor="#000000" android:fillColor="@color/control_primary"
android:strokeColor="#00000000" android:strokeColor="#00000000"
android:fillAlpha="1"/> android:fillAlpha="1"/>
</vector> </vector>

View file

@ -59,4 +59,12 @@
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<com.metallic.chiaki.touchcontrols.DPadView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="32dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -17,4 +17,24 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"/> app:layout_constraintBottom_toBottomOf="parent"/>
<com.metallic.chiaki.touchcontrols.ControlsBackgroundView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.metallic.chiaki.touchcontrols.DPadView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="32dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<com.metallic.chiaki.touchcontrols.DPadView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginRight="32dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>