diff --git a/src/main/kotlin/dev/luna5ama/trollhack/gui/AbstractTrollGui.kt b/src/main/kotlin/dev/luna5ama/trollhack/gui/AbstractTrollGui.kt index 27161a9..818a67e 100644 --- a/src/main/kotlin/dev/luna5ama/trollhack/gui/AbstractTrollGui.kt +++ b/src/main/kotlin/dev/luna5ama/trollhack/gui/AbstractTrollGui.kt @@ -9,6 +9,7 @@ import dev.luna5ama.trollhack.graphics.* import dev.luna5ama.trollhack.graphics.color.ColorRGB import dev.luna5ama.trollhack.graphics.font.renderer.MainFontRenderer import dev.luna5ama.trollhack.graphics.shaders.ParticleShader +import dev.luna5ama.trollhack.gui.IGuiScreen.Companion.forEachWindow import dev.luna5ama.trollhack.gui.rgui.MouseState import dev.luna5ama.trollhack.gui.rgui.WindowComponent import dev.luna5ama.trollhack.gui.rgui.windows.ListWindow @@ -17,6 +18,7 @@ import dev.luna5ama.trollhack.util.Wrapper import dev.luna5ama.trollhack.util.accessor.listShaders import dev.luna5ama.trollhack.util.math.vector.Vec2f import dev.luna5ama.trollhack.util.state.TimedFlag +import dev.luna5ama.trollhack.util.threads.runSynchronized import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.launch @@ -39,7 +41,7 @@ abstract class AbstractTrollGui : GuiScreen(), IListenerOwner by ListenerOwner() // Window override val windows = ObjectLinkedOpenHashSet() - private val windowsCachedList = FastObjectArrayList() + override val windowsCachedList = FastObjectArrayList() override var lastClicked: WindowComponent? = null override var hovered: WindowComponent? = null @@ -192,7 +194,7 @@ abstract class AbstractTrollGui : GuiScreen(), IListenerOwner by ListenerOwner() lastClicked = hovered lastClicked?.let { - windows.addAndMoveToLast(it) + windows.runSynchronized { addAndMoveToLast(it) } } } @@ -202,7 +204,7 @@ abstract class AbstractTrollGui : GuiScreen(), IListenerOwner by ListenerOwner() mouseState = MouseState.NONE lastClicked?.let { - windows.addAndMoveToLast(it) + windows.runSynchronized { addAndMoveToLast(it) } } } @@ -214,7 +216,7 @@ abstract class AbstractTrollGui : GuiScreen(), IListenerOwner by ListenerOwner() hovered?.onDrag(mousePos, lastClickPos, clickedMouseButton) lastClicked?.let { - windows.addAndMoveToLast(it) + windows.runSynchronized { addAndMoveToLast(it) } } } // End of mouse input @@ -264,13 +266,13 @@ abstract class AbstractTrollGui : GuiScreen(), IListenerOwner by ListenerOwner() mc.profiler.endStartSection("windows") GlStateUtils.rescaleTroll() - GlStateManager.translate(0.0f, -(Resolution.trollHeightF * (1.0f - multiplier)), 0.0f) + GlStateManager.translate(0.0f, -(Resolution.trollHeightF * (1.0f - multiplier)), 0.0f) drawWindows() drawTypedString() mc.profiler.endStartSection("post") GlStateUtils.rescaleMc() - GlStateManager.translate(0.0f, -(scaledResolution.scaledHeight * (1.0f - multiplier)), 0.0f) + GlStateManager.translate(0.0f, -(scaledResolution.scaledHeight * (1.0f - multiplier)), 0.0f) glDisable(GL_DEPTH_CLAMP) GlStateManager.tryBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO) @@ -328,26 +330,19 @@ abstract class AbstractTrollGui : GuiScreen(), IListenerOwner by ListenerOwner() forEachWindow { if (!it.visible) return@forEachWindow GlStateManager.pushMatrix() - GlStateManager.translate(it.renderPosX, it.renderPosY, 0.0f) + GlStateManager.translate(it.renderPosX, it.renderPosY, 0.0f) renderBlock(it) GlStateManager.popMatrix() } } - private inline fun forEachWindow(crossinline block: (WindowComponent) -> Unit) { - windowsCachedList.addAll(windows) - for (i in windowsCachedList.indices) { - block(windowsCachedList[i]) - } - windowsCachedList.clear() - } - private fun drawTypedString() { if (searchString.isNotBlank() && System.currentTimeMillis() - renderStringPosX.time <= 5000L) { val posX = Resolution.trollWidthF / 2.0f - renderStringPosX.get() / 2.0f val posY = Resolution.trollHeightF / 2.0f - MainFontRenderer.getHeight(2.0f) / 2.0f var color = GuiSetting.text - color = color.alpha(Easing.IN_CUBIC.dec(Easing.toDelta(renderStringPosX.time, 5000.0f), 0.0f, 255.0f).toInt()) + color = + color.alpha(Easing.IN_CUBIC.dec(Easing.toDelta(renderStringPosX.time, 5000.0f), 0.0f, 255.0f).toInt()) MainFontRenderer.drawString(searchString, posX, posY, color, 2.0f) } } diff --git a/src/main/kotlin/dev/luna5ama/trollhack/gui/IGuiScreen.kt b/src/main/kotlin/dev/luna5ama/trollhack/gui/IGuiScreen.kt index 66d3677..23daecc 100644 --- a/src/main/kotlin/dev/luna5ama/trollhack/gui/IGuiScreen.kt +++ b/src/main/kotlin/dev/luna5ama/trollhack/gui/IGuiScreen.kt @@ -1,8 +1,10 @@ package dev.luna5ama.trollhack.gui +import dev.fastmc.common.collection.FastObjectArrayList import dev.luna5ama.trollhack.gui.rgui.MouseState import dev.luna5ama.trollhack.gui.rgui.WindowComponent import dev.luna5ama.trollhack.util.math.vector.Vec2f +import dev.luna5ama.trollhack.util.threads.runSynchronized import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet interface IGuiScreen { @@ -16,7 +18,7 @@ interface IGuiScreen { fun closeWindow(window: WindowComponent): Boolean { var closed = false - if (windows.remove(window)) { + if (windows.runSynchronized { remove(window) }) { window.onClosed() closed = true } @@ -26,11 +28,23 @@ interface IGuiScreen { } fun displayWindow(window: WindowComponent): Boolean { - return if (windows.addAndMoveToLast(window)) { + return if (windows.runSynchronized { addAndMoveToLast(window) }) { window.onDisplayed() true } else { false } } + + val windowsCachedList: FastObjectArrayList + + companion object { + inline fun IGuiScreen.forEachWindow(crossinline block: (WindowComponent) -> Unit) { + windows.runSynchronized { windowsCachedList.addAll(this) } + for (i in windowsCachedList.indices) { + block(windowsCachedList[i]) + } + windowsCachedList.clear() + } + } } \ No newline at end of file diff --git a/src/main/kotlin/dev/luna5ama/trollhack/gui/clickgui/TrollClickGui.kt b/src/main/kotlin/dev/luna5ama/trollhack/gui/clickgui/TrollClickGui.kt index 7ba61bc..3cf2b27 100644 --- a/src/main/kotlin/dev/luna5ama/trollhack/gui/clickgui/TrollClickGui.kt +++ b/src/main/kotlin/dev/luna5ama/trollhack/gui/clickgui/TrollClickGui.kt @@ -10,6 +10,7 @@ import dev.luna5ama.trollhack.module.Category import dev.luna5ama.trollhack.module.ModuleManager import dev.luna5ama.trollhack.module.modules.client.ClickGUI import dev.luna5ama.trollhack.util.extension.remove +import dev.luna5ama.trollhack.util.threads.runSynchronized import org.lwjgl.input.Keyboard object TrollClickGui : AbstractTrollGui() { @@ -55,7 +56,7 @@ object TrollClickGui : AbstractTrollGui() { } } - windows.addAll(moduleWindows.values) + windows.runSynchronized { addAll(moduleWindows.values) } } override fun onGuiClosed() { diff --git a/src/main/kotlin/dev/luna5ama/trollhack/gui/hudgui/TrollHudGui.kt b/src/main/kotlin/dev/luna5ama/trollhack/gui/hudgui/TrollHudGui.kt index da4fdb2..6c62162 100644 --- a/src/main/kotlin/dev/luna5ama/trollhack/gui/hudgui/TrollHudGui.kt +++ b/src/main/kotlin/dev/luna5ama/trollhack/gui/hudgui/TrollHudGui.kt @@ -7,6 +7,7 @@ import dev.luna5ama.trollhack.event.listener import dev.luna5ama.trollhack.graphics.GlStateUtils import dev.luna5ama.trollhack.graphics.Resolution import dev.luna5ama.trollhack.gui.AbstractTrollGui +import dev.luna5ama.trollhack.gui.IGuiScreen.Companion.forEachWindow import dev.luna5ama.trollhack.gui.hudgui.component.HudButton import dev.luna5ama.trollhack.gui.rgui.Component import dev.luna5ama.trollhack.gui.rgui.windows.ListWindow @@ -14,6 +15,7 @@ import dev.luna5ama.trollhack.module.modules.client.Hud import dev.luna5ama.trollhack.module.modules.client.HudEditor import dev.luna5ama.trollhack.util.extension.remove import dev.luna5ama.trollhack.util.extension.rootName +import dev.luna5ama.trollhack.util.threads.runSynchronized import net.minecraft.client.renderer.GlStateManager import org.lwjgl.input.Keyboard import org.lwjgl.opengl.GL11.* @@ -59,14 +61,14 @@ object TrollHudGui : AbstractTrollGui() { } - windows.addAll(hudWindows.values) + windows.runSynchronized { addAll(hudWindows.values) } listener { if (!it.state || it.key == Keyboard.KEY_NONE || Keyboard.isKeyDown(Keyboard.KEY_F3)) return@listener - for (child in windows) { - if (child !is AbstractHudElement) continue - if (!child.bind.isDown(it.key)) continue + forEachWindow { child -> + if (child !is AbstractHudElement) return@forEachWindow + if (!child.bind.isDown(it.key)) return@forEachWindow child.visible = !child.visible } } @@ -75,12 +77,12 @@ object TrollHudGui : AbstractTrollGui() { internal fun register(hudElement: AbstractHudElement) { val button = HudButton(this, hudElement) hudWindows[hudElement.category]!!.children.add(button) - windows.addAndMoveToLast(hudElement) + windows.runSynchronized { addAndMoveToLast(hudElement) } } internal fun unregister(hudElement: AbstractHudElement) { hudWindows[hudElement.category]!!.children.removeIf { it is HudButton && it.hudElement == hudElement } - windows.remove(hudElement) + windows.runSynchronized { remove(hudElement) } } override fun onGuiClosed() { @@ -110,8 +112,8 @@ object TrollHudGui : AbstractTrollGui() { if (Hud.isEnabled) { GlStateManager.tryBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE) - for (window in windows) { - if (window !is AbstractHudElement || !window.visible) continue + forEachWindow { window -> + if (window !is AbstractHudElement || !window.visible) return@forEachWindow mc.profiler.startSection(window.rootName) renderHudElement(window) mc.profiler.endSection() diff --git a/src/main/kotlin/dev/luna5ama/trollhack/gui/rgui/WindowComponent.kt b/src/main/kotlin/dev/luna5ama/trollhack/gui/rgui/WindowComponent.kt index 058ad45..b553995 100644 --- a/src/main/kotlin/dev/luna5ama/trollhack/gui/rgui/WindowComponent.kt +++ b/src/main/kotlin/dev/luna5ama/trollhack/gui/rgui/WindowComponent.kt @@ -9,6 +9,7 @@ import dev.luna5ama.trollhack.setting.configs.AbstractConfig import dev.luna5ama.trollhack.util.delegate.FrameFloat import dev.luna5ama.trollhack.util.interfaces.Nameable import dev.luna5ama.trollhack.util.math.vector.Vec2f +import dev.luna5ama.trollhack.util.threads.runSynchronized import kotlin.math.max import kotlin.math.min @@ -89,13 +90,14 @@ open class WindowComponent( if (minimizable && prevState != MouseState.DRAG && buttonId == 1 - && mousePos.y - posY < draggableHeight) minimized = !minimized + && mousePos.y - posY < draggableHeight + ) minimized = !minimized if (mouseState != MouseState.DRAG) { updatePreDrag(mousePos.minus(posX, posY)) } - if (dockingOverlay in screen.windows) { + if (screen.windows.runSynchronized { contains(dockingOverlay) }) { dockingOverlay.onRelease(mousePos, clickPos, buttonId) } } @@ -130,11 +132,12 @@ open class WindowComponent( else -> null } - val centerSplitterVCenter = if (draggableHeight != height && horizontalSide == dev.luna5ama.trollhack.graphics.HAlign.CENTER) { - 2.5 - } else { - min(15.0, preDragSize.x / 3.0) - } + val centerSplitterVCenter = + if (draggableHeight != height && horizontalSide == dev.luna5ama.trollhack.graphics.HAlign.CENTER) { + 2.5 + } else { + min(15.0, preDragSize.x / 3.0) + } val verticalSide = when (relativeClickPos.y) { in -2.0..centerSplitterVCenter -> dev.luna5ama.trollhack.graphics.VAlign.TOP