diff options
author | Appability <appable@icloud.com> | 2022-10-21 23:33:52 -0700 |
---|---|---|
committer | Appability <appable@icloud.com> | 2022-10-21 23:33:52 -0700 |
commit | 4f25e7948c7e85151a80c17f7d2b25b72675cecf (patch) | |
tree | 7e152610a0f80f40f5524a231a157ea81ffab0e3 /src/main/kotlin/com/ambientaddons/gui/GuiElement.kt | |
parent | 1c3359e42f38012dae182e75200cc1f364dcda8f (diff) | |
download | AmbientAddons-4f25e7948c7e85151a80c17f7d2b25b72675cecf.tar.gz AmbientAddons-4f25e7948c7e85151a80c17f7d2b25b72675cecf.tar.bz2 AmbientAddons-4f25e7948c7e85151a80c17f7d2b25b72675cecf.zip |
moving gui stuff (attempt 1)
Diffstat (limited to 'src/main/kotlin/com/ambientaddons/gui/GuiElement.kt')
-rw-r--r-- | src/main/kotlin/com/ambientaddons/gui/GuiElement.kt | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/main/kotlin/com/ambientaddons/gui/GuiElement.kt b/src/main/kotlin/com/ambientaddons/gui/GuiElement.kt new file mode 100644 index 0000000..c398464 --- /dev/null +++ b/src/main/kotlin/com/ambientaddons/gui/GuiElement.kt @@ -0,0 +1,44 @@ +package com.ambientaddons.gui + +import AmbientAddons.Companion.persistentData +import com.ambientaddons.utils.render.OverlayUtils +import gg.essential.universal.UResolution +import net.minecraft.client.renderer.GlStateManager +import java.awt.Color + + +class GuiElement(val name: String, private val width: Int, private val height: Int) { + var position = persistentData.positions[name] ?: run { + persistentData.positions[name] = GuiPosition(0.0, 0.0, 1.0) + persistentData.positions[name]!! + } + + fun isInsideElement(mouseX: Double, mouseY: Double): Boolean { + val renderWidth = width * position.scale + val renderHeight = height * position.scale + val xInside = mouseX in (position.x - padding * renderWidth)..(position.x + renderWidth * (1 + padding)) + val yInside = mouseY in (position.y - padding * renderHeight)..(position.y + renderHeight * (1 + padding)) + return xInside && yInside + } + + fun coerceIntoScreen() { + val xRange = 0.0..(UResolution.scaledWidth - width * position.scale) + val yRange = 0.0..(UResolution.scaledHeight - height * position.scale) + position.x = if (xRange.isEmpty()) 0.0 else position.x.coerceIn(xRange) + position.y = if (yRange.isEmpty()) 0.0 else position.y.coerceIn(yRange) + } + + fun draw(mouseX: Double, mouseY: Double) { + GlStateManager.pushMatrix() + val renderWidth = width * position.scale + val renderHeight = height * position.scale + GlStateManager.translate(position.x - padding * renderWidth, position.y - padding * renderWidth, 400.0) + val color = if (isInsideElement(mouseX, mouseY)) Color(255, 255, 255, 128) else Color(128, 128, 128, 128) + OverlayUtils.renderRect(0.0, 0.0, renderWidth * (1 + padding * 2), renderHeight * (1 + padding * 2), color) + GlStateManager.popMatrix() + } + + companion object { + private const val padding = 0.05 + } +}
\ No newline at end of file |