mirror of
https://gitlab.com/2009scape/rt4-client.git
synced 2025-12-18 20:40:20 -07:00
reorganize
This commit is contained in:
parent
529f0c22b0
commit
cf6bb51d2c
20 changed files with 815 additions and 411 deletions
|
|
@ -0,0 +1,155 @@
|
|||
package KondoKit.components
|
||||
|
||||
import KondoKit.ImageCanvas
|
||||
import KondoKit.SpriteToBufferedImage
|
||||
import KondoKit.plugin.Companion.WIDGET_COLOR
|
||||
import KondoKit.plugin.Companion.secondaryColor
|
||||
import plugin.api.API
|
||||
import java.awt.*
|
||||
import java.awt.datatransfer.DataFlavor
|
||||
import java.awt.event.*
|
||||
import javax.swing.*
|
||||
|
||||
open class SearchField(
|
||||
private val onSearch: (String) -> Unit,
|
||||
private val placeholderText: String = "Search...",
|
||||
private val fieldWidth: Int = 230,
|
||||
private val fieldHeight: Int = 30
|
||||
) : Canvas() {
|
||||
|
||||
private var cursorVisible: Boolean = true
|
||||
private var text: String = ""
|
||||
|
||||
private val bufferedImageSprite = SpriteToBufferedImage.getBufferedImageFromSprite(API.GetSprite(1423)) // MAG_SPRITE
|
||||
private val imageCanvas = bufferedImageSprite.let {
|
||||
ImageCanvas(it).apply {
|
||||
preferredSize = Dimension(12, 12)
|
||||
size = preferredSize
|
||||
minimumSize = preferredSize
|
||||
maximumSize = preferredSize
|
||||
fillColor = WIDGET_COLOR
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
val dimension = Dimension(fieldWidth, fieldHeight)
|
||||
preferredSize = dimension
|
||||
background = WIDGET_COLOR
|
||||
foreground = secondaryColor
|
||||
font = Font("Arial", Font.PLAIN, 14)
|
||||
minimumSize = dimension
|
||||
maximumSize = dimension
|
||||
|
||||
addKeyListener(object : KeyAdapter() {
|
||||
override fun keyTyped(e: KeyEvent) {
|
||||
// Prevent null character from being typed on Ctrl+A & Ctrl+V
|
||||
if (e.isControlDown && (e.keyChar == '\u0001' || e.keyChar == '\u0016')) {
|
||||
e.consume()
|
||||
return
|
||||
}
|
||||
if (e.keyChar == '\b') {
|
||||
if (text.isNotEmpty()) {
|
||||
text = text.dropLast(1)
|
||||
}
|
||||
} else if (e.keyChar == '\n') {
|
||||
onSearch(text)
|
||||
} else {
|
||||
text += e.keyChar
|
||||
}
|
||||
SwingUtilities.invokeLater {
|
||||
repaint()
|
||||
}
|
||||
}
|
||||
|
||||
override fun keyPressed(e: KeyEvent) {
|
||||
if (e.isControlDown) {
|
||||
when (e.keyCode) {
|
||||
KeyEvent.VK_A -> {
|
||||
text = ""
|
||||
SwingUtilities.invokeLater {
|
||||
repaint()
|
||||
}
|
||||
}
|
||||
KeyEvent.VK_V -> {
|
||||
try {
|
||||
val clipboard = Toolkit.getDefaultToolkit().systemClipboard
|
||||
val pasteText = clipboard.getData(DataFlavor.stringFlavor) as String
|
||||
text += pasteText
|
||||
SwingUtilities.invokeLater {
|
||||
repaint()
|
||||
}
|
||||
} catch (ex: Exception) {
|
||||
// Ignore clipboard errors
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
addMouseListener(object : MouseAdapter() {
|
||||
override fun mouseClicked(e: MouseEvent) {
|
||||
if (e.x > width - 20 && e.y < 20) {
|
||||
text = ""
|
||||
onSearch(text)
|
||||
SwingUtilities.invokeLater {
|
||||
repaint()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Timer(500) { _ ->
|
||||
cursorVisible = !cursorVisible
|
||||
SwingUtilities.invokeLater {
|
||||
repaint()
|
||||
}
|
||||
}.start()
|
||||
}
|
||||
|
||||
override fun paint(g: Graphics) {
|
||||
super.paint(g)
|
||||
g.color = foreground
|
||||
g.font = font
|
||||
|
||||
val fm = g.fontMetrics
|
||||
val cursorX = fm.stringWidth(text) + 30
|
||||
|
||||
// Draw magnifying glass icon
|
||||
imageCanvas.let { canvas ->
|
||||
val imgG = g.create(5, 5, canvas.width, canvas.height)
|
||||
canvas.paint(imgG)
|
||||
imgG.dispose()
|
||||
}
|
||||
|
||||
// Use a local copy of the text to avoid threading issues
|
||||
val currentText = text
|
||||
|
||||
// Draw placeholder text if field is empty, otherwise draw actual text
|
||||
if (currentText.isEmpty()) {
|
||||
g.color = Color.GRAY // Use a lighter color for placeholder text
|
||||
g.drawString(placeholderText, 30, 20)
|
||||
} else {
|
||||
g.color = foreground // Use normal color for actual text
|
||||
g.drawString(currentText, 30, 20)
|
||||
}
|
||||
|
||||
if (cursorVisible && hasFocus()) {
|
||||
g.color = foreground
|
||||
g.drawLine(cursorX, 5, cursorX, 25)
|
||||
}
|
||||
|
||||
// Only draw the "x" button if there's text
|
||||
if (currentText.isNotEmpty()) {
|
||||
g.color = Color.RED
|
||||
g.drawString("x", width - 20, 20)
|
||||
}
|
||||
}
|
||||
|
||||
fun setText(newText: String) {
|
||||
text = newText
|
||||
repaint()
|
||||
}
|
||||
|
||||
fun getText(): String = text
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue