Search in sources :

Example 16 with Actor

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

the class HorizontalGroup method layoutWrapped.

private void layoutWrapped() {
    float prefHeight = getPrefHeight();
    if (prefHeight != lastPrefHeight) {
        lastPrefHeight = prefHeight;
        invalidateHierarchy();
    }
    int align = this.align;
    boolean round = this.round;
    float space = this.space, padBottom = this.padBottom, fill = this.fill, wrapSpace = this.wrapSpace;
    float maxWidth = prefWidth - padLeft - padRight;
    float rowY = prefHeight - padTop, groupWidth = getWidth(), xStart = padLeft, x = 0, rowHeight = 0;
    if ((align & Align.top) != 0)
        rowY += getHeight() - prefHeight;
    else if (// center
    (align & Align.bottom) == 0)
        rowY += (getHeight() - prefHeight) / 2;
    if ((align & Align.right) != 0)
        xStart += groupWidth - prefWidth;
    else if (// center
    (align & Align.left) == 0)
        xStart += (groupWidth - prefWidth) / 2;
    groupWidth -= padRight;
    align = this.rowAlign;
    FloatArray rowSizes = this.rowSizes;
    SnapshotArray<Actor> children = getChildren();
    int i = 0, n = children.size, incr = 1;
    if (reverse) {
        i = n - 1;
        n = -1;
        incr = -1;
    }
    for (int r = 0; i != n; i += incr) {
        Actor child = children.get(i);
        float width, height;
        Layout layout = null;
        if (child instanceof Layout) {
            layout = (Layout) child;
            width = layout.getPrefWidth();
            height = layout.getPrefHeight();
        } else {
            width = child.getWidth();
            height = child.getHeight();
        }
        if (x + width > groupWidth || r == 0) {
            x = xStart;
            if ((align & Align.right) != 0)
                x += maxWidth - rowSizes.get(r);
            else if (// center
            (align & Align.left) == 0)
                x += (maxWidth - rowSizes.get(r)) / 2;
            rowHeight = rowSizes.get(r + 1);
            if (r > 0)
                rowY -= wrapSpace;
            rowY -= rowHeight;
            r += 2;
        }
        if (fill > 0)
            height = rowHeight * fill;
        if (layout != null) {
            height = Math.max(height, layout.getMinHeight());
            float maxHeight = layout.getMaxHeight();
            if (maxHeight > 0 && height > maxHeight)
                height = maxHeight;
        }
        float y = rowY;
        if ((align & Align.top) != 0)
            y += rowHeight - height;
        else if (// center
        (align & Align.bottom) == 0)
            y += (rowHeight - height) / 2;
        if (round)
            child.setBounds(Math.round(x), Math.round(y), Math.round(width), Math.round(height));
        else
            child.setBounds(x, y, width, height);
        x += width + space;
        if (layout != null)
            layout.validate();
    }
}
Also used : FloatArray(com.badlogic.gdx.utils.FloatArray) Layout(com.badlogic.gdx.scenes.scene2d.utils.Layout) Actor(com.badlogic.gdx.scenes.scene2d.Actor)

Example 17 with Actor

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

the class TextField method findNextTextField.

private TextField findNextTextField(Array<Actor> actors, TextField best, Vector2 bestCoords, Vector2 currentCoords, boolean up) {
    for (int i = 0, n = actors.size; i < n; i++) {
        Actor actor = actors.get(i);
        if (actor == this)
            continue;
        if (actor instanceof TextField) {
            TextField textField = (TextField) actor;
            if (textField.isDisabled() || !textField.focusTraversal)
                continue;
            Vector2 actorCoords = actor.getParent().localToStageCoordinates(tmp3.set(actor.getX(), actor.getY()));
            if ((actorCoords.y < currentCoords.y || (actorCoords.y == currentCoords.y && actorCoords.x > currentCoords.x)) ^ up) {
                if (best == null || (actorCoords.y > bestCoords.y || (actorCoords.y == bestCoords.y && actorCoords.x < bestCoords.x)) ^ up) {
                    best = (TextField) actor;
                    bestCoords.set(actorCoords);
                }
            }
        } else if (actor instanceof Group)
            best = findNextTextField(((Group) actor).getChildren(), best, bestCoords, currentCoords, up);
    }
    return best;
}
Also used : Group(com.badlogic.gdx.scenes.scene2d.Group) Vector2(com.badlogic.gdx.math.Vector2) Actor(com.badlogic.gdx.scenes.scene2d.Actor)

Example 18 with Actor

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

the class InterpolationTest method create.

@Override
public void create() {
    Gdx.gl.glClearColor(.3f, .3f, .3f, 1);
    renderer = new ShapeRenderer();
    skin = new Skin(Gdx.files.internal("data/uiskin.json"));
    stage = new Stage(new ScreenViewport());
    resetPositions();
    Field[] interpolationFields = ClassReflection.getFields(Interpolation.class);
    // see how many fields are actually interpolations (for safety; other fields may be added with future)
    int interpolationMembers = 0;
    for (int i = 0; i < interpolationFields.length; i++) if (ClassReflection.isAssignableFrom(Interpolation.class, interpolationFields[i].getDeclaringClass()))
        interpolationMembers++;
    // get interpolation names
    interpolationNames = new String[interpolationMembers];
    for (int i = 0; i < interpolationFields.length; i++) if (ClassReflection.isAssignableFrom(Interpolation.class, interpolationFields[i].getDeclaringClass()))
        interpolationNames[i] = interpolationFields[i].getName();
    selectedInterpolation = interpolationNames[0];
    list = new List(skin);
    list.setItems(interpolationNames);
    list.addListener(new ChangeListener() {

        public void changed(ChangeEvent event, Actor actor) {
            selectedInterpolation = list.getSelected();
            time = 0;
            resetPositions();
        }
    });
    ScrollPane scroll = new ScrollPane(list, skin);
    scroll.setFadeScrollBars(false);
    scroll.setScrollingDisabled(true, false);
    table = new Table();
    table.setFillParent(true);
    table.add(scroll).expandX().left().width(100);
    stage.addActor(table);
    Gdx.input.setInputProcessor(new InputMultiplexer(new InputAdapter() {

        public boolean scrolled(int amount) {
            if (!Gdx.input.isKeyPressed(Keys.CONTROL_LEFT))
                return false;
            duration -= amount / 15f;
            duration = MathUtils.clamp(duration, 0, Float.POSITIVE_INFINITY);
            return true;
        }
    }, stage, new InputAdapter() {

        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
            if (// if "walking" was interrupted by this touch down event
            !Float.isNaN(time))
                // set startPosition to the current position
                startPosition.set(getPosition(time));
            targetPosition.set(stage.screenToStageCoordinates(targetPosition.set(screenX, screenY)));
            time = 0;
            return true;
        }
    }));
}
Also used : Table(com.badlogic.gdx.scenes.scene2d.ui.Table) InputAdapter(com.badlogic.gdx.InputAdapter) ScreenViewport(com.badlogic.gdx.utils.viewport.ScreenViewport) ShapeRenderer(com.badlogic.gdx.graphics.glutils.ShapeRenderer) Field(com.badlogic.gdx.utils.reflect.Field) Interpolation(com.badlogic.gdx.math.Interpolation) InputMultiplexer(com.badlogic.gdx.InputMultiplexer) ScrollPane(com.badlogic.gdx.scenes.scene2d.ui.ScrollPane) Actor(com.badlogic.gdx.scenes.scene2d.Actor) Stage(com.badlogic.gdx.scenes.scene2d.Stage) Skin(com.badlogic.gdx.scenes.scene2d.ui.Skin) List(com.badlogic.gdx.scenes.scene2d.ui.List) ChangeListener(com.badlogic.gdx.scenes.scene2d.utils.ChangeListener)

Example 19 with Actor

use of com.badlogic.gdx.scenes.scene2d.Actor 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 20 with Actor

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

the class MusicTest method create.

@Override
public void create() {
    music = Gdx.audio.newMusic(Gdx.files.internal("data/8.12.mp3"));
    music.play();
    buttons = new TextureRegion(new Texture(Gdx.files.internal("data/playback.png")));
    batch = new SpriteBatch();
    font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), false);
    stage = new Stage();
    Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));
    slider = new Slider(0, 100, 0.1f, false, skin);
    slider.setPosition(200, 20);
    slider.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeEvent event, Actor actor) {
            if (!sliderUpdating && slider.isDragging())
                music.setPosition((slider.getValue() / 100f) * songDuration);
        }
    });
    stage.addActor(slider);
    Gdx.input.setInputProcessor(stage);
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) Slider(com.badlogic.gdx.scenes.scene2d.ui.Slider) Actor(com.badlogic.gdx.scenes.scene2d.Actor) Stage(com.badlogic.gdx.scenes.scene2d.Stage) Skin(com.badlogic.gdx.scenes.scene2d.ui.Skin) ChangeListener(com.badlogic.gdx.scenes.scene2d.utils.ChangeListener) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) Texture(com.badlogic.gdx.graphics.Texture) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch)

Aggregations

Actor (com.badlogic.gdx.scenes.scene2d.Actor)67 ChangeListener (com.badlogic.gdx.scenes.scene2d.utils.ChangeListener)26 Stage (com.badlogic.gdx.scenes.scene2d.Stage)19 TextButton (com.badlogic.gdx.scenes.scene2d.ui.TextButton)19 Label (com.badlogic.gdx.scenes.scene2d.ui.Label)18 Table (com.badlogic.gdx.scenes.scene2d.ui.Table)15 Layout (com.badlogic.gdx.scenes.scene2d.utils.Layout)11 Skin (com.badlogic.gdx.scenes.scene2d.ui.Skin)10 InputEvent (com.badlogic.gdx.scenes.scene2d.InputEvent)9 Texture (com.badlogic.gdx.graphics.Texture)8 Group (com.badlogic.gdx.scenes.scene2d.Group)8 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)7 InputListener (com.badlogic.gdx.scenes.scene2d.InputListener)7 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)6 Dialog (com.badlogic.gdx.scenes.scene2d.ui.Dialog)6 Image (com.badlogic.gdx.scenes.scene2d.ui.Image)6 ScreenViewport (com.badlogic.gdx.utils.viewport.ScreenViewport)6 CheckBox (com.badlogic.gdx.scenes.scene2d.ui.CheckBox)5 ScrollPane (com.badlogic.gdx.scenes.scene2d.ui.ScrollPane)5 Slider (com.badlogic.gdx.scenes.scene2d.ui.Slider)5