OSDN Git Service

Touchpad, fixed get percent methods.
authorNathanSweet <nathan.sweet@gmail.com>
Wed, 19 Sep 2012 05:54:53 +0000 (22:54 -0700)
committerNathanSweet <nathan.sweet@gmail.com>
Wed, 19 Sep 2012 05:54:53 +0000 (22:54 -0700)
gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Touchpad.java
tests/gdx-tests/src/com/badlogic/gdx/tests/TouchpadTest.java

index 2d163e7..e6de02e 100644 (file)
@@ -24,7 +24,7 @@ public class Touchpad extends Widget {
        private TouchpadStyle style;\r
        boolean touched;\r
        private float deadzoneRadius;\r
-       private final Circle padBounds = new Circle(0, 0, 0);\r
+       private final Circle knobBounds = new Circle(0, 0, 0);\r
        private final Circle touchBounds = new Circle(0, 0, 0);\r
        private final Circle deadzoneBounds = new Circle(0, 0, 0);\r
        private final Vector2 knobPosition = new Vector2();\r
@@ -78,15 +78,19 @@ public class Touchpad extends Widget {
                float oldPositionY = knobPosition.y;\r
                float oldPercentX = knobPercent.x;\r
                float oldPercentY = knobPercent.y;\r
-               knobPosition.set(getWidth() / 2f, getHeight() / 2f);\r
+               float centerX = knobBounds.x;\r
+               float centerY = knobBounds.y;\r
+               knobPosition.set(centerX, centerY);\r
                knobPercent.set(0f, 0f);\r
                if (!isTouchUp) {\r
                        if (!deadzoneBounds.contains(x, y)) {\r
-                               knobPercent.set(x - padBounds.x, y - padBounds.y).nor();\r
-                               if (padBounds.contains(x, y)) {\r
+                               knobPercent.set((x - centerX) / knobBounds.radius, (y - centerY) / knobBounds.radius);\r
+                               float length = knobPercent.len();\r
+                               if (length > 1) knobPercent.mul(1 / length);\r
+                               if (knobBounds.contains(x, y)) {\r
                                        knobPosition.set(x, y);\r
                                } else {\r
-                                       knobPosition.set(knobPercent).mul(padBounds.radius).add(padBounds.x, padBounds.y);\r
+                                       knobPosition.set(knobPercent).nor().mul(knobBounds.radius).add(knobBounds.x, knobBounds.y);\r
                                }\r
                        }\r
                }\r
@@ -125,7 +129,7 @@ public class Touchpad extends Widget {
                float radius = Math.min(halfWidth, halfHeight);\r
                touchBounds.set(halfWidth, halfHeight, radius);\r
                if (style.knob != null) radius -= Math.max(style.knob.getMinWidth(), style.knob.getMinHeight()) / 2;\r
-               padBounds.set(halfWidth, halfHeight, radius);\r
+               knobBounds.set(halfWidth, halfHeight, radius);\r
                deadzoneBounds.set(halfWidth, halfHeight, deadzoneRadius);\r
                // Recalc pad values and knob position\r
                knobPosition.set(halfWidth, halfHeight);\r
index 7727b1c..3431014 100644 (file)
@@ -10,6 +10,7 @@ import com.badlogic.gdx.tests.utils.GdxTest;
 \r
 public class TouchpadTest extends GdxTest {\r
        Stage stage;\r
+       Touchpad touchpad;\r
 \r
        public void create () {\r
                stage = new Stage();\r
@@ -17,13 +18,13 @@ public class TouchpadTest extends GdxTest {
 \r
                Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));\r
 \r
-               Touchpad touchpad = new Touchpad(20, skin);\r
+               touchpad = new Touchpad(20, skin);\r
                touchpad.setBounds(15, 15, 100, 100);\r
                stage.addActor(touchpad);\r
        }\r
 \r
        public void render () {\r
-               // System.out.println(meow.getValue());\r
+               //System.out.println(touchpad.getKnobPercentX() + " " + touchpad.getKnobPercentY());\r
                Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);\r
                stage.act(Gdx.graphics.getDeltaTime());\r
                stage.draw();\r