2014-06-29 30 views
7

यहां एक कोड स्निपेट है।libgdx का उपयोग करके, मैं स्क्रॉलपैन में टेक्स्ट और छवियों दोनों को कैसे जोड़ सकता हूं?

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(); 

Result

उत्तर

14

आप छवियों या तालिकाओं को जोड़ने के लिए सूची विजेट का उपयोग नहीं कर सकते हैं या जहां तक ​​मुझे पता है, पाठ के अलावा कुछ भी। हालांकि, आप एक टेबल बना सकते हैं और पृष्ठभूमि को एक नए बनावट में बदल सकते हैं जो किसी घटना के दौरान सूची के प्रभाव को अनुकरण कर सकता है (जैसे माउसमोव इवेंट)। फिर आप उस तालिका को स्क्रॉलपैन में जोड़ सकते हैं जो आपके स्क्रॉल ईवेंट को संभाल सकता है।

यह आपको कोडिंग का एक सा लगेगा, लेकिन यहाँ एक काम उदाहरण मैं आप के लिए मार पड़ी है:

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() { 
} 

}

example

+0

धन्यवाद! यही वही है जो मुझे चाहिए था। – Sym

+0

यह उत्तर वास्तव में सहायक है, धन्यवाद: डी – thekucays

0

तो मैं इस सटीक बात कर ली है और हालांकि यह सबसे अच्छा तरीका है अपने केवल एक चीज मैं यह पता लगाने सकता है नहीं हो सकता है। तालिका में जोड़ने के लिए अपनी छवि को सेट करने के लिए सबसे पहले छवि अभिनेता का उपयोग करें। फिर पंक्ति() को कॉल करने से पहले तालिका में छवि और शीर्षक दोनों जोड़ें। उन्हें सही ढंग से संरेखित करने के लिए, मैं उन्हें बाएं संरेखण के रूप में सेट कर रहा था और फिर टेबल स्पेसिंग को ओवरराइड करने के लिए शीर्षक पर नकारात्मक पैडिंग का उपयोग कर रहा था, हालांकि मुझे यकीन है कि यह एक और तरीका है। इसके अलावा चुने जाने के बाद आप इसे सही कक्षा में डाल सकते हैं और getName() को कॉल कर सकते हैं जिसे आप किसी भी अभिनेता वर्ग में सेट कर सकते हैं ताकि आप इसे वहां कर सकें या बस टेक्स्ट या कुछ प्राप्त कर सकें।

संबंधित मुद्दे

 संबंधित मुद्दे