Kondo 2.0

This commit is contained in:
downthecrop 2024-10-08 01:20:42 -07:00
parent 637e5bf68a
commit b8c7a1150f
14 changed files with 1265 additions and 474 deletions

View file

@ -1,9 +1,16 @@
package KondoKit
import KondoKit.Constants.COLOR_BACKGROUND_DARK
import KondoKit.Constants.SKILL_DISPLAY_ORDER
import KondoKit.Constants.SKILL_SPRITE_DIMENSION
import KondoKit.Helpers.formatHtmlLabelText
import KondoKit.Helpers.getSpriteId
import KondoKit.Helpers.showToast
import KondoKit.SpriteToBufferedImage.getBufferedImageFromSprite
import KondoKit.plugin.Companion.VIEW_BACKGROUND_COLOR
import KondoKit.plugin.Companion.WIDGET_COLOR
import KondoKit.plugin.Companion.primaryColor
import KondoKit.plugin.Companion.secondaryColor
import com.google.gson.Gson
import plugin.api.API
import rt4.Sprites
@ -16,6 +23,7 @@ import java.awt.event.MouseEvent
import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.SocketTimeoutException
import java.net.URL
import javax.swing.*
import javax.swing.border.MatteBorder
@ -28,35 +36,38 @@ object Constants {
const val LVL_BAR_SPRITE = 898
// Dimensions
val SEARCH_FIELD_DIMENSION = Dimension(270, 30)
val SEARCH_FIELD_DIMENSION = Dimension(230, 30)
val ICON_DIMENSION_SMALL = Dimension(12, 12)
val ICON_DIMENSION_MEDIUM = Dimension(18, 20)
val ICON_DIMENSION_LARGE = Dimension(30, 30)
val HISCORE_PANEL_DIMENSION = Dimension(270, 400)
val FILTER_PANEL_DIMENSION = Dimension(270, 30)
val SKILLS_PANEL_DIMENSION = Dimension(300, 300)
val TOTAL_COMBAT_PANEL_DIMENSION = Dimension(270, 30)
val SKILL_PANEL_DIMENSION = Dimension(90, 35)
val HISCORE_PANEL_DIMENSION = Dimension(230, 500)
val FILTER_PANEL_DIMENSION = Dimension(230, 30)
val SKILLS_PANEL_DIMENSION = Dimension(230, 290)
val TOTAL_COMBAT_PANEL_DIMENSION = Dimension(230, 30)
val SKILL_PANEL_DIMENSION = Dimension(76, 35)
val IMAGE_CANVAS_DIMENSION = Dimension(20, 20)
val NUMBER_LABEL_DIMENSION = Dimension(30, 20)
val SKILL_SPRITE_DIMENSION = Dimension(14, 14)
val NUMBER_LABEL_DIMENSION = Dimension(20, 20)
// Colors
val COLOR_BACKGROUND_DARK = Color(27, 27, 27)
val COLOR_BACKGROUND_MEDIUM = Color(37, 37, 37)
val COLOR_BACKGROUND_LIGHT = Color(43, 43, 43)
val COLOR_BACKGROUND_MEDIUM = VIEW_BACKGROUND_COLOR
val COLOR_FOREGROUND_LIGHT = Color(200, 200, 200)
val COLOR_RED = Color.RED
val COLOR_SKILL_PANEL = Color(60, 60, 60)
// Fonts
val FONT_ARIAL_PLAIN_14 = Font("Arial", Font.PLAIN, 14)
val FONT_ARIAL_PLAIN_12 = Font("Arial", Font.PLAIN, 12)
val FONT_ARIAL_PLAIN_12 = Font("RuneScape Small", Font.TRUETYPE_FONT, 16)
val FONT_ARIAL_BOLD_12 = Font("Arial", Font.BOLD, 12)
val SKILL_DISPLAY_ORDER = arrayOf(0,3,14,2,16,13,1,15,10,4,17,7,5,12,11,6,9,8,20,18,19,22,21,23)
}
var text: String = ""
object HiscoresView {
var hiScoreView: JPanel? = null
class CustomSearchField(private val hiscoresPanel: JPanel) : Canvas() {
private var cursorVisible: Boolean = true
@ -69,6 +80,7 @@ object HiscoresView {
size = preferredSize
minimumSize = preferredSize
maximumSize = preferredSize
fillColor = COLOR_BACKGROUND_DARK
}
}
@ -125,7 +137,7 @@ object HiscoresView {
}
})
Timer(500) {
Timer(1000) {
cursorVisible = !cursorVisible
if(plugin.StateManager.focusedView == "HISCORE_SEARCH_VIEW")
repaint()
@ -140,7 +152,7 @@ object HiscoresView {
val fm = g.fontMetrics
val cursorX = fm.stringWidth(text) + 30
imageCanvas?.let { canvas ->
imageCanvas.let { canvas ->
val imgG = g.create(5, 5, canvas.width, canvas.height)
canvas.paint(imgG)
imgG.dispose()
@ -159,8 +171,10 @@ object HiscoresView {
}
fun searchPlayer(username: String) {
text = username
val apiUrl = "http://api.2009scape.org:3000/hiscores/playerSkills/1/${username.toLowerCase()}"
text = username.replace(" ", "_")
val apiUrl = "http://api.2009scape.org:3000/hiscores/playerSkills/1/${text.toLowerCase()}"
updateHiscoresView(null, "Searching...")
Thread {
try {
@ -168,6 +182,10 @@ object HiscoresView {
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "GET"
// If a request take longer than 5 seconds timeout.
connection.connectTimeout = 5000
connection.readTimeout = 5000
val responseCode = connection.responseCode
if (responseCode == HttpURLConnection.HTTP_OK) {
val reader = BufferedReader(InputStreamReader(connection.inputStream))
@ -179,27 +197,45 @@ object HiscoresView {
}
} else {
SwingUtilities.invokeLater {
showError("Player not found!")
showToast(hiscoresPanel, "Player not found!", JOptionPane.ERROR_MESSAGE)
}
}
} catch (e: Exception) {
} catch (e: SocketTimeoutException) {
SwingUtilities.invokeLater {
showError("Error fetching data!")
showToast(hiscoresPanel, "Request timed out", JOptionPane.ERROR_MESSAGE)
}
} catch (e: Exception) {
// Handle other errors
SwingUtilities.invokeLater {
showToast(hiscoresPanel, "Error fetching data!", JOptionPane.ERROR_MESSAGE)
}
}
}.start()
}
private fun updatePlayerData(jsonResponse: String, username: String) {
val hiscoresResponse = gson.fromJson(jsonResponse, HiscoresResponse::class.java)
updateHiscoresView(hiscoresResponse, username)
}
private fun updateHiscoresView(data: HiscoresResponse, username: String) {
private fun updateHiscoresView(data: HiscoresResponse?, username: String) {
val playerNameLabel = findComponentByName(hiscoresPanel, "playerNameLabel") as? JPanel
val ironMode = data.info.iron_mode
playerNameLabel?.removeAll() // Clear previous components
var nameLabel = JLabel(formatHtmlLabelText(username, secondaryColor, "", primaryColor), JLabel.CENTER).apply {
font = Constants.FONT_ARIAL_BOLD_12
foreground = Constants.COLOR_FOREGROUND_LIGHT
border = BorderFactory.createEmptyBorder(0, 6, 0, 0) // Top, Left, Bottom, Right padding
}
playerNameLabel?.add(nameLabel)
playerNameLabel?.revalidate()
playerNameLabel?.repaint()
if(data == null) return;
playerNameLabel?.removeAll()
val ironMode = data.info.iron_mode
if (ironMode != "0") {
val ironmanBufferedImage = getBufferedImageFromSprite(Sprites.nameIcons[Constants.IRONMAN_SPRITE + ironMode.toInt() - 1])
@ -213,11 +249,14 @@ object HiscoresView {
playerNameLabel?.add(imageCanvas)
}
val nameLabel = JLabel(username, JLabel.CENTER).apply {
val exp_multiplier = data.info.exp_multiplier
nameLabel = JLabel(formatHtmlLabelText(username, secondaryColor, " (${exp_multiplier}x)", primaryColor), JLabel.CENTER).apply {
font = Constants.FONT_ARIAL_BOLD_12
foreground = Constants.COLOR_FOREGROUND_LIGHT
border = BorderFactory.createEmptyBorder(0, 6, 0, 0) // Top, Left, Bottom, Right padding
}
playerNameLabel?.add(nameLabel)
playerNameLabel?.revalidate()
@ -296,7 +335,7 @@ object HiscoresView {
}
}
fun createHiscoreSearchView(): JPanel {
fun createHiscoreSearchView() {
val hiscorePanel = JPanel().apply {
layout = BoxLayout(this, BoxLayout.Y_AXIS)
name = "HISCORE_SEARCH_VIEW"
@ -324,14 +363,13 @@ object HiscoresView {
add(searchFieldWrapper)
}
hiscorePanel.add(Helpers.Spacer(height = 10))
hiscorePanel.add(Box.createVerticalStrut(10))
hiscorePanel.add(searchPanel)
hiscorePanel.add(Helpers.Spacer(height = 10))
hiscorePanel.add(Box.createVerticalStrut(10))
// Adding the player name panel in place of the filterPanel
val playerNamePanel = JPanel().apply {
layout = FlowLayout(FlowLayout.CENTER)
background = VIEW_BACKGROUND_COLOR
layout = GridBagLayout() // This will center the JLabel both vertically and horizontally
background = WIDGET_COLOR
preferredSize = Constants.FILTER_PANEL_DIMENSION
maximumSize = preferredSize
minimumSize = preferredSize
@ -339,7 +377,7 @@ object HiscoresView {
}
hiscorePanel.add(playerNamePanel)
hiscorePanel.add(Helpers.Spacer(height = 10))
hiscorePanel.add(Box.createVerticalStrut(10))
val skillsPanel = JPanel(FlowLayout(FlowLayout.CENTER, 0, 0)).apply {
background = Constants.COLOR_BACKGROUND_MEDIUM
@ -348,10 +386,10 @@ object HiscoresView {
minimumSize = preferredSize
}
for (i in 0 until 24) {
for (i in SKILL_DISPLAY_ORDER) {
val skillPanel = JPanel().apply {
layout = BorderLayout()
background = Constants.COLOR_SKILL_PANEL
background = COLOR_BACKGROUND_DARK
preferredSize = Constants.SKILL_PANEL_DIMENSION
maximumSize = preferredSize
minimumSize = preferredSize
@ -362,8 +400,9 @@ object HiscoresView {
val imageCanvas = bufferedImageSprite.let {
ImageCanvas(it).apply {
preferredSize = Constants.IMAGE_CANVAS_DIMENSION
size = Constants.IMAGE_CANVAS_DIMENSION
preferredSize = SKILL_SPRITE_DIMENSION
size = SKILL_SPRITE_DIMENSION
fillColor = COLOR_BACKGROUND_DARK
}
}
@ -376,7 +415,7 @@ object HiscoresView {
}
val imageContainer = JPanel(FlowLayout(FlowLayout.CENTER, 5, 0)).apply {
background = Constants.COLOR_BACKGROUND_DARK
background = COLOR_BACKGROUND_DARK
add(imageCanvas)
add(numberLabel)
}
@ -431,7 +470,7 @@ object HiscoresView {
}
val combatLevelPanel = JPanel(FlowLayout(FlowLayout.LEFT)).apply {
background = Constants.COLOR_BACKGROUND_DARK
background = COLOR_BACKGROUND_DARK
add(combatLevelIcon)
add(combatLevelLabel)
}
@ -439,8 +478,9 @@ object HiscoresView {
totalCombatPanel.add(totalLevelPanel)
totalCombatPanel.add(combatLevelPanel)
hiscorePanel.add(totalCombatPanel)
hiscorePanel.add(Box.createVerticalStrut(10))
return hiscorePanel
hiScoreView = hiscorePanel;
}
data class HiscoresResponse(