Any good way to find the position of a SpriteFont character?

Get help using Construct 2

Post » Mon Nov 24, 2014 10:43 pm

Basically, I'm trying to make a system which replaces tags in text with an image. For example, right here I can Include :D a smiley ;) right inline with the text. I'd like to be able to do something similar to that at runtime in C2, but I've come across a problem.

It's easy enough to identify the tags, read them, replace them with whitespace, etc. But, when I want to insert the actual sprite, I can't actually find the coordinates of the character it should be placed on top of! Basically, after doing the tag processing, I end up with a space character and the index of that character in the text. I simply can't find a good way to translate that text index into an actual coordinate. I suppose it'd be possible to loop through all the characters and add up their individual character widths, but it gets *really* dodgy when line breaks get into the mix. I'd like to potentially support this for regular text, too, and that's even more impossible since I can't even access character widths.

One workaround would of course be just to substitute the icon in for an unused character in the spritefont file itself, but there are a few limitations there. It's a bit of a pain to keep track of (æ = ;) etc) but the biggest issue is that I lose the ability to animate the icon.

Any thoughts?
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,679

Post » Tue Nov 25, 2014 12:31 am

Interesting problem. It seems like it would be very difficult to handle reliably with events, what with scaling, justification, kerning, line breaks, etc. Though, the spritefont object itself has to deal with all this...maybe a new expression, CharX/Y (char, index)?
Don't lose your work. Backup your game with Dropbox.
B
44
S
10
G
10
Posts: 1,106
Reputation: 9,202

Post » Tue Nov 25, 2014 12:34 am

Here's a less than optimized way to do it. Perhaps you can build upon it to satisfy your need.

The same method could probably be used with normal text, using ExecJS with a small script that calls the measureText method, although it would prove twice the challenge.
You do not have the required permissions to view the files attached to this post.
B
69
S
28
G
24
Posts: 977
Reputation: 18,739

Post » Tue Nov 25, 2014 1:48 am

Interesting! @Magistross great job! That's basically what I had in mind to do but it's just so... awkward to have to go to all that effort.
I think for my currently planned application I'll just go with the 'replace a Unicode character' approach.
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,679


Return to How do I....?

Who is online

Users browsing this forum: brunopalermo, plinkie, rayolf and 9 guests