From d5218f04323ba0d0c552e65b0f87817eda531e88 Mon Sep 17 00:00:00 2001
From: karinlie <81482313+karinlie@users.noreply.github.com>
Date: Fri, 24 Mar 2023 14:14:19 +0100
Subject: [PATCH] fikset knapp og slik at den bevegde seg

---
 core/src/tdt4240/netrunner/Netrunner.kt    | 21 +++++++-
 core/src/tdt4240/netrunner/SimpleButton.kt | 63 ++++++++++++++++------
 2 files changed, 67 insertions(+), 17 deletions(-)

diff --git a/core/src/tdt4240/netrunner/Netrunner.kt b/core/src/tdt4240/netrunner/Netrunner.kt
index 7e1fa90..cba53a0 100644
--- a/core/src/tdt4240/netrunner/Netrunner.kt
+++ b/core/src/tdt4240/netrunner/Netrunner.kt
@@ -3,6 +3,7 @@ package tdt4240.netrunner
 import com.badlogic.gdx.ApplicationAdapter
 import com.badlogic.gdx.Gdx
 import com.badlogic.gdx.Input
+import com.badlogic.gdx.InputMultiplexer
 import com.badlogic.gdx.graphics.Texture
 import com.badlogic.gdx.graphics.g2d.BitmapFont
 import com.badlogic.gdx.graphics.g2d.SpriteBatch
@@ -20,18 +21,27 @@ class Netrunner : ApplicationAdapter() {
     private lateinit var img: Texture
     private var position: Vector2 = Vector2(300f, 0f)
     private var speed: Float = 10.0f
-
+    private lateinit var buttonUp: SimpleButton
+    private lateinit var buttonDown: SimpleButton
+    public var inputMultiplexer = InputMultiplexer()
 
     override fun create() {
+        Gdx.input.inputProcessor = inputMultiplexer
         batch = SpriteBatch()
         img = Texture("whiteball.png")
+        buttonUp = SimpleButton(inputMultiplexer, Texture("up.png"), 550f, 100f,60f,60f)
+            { moveUp() }
+        buttonDown = SimpleButton(inputMultiplexer, Texture("down.png"), 550f, 20f,60f,60f)
+            { moveDown() }
     }
 
     override fun render() {
         ScreenUtils.clear(1f, 0f, 0f, 1f)
         batch.begin()
         batch.draw(img, position.x, position.y)
-        move(1.0f)
+        buttonUp.update(batch)
+        buttonDown.update(batch)
+
         batch.end()
     }
 
@@ -57,5 +67,12 @@ class Netrunner : ApplicationAdapter() {
             }
         }
     }
+    fun moveUp() {
+        position.y += speed
+    }
+    fun moveDown() {
+        position.y -= speed
+    }
+
 
 }
\ No newline at end of file
diff --git a/core/src/tdt4240/netrunner/SimpleButton.kt b/core/src/tdt4240/netrunner/SimpleButton.kt
index 0fe0fdb..c9c7ca2 100644
--- a/core/src/tdt4240/netrunner/SimpleButton.kt
+++ b/core/src/tdt4240/netrunner/SimpleButton.kt
@@ -1,30 +1,63 @@
 package tdt4240.netrunner
 
+import com.badlogic.gdx.Gdx
+import com.badlogic.gdx.InputMultiplexer
+import com.badlogic.gdx.InputProcessor
 import com.badlogic.gdx.graphics.Texture
 import com.badlogic.gdx.graphics.g2d.Sprite
 import com.badlogic.gdx.graphics.g2d.SpriteBatch
 
-class SimpleButton() {
+class SimpleButton(inputMultiplexer: InputMultiplexer, texture: Texture, x: Float, y: Float, width: Float, height: Float, callback: () -> Unit) : InputProcessor {
 
-    private lateinit var skin: Sprite
+    private var sprite = Sprite(texture)
+    private var callback = callback
 
-    fun SimpleButton(texture: Texture, x: Float, y: Float, width: Float, height: Float){
-        skin = Sprite(texture)
-        skin.setPosition(x, y)
-        skin.setSize(width, height)
+    init {
+        sprite.setPosition(x, y)
+        sprite.setSize(width, height)
+        inputMultiplexer.addProcessor(this)
     }
 
-    public fun update(batch: SpriteBatch, input_x: Float, input_y: Float){
-        checkIfClicked(input_x, input_y);
-        skin.draw(batch);
+    public fun update(batch: SpriteBatch) {
+        sprite.draw(batch);
     }
 
-    private fun checkIfClicked(ix: Float, iy: Float) {
-        if (ix > skin.x && ix < skin.x + skin.width) {
-            if (iy > skin.y && iy < skin.y + skin.height) {
-                // the button was clicked, perform an action
-                println("Button clicked !")
-            }
+    override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
+        val fixedScreenY = Gdx.graphics.height - screenY
+        println("$screenX,$screenY,$fixedScreenY")
+        if (screenX >= sprite.x && screenX <= sprite.x + sprite.width
+                && fixedScreenY >= sprite.y && fixedScreenY <= sprite.y + sprite.height) {
+            callback()
+            return true
         }
+        return false
+    }
+
+    override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
+        return false
+    }
+
+    override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
+        return false
+    }
+
+    override fun keyDown(keycode: Int): Boolean {
+        return false
+    }
+
+    override fun keyUp(keycode: Int): Boolean {
+        return false
+    }
+
+    override fun keyTyped(character: Char): Boolean {
+        return false
+    }
+
+    override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
+        return false
+    }
+
+    override fun scrolled(amountX: Float, amountY: Float): Boolean {
+        return false
     }
 }
\ No newline at end of file
-- 
GitLab