Libgdx nine patch text button height issue - java

I am using a TextButton to create a dialog bubble effect in my game.
I have created the nine patch file as on the picture
everything seems to be fine until I try to use this bubble.9 with LibGDX, the TextButton is not stretching correctly based on the text height.
You can see that a multilines text extends the TextButton height but not correctly.
Here is the code
Load the bitmap font
FileHandleResolver resolver = new InternalFileHandleResolver();
assetManager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
assetManager.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver));
FreetypeFontLoader.FreeTypeFontLoaderParameter dialogFont = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
dialogFont.fontFileName = Conf.ASSET_FONT;
dialogFont.fontParameters.size = 8;
Initialize the TextButtonStyle
NinePatch patch = atlas.createPatch("bubble");
NinePatchDrawable dialogBoxPatch = new NinePatchDrawable(patch);
buttonStyle = new TextButton.TextButtonStyle();
buttonStyle.up = dialogBoxPatch;
buttonStyle.down = dialogBoxPatch;
buttonStyle.checked = dialogBoxPatch;
buttonStyle.over = dialogBoxPatch;
buttonStyle.font = dialogFont;
buttonStyle.fontColor = Color.BLACK;
actual rendering
dialogBox = new TextButton("some\ntext", buttonStyle);
dialogBox.draw(batch, 1);
Does someone see what the problem is?

I finally found the problem.
I have decided to test the ninepatch with another font and it worked like a charm. To understand what was going on, I have opened the two fonts with glyphrstudio. It appears that the problematic font is not correctly designed.
Bad font
Good font
You can see that the base line and capheight are not correctly setted on the bad font.
I hope this will help other people facing the same problem.

Related

Libgdx Skin not Updating when Changing Font Programmatically

First, I loaded the skin as a json file. In the file, I put two BitmapFont defintions ("font" and "big-font") and left them practically blank (I did load a dummy ".fnt" file to prevent the parser from complaining). I then generated two FreeType Fonts using the following code:
//start font generator
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/button-font.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameters = new FreeTypeFontGenerator.FreeTypeFontParameter();
//default font (the sizes are arbitrary, since it did not work...)
parameters.size = 10;
BitmapFont defaultFont = generator.generateFont(parameters);
//big font
parameters.size = 100;
BitmapFont bigFont = generator.generateFont(parameters);
And set them as the skin fonts like so:
skin.add("font", defaultFont, BitmapFont.class);
skin.add("big-font", bigFont, BitmapFont.class);
The problem is that the gui (particularly the TextButton, which uses the "font" font) does not seem to care about what I did programmatically and keeps using the dummy font that I've set in the json file.
What am I doing wrong? Is there a better way to integrate ttf fonts and json skin parsing?
Thanks in advance for any help.
Follow these steps :
First, create your font from a TTF file
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/button-font.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 10;
BitmapFont defaultFont = generator.generateFont(parameter);
generator.dispose(); // don't forget to dispose to avoid memory leaks!
Create a blank skin using default constructor and add your font to the skin.
Skin skin = new Skin();
skin.add("font",defaultFont);
Add your atlas file to your skin.
skin.addRegion(new TextureAtlas(Gdx.files.internal("mySkin.atlas")));
Load your Json file to your skin.
skin.load(Gdx.files.internal("mySkin.json"));
then, in your JSON file you can reference “font”
{
font: font
}

libGDX freetypefont artifacts

When creating a BitmapFont in libGDX from a .ttf file with FreeTypeFontGenerator the resulting font has serious artifacts around it:
Those artifacts only appear when using mipmapping, so does it have something to do with transparent pixels? I have tried adding padding to the resulting font, but it does not work. I also tried using linear filtering but the resulting font looks pixelated.
Code I am using to generate the fonts:
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("font.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.size = 300;
parameter.color = Color.WHITE;
parameter.characters = "0123456789";
parameter.kerning = true;
parameter.magFilter = Texture.TextureFilter.MipMapLinearLinear;
parameter.minFilter = Texture.TextureFilter.MipMapLinearLinear;
parameter.genMipMaps = true;
font = generator.generateFont(parameter);

libgdx 1.5.5, help font loading-.ttf

Hi I'm making a asteroids clone using java and libGDX and have had some trouble trying to load in a ttf font and simply displaying it and i have no idea what I'm doing wrong I've looked at many sites but they all have outdated information (I'm using the latest version of freetype).
Here is code:
SpriteBatch sb;
BitmapFont font;
sb = new SpriteBatch();
font = new BitmapFont(Gdx.files.internal("fonts/Hyperspace_Bold.ttf"), true);
sb.setColor(1,1,1,1);
sb.begin();
font.draw(sb, Long.toString(player.getScore()), 40, 390);
sb.end();
I've created a folder in my assets folder called fonts fyi.
P.S. players score is just 0
EDIT: the sb.setColor and everything preceding is in its own render method but i just put it here for simplicity purposes. The purpose of this post is that I'm not sure HOW to use a ttf font type and display it in the latest version of libGDX.
Ok i figured it out. Finally! I needed to use a freeTypeFontGenerator like this:
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/Hyperspace_Bold.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 12;
BitmapFont font = generator.generateFont(parameter);
generator.dispose();
then just this in the draw/render method!:
sb.begin();
font.draw(sb, Long.toString(player.getScore()), 40, 390);
sb.end();

Issue regarding dynamically loading Images in loop using Java Swing

public String[] imagesArray = {Images.firstImage, Images.secondImage};
String imagesPath = "/testproject/images/";
for(int i = 0; i<imagesArray.length; i++) {
URL imageURL = this.getClass().getResource(imagesPath+imagesArray[i]);
ImageIcon orignalImageIcon = new ImageIcon(imageURL);
Image newImage = orignalImageIcon.getImage().getScaledInstance(100, 90, java.awt.Image.SCALE_SMOOTH);
ImageIcon newImageIcon = new ImageIcon(newImage);
JButton receiptButton = new JButton(newImageIcon);
receiptButton.setBorder((new EmptyBorder(0,0,0,0)));
toolBar.add(receiptButton);
add(toolBar);
}
Images not shown in my design layout?
The problem is most likely the asynchronous loading nature of using an ImageIcon to load the original images.
If that is the problem:
There is an easy way to test it. Add the orignalImageIcon to the button and see if they all appear.
There is an easy way to fix it. Load the images using ImageIO.read(URL) - a method that will block until the image is completely loaded.

IllegalArgumentException when setting Image in Jigloo

I am trying to make GUIs in SWT using Jigloo and when I want to set images to Labels or Buttons, I can see them in the preview pane. However, when i compile and run it, it throws
SWTResourceManager.getImage: Error getting image img/game/front/test.png, java.lang.IllegalArgumentException: Argument cannot be null
The code it generates looks like this:
opIcon = new Label(this, SWT.NONE);
FormData opIconLData = new FormData();
opIconLData.width = 64;
opIconLData.height = 64;
opIconLData.left = new FormAttachment(0, 1000, 12);
opIconLData.top = new FormAttachment(0, 1000, 12);
opIcon.setLayoutData(opIconLData);
opIcon.setImage(SWTResourceManager.getImage("img/game/front/test.png"));
The SWTResourceManager class seems to be largely undocumented so I'm kinda lost here. Any help?
This method requires an absolute path,
SWTResourceManager.getImage("/home/img/game/front/test.png");

Categories