Search in sources :

Example 1 with Interpolation

use of com.badlogic.gdx.math.Interpolation 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 2 with Interpolation

use of com.badlogic.gdx.math.Interpolation in project libgdx by libgdx.

the class InterpolationTest method render.

public void render() {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    float bottomLeftX = Gdx.graphics.getWidth() / 2 - graphSize / 2, bottomLeftY = Gdx.graphics.getHeight() / 2 - graphSize / 2;
    // only show up to two decimals
    String text = String.valueOf(duration);
    if (text.length() > 4)
        text = text.substring(0, text.lastIndexOf('.') + 3);
    text = "duration: " + text + " s (ctrl + scroll to change)";
    stage.getBatch().begin();
    list.getStyle().font.draw(stage.getBatch(), text, bottomLeftX + graphSize / 2, bottomLeftY + graphSize + list.getStyle().font.getLineHeight(), 0, Align.center, false);
    stage.getBatch().end();
    renderer.begin(ShapeType.Line);
    // graph bounds
    renderer.rect(bottomLeftX, bottomLeftY, graphSize, graphSize);
    float lastX = bottomLeftX, lastY = bottomLeftY;
    for (float step = 0; step <= steps; step++) {
        Interpolation interpolation = getInterpolation(selectedInterpolation);
        float percent = step / steps;
        float x = bottomLeftX + graphSize * percent, y = bottomLeftY + graphSize * interpolation.apply(percent);
        renderer.line(lastX, lastY, x, y);
        lastX = x;
        lastY = y;
    }
    time += Gdx.graphics.getDeltaTime();
    if (time > duration) {
        // stop "walking"
        time = Float.NaN;
        // set startPosition to targetPosition for next click
        startPosition.set(targetPosition);
    }
    // draw time marker
    renderer.line(bottomLeftX + graphSize * time / duration, bottomLeftY, bottomLeftX + graphSize * time / duration, bottomLeftY + graphSize);
    // draw path
    renderer.setColor(Color.GRAY);
    renderer.line(startPosition, targetPosition);
    renderer.setColor(Color.WHITE);
    renderer.end();
    // draw the position
    renderer.begin(ShapeType.Filled);
    if (// don't mess up position if time is NaN
    !Float.isNaN(time))
        getPosition(time);
    renderer.circle(position.x, position.y, 7);
    renderer.end();
    stage.act();
    stage.draw();
}
Also used : Interpolation(com.badlogic.gdx.math.Interpolation)

Example 3 with Interpolation

use of com.badlogic.gdx.math.Interpolation in project Mindustry by Anuken.

the class PlacementFragment method toggle.

private void toggle(boolean show) {
    float dur = 0.3f;
    Interpolation in = Interpolation.pow3Out;
    if (breaktable.getActions().size != 0 || shown == show)
        return;
    breaktable.getParent().swapActor(breaktable, next);
    if (!show) {
        control.input().breakMode = PlaceMode.none;
        if (control.input().placeMode.delete)
            control.input().placeMode = PlaceMode.none;
        breaktable.actions(Actions.translateBy(-breaktable.getWidth() - 5, 0, dur, in), Actions.call(() -> shown = false));
    } else {
        shown = true;
        breaktable.actions(Actions.translateBy(-breaktable.getTranslation().x - 5, 0, dur, in));
    }
}
Also used : Interpolation(com.badlogic.gdx.math.Interpolation)

Example 4 with Interpolation

use of com.badlogic.gdx.math.Interpolation in project Mindustry by Anuken.

the class Shield method update.

@Override
public void update() {
    float alpha = 0.1f;
    Interpolation interp = Interpolation.fade;
    if (active) {
        uptime = interp.apply(uptime, 1f, alpha * Timers.delta());
    } else {
        uptime = interp.apply(uptime, 0f, alpha * Timers.delta());
        if (uptime <= 0.05f)
            remove();
    }
    uptime = Mathf.clamp(uptime);
    if (!(tile.block() instanceof ShieldBlock)) {
        remove();
        return;
    }
    ShieldBlock block = (ShieldBlock) tile.block();
    Entities.getNearby(bulletGroup, x, y, block.shieldRadius * 2 * uptime + 10, entity -> {
        BulletEntity bullet = (BulletEntity) entity;
        if ((bullet.owner instanceof Enemy || hitPlayers)) {
            float dst = entity.distanceTo(this);
            if (dst < drawRadius() / 2f) {
                ((ShieldBlock) tile.block()).handleBullet(tile, bullet);
            }
        }
    });
}
Also used : Interpolation(com.badlogic.gdx.math.Interpolation) Enemy(io.anuke.mindustry.entities.enemies.Enemy) ShieldBlock(io.anuke.mindustry.world.blocks.types.defense.ShieldBlock) BulletEntity(io.anuke.ucore.entities.BulletEntity)

Example 5 with Interpolation

use of com.badlogic.gdx.math.Interpolation in project Mindustry by Anuken.

the class HudFragment method build.

public void build() {
    // menu at top left
    new table() {

        {
            atop();
            aleft();
            new table() {

                {
                    new table() {

                        {
                            left();
                            float dsize = 58;
                            defaults().size(dsize).left();
                            float isize = 40;
                            menu = new imagebutton("icon-menu", isize, ui.paused::show).get();
                            flip = new imagebutton("icon-arrow-up", isize, () -> {
                                if (wavetable.getActions().size != 0)
                                    return;
                                float dur = 0.3f;
                                Interpolation in = Interpolation.pow3Out;
                                flip.getStyle().imageUp = Core.skin.getDrawable(shown ? "icon-arrow-down" : "icon-arrow-up");
                                if (shown) {
                                    blockfrag.toggle(false, dur, in);
                                    wavetable.actions(Actions.translateBy(0, wavetable.getHeight() + dsize, dur, in), Actions.call(() -> shown = false));
                                    infolabel.actions(Actions.translateBy(0, wavetable.getHeight(), dur, in), Actions.call(() -> shown = false));
                                } else {
                                    shown = true;
                                    blockfrag.toggle(true, dur, in);
                                    wavetable.actions(Actions.translateBy(0, -wavetable.getTranslation().y, dur, in));
                                    infolabel.actions(Actions.translateBy(0, -infolabel.getTranslation().y, dur, in));
                                }
                            }).get();
                            new imagebutton("icon-pause", isize, () -> {
                                if (android)
                                    DebugFragment.printDebugInfo();
                                if (Net.active() && android) {
                                    ui.listfrag.visible = !ui.listfrag.visible;
                                } else {
                                    state.set(state.is(State.paused) ? State.playing : State.paused);
                                }
                            }).update(i -> {
                                if (Net.active() && android) {
                                    i.getStyle().imageUp = Core.skin.getDrawable("icon-players");
                                } else {
                                    i.setDisabled(Net.active());
                                    i.getStyle().imageUp = Core.skin.getDrawable(state.is(State.paused) ? "icon-play" : "icon-pause");
                                }
                            }).get();
                            new imagebutton("icon-settings", isize, () -> {
                                if (Net.active() && android) {
                                    if (ui.chatfrag.chatOpen()) {
                                        ui.chatfrag.hide();
                                    } else {
                                        ui.chatfrag.toggle();
                                    }
                                } else {
                                    ui.settings.show();
                                }
                            }).update(i -> {
                                if (Net.active() && android) {
                                    i.getStyle().imageUp = Core.skin.getDrawable("icon-chat");
                                } else {
                                    i.getStyle().imageUp = Core.skin.getDrawable("icon-settings");
                                }
                            }).get();
                        }
                    }.end();
                    row();
                    new table() {

                        {
                            touchable(Touchable.enabled);
                            visible(() -> shown);
                            addWaveTable();
                        }
                    }.fillX().end();
                    row();
                    visible(() -> !state.is(State.menu));
                    infolabel = new Label(() -> (Settings.getBool("fps") ? (Gdx.graphics.getFramesPerSecond() + " FPS") + (threads.isEnabled() ? " / " + threads.getFPS() + " TPS" : "") + (Net.client() && !gwt ? "\nPing: " + Net.getPing() : "") : ""));
                    row();
                    add(infolabel).size(-1);
                }
            }.end();
        }
    }.end();
    // tutorial ui table
    new table() {

        {
            control.tutorial().buildUI(this);
            visible(() -> control.tutorial().active());
        }
    }.end();
    // paused table
    new table() {

        {
            visible(() -> state.is(State.paused) && !Net.active());
            atop();
            new table("pane") {

                {
                    new label("[orange]< " + Bundles.get("text.paused") + " >").scale(0.75f).pad(6);
                }
            }.end();
        }
    }.end();
    // respawn background table
    new table("white") {

        {
            respawntable = get();
            respawntable.setColor(Color.CLEAR);
            update(t -> {
                if (state.is(State.menu)) {
                    respawntable.setColor(Color.CLEAR);
                }
            });
        }
    }.end();
    // respawn table
    new table() {

        {
            new table("pane") {

                {
                    new label(() -> "[orange]" + Bundles.get("text.respawn") + " " + (int) (control.getRespawnTime() / 60)).scale(0.75f).pad(10);
                    visible(() -> control.getRespawnTime() > 0 && !state.is(State.menu));
                }
            }.end();
        }
    }.end();
    new table() {

        {
            abottom();
            visible(() -> !state.is(State.menu) && control.getSaves().isSaving());
            new label("$text.saveload");
        }
    }.end();
    blockfrag.build();
}
Also used : io.anuke.ucore.scene.builders.table(io.anuke.ucore.scene.builders.table) Core(io.anuke.ucore.core.Core) Bundles(io.anuke.ucore.util.Bundles) Settings(io.anuke.ucore.core.Settings) io.anuke.ucore.scene.builders.imagebutton(io.anuke.ucore.scene.builders.imagebutton) Gdx(com.badlogic.gdx.Gdx) Net(io.anuke.mindustry.net.Net) Color(com.badlogic.gdx.graphics.Color) io.anuke.ucore.scene.builders.label(io.anuke.ucore.scene.builders.label) Label(io.anuke.ucore.scene.ui.Label) ImageButton(io.anuke.ucore.scene.ui.ImageButton) Table(io.anuke.ucore.scene.ui.layout.Table) Actions(io.anuke.ucore.scene.actions.Actions) Interpolation(com.badlogic.gdx.math.Interpolation) Vars(io.anuke.mindustry.Vars) State(io.anuke.mindustry.core.GameState.State) Touchable(io.anuke.ucore.scene.event.Touchable) Interpolation(com.badlogic.gdx.math.Interpolation) Label(io.anuke.ucore.scene.ui.Label) io.anuke.ucore.scene.builders.label(io.anuke.ucore.scene.builders.label) io.anuke.ucore.scene.builders.imagebutton(io.anuke.ucore.scene.builders.imagebutton) io.anuke.ucore.scene.builders.table(io.anuke.ucore.scene.builders.table)

Aggregations

Interpolation (com.badlogic.gdx.math.Interpolation)6 Color (com.badlogic.gdx.graphics.Color)2 Vars (io.anuke.mindustry.Vars)2 State (io.anuke.mindustry.core.GameState.State)2 Core (io.anuke.ucore.core.Core)2 Actions (io.anuke.ucore.scene.actions.Actions)2 io.anuke.ucore.scene.builders.imagebutton (io.anuke.ucore.scene.builders.imagebutton)2 io.anuke.ucore.scene.builders.label (io.anuke.ucore.scene.builders.label)2 io.anuke.ucore.scene.builders.table (io.anuke.ucore.scene.builders.table)2 Touchable (io.anuke.ucore.scene.event.Touchable)2 ImageButton (io.anuke.ucore.scene.ui.ImageButton)2 Label (io.anuke.ucore.scene.ui.Label)2 Table (io.anuke.ucore.scene.ui.layout.Table)2 Bundles (io.anuke.ucore.util.Bundles)2 Gdx (com.badlogic.gdx.Gdx)1 InputAdapter (com.badlogic.gdx.InputAdapter)1 InputMultiplexer (com.badlogic.gdx.InputMultiplexer)1 ShapeRenderer (com.badlogic.gdx.graphics.glutils.ShapeRenderer)1 Actor (com.badlogic.gdx.scenes.scene2d.Actor)1 Stage (com.badlogic.gdx.scenes.scene2d.Stage)1