Get Text dimmensions on CCTextFieldTTF - cocos2d-x

Hi I want to know the width of the text inside the CCTextFieldTTF but I can't find a way to do it, I want this width so I can move a CCLabelTTF in that position so it can be like a cursor for the CCTextFieldTTF
Thanks

How about:
float labelWidth = label.contentSize.width;
;)

So I came up with an idea that really works
In CCTextFieldTTF.h declare a new method
float getTextDimensions();
And in your .cpp
float CCTextFieldTTF::getTextDimensions()
{
return CCLabelTTF::getContentSize().width;
}
And know just call your CCTextFieldTTF object
m_pTextField->getTextDimensions()
And do whatever you like with it

Related

LibGDX - Get width of label cell in ImageTextButton

I have ImageTextButtons within a container Table. I would like to dynamically change the width of this table and have the ImageTextButtons respond in a particular manner. What I would like them to do is fade in the label text depending on the percentage of the text shown.
I believe I am dynamically setting the table width and contents correctly. As per the LibGDX documents we should not explicitly set the widgets dimensions, but the table (container) cell dimensions. So for testing, I have a simple clickListener on what of the buttons like this;
button.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
super.clicked(event, x, y);
float max = container.getCell(button).getMaxWidth();
container.getCell(button).width(max*0.9f);
container.invalidate();
}
});
This works fine, I click it and the cell (and the widget) shrink 90% each time.
For the next step, I thought it would be as simple as this, in my custom ImageTextButton's draw() method;
glyphWidth = getLabel().getGlyphLayout().width;
labelCellWidth = getLabelCell().getMinWidth(); ///<--problem here
float glyphOpacityMultiplier = MathUtils.clamp(labelCellWidth/glyphWidth, 0f, 1f);
origLabelColor = getLabel().getColor().cpy();
newLabelColor = origLabelColor.cpy();
newLabelColor = new Color(newLabelColor.r, newLabelColor.g, newLabelColor.b, newLabelColor.a*glyphOpacityMultiplier);
getLabel().setColor(newLabelColor);
super.draw(batch, parentAlpha);
getLabel().setColor(origLabelColor);
This all works, except for the fact I can't find any method to give me with actual width of the Cell that contains the label. Since the ImageTextButton itself is also a Table, I tried to do button.invalidate() in the ClickListener too but it didn't help. These are the methods I have tried so far;
getLabelCell().getMinWidth();
getLabelCell().getMaxWidth();
getLabelCell().getPrefWidth();
getLabelCell().getActorWidth();
getLabelCell().getSpaceLeft();
getLabelCell().getSpaceRight();
None of them seem to update and give me the correct size, however I know something is working somewhere because I turned on "setClip(true)" on the ImageTextButton, and the label text is successfully being clipped within the boundaries of the NinePatch background when it is resized. So how do I get the correct size?
Any help is greatly appreciated!
Oops.. solved it. I thought the cell containing the label would be offset and sized by the NinePatch background but its not, it is full size but has padding. Therefore the answer is;
labelCellWidth = getWidth()-getPadLeft()-getPadRight();

Setting QTextDocument painter's rectangle (where to paint)

I am painting on a window simple html using QTextDocument::drawContents(painter)
I want to do the drawing inside some margins in the window but I don't see a direct way to specify the target rectangle of the painting (in the painter/window).
I guess a few ways to do it:
Using the QTextDocuments::setMargin (although this does not allow different values for left/top.
Placing the html into an styled <div>
Applying a translation transform to the painter.
But all this seems a bit too much for what I want to do and I guess if I a missing something straight (as you do with QPainter::drawText where you tell the target rectangle)
Set the textWidth property to the width of the area where the text is supposed to fit. The clipping rectangle you pass to drawContents will cut the text off vertically if there's too much of it to fit; you can't do much about that of course.
So, this would be the missing function you're after:
void drawContents(QPainter * p, QTextDocument & doc, const QRectF & rect) {
p->save();
p->translate(rect.topLeft());
doc.setTextWidth(rect.width());
doc.drawContents(p, rect);
p->restore();
}
Yes, you do need to jump through a few hoops, that's why it needs to be factored out. It's perhaps lamentable that a similar overload of drawContents doesn't exist.

In Phaser, is there a way to clear screen or clear game stage/world?

I'm looking for a simple and quick way to clear the entire Phaser screen, like how in HTML5 canvas you can erase everything by resetting the width of the canvas to itself. I couldn't find any such method with a search - only graphics.clear(), but that doesn't hit other stuff like text objects. Is there such a way to clear screen?
Thanks.
There are methods to destroy specific elements - obj.kill() and obj.destroy() - but it is possible to delete all elements by calling game.world.removeAll().
#FabiánRodríguez replied, but you can also make a array or object literal with the objects you'd like to remove, so iterate and remove each. That is when you want to group objects.
var layout = {
rect: new Phaser.Rectangle(0, 0, 200, 200)
};
for(var i in layout) {
layout[i].kill();
layout[i].remove();
}

Swing Line break - overriding graphics draw

In the code, I am reading an xml file and painting the contents accordingly on a JPanel.
If the file contains a tag <LineBreak/> I am supposed to insert a Line Break before the next word.
I thought I could use a
g.drawString(..)
but that requires that I know what follows the break, which in this case, I do not.
Is there any other way to do this by overriding the draw method?
Really appreciate the help. Thanks
I think you will need to use FontMetrics (g.getFontMetrics()) to determine the number of vertical pixels needed to display a line of text. When you call drawString to paint the second line of text, you must pass a y value large enough that the second line of text is drawn below the first line of text.
String[] lines = myStringFromXmlDoc.split("\r\n|\r|\n");
int verticalOffset = 0; // your starting offset would probably be more dynamic
foreach(String line : lines)
{
g.drawString(line, 0, verticalOffset);
// use FontMetrics to implement calcLineHeight for yourself
int currentLineHeight = calcLineHeight(line, g);
verticalOffset += currentLineHeight;
}
Edit:
Also see java.awt.font.LineBreakMeasurer for a possibly useful utility class.

Expand div on hover

i tried doing it with css, the closest i got was: http://jsfiddle.net/XyDec/
It kind of works, but don't hide the content inside it and i would like some smooth animation
, so i guess it's scripts time.
i can't write them or don't know where to look for them, could anybody help me ?
you may take a look at the jquery javascript framework (user friendly and powerfull)
here is an example of smooth slide down of div using jquery:
http://api.jquery.com/slideDown/
this is made with one line : $("div").slideDown("slow");
hope it help
Have you tried jQuery? You can treat the div as an object and assign an action to an event, through jQuery methods.
For instance:
function hide(){
$('#the-div').hide('slow')
}
Then you just call the function on the hover event.
If you want to go a step further, you can assign a timer for it to show again. Or even use a callback.
function hide(){
var x = $('#the-div')
$('#the-div').hide('slow', function(x){
//do something with the variable x
})
}
I think that should work.
Hope that helps.
Cheers.