Skip to content

Commit

Permalink
feat: ClickGUI cache option (#4505)
Browse files Browse the repository at this point in the history
ClickGUI will now open without animation and delay.
  • Loading branch information
1zun4 authored Nov 15, 2024
1 parent 0997edb commit 2d70fc0
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,22 @@
*/
package net.ccbluex.liquidbounce.features.module.modules.render

import com.mojang.blaze3d.systems.RenderSystem
import net.ccbluex.liquidbounce.event.EventManager
import net.ccbluex.liquidbounce.event.events.BrowserReadyEvent
import net.ccbluex.liquidbounce.event.events.ClickGuiScaleChangeEvent
import net.ccbluex.liquidbounce.event.events.GameRenderEvent
import net.ccbluex.liquidbounce.event.handler
import net.ccbluex.liquidbounce.features.module.Category
import net.ccbluex.liquidbounce.features.module.Module
import net.ccbluex.liquidbounce.integration.VirtualScreenType
import net.ccbluex.liquidbounce.integration.VrScreen
import net.ccbluex.liquidbounce.integration.browser.supports.tab.ITab
import net.ccbluex.liquidbounce.integration.theme.ThemeManager
import net.ccbluex.liquidbounce.utils.client.asText
import net.ccbluex.liquidbounce.utils.kotlin.EventPriorityConvention
import net.minecraft.client.gui.DrawContext
import net.minecraft.client.gui.screen.Screen
import org.lwjgl.glfw.GLFW

/**
Expand All @@ -40,17 +50,87 @@ object ModuleClickGui :
EventManager.callEvent(ClickGuiScaleChangeEvent(it))
}

private val cache by boolean("Cache", true).onChanged { cache ->
RenderSystem.recordRenderCall {
if (cache) {
createView()
} else {
closeView()
}

if (mc.currentScreen is VrScreen || mc.currentScreen is ClickScreen) {
enable()
}
}
}

@Suppress("UnusedPrivateProperty")
private val searchBarAutoFocus by boolean("SearchBarAutoFocus", true)

private var clickGuiTab: ITab? = null

override fun enable() {
// Pretty sure we are not in a game, so we can't open the clickgui
if (mc.player == null || mc.world == null) {
return
}

mc.setScreen(VrScreen(VirtualScreenType.CLICK_GUI))
mc.setScreen(if (clickGuiTab == null) {
VrScreen(VirtualScreenType.CLICK_GUI)
} else {
ClickScreen()
})
super.enable()
}

@Suppress("unused")
private val browserReadyHandler = handler<BrowserReadyEvent>(
priority = EventPriorityConvention.OBJECTION_AGAINST_EVERYTHING,
ignoreCondition = true
) {
if (cache) {
createView()
}
}

private fun createView() {
if (clickGuiTab != null) {
return
}

clickGuiTab = ThemeManager.openInputAwareImmediate(VirtualScreenType.CLICK_GUI, true) {
mc.currentScreen is ClickScreen
}.preferOnTop()
}

private fun closeView() {
clickGuiTab?.closeTab()
clickGuiTab = null
}

@Suppress("unused")
private val gameRenderHandler = handler<GameRenderEvent>(
priority = EventPriorityConvention.OBJECTION_AGAINST_EVERYTHING,
ignoreCondition = true
) {
// A hack to prevent the clickgui from being drawn
if (mc.currentScreen !is ClickScreen) {
clickGuiTab?.drawn = true
}
}

/**
* An empty screen that acts as hint when to draw the clickgui
*/
class ClickScreen : Screen("ClickGUI".asText()) {
override fun render(context: DrawContext?, mouseX: Int, mouseY: Int, delta: Float) {
super.render(context, mouseX, mouseY, delta)
}

override fun shouldPause(): Boolean {
// preventing game pause
return false
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,15 @@ object ThemeManager : Configurable("theme") {
* Open [ITab] with the given [VirtualScreenType] and mark as static if [markAsStatic] is true.
* This tab will be locked to the highest refresh rate since it is input aware.
*/
fun openInputAwareImmediate(virtualScreenType: VirtualScreenType? = null, markAsStatic: Boolean = false): ITab =
BrowserManager.browser?.createInputAwareTab(route(virtualScreenType, markAsStatic).url, frameRate = refreshRate,
takesInput = takesInputHandler) ?: error("Browser is not initialized")
fun openInputAwareImmediate(
virtualScreenType: VirtualScreenType? = null,
markAsStatic: Boolean = false,
takesInput: () -> Boolean = takesInputHandler
): ITab = BrowserManager.browser?.createInputAwareTab(
route(virtualScreenType, markAsStatic).url,
frameRate = refreshRate,
takesInput = takesInput
) ?: error("Browser is not initialized")

fun updateImmediate(tab: ITab?, virtualScreenType: VirtualScreenType? = null, markAsStatic: Boolean = false) =
tab?.loadUrl(route(virtualScreenType, markAsStatic).url)
Expand Down

0 comments on commit 2d70fc0

Please sign in to comment.