Skip to content
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
4 changes: 4 additions & 0 deletions core/api/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,10 @@ public final class org/polyfrost/compose/host/ComposeOptionsKt {
public static final fun getLocalComposeOptions ()Landroidx/compose/runtime/ProvidableCompositionLocal;
}

public final class org/polyfrost/compose/host/ContentScaleKt {
public static final fun getLocalContentScale ()Landroidx/compose/runtime/ProvidableCompositionLocal;
}

public abstract interface class org/polyfrost/compose/host/GameInputLayer {
public static final field ABOVE_HUD_LAYERS I
public static final field BELOW_HUD_LAYERS I
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.polyfrost.compose.host

import androidx.compose.runtime.ProvidableCompositionLocal
import androidx.compose.runtime.compositionLocalOf

public val LocalContentScale: ProvidableCompositionLocal<Float> = compositionLocalOf { 1f }
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,13 @@ internal object McItemAtlas {
return entry
}

private fun renderPx(ctx: RenderContext, size: Float): Int {
return ceil(size * ctx.deviceScale() - 0.01f).toInt().coerceAtLeast(1)
private fun renderPx(ctx: RenderContext, size: Float): Int = renderPx(size * ctx.deviceScale())

private fun renderPx(devicePx: Float): Int = ceil(devicePx - 0.01f).toInt().coerceAtLeast(1)

fun requestSize(handle: McItemIconHandle, devicePx: Float) {
val entry = handle as? IconEntry ?: return
if (entry.requestedSizePx == 0) entry.setRenderSizePx(renderPx(devicePx))
}

private fun ensureHooks() {
Expand Down Expand Up @@ -535,7 +540,7 @@ internal object McItemAtlas {
if (active.none { it !== entry && it.failedStack === variant }) failedVariants.removeAll { it === variant }
}

private fun renderAll() {
fun renderAll() {
if (!available.value || renderingFailed) return
try {
reconcileTopology()
Expand Down
13 changes: 13 additions & 0 deletions src/main/kotlin/org/polyfrost/compose/minecraft/mc/McItemIcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
//? if > 1.8.9 {
Expand All @@ -21,6 +22,8 @@ import org.polyfrost.compose.backend.drawPoly
import org.polyfrost.compose.composables.NodeCanvas
import org.polyfrost.compose.composables.PolyModifier
import org.polyfrost.compose.composables.size
import org.polyfrost.compose.host.LocalContentScale
import org.polyfrost.compose.minecraft.platform.McWindow
import org.polyfrost.compose.render.RenderContext

object McItemIcons {
Expand Down Expand Up @@ -49,6 +52,12 @@ object McItemIcons {
McItemAtlas.requestAvailability()
}

/** Renders icons needed by HUD consumers before the HUD dirty gate. */
@JvmStatic
fun renderPending() {
McItemAtlas.renderAll()
}

@JvmStatic
@JvmOverloads
fun drawDecorations(ctx: RenderContext, stack: ItemStack, x: Float, y: Float, size: Float = ITEM_SIZE, countOverride: String? = null) {
Expand Down Expand Up @@ -88,6 +97,8 @@ fun McItemIcon(
alpha: Float = 1f,
) {
val handle = rememberItemIconHandle(stack, decorations)
val density = LocalDensity.current
SideEffect { McItemAtlas.requestSize(handle, with(density) { size.toPx() }) }
Box(
modifier
.size(size)
Expand Down Expand Up @@ -129,6 +140,8 @@ fun NodeMcItemIcon(
alpha: Float = 1f,
) {
val handle = rememberItemIconHandle(stack, decorations)
val scale = LocalContentScale.current
SideEffect { McItemAtlas.requestSize(handle, size * McWindow.guiScale() * scale) }
val revision = handle.revision
val available = McItemIcons.available
val onDraw: RenderContext.(Float, Float, Float, Float) -> Unit = remember(handle, revision, available, decorations, countOverride, alpha) {
Expand Down