Search in sources :

Example 1 with InputEvent

use of com.badlogic.gdx.scenes.scene2d.InputEvent in project AmazingMaze by TheVirtualMachine.

the class FishMiniGame method dialog.

/**
	 * Displays the results dialog.
	 */
public void dialog() {
    message = formatString(answerField.getText());
    Label.LabelStyle labelStyle = new Label.LabelStyle(game.assets.getFont(Assets.MONO_REGULAR, Assets.SMALL_FONT_SIZE), Color.WHITE);
    final Dialog dialog = new Dialog("Results", game.assets.skin);
    final TextButton okButton = new TextButton("OK", game.assets.skin);
    dialog.getButtonTable().bottom();
    if (checkAnswer() == -1) {
        Label label = new Label("Invalid answer. Please try again.", labelStyle);
        label.setScale(.5f);
        label.setWrap(true);
        label.setAlignment(Align.center);
        dialog.add(label).width(500).pad(50);
        dialog.add(okButton).bottom();
        okButton.addListener(new ChangeListener() {

            @Override
            public void changed(ChangeEvent event, Actor actor) {
                if (okButton.isPressed()) {
                    dialog.hide();
                    canvas.setColor(drawColor);
                }
            }
        });
        dialog.addListener(new InputListener() {

            @Override
            public boolean keyDown(InputEvent event, int keycode) {
                if (keycode == Keys.ENTER) {
                    dialog.hide();
                    return true;
                }
                return false;
            }
        });
    } else {
        Label label = new Label("Your answer was: " + message + ". " + "The correct answer was: " + answer + ". " + "You get " + checkAnswer() + " back!", labelStyle);
        game.save.addScore(checkAnswer());
        game.save.setLives(player.getLives());
        label.setScale(.5f);
        label.setWrap(true);
        label.setAlignment(Align.center);
        dialog.add(label).width(500).pad(50);
        dialog.add(okButton).bottom();
        okButton.addListener(new ChangeListener() {

            @Override
            public void changed(ChangeEvent event, Actor actor) {
                if (okButton.isPressed()) {
                    dialog.cancel();
                    if ((game.save.getLevel() - 1) % 5 == 0) {
                        game.setScreen(new ContinueScreen(game, true));
                    } else {
                        game.setScreen(new MazeScreen(game, false));
                    }
                }
            }
        });
        dialog.addListener(new InputListener() {

            @Override
            public boolean keyDown(InputEvent event, int keycode) {
                if (keycode == Keys.ENTER) {
                    if ((game.save.getLevel() - 1) % 5 == 0) {
                        game.setScreen(new ContinueScreen(game, true));
                    } else {
                        game.setScreen(new MazeScreen(game, false));
                    }
                    return true;
                }
                return false;
            }
        });
    }
    dialog.show(stage);
}
Also used : TextButton(com.badlogic.gdx.scenes.scene2d.ui.TextButton) Label(com.badlogic.gdx.scenes.scene2d.ui.Label) InputListener(com.badlogic.gdx.scenes.scene2d.InputListener) Dialog(com.badlogic.gdx.scenes.scene2d.ui.Dialog) Actor(com.badlogic.gdx.scenes.scene2d.Actor) ChangeListener(com.badlogic.gdx.scenes.scene2d.utils.ChangeListener) InputEvent(com.badlogic.gdx.scenes.scene2d.InputEvent)

Example 2 with InputEvent

use of com.badlogic.gdx.scenes.scene2d.InputEvent in project AmazingMaze by TheVirtualMachine.

the class ContinueScreen method highScoreDialog.

/**
	 * Displays the high score dialog.
	 */
public void highScoreDialog() {
    Label.LabelStyle labelStyle = new Label.LabelStyle(game.assets.getFont(Assets.MONO_REGULAR, Assets.SMALL_FONT_SIZE), Color.WHITE);
    final Dialog dialog = new Dialog("High Score", game.assets.skin);
    final TextButton okButton = new TextButton("OK", game.assets.skin);
    dialog.getButtonTable().bottom();
    Label label = new Label("Enter your name:", labelStyle);
    label.setScale(.5f);
    label.setWrap(true);
    label.setAlignment(Align.center);
    final TextField nameField = new TextField("", game.assets.skin);
    dialog.add(label).width(500).pad(50);
    dialog.add(nameField);
    dialog.add(okButton).bottom();
    nameField.setTextFieldListener(new TextFieldListener() {

        @Override
        public void keyTyped(TextField textField, char key) {
            name = formatString(nameField.getText());
            if (!name.equals("")) {
                if (key == (char) 13) {
                    displayHighScores(name);
                }
            }
        }
    });
    okButton.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeEvent event, Actor actor) {
            name = formatString(nameField.getText());
            if (!name.equals("")) {
                if (okButton.isPressed()) {
                    dialog.hide();
                    displayHighScores(name);
                }
            }
        }
    });
    dialog.addListener(new InputListener() {

        @Override
        public boolean keyDown(InputEvent event, int keycode) {
            name = formatString(nameField.getText());
            if (!name.equals("")) {
                if (keycode == Keys.ENTER) {
                    displayHighScores(name);
                    return true;
                }
            }
            return false;
        }
    });
    dialog.show(stage);
}
Also used : TextButton(com.badlogic.gdx.scenes.scene2d.ui.TextButton) Label(com.badlogic.gdx.scenes.scene2d.ui.Label) InputListener(com.badlogic.gdx.scenes.scene2d.InputListener) Dialog(com.badlogic.gdx.scenes.scene2d.ui.Dialog) Actor(com.badlogic.gdx.scenes.scene2d.Actor) TextField(com.badlogic.gdx.scenes.scene2d.ui.TextField) TextFieldListener(com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldListener) ChangeListener(com.badlogic.gdx.scenes.scene2d.utils.ChangeListener) InputEvent(com.badlogic.gdx.scenes.scene2d.InputEvent)

Example 3 with InputEvent

use of com.badlogic.gdx.scenes.scene2d.InputEvent in project Catacomb-Snatch by Catacomb-Snatch.

the class Scene method addTextButton.

/**
     * Adds a new text button
     *
     * @param text The text it contains
     * @param x    The x-position
     * @param y    The y-position
     * @return The created {@link TextButton}
     */
public TextButton addTextButton(String text, int x, int y) {
    final TextButton button = new TextButton(text, Art.skin);
    button.setPosition(x, y);
    button.addListener(new ClickListener() {

        @Override
        public void clicked(InputEvent event, float x, float y) {
            button.act(Gdx.graphics.getDeltaTime());
        }
    });
    addActor(button);
    return button;
}
Also used : TextButton(com.badlogic.gdx.scenes.scene2d.ui.TextButton) InputEvent(com.badlogic.gdx.scenes.scene2d.InputEvent) ClickListener(com.badlogic.gdx.scenes.scene2d.utils.ClickListener)

Example 4 with InputEvent

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

the class DragAndDrop method addSource.

public void addSource(final Source source) {
    DragListener listener = new DragListener() {

        public void dragStart(InputEvent event, float x, float y, int pointer) {
            if (activePointer != -1) {
                event.stop();
                return;
            }
            activePointer = pointer;
            dragStartTime = System.currentTimeMillis();
            payload = source.dragStart(event, getTouchDownX(), getTouchDownY(), pointer);
            event.stop();
            if (cancelTouchFocus && payload != null)
                source.getActor().getStage().cancelTouchFocusExcept(this, source.getActor());
        }

        public void drag(InputEvent event, float x, float y, int pointer) {
            if (payload == null)
                return;
            if (pointer != activePointer)
                return;
            Stage stage = event.getStage();
            Touchable dragActorTouchable = null;
            if (dragActor != null) {
                dragActorTouchable = dragActor.getTouchable();
                dragActor.setTouchable(Touchable.disabled);
            }
            // Find target.
            Target newTarget = null;
            isValidTarget = false;
            float stageX = event.getStageX() + touchOffsetX, stageY = event.getStageY() + touchOffsetY;
            // Prefer touchable actors.
            Actor hit = event.getStage().hit(stageX, stageY, true);
            if (hit == null)
                hit = event.getStage().hit(stageX, stageY, false);
            if (hit != null) {
                for (int i = 0, n = targets.size; i < n; i++) {
                    Target target = targets.get(i);
                    if (!target.actor.isAscendantOf(hit))
                        continue;
                    newTarget = target;
                    target.actor.stageToLocalCoordinates(tmpVector.set(stageX, stageY));
                    break;
                }
            }
            // If over a new target, notify the former target that it's being left behind.
            if (newTarget != target) {
                if (target != null)
                    target.reset(source, payload);
                target = newTarget;
            }
            // Notify new target of drag.
            if (newTarget != null)
                isValidTarget = newTarget.drag(source, payload, tmpVector.x, tmpVector.y, pointer);
            if (dragActor != null)
                dragActor.setTouchable(dragActorTouchable);
            // Add/remove and position the drag actor.
            Actor actor = null;
            if (target != null)
                actor = isValidTarget ? payload.validDragActor : payload.invalidDragActor;
            if (actor == null)
                actor = payload.dragActor;
            if (actor == null)
                return;
            if (dragActor != actor) {
                if (dragActor != null)
                    dragActor.remove();
                dragActor = actor;
                stage.addActor(actor);
            }
            float actorX = event.getStageX() - actor.getWidth() + dragActorX;
            float actorY = event.getStageY() + dragActorY;
            if (keepWithinStage) {
                if (actorX < 0)
                    actorX = 0;
                if (actorY < 0)
                    actorY = 0;
                if (actorX + actor.getWidth() > stage.getWidth())
                    actorX = stage.getWidth() - actor.getWidth();
                if (actorY + actor.getHeight() > stage.getHeight())
                    actorY = stage.getHeight() - actor.getHeight();
            }
            actor.setPosition(actorX, actorY);
        }

        public void dragStop(InputEvent event, float x, float y, int pointer) {
            if (pointer != activePointer)
                return;
            activePointer = -1;
            if (payload == null)
                return;
            if (System.currentTimeMillis() - dragStartTime < dragTime)
                isValidTarget = false;
            if (dragActor != null)
                dragActor.remove();
            if (isValidTarget) {
                float stageX = event.getStageX() + touchOffsetX, stageY = event.getStageY() + touchOffsetY;
                target.actor.stageToLocalCoordinates(tmpVector.set(stageX, stageY));
                target.drop(source, payload, tmpVector.x, tmpVector.y, pointer);
            }
            source.dragStop(event, x, y, pointer, payload, isValidTarget ? target : null);
            if (target != null)
                target.reset(source, payload);
            payload = null;
            target = null;
            isValidTarget = false;
            dragActor = null;
        }
    };
    listener.setTapSquareSize(tapSquareSize);
    listener.setButton(button);
    source.actor.addCaptureListener(listener);
    sourceListeners.put(source, listener);
}
Also used : Actor(com.badlogic.gdx.scenes.scene2d.Actor) Stage(com.badlogic.gdx.scenes.scene2d.Stage) InputEvent(com.badlogic.gdx.scenes.scene2d.InputEvent) Touchable(com.badlogic.gdx.scenes.scene2d.Touchable)

Example 5 with InputEvent

use of com.badlogic.gdx.scenes.scene2d.InputEvent 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)

Aggregations

InputEvent (com.badlogic.gdx.scenes.scene2d.InputEvent)89 ClickListener (com.badlogic.gdx.scenes.scene2d.utils.ClickListener)55 TextButton (com.badlogic.gdx.scenes.scene2d.ui.TextButton)35 Label (com.badlogic.gdx.scenes.scene2d.ui.Label)34 InputListener (com.badlogic.gdx.scenes.scene2d.InputListener)32 Actor (com.badlogic.gdx.scenes.scene2d.Actor)30 Stage (com.badlogic.gdx.scenes.scene2d.Stage)30 Table (com.badlogic.gdx.scenes.scene2d.ui.Table)30 Skin (com.badlogic.gdx.scenes.scene2d.ui.Skin)20 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)18 Texture (com.badlogic.gdx.graphics.Texture)17 ChangeListener (com.badlogic.gdx.scenes.scene2d.utils.ChangeListener)15 Image (com.badlogic.gdx.scenes.scene2d.ui.Image)13 ImageButton (com.badlogic.gdx.scenes.scene2d.ui.ImageButton)11 TextureRegionDrawable (com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable)11 Button (com.badlogic.gdx.scenes.scene2d.ui.Button)10 ScrollPane (com.badlogic.gdx.scenes.scene2d.ui.ScrollPane)10 TextField (com.badlogic.gdx.scenes.scene2d.ui.TextField)10 Sprite (com.badlogic.gdx.graphics.g2d.Sprite)8 Dialog (com.badlogic.gdx.scenes.scene2d.ui.Dialog)7