Skip to content

Commit

Permalink
tweak zoom calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
qimiko committed Jan 23, 2025
1 parent 071cf40 commit bb6146b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
3 changes: 1 addition & 2 deletions app/src/main/java/com/geode/launcher/GeometryDashActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,7 @@ class GeometryDashActivity : AppCompatActivity(), Cocos2dxHelper.Cocos2dxHelperL
}

if (mScreenZoomFit && mScreenZoom != 1.0f) {
frameLayout.scaleX = 1/mScreenZoom
frameLayout.scaleY = 1/mScreenZoom
frameLayout.fitZoom = true
}

if (mScreenZoom != 1.0f) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,71 @@ package com.geode.launcher.utils

import android.content.Context
import android.widget.FrameLayout
import kotlin.math.roundToInt
import kotlin.math.ceil
import kotlin.math.floor
import kotlin.math.min

class ConstrainedFrameLayout(context: Context) : FrameLayout(context) {
// avoid running the aspect ratio fix when we don't need it
var aspectRatio: Float? = null
var zoom: Float? = null
var fitZoom: Boolean = false

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
var paddingX = 0
var paddingY = 0
var paddingX = 0.0f
var paddingY = 0.0f

val currentAspectRatio = aspectRatio
if (currentAspectRatio != null) {
val currentZoom = zoom
if (currentZoom != null) {
val width = MeasureSpec.getSize(widthMeasureSpec)
val height = MeasureSpec.getSize(heightMeasureSpec)
if (width == 0 || height == 0) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
return
}

val calculatedWidth = height * currentAspectRatio
val padding = ((width - calculatedWidth) / 2).toInt()
val calculatedWidth = width * currentZoom
val calculatedHeight = height * currentZoom

// ignore paddings where the screen ends up looking odd
paddingX += padding
paddingX = (width - calculatedWidth)/2
paddingY = (height - calculatedHeight)/2
}

val currentZoom = zoom
if (currentZoom != null) {
val currentAspectRatio = aspectRatio
if (currentAspectRatio != null) {
val width = MeasureSpec.getSize(widthMeasureSpec)
val height = MeasureSpec.getSize(heightMeasureSpec)
if (width == 0 || height == 0) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
return
}

val zoomFactor = 1 - currentZoom
val widthFactor = width/2.0f + paddingX/4.0f
val heightFactor = height/2.0f
val calculatedWidth = (height - paddingY * 2) * currentAspectRatio
val padding = (width - calculatedWidth) / 2

paddingX = (widthFactor * zoomFactor).roundToInt()
paddingY = (heightFactor * zoomFactor).roundToInt()
paddingX = padding
}

if (paddingX > 0 || paddingY > 0) {
setPadding(paddingX, paddingY, paddingX, paddingY)
if (paddingX > 0.0 || paddingY > 0.0) {
if (fitZoom) {
val width = MeasureSpec.getSize(widthMeasureSpec)
val height = MeasureSpec.getSize(heightMeasureSpec)

val calculatedHeight = (height - paddingY * 2)
val calculatedWidth = (width - paddingX * 2)

val zoomFactor = min(height / calculatedHeight, width / calculatedWidth)

scaleX = zoomFactor
scaleY = zoomFactor
}

setPadding(
ceil(paddingX).toInt(),
ceil(paddingY).toInt(),
floor(paddingX).toInt(),
floor(paddingY).toInt()
)
}

super.onMeasure(
Expand Down

0 comments on commit bb6146b

Please sign in to comment.