From ff3e50d607c3e91ba370740881b4975534ed1b7c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Drahom=C3=ADr=20Karch=C5=88=C3=A1k?=
 <drahomir.karchnak@nic.cz>
Date: Thu, 21 Jan 2016 16:37:41 +0100
Subject: [PATCH] #81 Code refactoring

---
 .../screen/createuser/PanoramaScreen.java     | 75 ++++++++++---------
 1 file changed, 40 insertions(+), 35 deletions(-)

diff --git a/core/src/cz/nic/tablexia/screen/createuser/PanoramaScreen.java b/core/src/cz/nic/tablexia/screen/createuser/PanoramaScreen.java
index 406c20212..f05cda92b 100644
--- a/core/src/cz/nic/tablexia/screen/createuser/PanoramaScreen.java
+++ b/core/src/cz/nic/tablexia/screen/createuser/PanoramaScreen.java
@@ -89,6 +89,10 @@ public class PanoramaScreen extends AbstractTablexiaScreen<int[][]> {
     private Stack newspaperStages;
     private boolean newspapersOpened = false;
 
+    private Image hintFinger;
+    private float lastScroll = Float.NEGATIVE_INFINITY;
+    private Action lastHintFingerAction;
+
     @Override
     public boolean canBePaused() {
         return false;
@@ -592,48 +596,49 @@ public class PanoramaScreen extends AbstractTablexiaScreen<int[][]> {
     }
 
     private void prepareSwipeStreetFingerHint() {
-        final Image leftFinger = ScaleUtil.createImageWidthPosition(getScreenTextureRegion(GFX_PATH + "newspaper/left_finger"), getStage().getWidth() * PanoramaActorsLayout.STREET_HINT_FINGER_WIDTH, getStage().getWidth() * PanoramaActorsLayout.STREET_HINT_FINGER_X - getStage().getWidth() * PanoramaActorsLayout.STREET_HINT_FINGER_WIDTH / 2, 0);
-        leftFinger.setVisible(false);
-        leftFinger.setTouchable(Touchable.disabled);
-        panel.addActor(leftFinger);
+        hintFinger = ScaleUtil.createImageWidthPosition(getScreenTextureRegion(GFX_PATH + "newspaper/left_finger"), getStage().getWidth() * PanoramaActorsLayout.STREET_HINT_FINGER_WIDTH, getStage().getWidth() * PanoramaActorsLayout.STREET_HINT_FINGER_X - getStage().getWidth() * PanoramaActorsLayout.STREET_HINT_FINGER_WIDTH / 2, 0);
+        hintFinger.setVisible(false);
+        hintFinger.setTouchable(Touchable.disabled);
+        panel.addActor(hintFinger);
         scrollpane.addListener(new EventListener() { // animate finger after 3s of inactivity and only if scrolled to less than 0.7 of maxX
-            private float lastScroll = Float.NEGATIVE_INFINITY;
-            private Action action = null;
 
             @Override
             public boolean handle(Event event) {
-                if (scrollpane.getScrollX() != lastScroll) {
-                    final Action oldAction = action;
-                    leftFinger.addAction(Actions.sequence(Actions.fadeOut(PanoramaActorsLayout.FINGER_FADE_OUT), Actions.run(new Runnable() {
-                        @Override
-                        public void run() {
-                            leftFinger.removeAction(oldAction);
-                            leftFinger.setScale(PanoramaActorsLayout.FINGER_ORIGINAL_SCALE_TO);
-                            leftFinger.setPosition(getStage().getWidth() * PanoramaActorsLayout.STREET_HINT_FINGER_X - getStage().getWidth() * PanoramaActorsLayout.STREET_HINT_FINGER_WIDTH / 2, 0);
-                        }
-                    })));
-                    //ScrollPanes getScrollPercentX method returns NaN if scroll pane wasn't touched yet...
-                    float percentScrollX = Float.isNaN(scrollpane.getScrollPercentX()) ? 0 : scrollpane.getScrollPercentX();
-                    if (percentScrollX < PanoramaActorsLayout.STREET_HINT_END) {
-                        leftFinger.addAction(action = Actions.sequence(
-                                Actions.delay(PanoramaActorsLayout.STREET_HINT_DELAY)
-                                , Actions.alpha(0)
-                                , Actions.visible(true)
-                                , Actions.fadeIn(PanoramaActorsLayout.FINGER_FADE_IN)
-                                , Actions.forever(Actions.sequence(
-                                        Actions.moveTo(getStage().getWidth() * PanoramaActorsLayout.STREET_HINT_MOVE_TO - leftFinger.getWidth() / 2, leftFinger.getY(), PanoramaActorsLayout.FINGER_MOVE_DURATION)
-                                        , Actions.scaleTo(PanoramaActorsLayout.FINGER_SCALE_TO, PanoramaActorsLayout.FINGER_SCALE_TO, PanoramaActorsLayout.FINGER_SCALE_DURATION)
-                                        , Actions.moveTo(-leftFinger.getWidth(), leftFinger.getY(), PanoramaActorsLayout.FINGER_MOVE_DURATION)
-                                        , Actions.scaleTo(PanoramaActorsLayout.FINGER_ORIGINAL_SCALE_TO, PanoramaActorsLayout.FINGER_ORIGINAL_SCALE_TO, PanoramaActorsLayout.FINGER_ORIGINAL_SCALE_DURATION)))));
-                    }
-                }
-                lastScroll = scrollpane.getScrollX();
-                return false;
+                return handleHintFinger();
             }
         });
 
-        //We need to run scroll panes listener handle method at least once
-        scrollpane.fire(new Event());
+        handleHintFinger();
+    }
+
+    private boolean handleHintFinger() {
+        if (scrollpane.getScrollX() != lastScroll) {
+            final Action oldAction = lastHintFingerAction;
+            hintFinger.addAction(Actions.sequence(Actions.fadeOut(PanoramaActorsLayout.FINGER_FADE_OUT), Actions.run(new Runnable() {
+                @Override
+                public void run() {
+                    hintFinger.removeAction(oldAction);
+                    hintFinger.setScale(PanoramaActorsLayout.FINGER_ORIGINAL_SCALE_TO);
+                    hintFinger.setPosition(getStage().getWidth() * PanoramaActorsLayout.STREET_HINT_FINGER_X - getStage().getWidth() * PanoramaActorsLayout.STREET_HINT_FINGER_WIDTH / 2, 0);
+                }
+            })));
+            //ScrollPanes getScrollPercentX method returns NaN if scroll pane wasn't touched yet...
+            float percentScrollX = Float.isNaN(scrollpane.getScrollPercentX()) ? 0 : scrollpane.getScrollPercentX();
+            if (percentScrollX < PanoramaActorsLayout.STREET_HINT_END) {
+                hintFinger.addAction(lastHintFingerAction = Actions.sequence(
+                        Actions.delay(PanoramaActorsLayout.STREET_HINT_DELAY)
+                        , Actions.alpha(0)
+                        , Actions.visible(true)
+                        , Actions.fadeIn(PanoramaActorsLayout.FINGER_FADE_IN)
+                        , Actions.forever(Actions.sequence(
+                                Actions.moveTo(getStage().getWidth() * PanoramaActorsLayout.STREET_HINT_MOVE_TO - hintFinger.getWidth() / 2, hintFinger.getY(), PanoramaActorsLayout.FINGER_MOVE_DURATION)
+                                , Actions.scaleTo(PanoramaActorsLayout.FINGER_SCALE_TO, PanoramaActorsLayout.FINGER_SCALE_TO, PanoramaActorsLayout.FINGER_SCALE_DURATION)
+                                , Actions.moveTo(-hintFinger.getWidth(), hintFinger.getY(), PanoramaActorsLayout.FINGER_MOVE_DURATION)
+                                , Actions.scaleTo(PanoramaActorsLayout.FINGER_ORIGINAL_SCALE_TO, PanoramaActorsLayout.FINGER_ORIGINAL_SCALE_TO, PanoramaActorsLayout.FINGER_ORIGINAL_SCALE_DURATION)))));
+            }
+        }
+        lastScroll = scrollpane.getScrollX();
+        return false;
     }
 
     protected void screenResized(int width, int height) {
-- 
GitLab