diff --git a/CHANGELOG.md b/CHANGELOG.md index 1028504..507b02a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ - Fixed radial wheels and other things drawn straight onto the HUD flickering on 1.8.9 when the HUD is cached - Fixed the game drawing the wrong texture on 1.21.1-1.21.10 when a skin finished loading while PolyCompose was drawing - Fixed CJK characters, symbols and Force Unicode Font text on 1.21.1-1.21.5 not using the game's unicode font +- Added `PolyModifier.widthIn` and `PolyModifier.heightIn` for minimum and maximum sizes, and `PolyModifier.fitContent`, which shrinks a node's content to fit when it overflows ## [3.0.10] diff --git a/core/api/core.api b/core/api/core.api index 3fcbb41..c208799 100644 --- a/core/api/core.api +++ b/core/api/core.api @@ -372,8 +372,11 @@ public final class org/polyfrost/compose/composables/PolyModifierKt { public static final fun fill (Lorg/polyfrost/compose/composables/PolyModifier;)Lorg/polyfrost/compose/composables/PolyModifier; public static final fun fillHeight (Lorg/polyfrost/compose/composables/PolyModifier;)Lorg/polyfrost/compose/composables/PolyModifier; public static final fun fillWidth (Lorg/polyfrost/compose/composables/PolyModifier;)Lorg/polyfrost/compose/composables/PolyModifier; + public static final fun fitContent (Lorg/polyfrost/compose/composables/PolyModifier;)Lorg/polyfrost/compose/composables/PolyModifier; public static final fun height (Lorg/polyfrost/compose/composables/PolyModifier;F)Lorg/polyfrost/compose/composables/PolyModifier; public static final fun height (Lorg/polyfrost/compose/composables/PolyModifier;Lorg/polyfrost/compose/layout/PolySize;)Lorg/polyfrost/compose/composables/PolyModifier; + public static final fun heightIn (Lorg/polyfrost/compose/composables/PolyModifier;FF)Lorg/polyfrost/compose/composables/PolyModifier; + public static synthetic fun heightIn$default (Lorg/polyfrost/compose/composables/PolyModifier;FFILjava/lang/Object;)Lorg/polyfrost/compose/composables/PolyModifier; public static final fun margin (Lorg/polyfrost/compose/composables/PolyModifier;F)Lorg/polyfrost/compose/composables/PolyModifier; public static final fun margin (Lorg/polyfrost/compose/composables/PolyModifier;FF)Lorg/polyfrost/compose/composables/PolyModifier; public static final fun margin (Lorg/polyfrost/compose/composables/PolyModifier;FFFF)Lorg/polyfrost/compose/composables/PolyModifier; @@ -392,6 +395,8 @@ public final class org/polyfrost/compose/composables/PolyModifierKt { public static final fun tag (Lorg/polyfrost/compose/composables/PolyModifier;Ljava/lang/Object;)Lorg/polyfrost/compose/composables/PolyModifier; public static final fun width (Lorg/polyfrost/compose/composables/PolyModifier;F)Lorg/polyfrost/compose/composables/PolyModifier; public static final fun width (Lorg/polyfrost/compose/composables/PolyModifier;Lorg/polyfrost/compose/layout/PolySize;)Lorg/polyfrost/compose/composables/PolyModifier; + public static final fun widthIn (Lorg/polyfrost/compose/composables/PolyModifier;FF)Lorg/polyfrost/compose/composables/PolyModifier; + public static synthetic fun widthIn$default (Lorg/polyfrost/compose/composables/PolyModifier;FFILjava/lang/Object;)Lorg/polyfrost/compose/composables/PolyModifier; public static final fun wrapHeight (Lorg/polyfrost/compose/composables/PolyModifier;)Lorg/polyfrost/compose/composables/PolyModifier; public static final fun wrapWidth (Lorg/polyfrost/compose/composables/PolyModifier;)Lorg/polyfrost/compose/composables/PolyModifier; } @@ -1110,10 +1115,15 @@ public final class org/polyfrost/compose/layout/PolyLayoutStyle { public final fun getBorderWidth ()F public final fun getClipToBounds ()Z public final fun getFade ()Lkotlin/jvm/functions/Function0; + public final fun getFitContent ()Z public final fun getGap ()F public final fun getHeight ()Lorg/polyfrost/compose/layout/PolySize; public final fun getLayoutType ()Lorg/polyfrost/compose/layout/LayoutType; public final fun getMargin ()Lorg/polyfrost/compose/layout/PolyInsets; + public final fun getMaxHeight ()F + public final fun getMaxWidth ()F + public final fun getMinHeight ()F + public final fun getMinWidth ()F public final fun getPadding ()Lorg/polyfrost/compose/layout/PolyInsets; public final fun getPositionMode ()Lorg/polyfrost/compose/layout/PolyPositionMode; public final fun getRadius ()F @@ -1128,10 +1138,15 @@ public final class org/polyfrost/compose/layout/PolyLayoutStyle { public final fun setBorderWidth (F)V public final fun setClipToBounds (Z)V public final fun setFade (Lkotlin/jvm/functions/Function0;)V + public final fun setFitContent (Z)V public final fun setGap (F)V public final fun setHeight (Lorg/polyfrost/compose/layout/PolySize;)V public final fun setLayoutType (Lorg/polyfrost/compose/layout/LayoutType;)V public final fun setMargin (Lorg/polyfrost/compose/layout/PolyInsets;)V + public final fun setMaxHeight (F)V + public final fun setMaxWidth (F)V + public final fun setMinHeight (F)V + public final fun setMinWidth (F)V public final fun setPadding (Lorg/polyfrost/compose/layout/PolyInsets;)V public final fun setPositionMode (Lorg/polyfrost/compose/layout/PolyPositionMode;)V public final fun setRadius (F)V @@ -1295,6 +1310,7 @@ public class org/polyfrost/compose/node/PolyNode { public static final field $stable I public fun ()V public final fun getChildren ()Ljava/util/List; + public final fun getContentScale ()F public final fun getHeight ()F protected fun getLiveMeasure ()Z public final fun getParent ()Lorg/polyfrost/compose/node/PolyNode; @@ -1309,6 +1325,7 @@ public class org/polyfrost/compose/node/PolyNode { protected final fun notifyLiveMeasureChanged ()V public fun render (Lorg/polyfrost/compose/render/RenderContext;)V protected fun renderContent (Lorg/polyfrost/compose/render/RenderContext;)V + public final fun setContentScale (F)V public final fun setHeight (F)V public final fun setParent (Lorg/polyfrost/compose/node/PolyNode;)V public final fun setWidth (F)V diff --git a/core/src/main/kotlin/org/polyfrost/compose/composables/PolyModifier.kt b/core/src/main/kotlin/org/polyfrost/compose/composables/PolyModifier.kt index 3d7fdd4..4cd2a31 100644 --- a/core/src/main/kotlin/org/polyfrost/compose/composables/PolyModifier.kt +++ b/core/src/main/kotlin/org/polyfrost/compose/composables/PolyModifier.kt @@ -30,6 +30,11 @@ public open class PolyModifier internal constructor( val g = style.gap style.width = PolySize.Wrap style.height = PolySize.Wrap + style.minWidth = Float.NaN + style.maxWidth = Float.NaN + style.minHeight = Float.NaN + style.maxHeight = Float.NaN + style.fitContent = false style.align = PolyAlign.TopLeft style.padding = NO_INSETS style.margin = NO_INSETS @@ -58,6 +63,9 @@ public fun PolyModifier.width(w: Float): PolyModifier = op("width", w) { width = public fun PolyModifier.width(w: PolySize): PolyModifier = op("width", w) { width = w } public fun PolyModifier.height(h: Float): PolyModifier = op("height", h) { height = PolySize.Px(h) } public fun PolyModifier.height(h: PolySize): PolyModifier = op("height", h) { height = h } +public fun PolyModifier.widthIn(min: Float = Float.NaN, max: Float = Float.NaN): PolyModifier = op("widthIn", min, max) { minWidth = min; maxWidth = max } +public fun PolyModifier.heightIn(min: Float = Float.NaN, max: Float = Float.NaN): PolyModifier = op("heightIn", min, max) { minHeight = min; maxHeight = max } +public fun PolyModifier.fitContent(): PolyModifier = op("fitContent") { fitContent = true } public fun PolyModifier.fillWidth(): PolyModifier = op("fillWidth") { width = PolySize.Fill } public fun PolyModifier.fillHeight(): PolyModifier = op("fillHeight") { height = PolySize.Fill } public fun PolyModifier.fill(): PolyModifier = op("fill") { width = PolySize.Fill; height = PolySize.Fill } diff --git a/core/src/main/kotlin/org/polyfrost/compose/layout/PolyLayoutEngine.kt b/core/src/main/kotlin/org/polyfrost/compose/layout/PolyLayoutEngine.kt index e272e47..15545b7 100644 --- a/core/src/main/kotlin/org/polyfrost/compose/layout/PolyLayoutEngine.kt +++ b/core/src/main/kotlin/org/polyfrost/compose/layout/PolyLayoutEngine.kt @@ -3,6 +3,7 @@ package org.polyfrost.compose.layout import org.polyfrost.compose.node.PolyNode public object PolyLayoutEngine { + private const val MIN_FIT_SCALE = 0.01f public fun layout(node: PolyNode, parentWidth: Float, parentHeight: Float) { measure(node, parentWidth, parentHeight) @@ -16,8 +17,8 @@ public object PolyLayoutEngine { val wrapWidth = style.width === PolySize.Wrap && node.intrinsicWidth < 0f val wrapHeight = style.height === PolySize.Wrap && node.intrinsicHeight < 0f - if (!wrapWidth) node.width = resolveSize(style.width, parentWidth, node, horizontal = true) - if (!wrapHeight) node.height = resolveSize(style.height, parentHeight, node, horizontal = false) + if (!wrapWidth) node.width = resolveSize(style.width, parentWidth, node, horizontal = true).constrained(style.minWidth, style.maxWidth) + if (!wrapHeight) node.height = resolveSize(style.height, parentHeight, node, horizontal = false).constrained(style.minHeight, style.maxHeight) val padH = style.padding.horizontal val padV = style.padding.vertical @@ -25,26 +26,51 @@ public object PolyLayoutEngine { val provisionalH = ((if (wrapHeight) parentHeight else node.height) - padV).coerceAtLeast(0f) for (i in children.indices) measure(children[i], provisionalW, provisionalH) - if (!wrapWidth && !wrapHeight) return - if (wrapWidth) node.width = resolveSize(PolySize.Wrap, parentWidth, node, horizontal = true) - if (wrapHeight) node.height = resolveSize(PolySize.Wrap, parentHeight, node, horizontal = false) - - val finalW = (node.width - padH).coerceAtLeast(0f) - val finalH = (node.height - padV).coerceAtLeast(0f) - if (finalW == provisionalW && finalH == provisionalH) return - for (i in children.indices) { - val child = children[i] - val cw = child.style.width - val ch = child.style.height - val fillW = wrapWidth && (cw === PolySize.Fill || cw is PolySize.Percent) - val fillH = wrapHeight && (ch === PolySize.Fill || ch is PolySize.Percent) - if (fillW || fillH) measure(child, finalW, finalH) + if (wrapWidth || wrapHeight) { + resolveWrap(node, wrapWidth, wrapHeight, parentWidth, parentHeight) + val finalW = (node.width - padH).coerceAtLeast(0f) + val finalH = (node.height - padV).coerceAtLeast(0f) + if (finalW != provisionalW || finalH != provisionalH) { + for (i in children.indices) { + val child = children[i] + val cw = child.style.width + val ch = child.style.height + val fillW = wrapWidth && (cw === PolySize.Fill || cw is PolySize.Percent) + val fillH = wrapHeight && (ch === PolySize.Fill || ch is PolySize.Percent) + if (fillW || fillH) measure(child, finalW, finalH) + } + resolveWrap(node, wrapWidth, wrapHeight, parentWidth, parentHeight) + } } - if (wrapWidth) node.width = resolveSize(PolySize.Wrap, parentWidth, node, horizontal = true) - if (wrapHeight) node.height = resolveSize(PolySize.Wrap, parentHeight, node, horizontal = false) + node.contentScale = if (style.fitContent) fitScale(node, parentWidth, parentHeight) else 1f + } + + private fun resolveWrap(node: PolyNode, wrapWidth: Boolean, wrapHeight: Boolean, parentWidth: Float, parentHeight: Float) { + val style = node.style + if (wrapWidth) node.width = resolveSize(PolySize.Wrap, parentWidth, node, horizontal = true).constrained(style.minWidth, style.maxWidth) + if (wrapHeight) node.height = resolveSize(PolySize.Wrap, parentHeight, node, horizontal = false).constrained(style.minHeight, style.maxHeight) + } + + private fun fitScale(node: PolyNode, parentWidth: Float, parentHeight: Float): Float { + val padding = node.style.padding + val naturalContentWidth = resolveSize(PolySize.Wrap, parentWidth, node, horizontal = true, fillParent = false) - padding.horizontal + val naturalContentHeight = resolveSize(PolySize.Wrap, parentHeight, node, horizontal = false, fillParent = false) - padding.vertical + val contentWidth = (node.width - padding.horizontal).coerceAtLeast(0f) + val contentHeight = (node.height - padding.vertical).coerceAtLeast(0f) + var scale = 1f + if (naturalContentWidth > contentWidth && naturalContentWidth > 0f) scale = minOf(scale, contentWidth / naturalContentWidth) + if (naturalContentHeight > contentHeight && naturalContentHeight > 0f) scale = minOf(scale, contentHeight / naturalContentHeight) + return scale.coerceAtLeast(MIN_FIT_SCALE) + } + + private fun Float.constrained(min: Float, max: Float): Float { + var value = this + if (!max.isNaN()) value = minOf(value, max) + if (!min.isNaN()) value = maxOf(value, min) + return value } - private fun resolveSize(size: PolySize, parentSize: Float, node: PolyNode, horizontal: Boolean): Float { + private fun resolveSize(size: PolySize, parentSize: Float, node: PolyNode, horizontal: Boolean, fillParent: Boolean = true): Float { return when (size) { is PolySize.Px -> size.value is PolySize.Percent -> parentSize * size.value @@ -72,7 +98,7 @@ public object PolyLayoutEngine { val margin = if (horizontal) cs.margin.horizontal else cs.margin.vertical content = if (sums) content + extent + margin else maxOf(content, extent + margin) } - if (hasFill && content == 0f) return (parentSize).coerceAtLeast(padding) + if (hasFill && content == 0f) return if (fillParent) parentSize.coerceAtLeast(padding) else padding val gaps = if (n > 1) gap * (n - 1) else 0f if (sums) content += gaps content + padding @@ -89,8 +115,8 @@ public object PolyLayoutEngine { val ny = node.y val cx = nx + pad.left val cy = ny + pad.top - val cw = (node.width - pad.horizontal).coerceAtLeast(0f) - val ch = (node.height - pad.vertical).coerceAtLeast(0f) + val cw = (node.width - pad.horizontal).coerceAtLeast(0f) / node.contentScale + val ch = (node.height - pad.vertical).coerceAtLeast(0f) / node.contentScale when (s.layoutType) { LayoutType.Row -> { diff --git a/core/src/main/kotlin/org/polyfrost/compose/layout/PolyLayoutStyle.kt b/core/src/main/kotlin/org/polyfrost/compose/layout/PolyLayoutStyle.kt index fbe85ca..8c4ed29 100644 --- a/core/src/main/kotlin/org/polyfrost/compose/layout/PolyLayoutStyle.kt +++ b/core/src/main/kotlin/org/polyfrost/compose/layout/PolyLayoutStyle.kt @@ -13,6 +13,16 @@ public class PolyLayoutStyle { set(value) { if (field != value) { beforeLayoutChange(); field = value } } public var height: PolySize = PolySize.Wrap set(value) { if (field != value) { beforeLayoutChange(); field = value } } + public var minWidth: Float = Float.NaN + set(value) { if (!field.equals(value)) { beforeLayoutChange(); field = value } } + public var maxWidth: Float = Float.NaN + set(value) { if (!field.equals(value)) { beforeLayoutChange(); field = value } } + public var minHeight: Float = Float.NaN + set(value) { if (!field.equals(value)) { beforeLayoutChange(); field = value } } + public var maxHeight: Float = Float.NaN + set(value) { if (!field.equals(value)) { beforeLayoutChange(); field = value } } + public var fitContent: Boolean = false + set(value) { if (field != value) { beforeLayoutChange(); field = value } } public var align: PolyAlign = PolyAlign.TopLeft set(value) { if (field != value) { beforeLayoutChange(); field = value } } public var padding: PolyInsets = PolyInsets() @@ -40,6 +50,11 @@ public class PolyLayoutStyle { private var laidWidth: PolySize = PolySize.Wrap private var laidHeight: PolySize = PolySize.Wrap + private var laidMinWidth: Float = Float.NaN + private var laidMaxWidth: Float = Float.NaN + private var laidMinHeight: Float = Float.NaN + private var laidMaxHeight: Float = Float.NaN + private var laidFitContent: Boolean = false private var laidAlign: PolyAlign = PolyAlign.TopLeft private var laidPadding: PolyInsets = padding private var laidMargin: PolyInsets = margin @@ -55,6 +70,11 @@ public class PolyLayoutStyle { pending = true laidWidth = width laidHeight = height + laidMinWidth = minWidth + laidMaxWidth = maxWidth + laidMinHeight = minHeight + laidMaxHeight = maxHeight + laidFitContent = fitContent laidAlign = align laidPadding = padding laidMargin = margin @@ -67,7 +87,8 @@ public class PolyLayoutStyle { } internal fun layoutDiffers(): Boolean = - laidWidth != width || laidHeight != height || laidAlign != align || laidPadding != padding || + laidWidth != width || laidHeight != height || !laidMinWidth.equals(minWidth) || !laidMaxWidth.equals(maxWidth) || + !laidMinHeight.equals(minHeight) || !laidMaxHeight.equals(maxHeight) || laidFitContent != fitContent || laidAlign != align || laidPadding != padding || laidMargin != margin || laidPositionMode != positionMode || laidX != x || laidY != y || laidGap != gap || laidLayoutType != layoutType } diff --git a/core/src/main/kotlin/org/polyfrost/compose/node/CanvasNode.kt b/core/src/main/kotlin/org/polyfrost/compose/node/CanvasNode.kt index a661b88..128a847 100644 --- a/core/src/main/kotlin/org/polyfrost/compose/node/CanvasNode.kt +++ b/core/src/main/kotlin/org/polyfrost/compose/node/CanvasNode.kt @@ -14,8 +14,8 @@ public class CanvasNode : PolyNode() { val pad = style.padding val x = x + pad.left val y = y + pad.top - val w = (width - pad.horizontal).coerceAtLeast(0f) - val h = (height - pad.vertical).coerceAtLeast(0f) + val w = (width - pad.horizontal).coerceAtLeast(0f) / contentScale + val h = (height - pad.vertical).coerceAtLeast(0f) / contentScale if (!live) return draw(ctx, x, y, w, h) ctx.renderer.live { draw(liveContext(it), x, y, w, h) } } diff --git a/core/src/main/kotlin/org/polyfrost/compose/node/ImageNode.kt b/core/src/main/kotlin/org/polyfrost/compose/node/ImageNode.kt index 62bc516..47cfdd5 100644 --- a/core/src/main/kotlin/org/polyfrost/compose/node/ImageNode.kt +++ b/core/src/main/kotlin/org/polyfrost/compose/node/ImageNode.kt @@ -23,6 +23,9 @@ public class ImageNode( override fun renderContent(ctx: RenderContext) { val pad = style.padding - ctx.image(image, x + pad.left, y + pad.top, (width - pad.horizontal).coerceAtLeast(0f), (height - pad.vertical).coerceAtLeast(0f)) + val natural = style.fitContent + val w = if (natural) intrinsicWidth else (width - pad.horizontal).coerceAtLeast(0f) + val h = if (natural) intrinsicHeight else (height - pad.vertical).coerceAtLeast(0f) + ctx.image(image, x + pad.left, y + pad.top, w, h) } } diff --git a/core/src/main/kotlin/org/polyfrost/compose/node/PolyNode.kt b/core/src/main/kotlin/org/polyfrost/compose/node/PolyNode.kt index 1f5b63e..405afc0 100644 --- a/core/src/main/kotlin/org/polyfrost/compose/node/PolyNode.kt +++ b/core/src/main/kotlin/org/polyfrost/compose/node/PolyNode.kt @@ -13,6 +13,7 @@ public open class PolyNode { public var y: Float = 0f public var width: Float = 0f public var height: Float = 0f + public var contentScale: Float = 1f internal var intrinsicWidth: Float = -1f internal var intrinsicHeight: Float = -1f @@ -99,6 +100,15 @@ public open class PolyNode { val r = s.radius if (r > 0f) ctx.clipRRect(x, y, width, height, r) else ctx.clipRect(x, y, width, height) } + val scaled = contentScale != 1f + if (scaled) { + val px = x + s.padding.left + val py = y + s.padding.top + ctx.save() + ctx.translate(px, py) + ctx.scale(contentScale, contentScale) + ctx.translate(-px, -py) + } renderContent(ctx) val kids = children for (i in kids.indices) { @@ -126,6 +136,7 @@ public open class PolyNode { } } } + if (scaled) ctx.restore() if (clip) ctx.restore() } diff --git a/core/src/test/kotlin/org/polyfrost/compose/layout/PolyLayoutEngineTest.kt b/core/src/test/kotlin/org/polyfrost/compose/layout/PolyLayoutEngineTest.kt new file mode 100644 index 0000000..4791046 --- /dev/null +++ b/core/src/test/kotlin/org/polyfrost/compose/layout/PolyLayoutEngineTest.kt @@ -0,0 +1,50 @@ +package org.polyfrost.compose.layout + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Test +import org.polyfrost.compose.node.PolyNode + +class PolyLayoutEngineTest { + private fun node(w: PolySize, h: PolySize, vararg kids: PolyNode): PolyNode = PolyNode().apply { + style.width = w + style.height = h + for (k in kids) { + children.add(k) + k.parent = this + } + } + + @Test + fun defaultBoundsDoNotReportLayoutChange() { + assertFalse(PolyLayoutStyle().layoutDiffers()) + } + + @Test + fun fitContentScalesOverflowInFixedBox() { + val box = node(PolySize.Px(100f), PolySize.Px(100f), node(PolySize.Px(200f), PolySize.Px(50f))) + box.style.fitContent = true + PolyLayoutEngine.layout(box, 500f, 500f) + assertEquals(100f, box.width) + assertEquals(0.5f, box.contentScale) + } + + @Test + fun fitContentScalesWrapClampedByMax() { + val box = node(PolySize.Wrap, PolySize.Wrap, node(PolySize.Px(200f), PolySize.Px(50f))) + box.style.maxWidth = 100f + box.style.fitContent = true + PolyLayoutEngine.layout(box, 500f, 500f) + assertEquals(100f, box.width) + assertEquals(50f, box.height) + assertEquals(0.5f, box.contentScale) + } + + @Test + fun fitContentKeepsScaleWhenContentFits() { + val box = node(PolySize.Px(100f), PolySize.Px(100f), node(PolySize.Px(40f), PolySize.Px(40f))) + box.style.fitContent = true + PolyLayoutEngine.layout(box, 500f, 500f) + assertEquals(1f, box.contentScale) + } +}