How to handle the Click output in the Table - libgdx

I have a table and add all the button, below is the button code, it is repeat to make the level0 - 9 button. When I click the button, it will save their number into the Preferences (prefs) and go to the next screen, but all the button also save the last number (10) into the Preferences (prefs). How can I improve it?
for (level = 0; level < 10;level++) {
levelbutton[level].getLabel().setFontScale(1 * witdh / 540, 1 * height / 960);
levelbutton[level].addListener(new ClickListener() {
public void clicked(InputEvent event, float x, float y) {
prefs.putInteger("Level",level);
prefs.flush();
((Game) Gdx.app.getApplicationListener()).setScreen(basicGame);
}
});
leveltable.add(levelbutton[level]).width(170*witdh/540).height(80*height/960);
leveltable.row();
}

Actor class having a name data member that can be used for identification. You can use this for your requirement.
for (level = 0; level < 10;level++) {
levelbutton[level].getLabel().setFontScale(1 * witdh / 540, 1 * height / 960);
levelbutton[level].setName(String.valueOf(level));
levelbutton[level].addListener(new ClickListener() {
public void clicked(InputEvent event, float x, float y) {
Actor target = event.getTarget();
String targetName = target.getName();
if(targetName !=null) {
int levelNum=Integer.parseInt(targetName);
prefs.putInteger("Level",levelNum);
prefs.flush();
((Game) Gdx.app.getApplicationListener()).setScreen(basicGame);
}
}
});
leveltable.add(levelbutton[level]).width(170*witdh/540).height(80*height/960);
leveltable.row();
}
EDIT
Yes getListenerActor() is better option, that returns attached actor of listener.
target = event.getListenerActor();

Related

Using Actions on a Button-LibGdx

I have a play button like this;
private void drawPlayButton() {
playButton = new ImageButton(new TextureRegionDrawable(playTexture), new TextureRegionDrawable(pressTexture));
stage.addActor(playButton);
playButton.setPosition(UIConstants.PLAY_X, UIConstants.PLAY_Y, Align.center);
playButton.addListener(new ActorGestureListener() {
#Override
public void tap(InputEvent event, float x, float y, int count, int button) {
super.tap(event, x, y, count, button);
game.setScreen(new GameScreen(game));
}
});
}
I want to add scale in and out effect to this button using Actions.
I am not that familiar with Actions,I tried something like this;
float duationsec= 0.5f;
playButton.addAction(Actions.sequence(Actions.scaleBy(0.2f,0.2f,duationsec),
Actions.scaleTo(1f, 1f, duationsec)));
img.setOrigin(Align.center);
stage.addActor(playButton);
But this is not giving any effect to the button also same code works for an Image.Is this because button uses drawable texture?
How can I give this effect to a button using Actions?
Also my code is working for one time only.I am calling it in show().If I call it in render(),it works in an abnormal manner.I want this effect to be displaying forever.
Is it possible to achieve this?
Any help would be appreciated.
For performance reason most scene2d.ui groups have transform set to false by default and ImageButton is part of that ui group so you've to enable transform by using setTransform(..) method.
For more detail you can check
https://github.com/libgdx/libgdx/wiki/Scene2d.ui#rotation-and-scale
float duationsec= 0.5f;
playButton.setOrigin(Align.center);
playButton.setTransform(true); // <-- Enable Transform
stage.addActor(playButton);
playButton.addAction(Actions.sequence(Actions.scaleBy(0.2f,0.2f,duationsec),Actions.scaleTo(1f, 1f, duationsec)));
EDIT
If you want to scale up/down on user Click, Do in this way :
playButton.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
playButton.addAction(Actions.sequence(Actions.scaleBy(0.2f,0.2f,duationsec),Actions.scaleTo(1f, 1f, duationsec), Actions.run(new Runnable() {
#Override
public void run() {
game.setScreen(new GameScreen(game));
}
})));
super.clicked(event, x, y);
}
});

Libgdx Box2D create Bodies

in my libgdx tiles 2D jump and run game I want to create Bodies every second at the positin of my cannons, but as far as I am, only one cannon shoots every second.
Can anyone help me how to fix this?
Heres where I create my cannons:
//create cannon bodies/fixtures
cannons = new Array<Cannon>();
for (MapObject object : map.getLayers().get(5).getObjects().getByType(RectangleMapObject.class)) {
recta = ((RectangleMapObject) object).getRectangle();
cannons.add(new Cannon(screen, recta.getX(), recta.getY()));
}
}
public float X() {
return recta.getX();
}
public float Y() {
return recta.getY();
}
Here I add canons( that works for every position) :
private Array<Bullet> bullets;
public Cannon(PlayScreen screen, float x, float y) {
super(screen, x, y);
setBounds(getX(), getY(), getWidth(), getHeight());
}
public Cannon() {
super();
}
#Override
protected void defineTrap() {
b2body = Body.addStatic(b2body, world, 0f, getX(), getY(), 32, x.CANNON_BIT, "cannon");
createBullet();
}
public void createBullet() {
bullets = new Array<Bullet>();
bullets.add(new Bullet(screen, getX(), getY()));
}
Here I add the first bullet(that waoks too for all cannons):
public Bullet(PlayScreen screen, float x, float y) {
super(screen, x, y);
setBounds(getX(), getY(), getWidth(), getHeight());
}
public Bullet() {
}
#Override
public void defineTrap() {
b2body = Body.addDynamic(b2body, world, 0f, getX(), getY(), 8, x.BULLET_BIT, "bullet");
b2body.setLinearVelocity(new Vector2(-1f, 0));
b2body.setGravityScale(0);
}
And here I want every cannon to shoot, but only one does (its my render mehtod):
if (TimeUtils.timeSinceNanos(startTime) > 1000000000) {
bullets = new Array<Bullet>();
bullets.add(new Bullet(this, creator.X(), creator.Y()));
startTime = TimeUtils.nanoTime();
}
I hope you can help me!
You should iterate through all cannons and call some method that can access the private field bullets, I called it addBullet(Screen, float, float). You might to need to do something with creator before addBullet() as I'm unsure what you are doing with it.
if (TimeUtils.timeSinceNanos(startTime) > 1000000000) {
for(int i = 0; i < Cannons.size; i++) {
Cannon cannon = cannons.get(i);
cannon.addBullet(this, creator.X(), creator.Y());
}
startTime = TimeUtils.nanoTime();
}
Then create the addBullet method in the Cannon class.
public void addBullet(Screen screen, float x, float y) {
bullets.add(new Bullet(screen, x, y));
}
As far as I can tell there is no reason to reinitialize the bullet array every time as you have been doing. Just use pop(); or removeIndex(index); to remove the array items that are no longer used. You could add some more methods to the Cannon class
public void removeLastBullet() {
bullets.pop();
}
public void removeBullet(int i) {
bullets.removeIndex(i);
}

How to check if Tree was hit or not in libgdx scene2d?

Basically all I want is to clear selection of the tree if user clicked not on the tree. My current code is:
entityTree.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
if (entityTree.getNodeAt(y) == null) {
entityTree.getSelection().clear();
}
}
});
But it doesn't work for two reasons:
clicked is only called if the tree is actually clicked. If click happens on some button or wherever else, the tree doesn't get a click event.
Current code checks only y-component. It should be combined with code that checks if tree boundaries are hit.
tree.getStage().addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
if (tree.getStage().hit(x, y, true) != tree || tree.getNodeAt(y) == null)
tree.getSelection().clear();
return false;
}
});

Fix touchDragged logic

What i want:
I want allow the user to drag and drop the actors he puts on the screen
1) An actor (32x32 pixel) can be put in a certain area defined by me (a centered rectangle 5x5, 32 pixel each cell)
2) The actors are snapped to an imaginary grid of the screen when added to the stage.
3) This snap to grid is needed also during the drag and drop
4) An actor can't be moved on another one
5) An actor must be rotated by 90 degrees when the user clicks on it
6) The actor can't be moved outside the bounds
What actually i get:
1) ok
2) ok
3) ok (with bugs)
4) not implemented yet
5) ok (with bugs)
6) not implemented yet
The bugs are:
1) The rotations is applied in any case. I don't want this behavior. I don't want to rotate an actor if the user is just doing drag and drop. (i know that this happens because i put this part of the code in the touchDown method)
2) This is hard to describe with my english, i'll try.
I put an actor and i can move it (when i say "move" i mean drag and drop).
I put a second actor and i can move it.
Now, if i try to move the first actor i put, the second one moves over the first (overlapping to it) and starts moving along with it!
Now the code:
Events in the GameStage
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
translateScreenToWorldCoordinates(screenX, screenY);
// check if the user clicked or not in the center area
if(!centerArea.contains(touchPoint.x, touchPoint.y)) {
return true;
}
// rotation
Iterator<Square> squareIterator = squares.iterator();
while(squareIterator.hasNext()) {
Square nextSquare = squareIterator.next();
if(nextSquare.getBounds().contains(touchPoint.x, touchPoint.y)) {
nextSquare.rotate();
return true;
}
}
// add an Actor
touchPoint.set(touchPoint.x - Constants.SQUARE_SIZE / 2, touchPoint.y - Constants.SQUARE_SIZE / 2, 0f);
float snapX = Math.round((touchPoint.x / Constants.SQUARE_SIZE)) * Constants.SQUARE_SIZE;
float snapY = Math.round((touchPoint.y / Constants.SQUARE_SIZE)) * Constants.SQUARE_SIZE;
Square square = new Square(snapX, snapY);
squares.add(square);
addActor(square);
return true;
}
public boolean touchDragged(int screenX, int screenY, int pointer) {
Vector3 newTouchPoint = new Vector3(screenX, screenY, 0f);
getCamera().unproject(newTouchPoint.set(screenX, screenY, 0f));
Square next = null;
Iterator<Square> squareIterator = squares.listIterator();
while(squareIterator.hasNext()) {
next = squareIterator.next();
if(next.getBounds().contains(newTouchPoint.x, newTouchPoint.y)) {
next.setX(newTouchPoint.x - Constants.SQUARE_SIZE / 2);
next.setY(newTouchPoint.y - Constants.SQUARE_SIZE / 2);
continue;
}
}
newTouchPoint.set(newTouchPoint.x - Constants.SQUARE_SIZE / 2, newTouchPoint.y - Constants.SQUARE_SIZE / 2, 0f);
float snapX = Math.round((newTouchPoint.x / Constants.SQUARE_SIZE)) * Constants.SQUARE_SIZE;
float snapY = Math.round((newTouchPoint.y / Constants.SQUARE_SIZE)) * Constants.SQUARE_SIZE;
next.setX(snapX);
next.setY(snapY);
return true;
}
private void translateScreenToWorldCoordinates(int x, int y) {
getCamera().unproject(touchPoint.set(x, y, 0f));
}
Actor
public class Square extends GameActor {
private float rotation;
private ShapeRenderer shapeRenderer;
public Square(float x, float y) {
super(x, y);
shapeRenderer = new ShapeRenderer();
}
#Override
protected void initializeSprite() {
texture = new Texture(Gdx.files.internal(Constants.SQUARE_LOCATION));
}
#Override
public void act(float delta) {
super.act(delta);
}
#Override
protected void updateBounds() {
bounds.setPosition(x, y);
}
public void rotate() {
rotation -= 90;
if(rotation <= -360) {
rotation = 0;
}
setRotation(rotation);
}
public void setX(float x) {
this.x = x;
}
public void setY(float y) {
this.y = y;
}
#Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
/*shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
shapeRenderer.setColor(Color.RED);
shapeRenderer.rect(bounds.getX(), bounds.getY(), Constants.SQUARE_SIZE, Constants.SQUARE_SIZE);
shapeRenderer.end();*/
//batch.begin();
batch.draw(texture, x, y, texture.getWidth() / 2, texture.getHeight() / 2,
texture.getWidth(), texture.getHeight(), 1, 1, rotation, 0, 0, Constants.SQUARE_SIZE, Constants.SQUARE_SIZE, false, false);
}
}
I hope you can help me, i'm new to Libgdx, thank you.
UPDATE
I fixed the rotation bug moving its logic inside the touchUp method.

Resizing panel with flow layout does not invoke the scroll bars

I have a containing panel JPanel with a Flow Layout, the containing panel is in a JScrollPane, the containing panel holds a bunch of other JPanels, inner panels, in it. All the inner panels have the same dimensions. If there are more panels then the containing panel can hold in its width then they are gridded downwards, and if there are more panels then the containing panel can hold in its height, then the inner panels are aligned in the same grid with the exception of the last row which is centered with the row before last.
While I resize the dialog the containing panel extends and the layout flow layout perform its duty, but the scroll bars do not appear although the size of the panel exceeds the bounds of the JScrollPane.
How do I control the appearance of the scroll bars when I resize the containing panel dynamically?
as for the images, they should sum it up:
After extending the dialog width:
Adam
You need to make the containing panel implement Scrollable, and set the preferred scrollable viewport size according to the width.
import java.awt.*;
import javax.swing.*;
public class Test
{
public static void main(String args[])
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JPanel container = new ScrollablePanel();
container.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
for( int i = 0; i < 20; ++i )
{
JPanel p = new JPanel();
p.setBorder(BorderFactory.createLineBorder(Color.RED));
p.setPreferredSize(new Dimension(50, 50));
p.add(new JLabel("" + i));
container.add(p);
}
JScrollPane scroll = new JScrollPane(container);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(scroll);
f.pack();
f.setSize(250, 300);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
}
class ScrollablePanel extends JPanel implements Scrollable
{
public Dimension getPreferredSize()
{
return getPreferredScrollableViewportSize();
}
public Dimension getPreferredScrollableViewportSize()
{
if( getParent() == null )
return getSize();
Dimension d = getParent().getSize();
int c = (int)Math.floor((d.width - getInsets().left - getInsets().right) / 50.0);
if( c == 0 )
return d;
int r = 20 / c;
if( r * c < 20 )
++r;
return new Dimension(c * 50, r * 50);
}
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
{
return 50;
}
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
{
return 10;
}
public boolean getScrollableTracksViewportHeight()
{
return false;
}
public boolean getScrollableTracksViewportWidth()
{
return getParent() != null ? getParent().getSize().width > getPreferredSize().width : true;
}
}
All numbers are hardcoded for simplicity, but you should get the idea.