Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Commit

Permalink
[enhancement] Hud element scaling (#1817)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luna5ama authored Jan 7, 2021
1 parent 14ff66c commit f49ca78
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 50 deletions.
15 changes: 15 additions & 0 deletions src/main/java/me/zeroeightsix/kami/gui/hudgui/HudElement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import me.zeroeightsix.kami.gui.rgui.windows.BasicWindow
import me.zeroeightsix.kami.module.modules.client.GuiColors
import me.zeroeightsix.kami.module.modules.client.Hud
import me.zeroeightsix.kami.setting.GuiConfig
import me.zeroeightsix.kami.setting.GuiConfig.setting
import me.zeroeightsix.kami.util.graphics.RenderUtils2D
import me.zeroeightsix.kami.util.graphics.VertexHelper
import me.zeroeightsix.kami.util.graphics.font.FontRenderAdapter
import me.zeroeightsix.kami.util.math.Vec2d
import me.zeroeightsix.kami.util.math.Vec2f
import me.zeroeightsix.kami.util.threads.safeListener
import net.minecraftforge.fml.common.gameevent.TickEvent
import org.kamiblue.commons.interfaces.DisplayEnum
import org.lwjgl.opengl.GL11.glScalef

open class HudElement(
name: String,
Expand All @@ -22,8 +25,19 @@ open class HudElement(
enabledByDefault: Boolean = false
) : BasicWindow(name, 20.0f, 20.0f, 100.0f, 50.0f, SettingGroup.HUD_GUI) {

val scale by setting("Scale", 1.0f, 0.1f..4.0f, 0.05f)

override val resizable = false

final override val minWidth: Float get() = FontRenderAdapter.getFontHeight() * scale * 2.0f
final override val minHeight: Float get() = FontRenderAdapter.getFontHeight() * scale

final override val maxWidth: Float get() = hudWidth * scale
final override val maxHeight: Float get() = hudHeight * scale

open val hudWidth: Float get() = 20f
open val hudHeight: Float get() = 10f

val settingList get() = GuiConfig.getGroupOrPut(SettingGroup.HUD_GUI.groupName).getGroupOrPut(originalName).getSettings()

init {
Expand All @@ -50,6 +64,7 @@ open class HudElement(

final override fun onRender(vertexHelper: VertexHelper, absolutePos: Vec2f) {
renderFrame(vertexHelper)
glScalef(scale, scale, scale)
renderHud(vertexHelper)
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/me/zeroeightsix/kami/gui/hudgui/KamiHudGui.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ object KamiHudGui : AbstractKamiGui<HudSettingWindow, HudElement>() {
private fun renderHudElement(vertexHelper: VertexHelper, window: HudElement) {
glPushMatrix()
glTranslatef(window.renderPosX, window.renderPosY, 0.0f)

if (Hud.hudFrame) window.renderFrame(vertexHelper)

glScalef(window.scale, window.scale, window.scale)
window.renderHud(vertexHelper)

glPopMatrix()
}

Expand Down
7 changes: 2 additions & 5 deletions src/main/java/me/zeroeightsix/kami/gui/hudgui/LabelHud.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package me.zeroeightsix.kami.gui.hudgui

import me.zeroeightsix.kami.event.SafeClientEvent
import me.zeroeightsix.kami.util.graphics.VertexHelper
import me.zeroeightsix.kami.util.graphics.font.FontRenderAdapter
import me.zeroeightsix.kami.util.graphics.font.TextComponent
import me.zeroeightsix.kami.util.math.Vec2d
import me.zeroeightsix.kami.util.threads.safeAsyncListener
Expand All @@ -17,10 +16,8 @@ abstract class LabelHud(
enabledByDefault: Boolean = false
) : HudElement(name, alias, category, description, alwaysListening, enabledByDefault) {

override val minWidth: Float get() = FontRenderAdapter.getFontHeight()
override val minHeight: Float get() = FontRenderAdapter.getFontHeight()
override val maxWidth: Float get() = displayText.getWidth() + 2.0f
override val maxHeight: Float get() = displayText.getHeight(2)
override val hudWidth: Float get() = displayText.getWidth() + 2.0f
override val hudHeight: Float get() = displayText.getHeight(2)

protected val displayText = TextComponent()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ object BaritoneProcess : LabelHud(
description = "Shows what Baritone is doing"
) {

override val minWidth = 10f
override val minHeight = 10f

override fun SafeClientEvent.updateText() {
val process = BaritoneUtils.primary?.pathingControlManager?.mostRecentInControl() ?: return
if (!process.isPresent) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ object ModuleList : HudElement(
CATEGORY("Category", compareBy { it.category.ordinal })
}

override val maxWidth: Float
override val hudWidth: Float
get() = sortedModuleList.maxOfOrNull {
if (toggleMap[it]?.value == true) it.textLine.getWidth() + 4.0f
else 20.0f
}?.let {
max(it, 20.0f)
} ?: 20.0f

override val maxHeight: Float
override val hudHeight: Float
get() = max(toggleMap.values.sumByFloat { it.displayHeight }, 20.0f)

private var sortedModuleList : Collection<Module> = ModuleManager.modules
Expand Down Expand Up @@ -94,9 +94,9 @@ object ModuleList : HudElement(
super.renderHud(vertexHelper)
GlStateManager.pushMatrix()

GlStateManager.translate(renderWidth * dockingH.multiplier, 0.0f, 0.0f)
GlStateManager.translate(renderWidth / scale * dockingH.multiplier, 0.0f, 0.0f)
if (dockingV == VAlign.BOTTOM) {
GlStateManager.translate(0.0f, renderHeight - (FontRenderAdapter.getFontHeight() + 2.0f), 0.0f)
GlStateManager.translate(0.0f, renderHeight / scale - (FontRenderAdapter.getFontHeight() + 2.0f), 0.0f)
}

drawModuleList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import me.zeroeightsix.kami.KamiMod
import me.zeroeightsix.kami.event.SafeClientEvent
import me.zeroeightsix.kami.gui.hudgui.LabelHud
import me.zeroeightsix.kami.module.modules.client.Capes
import me.zeroeightsix.kami.util.graphics.VertexHelper
import org.lwjgl.opengl.GL11.glScalef

object WaterMark : LabelHud(
name = "Watermark",
Expand All @@ -12,6 +14,9 @@ object WaterMark : LabelHud(
enabledByDefault = true
) {

override val hudWidth: Float get() = (displayText.getWidth() + 2.0f) / scale
override val hudHeight: Float get() = displayText.getHeight(2) / scale

override val closeable: Boolean get() = Capes.isPremium

override fun onGuiInit() {
Expand All @@ -24,6 +29,12 @@ object WaterMark : LabelHud(
displayText.add(KamiMod.VERSION_SIMPLE, secondaryColor)
}

override fun renderHud(vertexHelper: VertexHelper) {
val reversedScale = 1.0f / scale
glScalef(reversedScale, reversedScale, reversedScale)
super.renderHud(vertexHelper)
}

init {
posX = 0.0f
posY = 0.0f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ object Armor : HudElement(
private val classic = setting("Classic", false)
private val armorCount = setting("ArmorCount", true)

override val maxWidth: Float
override val hudWidth: Float
get() = if (classic.value) {
80.0f
} else {
stringWidth
}

override val maxHeight: Float
override val hudHeight: Float
get() = if (classic.value) {
40.0f
} else {
Expand All @@ -58,9 +58,6 @@ object Armor : HudElement(
for ((index, item) in armorItems.withIndex()) {
armorCounts[index] = InventoryUtils.countItemAll(item)
}

width = maxWidth
height = maxHeight
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ object CombatItemCount : LabelHud(
ItemStack(Items.CHORUS_FRUIT, -1)
)

override val maxWidth: Float
override val hudWidth: Float
get() = if (showIcon) {
if (horizontal) 20.0f * itemSettings.keys.count { it.value }
else 20.0f
} else {
displayText.getWidth()
}

override val maxHeight: Float
override val hudHeight: Float
get() = if (showIcon) {
if (horizontal) 20.0f
else 20.0f * itemSettings.keys.count { it.value }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ object Time : LabelHud(
private val timeFormat = setting("TimeFormat", TimeUtils.TimeFormat.HHMM, { showTime.value })
private val timeUnit = setting("TimeUnit", TimeUtils.TimeUnit.H12, { showTime.value })

override val minWidth = 10f
override val minHeight = 10f

override fun SafeClientEvent.updateText() {
if (showDate.value) displayText.addLine(TimeUtils.getDate(dateFormat.value))
if (showTime.value) displayText.addLine(TimeUtils.getTime(timeFormat.value, timeUnit.value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ object InventoryViewer : HudElement(
private val containerTexture = ResourceLocation("textures/gui/container/inventory.png")
private val kamiIcon = ResourceLocation("kamiblue/kami_icon.png")

override val minWidth: Float = 162.0f
override val minHeight: Float = 54.0f
override val hudWidth: Float = 162.0f
override val hudHeight: Float = 54.0f

override fun renderHud(vertexHelper: VertexHelper) {
super.renderHud(vertexHelper)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,59 @@ import me.zeroeightsix.kami.setting.GuiConfig.setting
import me.zeroeightsix.kami.util.graphics.GlStateUtils
import me.zeroeightsix.kami.util.graphics.KamiTessellator
import me.zeroeightsix.kami.util.graphics.VertexHelper
import me.zeroeightsix.kami.util.threads.runSafe
import net.minecraft.client.gui.inventory.GuiInventory
import net.minecraft.client.renderer.GlStateManager
import net.minecraft.entity.EntityLivingBase
import net.minecraft.util.math.MathHelper
import org.lwjgl.opengl.GL11.*
import kotlin.math.roundToInt

object PlayerModel : HudElement(
name = "PlayerModel",
category = Category.PLAYER,
description = "Your player icon, or players you attacked"
) {
private val scale by setting("Size", 1.0f, 0.5f..2.0f, 0.05f)
private val resetDelay by setting("ResetDelay", 100, 0..200, 5)
private val emulatePitch by setting("EmulatePitch", true)
private val emulateYaw by setting("EmulateYaw", false)

override val minWidth: Float get() = adjust(114.0f)
override val minHeight: Float get() = adjust(204.0f)
override val hudWidth: Float get() = 50.0f
override val hudHeight: Float get() = 80.0f

override val resizable: Boolean = true

override fun renderHud(vertexHelper: VertexHelper) {
if (mc.player == null || mc.renderManager.renderViewEntity == null) return
super.renderHud(vertexHelper)
if (mc.renderManager.renderViewEntity == null) return

val attackedEntity = mc.player?.lastAttackedEntity
val entity = if (attackedEntity != null && mc.player.ticksExisted - mc.player.lastAttackedEntityTime <= resetDelay) {
attackedEntity
} else {
mc.player
super.renderHud(vertexHelper)
runSafe {
val attackedEntity: EntityLivingBase? = player.lastAttackedEntity
val entity = if (attackedEntity != null && player.ticksExisted - player.lastAttackedEntityTime <= resetDelay) {
attackedEntity
} else {
player
}

val yaw = if (emulateYaw) interpolateAndWrap(entity.prevRotationYaw, entity.rotationYaw) else 0.0f
val pitch = if (emulatePitch) interpolateAndWrap(entity.prevRotationPitch, entity.rotationPitch) else 0.0f

glPushMatrix()
glTranslatef(renderWidth / scale / 2.0f, renderHeight / scale - 8.0f, 0.0f)
GlStateUtils.depth(true)
glColor4f(1.0f, 1.0f, 1.0f, 1.0f)

GuiInventory.drawEntityOnScreen(0, 0, 35, -yaw, -pitch, entity)

glColor4f(1.0f, 1.0f, 1.0f, 1.0f)
GlStateUtils.depth(false)
GlStateUtils.texture2d(true)
GlStateUtils.blend(true)
GlStateManager.disableColorMaterial()
glPopMatrix()
}

val yaw = if (emulateYaw) interpolateAndWrap(entity.prevRotationYaw, entity.rotationYaw) else 0.0f
val pitch = if (emulatePitch) interpolateAndWrap(entity.prevRotationPitch, entity.rotationPitch) else 0.0f

glPushMatrix()
glTranslatef(width / 2, height - adjust(7.5f), 0f)
GlStateUtils.depth(true)
GuiInventory.drawEntityOnScreen(0, 0, (scale * 35.0f).roundToInt(), -yaw, -pitch, entity)
GlStateUtils.depth(false)
GlStateUtils.texture2d(true)
GlStateUtils.blend(true)
GlStateManager.disableColorMaterial()
glPopMatrix()
}

private fun interpolateAndWrap(prev: Float, current: Float): Float {
return MathHelper.wrapDegrees(prev + (current - prev) * KamiTessellator.pTicks())
}

private fun adjust(value: Float) = value * scale * 0.35f
}

0 comments on commit f49ca78

Please sign in to comment.