1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
package de.romjaki.pluggabledino.api
import de.romjaki.pluggabledino.*
import org.newdawn.slick.Graphics
import org.newdawn.slick.Input
import java.awt.Rectangle
import kotlin.math.max
typealias ChangeListener = (index: Int) -> Unit
class ToggleButton(private val texts: List<String>, val x: Float, val y: Float) {
val width = max(buttonImage.width, texts.map { font.getWidth(it) + 10 }.max()!!)
val image = buttonImage.getScaledCopy(width, buttonImage.height)
private var lastClicked = false
var index = 0
val leftX
get() = x - width / 2
val rightX
get() = x + width / 2
val topY
get() = y - image.height / 2
val bottomY
get() = y + image.height / 2
private val changeListeners = mutableListOf<ChangeListener>()
val rectangle
get() = Rectangle(leftX.toInt(), topY.toInt(), (rightX - leftX).toInt(), (bottomY - topY).toInt())
fun addChangeListener(block: ChangeListener) {
changeListeners.add(block)
}
fun draw(g: Graphics) {
g.drawImageCentered(image, x, y)
g.drawStringCentered(texts[index], x, y)
}
override fun toString(): String =
"X: $leftX - $rightX, $topY - $bottomY, width=$width"
fun enter() {
lastClicked = true
}
fun isClicked(input: Input): Boolean =
input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) && isMouseOver(input)
fun update(input: Input) {
val ret = isClicked(input)
if (!lastClicked && ret) {
index = (index + 1) % texts.size
changeListeners.forEach { it(index) }
}
lastClicked = ret
}
fun isMouseOver(input: Input): Boolean =
rectangle.contains((input.mouseX / WIDTH_RATIO).toInt(), (input.mouseY / HEIGHT_RATIO).toInt())
}
|