Attributed labels in Cocos2d - cocos2d-iphone

How can I create an attributed label in Cocos2d?
Specifically I'm looking for a label where the color of each word can be unique. I read about ZAttributed string however the Github page no longer exists. Also searching the Cocos2d v2.1-beta2 codebase doesn't turn up anything for attributed string on iOS.
If nothing exists I'll probably just create my own object. I think everything would be straight forward except possibly the spacing between groups of words across a color change boundry.

CCLabelBMFont label is a series of textures that can be manipulated like any normal sprite. You can run actions on them, transform them and change their color.
Letters are tagged by their position
CCSprite *gletter = (CCSprite*)[label getChildByTag:0];

Related

how to create an outline of a text with fonts with separate polygons for each letter?

I want the outline of text with the standard Windows font "Bahnschrift". I tried to convert it with "Path->Object to Path" and then setting fill to X and stroke to black, but looks like the font is constructed with separate polygons for each letter. See this example:
Top is Bahnschrift, and bottom is Arial which works fine.
Is it possible to calculate somehow automatically the outline of the top text, without intersections, and without doing it all manually for all the letters? And including the right outline for letters with holes, like the "e"?
Some letters are even kind of twisted internally, looks pretty bad:
So I guess an algorithm to detect if it is a real hole, or if it is a hole from such twists and to ignore it, could be difficult.
Select the complete character in question. Then select from the drop down menu:
Path intersection followed by path union. This fixes the character font outline problem where the outline crosses into the body of the character.

Text deleted by backspace is not being updated on my arduino tft display (adafruit gfx library)

For university, we need to make a game in Unity that is controlled with an Arduino. My idea was a hacking game where the Arduino acts as the 'hacking device' when hacking something in the game. The arduino would have a screen and on that screen would be a basic command-line interface that lets me input simple commands to 'hack' but I've been having trouble regarding text and clearing it.
I've been able to use unity to send typed characters to the display as-well as a backspace function (pressing backspace would remove last character in the string)
I first had issue with clearing all the text when writing (calling tft.print doesn't clear any previous text). I was using fillScreen which was slow. I found out setTextColor had a second argument that let me just set all certain colored text to a different color. Setting it to black would essentially clear it.
This made it update pretty much instantly when writing to the screen but I now had a new issue, backspace would no longer would.
My understand is that when removing the character, it's color won't be updated when calling setTextColor leaving it on the screen until a restart/fillScreen is called.
I'm not really sure how to solve this and all google searches turn up little to no help.
Here's my code for updating the text:
void updateString(char c){
tft.setTextColor(WHITE,BLACK);
if(c!='<'){
//Add new character to end of string
str.concat(String(c));
}
else if(c=='<' && str.length()>2){
//Remove last character in string
str.remove(str.length()-1);
}
//Set cursor back to 0,0
tft.setCursor(0,0);
//Display text
tft.print(str);
}
I'd appreciate any help.
Maybe, use a similar function to tft.clear() each time you refresh the screen or you can draw a filled square of the background on the text so it looks like it has been erased then you rewrite the text.
Sounds like you are using proportionally-spaced fonts instead of the original classic fonts that ships with Adafruit_GFX. Historically, when using the default classic fonts one could set the background color option of the text to the same color as the background of the screen (usually black). This would overwrite the old screen contents with new data and work because the characters using the classic fonts are a uniform size. When using proportionally-spaced fonts, the background color option for the font is disabled by design.
This was presumably because of memory requirements and speed on slower AVR's. Regardless, when using proportionally-spaced fonts the background color feature won't work due to non-uniform sized characters overlapping the same regions as adjacent characters.
There are two work-arounds to this. Adafruit says in order to replace previously drawn text when using custom fonts you can either:
Use getTextBounds() to determine the smallest rectangle that encloses a
string and then erase that area using fillRect() prior to drawing
the new text. OR
Create an offscreen bitmap using GFXcanvas1 for a fixed size area,
draw the text in the GFXcanvas1 object and then copy to the screen
using drawBitmap().
Note that options 1 & 2 above are mutually exclusive. The second method requires more memory. The first method isn't perfect and produces some small amount of flicker, but is in general acceptable if coded carefully.
I hope that I have understood what the nature of your problem is and answered it in a satisfactory manner. If nothing else, at least now you know why custom font's will not work with the so called background color feature that works with the original 'classic' Adafruit fonts.
Nikki Cooper

Cutting of parts of sf::Text, sf::Texture, etc?

I'm creating a simple chat client and server and I'm having some issues with how sf::Text is rendered. Say in the chat history for example. When users type their messages, the old messages will be pushed up. Eventually the message hits the top and I'll want to only display the part of the message that is still within the chat history. This is where I'm lost.
The element containing the text has two visible objects - sf::RectangleShape (the background for the text) and a sf::Text. I can easily adjust the size of the shape once it hits the top so it's not visible outside the chat history, but I can't adjust the size of the text without distorting the contents. Changing the character size obviously isn't ideal either.
What I want is to cut the text in half horizontally if half of the text is outside the chat history. My first thought was to mask it somehow but it seems like SFML has no support for that.
The reason I'm mentioning sf::Texture in the title as well is because I've had the same issue with that in the past. I just couldn't find a way to cut off parts of it, I only managed to distort it.
Is this possible at all in SFML?
One way of doing this is to draw the chat history to a sf::RenderTexture, which is the tool used for off-screen rendering.
Once you've drawn the chat history to your render texture, you can draw the texture on to the screen.
So if you place text that is half on the render texture and half outside, only the part that actually fits on there will be drawn.
Be aware though that render_texture are fairly expensive to construct, so you you want to re-use them and not create new ones all the time.

Cocos2d CCLabelBMFont how to add a background to string

I am wondering how can I add a border & background to labels generated via CCLabelBMFont class in cocos2d.
I don't want to use sprites because my labels are generated on the fly and will keep changing and the size of labels are also different.
Also, I want user to touch and move these labels across the screen. When user picks a label it wiggles a bit like in free air. In that case, I wish to keep low complexity and preserve memory and cpu calculations.
Anyone knows best ways to achieve this?
IOS app LetterPress has similar effects.
Create your own class, that will incapsulate creation of complex node.
It will have several layers, for example, the first layer can be simple CCLayerColor of given rect with zOrder -2, the next layer will be your CCLabelBMFont with zOrder -1 and then you can overload draw method to draw border over your control. All that you draw in this method will be drawn with zOrder 0.
Then you can encapsulate any effects in this class. For example, you can rotate it a bit with method pick, etc. Whatever you want.

How to use colors in Motif

I'm new to GUI programming in C and Linux, and I'm having a hard time with it. It seems like a fairly simple/straightforward thing, but I can't find any answers googling. I want to add a background color to a widget. XmNbackground seems to be what I want to use, but I don't understand what I set it to, like a simple color blue, how do I get "blue" to set XmNbackground color to that?
See here for an answer in the function Pixel convert_color_name_to_pixel, and also here.
Hope this helps.
You can use a resource file which includes "*background: blue" or use XSetArgVal to do the same thing in the code.
If you have the widget in a function, you can set the widget background color resource like this, assuming the widget name is w:
XtVaSetValues(w, XmNbackground, XmRstring, "#c4c4c4", 8, NULL);
Where #c4c4c3 is the hex color code for red (first two characters), green (middle two characters) and blue (last two characters).
You can also use any of the textual color names from file rgb.txt - where that file is located seems to be something of a moving target, but on the computer I'm using at the moment it's in /usr/share/X11