diff options
-rw-r--r-- | src/main/kotlin/de/romjaki/pluggabledino/api/ToggleButton.kt | 66 | ||||
-rw-r--r-- | src/main/kotlin/de/romjaki/pluggabledino/states/SettingsState.kt | 6 |
2 files changed, 72 insertions, 0 deletions
diff --git a/src/main/kotlin/de/romjaki/pluggabledino/api/ToggleButton.kt b/src/main/kotlin/de/romjaki/pluggabledino/api/ToggleButton.kt new file mode 100644 index 0000000..cce59fc --- /dev/null +++ b/src/main/kotlin/de/romjaki/pluggabledino/api/ToggleButton.kt @@ -0,0 +1,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()) + +} + diff --git a/src/main/kotlin/de/romjaki/pluggabledino/states/SettingsState.kt b/src/main/kotlin/de/romjaki/pluggabledino/states/SettingsState.kt index 3e667da..d79837d 100644 --- a/src/main/kotlin/de/romjaki/pluggabledino/states/SettingsState.kt +++ b/src/main/kotlin/de/romjaki/pluggabledino/states/SettingsState.kt @@ -2,6 +2,7 @@ package de.romjaki.pluggabledino.states import de.romjaki.pluggabledino.* import de.romjaki.pluggabledino.api.Button +import de.romjaki.pluggabledino.api.ToggleButton import org.newdawn.slick.Color import org.newdawn.slick.GameContainer import org.newdawn.slick.Graphics @@ -18,16 +19,20 @@ class SettingsState : BasicGameState() { override fun enter(container: GameContainer?, game: StateBasedGame?) { backButton.enter() + scoreButton.enter() } override fun update(container: GameContainer?, game: StateBasedGame?, delta: Int) { container!! + scoreButton.update(container.input) backButton.update(container.input) } override fun getID(): Int = SETTINGS + val scoreButton = ToggleButton(listOf("ON", "OFF"), WIDTH * 3 / 4f, HEIGHT / 8f) + val backButton = Button("BACK", WIDTH / 2f, HEIGHT / 8 * 7f) override fun render(container: GameContainer?, game: StateBasedGame?, g: Graphics?) { @@ -35,6 +40,7 @@ class SettingsState : BasicGameState() { g.scale(WIDTH_RATIO, HEIGHT_RATIO) g.background = Color.lightGray backButton.draw(g) + scoreButton.draw(g) } } |