Search in sources :

Example 1 with InputEvent

use of io.anuke.ucore.scene.event.InputEvent in project Mindustry by Anuken.

the class LevelDialog method setup.

void setup() {
    Table maps = new Table();
    pane = new ScrollPane(maps);
    pane.setFadeScrollBars(false);
    int maxwidth = 4;
    Table selmode = new Table();
    ButtonGroup<TextButton> group = new ButtonGroup<>();
    selmode.add("$text.level.mode").padRight(15f);
    for (GameMode mode : GameMode.values()) {
        TextButton[] b = { null };
        b[0] = Elements.newButton("$mode." + mode.name() + ".name", "toggle", () -> state.mode = mode);
        b[0].update(() -> b[0].setChecked(state.mode == mode));
        group.add(b[0]);
        selmode.add(b[0]).size(130f, 54f);
    }
    selmode.addButton("?", this::displayGameModeHelp).size(50f, 54f).padLeft(18f);
    content().add(selmode);
    content().row();
    Difficulty[] ds = Difficulty.values();
    float s = 50f;
    Table sdif = new Table();
    sdif.add("$setting.difficulty.name").padRight(15f);
    sdif.defaults().height(s + 4);
    sdif.addImageButton("icon-arrow-left", 10 * 3, () -> {
        state.difficulty = (ds[Mathf.mod(state.difficulty.ordinal() - 1, ds.length)]);
    }).width(s);
    sdif.addButton("", () -> {
    }).update(t -> {
        t.setText(state.difficulty.toString());
        t.setTouchable(Touchable.disabled);
    }).width(180f);
    sdif.addImageButton("icon-arrow-right", 10 * 3, () -> {
        state.difficulty = (ds[Mathf.mod(state.difficulty.ordinal() + 1, ds.length)]);
    }).width(s);
    content().add(sdif);
    content().row();
    int i = 0;
    for (Map map : world.maps().list()) {
        if (!map.visible && !debug)
            continue;
        if (i % maxwidth == 0) {
            maps.row();
        }
        Table inset = new Table("pane-button");
        inset.add("[accent]" + Bundles.get("map." + map.name + ".name", map.name)).pad(3f);
        inset.row();
        inset.label((() -> {
            try {
                return Bundles.format("text.level.highscore", Settings.getInt("hiscore" + map.name));
            } catch (Exception e) {
                Settings.defaults("hiscore" + map.name, 1);
                return Bundles.format("text.level.highscore", 0);
            }
        })).pad(3f);
        inset.pack();
        float images = 154f;
        Stack stack = new Stack();
        Image back = new Image("white");
        back.setColor(map.backgroundColor);
        ImageButton image = new ImageButton(new TextureRegion(map.texture), "togglemap");
        image.row();
        image.add(inset).width(images + 6);
        TextButton[] delete = new TextButton[1];
        if (map.custom) {
            image.row();
            delete[0] = image.addButton("Delete", () -> {
                Timers.run(1f, () -> {
                    ui.showConfirm("$text.level.delete.title", Bundles.format("text.level.delete", Bundles.get("map." + map.name + ".name", map.name)), () -> {
                        world.maps().removeMap(map);
                        reload();
                        Core.scene.setScrollFocus(pane);
                    });
                });
            }).width(images + 16).padBottom(-10f).grow().get();
        }
        Vector2 hit = new Vector2();
        image.addListener(new ClickListener() {

            public void clicked(InputEvent event, float x, float y) {
                image.localToStageCoordinates(hit.set(x, y));
                if (delete[0] != null && (delete[0].getClickListener().isOver() || delete[0].getClickListener().isPressed() || (Core.scene.hit(hit.x, hit.y, true) != null && Core.scene.hit(hit.x, hit.y, true).isDescendantOf(delete[0])))) {
                    return;
                }
                selectedMap = map;
                hide();
                control.playMap(selectedMap);
            }
        });
        image.getImageCell().size(images);
        stack.add(back);
        stack.add(image);
        maps.add(stack).width(170).top().pad(4f);
        maps.marginRight(26);
        i++;
    }
    content().add(pane).uniformX();
    shown(() -> {
        // this is necessary for some reason?
        Timers.run(2f, () -> {
            Core.scene.setScrollFocus(pane);
        });
    });
}
Also used : Map(io.anuke.mindustry.world.Map) Core(io.anuke.ucore.core.Core) Bundles(io.anuke.ucore.util.Bundles) Settings(io.anuke.ucore.core.Settings) Stack(io.anuke.ucore.scene.ui.layout.Stack) Elements(io.anuke.ucore.scene.utils.Elements) InputEvent(io.anuke.ucore.scene.event.InputEvent) ClickListener(io.anuke.ucore.scene.event.ClickListener) io.anuke.ucore.scene.ui(io.anuke.ucore.scene.ui) Vector2(com.badlogic.gdx.math.Vector2) GameMode(io.anuke.mindustry.game.GameMode) Table(io.anuke.ucore.scene.ui.layout.Table) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) Difficulty(io.anuke.mindustry.game.Difficulty) Mathf(io.anuke.ucore.util.Mathf) Timers(io.anuke.ucore.core.Timers) Vars(io.anuke.mindustry.Vars) Touchable(io.anuke.ucore.scene.event.Touchable) Table(io.anuke.ucore.scene.ui.layout.Table) Difficulty(io.anuke.mindustry.game.Difficulty) Stack(io.anuke.ucore.scene.ui.layout.Stack) GameMode(io.anuke.mindustry.game.GameMode) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) Vector2(com.badlogic.gdx.math.Vector2) InputEvent(io.anuke.ucore.scene.event.InputEvent) Map(io.anuke.mindustry.world.Map) ClickListener(io.anuke.ucore.scene.event.ClickListener)

Example 2 with InputEvent

use of io.anuke.ucore.scene.event.InputEvent in project Mindustry by Anuken.

the class TextFieldDialogListener method add.

public static void add(TextField field, int type, int max) {
    field.addListener(new TextFieldDialogListener(field, type, max));
    field.addListener(new InputListener() {

        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            Gdx.input.setOnscreenKeyboardVisible(false);
            return false;
        }
    });
}
Also used : InputListener(io.anuke.ucore.scene.event.InputListener) InputEvent(io.anuke.ucore.scene.event.InputEvent)

Example 3 with InputEvent

use of io.anuke.ucore.scene.event.InputEvent in project Mindustry by Anuken.

the class BlocksFragment method build.

public void build() {
    InputHandler input = control.input();
    new table() {

        {
            abottom();
            aright();
            visible(() -> !state.is(State.menu) && shown);
            blocks = new table() {

                {
                    itemtable = new Table("button");
                    itemtable.setVisible(() -> input.recipe == null && !state.mode.infiniteResources);
                    desctable = new Table("button");
                    desctable.setVisible(() -> hoveredDescriptionRecipe != null || input.recipe != null);
                    desctable.update(() -> {
                        // note: This is required because there is no direct connection between
                        // input.recipe and the description ui. If input.recipe gets set to null
                        // a proper cleanup of the ui elements is required.
                        boolean anyRecipeShown = input.recipe != null || hoveredDescriptionRecipe != null;
                        boolean descriptionTableClean = desctable.getChildren().size == 0;
                        boolean cleanupRequired = !anyRecipeShown && !descriptionTableClean;
                        if (cleanupRequired) {
                            desctable.clear();
                        }
                    });
                    stack.add(itemtable);
                    stack.add(desctable);
                    add(stack).fillX().uniformX();
                    row();
                    new table("pane") {

                        {
                            touchable(Touchable.enabled);
                            int rows = 4;
                            int maxcol = 0;
                            float size = 48;
                            Stack stack = new Stack();
                            ButtonGroup<ImageButton> group = new ButtonGroup<>();
                            Array<Recipe> recipes = new Array<>();
                            for (Section sec : Section.values()) {
                                recipes.clear();
                                Recipes.getBy(sec, recipes);
                                maxcol = Math.max((int) ((float) recipes.size / rows + 1), maxcol);
                            }
                            for (Section sec : Section.values()) {
                                recipes.clear();
                                Recipes.getBy(sec, recipes);
                                Table table = new Table();
                                ImageButton button = new ImageButton("icon-" + sec.name(), "toggle");
                                button.clicked(() -> {
                                    if (!table.isVisible() && input.recipe != null) {
                                        input.recipe = null;
                                    }
                                });
                                button.setName("sectionbutton" + sec.name());
                                add(button).growX().height(54).padLeft(-1).padTop(sec.ordinal() <= 2 ? -10 : -5);
                                button.getImageCell().size(40).padBottom(4).padTop(2);
                                group.add(button);
                                if (sec.ordinal() % 3 == 2 && sec.ordinal() > 0) {
                                    row();
                                }
                                table.margin(4);
                                table.top().left();
                                int i = 0;
                                for (Recipe r : recipes) {
                                    TextureRegion region = Draw.hasRegion(r.result.name() + "-icon") ? Draw.region(r.result.name() + "-icon") : Draw.region(r.result.name());
                                    ImageButton image = new ImageButton(region, "select");
                                    image.addListener(new ClickListener() {

                                        @Override
                                        public void enter(InputEvent event, float x, float y, int pointer, Element fromActor) {
                                            super.enter(event, x, y, pointer, fromActor);
                                            if (hoveredDescriptionRecipe != r) {
                                                hoveredDescriptionRecipe = r;
                                                updateRecipe(r);
                                            }
                                        }

                                        @Override
                                        public void exit(InputEvent event, float x, float y, int pointer, Element toActor) {
                                            super.exit(event, x, y, pointer, toActor);
                                            hoveredDescriptionRecipe = null;
                                            updateRecipe(input.recipe);
                                        }
                                    });
                                    image.clicked(() -> {
                                        // note: input.recipe only gets set here during a click.
                                        // during a hover only the visual description will be updated.
                                        boolean nothingSelectedYet = input.recipe == null;
                                        boolean selectedSomethingElse = !nothingSelectedYet && input.recipe != r;
                                        boolean shouldMakeSelection = nothingSelectedYet || selectedSomethingElse;
                                        if (shouldMakeSelection) {
                                            input.recipe = r;
                                            hoveredDescriptionRecipe = r;
                                            updateRecipe(r);
                                        } else {
                                            input.recipe = null;
                                            hoveredDescriptionRecipe = null;
                                            updateRecipe(null);
                                        }
                                    });
                                    table.add(image).size(size + 8);
                                    image.getImageCell().size(size);
                                    image.update(() -> {
                                        boolean canPlace = !control.tutorial().active() || control.tutorial().canPlace();
                                        boolean has = (state.inventory.hasItems(r.requirements)) && canPlace;
                                        image.setChecked(input.recipe == r);
                                        image.setTouchable(canPlace ? Touchable.enabled : Touchable.disabled);
                                        image.getImage().setColor(has ? Color.WHITE : Hue.lightness(0.33f));
                                    });
                                    if (i % rows == rows - 1)
                                        table.row();
                                    i++;
                                }
                                table.setVisible(button::isChecked);
                                stack.add(table);
                            }
                            row();
                            add(stack).colspan(Section.values().length);
                            margin(10f);
                            marginLeft(1f);
                            marginRight(1f);
                            end();
                        }
                    }.right().bottom().uniformX();
                    row();
                    if (!android) {
                        weapons = new table("button").margin(0).fillX().end().get();
                    }
                    visible(() -> !state.is(State.menu) && shown);
                }
            }.end().get();
        }
    }.end();
    updateWeapons();
}
Also used : InputHandler(io.anuke.mindustry.input.InputHandler) Table(io.anuke.ucore.scene.ui.layout.Table) Element(io.anuke.ucore.scene.Element) Stack(io.anuke.ucore.scene.ui.layout.Stack) Array(com.badlogic.gdx.utils.Array) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) InputEvent(io.anuke.ucore.scene.event.InputEvent) io.anuke.ucore.scene.builders.table(io.anuke.ucore.scene.builders.table) ClickListener(io.anuke.ucore.scene.event.ClickListener)

Aggregations

InputEvent (io.anuke.ucore.scene.event.InputEvent)3 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)2 ClickListener (io.anuke.ucore.scene.event.ClickListener)2 Stack (io.anuke.ucore.scene.ui.layout.Stack)2 Table (io.anuke.ucore.scene.ui.layout.Table)2 Vector2 (com.badlogic.gdx.math.Vector2)1 Array (com.badlogic.gdx.utils.Array)1 Vars (io.anuke.mindustry.Vars)1 Difficulty (io.anuke.mindustry.game.Difficulty)1 GameMode (io.anuke.mindustry.game.GameMode)1 InputHandler (io.anuke.mindustry.input.InputHandler)1 Map (io.anuke.mindustry.world.Map)1 Core (io.anuke.ucore.core.Core)1 Settings (io.anuke.ucore.core.Settings)1 Timers (io.anuke.ucore.core.Timers)1 Element (io.anuke.ucore.scene.Element)1 io.anuke.ucore.scene.builders.table (io.anuke.ucore.scene.builders.table)1 InputListener (io.anuke.ucore.scene.event.InputListener)1 Touchable (io.anuke.ucore.scene.event.Touchable)1 io.anuke.ucore.scene.ui (io.anuke.ucore.scene.ui)1