Add Face Buttons to Android

This commit is contained in:
Florian Märkl 2019-09-28 22:29:13 +02:00
commit 5d0ec59ea3
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
15 changed files with 441 additions and 3 deletions

View file

@ -25,6 +25,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.*
import com.metallic.chiaki.R
import com.metallic.chiaki.StreamStateIdle
import com.metallic.chiaki.lib.ConnectInfo
import com.metallic.chiaki.touchcontrols.TouchControlsFragment
import kotlinx.android.synthetic.main.activity_stream.*
@ -60,7 +61,7 @@ class StreamActivity : AppCompatActivity()
viewModel.session.attachToTextureView(textureView)
viewModel.session.state.observe(this, Observer {
stateTextView.text = "$it"
stateTextView.text = if(it != StreamStateIdle) "$it" else ""
})
textureView.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->

View file

@ -0,0 +1,77 @@
/*
* 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.graphics.Canvas
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.metallic.chiaki.R
class ButtonView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr)
{
var buttonPressed = false
private set(value)
{
val diff = field != value
field = value
if(diff)
{
invalidate()
buttonPressedCallback?.let { it(field) }
}
}
var buttonPressedCallback: ((Boolean) -> Unit)? = null
private val drawableIdle: Drawable?
private val drawablePressed: Drawable?
init
{
context.theme.obtainStyledAttributes(attrs, R.styleable.ButtonView, 0, 0).apply {
drawableIdle = getDrawable(R.styleable.ButtonView_drawableIdle)
drawablePressed = getDrawable(R.styleable.ButtonView_drawablePressed)
recycle()
}
isClickable = true
}
override fun onDraw(canvas: Canvas)
{
super.onDraw(canvas)
val drawable = if(buttonPressed) drawablePressed else drawableIdle
drawable?.setBounds(0, 0, width, height)
drawable?.draw(canvas)
}
override fun onTouchEvent(event: MotionEvent): Boolean
{
when(event.action)
{
MotionEvent.ACTION_DOWN -> buttonPressed = true
MotionEvent.ACTION_UP -> buttonPressed = false
}
return true
}
}

View file

@ -47,6 +47,11 @@ class TouchControlsFragment : Fragment()
{
super.onViewCreated(view, savedInstanceState)
dpadView.stateChangeCallback = this::dpadStateChanged
crossButtonView.buttonPressedCallback = buttonStateChanged(ControllerState.BUTTON_CROSS)
moonButtonView.buttonPressedCallback = buttonStateChanged(ControllerState.BUTTON_MOON)
pyramidButtonView.buttonPressedCallback = buttonStateChanged(ControllerState.BUTTON_PYRAMID)
boxButtonView.buttonPressedCallback = buttonStateChanged(ControllerState.BUTTON_BOX)
psButtonView.buttonPressedCallback = buttonStateChanged(ControllerState.BUTTON_PS)
}
private fun dpadStateChanged(direction: DPadView.Direction?)
@ -67,4 +72,15 @@ class TouchControlsFragment : Fragment()
})
}
}
private fun buttonStateChanged(buttonMask: UInt) = { pressed: Boolean ->
controllerState = controllerState.copy().apply {
buttons =
if(pressed)
buttons or buttonMask
else
buttons and buttonMask.inv()
}
}
}

View file

@ -0,0 +1,13 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="512dp"
android:height="512dp"
android:viewportWidth="135.46666"
android:viewportHeight="135.46667">
<path
android:pathData="m67.733,0c-43.925,-1.668 -79.229,46.32 -64.708,87.75 11.396,42.428 67.652,62.049 102.953,35.886 37.18,-23.426 39.288,-82.963 3.859,-108.96 -11.851,-9.454 -26.946,-14.714 -42.104,-14.676zM25.401,25.401c28.222,0 56.444,0 84.666,0 0,28.222 0,56.444 0,84.666 -28.222,0 -56.444,0 -84.666,0 0,-28.222 0,-56.444 0,-84.666zM37.277,37.277c0,20.304 0,40.608 0,60.912 20.304,0 40.608,0 60.912,0 0,-20.304 0,-40.608 0,-60.912 -20.304,0 -40.608,0 -60.912,0z"
android:strokeAlpha="1"
android:strokeWidth="9.17431831"
android:fillColor="@color/control_primary"
android:strokeColor="#00000000"
android:fillAlpha="1"/>
</vector>

View file

@ -0,0 +1,13 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="512dp"
android:height="512dp"
android:viewportWidth="135.46666"
android:viewportHeight="135.46667">
<path
android:pathData="m67.733,0c-43.925,-1.668 -79.229,46.32 -64.708,87.75 11.396,42.428 67.652,62.049 102.953,35.886 37.18,-23.426 39.288,-82.963 3.859,-108.96 -11.851,-9.454 -26.946,-14.714 -42.104,-14.676zM25.401,25.401c28.222,0 56.444,0 84.666,0 0,28.222 0,56.444 0,84.666 -28.222,0 -56.444,0 -84.666,0 0,-28.222 0,-56.444 0,-84.666zM37.277,37.277c0,20.304 0,40.608 0,60.912 20.304,0 40.608,0 60.912,0 0,-20.304 0,-40.608 0,-60.912 -20.304,0 -40.608,0 -60.912,0z"
android:strokeAlpha="1"
android:strokeWidth="9.17431831"
android:fillColor="@color/control_pressed"
android:strokeColor="#00000000"
android:fillAlpha="1"/>
</vector>

View file

@ -0,0 +1,13 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="512dp"
android:height="512dp"
android:viewportWidth="135.46666"
android:viewportHeight="135.46667">
<path
android:pathData="M67.733,0A67.733,67.733 0,0 0,0 67.733,67.733 67.733,46.804 0,0 67.733,135.467 67.733,67.733 0,0 0,135.467 67.733,67.733 67.733,133.241 0,0 67.733,0ZM33.309,24.328 L67.733,58.753 102.158,24.328 111.138,33.309 76.714,67.733 111.138,102.158 102.158,111.138 67.733,76.714 33.309,111.138 24.328,102.158 58.753,67.733 24.328,33.309Z"
android:strokeAlpha="1"
android:strokeWidth="34.67459106"
android:fillColor="@color/control_primary"
android:strokeColor="#00000000"
android:fillAlpha="1"/>
</vector>

View file

@ -0,0 +1,13 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="512dp"
android:height="512dp"
android:viewportWidth="135.46666"
android:viewportHeight="135.46667">
<path
android:pathData="M67.733,0A67.733,67.733 0,0 0,0 67.733,67.733 67.733,46.804 0,0 67.733,135.467 67.733,67.733 0,0 0,135.467 67.733,67.733 67.733,133.241 0,0 67.733,0ZM33.309,24.328 L67.733,58.753 102.158,24.328 111.138,33.309 76.714,67.733 111.138,102.158 102.158,111.138 67.733,76.714 33.309,111.138 24.328,102.158 58.753,67.733 24.328,33.309Z"
android:strokeAlpha="1"
android:strokeWidth="34.67459106"
android:fillColor="@color/control_pressed"
android:strokeColor="#00000000"
android:fillAlpha="1"/>
</vector>

View file

@ -0,0 +1,13 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="512dp"
android:height="512dp"
android:viewportWidth="135.46666"
android:viewportHeight="135.46667">
<path
android:pathData="M67.733,0A67.733,67.733 0,0 0,0 67.733,67.733 67.733,46.804 0,0 67.733,135.467 67.733,67.733 0,0 0,135.467 67.733,67.733 67.733,133.241 0,0 67.733,0ZM67.732,14.801c29.158,0 52.934,23.775 52.934,52.934 0,29.158 -23.775,52.932 -52.934,52.932 -29.158,0 -52.932,-23.773 -52.932,-52.932 0,-29.158 23.773,-52.934 52.932,-52.934zM67.732,27.502c-22.295,0 -40.23,17.938 -40.23,40.233 0,22.295 17.935,40.231 40.23,40.231 22.295,0 40.233,-17.936 40.233,-40.231 0,-22.295 -17.938,-40.233 -40.233,-40.233z"
android:strokeAlpha="1"
android:strokeWidth="34.67459106"
android:fillColor="@color/control_primary"
android:strokeColor="#00000000"
android:fillAlpha="1"/>
</vector>

View file

@ -0,0 +1,13 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="512dp"
android:height="512dp"
android:viewportWidth="135.46666"
android:viewportHeight="135.46667">
<path
android:pathData="M67.733,0A67.733,67.733 0,0 0,0 67.733,67.733 67.733,46.804 0,0 67.733,135.467 67.733,67.733 0,0 0,135.467 67.733,67.733 67.733,133.241 0,0 67.733,0ZM67.732,14.801c29.158,0 52.934,23.775 52.934,52.934 0,29.158 -23.775,52.932 -52.934,52.932 -29.158,0 -52.932,-23.773 -52.932,-52.932 0,-29.158 23.773,-52.934 52.932,-52.934zM67.732,27.502c-22.295,0 -40.23,17.938 -40.23,40.233 0,22.295 17.935,40.231 40.23,40.231 22.295,0 40.233,-17.936 40.233,-40.231 0,-22.295 -17.938,-40.233 -40.233,-40.233z"
android:strokeAlpha="1"
android:strokeWidth="34.67459106"
android:fillColor="@color/control_pressed"
android:strokeColor="#00000000"
android:fillAlpha="1"/>
</vector>

View file

@ -0,0 +1,13 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="512dp"
android:height="512dp"
android:viewportWidth="135.46666"
android:viewportHeight="135.46667">
<path
android:pathData="M67.733,0A67.733,67.733 0,0 0,0 67.733,67.733 67.733,46.804 0,0 67.733,135.467 67.733,67.733 0,0 0,135.467 67.733,67.733 67.733,133.241 0,0 67.733,0ZM67.732,9.576 L112.6,87.287 118.1,96.812L17.367,96.812ZM67.734,34.977 L39.365,84.111l56.737,0z"
android:strokeAlpha="1"
android:strokeWidth="34.67459106"
android:fillColor="@color/control_primary"
android:strokeColor="#00000000"
android:fillAlpha="1"/>
</vector>

View file

@ -0,0 +1,13 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="512dp"
android:height="512dp"
android:viewportWidth="135.46666"
android:viewportHeight="135.46667">
<path
android:pathData="M67.733,0A67.733,67.733 0,0 0,0 67.733,67.733 67.733,46.804 0,0 67.733,135.467 67.733,67.733 0,0 0,135.467 67.733,67.733 67.733,133.241 0,0 67.733,0ZM67.732,9.576 L112.6,87.287 118.1,96.812L17.367,96.812ZM67.734,34.977 L39.365,84.111l56.737,0z"
android:strokeAlpha="1"
android:strokeWidth="34.67459106"
android:fillColor="@color/control_pressed"
android:strokeColor="#00000000"
android:fillAlpha="1"/>
</vector>

View file

@ -10,11 +10,77 @@
<com.metallic.chiaki.touchcontrols.DPadView
android:id="@+id/dpadView"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_width="128dp"
android:layout_height="128dp"
android:layout_marginLeft="32dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="144dp"
android:layout_height="144dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<com.metallic.chiaki.touchcontrols.ButtonView
android:id="@+id/crossButtonView"
android:layout_width="@dimen/control_face_button_size"
android:layout_height="@dimen/control_face_button_size"
android:padding="8dp"
app:drawableIdle="@drawable/control_button_cross"
app:drawablePressed="@drawable/control_button_cross_pressed"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<com.metallic.chiaki.touchcontrols.ButtonView
android:id="@+id/moonButtonView"
android:layout_width="@dimen/control_face_button_size"
android:layout_height="@dimen/control_face_button_size"
android:padding="8dp"
app:drawableIdle="@drawable/control_button_moon"
app:drawablePressed="@drawable/control_button_moon_pressed"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<com.metallic.chiaki.touchcontrols.ButtonView
android:id="@+id/pyramidButtonView"
android:layout_width="@dimen/control_face_button_size"
android:layout_height="@dimen/control_face_button_size"
android:padding="8dp"
app:drawableIdle="@drawable/control_button_pyramid"
app:drawablePressed="@drawable/control_button_pyramid_pressed"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<com.metallic.chiaki.touchcontrols.ButtonView
android:id="@+id/boxButtonView"
android:layout_width="@dimen/control_face_button_size"
android:layout_height="@dimen/control_face_button_size"
android:padding="8dp"
app:drawableIdle="@drawable/control_button_box"
app:drawablePressed="@drawable/control_button_box_pressed"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.metallic.chiaki.touchcontrols.ButtonView
android:id="@+id/psButtonView"
android:layout_width="@dimen/control_face_button_size"
android:layout_height="@dimen/control_face_button_size"
android:padding="8dp"
android:layout_marginTop="8dp"
app:drawableIdle="@drawable/control_button_pyramid"
app:drawablePressed="@drawable/control_button_pyramid_pressed"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ButtonView">
<attr name="drawableIdle" format="reference" />
<attr name="drawablePressed" format="integer" />
</declare-styleable>
</resources>

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="control_face_button_size">48dp</dimen>
</resources>