From 6cc9288c1330b3bf7768af7b7cd74a72a1ab3611 Mon Sep 17 00:00:00 2001 From: Recrown Date: Tue, 21 Nov 2017 21:40:10 -0600 Subject: [PATCH] better scroll control --- .../ui/components/MusicSelectable.java | 8 +- .../graphics/ui/pages/MusicSelectionPage.java | 101 ++++++++++++++---- 2 files changed, 83 insertions(+), 26 deletions(-) diff --git a/core/src/zero1hd/rhythmbullet/graphics/ui/components/MusicSelectable.java b/core/src/zero1hd/rhythmbullet/graphics/ui/components/MusicSelectable.java index d78fc9b..7e6f032 100755 --- a/core/src/zero1hd/rhythmbullet/graphics/ui/components/MusicSelectable.java +++ b/core/src/zero1hd/rhythmbullet/graphics/ui/components/MusicSelectable.java @@ -67,12 +67,8 @@ public class MusicSelectable extends WidgetGroup implements Disposable { addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { - if (selected) { - deselect(); - } else { - msp.deselectAll(); - select(); - } + msp.deselectAll(); + select(); super.clicked(event, x, y); } }); diff --git a/core/src/zero1hd/rhythmbullet/graphics/ui/pages/MusicSelectionPage.java b/core/src/zero1hd/rhythmbullet/graphics/ui/pages/MusicSelectionPage.java index b85799a..c81db40 100755 --- a/core/src/zero1hd/rhythmbullet/graphics/ui/pages/MusicSelectionPage.java +++ b/core/src/zero1hd/rhythmbullet/graphics/ui/pages/MusicSelectionPage.java @@ -1,11 +1,13 @@ package zero1hd.rhythmbullet.graphics.ui.pages; +import java.util.Observable; +import java.util.Observer; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.Preferences; import com.badlogic.gdx.Input.Keys; +import com.badlogic.gdx.Preferences; import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.graphics.Pixmap; @@ -30,7 +32,7 @@ import zero1hd.rhythmbullet.audio.SongList; import zero1hd.rhythmbullet.graphics.ui.components.MusicSelectable; import zero1hd.rhythmbullet.graphics.ui.components.ScrollText; -public class MusicSelectionPage extends Page { +public class MusicSelectionPage extends Page implements Observer { Preferences musicFileAnnotation; private SongList songList; @@ -57,6 +59,11 @@ public class MusicSelectionPage extends Page { private Image vertSplitBar; private Image horSplitBar; private Texture white; + + private boolean down, up; + private int index; + private float scrollTimer, scrollDelay = 0.2f, scrollDelMod; + public MusicSelectionPage(Skin skin, SongList songList, AssetManager assetManager, Vector3 cameraTarget) { super("Select music", skin); songInfoTable = new Table(); @@ -112,33 +119,56 @@ public class MusicSelectionPage extends Page { addListener(new InputListener() { @Override public boolean keyDown(InputEvent event, int keycode) { - int index; + scrollTimer = 0; + scrollDelMod = 1f; if (keycode == Keys.DOWN) { - if ((index = (songTable.getChildren().indexOf(currentlySelected, true) + 1)) == songTable.getChildren().size) { - index = 0; - } - deselectAll(); - ((MusicSelectable)songTable.getChildren().get(index)).select(); - scrollbar.scrollTo(currentlySelected.getX(), currentlySelected.getY(), currentlySelected.getWidth(), currentlySelected.getHeight()); + down = true; } if (keycode == Keys.UP) { - if ((index = (songTable.getChildren().indexOf(currentlySelected, true) - 1)) < 0) { - - index = songTable.getChildren().size-1; - - } - deselectAll(); - ((MusicSelectable)songTable.getChildren().get(index)).select(); - scrollbar.scrollTo(currentlySelected.getX(), currentlySelected.getY(), currentlySelected.getWidth(), currentlySelected.getHeight()); + up = true; } return super.keyDown(event, keycode); } + + @Override + public boolean keyUp(InputEvent event, int keycode) { + if (keycode == Keys.DOWN) { + down = false; + } + + if (keycode == Keys.UP) { + up = false; + } + return super.keyUp(event, keycode); + } }); } @Override public void act(float delta) { - Gdx.gl.glLineWidth(2); + if (down) { + if (scrollDelMod > 0.25f) { + scrollDelMod -= delta/0.5f; + } + if (scrollTimer <= 0) { + scrollTimer = scrollDelay*scrollDelMod; + scrollDown(); + } else { + scrollTimer -= delta; + } + } + + if (up) { + if (scrollDelMod > 0.25f) { + scrollDelMod -= delta/0.5f; + } + if (scrollTimer <= 0) { + scrollTimer = scrollDelay*scrollDelMod; + scrollUp(); + } else { + scrollTimer -= delta; + } + } super.act(delta); } @@ -188,13 +218,44 @@ public class MusicSelectionPage extends Page { } public void deselectAll() { - for (int i = 0; i < selectables.size; i++) { - selectables.get(i).deselect(); + if (currentlySelected != null) { + currentlySelected.deselect(); + } else { + for (int i = 0; i < selectables.size; i++) { + selectables.get(i).deselect(); + } } + } public void setCurrentlySelected(MusicSelectable currentlySelected) { this.currentlySelected = currentlySelected; } + private void scrollDown() { + if ((index = (songTable.getChildren().indexOf(currentlySelected, true) + 1)) == songTable.getChildren().size) { + index = 0; + } + deselectAll(); + + ((MusicSelectable)songTable.getChildren().get(index)).select(); + scrollbar.scrollTo(currentlySelected.getX(), currentlySelected.getY(), currentlySelected.getWidth(), currentlySelected.getHeight()); + } + + private void scrollUp() { + if ((index = (songTable.getChildren().indexOf(currentlySelected, true) - 1)) < 0) { + index = songTable.getChildren().size-1; + } + deselectAll(); + + ((MusicSelectable)songTable.getChildren().get(index)).select(); + scrollbar.scrollTo(currentlySelected.getX(), currentlySelected.getY(), currentlySelected.getWidth(), currentlySelected.getHeight()); + } + + @Override + public void update(Observable o, Object arg) { + if (o == songList) { + deselectAll(); + } + } }