Skip to content

Commit 0204fca

Browse files
committed
fix HUD resizing issues !!
1 parent 982822e commit 0204fca

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name=OneConfig
33
mod_id=oneconfig
44
version_major=1
55
version_minor=0
6-
version_patch=0-alpha.38
6+
version_patch=0-alpha.39
77

88
polyfrost.defaults.loom=3
99

modules/hud/src/main/kotlin/org/polyfrost/oneconfig/api/hud/v1/HudManager.kt

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,33 @@ object HudManager {
114114
@ApiStatus.Internal
115115
fun initialize() {
116116
polyUI.translator.addDelegate("assets/oneconfig/hud")
117+
LOGGER.info("Initializing HUD...")
118+
val now = System.nanoTime()
117119
Runtime.getRuntime().addShutdownHook(Thread {
118-
ConfigManager.internal().folder.resolve("hudLock.lock").writeText(polyUI.size.value.toString())
120+
ConfigManager.internal().folder.resolve("size.lock").writeText(polyUI.size.value.toString())
119121
})
120-
val sizeFile = ConfigManager.internal().folder.resolve("hudLock.lock")
121-
val size = Vec2(if (sizeFile.exists()) sizeFile.readText().toLong() else 0L)
122-
// todo size stuff (upstream in polyui) AHHH
122+
val sizeFile = ConfigManager.internal().folder.resolve("size.lock")
123+
val size = Vec2(if (sizeFile.exists()) sizeFile.readText().toLongOrNull() ?: 0L else 0L)
124+
val prevSize: Vec2
125+
if (size.isPositive) {
126+
prevSize = polyUI.size
127+
polyUI.resize(size.x, size.y)
128+
} else {
129+
LOGGER.warn("Failed to read previous size from size.lock: HUD positions may be inaccurate. If this is first start, you may ignore this message.")
130+
prevSize = Vec2.ZERO
131+
}
123132

124133
// todo use for inspections
125134
// it.master.onClick { (x, y) ->
126135
// val obj = polyUI.inputManager.rayCheckUnsafe(this, x, y) ?: return@onClick false
127136
// return@onClick false
128137
// }
129-
val used = HashSet<Class<out Hud<out Drawable>>>(hudProviders.size)
138+
val used = HashSet<Class<Hud<Drawable>>>(hudProviders.size)
139+
var i = 0
130140
ConfigManager.active().gatherAll("huds").forEach { data ->
131141
try {
132142
val clsName = data.getProp("hudClass").get() as? String ?: throw IllegalArgumentException("hud tree ${data.id} is missing class name, will be ignored")
133-
val cls = Class.forName(clsName) as? Class<Hud<out Drawable>> ?: throw IllegalArgumentException("hud class $clsName is not a subclass of org.polyfrost.oneconfig.api.v1.hud.Hud, will be ignored")
143+
val cls = Class.forName(clsName) as? Class<Hud<Drawable>> ?: throw IllegalArgumentException("hud class $clsName is not a subclass of org.polyfrost.oneconfig.api.v1.hud.Hud, will be ignored")
134144
// asm: the documentation of Hud states that code should not be run in the constructor
135145
// so, we are fine to (potentially) malloc the HUD here
136146
// note that this is stored in a map separate to the loaded hud list.
@@ -140,15 +150,17 @@ object HudManager {
140150
val hud = h.make(data)
141151
val theHud = hud.build()
142152
polyUI.master.addChild(theHud, recalculate = false)
143-
LOGGER.info("Loaded HUD ${hud.title()} from ${data.id}")
144153
val x: Float = data.getProp("x").getAs()
145154
val y: Float = data.getProp("y").getAs()
146155
theHud.x = x - (hud.get().x - theHud.x)
147156
theHud.y = y - (hud.get().y - theHud.y)
157+
i++
148158
} catch (e: Exception) {
149159
LOGGER.error("Failed to load HUD from ${data.id}", e)
150160
}
151161
}
162+
if (prevSize.isPositive) polyUI.resize(prevSize.x, prevSize.y)
163+
LOGGER.info("successfully loaded {} HUDs from {} providers", i, hudProviders.size)
152164
hudProviders.forEach { (cls, h) ->
153165
if (cls in used) return@forEach
154166
val default = h.defaultPosition()
@@ -158,8 +170,9 @@ object HudManager {
158170
theHud.x = default.x
159171
theHud.y = default.y
160172
polyUI.master.addChild(theHud, recalculate = false)
161-
LOGGER.info("Adding HUD {} to {} (default)", hud.title(), default)
173+
LOGGER.info("Added HUD {} to {} (default)", hud.title(), default)
162174
}
175+
LOGGER.info("HUD load took {}ms", (System.nanoTime() - now) / 1_000_000.0)
163176
}
164177

165178
@ApiStatus.Internal

0 commit comments

Comments
 (0)