Получить выбранный элемент из списка - PullRequest
0 голосов
/ 10 марта 2019

В настоящее время я создаю систему инвентаризации, в которой пользователь выбирает элемент из списка, а значок справа будет обновляться в зависимости от выбранного элемента.

Мне нужен способ получить элемент списка, который пользователь выбрал в данный момент. Затем мне нужно использовать этот элемент списка для отображения значка, который увидит пользователь.

В настоящее время я пытался использовать метод getSelected () в списке инвентаря, который, похоже, возвращает только первый элемент в списке. Мне нужен способ получить элемент, который пользователь выбрал в данный момент.

Мне нужно выбрать текущий предмет в списке под названием «Инвентарь».

package com.sps.game.inventory;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.scenes.scene2d.ui.*;

import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.sps.game.controller.InventoryController;
import com.sps.game.controller.PlayerController;

public class PlayerInventory {
    public Stage stage;
    public SpriteBatch sb;
    private Viewport viewport;

    private Skin skin = new Skin(Gdx.files.internal("core/assets/pixthulhuui/pixthulhu-ui.json"));

    private List<Item> inventory;
    private List<Image> itemImages;

    private InventoryController inventoryController;
    private InputProcessor oldInput;


    public PlayerInventory(SpriteBatch sb, PlayerController playerController) {
        this.sb = sb;
        viewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), new OrthographicCamera());
        stage = new Stage(viewport, sb);

        inventoryController = new InventoryController();
        inventory = inventoryController.getInventoryList();
        itemImages = inventoryController.getImageList();
    }

    private void formatting() {
        stage = new Stage();
        Label inventoryLabel = new Label("Inventory", skin);
        Label imageLabel = new Label("Item", skin);

        Table table = new Table(skin);
        table.setDebug(true);
        table.defaults();
        table.center();
        table.setFillParent(true);
        table.add(inventoryLabel);
        table.add(imageLabel);
        table.row();


        table.add(inventory); //need a way to get the current item selected 
        table.add(itemImages.getSelected()); 

        stage.addActor(itemImages);
        stage.addActor(table);
    }
    public void setInput() {
        oldInput = Gdx.input.getInputProcessor(); //Get the old input from the user.
        Gdx.input.setInputProcessor(stage);       //Set the input to now work on the inventory.
    }

    public void update() {
        if (Gdx.input.isKeyPressed(Input.Keys.I) && oldInput == null) {
            formatting();
            setInput();
        }

        if (Gdx.input.isKeyPressed(Input.Keys.O) && oldInput != null) {
            stage.dispose();
            Gdx.input.setInputProcessor(oldInput);
            oldInput = null;
        }
    }

    public void dispose() {
        stage.dispose();
    }

}

1 Ответ

1 голос
/ 11 марта 2019

Я нашел решение, используя Clicklistener.

    inventory.addListener(new ClickListener() {
        public void clicked(InputEvent event, float x, float y) {
            String clickedItem = inventory.getSelected();
            table.add(clickedItem);
            System.out.println(item.getName());
            }
        });
...