OSDN Git Service

ScrollPane, added isDragging and methods to hook when scrolling changes.
authorNathanSweet <nathan.sweet@gmail.com>
Sat, 29 Sep 2012 19:47:52 +0000 (12:47 -0700)
committerNathanSweet <nathan.sweet@gmail.com>
Sat, 29 Sep 2012 19:47:52 +0000 (12:47 -0700)
gdx/src/com/badlogic/gdx/scenes/scene2d/ui/ScrollPane.java

index 80d116c..18812af 100644 (file)
@@ -75,6 +75,7 @@ public class ScrollPane extends WidgetGroup {
        private boolean disableX, disableY;\r
        private boolean clamp = true;\r
        private boolean scrollbarsOnTop;\r
+       int draggingPointer = -1;\r
 \r
        /** @param widget May be null. */\r
        public ScrollPane (Actor widget) {\r
@@ -104,7 +105,6 @@ public class ScrollPane extends WidgetGroup {
 \r
                addCaptureListener(new InputListener() {\r
                        private float handlePosition;\r
-                       private int draggingPointer = -1;\r
 \r
                        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {\r
                                if (draggingPointer != -1) return false;\r
@@ -234,10 +234,10 @@ public class ScrollPane extends WidgetGroup {
 \r
        void clamp () {\r
                if (!clamp) return;\r
-               amountX = overscrollX ? MathUtils.clamp(amountX, -overscrollDistance, maxX + overscrollDistance) : MathUtils.clamp(amountX,\r
-                       0, maxX);\r
-               amountY = overscrollY ? MathUtils.clamp(amountY, -overscrollDistance, maxY + overscrollDistance) : MathUtils.clamp(amountY,\r
-                       0, maxY);\r
+               scrollX(overscrollX ? MathUtils.clamp(amountX, -overscrollDistance, maxX + overscrollDistance) : MathUtils.clamp(amountX,\r
+                       0, maxX));\r
+               scrollY(overscrollY ? MathUtils.clamp(amountY, -overscrollDistance, maxY + overscrollDistance) : MathUtils.clamp(amountY,\r
+                       0, maxY));\r
        }\r
 \r
        public void setStyle (ScrollPaneStyle style) {\r
@@ -284,17 +284,21 @@ public class ScrollPane extends WidgetGroup {
                }\r
 \r
                if (smoothScrolling && flingTimer <= 0 && !touchScrollH && !touchScrollV && !panning) {\r
-                       if (visualAmountX < amountX)\r
-                               visualAmountX = Math.min(amountX, visualAmountX + Math.max(150 * delta, (amountX - visualAmountX) * 5 * delta));\r
-                       else\r
-                               visualAmountX = Math.max(amountX, visualAmountX - Math.max(150 * delta, (visualAmountX - amountX) * 5 * delta));\r
-                       if (visualAmountY < amountY)\r
-                               visualAmountY = Math.min(amountY, visualAmountY + Math.max(150 * delta, (amountY - visualAmountY) * 5 * delta));\r
-                       else\r
-                               visualAmountY = Math.max(amountY, visualAmountY - Math.max(150 * delta, (visualAmountY - amountY) * 5 * delta));\r
+                       if (visualAmountX != amountX) {\r
+                               if (visualAmountX < amountX)\r
+                                       visualScrollX(Math.min(amountX, visualAmountX + Math.max(150 * delta, (amountX - visualAmountX) * 5 * delta)));\r
+                               else\r
+                                       visualScrollX(Math.max(amountX, visualAmountX - Math.max(150 * delta, (visualAmountX - amountX) * 5 * delta)));\r
+                       }\r
+                       if (visualAmountY != amountY) {\r
+                               if (visualAmountY < amountY)\r
+                                       visualScrollY(Math.min(amountY, visualAmountY + Math.max(150 * delta, (amountY - visualAmountY) * 5 * delta)));\r
+                               else\r
+                                       visualScrollY(Math.max(amountY, visualAmountY - Math.max(150 * delta, (visualAmountY - amountY) * 5 * delta)));\r
+                       }\r
                } else {\r
-                       visualAmountX = amountX;\r
-                       visualAmountY = amountY;\r
+                       if (visualAmountX != amountX) visualScrollX(amountX);\r
+                       if (visualAmountY != amountY) visualScrollY(amountY);\r
                }\r
 \r
                if (!panning) {\r
@@ -303,13 +307,13 @@ public class ScrollPane extends WidgetGroup {
                                        resetFade();\r
                                        amountX += (overscrollSpeedMin + (overscrollSpeedMax - overscrollSpeedMin) * -amountX / overscrollDistance)\r
                                                * delta;\r
-                                       if (amountX > 0) amountX = 0;\r
+                                       if (amountX > 0) scrollX(0);\r
                                } else if (amountX > maxX) {\r
                                        resetFade();\r
                                        amountX -= (overscrollSpeedMin + (overscrollSpeedMax - overscrollSpeedMin) * -(maxX - amountX)\r
                                                / overscrollDistance)\r
                                                * delta;\r
-                                       if (amountX < maxX) amountX = maxX;\r
+                                       if (amountX < maxX) scrollX(maxX);\r
                                }\r
                        }\r
                        if (overscrollY && scrollY) {\r
@@ -420,7 +424,7 @@ public class ScrollPane extends WidgetGroup {
                        if (scrollX) maxY -= scrollbarHeight;\r
                        if (scrollY) maxX -= scrollbarWidth;\r
                }\r
-               amountX = MathUtils.clamp(amountX, 0, maxX);\r
+               scrollX(MathUtils.clamp(amountX, 0, maxX));\r
                amountY = MathUtils.clamp(amountY, 0, maxY);\r
 \r
                // Set the bounds and scroll knob sizes if scrollbars are needed.\r
@@ -594,8 +598,28 @@ public class ScrollPane extends WidgetGroup {
                return super.hit(x, y, touchable);\r
        }\r
 \r
+       /** Called whenever the x scroll amount is changed. */\r
+       protected void scrollX (float pixelsX) {\r
+               this.amountX = pixelsX;\r
+       }\r
+\r
+       /** Called whenever the y scroll amount is changed. */\r
+       protected void scrollY (float pixelsY) {\r
+               this.amountY = pixelsY;\r
+       }\r
+\r
+       /** Called whenever the visual x scroll amount is changed. */\r
+       protected void visualScrollX (float pixelsX) {\r
+               this.visualAmountX = pixelsX;\r
+       }\r
+\r
+       /** Called whenever the visual y scroll amount is changed. */\r
+       protected void visualScrollY (float pixelsY) {\r
+               this.visualAmountY = pixelsY;\r
+       }\r
+\r
        public void setScrollX (float pixels) {\r
-               this.amountX = MathUtils.clamp(pixels, 0, maxX);\r
+               scrollX(MathUtils.clamp(pixels, 0, maxX));\r
        }\r
 \r
        /** Returns the x scroll position in pixels. */\r
@@ -625,7 +649,7 @@ public class ScrollPane extends WidgetGroup {
        }\r
 \r
        public void setScrollPercentX (float percentX) {\r
-               amountX = maxX * MathUtils.clamp(percentX, 0, 1);\r
+               scrollX(maxX * MathUtils.clamp(percentX, 0, 1));\r
        }\r
 \r
        public float getScrollPercentY () {\r
@@ -649,25 +673,29 @@ public class ScrollPane extends WidgetGroup {
        /** Sets the scroll offset so the specified rectangle is fully in view, if possible. Coordinates are in the scroll pane widget's\r
         * coordinate system. */\r
        public void scrollTo (float x, float y, float width, float height) {\r
+               float amountX = this.amountX;\r
                if (x + width > amountX + areaWidth) amountX = x + width - areaWidth;\r
                if (x < amountX) amountX = x;\r
-               amountX = MathUtils.clamp(amountX, 0, maxX);\r
+               scrollX(MathUtils.clamp(amountX, 0, maxX));\r
 \r
+               float amountY = this.amountY;\r
                if (amountY > maxY - y - height + areaHeight) amountY = maxY - y - height + areaHeight;\r
                if (amountY < maxY - y) amountY = maxY - y;\r
-               amountY = MathUtils.clamp(amountY, 0, maxY);\r
+               scrollY(MathUtils.clamp(amountY, 0, maxY));\r
        }\r
 \r
        /** Sets the scroll offset so the specified rectangle is fully in view and centered vertically in the scroll pane, if possible.\r
         * Coordinates are in the scroll pane widget's coordinate system. */\r
        public void scrollToCenter (float x, float y, float width, float height) {\r
+               float amountX = this.amountX;\r
                if (x + width > amountX + areaWidth) amountX = x + width - areaWidth;\r
                if (x < amountX) amountX = x;\r
-               amountX = MathUtils.clamp(amountX, 0, maxX);\r
+               scrollX(MathUtils.clamp(amountX, 0, maxX));\r
 \r
+               float amountY = this.amountY;\r
                float centerY = maxY - y + areaHeight / 2 - height / 2;\r
                if (amountY < centerY - areaHeight / 4 || amountY > centerY + areaHeight / 4) amountY = centerY;\r
-               amountY = MathUtils.clamp(amountY, 0, maxY);\r
+               scrollY(MathUtils.clamp(amountY, 0, maxY));\r
        }\r
 \r
        /** Returns the maximum scroll value in the x direction. */\r
@@ -702,6 +730,10 @@ public class ScrollPane extends WidgetGroup {
                disableY = y;\r
        }\r
 \r
+       public boolean isDragging () {\r
+               return draggingPointer != -1;\r
+       }\r
+\r
        public boolean isPanning () {\r
                return gestureListener.getGestureDetector().isPanning();\r
        }\r