Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes menuPrompt overlap, adds menuPrompt position memory #66

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ android {
applicationId "app.jerboa.spp"
minSdk 23
targetSdk 34
versionCode 47
versionCode 48
versionName "0.7.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/app/jerboa/spp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
Expand All @@ -29,6 +30,7 @@ import app.jerboa.spp.ui.theme.SPPTheme
import app.jerboa.spp.viewmodel.AboutViewModel
import app.jerboa.spp.viewmodel.MUSIC
import app.jerboa.spp.viewmodel.MenuPromptViewModel
import app.jerboa.spp.viewmodel.NULL_MENU_POSITION
import app.jerboa.spp.viewmodel.REVIEW_RATE_LIMIT_MILLIS
import app.jerboa.spp.viewmodel.SOCIAL
import app.jerboa.spp.viewmodel.SPPViewModel
Expand All @@ -50,7 +52,7 @@ import java.lang.Integer.min
import java.util.Date


val news = "news-21-07-24"
val news = "news-20-08-24"

val DEBUG = false

Expand Down Expand Up @@ -556,13 +558,19 @@ class MainActivity : AppCompatActivity() {
prefsEdit.apply()
}

menuPromptViewModel.onPositionChanged(
Offset(
prefs.getFloat("menuX", NULL_MENU_POSITION.x),
prefs.getFloat("menuY", NULL_MENU_POSITION.y)
)
)

toyMenuViewModel.onShowToysChanged(prefs.getBoolean("showToys", false))

// if (BuildConfig.DEBUG){
// prefs.edit().clear().apply()
// }


// play game services

if (isGooglePlayGamesServicesInstalled(this)) {
Expand Down Expand Up @@ -641,6 +649,8 @@ class MainActivity : AppCompatActivity() {
val prefs = getSharedPreferences("jerboa.app.spp.prefs", MODE_PRIVATE)
val prefsEdit = prefs.edit()
prefsEdit.putLong("playTime", totalTime)
prefsEdit.putFloat("menuX", menuPromptViewModel.position.value!!.x)
prefsEdit.putFloat("menuY", menuPromptViewModel.position.value!!.y)
prefsEdit.apply()
super.onStop()
sppViewModel.stopClock()
Expand Down
63 changes: 42 additions & 21 deletions app/src/main/java/app/jerboa/spp/composable/menuPrompt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.*
Expand All @@ -23,6 +24,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalConfiguration
Expand All @@ -31,6 +33,7 @@ import androidx.compose.ui.unit.dp
import app.jerboa.spp.AppInfo
import app.jerboa.spp.viewmodel.AboutViewModel
import app.jerboa.spp.viewmodel.MenuPromptViewModel
import app.jerboa.spp.viewmodel.NULL_MENU_POSITION
import app.jerboa.spp.viewmodel.SPPViewModel
import kotlin.math.abs
import kotlin.math.max
Expand All @@ -54,6 +57,8 @@ fun menuPrompt(
val paused: Boolean by menuPromptViewModel.paused.observeAsState(initial = false)
val playSuccess: Boolean by sppViewModel.playSuccess.observeAsState(initial = false)

val position: Offset by menuPromptViewModel.position.observeAsState(initial = Offset(0.0f, 0.0f))

val fadePromptAlpha: Float by animateFloatAsState(
targetValue = if (!displayingMenu) 0.33f else 1.0f,
animationSpec = tween(
Expand All @@ -62,27 +67,43 @@ fun menuPrompt(
), label = "alpha for fading the prompt"
)

val xmax = info.widthDp*info.density-1.7f*menuItemHeight.toFloat()*info.density
val ymax = info.heightDp*info.density-1.75f*menuItemHeight.toFloat()*info.density
var position by remember { mutableStateOf(Offset(0.0f,ymax)) }
val configuration = LocalConfiguration.current
val screenWidth = configuration.screenWidthDp.dp.value
val screenHeight = configuration.screenHeightDp.dp.value

val pad = 0.1f
val xBounds = Pair(
pad*menuItemHeight.toFloat()*info.density,
screenWidth*info.density-(1f+pad)*menuItemHeight.toFloat()*info.density
)
val yBounds = Pair(
pad*menuItemHeight.toFloat()*info.density,
screenHeight*info.density-(1f+pad)*menuItemHeight.toFloat()*info.density
)

fun applyBounds(p: Float, bounds: Pair<Float, Float>): Float {
return min(max(p, bounds.first), bounds.second)
}

if (position == NULL_MENU_POSITION) {
menuPromptViewModel.onPositionChanged(Offset(0.0f, yBounds.second))
}

Box(modifier = Modifier
.fillMaxHeight()
.fillMaxWidth()
.padding(16.dp)) {
.fillMaxWidth()) {
Box(
modifier = Modifier
.fillMaxHeight()
.fillMaxWidth()
.graphicsLayer {
if (displayingAbout || displayingSliders) {
if (abs(position.x-xmax) < abs(position.x)) {
translationX = xmax
}
else {
if (abs(position.x - xBounds.second) < abs(position.x)) {
translationX = xBounds.second
} else {
translationX = 0.0f
}
translationY = ymax
translationY = yBounds.second
} else {
translationX = position.x
translationY = position.y
Expand All @@ -97,8 +118,7 @@ fun menuPrompt(
verticalMenu(
modifier = Modifier,
offset = Pair(0.dp, 0.dp),
contentPadding = 16.dp,
headSpacePx = if (displayingAbout || displayingSliders) { ymax } else { position.y }
headSpacePx = if (displayingAbout || displayingSliders) { yBounds.second } else { position.y }
) {
Image(
painter = painterResource(id = images["toyMenu"]!!),
Expand All @@ -118,7 +138,7 @@ fun menuPrompt(
menuPromptViewModel,
menuItemHeight,
images,
left = position.x > xmax/2.0f
left = position.x > xBounds.second/2.0f
)
}
else {
Expand Down Expand Up @@ -204,13 +224,13 @@ fun menuPrompt(
.size(menuItemHeight.dp)
.graphicsLayer {
if (displayingAbout || displayingSliders) {
if (abs(position.x-xmax) < abs(position.x)) {
translationX = xmax
if (abs(position.x-xBounds.second) < abs(position.x)) {
translationX = xBounds.second
}
else {
translationX = 0.0f
}
translationY = ymax
translationY = yBounds.second
} else {
translationX = position.x
translationY = position.y
Expand All @@ -219,11 +239,12 @@ fun menuPrompt(
.conditional(!(displayingAbout || displayingSliders)){ Modifier.pointerInput(Unit) {
detectDragGestures { change, dragAmount ->
change.consume()
var x = max(0.0f, position.x+dragAmount.x)
x = min(x, xmax)
var y = max(0.0f, position.y+dragAmount.y)
y = min(y, ymax)
position = Offset(x, y)
menuPromptViewModel.onPositionChanged(
Offset(
applyBounds(position.x+dragAmount.x, xBounds),
applyBounds(position.y+dragAmount.y, yBounds)
)
)
}
}}
) {
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/app/jerboa/spp/viewmodel/menuPromptViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package app.jerboa.spp.viewmodel

import androidx.compose.ui.geometry.Offset
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

enum class MUSIC {FORREST, RAIN, NOTHING}

val NULL_MENU_POSITION = Offset(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY)

class MenuPromptViewModel : ViewModel() {

private val _displayingMenu = MutableLiveData(false)
Expand Down Expand Up @@ -48,4 +51,11 @@ class MenuPromptViewModel : ViewModel() {
_clear.postValue(v)
}

private val _position = MutableLiveData(Offset(0.0f, 0.0f))
val position: MutableLiveData<Offset> = _position

fun onPositionChanged(p: Offset) {
_position.value = p
}

}
34 changes: 34 additions & 0 deletions app/src/main/res/drawable/dismiss.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="85.777dp"
android:height="84.655dp"
android:viewportWidth="22.695"
android:viewportHeight="22.398">
<path
android:pathData="M2.187,18.348l16.399,-16.185l2.05,2.023l-16.399,16.185z"
android:strokeLineJoin="round"
android:strokeWidth="1.32292"
android:fillColor="#00000000"
android:strokeColor="#000000"
android:strokeLineCap="round"/>
<path
android:pathData="M2.187,18.348l16.399,-16.185l2.05,2.023l-16.399,16.185z"
android:strokeLineJoin="round"
android:strokeWidth="1.32292"
android:fillColor="#000000"
android:strokeColor="#00000000"
android:strokeLineCap="round"/>
<path
android:pathData="M20.636,18.348l-16.399,-16.185l-2.05,2.023l16.399,16.185z"
android:strokeLineJoin="round"
android:strokeWidth="1.32292"
android:fillColor="#00000000"
android:strokeColor="#000000"
android:strokeLineCap="round"/>
<path
android:pathData="M20.636,18.348l-16.399,-16.185l-2.05,2.023l16.399,16.185z"
android:strokeLineJoin="round"
android:strokeWidth="1.32292"
android:fillColor="#000000"
android:strokeColor="#00000000"
android:strokeLineCap="round"/>
</vector>
Binary file modified app/src/main/res/drawable/news.webp
Binary file not shown.
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
<string name="news7">0.5.6 20th July 2024\nFixes crashes on app links and concurrent writing, upgrades kotlin/compose.</string>
<string name="news8">0.6.0 24th July 2024\nAdds various new sliders and orbiter toy.</string>
<string name="news9">0.6.1 31st July 2024\nFixes shader and concurrent write crashes.</string>
<string name="news10">0.7.0 17th August 2024\nAdds multi-touch drag/placing, particle size slider, condensed menu, old news section.</string>
<string name="news10">0.7.0 20th August 2024\nAdds multi-touch drag/placing, particle size slider, movable menu widget, old news section.</string>

</resources>
Binary file modified assets/news.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading