Skip to content

Commit

Permalink
hud safeguards, fix rendering issue (i think) and kotlinify
Browse files Browse the repository at this point in the history
  • Loading branch information
nextdayy committed Nov 4, 2024
1 parent cbf1133 commit 44aa8ad
Show file tree
Hide file tree
Showing 17 changed files with 214 additions and 339 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ kotlin = "2.0.20"
kotlinx-coroutines = "1.8.1"
kotlinx-atomicfu = "0.24.0"
fabric-language-kotlin = "1.12.2+kotlin.2.0.20"
polyui = "1.7.03"
polyui = "1.7.1"
annotations = "24.1.0"
hypixel-modapi = "1.0"
hypixel-data = "0.1.2" # Dep of hypixel-modapi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,18 @@ abstract class Hud<T : Drawable> : Cloneable, Config("null", null, "null", null)
val value = !new
// useless null-safety checks, but I don't want to risk dumb errors
val it = it ?: return
val siblings = it.parent.children ?: return
if (value == it.isEnabled) return

it.isEnabled = value

val parent = it._parent ?: return
val siblings = parent.children ?: return

if (siblings.size == 1) {
it.parent.isEnabled = value
parent.isEnabled = value
} else if (!value && siblings.fastAll { !it.renders }) {
it.parent.isEnabled = false
parent.isEnabled = false
}
it.parent.recalculate()
parent.recalculate()
}

/**
Expand All @@ -235,7 +237,7 @@ abstract class Hud<T : Drawable> : Cloneable, Config("null", null, "null", null)
*/
protected fun updateWhenChanged(optionName: String) {
if (isReal) addCallback(optionName) {
if (update()) get().parent.recalculate()
if (update()) get()._parent?.recalculate()
}
else LOGGER.warn("attempted to add callback to {}'s option '{}', but it is not real. no action has been taken.", title(), optionName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ abstract class TextHud(
if (isReal) {
addCallback("template") {
_formatter = null
if(update()) get().parent.recalculate()
if(update()) get()._parent?.recalculate()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ fun Hud<*>.build(): Block {
} else {
HudManager.polyUI.every(freq) {
if (update()) {
get().parent.recalculate()
get()._parent?.recalculate()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fun HudsPage(huds: Collection<Hud<*>>): Drawable {
if (!HudManager.panelExists) return@every
huds.forEach {
if (it.update()) {
it.get().parent.recalculate()
it.get()._parent?.recalculate()
}
}
}
Expand Down Expand Up @@ -155,7 +155,7 @@ private fun createDesigner(hud: Hud<*>): Drawable {
if (isLegacy) {
Text("oneconfig.hudeditor.cantedit.aslegacy").secondary()
} else {
if ((hud.get().parent.children?.size ?: 0) > 1) {
if ((hud.get()._parent?.children?.size ?: 0) > 1) {
Text("oneconfig.hudeditor.choosesomething").padded(3f, 3f).secondary()
} else {
when (hud.get()) {
Expand Down Expand Up @@ -238,7 +238,7 @@ private fun interactiveAlignment(hud: Hud<*>): Drawable {
.onDragStart {
px = polyUI.mouseX
py = polyUI.mouseY
hud.get().parent.let {
hud.get()._parent?.let {
it as Drawable
s0 = it.skewX
s1 = it.skewY
Expand All @@ -259,7 +259,7 @@ private fun interactiveAlignment(hud: Hud<*>): Drawable {
it.skewX = sx
it.skewY = sy
}
hud.get().parent.let {
hud.get()._parent?.let {
it as Drawable
it.skewX = sx
it.skewY = sy
Expand Down Expand Up @@ -320,7 +320,7 @@ fun textOptions(text: Text): Drawable {
2 -> mcFont.get(text.fontWeight, text.italic)
else -> polyUI.fonts.get(text.fontWeight, text.italic)
}
text.parent.recalculate()
text._parent?.recalculate()
val ex = (parent.parent[1][0] as? Text) ?: return@onChange false
ex.font = text.font
ex.parent.recalculate()
Expand All @@ -330,7 +330,7 @@ fun textOptions(text: Text): Drawable {
.apply {
(this[1][0] as TextInput).numeric(1f, 100f).on(Event.Change.Number) { (it) ->
text.fontSize = it.toFloat()
text.parent.recalculate()
text._parent?.recalculate()
val ex = (parent.parent.parent[1][0] as? Text) ?: return@on true
ex.fontSize = text.fontSize
ex.parent.recalculate()
Expand Down Expand Up @@ -358,7 +358,7 @@ fun textOptions(text: Text): Drawable {
"oneconfig.fweight.900"
).onChange { it: Int ->
text.fontWeight = Font.byWeight((it + 1) * 100)
text.parent.recalculate()
text._parent?.recalculate()
val ex = (parent.parent[1][0] as? Text) ?: return@onChange false
ex.fontWeight = text.fontWeight
ex.parent.recalculate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ object OneConfigUI {
private val playerHead = PolyImage(
"https://mc-heads.net/avatar/${Platform.player().playerName ?: "Steve"}/24",
type = PolyImage.Type.Raster,
).also {
it.size = (24f by 24f)
}
)
private val searchNoneFound = Text("oneconfig.search.nonefound", fontSize = 16f)
private val search = Group(searchNoneFound, visibleSize = Vec2(1130f, 635f))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
public final class OCPolyUIBuilder extends PolyUIBuilder {
private float desiredScreenWidth, desiredScreenHeight;
private Consumer<PolyUI> onClose;
private boolean pauses, blurs;

private OCPolyUIBuilder() {
Settings s = getSettings();
Expand All @@ -61,8 +62,26 @@ public OCPolyUIBuilder atResolution(float desiredScreenWidth, float desiredScree
return this;
}

public OCPolyUIBuilder pauses() {
return pauses(true);
}

public OCPolyUIBuilder pauses(boolean state) {
this.pauses = state;
return this;
}

public OCPolyUIBuilder blurs() {
return blurs(true);
}

public OCPolyUIBuilder blurs(boolean state) {
this.blurs = state;
return this;
}

public PolyUI make(Drawable... drawables) {
if (getRenderer() == null) setRenderer(UIManager.INSTANCE.getRenderer());
if (getRenderer() == null) renderer(UIManager.INSTANCE.getRenderer());
return super.make(drawables);
}

Expand All @@ -71,7 +90,7 @@ public PolyUI make(Drawable... drawables) {
*/
public PolyUI makeAndOpen(Drawable... drawables) {
PolyUI p = make(drawables);
Object screen = UIManager.INSTANCE.createPolyUIScreen(p, desiredScreenWidth, desiredScreenHeight, getPauses(), getBlurs(), onClose);
Object screen = UIManager.INSTANCE.createPolyUIScreen(p, desiredScreenWidth, desiredScreenHeight, pauses, blurs, onClose);
p.setWindow(UIManager.INSTANCE.createWindow());
Platform.screen().display(screen);
return p;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public interface NanoVgApi {

interface NanoVgConstants {
interface Constants {

int NVG_ROUND();

Expand All @@ -20,7 +20,7 @@ interface NanoVgConstants {

}

NanoVgConstants constants();
Constants constants();

long handle();
long svgHandle();
Expand Down Expand Up @@ -137,10 +137,21 @@ interface NanoVgConstants {

float[] svgBounds(long address);

Triple<Long, Float, Float> parseSvg(ByteBuffer data);
SVG parseSvg(ByteBuffer data);

void deleteSvg(long address);

void rasterizeSvg(long address, float x, float y, float scale, ByteBuffer data, int w, int h, int stride);


final class SVG {
public final long address;
public final float width, height;

public SVG(long address, float width, float height) {
this.address = address;
this.width = width;
this.height = height;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ object NotificationsManager {
@JvmOverloads
fun enqueue(type: Type, title: String = type.title, description: String, durationNanos: Long) = enqueueCustom(
Block(
Image("polyui/chevron-down.svg".image(Vec2(32f, 32f))),
Image("polyui/chevron-down.svg".image(), size = Vec2(32f, 32f)),
Text(title, fontSize = 14f).setFont { medium },
Text(description, fontSize = 12f),
size = Vec2(235f, 100f),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ package org.polyfrost.oneconfig.api.ui.v1.internal

import org.lwjgl.system.MemoryUtil
import org.polyfrost.oneconfig.api.ui.v1.api.LwjglApi
import java.nio.ByteBuffer

class LwjglImpl : LwjglApi {

override fun memAlloc(size: Int): ByteBuffer {
return MemoryUtil.memAlloc(size)
}

override fun memAlloc(size: Int) = MemoryUtil.memAlloc(size)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,28 @@ class NanoVgImpl(

}

object NanoVgConstantsImpl : NanoVgApi.NanoVgConstants {
object ConstantsImpl : NanoVgApi.Constants {

override fun NVG_ROUND(): Int {
return NanoVG.NVG_ROUND
}
override fun NVG_ROUND() = NanoVG.NVG_ROUND

override fun NVG_ALIGN_LEFT(): Int {
return NanoVG.NVG_ALIGN_LEFT
}
override fun NVG_ALIGN_LEFT() = NanoVG.NVG_ALIGN_LEFT

override fun NVG_ALIGN_TOP(): Int {
return NanoVG.NVG_ALIGN_TOP
}
override fun NVG_ALIGN_TOP() = NanoVG.NVG_ALIGN_TOP

override fun NVG_HOLE(): Int {
return NanoVG.NVG_HOLE
}
override fun NVG_HOLE() = NanoVG.NVG_HOLE

override fun NVG_IMAGE_FLIPY(): Int {
return NanoVG.NVG_IMAGE_FLIPY
}
override fun NVG_IMAGE_FLIPY() = NanoVG.NVG_IMAGE_FLIPY

}

private var handle: Long = -1
private var svgHandle: Long = -1

override fun constants(): NanoVgApi.NanoVgConstants {
return NanoVgConstantsImpl
}
override fun constants() = ConstantsImpl

override fun handle(): Long {
return handle
}
override fun handle() = handle

override fun svgHandle(): Long {
return svgHandle
}
override fun svgHandle() = svgHandle

override fun maybeSetup() {
if (handle == -1L) {
Expand Down Expand Up @@ -131,21 +115,15 @@ class NanoVgImpl(
NanoVG.nvgRestore(handle)
}

override fun createPaint(): Long {
return NVGPaint.malloc().address()
}
override fun createPaint() = NVGPaint.malloc().address()

override fun fillPaint(address: Long) {
NanoVG.nvgFillPaint(handle, NVGPaint.create(address))
}

override fun getPaintColor(address: Long): Long {
return NVGPaint.create(address).innerColor().address()
}
override fun getPaintColor(address: Long) = NVGPaint.create(address).innerColor().address()

override fun createColor(): Long {
return NVGColor.malloc().address()
}
override fun createColor() = NVGColor.malloc().address()

override fun fillColor(address: Long) {
NanoVG.nvgFillColor(handle, NVGColor.create(address))
Expand Down Expand Up @@ -306,9 +284,9 @@ class NanoVgImpl(
return floatArrayOf(svg.width(), svg.height())
}

override fun parseSvg(data: ByteBuffer): Triple<Long, Float, Float> {
override fun parseSvg(data: ByteBuffer): NanoVgApi.SVG {
val result = NanoSVG.nsvgParse(data, PIXELS, 96f) ?: throw IllegalStateException("Failed to parse SVG data")
return Triple(result.address(), result.width(), result.height())
return NanoVgApi.SVG(result.address(), result.width(), result.height())
}

override fun deleteSvg(address: Long) {
Expand Down
Loading

0 comments on commit 44aa8ad

Please sign in to comment.