Skip to content

Commit

Permalink
image contain
Browse files Browse the repository at this point in the history
  • Loading branch information
Hobbyshop committed May 14, 2024
1 parent 2c69a3e commit b00f2a6
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/main/kotlin/com/neptuneclient/voidui/widgets/Image.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.neptuneclient.voidui.objects.CornerRadius
import com.neptuneclient.voidui.rendering.RenderObject
import com.neptuneclient.voidui.rendering.Renderer
import com.neptuneclient.voidui.utils.ImageBuffer
import kotlin.math.min

/**
* A widget which renders an image to the screen.
Expand Down Expand Up @@ -37,6 +38,7 @@ class Image(

override fun layout(constraints: BoxConstraints) {
if (imageSize != null) {
src.size = imageSize
size = constraints.constrain(imageSize)
return
}
Expand Down Expand Up @@ -81,12 +83,30 @@ class Image(
val overflow = src.size.height - size.height
Offset(0f, -overflow / 2f)
}
} else if (fit == ImageFit.CONTAIN) {
imageOffset = if (src.size.width > src.size.height) {
val overflow = size.height - src.size.height
Offset(0f, overflow / 2f)
} else {
val overflow = size.width - src.size.width
Offset(overflow / 2f, 0f)
}
}
super.postLayoutInit(parentOffset, parent)
}

override fun createRenderObject(): RenderObject? {
return ImageRenderObject(offset, size, src, radius, imageOffset)
val s = Size(
width = min(src.size.width, size.width),
height = min(src.size.height, size.height)
)
return ImageRenderObject(
if (fit == ImageFit.CONTAIN) offset + imageOffset else offset,
s,
src,
radius,
if (fit == ImageFit.COVER) imageOffset else Offset.zero
)
}

override fun remove() {
Expand Down

0 comments on commit b00f2a6

Please sign in to comment.