【问题标题】:Using libgdx, how can I add both text and images to a ScrollPane?使用 libgdx,如何将文本和图像添加到 ScrollPane?
【发布时间】:2014-08-20 05:43:12
【问题描述】:

这是一个代码 sn-p。

itemList = new List(skin, "ariel.32.white");
String[] tmpInv = new String[b+1];
tmpInv[0] = "<Empty>";
a++;
for (Entry<String, String> entry : inventoryItems.entrySet()) {
    tmpInv[a] = entry.getKey(); 
    a++;
    //String key = entry.getKey();
    //Object value = entry.getValue();
    // ...
}
Arrays.sort(tmpInv);

itemList.setItems(tmpInv);

inventoryPane  = new ScrollPane(itemList, skin);

这是我得到的,它工作正常。我想在每个项目前面添加描述性图标,但我似乎无法让它工作。我还需要一些方法来获取添加后所选内容的名称。目前我使用

itemlist.getSelectedIndex();

【问题讨论】:

    标签: image libgdx inline scene2d scrollpane


    【解决方案1】:

    据我所知,您不能使用列表小部件来添加图像或表格或文本以外的任何内容。但是,您可以制作一个表格并将背景 Drawable 更改为新的 Texture,它可以在某些事件发生时模拟列表的效果(如 mouseMove 事件)。然后,您可以将该表添加到可以处理滚动事件的 ScrollPane 中。

    这将花费您一些编码,但这是我为您准备的一个工作示例:

    package <some package>;
    
    import com.badlogic.gdx.Game;
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.Screen;
    import com.badlogic.gdx.graphics.GL20;
    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.graphics.g2d.TextureRegion;
    import com.badlogic.gdx.scenes.scene2d.Event;
    import com.badlogic.gdx.scenes.scene2d.Stage;
    import com.badlogic.gdx.scenes.scene2d.ui.Image;
    import com.badlogic.gdx.scenes.scene2d.ui.Label;
    import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
    import com.badlogic.gdx.scenes.scene2d.ui.Skin;
    import com.badlogic.gdx.scenes.scene2d.ui.Table;
    import com.badlogic.gdx.scenes.scene2d.utils.FocusListener;
    import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
    
    public class ScrollScreenTest implements Screen{
        Game game;
        ScrollPane scrollpane;
        Skin skin;
        Stage stage;
        Table container, table1, table2, table3;
        Texture texture1, texture2, texture3;
    
        public ScrollScreenTest(Game game){
            this.game = game;
        }
        @Override
        public void render(float delta) {
            Gdx.gl.glClearColor(0, 0, 0, 1);    //sets up the clear color (background color) of the screen.
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);  //instructs openGL to actually clear the screen to the newly set clear color.
            stage.draw();
            stage.act(delta);
    
        }
    
        @Override
        public void resize(int width, int height) {
        }
    
        @Override
        public void show() {
    
            // setup skin
            skin = new Skin(Gdx.files.internal("data/uiskin.json"));
    
            texture1 = new Texture(Gdx.files.internal("iron_axe.png"));
            texture2 = new Texture(Gdx.files.internal("iron_dagger.png"));
            texture3 = new Texture(Gdx.files.internal("iron_sword.png"));
    
            // table that holds the scroll pane
            container = new Table();
            container.setWidth(320f);
            container.setHeight(300f);
    
            // tables that hold the data you want to display
            table1 = new Table(skin);
            table1.add(new Image(texture1)).expandY().fillY();
            table1.add(new Label("", skin)).width(10f).expandY().fillY();// a spacer
            table1.add(new Label("Look at this axe I stole!", skin)).expandY().fillY();
    
            table2 = new Table(skin);
            table2.add(new Image(texture2)).expandY().fillY();
            table2.add(new Label("", skin)).width(10f).expandY().fillY();// a spacer
            table2.add(new Label("So dagger, much pointy.", skin)).expandY().fillY();
    
            table3 = new Table(skin);
            table3.add(new Image(texture3)).expandY().fillY();
            table3.add(new Label("", skin)).width(10f).expandY().fillY();// a spacer
            table3.add(new Label("Valyrian steel..", skin)).expandY().fillY();
    
    
            //inner table that is used as a makeshift list.
            Table innerContainer = new Table();
            innerContainer.add(table1).expand().fill();
            innerContainer.row();
            innerContainer.add(table2).expand().fill();
            innerContainer.row();
            innerContainer.add(table3).expand().fill();
    
            // create the scrollpane
            scrollpane = new ScrollPane(innerContainer);
    
            //add the scroll pane to the container
            container.add(scrollpane).fill().expand();
    
            // setup stage
            stage = new Stage();
    
            // add container to the stage
            stage.addActor(container);
    
            // setup input processor (gets clicks and stuff)
            Gdx.input.setInputProcessor(stage);
    
            // setup a listener for the tables with out data
    
            table1.addListener(new FocusListener(){
                @Override
                public boolean handle(Event event){
    
                if (event.toString().equals("mouseMoved")){
                    table1.background(new TextureRegionDrawable(new TextureRegion(new Texture("gray.png"))));
                    return false;
                }
                else if(event.toString().equals("exit")){
                    //table1.setBackground(null);
                    //table1.background("");
                    table1.setBackground(null, false);
    
                    return false;
                }
                    return true;
                }
    
            });
            table2.addListener(new FocusListener(){
                @Override
                public boolean handle(Event event){
    
                if (event.toString().equals("mouseMoved")){
                    table2.background(new TextureRegionDrawable(new TextureRegion(new Texture("gray.png"))));
                    return false;
                }
                else if(event.toString().equals("exit")){
                    //table1.setBackground(null);
                    //table1.background("");
                    table2.setBackground(null, false);
    
                    return false;
                }
                    return true;
                }
    
            });
            table3.addListener(new FocusListener(){
                @Override
                public boolean handle(Event event){
    
                if (event.toString().equals("mouseMoved")){
                    table3.background(new TextureRegionDrawable(new TextureRegion(new Texture("gray.png"))));
                    return false;
                }
                else if(event.toString().equals("exit")){
                    //table1.setBackground(null);
                    //table1.background("");
                    table3.setBackground(null, false);
    
                    return false;
                }
                    return true;
                }
    
            });
        }
    
        @Override
        public void hide() {
        }
    
        @Override
        public void pause() {
        }
    
        @Override
        public void resume() {
        }
    
        @Override
        public void dispose() {
        }
    
    }
    

    【讨论】:

    • 谢谢!这正是我所需要的。
    【解决方案2】:

    如果您想保留列表,您可以继承列表并覆盖 drawItem 方法,如下所示:

    public class ItemList extends List<Entry<String, String>> {
    
        private int alignment;
    
        public ItemList(Skin skin, String styleName) {
            super(skin, styleName);
        }
    
        @Override
        protected GlyphLayout drawItem (Batch batch, BitmapFont font, int index, Entry<String, String> item, float x, float y, float width) {
            String string = toString(item);
    
            TextureAtlas.AtlasRegion image = // get the image, you want to draw
    
            batch.draw(image, x, y-image.regionHeight)
    
            return font.draw(batch, string, x +image.regionWidth +5, y, 0, string.length(), width -image.regionWidth -5, this.alignment, false, "...");
        }
    
        @Override
        public void setAlignment (int alignment) {
            this.alignment = alignment;
        }
    
    }
    
    

    使用此方法,您不必重新实现列表的选择机制。

    【讨论】:

      【解决方案3】:

      所以我做了这件事,虽然它可能不是最好的方法,但它是我唯一能想到的。首先使用 Image actor 来设置要添加到表格中的图像。然后在调用 row() 之前将图像和标题都添加到表中。为了让它们正确对齐,我将它们设置为左对齐,然后在标题上使用负填充来覆盖表格间距,尽管我确信还有另一种方法。同样使用 get selected 您可以将其转换为正确的类并调用 getName() ,您可以在任何演员类中设置它,这样您就可以在那里进行操作,或者只是调用 get text 或其他东西。

      【讨论】:

        猜你喜欢
        • 2018-05-31
        • 2019-12-14
        • 1970-01-01
        • 1970-01-01
        • 2015-02-19
        • 2019-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多