diff options
Diffstat (limited to 'src/main/kotlin/com/ambientaddons/gui')
-rw-r--r-- | src/main/kotlin/com/ambientaddons/gui/GuiElement.kt | 44 | ||||
-rw-r--r-- | src/main/kotlin/com/ambientaddons/gui/GuiPosition.kt | 10 | ||||
-rw-r--r-- | src/main/kotlin/com/ambientaddons/gui/MoveGui.kt | 77 |
3 files changed, 131 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 diff --git a/src/main/kotlin/com/ambientaddons/gui/GuiPosition.kt b/src/main/kotlin/com/ambientaddons/gui/GuiPosition.kt new file mode 100644 index 0000000..50b3f07 --- /dev/null +++ b/src/main/kotlin/com/ambientaddons/gui/GuiPosition.kt @@ -0,0 +1,10 @@ +package com.ambientaddons.gui + +import kotlinx.serialization.Serializable + +@Serializable +data class GuiPosition ( + var x: Double, + var y: Double, + var scale: Double +)
\ No newline at end of file diff --git a/src/main/kotlin/com/ambientaddons/gui/MoveGui.kt b/src/main/kotlin/com/ambientaddons/gui/MoveGui.kt new file mode 100644 index 0000000..33c96e7 --- /dev/null +++ b/src/main/kotlin/com/ambientaddons/gui/MoveGui.kt @@ -0,0 +1,77 @@ +package com.ambientaddons.gui + +import AmbientAddons.Companion.guiElements +import AmbientAddons.Companion.persistentData +import com.ambientaddons.utils.Alignment +import com.ambientaddons.utils.dungeon.TextStyle +import com.ambientaddons.utils.render.OverlayUtils +import gg.essential.universal.UResolution +import net.minecraft.client.gui.GuiScreen +import net.minecraft.client.renderer.GlStateManager +import org.lwjgl.input.Mouse +import org.lwjgl.opengl.Display +import java.awt.Color + +class MoveGui : GuiScreen() { + private var currentElement: GuiElement? = null + private var clickOffsetX = 0.0 + private var clickOffsetY = 0.0 + + override fun drawScreen(x: Int, y: Int, partialTicks: Float) { + super.drawScreen(x, y, partialTicks) + val (mouseX, mouseY) = getMouseCoordinates() + OverlayUtils.renderRect( + 0.0, 0.0, UResolution.scaledWidth.toDouble(), UResolution.scaledHeight.toDouble(), Color(0, 0, 0, 64) + ) + GlStateManager.pushMatrix() + GlStateManager.translate(UResolution.scaledWidth / 2.0, UResolution.scaledHeight / 2.0, 300.0) + OverlayUtils.drawString(0, 10, "Drag to move GUI elements.", TextStyle.Outline, Alignment.Center) + OverlayUtils.drawString(0, 20, "Scroll inside elements to scale.", TextStyle.Outline, Alignment.Center) + GlStateManager.popMatrix() + guiElements.forEach { it.draw(mouseX, mouseY) } + } + + override fun mouseClicked(x: Int, y: Int, mouseButton: Int) { + val (mouseX, mouseY) = getMouseCoordinates() + currentElement = guiElements.find { it.isInsideElement(mouseX, mouseY) }?.apply { + clickOffsetX = mouseX - position.x + clickOffsetY = mouseY - position.y + } + super.mouseClicked(x, y, mouseButton) + } + + override fun mouseClickMove(x: Int, y: Int, clickedMouseButton: Int, timeSinceLastClick: Long) { + currentElement?.apply { + val (mouseX, mouseY) = getMouseCoordinates() + position.x = mouseX - clickOffsetX + position.y = mouseY - clickOffsetY + coerceIntoScreen() + } + super.mouseClickMove(x, y, clickedMouseButton, timeSinceLastClick) + } + + override fun handleMouseInput() { + super.handleMouseInput() + val (mouseX, mouseY) = getMouseCoordinates() + currentElement = guiElements.find { it.isInsideElement(mouseX, mouseY) }?.apply { + clickOffsetX = mouseX - position.x + clickOffsetY = mouseY - position.y + val scrollAmount = Mouse.getEventDWheel() + val oldScale = position.scale + val newScale = (position.scale + scrollAmount / 7200.0).coerceAtLeast(0.1) + position.x = mouseX + (newScale / oldScale) * (position.x - mouseX) + position.y = mouseY + (newScale / oldScale) * (position.y - mouseY) + position.scale = newScale + coerceIntoScreen() + } + } + + private fun getMouseCoordinates(): Pair<Double, Double> { + val mouseX = Mouse.getX() / UResolution.scaleFactor + val mouseY = (Display.getHeight() - Mouse.getY()) / UResolution.scaleFactor + return Pair(mouseX, mouseY) + } + + override fun onGuiClosed() = persistentData.save() + +}
\ No newline at end of file |