Draw DPad Press on Android

This commit is contained in:
Florian Märkl 2019-09-28 15:15:19 +02:00
commit 2df51efa38
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
5 changed files with 67 additions and 12 deletions

View file

@ -35,7 +35,7 @@ data class StreamStateLoginPinRequest(val pinIncorrect: Boolean): StreamState()
class StreamSession(val connectInfo: ConnectInfo) class StreamSession(val connectInfo: ConnectInfo)
{ {
var session: Session? = null var session: Session? = null
private set(value) { field = value } private set
private val _state = MutableLiveData<StreamState>(StreamStateIdle) private val _state = MutableLiveData<StreamState>(StreamStateIdle)
val state: LiveData<StreamState> get() = _state val state: LiveData<StreamState> get() = _state

View file

@ -18,10 +18,7 @@
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
@ -36,17 +33,35 @@ class DPadView @JvmOverloads constructor(
{ {
enum class Direction { LEFT, RIGHT, UP, DOWN } enum class Direction { LEFT, RIGHT, UP, DOWN }
private val colorPrimary = resources.getColor(R.color.control_primary, null) var state: Direction? = null
private val dpadDrawable = VectorDrawableCompat.create(resources, R.drawable.control_dpad, null) private set
private val dpadIdleDrawable = VectorDrawableCompat.create(resources, R.drawable.control_dpad_idle, null)
private val dpadLeftDrawable = VectorDrawableCompat.create(resources, R.drawable.control_dpad_left, null)
override fun onDraw(canvas: Canvas) override fun onDraw(canvas: Canvas)
{ {
super.onDraw(canvas) super.onDraw(canvas)
dpadDrawable?.setBounds(0, 0, width, height)
//dpadDrawable?.setTint(colorPrimary) val state = state
//dpadDrawable?.setTintBlendMode(BlendMode.SRC_ATOP) val drawable: VectorDrawableCompat?
//dpadDrawable?.setTintMode(PorterDuff.Mode.SRC_ATOP) if(state != null)
dpadDrawable?.draw(canvas) {
drawable = dpadLeftDrawable
when(state)
{
Direction.UP -> canvas.rotate(90f, width.toFloat() * 0.5f, height.toFloat() * 0.5f)
Direction.DOWN -> canvas.rotate(90f*3f, width.toFloat() * 0.5f, height.toFloat() * 0.5f)
Direction.LEFT -> {}
Direction.RIGHT -> canvas.rotate(90f*2f, width.toFloat() * 0.5f, height.toFloat() * 0.5f)
}
}
else
drawable = dpadIdleDrawable
drawable?.setBounds(0, 0, width, height)
drawable?.alpha = 127
drawable?.draw(canvas)
} }
fun directionForPosition(x: Float, y: Float): Direction fun directionForPosition(x: Float, y: Float): Direction
@ -64,7 +79,12 @@ class DPadView @JvmOverloads constructor(
override fun onTouchEvent(event: MotionEvent): Boolean override fun onTouchEvent(event: MotionEvent): Boolean
{ {
Log.i("DPadView", "direction: ${directionForPosition(event.x, event.y)}") val dir = directionForPosition(event.x, event.y)
if(event.action == MotionEvent.ACTION_UP)
state = null
else if(event.action == MotionEvent.ACTION_DOWN)
state = dir
invalidate()
return true return true
} }

View file

@ -0,0 +1,34 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="512dp"
android:height="512dp"
android:viewportWidth="135.46667"
android:viewportHeight="135.46667">
<path
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="@color/control_primary"
android:strokeColor="#00000000"
android:fillAlpha="1"/>
<path
android:pathData="M0,45.244L0,90.223L36.263,90.223L53.181,73.305L53.181,62.162L36.263,45.244Z"
android:strokeAlpha="1"
android:strokeWidth="30.92149353"
android:fillColor="@color/control_pressed"
android:strokeColor="#00000000"
android:fillAlpha="1"/>
<path
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:strokeWidth="30.92149353"
android:fillColor="@color/control_primary"
android:strokeColor="#00000000"
android:fillAlpha="1"/>
<path
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:strokeWidth="30.92149353"
android:fillColor="@color/control_primary"
android:strokeColor="#00000000"
android:fillAlpha="1"/>
</vector>

View file

@ -8,4 +8,5 @@
<color name="stream_background">@android:color/black</color> <color name="stream_background">@android:color/black</color>
<color name="control_primary">@android:color/white</color> <color name="control_primary">@android:color/white</color>
<color name="control_pressed">#8888ff</color>
</resources> </resources>