Specify TextButton padding in skin json file? - libgdx

I have a typical skin json file, like the sample one here:
https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests-android/assets/data/uiskin.json
is it possible to specify "inner" padding for text buttons (the space around the text to the edge of the button) in the skin file - or can we only do that programatically at runtime?
Thanks

Buttons in scene2d use the background drawable to determine padding. To the best of my knowledge, the only three reliable ways to set the padding of a Button are
Use a 9 patch drawable for the background drawable. The 9 patch defines its own padding.
Programatically modify any Drawable to have padding by using its setTopHeight(), setLeftWidth(), etc. methods. Then apply this Drawable as the Button's background programatically.
Edit: As of LibGDX 1.7.2, this is now possible to do by specifying a TextureRegionDrawable in the JSON file. Use a TextureRegionDrawable as the background of the button and define its padding like this:
com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable: {
paddedWhite: { region: white, leftWidth: 5, rightWidth: 5, topHeight: 4, bottomHeight: 4 }
}
Note that if you simply use button.pad(...), your padding settings will be cancelled the moment the button is pressed the first time.

Along with your .json file, you should also have a .atlas file
in your .atlas file, you define your button
in your .atlas file:
Button.png
size: 70,10
format: RGBA8888
filter: Linear,Linear
repeat: none
Button1
rotate: false
xy: 0, 0
size: 30, 30
orig: 30, 30
offset: 0, 0
index: -1
so if your button size is 30, 30
then if you want your button to be slightly larger on the x axis, then make your orig 20, 30
Hope you like this answer
If you need more help visit
here or find yourself more examples

If you want to make a padiding from the code inside a table.
this is an example from the wiki table:
Padding
Padding is extra space around the edges of a cell.
table.add(nameLabel);
table.add(nameText).width(100).padBottom(10); // Sets bottom padding.
table.row();
table.add(addressLabel);
table.add(addressText).width(100).pad(10); // Sets top, left, bottom, right padding.
look this:
https://github.com/libgdx/libgdx/wiki/Table
https://github.com/EsotericSoftware/tablelayout

Related

Can someone help me flip this Actionscript 3 graphic?

newPlacement = new Block();
newPlacement.graphics.beginFill(0xFFFFFF);
newPlacement.graphics.drawRoundRect(0, 0, 25, 25, 9);
newPlacement.graphics.endFill();
newPlacement.graphics.beginFill(0xFFFFFF);
newPlacement.graphics.drawRect(0,0,10,25);
newPlacement.graphics.endFill();
blockHolder.addChild(newPlacement);
Here is code I am using in Flash to draw a square object on the stage that is jagged on the left and rounded on the right. Can anyone help me figure out how to code the flipped image of this (a square object that is rounded on the left and jagged on the right). Thanks!
Your code is basically adding a rounded rectangle and then placing a regular rectangle on top of that. The regular rectangle is not as wide, so the rounded rectangle doesn't get covered up on the right side.
Option 1: change relative x position of regular rectangle
All you need to do is place the regular rectangle 15 units to the right. So instead of this:
newPlacement.graphics.drawRect(0,0,10,25);
do this:
newPlacement.graphics.drawRect(15,0,10,25);
Option 2: flip the object
But I still like this best:
newPlacement.scaleX = -1;
Personal preference, though.

Fill a percentage of an SVG and animate the fill

Currently, I am working on a project that is comparing state data with data from another country. One data point is percentage of protected land and I want to fill the a percentage of the state that matches the data point. So for example, if 25% of North Carolina is protected, then I want 25% of the state to fill. Currently, I am trying to use an svg and I want the fill to happen on page load.
Any suggestions or resources on how to do this would be greatly appreciated.
Example I drew up in Illustrator:
Here are my two cents:
You can have a linear gradient like this:
<linearGradient y2="0%" x2="100%" y1="0%" x1="0%" id="F1g"><stop stop-color="#00FF00" offset="0%" id="F1gst1"/><stop stop-color="#FFFFFF" offset="0%" id="F1gst2"/></linearGradient>
Then take the first stop element:
var firstStop = document.getElementById('F1gst1');
And then assign the percentage you want fill, with the attribute offset:
percentage = '35%'; firstStop.setAttribute('offset',percentage);
And that is the way in javascript. You need a gradient for every state (you can use a group) and maybe you will need to define a path object with a fill inside every state with the same form, so you can apply the linear gradient to that path fill attribute.
If you need an animation, you can set a setInterval, and add an '1%' every x miliseconds to make the effect, and stop every interval when the desired percentage is reached.
I hope this at least have given you a clue.
Regards.
This can be done by plain css and html: http://jsfiddle.net/haohcraft/rAPN5/1/
Basically, the trick is
You need a image which has a transparent inner field but
non-transparent outer field, like the image in the fiddle. And you
need to set z-index:1 in order to place it above the filled
<div>.
Set the filled <div> and the img to be position: absolute; width:90px; height:90px; in that case.
Then you can adjust the height of the filled div to show the percentage
Well, here's a pretty dumb way in Canvas...(and I'm assuming you mean you want a certain % of interior area filled).
Step 1: Dump a solid image of each state into Canvas
Step 2: Count the number of nonzero pixels
Step 3: Extract the edges using an edge extraction convolution
Step 4: For each line, iterate horizontally within each row within the shape, coloring in pixels until you've reached the x% of the shape you'd like to portray.
It is possible to do this in SVG, but you'd need to hand-tesselate the shape, track all your areas and then hand calculate the ones to fill and it wouldn't do what I think you want - which is to have a state fill up like it's a water container?
An alternative solution is, of course, to 3D print transparent containers in the shape of all 50 states, fill them with colored water to the desired levels. Photograph them, and then manipulate that image via an SVG filter (feImage + feColorMatrix+feComposite) to selectively fill an SVG image. This may be faster than learning tesselation (or Canvas).
ProgressBar looks promising and easy to use:
https://kimmobrunfeldt.github.io/progressbar.js/
Here's a nice Fiddle example:
https://jsfiddle.net/kimmobrunfeldt/72tkyn40/
Javascript:
// progressbar.js#1.0.0 version is used
// Docs: http://progressbarjs.readthedocs.org/en/1.0.0/
var bar = new ProgressBar.Circle(container, {
color: '#aaa',
// This has to be the same size as the maximum width to
// prevent clipping
strokeWidth: 4,
trailWidth: 1,
easing: 'easeInOut',
duration: 1400,
text: {
autoStyleContainer: false
},
from: { color: '#aaa', width: 1 },
to: { color: '#333', width: 4 },
// Set default step function for all animate calls
step: function(state, circle) {
circle.path.setAttribute('stroke', state.color);
circle.path.setAttribute('stroke-width', state.width);
var value = Math.round(circle.value() * 100);
if (value === 0) {
circle.setText('');
} else {
circle.setText(value);
}
}
});
bar.text.style.fontFamily = '"Raleway", Helvetica, sans-serif';
bar.text.style.fontSize = '2rem';
bar.animate(1.0); // Number from 0.0 to 1.0

Flash, AS3: Drawn objects are not the same

I'm playing with Open Flash Chart. Take a look at this chart:
http://teethgrinder.co.uk/open-flash-chart-2/line-solid-dot.php
As you can see, the rounded dot points look ugly. Some of them are more rounded, some of them less, they don't look the same, as they should. I don't know AS3 and have no idea what is the case. I checked the source code:
this.graphics.lineStyle( 0, 0, 0 );
this.graphics.beginFill( colour, 1 );
this.graphics.drawCircle( 0, 0, style.get('dot-size') );
this.graphics.endFill();
I tried to change the size or draw rectangles instead, but they still don't look the same. I guess the problem is somewhere else...
EDIT: I also noticed, that other elements also looks a little bit different (and they shouldn't) - for example axis ticks. My guess is that it is the quality problem. But when I right-click on the flash object, there is an option "quality" and the "high" is set (there is also a "medium" and a "low" to choose). Can I increase the quality level somewhere else?
From hollow dots, I came to realize there is something around each dot which separated each dot from the connecting lines & also produced blurry hollow dots.
Setting the attribute "halo_size" to 0 helped in this case.
I noticed the same gap in your sold dots examples as well. Maybe that's the problem.
To set stage quality, simply use:
stage.quality = "low";
stage.quality = "medium";
stage.quality = "high";
You may set it in the main class itself which happens to be the document class for this project.
I noticed that if the chart size is 400 x 400 these inconsistencies cease to exist. So the problem we see is a scaling problem & not at the place we think.
Besides, Setting line style allows a proper border around the circle. This will at least appear better.
this.graphics.lineStyle(1, colour, 1);
this.graphics.beginFill( colour, 1 );
this.graphics.drawCircle( 0, 0, style.get('dot-size') );
this.graphics.endFill();

HTML5 Canvas and Line Width

I'm drawing line graphs on a canvas. The lines draw fine. The graph is scaled, every segment is drawn, color are ok, etc. My only problem is visually the line width varies. It's almost like the nib of a caligraphy pen. If the stroke is upward the line is thin, if the stroke is horizontal, the line is thicker.
My line thickness is constant, and my strokeStyle is set to black. I don't see any other properties of the canvas that affect such a varying line width but there must be.
Javascript:
var badCanvas = document.getElementById("badCanvas"),
goodCanvas = document.getElementById("goodCanvas"),
bCtx = badCanvas.getContext("2d"),
gCtx = goodCanvas.getContext("2d");
badCanvas.width = goodCanvas.width = badCanvas.height = goodCanvas.height = 300;
// Line example where the lines are blurry weird ect.
// Horizontal
bCtx.beginPath();
bCtx.moveTo(10,10);
bCtx.lineTo(200,10);
bCtx.stroke();
//Verticle
bCtx.beginPath();
bCtx.moveTo(30,30);
bCtx.lineTo(30,200);
bCtx.stroke();
// Proper way to draw them so they are "clear"
//Horizontal
gCtx.beginPath();
gCtx.moveTo(10.5,10.5);
gCtx.lineTo(200.5,10.5);
gCtx.stroke();
//Verticle
gCtx.beginPath();
gCtx.moveTo(30.5,30);
gCtx.lineTo(30.5,200);
gCtx.stroke();
// Change the line width
bCtx.lineWidth = 4;
gCtx.lineWidth = 4;
// Line example where the lines are blurry weird ect.
// Horizontal
bCtx.beginPath();
bCtx.moveTo(10,20.5);
bCtx.lineTo(200,20.5);
bCtx.stroke();
//Verticle
bCtx.beginPath()
bCtx.moveTo(50.5,30);
bCtx.lineTo(50.5,200);
bCtx.stroke();
// Proper way to draw them so they are "clear"
//Horizontal
gCtx.beginPath();
gCtx.moveTo(10,20);
gCtx.lineTo(200,20);
gCtx.stroke();
//Verticle
gCtx.beginPath();
gCtx.moveTo(50,30);
gCtx.lineTo(50,200);
gCtx.stroke();
HTML:
<h2>BadCanvas</h2>
<canvas id="badCanvas"></canvas>
<h2>Good Canvas</h2>
<canvas id="goodCanvas"></canvas>
CSS:
canvas{border:1px solid blue;}
Live Demo
My live demo basically just recreates what the MDN says. For even stroke widths you can use integers for coordinates, for odd stroke widths you want to use .5 to get crisp lines that fill the pixels correctly.
From MDN Article
If you consider a path from (3,1) to (3,5) with a line thickness of
1.0, you end up with the situation in the second image. The actual
area to be filled (dark blue) only extends halfway into the pixels on
either side of the path. An approximation of this has to be rendered,
which means that those pixels being only partially shaded, and results
in the entire area (the light blue and dark blue) being filled in with
a color only half as dark as the actual stroke color. This is what
happens with the 1.0 width line in the previous example code.
To fix this, you have to be very precise in your path creation.
Knowing that a 1.0 width line will extend half a unit to either side
of the path, creating the path from (3.5,1) to (3.5,5) results in the
situation in the third image — the 1.0 line width ends up completely
and precisely filling a single pixel vertical line.
If linewidth is an odd number, just add 0.5 to x or y.
I just solved a problem of a similar nature. It involved a bug in a For loop.
PROBLEM: I had created a for loop to create a series of connected line segments and noticed that the line was thick to start but thinned out significantly by the final segment (no gradients were explicitly used).
FIRST, DEAD END THOUGHT: At first I assumed it was the above pixel issue, but the problem persisted even after forcing all the segments to remain at a constant level.
OBSERVATION: I noticed that I made a newbie's mistake -- I only used a single "ctx.beginPath()" and "ctx.moveTo(posX,posY)" PRIOR to the For loop and a single "ctx.stroke()" AFTER the For loop and the loop itself wrapped a single ctx.lineTo().
SOLUTION: Once I moved all methods (.beginPath(), .moveTo(), .lineTo() and .stroke()) together into the For loop so they would all be hit on each iteration, the problem went away. My connected line had the desired uniform thickness.
Try lineCap = "round" and lineJoin = "round". See "Line Styles" in this PDF to see what these parameters do.
Edit 17-July-2015: Great cheat sheet, but the link is dead. As far as I can tell, there's a copy of it at http://www.cheat-sheets.org/saved-copy/HTML5_Canvas_Cheat_Sheet.pdf.

Actionscript 3.0 drawRect works weird

I have a BitmapData object named myBitmapData. It was loaded of PNG of size 104x104. This PNG represents a red circle on the transparent background.
There is also a Sprite object named myBackground. I want render that red circle into myBackground.
myBackground.graphics.beginBitmapFill(myBitmapData);
myBackground.graphics.drawRect(0, 0, myBitmapData.width, myBitmapData.height);
myBackground.graphics.endFill();
addChild(myBackground);
Everything is fine. I see a red circle in the left top of myBackground.
But when I change the third line to
myBackground.graphics.drawRect(0, 52, myBitmapData.width, myBitmapData.height);
and expect my circle to be translated 52 pixels down, I actually obtain something strange (for me :)): there are two red half-circles (they form like hourglass).
So, the question is: how do I render myBitmapData into the random position of myBackground?
P.S.
In the case of
myBackground.graphics.drawRect(0, 104, myBitmapData.width, myBitmapData.height);
it is circle again :)
This is caused by beginBitmapFill's default repeat = true parameter. There's an example in the docs. Disabling the repetition won't work though, you'd just get a half circle then.
There are several ways to fix this:
Use a Matrix with a translation (displacement) as argument in beginBitmapFill.
Draw the rectangle at 0,0 on another Sprite, and move that sprite to where you want it on the background.
Don't draw directly to the background, but to another bitmap using copyPixels. Then fill the background with that bitmap.