Last active
July 17, 2017 08:01
-
-
Save MobiDevelop/5527397 to your computer and use it in GitHub Desktop.
A ScrollPane that centers on a particular actor.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.badlogic.gdx.tests.examples; | |
import com.badlogic.gdx.math.MathUtils; | |
import com.badlogic.gdx.scenes.scene2d.Actor; | |
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane; | |
import com.badlogic.gdx.scenes.scene2d.ui.Skin; | |
import com.badlogic.gdx.scenes.scene2d.ui.Table; | |
import com.badlogic.gdx.utils.Array; | |
public class PickScrollPane extends ScrollPane { | |
private float pickAmountX; | |
private float pickDifferenceX = -1; | |
private boolean wasPanDragFling = false; | |
private float scrollToPickedSpeed = 2500; | |
public void setScrollToPickedSpeed (float scrollToPickedSpeed) { | |
this.scrollToPickedSpeed = scrollToPickedSpeed; | |
} | |
public PickScrollPane (Actor widget) { | |
super(widget); | |
setFlingTime(0.0f); | |
} | |
public PickScrollPane (Actor widget, Skin skin) { | |
super(widget, skin); | |
setFlingTime(0.0f); | |
} | |
public PickScrollPane (Actor widget, Skin skin, String styleName) { | |
super(widget, skin, styleName); | |
setFlingTime(0.0f); | |
} | |
public PickScrollPane (Actor widget, ScrollPaneStyle style) { | |
super(widget, style); | |
setFlingTime(0.0f); | |
} | |
@Override | |
public void act (float delta) { | |
super.act(delta); | |
if (wasPanDragFling && !isPanning() && !isDragging() && !isFlinging()) { | |
wasPanDragFling = false; | |
scrollToPicked(); | |
} else { | |
if (isPanning() || isDragging() || isFlinging()) { | |
wasPanDragFling = true; | |
} | |
} | |
if (pickDifferenceX != -1) { | |
float diff = scrollToPickedSpeed * delta; | |
float amountX = getScrollX(); | |
if (pickDifferenceX > 0) { | |
amountX += diff; | |
setScrollX(amountX); | |
pickDifferenceX -= diff; | |
if (pickDifferenceX < 0 || amountX >= pickAmountX) { | |
pickDifferenceX = -1; | |
setScrollX(pickAmountX); | |
} | |
} else if (pickDifferenceX < 0) { | |
amountX -= diff; | |
setScrollX(amountX); | |
pickDifferenceX += diff; | |
if (pickDifferenceX > 0 || amountX <= pickAmountX) { | |
pickDifferenceX = -1; | |
setScrollX(pickAmountX); | |
} | |
} | |
} | |
} | |
private void scrollToPicked () { | |
final float width = getWidth(); | |
final float scrollX = getScrollX(); | |
final float maxX = getMaxX(); | |
Actor widget = getWidget(); | |
if (widget instanceof Table) { | |
Table table = (Table)widget; | |
if (scrollX >= maxX || scrollX <= 0) { | |
return; | |
} | |
if (widget instanceof Table) { | |
Table myTable = (Table)widget; | |
Array<Actor> tableActors = myTable.getChildren(); | |
float pickedX = 0; | |
float pickedWidth = 0; | |
if (tableActors.size > 0) { | |
for (Actor a : tableActors) { | |
pickedX = a.getX(); | |
pickedWidth = a.getWidth(); | |
if (scrollX < (pickedX + pickedWidth * 0.5)) { | |
break; | |
} | |
} | |
pickDifferenceX = pickedX - scrollX - (width - pickedWidth) / 2; | |
if (pickDifferenceX == 0) { | |
pickDifferenceX = -1; | |
} | |
pickAmountX = MathUtils.clamp(pickedX - (width - pickedWidth) / 2, 0, maxX); | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************* | |
* Copyright 2011 See AUTHORS file. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
******************************************************************************/ | |
package com.badlogic.gdx.tests.examples; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.graphics.GL10; | |
import com.badlogic.gdx.scenes.scene2d.Actor; | |
import com.badlogic.gdx.scenes.scene2d.InputEvent; | |
import com.badlogic.gdx.scenes.scene2d.InputListener; | |
import com.badlogic.gdx.scenes.scene2d.Stage; | |
import com.badlogic.gdx.scenes.scene2d.ui.Label; | |
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane; | |
import com.badlogic.gdx.scenes.scene2d.ui.Skin; | |
import com.badlogic.gdx.scenes.scene2d.ui.Slider; | |
import com.badlogic.gdx.scenes.scene2d.ui.Table; | |
import com.badlogic.gdx.scenes.scene2d.ui.TextButton; | |
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle; | |
import com.badlogic.gdx.scenes.scene2d.utils.Align; | |
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; | |
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; | |
import com.badlogic.gdx.tests.utils.GdxTest; | |
import com.esotericsoftware.tablelayout.Value; | |
public class PickScrollPaneTest extends GdxTest { | |
private Stage stage; | |
private Table container; | |
public void create () { | |
stage = new Stage(0, 0, false); | |
Skin skin = new Skin(Gdx.files.internal("data/uiskin.json")); | |
Gdx.input.setInputProcessor(stage); | |
container = new Table(); | |
stage.addActor(container); | |
container.setFillParent(true); | |
Table table = new Table(); | |
table.defaults().space(100); | |
Table levels1 = new Table().pad(50); | |
Table levels2 = new Table().pad(50); | |
Table levels3 = new Table().pad(50); | |
Table levels4 = new Table().pad(50); | |
table.debug(); | |
levels1.debug(); | |
levels2.debug(); | |
levels3.debug(); | |
levels4.debug(); | |
int c = 1; | |
for (int y = 0; y < 3; y++) { | |
levels1.row(); | |
for (int x = 0; x < 4; x++) { | |
TextButton label = new TextButton(Integer.toString(c), skin); | |
label.setName("Level" + Integer.toString(c)); | |
label.addListener(levelClickListener); | |
levels1.add(label).expand().fill(); | |
c++; | |
} | |
} | |
for (int y = 0; y < 3; y++) { | |
levels2.row(); | |
for (int x = 0; x < 4; x++) { | |
TextButton label = new TextButton(Integer.toString(c), skin); | |
label.setName("Level" + Integer.toString(c)); | |
label.addListener(levelClickListener); | |
levels2.add(label).expand().fill(); | |
c++; | |
} | |
} | |
for (int y = 0; y < 3; y++) { | |
levels3.row(); | |
for (int x = 0; x < 4; x++) { | |
TextButton label = new TextButton(Integer.toString(c), skin); | |
label.setName("Level" + Integer.toString(c)); | |
label.addListener(levelClickListener); | |
levels3.add(label).expand().fill(); | |
c++; | |
} | |
} | |
for (int y = 0; y < 3; y++) { | |
levels4.row(); | |
for (int x = 0; x < 4; x++) { | |
TextButton label = new TextButton(Integer.toString(c), skin); | |
label.setName("Level" + Integer.toString(c)); | |
label.addListener(levelClickListener); | |
levels4.add(label).expand().fill(); | |
c++; | |
} | |
} | |
table.add(levels1).width(Gdx.graphics.getWidth()).expandY().fillY(); | |
table.add(levels2).width(Gdx.graphics.getWidth()).expandY().fillY(); | |
table.add(levels3).width(Gdx.graphics.getWidth()).expandY().fillY(); | |
table.add(levels4).width(Gdx.graphics.getWidth()).expandY().fillY(); | |
ScrollPane scroll = new PickScrollPane(table, skin); | |
container.add(scroll).expand().fill(); | |
} | |
public void render () { | |
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); | |
stage.act(Gdx.graphics.getDeltaTime()); | |
stage.draw(); | |
Table.drawDebug(stage); | |
} | |
public void resize (int width, int height) { | |
stage.setViewport(width, height, false); | |
} | |
public void dispose () { | |
stage.dispose(); | |
} | |
public boolean needsGL20 () { | |
return false; | |
} | |
public ClickListener levelClickListener = new ClickListener() { | |
@Override | |
public void clicked (InputEvent event, float x, float y) { | |
System.out.println("Click: " + event.getListenerActor().getName()); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment