diff --git a/src/main/kotlin/me/odinmain/clickgui/ClickGUI.kt b/src/main/kotlin/me/odinmain/clickgui/ClickGUI.kt index 6fc443409..49b54f375 100644 --- a/src/main/kotlin/me/odinmain/clickgui/ClickGUI.kt +++ b/src/main/kotlin/me/odinmain/clickgui/ClickGUI.kt @@ -13,6 +13,7 @@ import me.odinmain.utils.ui.mouseX import me.odinmain.utils.ui.mouseY import me.odinmain.utils.ui.rendering.NVGRenderer import net.minecraftforge.common.MinecraftForge +import me.odinmain.utils.ui.SearchBar import kotlin.math.sign /** @@ -25,6 +26,7 @@ import kotlin.math.sign * @see [Panel] */ object ClickGUI : Screen() { + val searchBar = SearchBar() private val panels: ArrayList = arrayListOf().apply { if (Category.entries.any { ClickGUIModule.panelSetting[it] == null }) ClickGUIModule.resetPositions() @@ -47,7 +49,7 @@ object ClickGUI : Screen() { } for (i in 0 until panels.size) { panels[i].draw(mouseX, mouseY) } - SearchBar.draw(mc.displayWidth / 2f - 175f, mc.displayHeight - 110f, mouseX, mouseY) + searchBar.draw(mc.displayWidth / 2f - 175f, mc.displayHeight - 110f, 350f, 40f, mouseX, mouseY) desc.render() NVGRenderer.endFrame() @@ -62,7 +64,7 @@ object ClickGUI : Screen() { } override fun mouseClicked(x: Int, y: Int, mouseButton: Int) { - SearchBar.mouseClicked(mouseX, mouseY, mouseButton) + searchBar.mouseClicked(mouseX, mouseY, mouseButton) for (i in panels.size - 1 downTo 0) { if (panels[i].mouseClicked(mouseX, mouseY, mouseButton)) return } @@ -70,7 +72,7 @@ object ClickGUI : Screen() { } override fun mouseReleased(mouseX: Int, mouseY: Int, state: Int) { - SearchBar.mouseReleased() + searchBar.mouseReleased() for (i in panels.size - 1 downTo 0) { panels[i].mouseReleased(state) } @@ -78,8 +80,8 @@ object ClickGUI : Screen() { } override fun keyTyped(typedChar: Char, keyCode: Int) { - val searchTyped = SearchBar.keyTyped(typedChar) - val searchPressed = SearchBar.keyPressed(keyCode) + val searchTyped = searchBar.keyTyped(typedChar) + val searchPressed = searchBar.keyPressed(keyCode) if (searchTyped || searchPressed) return for (i in panels.size - 1 downTo 0) { diff --git a/src/main/kotlin/me/odinmain/clickgui/Panel.kt b/src/main/kotlin/me/odinmain/clickgui/Panel.kt index dea0609ec..d70c6e31f 100644 --- a/src/main/kotlin/me/odinmain/clickgui/Panel.kt +++ b/src/main/kotlin/me/odinmain/clickgui/Panel.kt @@ -1,6 +1,7 @@ package me.odinmain.clickgui import me.odinmain.clickgui.ClickGUI.gray26 +import me.odinmain.clickgui.ClickGUI.searchBar import me.odinmain.clickgui.settings.ModuleButton import me.odinmain.features.Category import me.odinmain.features.ModuleManager @@ -21,7 +22,6 @@ import kotlin.math.floor * @see [ModuleButton] */ class Panel(private val category: Category) { - val moduleButtons: ArrayList = ArrayList().apply { ModuleManager.modules .filter { it.category == category && (!it.isDevModule || PlayerSize.isRandom) } @@ -55,7 +55,7 @@ class Panel(private val category: Category) { var startY = scrollOffset + HEIGHT if (panelSetting.extended) { for (button in moduleButtons) { - if (!button.module.name.contains(SearchBar.currentSearch, true)) continue + if (!button.module.name.contains(searchBar.currentSearch, true)) continue startY += button.draw(panelSetting.x, startY + panelSetting.y) } } @@ -84,7 +84,7 @@ class Panel(private val category: Category) { } } else if (isMouseOverExtended) { return moduleButtons.reversed().any { - if (!it.module.name.contains(SearchBar.currentSearch, true)) return@any false + if (!it.module.name.contains(searchBar.currentSearch, true)) return@any false it.mouseClicked(mouseX, mouseY, button) } } @@ -96,7 +96,7 @@ class Panel(private val category: Category) { if (panelSetting.extended) moduleButtons.reversed().forEach { - if (!it.module.name.contains(SearchBar.currentSearch, true)) return@forEach + if (!it.module.name.contains(searchBar.currentSearch, true)) return@forEach it.mouseReleased(state) } } @@ -105,7 +105,7 @@ class Panel(private val category: Category) { if (!panelSetting.extended) return false return moduleButtons.reversed().any { - if (!it.module.name.contains(SearchBar.currentSearch, true)) return@any false + if (!it.module.name.contains(searchBar.currentSearch, true)) return@any false it.keyTyped(typedChar) } } @@ -114,7 +114,7 @@ class Panel(private val category: Category) { if (!panelSetting.extended) return false return moduleButtons.reversed().any { - if (!it.module.name.contains(SearchBar.currentSearch, true)) return@any false + if (!it.module.name.contains(searchBar.currentSearch, true)) return@any false it.keyPressed(keyCode) } } diff --git a/src/main/kotlin/me/odinmain/clickgui/SearchBar.kt b/src/main/kotlin/me/odinmain/clickgui/SearchBar.kt deleted file mode 100644 index f1080d041..000000000 --- a/src/main/kotlin/me/odinmain/clickgui/SearchBar.kt +++ /dev/null @@ -1,56 +0,0 @@ -package me.odinmain.clickgui - -import me.odinmain.clickgui.ClickGUI.gray38 -import me.odinmain.features.impl.render.ClickGUIModule -import me.odinmain.utils.render.Colors -import me.odinmain.utils.ui.TextInputHandler -import me.odinmain.utils.ui.rendering.NVGRenderer - -object SearchBar { - - var currentSearch = "" - private set (value) { - if (value == field || value.length > 16) return - field = value - searchWidth = NVGRenderer.textWidth(value, 20f, NVGRenderer.defaultFont) - } - - private var placeHolderWidth = NVGRenderer.textWidth("Search here...", 20f, NVGRenderer.defaultFont) - private var searchWidth = NVGRenderer.textWidth(currentSearch, 20f, NVGRenderer.defaultFont) - - private val textInputHandler = TextInputHandler( - textProvider = { currentSearch }, - textSetter = { currentSearch = it } - ) - - fun draw(x: Float, y: Float, mouseX: Float, mouseY: Float) { - NVGRenderer.dropShadow(x, y, 350f, 40f, 10f, 0.75f, 9f) - NVGRenderer.rect(x, y, 350f, 40f, gray38.rgba, 9f) - NVGRenderer.hollowRect(x, y, 350f, 40f, 3f, ClickGUIModule.clickGUIColor.rgba, 9f) - - val textY = y + 10f - - if (currentSearch.isEmpty()) NVGRenderer.text("Search here...", x + 175f - placeHolderWidth / 2, textY, 20f, Colors.WHITE.rgba, NVGRenderer.defaultFont) - textInputHandler.x = (x + 175f - searchWidth / 2 - if (currentSearch.isEmpty()) placeHolderWidth / 2 + 2f else 0f).coerceAtLeast(x) - textInputHandler.y = textY - 1 - textInputHandler.width = 250f - textInputHandler.height = 22f - textInputHandler.draw(mouseX, mouseY) - } - - fun mouseClicked(mouseX: Float, mouseY: Float, mouseButton: Int): Boolean { - return textInputHandler.mouseClicked(mouseX, mouseY, mouseButton) - } - - fun mouseReleased() { - textInputHandler.mouseReleased() - } - - fun keyPressed(keyCode: Int): Boolean { - return textInputHandler.keyPressed(keyCode) - } - - fun keyTyped(typedChar: Char): Boolean { - return textInputHandler.keyTyped(typedChar) - } -} \ No newline at end of file diff --git a/src/main/kotlin/me/odinmain/features/ModuleManager.kt b/src/main/kotlin/me/odinmain/features/ModuleManager.kt index 6c662e764..79a9c13e3 100644 --- a/src/main/kotlin/me/odinmain/features/ModuleManager.kt +++ b/src/main/kotlin/me/odinmain/features/ModuleManager.kt @@ -65,7 +65,7 @@ object ModuleManager { //skyblock NoCursorReset, AutoSprint, BlazeAttunement, ChatCommands, DeployableTimer, DianaHelper, Ragnarock, MobSpawn, Splits, WardrobeKeybinds, InvincibilityTimer, ItemsHighlight, PlayerDisplay, - FarmKeys, PetKeybinds, CommandKeybinds, SpringBoots, AbilityTimers, SlotBinds, + FarmKeys, PetKeybinds, CommandKeybinds, SpringBoots, AbilityTimers, SlotBinds, SearchBar, // kuudra BuildHelper, FreshTools, KuudraDisplay, NoPre, PearlWaypoints, RemovePerks, SupplyHelper, TeamHighlight, diff --git a/src/main/kotlin/me/odinmain/features/impl/skyblock/SearchBar.kt b/src/main/kotlin/me/odinmain/features/impl/skyblock/SearchBar.kt new file mode 100644 index 000000000..614976c55 --- /dev/null +++ b/src/main/kotlin/me/odinmain/features/impl/skyblock/SearchBar.kt @@ -0,0 +1,69 @@ +package me.odinmain.features.impl.skyblock + +import me.odinmain.clickgui.settings.impl.ColorSetting +import me.odinmain.events.impl.GuiEvent +import me.odinmain.features.Module +import me.odinmain.utils.render.Color.Companion.withAlpha +import me.odinmain.utils.render.Colors +import me.odinmain.utils.skyblock.lore +import me.odinmain.utils.ui.SearchBar +import me.odinmain.utils.ui.mouseX +import me.odinmain.utils.ui.mouseY +import me.odinmain.utils.ui.rendering.NVGRenderer +import net.minecraft.client.gui.ScaledResolution +import net.minecraft.client.gui.inventory.GuiContainer +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +object SearchBar: Module( + name = "Search Bar", + description = "Search bar for inventories." +) { + private val highlightColor by ColorSetting("Highlight Color", Colors.MINECRAFT_BLUE.withAlpha(0.5f), true, desc = "Color of the highlight for matching items.") + + private val searchBar = SearchBar() + + @SubscribeEvent + fun onRenderInventory(event: GuiEvent.DrawGuiForeground) { + NVGRenderer.beginFrame(mc.displayWidth.toFloat(), mc.displayHeight.toFloat()) + + searchBar.draw(mc.displayWidth / 2f - 175f, mc.displayHeight - 110f, 350f, 40f, mouseX, mouseY) + + val scale = ScaledResolution(mc).scaleFactor.toFloat() + NVGRenderer.scale(scale, scale) + + if (searchBar.currentSearch.isNotEmpty()) { + for (inventorySlot in event.gui.inventorySlots.inventorySlots) { + if (inventorySlot?.stack?.displayName?.contains(searchBar.currentSearch, ignoreCase = true) == true || inventorySlot?.stack?.lore?.any { it.contains(searchBar.currentSearch, ignoreCase = true) } == true) { + val slotX = inventorySlot.xDisplayPosition.toFloat() + event.guiLeft + val slotY = inventorySlot.yDisplayPosition.toFloat() + event.guiTop + NVGRenderer.rect(slotX, slotY, 16f, 16f, highlightColor.rgba) + } + } + } + + NVGRenderer.scale(1f / scale, 1f / scale) + NVGRenderer.endFrame() + } + + @SubscribeEvent + fun onGuiMouseClick(event: GuiEvent.MouseClick) { + if (event.gui !is GuiContainer) return + if (searchBar.mouseClicked(mouseX, mouseY, event.button)) { + event.isCanceled = true + } + } + + @SubscribeEvent + fun onGuiMouseRelease(event: GuiEvent.MouseRelease) { + if (event.gui !is GuiContainer) return + searchBar.mouseReleased() + } + + @SubscribeEvent + fun onGuiKeyPress(event: GuiEvent.KeyPress) { + if (event.gui !is GuiContainer) return + if (searchBar.keyTyped(event.char) || searchBar.keyPressed(event.key)) { + event.isCanceled = true + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/me/odinmain/utils/ui/SearchBar.kt b/src/main/kotlin/me/odinmain/utils/ui/SearchBar.kt new file mode 100644 index 000000000..943c0b5d4 --- /dev/null +++ b/src/main/kotlin/me/odinmain/utils/ui/SearchBar.kt @@ -0,0 +1,46 @@ +package me.odinmain.utils.ui + +import me.odinmain.clickgui.ClickGUI.gray38 +import me.odinmain.features.impl.render.ClickGUIModule +import me.odinmain.utils.render.Colors +import me.odinmain.utils.ui.rendering.NVGRenderer + +class SearchBar { + var currentSearch = "" + private set (value) { + if (value == field || value.length > 16) return + field = value + searchWidth = NVGRenderer.textWidth(value, 20f, NVGRenderer.defaultFont) + } + + private var placeHolderWidth = NVGRenderer.textWidth("Search here...", 20f, NVGRenderer.defaultFont) + private var searchWidth = NVGRenderer.textWidth(currentSearch, 20f, NVGRenderer.defaultFont) + + private val textInputHandler = TextInputHandler( + textProvider = { currentSearch }, + textSetter = { currentSearch = it } + ) + + fun draw(x: Float, y: Float, width: Float, height: Float, mouseX: Float, mouseY: Float) { + NVGRenderer.dropShadow(x, y, width, height, 10f, 0.75f, 9f) + NVGRenderer.rect(x, y, width, height, gray38.rgba, 9f) + NVGRenderer.hollowRect(x, y, width, height, 3f, ClickGUIModule.clickGUIColor.rgba, 9f) + + val textY = y + 10f + + if (currentSearch.isEmpty()) NVGRenderer.text("Search here...", x + (width / 2) - placeHolderWidth / 2, textY, 20f, Colors.WHITE.rgba, NVGRenderer.defaultFont) + textInputHandler.x = (x + (width / 2) - searchWidth / 2 - if (currentSearch.isEmpty()) placeHolderWidth / 2 + 2f else 0f).coerceAtLeast(x) + textInputHandler.y = textY - 1 + textInputHandler.width = width - 100 // ?? not sure if these should be handled like this but whatever + textInputHandler.height = height - 18 // ?? + textInputHandler.draw(mouseX, mouseY) + } + + fun mouseClicked(mouseX: Float, mouseY: Float, mouseButton: Int): Boolean = textInputHandler.mouseClicked(mouseX, mouseY, mouseButton) + + fun mouseReleased() = textInputHandler.mouseReleased() + + fun keyPressed(keyCode: Int): Boolean = textInputHandler.keyPressed(keyCode) + + fun keyTyped(typedChar: Char): Boolean = textInputHandler.keyTyped(typedChar) +} \ No newline at end of file