Search in sources :

Example 6 with Group

use of com.badlogic.gdx.scenes.scene2d.Group in project libgdx by libgdx.

the class StageTest method create.

@Override
public void create() {
    texture = new Texture(Gdx.files.internal("data/badlogicsmall.jpg"));
    texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), false);
    stage = new Stage(new ScreenViewport());
    float loc = (NUM_SPRITES * (32 + SPACING) - SPACING) / 2;
    for (int i = 0; i < NUM_GROUPS; i++) {
        Group group = new Group();
        group.setX((float) Math.random() * (stage.getWidth() - NUM_SPRITES * (32 + SPACING)));
        group.setY((float) Math.random() * (stage.getHeight() - NUM_SPRITES * (32 + SPACING)));
        group.setOrigin(loc, loc);
        fillGroup(group, texture);
        stage.addActor(group);
    }
    uiTexture = new Texture(Gdx.files.internal("data/ui.png"));
    uiTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    ui = new Stage(new ScreenViewport());
    Image blend = new Image(new TextureRegion(uiTexture, 0, 0, 64, 32));
    blend.setAlign(Align.center);
    blend.setScaling(Scaling.none);
    blend.addListener(new InputListener() {

        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            if (stage.getBatch().isBlendingEnabled())
                stage.getBatch().disableBlending();
            else
                stage.getBatch().enableBlending();
            return true;
        }
    });
    blend.setY(ui.getHeight() - 64);
    Image rotate = new Image(new TextureRegion(uiTexture, 64, 0, 64, 32));
    rotate.setAlign(Align.center);
    rotate.setScaling(Scaling.none);
    rotate.addListener(new InputListener() {

        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            rotateSprites = !rotateSprites;
            return true;
        }
    });
    rotate.setPosition(64, blend.getY());
    Image scale = new Image(new TextureRegion(uiTexture, 64, 32, 64, 32));
    scale.setAlign(Align.center);
    scale.setScaling(Scaling.none);
    scale.addListener(new InputListener() {

        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            scaleSprites = !scaleSprites;
            return true;
        }
    });
    scale.setPosition(128, blend.getY());
    {
        Actor shapeActor = new Actor() {

            public void drawDebug(ShapeRenderer shapes) {
                shapes.set(ShapeType.Filled);
                shapes.setColor(getColor());
                shapes.rect(getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());
            }
        };
        shapeActor.setBounds(0, 0, 100, 150);
        shapeActor.setOrigin(50, 75);
        shapeActor.debug();
        sprites.add(shapeActor);
        Group shapeGroup = new Group();
        shapeGroup.setBounds(300, 300, 300, 300);
        shapeGroup.setOrigin(50, 75);
        shapeGroup.setTouchable(Touchable.childrenOnly);
        shapeGroup.addActor(shapeActor);
        stage.addActor(shapeGroup);
    }
    ui.addActor(blend);
    ui.addActor(rotate);
    ui.addActor(scale);
    fps = new Label("fps: 0", new Label.LabelStyle(font, Color.WHITE));
    fps.setPosition(10, 30);
    fps.setColor(0, 1, 0, 1);
    ui.addActor(fps);
    renderer = new ShapeRenderer();
    Gdx.input.setInputProcessor(this);
}
Also used : Group(com.badlogic.gdx.scenes.scene2d.Group) Label(com.badlogic.gdx.scenes.scene2d.ui.Label) ScreenViewport(com.badlogic.gdx.utils.viewport.ScreenViewport) Image(com.badlogic.gdx.scenes.scene2d.ui.Image) Texture(com.badlogic.gdx.graphics.Texture) ShapeRenderer(com.badlogic.gdx.graphics.glutils.ShapeRenderer) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) InputListener(com.badlogic.gdx.scenes.scene2d.InputListener) Actor(com.badlogic.gdx.scenes.scene2d.Actor) Stage(com.badlogic.gdx.scenes.scene2d.Stage) InputEvent(com.badlogic.gdx.scenes.scene2d.InputEvent) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Example 7 with Group

use of com.badlogic.gdx.scenes.scene2d.Group in project libgdx by libgdx.

the class StageDebugTest method create.

@Override
public void create() {
    textureRegion = new TextureRegion(new Texture("data/badlogic.jpg"));
    Gdx.input.setInputProcessor(this);
    stage1 = new Stage();
    stage1.getCamera().position.set(100, 100, 0);
    Group group = new Group();
    // group.setBounds(0, 0, 10, 10);
    // group.setOrigin(25, 50);
    group.setRotation(10);
    group.setScale(1.2f);
    stage1.addActor(group);
    DebugActor actor = new DebugActor();
    actor.setBounds(300, 140, 50, 100);
    actor.setOrigin(25, 50);
    actor.setRotation(-45);
    actor.setScale(2f);
    actor.addAction(forever(rotateBy(360, 8f)));
    group.addActor(actor);
    group.debugAll();
    stage2 = new Stage();
    Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));
    TextButton shortButton = new TextButton("Button short", skin);
    shortButton.debug();
    TextButton longButton = new TextButton("Button loooooooooong", skin);
    longButton.debug();
    Table root = new Table(skin);
    root.setFillParent(true);
    root.setBackground(skin.getDrawable("default-pane"));
    root.defaults().space(6);
    root.setTransform(true);
    root.rotateBy(10);
    root.setScale(1.3f, 1);
    root.debug();
    stage2.addActor(root);
    root.add(shortButton).pad(5);
    root.add(longButton).row();
    root.add("Colspan").colspan(2).row();
    switchStage();
}
Also used : TextButton(com.badlogic.gdx.scenes.scene2d.ui.TextButton) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) Group(com.badlogic.gdx.scenes.scene2d.Group) Table(com.badlogic.gdx.scenes.scene2d.ui.Table) Stage(com.badlogic.gdx.scenes.scene2d.Stage) Skin(com.badlogic.gdx.scenes.scene2d.ui.Skin) Texture(com.badlogic.gdx.graphics.Texture)

Example 8 with Group

use of com.badlogic.gdx.scenes.scene2d.Group in project libgdx by libgdx.

the class StagePerformanceTest method render.

@Override
public void render() {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    if (useStage) {
        stage.act(Gdx.graphics.getDeltaTime());
        stage.getBatch().disableBlending();
        Group root = stage.getRoot();
        Array<Actor> actors = root.getChildren();
        // for(int i = 0; i < actors.size(); i++) {
        // actors.get(i).rotation += 45 * Gdx.graphics.getDeltaTime();
        // }
        stage.draw();
    } else {
        batch.getProjectionMatrix().setToOrtho2D(0, 0, 24, 12);
        batch.getTransformMatrix().idt();
        batch.disableBlending();
        batch.begin();
        for (int i = 0; i < sprites.length; i++) {
            // sprites[i].rotate(45 * Gdx.graphics.getDeltaTime());
            sprites[i].draw(batch);
        }
        batch.end();
    }
    batch.getProjectionMatrix().setToOrtho2D(0, 0, 480, 320);
    batch.enableBlending();
    batch.begin();
    font.setColor(0, 0, 1, 1);
    font.getData().setScale(2);
    font.draw(batch, "fps: " + Gdx.graphics.getFramesPerSecond() + (useStage ? ", stage" : "sprite"), 10, 40);
    batch.end();
    if (Gdx.input.justTouched()) {
        useStage = !useStage;
    }
}
Also used : Group(com.badlogic.gdx.scenes.scene2d.Group) Actor(com.badlogic.gdx.scenes.scene2d.Actor)

Example 9 with Group

use of com.badlogic.gdx.scenes.scene2d.Group in project libgdx by libgdx.

the class StageTest method render.

@Override
public void render() {
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight());
    Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    if (Gdx.input.isTouched()) {
        stage.screenToStageCoordinates(stageCoords.set(Gdx.input.getX(), Gdx.input.getY()));
        Actor actor = stage.hit(stageCoords.x, stageCoords.y, true);
        if (actor != null)
            actor.setColor((float) Math.random(), (float) Math.random(), (float) Math.random(), 0.5f + 0.5f * (float) Math.random());
    }
    Array<Actor> actors = stage.getActors();
    int len = actors.size;
    if (rotateSprites) {
        for (int i = 0; i < len; i++) actors.get(i).rotateBy(Gdx.graphics.getDeltaTime() * 10);
    }
    scale += vScale * Gdx.graphics.getDeltaTime();
    if (scale > 1) {
        scale = 1;
        vScale = -vScale;
    }
    if (scale < 0.5f) {
        scale = 0.5f;
        vScale = -vScale;
    }
    len = sprites.size;
    for (int i = 0; i < len; i++) {
        Actor sprite = sprites.get(i);
        if (rotateSprites)
            sprite.rotateBy(-40 * Gdx.graphics.getDeltaTime());
        else
            sprite.setRotation(0);
        if (scaleSprites) {
            sprite.setScale(scale);
        } else {
            sprite.setScale(1);
        }
    }
    stage.draw();
    renderer.begin(ShapeType.Point);
    renderer.setColor(1, 0, 0, 1);
    len = actors.size;
    for (int i = 0; i < len; i++) {
        Group group = (Group) actors.get(i);
        renderer.point(group.getX() + group.getOriginX(), group.getY() + group.getOriginY(), 0);
    }
    renderer.end();
    fps.setText("fps: " + Gdx.graphics.getFramesPerSecond() + ", actors " + sprites.size + ", groups " + sprites.size);
    ui.draw();
}
Also used : Group(com.badlogic.gdx.scenes.scene2d.Group) Actor(com.badlogic.gdx.scenes.scene2d.Actor)

Example 10 with Group

use of com.badlogic.gdx.scenes.scene2d.Group in project libgdx by libgdx.

the class Widget method validate.

public void validate() {
    if (!layoutEnabled)
        return;
    Group parent = getParent();
    if (fillParent && parent != null) {
        float parentWidth, parentHeight;
        Stage stage = getStage();
        if (stage != null && parent == stage.getRoot()) {
            parentWidth = stage.getWidth();
            parentHeight = stage.getHeight();
        } else {
            parentWidth = parent.getWidth();
            parentHeight = parent.getHeight();
        }
        setSize(parentWidth, parentHeight);
    }
    if (!needsLayout)
        return;
    needsLayout = false;
    layout();
}
Also used : Group(com.badlogic.gdx.scenes.scene2d.Group) Stage(com.badlogic.gdx.scenes.scene2d.Stage)

Aggregations

Group (com.badlogic.gdx.scenes.scene2d.Group)14 Actor (com.badlogic.gdx.scenes.scene2d.Actor)8 Stage (com.badlogic.gdx.scenes.scene2d.Stage)5 Texture (com.badlogic.gdx.graphics.Texture)3 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)3 Layout (com.badlogic.gdx.scenes.scene2d.utils.Layout)2 Batch (com.badlogic.gdx.graphics.g2d.Batch)1 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)1 CpuSpriteBatch (com.badlogic.gdx.graphics.g2d.CpuSpriteBatch)1 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)1 ShapeRenderer (com.badlogic.gdx.graphics.glutils.ShapeRenderer)1 Vector2 (com.badlogic.gdx.math.Vector2)1 InputEvent (com.badlogic.gdx.scenes.scene2d.InputEvent)1 InputListener (com.badlogic.gdx.scenes.scene2d.InputListener)1 Image (com.badlogic.gdx.scenes.scene2d.ui.Image)1 Label (com.badlogic.gdx.scenes.scene2d.ui.Label)1 Skin (com.badlogic.gdx.scenes.scene2d.ui.Skin)1 Table (com.badlogic.gdx.scenes.scene2d.ui.Table)1 TextButton (com.badlogic.gdx.scenes.scene2d.ui.TextButton)1 TextureRegionDrawable (com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable)1