I created an ImageTextButton in LibGDX:
_rewardedVideo = new Texture(Gdx.files.internal("menus/blue.png"));
_rewardedVideo.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
_rewardedVideoPressed = new Texture(Gdx.files.internal("menus/bluePressed.png"));
_rewardedVideoPressed.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
_symbolRewardedVideo = new Texture(Gdx.files.internal("menus/symbolRewardedVideo.png"));
_symbolRewardedVideo.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
ImageTextButton.ImageTextButtonStyle styleRewardedVideo = new ImageTextButton.ImageTextButtonStyle();
styleRewardedVideo.up = new TextureRegionDrawable(new TextureRegion(_rewardedVideo));
styleRewardedVideo.down = new TextureRegionDrawable(new TextureRegion(_rewardedVideoPressed));
styleRewardedVideo.imageUp = new TextureRegionDrawable(new TextureRegion(_symbolRewardedVideo));
styleRewardedVideo.font = skin.getFont("large-font");
styleRewardedVideo.fontColor = Color.WHITE;
buttonRewardedVideo = new ImageTextButton("WATCH VIDEO", styleRewardedVideo);
buttonRewardedVideo.setPosition(580, 180);
buttonRewardedVideo.setSize(450, 80);
// The last two lines do not work:
buttonRewardedVideo.getLabel().setAlignment(Align.left);
buttonRewardedVideo.getImage().setAlign(Align.right);
I was hoping the button would contain a left-aligned label and a right-aligned image. But it was the opposite:
https://i.ibb.co/QCZPqT8/Image-Text-Button.png
What's wrong and how can I solve it?
An ImageTextButton is a Table, if you look the class it adds first the Image and then the Label...
public ImageTextButton (String text, ImageTextButtonStyle style) {
super(style);
this.style = style;
defaults().space(3);
image = new Image();
image.setScaling(Scaling.fit);
label = new Label(text, new LabelStyle(style.font, style.fontColor));
label.setAlignment(Align.center);
add(image);
add(label);
setStyle(style);
setSize(getPrefWidth(), getPrefHeight());
}
You can create your own class which would be a copy of ImageTextButton and just swap those lines or just clear the table and re-add the items like so...
buttonRewardedVideo = new ImageTextButton("WATCH VIDEO", styleRewardedVideo);
buttonRewardedVideo.clearChildren();
buttonRewardedVideo.add(buttonRewardedVideo.getLabel());
buttonRewardedVideo.add(buttonRewardedVideo.getImage());
buttonRewardedVideo.setPosition(580, 180);
buttonRewardedVideo.setSize(450, 80);
Related
I have a map using Xamarin.Forms.Googlemap in my xamarin forms app.
Is there a way to click on what is behind a pin?
I am creating polygons on the googlemap and then adding custom pins with orchard names on it.
Pin pin = new Pin()
{
Icon = CreateLabels(orchardname, 60),
Flat = true,
Address = string.Empty,
Label = string.Empty,
Position = Xamarin.Forms.GoogleMaps.Bounds.FromPositions(allpositions).Center,
Anchor = new Point(0.5, -1),
IsVisible = _showPropertyzoom
};
protected BitmapDescriptor CreateLabels(string Text, int size)
{
SKBitmap bitmap;
SKCanvas bitmapCanvas;
SKColor white = new SKColor(255,255,255);
SKColor blue = new SKColor(0,88,164);
SKPaint textPaintStroke = new SKPaint { TextSize = size, StrokeWidth=3, Color = white, FakeBoldText=true, Style = SKPaintStyle.Stroke};
SKPaint textPaint = new SKPaint { TextSize = size, Color = blue };
SKRect bounds = new SKRect();
textPaint.MeasureText(Text, ref bounds);
bitmap = new SKBitmap((int)(bounds.Right + 10), (int)(bounds.Height +5));
using (bitmapCanvas = new SKCanvas(bitmap))
{
bitmapCanvas.Clear();
bitmapCanvas.DrawText(Text, bounds.Left, -bounds.Top, textPaintStroke);
bitmapCanvas.DrawText(Text, bounds.Left, -bounds.Top, textPaint);
}
SKData data = SKImage.FromBitmap(bitmap).Encode(SKEncodedImageFormat.Png, 100);
var image = BitmapDescriptorFactory.FromStream(data.AsStream());
return image;
}
The problem i am having is when i click on a polygon to trigger the polygonclicked event i am clicking on the pin so nothing happens.
I can't just use the pinclicked event to trigger the polygonclicked event in this case, because the pin/label can span across multiple polygons.
I have an issue with libgdx BitMapFont, I display labels inside of a scene2d table, but the font is "cut" at some parts of the text (see below).
Here is my code :
For declaring the font :
font12 = new BitmapFont(Gdx.files.internal("fonts/text.fnt"));
font12.setUseIntegerPositions(false);
font12.getData().setScale(0.2f);
For declaring the table :
Table table = new Table();
table.top();
table.setFillParent(true);
LabelStyle lblStyle = new LabelStyle();
lblStyle.font = font12;
scoreLabel =new Label("SCORE", lblStyle);
timeLabel = new Label("TIME", lblStyle);
levelLabel = new Label("LEVEL", lblStyle);
Thank you for your help.
[EDIT]
Here is the code I tried using freetype but this doesn't look smooth :
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/OpenSans-Regular.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 18;
parameter.color = Color.BLACK;
generator.scaleForPixelHeight(18);
parameter.minFilter = Texture.TextureFilter.Linear;
parameter.magFilter = Texture.TextureFilter.Linear;
parameter.mono = false;
parameter.gamma = 2f;
font12 = generator.generateFont(parameter); // font size 12 pixels
font12.setUseIntegerPositions(false);
font12.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
generator.dispose(); // don't forget to dispose to avoid memory leaks!
First thing, you should always avoid scaling fonts because the result is pixelated and looks really bad. You can generator fonts to the correct size that you need within your program using the FreeTypeFontGenerator:
https://github.com/libgdx/libgdx/wiki/Gdx-freetype
When adding components to a table the table sets the size on its child components. Essentially, the table will override the size of the label when the label is added to the table.
To make sure the label keeps the same size, set the size on the table's cell that is holding the label like this:
table.add(label).size(label.getWidth(), label.getHeight());
You can also apply a different size to the label when adding it to the table (if for example you wanted to have extra space around the label):
table.add(label).size(500, 100);
EDIT
This code works for me, give this a try.
private Stage stage;
#Override
public void create () {
stage = new Stage();
stage.setViewport(new ScreenViewport(stage.getViewport().getCamera()));
Table table = new Table();
table.setFillParent(true);
stage.addActor(table);
FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal("internal/arialbd.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter param = new FreeTypeFontGenerator.FreeTypeFontParameter();
param.size = 18;
param.borderColor = new Color(Color.BLACK);
param.borderWidth = 1;
BitmapFont font = gen.generateFont(param);
gen.dispose();
Label.LabelStyle style = new Label.LabelStyle();
style.font = font;
Label label = new Label("Hello World", style);
table.add(label).size(label.getWidth(), label.getHeight());
}
#Override
public void render() {
Gdx.gl.glClearColor(0.6f, 0.8f, 0.8f, 1.0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
#Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
I'm trying to use Skins with TextButtons. However I'm having an issue where the TextureRegions of the Skin are NOT being scaled down to the tablesize.
This is leading to an issue like in this image:
As you can see that is the e from the word Hello. I managed to scale that down to a more reasonable size (still wrong). However the Button textures are still way too large.
Any ideas? Because I am pulling my hair out. I am using a custom viewport with a custom screenspace width and height. Here is the create method for the menu.
public void create(){
menuStage = new Stage();
camera = new OrthographicCamera();
viewPort = new StretchViewport(SCREEN_SPACE_WIDTH, SCREEN_SPACE_HEIGHT, camera);
viewPort.setWorldSize(SCREEN_SPACE_WIDTH, SCREEN_SPACE_HEIGHT);
menuStage.setViewport(viewPort);
devTexture = new Sprite (new Texture(Gdx.files.internal("data/16by9.png"), true));
Image newActor = new Image(devTexture.getTexture());
newActor.setSize(
viewPort.getWorldWidth(),
viewPort.getWorldHeight()
);
menuStage.addActor(newActor);
skin = new Skin(Gdx.files.internal("data/uiskin.json"));
menuTable = new Table();
skin.getFont("default-font").setScale(0.08f);
menuTable.debugAll();
menuTable.setFillParent(true);
TextButton a,b,c;
a = new TextButton("hello", skin);
b = new TextButton("hello", skin);
c = new TextButton("hello", skin);
a.getStyle().up.setMinWidth(2.0f);
b.getStyle().up.setMinHeight(2.0f);
b.getStyle().up.setMinWidth(2.0f);
c.getStyle().up.setMinHeight(2.0f);
c.getStyle().up.setMinWidth(2.0f);
menuTable.row();
menuTable.add(a).size(2.0f, 1.0f);
menuTable.add(b).size(2.0f, 1.0f);
menuTable.add(c).size(2.0f, 1.0f);
menuStage.addActor(menuTable);
camera.translate(
newActor.getWidth() / 2,
newActor.getHeight() / 2
);
}
I'm using geoTools 14.1
I'm trying to plot on an image some points
This is the code I'm using:
double[][] points = new double[8][2];
points[0] = new double[]{45.46433710338643, 9.190417528152478};
points[1] = new double[]{45.46195085146914, 9.189746320685355};
points[2] = new double[]{45.460062304163635, 9.19015527826191};
points[3] = new double[]{45.472950871127445, 9.17363731952788};
points[4] = new double[]{45.4737153001908,9.203728795018847};
points[5] = new double[]{45.4849795331724,9.20162835217198};
points[6] = new double[]{45.48560542313713,9.195953607559215};
points[7] = new double[]{45.48348421787171,9.188765287399292};
final SimpleFeatureType TYPE = DataUtilities.createType("Location",
"location:Point:srid=3857,"+// <- the geometry attribute: Point type
"nome:String," + // <- a String attribute
"id:Integer" // a number attribute
);
FeatureCollection<SimpleFeatureType, SimpleFeature> collection = new DefaultFeatureCollection();
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
CoordinateReferenceSystem pointSrc = CRS.decode("EPSG:4326");
CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:3857");
MathTransform transform = CRS.findMathTransform(pointSrc, targetCRS);
for (int i = 0; i < points.length; i++)
{
double[] coords = points[i];
Point point = geometryFactory.createPoint(new Coordinate(coords[0], coords[1]));
Point converted = (Point) JTS.transform( point, transform);
featureBuilder.add(converted);
featureBuilder.add("Punto "+i);
featureBuilder.add(i);
SimpleFeature feature = featureBuilder.buildFeature(""+i);
logger.info(""+feature+" feature.getDefaultGeometry() "+feature.getDefaultGeometry()+ " feature.getDefaultGeometryProperty() "+feature.getDefaultGeometryProperty());
((DefaultFeatureCollection)collection).add(feature);
}
String wellKnownName = "Circle";
Style style = SLD.createPointStyle(wellKnownName,Color.RED,Color.RED,0f,10f);
FeatureLayer fl = new FeatureLayer(collection, style);
fl.setVisible(true);
fl.setSelected(true);
logger.info(""+fl);
mapcontent.addLayer((org.geotools.map.Layer)fl); //"1010177.1917802,5688070.7096562,1029133.5747922,5704122.4855938"
ReferencedEnvelope bounds = new ReferencedEnvelope(1010177.1917802,1029133.5747922,5688070.7096562, 5704122.4855938, targetCRS);
BufferedImage ret = buildImage(mapcontent, 5000, 5000, bounds, Color.white);
ImageIO.write((RenderedImage) ret, "png", new File("/home/angelo/Scrivania/immagineResult.png"));
It seems to me all correct, but the generated image contains no point.
This is the generated image
As you can see it's all white; I was expecting only 8 red circles on the image... Is there any error in my code? Am I doing anything wrong?
Thank you
Angelo
UPDATE: added build image method
public BufferedImage buildImage(final MapContent map, final int imageWidth,final int imageHeight,ReferencedEnvelope bounds,Color bgcolor) {
GTRenderer renderer = new StreamingRenderer();
renderer.setMapContent(map);
renderer.setMapContent(map);
Rectangle imageBounds = null;
ReferencedEnvelope mapBounds = bounds;
try {
if(bounds==null) mapBounds = map.getMaxBounds();
imageBounds = new Rectangle(imageWidth, imageHeight);
} catch (Exception e) {
failed to access map layers
throw new RuntimeException(e);
}
BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D gr = image.createGraphics();
int type = AlphaComposite.SRC;
gr.setComposite(AlphaComposite.getInstance(type));
Color c = new Color(bgcolor.getRed(), bgcolor.getGreen(), bgcolor.getBlue(), 0);
gr.setBackground(bgcolor);
gr.setColor(c);
gr.fillRect(0, 0, image.getWidth(), image.getHeight());
type = AlphaComposite.SRC_OVER;
gr.setComposite(AlphaComposite.getInstance(type));
try {
renderer.paint(gr, imageBounds, bounds);
} catch (Exception e) {
throw new RuntimeException(e);
}
return image;
}
After a lot of playing I have found the problem :-) There is no bug in your code! There are in fact 8 red dots in that white image, but they are very hard to find!
As the above image shows at 2000% zoom (and panning along the top edge) you will find a dot (I'm assuming the others are there) - the simple answer is either to make the dots 10 times bigger (100px) or the image much smaller (500x500) and in both cases the dots are immediately visible.
I'm trying to make the UI more appealing by setting the main menu buttons to be the same size.
So the "Play" button is a bit bigger than the "Quit" button, and I tried :
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/myfont.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 22;
mButtonSpacefont = generator.generateFont(parameter);
generator.dispose();
// Table for Buttons
Table table=new Table();
table.setSize(800, 480);
// Button Skin
Skin skin = new Skin();
TextureAtlas buttonAtlas = new TextureAtlas(Gdx.files.internal("gfx/buttons.pack"));
skin.addRegions(buttonAtlas);
// BUTTON PLAY
TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.font = mButtonSpacefont;
textButtonStyle.up = skin.getDrawable("button_top");
textButtonStyle.down = skin.getDrawable("button_bottom");
final TextButton buttonPlay = new TextButton("Play", textButtonStyle);
table.add(buttonPlay);
table.row();
// BUTTON QUIT
final TextButton buttonQuit = new TextButton("Quit", textButtonStyle);
buttonQuit.setWidth(buttonPlay.getWidth());// make the Quit button same size as Play button
table.add(buttonQuit).padTop(50);
table.row();
However, this doesn't work! The Quit button is still smaller than the Play button
Is there anything I did wrong ?
To size all your buttons you could use the width method from the Cell class instead, like for example:
table.add(buttonPlay).width(100);
table.row();
table.add(buttonQuit).width(100);
then your buttons should end up having all the same width.