Skip to content

Commit

Permalink
add loading image to resourcify assets
Browse files Browse the repository at this point in the history
  • Loading branch information
DeDiamondPro committed Oct 23, 2024
1 parent efb9ac2 commit 0929454
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/main/kotlin/dev/dediamondpro/resourcify/elements/Icon.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is part of Resourcify
* Copyright (C) 2023 DeDiamondPro
* Copyright (C) 2023-2024 DeDiamondPro
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand All @@ -18,7 +18,7 @@
package dev.dediamondpro.resourcify.elements

import dev.dediamondpro.resourcify.util.EmptyImage
import dev.dediamondpro.resourcify.util.supplyAsync
import dev.dediamondpro.resourcify.util.supply
import gg.essential.elementa.components.UIContainer
import gg.essential.elementa.components.UIImage
import gg.essential.elementa.constraints.ColorConstraint
Expand All @@ -32,7 +32,7 @@ class Icon(icon: String, shadow: Boolean, color: ColorConstraint) : UIContainer(
init {
(if (shadow) ShadowImage(icon, color)
else UIImage(
supplyAsync { ImageIO.read(this::class.java.getResourceAsStream(icon)) },
supply { ImageIO.read(this::class.java.getResourceAsStream(icon)) },
EmptyImage, EmptyImage
)).constrain {
x = 0.pixels()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import dev.dediamondpro.minemark.elements.ChildMovingElement
import dev.dediamondpro.minemark.elements.Element
import dev.dediamondpro.minemark.utils.MouseButton
import dev.dediamondpro.resourcify.util.Utils
import dev.dediamondpro.resourcify.util.ofResourceCustom
import gg.essential.elementa.components.UIImage
import gg.essential.universal.UMatrixStack
import org.xml.sax.Attributes
Expand Down Expand Up @@ -65,8 +66,8 @@ class SummaryElement(
}

companion object {
private val closedImage = UIImage.ofResource("/assets/resourcify/expandable-closed.png")
private val openedImage = UIImage.ofResource("/assets/resourcify/expandable-opened.png")
private val closedImage = UIImage.ofResourceCustom("/assets/resourcify/expandable-closed.png", loadSync = true)
private val openedImage = UIImage.ofResourceCustom("/assets/resourcify/expandable-opened.png", loadSync = true)
private val iconColor = Color.WHITE
private val iconShadowColor = Utils.getShadowColor(iconColor)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import dev.dediamondpro.resourcify.services.IService
import dev.dediamondpro.resourcify.services.ProjectType
import dev.dediamondpro.resourcify.services.ServiceRegistry
import dev.dediamondpro.resourcify.util.localize
import dev.dediamondpro.resourcify.util.ofResourceCustom
import dev.dediamondpro.resourcify.util.supplyAsync
import dev.dediamondpro.resourcify.util.toURI
import gg.essential.elementa.components.*
Expand All @@ -42,6 +43,7 @@ import gg.essential.universal.UDesktop
import gg.essential.universal.UMatrixStack
import gg.essential.universal.UMinecraft
import net.minecraft.client.gui.GuiScreen
import scala.swing.Applet.UI
import java.awt.Color
import java.io.File
import java.util.concurrent.CompletableFuture
Expand Down Expand Up @@ -271,7 +273,7 @@ class BrowseScreen(
if (it.mouseButton != 0 || guiOpenedTime + 500 > UMinecraft.getTime()) return@onMouseClick
UDesktop.browse(adProvider.getUrl().toURI())
}
UIImage.ofResource(adProvider.getImagePath()).constrain {
UIImage.ofResourceCustom(adProvider.getImagePath()).constrain {
x = 4.pixels()
y = 4.pixels()
width = 21.pixels()
Expand All @@ -282,7 +284,12 @@ class BrowseScreen(
y = CenterConstraint()
width = 100.percent() - 33.pixels()
} childOf adBox
UIImage.ofResource("/assets/resourcify/advertisement-text.png").constrain {
UIImage.ofResourceCustom(
"/assets/resourcify/advertisement-text.png",
loadSync = true,
minFilter = UIImage.TextureScalingMode.NEAREST,
magFilter = UIImage.TextureScalingMode.NEAREST
).constrain {
x = 1.pixels(alignOpposite = true)
y = 1.pixels(alignOpposite = true)
width = 58.pixels()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import dev.dediamondpro.resourcify.services.IProject
import dev.dediamondpro.resourcify.services.IService
import dev.dediamondpro.resourcify.services.ProjectType
import dev.dediamondpro.resourcify.util.localize
import dev.dediamondpro.resourcify.util.ofResourceCustom
import dev.dediamondpro.resourcify.util.ofURL
import gg.essential.elementa.components.*
import gg.essential.elementa.constraints.CenterConstraint
Expand Down Expand Up @@ -78,7 +79,7 @@ class ResourceCard(service: IService, project: IProject, type: ProjectType, down

val iconUrl = project.getIconUrl()
if (iconUrl.isNullOrBlank()) {
UIImage.ofResource("/assets/resourcify/pack.png")
UIImage.ofResourceCustom("/assets/resourcify/pack.png")
} else {
UIImage.ofURL(iconUrl)
}.constrain {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class ProjectScreen(
height = ImageAspectConstraint()
} childOf sideBox
val iconUrl = project.getIconUrl()
(if (iconUrl.isNullOrBlank()) UIImage.ofResource("/assets/resourcify/pack.png")
(if (iconUrl.isNullOrBlank()) UIImage.ofResourceCustom("/assets/resourcify/pack.png")
else UIImage.ofURL(iconUrl))
.constrain {
x = 4.pixels()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import dev.dediamondpro.resourcify.constraints.ChildLocationSizeConstraint
import dev.dediamondpro.resourcify.gui.ConfirmLinkScreen
import dev.dediamondpro.resourcify.gui.projectpage.components.VersionCard
import dev.dediamondpro.resourcify.services.IVersion
import dev.dediamondpro.resourcify.util.ImageURLUtils
import dev.dediamondpro.resourcify.util.localize
import dev.dediamondpro.resourcify.util.markdown
import dev.dediamondpro.resourcify.util.ofURL
import dev.dediamondpro.resourcify.util.*
import gg.essential.elementa.components.*
import gg.essential.elementa.constraints.ChildBasedMaxSizeConstraint
import gg.essential.elementa.constraints.ChildBasedSizeConstraint
Expand Down Expand Up @@ -129,7 +126,7 @@ class VersionsPage(private val screen: ProjectScreen) : UIContainer() {
} childOf changeLogHolder
val iconUrl = project.getIconUrl()
if (iconUrl.isNullOrBlank()) {
UIImage.ofResource("/assets/resourcify/pack.png")
UIImage.ofResourceCustom("/assets/resourcify/pack.png")
} else {
UIImage.ofURL(
iconUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class UpdateCard(

val iconUrl = project.getIconUrl()
if (iconUrl.isNullOrBlank()) {
UIImage.ofResource("/assets/resourcify/pack.png")
UIImage.ofResourceCustom("/assets/resourcify/pack.png")
} else {
UIImage.ofURL(iconUrl)
}.constrain {
Expand Down
63 changes: 61 additions & 2 deletions src/main/kotlin/dev/dediamondpro/resourcify/util/ElementaUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ import dev.dediamondpro.resourcify.elements.markdown.ExpandableMarkdownElement
import dev.dediamondpro.resourcify.elements.markdown.SummaryElement
import gg.essential.elementa.UIComponent
import gg.essential.elementa.components.UIImage
import gg.essential.elementa.components.image.DefaultLoadingImage
import gg.essential.elementa.components.image.ImageProvider
import gg.essential.universal.UGraphics
import gg.essential.universal.UMatrixStack
import gg.essential.universal.UResolution
import gg.essential.universal.utils.ReleasedDynamicTexture
import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension
import org.commonmark.ext.gfm.tables.TablesExtension
import java.awt.Color
import java.awt.image.BufferedImage
import java.util.concurrent.CompletableFuture
import javax.imageio.ImageIO
import kotlin.math.min

fun UIImage.Companion.ofURL(
source: String,
Expand All @@ -53,7 +59,7 @@ fun UIImage.Companion.ofURL(
height = height?.times(scaleFactor),
fit = fit
),
loadingImage = if (loadingImage) DefaultLoadingImage else EmptyImage
loadingImage = if (loadingImage) ElementaUtils.ElementLoadingImage else EmptyImage
)
if (!loadingImage) image.imageHeight = 0.5625f
if (width != null) image.imageWidth = width * scaleFactor
Expand All @@ -63,6 +69,28 @@ fun UIImage.Companion.ofURL(
return image
}

fun UIImage.Companion.ofResourceCustom(
path: String,
loadSync: Boolean = false,
loadingImage: Boolean = true,
minFilter: UIImage.TextureScalingMode = UIImage.TextureScalingMode.LINEAR,
magFilter: UIImage.TextureScalingMode = UIImage.TextureScalingMode.LINEAR,
): UIImage {
val imageFuture = if (loadSync) {
supply { ImageIO.read(this::class.java.getResourceAsStream(path)) }
} else {
supplyAsync { ImageIO.read(this::class.java.getResourceAsStream(path)) }
}
val image = UIImage(
imageFuture,
loadingImage = if (loadingImage) ElementaUtils.ElementLoadingImage else EmptyImage
)
if (!loadingImage) image.imageHeight = 0.5625f
image.textureMinFilter = minFilter
image.textureMagFilter = magFilter
return image
}

fun UIComponent.isHidden(): Boolean = !parent.children.contains(this)

fun markdown(
Expand All @@ -88,4 +116,35 @@ object ElementaUtils {
Color(65, 105, 225), ConfirmingBrowserProvider
)
)

object ElementLoadingImage : ImageProvider {
private var image: BufferedImage? =
ImageIO.read(this.javaClass.getResourceAsStream("/assets/resourcify/elementa-loading.png"))
private lateinit var texture: ReleasedDynamicTexture

override fun drawImage(
matrixStack: UMatrixStack,
x: Double,
y: Double,
width: Double,
height: Double,
color: Color
) {
if (!::texture.isInitialized) {
texture = UGraphics.getTexture(image)
image = null
}

// Get square size
val size = min(width, height)
val centerX = x + width / 2.0 - size / 2.0
val centerY = y + height / 2.0 - size / 2.0

Utils.drawTexture(
matrixStack, texture,
centerX, centerY, 0.0, 0.0,
size, size, size, size
)
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0929454

Please sign in to comment.