Skip to content

Commit 32c0011

Browse files
committed
reshuffle internal HUD classes, remove init block from LegacyHud
1 parent 111d435 commit 32c0011

File tree

10 files changed

+603
-595
lines changed

10 files changed

+603
-595
lines changed

modules/hud/api/hud.api

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ public final class org/polyfrost/oneconfig/api/hud/v1/HudManager {
6565
public final fun getHudsOfType (Ljava/lang/Class;)Ljava/util/ArrayList;
6666
public final fun getPanel ()Lorg/polyfrost/polyui/component/Drawable;
6767
public final fun getProvider (Ljava/lang/Class;)Lorg/polyfrost/oneconfig/api/hud/v1/Hud;
68-
public final fun getSlinex ()F
69-
public final fun getSliney ()F
7068
public final fun getUseGuiScale ()Z
7169
public final fun getWithEditor ()Ldev/deftu/omnicore/api/client/screen/OmniScreen;
7270
public final fun initialize ()V
@@ -77,8 +75,6 @@ public final class org/polyfrost/oneconfig/api/hud/v1/HudManager {
7775
public static final fun register ([Lorg/polyfrost/oneconfig/api/hud/v1/Hud;)V
7876
public final fun removeHud (Lorg/polyfrost/oneconfig/api/hud/v1/Hud;Z)V
7977
public static synthetic fun removeHud$default (Lorg/polyfrost/oneconfig/api/hud/v1/HudManager;Lorg/polyfrost/oneconfig/api/hud/v1/Hud;ZILjava/lang/Object;)V
80-
public final fun setSlinex (F)V
81-
public final fun setSliney (F)V
8278
public final fun setUseGuiScale (Z)V
8379
public final fun toggle ()V
8480
public final fun toggleAllHuds (Lorg/polyfrost/oneconfig/api/hud/v1/Hud;Z)V

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

Lines changed: 9 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,15 @@ import org.polyfrost.oneconfig.api.config.v1.Properties.simple
3535
import org.polyfrost.oneconfig.api.config.v1.Tree
3636
import org.polyfrost.oneconfig.api.config.v1.annotations.Keybind
3737
import org.polyfrost.oneconfig.api.config.v1.annotations.Switch
38-
import org.polyfrost.oneconfig.api.config.v1.getProp
39-
import org.polyfrost.oneconfig.api.event.v1.events.HudEvent
40-
import org.polyfrost.oneconfig.api.event.v1.events.ScreenOpenEvent
41-
import org.polyfrost.oneconfig.api.event.v1.invoke.EventHandler
4238
import org.polyfrost.oneconfig.api.hud.v1.HudManager.LOGGER
43-
import org.polyfrost.oneconfig.api.ui.v1.keybind.OCKeybindHelper
39+
import org.polyfrost.oneconfig.api.hud.v1.internal.HudConfigVisualizer
4440
import org.polyfrost.polyui.color.PolyColor
4541
import org.polyfrost.polyui.component.Component
4642
import org.polyfrost.polyui.component.Drawable
4743
import org.polyfrost.polyui.component.impl.Block
48-
import org.polyfrost.polyui.component.impl.Text
4944
import org.polyfrost.polyui.input.PolyBind
5045
import org.polyfrost.polyui.unit.Vec2
5146
import org.polyfrost.polyui.utils.fastAll
52-
import org.polyfrost.polyui.utils.fastEachIndexed
53-
import java.util.function.Consumer
54-
import kotlin.experimental.and
55-
import kotlin.experimental.or
56-
import kotlin.io.path.exists
57-
import kotlin.random.Random
5847

5948
/**
6049
* HUD (Heads Up Display) is a component that is rendered on top of the screen. They are used for displaying information to the user, such as the time, or the player's health.
@@ -111,20 +100,20 @@ abstract class Hud<T : Drawable>(id: String, title: String, val category: Catego
111100
fun make(with: Tree? = null): Hud<T> {
112101
if (tree != null) throw IllegalArgumentException("HUD is already made, it cannot be made again")
113102
val out = if (multipleInstancesAllowed()) clone() else this
114-
val id = with?.id ?: genRid()
103+
val id = with?.id ?: HudConfigVisualizer.makeIDForHudTree(this)
115104
val tree = ConfigManager.collect(out, id)
116105
out.apply {
117106
tree.title = title
118107
tree.addMetadata("category", category)
119108
tree.addMetadata("hidden", true)
120-
val hud = get()
121-
hud.rawRescalePosition = true
122-
tree["x"] = ktProperty(hud::x)
123-
tree["y"] = ktProperty(hud::y)
124-
inspect(hud, tree)
109+
val hudComponent = get()
110+
hudComponent.rawRescalePosition = true
111+
tree["x"] = ktProperty(hudComponent::x)
112+
tree["y"] = ktProperty(hudComponent::y)
113+
HudConfigVisualizer.addHudConfigEntries(hudComponent, tree)
125114
addToSerialized(tree)
126115
tree["hudClass"] = simple(value = out::class.java.name)
127-
addHideHandlers(tree)
116+
HudConfigVisualizer.addDefaultCallbacks(out, tree)
128117
addCallbacks(tree)
129118
if (with == null) LOGGER.info("generated new HUD config for $title -> ${tree.id}")
130119
ConfigManager.active().register(tree)
@@ -138,92 +127,6 @@ abstract class Hud<T : Drawable>(id: String, title: String, val category: Catego
138127
* This method is called during config tree initialization. it is
139128
*/
140129
protected open fun addCallbacks(tree: Tree) {
141-
// initial
142-
tree.getProp<Boolean>("staticWidth")?.addCallback {
143-
bgUseSetSize = it
144-
false
145-
}
146-
}
147-
148-
@Suppress("UnstableApiUsage")
149-
private var bgUseSetSize: Boolean
150-
get() = (getBackground()?.createdWithSetSize ?: false)
151-
set(value) {
152-
getBackground()?.let {
153-
if (value) it.layoutFlags = it.layoutFlags or 0b00000010
154-
else it.layoutFlags = it.layoutFlags and 0b11111101.toByte()
155-
}
156-
}
157-
158-
private fun addHideHandlers(tree: Tree) {
159-
val hideHandler = Consumer<HudEvent> { (opened): HudEvent ->
160-
hidden = opened
161-
}
162-
val hideF3Handler = EventHandler.of(HudEvent.Debug::class.java, hideHandler)
163-
val hideTabHandler = EventHandler.of(HudEvent.Tab::class.java, hideHandler)
164-
val hideScreenHandler = EventHandler.of(ScreenOpenEvent::class.java) { (screen): ScreenOpenEvent ->
165-
hidden = screen != null
166-
// asm: don't hide when we open the hud editor
167-
// because otherwise you couldn't edit it (which sucks)
168-
if (HudManager.isEditing) hidden = false
169-
}
170-
tree.getProp<Boolean>("showInF3")?.addCallback {
171-
if (!it) hideF3Handler.register()
172-
else hideF3Handler?.unregister()
173-
false
174-
}
175-
tree.getProp<Boolean>("showInTab")?.addCallback {
176-
if (!it) hideTabHandler.register()
177-
else hideTabHandler?.unregister()
178-
false
179-
}
180-
tree.getProp<Boolean>("showInScreens")?.addCallback {
181-
if (!it) hideScreenHandler.register()
182-
else hideScreenHandler?.unregister()
183-
false
184-
}
185-
186-
showKey = OCKeybindHelper.builder().does { hidden = !it }.register()
187-
toggleKey = OCKeybindHelper.builder().does { if (it) hidden = !hidden }.register()
188-
}
189-
190-
private fun inspect(cmp: Component, tree: Tree) {
191-
if (cmp is Drawable) {
192-
tree["color"] = ktProperty(cmp::_color)
193-
}
194-
when (cmp) {
195-
is Block -> {
196-
tree["boarderColor"] = ktProperty(cmp::borderColor)
197-
tree["boarderWidth"] = ktProperty(cmp::borderWidth)
198-
}
199-
200-
is Text -> {
201-
tree["font"] = ktProperty(cmp::_font)
202-
tree["fontSize"] = ktProperty(cmp::uFontSize)
203-
}
204-
}
205-
cmp.children?.fastEachIndexed { i, it ->
206-
val child = Tree.tree("$i")
207-
inspect(it, child)
208-
tree.put(child)
209-
}
210-
}
211-
212-
private fun genRid(): String {
213-
val folder = ConfigManager.active().folder
214-
val init = "huds/${id}"
215-
if (!folder.resolve(init).exists()) return init
216-
var p = "huds/${Random.Default.nextInt(0, 100)}-${id}"
217-
var i = 0
218-
while (folder.resolve(p).exists()) {
219-
p = "huds/${Random.Default.nextInt(0, 999)}-${id}"
220-
when (i++) {
221-
100 -> LOGGER.warn("they all say that it gets better")
222-
500 -> LOGGER.warn("yeah they all say that it gets better;; it gets better the more you grow")
223-
999 -> throw IllegalStateException("... but what if i dont?")
224-
}
225-
}
226-
return p
227130
}
228131

229132
/**
@@ -286,22 +189,7 @@ abstract class Hud<T : Drawable>(id: String, title: String, val category: Catego
286189
*
287190
* this method will be called once, and once only.
288191
*/
289-
@MustBeInvokedByOverriders
290192
open fun setup() {
291-
if (isReal) {
292-
// asm: add all the background properties to the tree as well (we have to do it here because in make, the background is not be created yet)
293-
val cmp = getBackground() ?: get()
294-
tree["alignment"] = ktProperty(cmp::alignment)
295-
tree["alpha"] = ktProperty(cmp::alpha)
296-
tree["scaleX"] = ktProperty(cmp::scaleX)
297-
tree["scaleY"] = ktProperty(cmp::scaleY)
298-
tree["rotation"] = ktProperty(cmp::rotation)
299-
tree["skewX"] = ktProperty(cmp::skewX)
300-
tree["skewY"] = ktProperty(cmp::skewY)
301-
tree["padding"] = ktProperty(cmp::padding)
302-
303-
if (cmp is Block) tree["radii"] = ktProperty(cmp::radii)
304-
}
305193
}
306194

307195
/**
@@ -378,7 +266,7 @@ abstract class Hud<T : Drawable>(id: String, title: String, val category: Catego
378266
@JvmName("minimumSize")
379267
open fun minimumSize(): Vec2 = Vec2.ZERO
380268

381-
open fun remove() { }
269+
open fun remove() {}
382270

383271
/**
384272
* This method will create a new instance of the HUD. It is key to the functionality of the HUD system.

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

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ import org.polyfrost.oneconfig.api.config.v1.ConfigManager
3535
import org.polyfrost.oneconfig.api.event.v1.EventManager
3636
import org.polyfrost.oneconfig.api.event.v1.eventHandler
3737
import org.polyfrost.oneconfig.api.event.v1.events.ScreenOpenEvent
38+
import org.polyfrost.oneconfig.api.hud.v1.HudManager.getHudsOfType
39+
import org.polyfrost.oneconfig.api.hud.v1.HudManager.getProvider
40+
import org.polyfrost.oneconfig.api.hud.v1.HudManager.removeHud
41+
import org.polyfrost.oneconfig.api.hud.v1.HudManager.toggleAllHuds
42+
import org.polyfrost.oneconfig.api.hud.v1.HudManager.unregister
3843
import org.polyfrost.oneconfig.api.hud.v1.events.HudEditorToggleEvent
3944
import org.polyfrost.oneconfig.api.hud.v1.internal.*
4045
import org.polyfrost.oneconfig.api.platform.v1.Platform
@@ -66,24 +71,8 @@ import kotlin.io.path.writeText
6671
object HudManager {
6772
internal val LOGGER = LogManager.getLogger("OneConfig/HUD")
6873
private val hudProviders = HashMap<Class<out Hud<*>>, Hud<*>>()
69-
private val snapLineColor = rgba(170, 170, 170, 0.8f)
70-
7174
private var init = false
7275

73-
/**
74-
* the vertical line x position used for snapping.
75-
* Do not set this value.
76-
*/
77-
@ApiStatus.Internal
78-
var slinex = -1f
79-
80-
/**
81-
* the horizontal line y position used for snapping.
82-
* Do not set this value.
83-
*/
84-
@ApiStatus.Internal
85-
var sliney = -1f
86-
8776
/**
8877
* Whether the HUD editor panel is currently open, i.e. collapsed or expanded.
8978
*/
@@ -262,7 +251,7 @@ object HudManager {
262251
used.add(cls)
263252
val hud = h.make(data)
264253
activeInstances.add(hud)
265-
val bg = hud.build()
254+
val bg = HudFactory.build(hud)
266255
polyUI.master.addChild(bg, recalculate = false)
267256
val x = data.getProp("x")?.getAs<Number?>()?.toFloat() ?: 0f
268257
val y = data.getProp("y")?.getAs<Number?>()?.toFloat() ?: 0f
@@ -294,7 +283,7 @@ object HudManager {
294283
val default = h.defaultPosition()
295284
if (!default.isPositive) return@forEach
296285
val hud = h.make()
297-
val theHud = hud.build()
286+
val theHud = HudFactory.build(hud)
298287
theHud.x = default.x
299288
theHud.y = default.y
300289
activeInstances.add(hud)
@@ -310,7 +299,7 @@ object HudManager {
310299
if (useGuiScale) setAllHudsScaleFactor(OmniResolution.scaleFactor.toFloat())
311300
// in the case the screen crashes or doesn't exit cleanly, we force it to close here
312301
// as otherwise it can end up being stuck open. seems to be confined to modern only. see #505.
313-
if(screen == null && isEditing) editorClose()
302+
if (screen == null && isEditing) editorClose()
314303
}
315304

316305
LOGGER.info("HUD load took {}ms", (System.nanoTime() - now) / 1_000_000.0)
@@ -352,7 +341,7 @@ object HudManager {
352341
private fun editorClose() {
353342
toggleHudPicker()
354343
polyUI.unfocus()
355-
removeMenu()
344+
HudScreenOverlay.closeMenu()
356345
ConfigManager.active().saveAll()
357346
}
358347

@@ -362,7 +351,7 @@ object HudManager {
362351
@ApiStatus.Internal
363352
fun toggle() {
364353
panelOpen = !panelOpen
365-
removeMenu()
354+
HudScreenOverlay.closeMenu()
366355
val pg = panel
367356
if (!panelOpen) {
368357
Move(pg, polyUI.size.x - 32f, pg.y, false, Animations.Default.create(0.2.seconds)).add()
@@ -450,19 +439,10 @@ object HudManager {
450439
hudsPage,
451440
size = Vec2(500f, 1048f),
452441
alignment = Align(line = Align.Line.Start, pad = Vec2(0f, 16f)),
453-
).setPalette { page.bg }.withBorder().apply {
454-
addOperation {
455-
if (polyUI.mouseDown) {
456-
if (slinex != -1f) polyUI.renderer.line(slinex, 0f, slinex, polyUI.size.y, snapLineColor, 1f)
457-
if (sliney != -1f) polyUI.renderer.line(0f, sliney, polyUI.size.x, sliney, snapLineColor, 1f)
458-
} else {
459-
slinex = -1f
460-
sliney = -1f
461-
}
462-
}
463-
},
442+
).setPalette { page.bg }.withBorder(),
464443
size = Vec2(0f, 1080f)
465444
).also {
445+
HudScreenOverlay.addSnapLinesRenderer(it)
466446
it.rawRescalePosition = true
467447
it.rawRescaleSize = true
468448
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ abstract class LegacyHud(id: String, title: String, category: Category) : Hud<Dr
5454
abstract var width: Float
5555
abstract var height: Float
5656

57-
init {
58-
get()
59-
}
60-
6157
/**
6258
* Support for complex hybrid legacy HUDs with PolyUI children is currently experimental. Please report any issues you find.
6359
*/

0 commit comments

Comments
 (0)