diff options
Diffstat (limited to 'src/main/kotlin')
6 files changed, 48 insertions, 6 deletions
diff --git a/src/main/kotlin/de/romjaki/pluggabledino/api/Button.kt b/src/main/kotlin/de/romjaki/pluggabledino/api/Button.kt index edced5a..439acb8 100644 --- a/src/main/kotlin/de/romjaki/pluggabledino/api/Button.kt +++ b/src/main/kotlin/de/romjaki/pluggabledino/api/Button.kt @@ -6,7 +6,10 @@ import org.newdawn.slick.Input import java.awt.Rectangle import kotlin.math.max -class Button(private val text: String, val x: Float, val y: Float) { +class Button(private val text: String, val x: Float, val y: Float) : SettingsElement { + override fun render(g: Graphics) { + draw(g) + } val width = max(buttonImage.width, font.getWidth(text) + 10) val image = buttonImage.getScaledCopy(width, buttonImage.height) @@ -40,14 +43,14 @@ class Button(private val text: String, val x: Float, val y: Float) { override fun toString(): String = "X: $leftX - $rightX, $topY - $bottomY, width=$width" - fun enter() { + override fun enter() { lastClicked = true } fun isClicked(input: Input): Boolean = input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) && isMouseOver(input) - fun update(input: Input) { + override fun update(input: Input) { val ret = isClicked(input) if (!lastClicked && ret) { clickHandlers.forEach({ it() }) diff --git a/src/main/kotlin/de/romjaki/pluggabledino/api/Renderable.kt b/src/main/kotlin/de/romjaki/pluggabledino/api/Renderable.kt new file mode 100644 index 0000000..f7ac4be --- /dev/null +++ b/src/main/kotlin/de/romjaki/pluggabledino/api/Renderable.kt @@ -0,0 +1,7 @@ +package de.romjaki.pluggabledino.api + +import org.newdawn.slick.Graphics + +interface Renderable { + fun render(g: Graphics) +} diff --git a/src/main/kotlin/de/romjaki/pluggabledino/api/SettingsElement.kt b/src/main/kotlin/de/romjaki/pluggabledino/api/SettingsElement.kt new file mode 100644 index 0000000..8ca7a37 --- /dev/null +++ b/src/main/kotlin/de/romjaki/pluggabledino/api/SettingsElement.kt @@ -0,0 +1,6 @@ +package de.romjaki.pluggabledino.api + +import de.romjaki.pluggabledino.api.Renderable +import de.romjaki.pluggabledino.api.Updateable + +interface SettingsElement : Renderable, Updateable diff --git a/src/main/kotlin/de/romjaki/pluggabledino/api/ToggleButton.kt b/src/main/kotlin/de/romjaki/pluggabledino/api/ToggleButton.kt index cce59fc..b776637 100644 --- a/src/main/kotlin/de/romjaki/pluggabledino/api/ToggleButton.kt +++ b/src/main/kotlin/de/romjaki/pluggabledino/api/ToggleButton.kt @@ -8,7 +8,10 @@ import kotlin.math.max typealias ChangeListener = (index: Int) -> Unit -class ToggleButton(private val texts: List<String>, val x: Float, val y: Float) { +class ToggleButton(private val texts: List<String>, val x: Float, val y: Float) : SettingsElement { + override fun render(g: Graphics) { + draw(g) + } val width = max(buttonImage.width, texts.map { font.getWidth(it) + 10 }.max()!!) val image = buttonImage.getScaledCopy(width, buttonImage.height) @@ -43,14 +46,14 @@ class ToggleButton(private val texts: List<String>, val x: Float, val y: Float) override fun toString(): String = "X: $leftX - $rightX, $topY - $bottomY, width=$width" - fun enter() { + override fun enter() { lastClicked = true } fun isClicked(input: Input): Boolean = input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) && isMouseOver(input) - fun update(input: Input) { + override fun update(input: Input) { val ret = isClicked(input) if (!lastClicked && ret) { index = (index + 1) % texts.size diff --git a/src/main/kotlin/de/romjaki/pluggabledino/api/Updateable.kt b/src/main/kotlin/de/romjaki/pluggabledino/api/Updateable.kt new file mode 100644 index 0000000..42798bc --- /dev/null +++ b/src/main/kotlin/de/romjaki/pluggabledino/api/Updateable.kt @@ -0,0 +1,8 @@ +package de.romjaki.pluggabledino.api + +import org.newdawn.slick.Input + +interface Updateable { + fun update(input: Input) + fun enter() +} diff --git a/src/main/kotlin/de/romjaki/pluggabledino/states/SettingsState.kt b/src/main/kotlin/de/romjaki/pluggabledino/states/SettingsState.kt index d79837d..d55d76f 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.SettingsElement import de.romjaki.pluggabledino.api.ToggleButton import org.newdawn.slick.Color import org.newdawn.slick.GameContainer @@ -20,27 +21,41 @@ class SettingsState : BasicGameState() { override fun enter(container: GameContainer?, game: StateBasedGame?) { backButton.enter() scoreButton.enter() + additionals.forEach { + it.enter() + } } override fun update(container: GameContainer?, game: StateBasedGame?, delta: Int) { container!! scoreButton.update(container.input) backButton.update(container.input) + additionals.forEach { + it.update(container.input) + } } override fun getID(): Int = SETTINGS + fun addSettingsElement(el: SettingsElement) { + additionals.add(el) + } + val scoreButton = ToggleButton(listOf("ON", "OFF"), WIDTH * 3 / 4f, HEIGHT / 8f) val backButton = Button("BACK", WIDTH / 2f, HEIGHT / 8 * 7f) + val additionals = mutableListOf<SettingsElement>() + override fun render(container: GameContainer?, game: StateBasedGame?, g: Graphics?) { g!! g.scale(WIDTH_RATIO, HEIGHT_RATIO) g.background = Color.lightGray backButton.draw(g) scoreButton.draw(g) + additionals.forEach { it.render(g) } + } } |