-
-
Notifications
You must be signed in to change notification settings - Fork 94
search bar aaa #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
search bar aaa #213
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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() | ||
| } | ||
|
Comment on lines
+26
to
+46
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling |
||
|
|
||
| @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 | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 // ?? | ||
|
Comment on lines
+34
to
+35
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The magic numbers |
||
| 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) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This conditional logic for checking the item name and lore can be made safer and more readable by using a
letscope function to handle the nullablestack. This avoids the long chain of safe calls and the== truecheck.