简体   繁体   English

如何使字体文本可点击?

[英]How to make font text clickable?

My game has 3 BitmapFont (later more) on screen. 我的游戏在屏幕上有3个BitmapFont (更多)。 I Want to be able to touch the font and output it's string in the console so I know which one is pressed. 我希望能够触摸字体并在控制台中输出它的字符串,因此我知道按下了哪一个。 I tried to create a rectangle but I was unable to get the string of the touched BitmapFont . 我试图创建一个矩形,但无法获取被触摸的BitmapFont的字符串。

Here is my code to create BitmapFont : 这是我创建BitmapFont代码:

public class simple  implements ApplicationListener {
    private OrthographicCamera camera;
    private SpriteBatch batch;
    BitmapFont font;
    GlyphLayout layout;
    String a1 = "aa";
    String a2 = "bb";
    String a3 = "cc";
    int a = 0;
    @Override
    public void create() {
      camera = new OrthographicCamera();
      camera.setToOrtho(false, 800, 480);
      batch = new SpriteBatch();
      layout = new GlyphLayout();
      font = new BitmapFont(Gdx.files.internal("arial-15.fnt"));
   }
   @Override
   public void render() {
      Gdx.gl.glClearColor(0, 0, 0.2f, 1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);    
      camera.update();
      batch.setProjectionMatrix(camera.combined);
      batch.begin();
      for (int i =1; i< 4;i++){ 
          layout.setText(font, "a"+i);
          font.draw(batch, layout,200+(15*i),200 );
      }
      batch.end();  
  }

You want to have combined functionality of String, BitmapFont, Layout and positioning. 您想要组合使用字符串,位图字体,布局和定位功能。 The best way is to create a class for this clickable font that contains all that we need. 最好的方法是为该可点击字体创建一个包含我们所需所有内容的class I did some work for you since I am a nice guy and I actually have plenty of other things to do :D. 因为我是一个好人,所以我为您做了一些工作,实际上我还有很多其他事情要做:D。

public class ClickableFont {

//Declare the fields
private GlyphLayout layout;
private BitmapFont font;

private String text;

private int posX;
private int posY;

/**
 * Constructs clickable text from a font
 * @param text Text to display
 * @param posX X position of the text
 * @param posY Y position of the text
 */
public ClickableFont(String text, int posX, int posY) {
    this.text = text;
    this.posX = posX;
    this.posY = posY;

    font = new BitmapFont(Gdx.files.internal("arial-15.fnt"));
    layout = new GlyphLayout(font, text);

}

/**
 * @param batch Draws the text using the given SpriteBatch.
 * @param camera Requires a camera to calculate touches between screen and world.
 */
public void update(SpriteBatch batch, OrthographicCamera camera)
{
    checkClicked(camera);

    font.draw(batch, layout, posX, posY);
}

/**
 * Checks if this object is clicked and outputs to console
 * @param camera the camera
 */
private void checkClicked(OrthographicCamera camera)
{
    if (Gdx.input.justTouched())
    {
        //Get screen coordinates
        Vector3 touch = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
        //Transform screen touch to world coordinates using the camera you are drawing with
        camera.unproject(touch);

        //System.out.println(getRectangle());
        //System.out.println(touch);

        if (getRectangle().contains(touch.x, touch.y))
        {
            System.out.println(text + " has been clicked.");
        }
    }
}

/**
 * Creates a rectangle for the sprite to perform collision calculations.
 * Since it seems font.draw draws from top to bottom (coordinate system of LibGDX is not consistent)
 * We have to adept the rectangle position Y position
 * @return rectangle of font bounds
 */
private Rectangle getRectangle()
{
    return new Rectangle(posX, posY - (int)layout.height, (int)layout.width, (int)layout.height);
}
}

As you can see it tackles your problem in steps. 如您所见,它可以逐步解决您的问题。 Tackling problems is all you are doing in programming. 解决问题是您在编程中所要做的。 A rule of thumb is to never make a method more then 10 lines, excluding comments. 经验法则是,永远不要使方法超过10行(注释除外)。 Exceptions can be made but any large method can be broken down into much more readable smaller methods. 可以设置例外,但任何大型方法都可以分解为可读性强的较小方法。

Now how to use this ClickableFont class? 现在如何使用此ClickableFont类?

    public class simple  implements ApplicationListener {
    private OrthographicCamera camera;
    private SpriteBatch batch;
    //folowing are not nececary anymore since it's handled by the new class
    //BitmapFont font; 
    //GlyphLayout layout; 
    //String a1 = "aa";
    //String a2 = "bb";
    //String a3 = "cc";

    int a = 0;

    //Declare a list to hold your clickable fonts
    List<ClickableFont> clickableFonts = new ArrayList<ClickableFont>();

    @Override
    public void create() {
      camera = new OrthographicCamera();
      camera.setToOrtho(false, 800, 480);
      batch = new SpriteBatch();

    //Add clickable fonts to the list
      clickableFonts.add(new ClickableFont("aa", 200, 200));
      clickableFonts.add(new ClickableFont("bb", 200 + 150, 200));
      clickableFonts.add(new ClickableFont("cc", 200 + 150 * 2, 200));
   }

   @Override
   public void render() {
      Gdx.gl.glClearColor(0, 0, 0.2f, 1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);    
      camera.update();
      batch.setProjectionMatrix(camera.combined);
      batch.begin();
      /* replace your loop
      for (int i =1; i< 4;i++){ 
          layout.setText(font, "a"+i);
          font.draw(batch, layout,200+(15*i),200 );
      }*/
      for (ClickableFont font : clickableFonts)
      {
          font.update(batch, camera);
      }
      batch.end();  
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM