mirror of
https://gitlab.com/2009scape/rt4-client.git
synced 2025-12-20 05:20:24 -07:00
Move some logic from the base plugin to the views as callbacks
This commit is contained in:
parent
fec060626b
commit
4a053d5698
7 changed files with 295 additions and 130 deletions
|
|
@ -24,6 +24,8 @@ import KondoKit.plugin.Companion.primaryColor
|
|||
import KondoKit.plugin.Companion.secondaryColor
|
||||
import KondoKit.plugin.StateManager.focusedView
|
||||
import KondoKit.views.BaseView
|
||||
import KondoKit.views.OnUpdateCallback
|
||||
import KondoKit.views.OnXPUpdateCallback
|
||||
import plugin.api.API
|
||||
import java.awt.*
|
||||
import java.awt.event.MouseAdapter
|
||||
|
|
@ -33,13 +35,15 @@ import java.io.InputStreamReader
|
|||
import java.nio.charset.StandardCharsets
|
||||
import javax.swing.*
|
||||
|
||||
object XPTrackerView {
|
||||
object XPTrackerView : View, OnUpdateCallback, OnXPUpdateCallback {
|
||||
private val COMBAT_SKILLS = intArrayOf(0,1,2,3,4)
|
||||
val xpWidgets: MutableMap<Int, XPWidget> = HashMap()
|
||||
var totalXPWidget: XPWidget? = null
|
||||
val initialXP: MutableMap<Int, Int> = HashMap()
|
||||
var xpTrackerView: JPanel? = null
|
||||
const val VIEW_NAME = "XP_TRACKER_VIEW"
|
||||
override val name: String = VIEW_NAME
|
||||
override val iconSpriteId: Int = KondoKit.plugin.LVL_ICON
|
||||
private val skillIconCache: MutableMap<Int, java.awt.image.BufferedImage> = HashMap()
|
||||
|
||||
|
||||
|
|
@ -64,6 +68,91 @@ object XPTrackerView {
|
|||
emptyMap()
|
||||
}
|
||||
|
||||
override val panel: JPanel
|
||||
get() = xpTrackerView ?: JPanel()
|
||||
|
||||
override fun createView() {
|
||||
createXPTrackerView()
|
||||
}
|
||||
|
||||
override fun registerFunctions() {
|
||||
// Register callbacks with the plugin
|
||||
KondoKit.plugin.registerUpdateCallback(this)
|
||||
KondoKit.plugin.registerXPUpdateCallback(this)
|
||||
}
|
||||
|
||||
override fun onUpdate() {
|
||||
val widgets = xpWidgets.values
|
||||
val totalXP = totalXPWidget
|
||||
|
||||
widgets.forEach { xpWidget ->
|
||||
val elapsedTime = (System.currentTimeMillis() - xpWidget.startTime) / 1000.0 / 60.0 / 60.0
|
||||
val xpPerHour = if (elapsedTime > 0) (xpWidget.totalXpGained / elapsedTime).toInt() else 0
|
||||
val formattedXpPerHour = formatNumber(xpPerHour)
|
||||
xpWidget.xpPerHourLabel.text =
|
||||
formatHtmlLabelText("XP /hr: ", primaryColor, formattedXpPerHour, secondaryColor)
|
||||
xpWidget.container.repaint()
|
||||
}
|
||||
|
||||
totalXP?.let { widget ->
|
||||
val elapsedTime = (System.currentTimeMillis() - widget.startTime) / 1000.0 / 60.0 / 60.0
|
||||
val totalXPPerHour = if (elapsedTime > 0) (widget.totalXpGained / elapsedTime).toInt() else 0
|
||||
val formattedTotalXpPerHour = formatNumber(totalXPPerHour)
|
||||
widget.xpPerHourLabel.text =
|
||||
formatHtmlLabelText("XP /hr: ", primaryColor, formattedTotalXpPerHour, secondaryColor)
|
||||
widget.container.repaint()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onXPUpdate(skillId: Int, xp: Int) {
|
||||
if (!initialXP.containsKey(skillId)) {
|
||||
initialXP[skillId] = xp
|
||||
return
|
||||
}
|
||||
val previousXpSnapshot = initialXP[skillId] ?: xp
|
||||
if (xp == initialXP[skillId]) return
|
||||
|
||||
val ensureOnEdt = Runnable {
|
||||
var xpWidget = xpWidgets[skillId]
|
||||
if (xpWidget != null) {
|
||||
updateWidget(xpWidget, xp)
|
||||
} else {
|
||||
xpWidget = createXPWidget(skillId, previousXpSnapshot)
|
||||
xpWidgets[skillId] = xpWidget
|
||||
|
||||
val wrapped = wrappedWidget(xpWidget.container)
|
||||
// Attach per-widget remove menu
|
||||
val popupMenu = removeXPWidgetMenu(wrapped, skillId)
|
||||
val rightClickListener = object : MouseAdapter() {
|
||||
override fun mousePressed(e: MouseEvent) {
|
||||
if (e.isPopupTrigger) popupMenu.show(e.component, e.x, e.y)
|
||||
}
|
||||
override fun mouseReleased(e: MouseEvent) {
|
||||
if (e.isPopupTrigger) popupMenu.show(e.component, e.x, e.y)
|
||||
}
|
||||
}
|
||||
addMouseListenerToAll(wrapped, rightClickListener)
|
||||
wrapped.addMouseListener(rightClickListener)
|
||||
|
||||
xpTrackerView?.add(wrapped)
|
||||
xpTrackerView?.add(Box.createVerticalStrut(5))
|
||||
|
||||
if(focusedView == VIEW_NAME) {
|
||||
xpTrackerView?.revalidate()
|
||||
xpTrackerView?.repaint()
|
||||
}
|
||||
|
||||
updateWidget(xpWidget, xp)
|
||||
}
|
||||
}
|
||||
|
||||
if (SwingUtilities.isEventDispatchThread()) {
|
||||
ensureOnEdt.run()
|
||||
} else {
|
||||
SwingUtilities.invokeLater(ensureOnEdt)
|
||||
}
|
||||
}
|
||||
|
||||
fun updateWidget(xpWidget: XPWidget, xp: Int) {
|
||||
val (currentLevel, xpGainedSinceLastLevel) = XPTable.getLevelForXp(xp)
|
||||
|
||||
|
|
@ -489,7 +578,6 @@ object XPTrackerView {
|
|||
}
|
||||
|
||||
|
||||
|
||||
data class XPWidget(
|
||||
val container: Container,
|
||||
val skillId: Int,
|
||||
|
|
@ -501,4 +589,4 @@ data class XPWidget(
|
|||
var totalXpGained: Int = 0,
|
||||
var startTime: Long = System.currentTimeMillis(),
|
||||
var previousXp: Int = 0
|
||||
)
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue