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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
17 changes: 17 additions & 0 deletions core/api/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -1295,6 +1310,7 @@ public class org/polyfrost/compose/node/PolyNode {
public static final field $stable I
public fun <init> ()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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -16,35 +17,60 @@ 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
val provisionalW = ((if (wrapWidth) parentWidth else node.width) - padH).coerceAtLeast(0f)
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
Expand Down Expand Up @@ -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
Expand All @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions core/src/main/kotlin/org/polyfrost/compose/node/CanvasNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/kotlin/org/polyfrost/compose/node/ImageNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
11 changes: 11 additions & 0 deletions core/src/main/kotlin/org/polyfrost/compose/node/PolyNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -126,6 +136,7 @@ public open class PolyNode {
}
}
}
if (scaled) ctx.restore()
if (clip) ctx.restore()
}

Expand Down
Loading