I am trying to create a side sliding menu similar to Android’s NavigationView in libgdx the problem when I try to close by sliding to the left the menu closes quickly and I want it to close gradually depending on the sliding handled by GestureListener.
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
public class MyGdxGame extends ApplicationAdapter {
private Stage stage;
private Table menuTable;
private boolean isMenuOpen = false;
private float startX;
private float initialX;
private boolean isDraggingFromEdge = false;
@Override
public void create() {
stage = new Stage();
menuTable = new Table();
initialX = -Gdx.graphics.getWidth() / 2;
menuTable.setSize(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight());
menuTable.setPosition(initialX, 0);
menuTable.left().top();
Pixmap pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
pixmap.setColor(Color.BLUE);
pixmap.fill();
Texture texture = new Texture(pixmap);
TextureRegionDrawable textureRegionDrawable =
new TextureRegionDrawable(new TextureRegion(texture));
menuTable.setBackground(textureRegionDrawable);
pixmap.dispose();
menuTable.add(new Label("Opción 1", new Label.LabelStyle(new BitmapFont(), Color.WHITE))).row();
menuTable.add(new Label("Opción 2", new Label.LabelStyle(new BitmapFont(), Color.WHITE))).row();
stage.addActor(menuTable);
GestureDetector gestureDetector =
new GestureDetector(
new GestureDetector.GestureAdapter() {
@Override
public boolean touchDown(float x, float y, int pointer, int button) {
isDraggingFromEdge = x < Gdx.graphics.getWidth() * 0.05 && !isMenuOpen;
if (isDraggingFromEdge) {
isDraggingFromEdge = true;
startX = x;
return true;
} else if (isMenuOpen && x > Gdx.graphics.getWidth() / 2 - 20) {
isDraggingFromEdge = true;
startX = x;
return true;
}
return false;
}
@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
if (isDraggingFromEdge) {
float cx = x - startX;
float newX = initialX + cx;
if (newX > 0) newX = 0;
if (newX < initialX) newX = initialX;
menuTable.setPosition(newX, 0);
}
return true;
}
@Override
public boolean panStop(float x, float y, int pointer, int button) {
if (isDraggingFromEdge) {
float currentX = menuTable.getX();
if (currentX > initialX / 2) {
menuTable.addAction(Actions.moveTo(0, 0, 0.5f));
isMenuOpen = true;
} else {
menuTable.addAction(Actions.moveTo(initialX, 0, 0.5f));
isMenuOpen = false;
}
isDraggingFromEdge = false;
}
return true;
}
});
Gdx.input.setInputProcessor(gestureDetector);
}
@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
@Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
@Override
public void dispose() {
stage.dispose();
}
}
And I want it to change size as I slide to the left so that it doesn’t close abruptly.