Changing lines and color on strings fails

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
ZodaInk
Prole
Posts: 18
Joined: Thu Jan 08, 2015 5:18 am

Changing lines and color on strings fails

Post by ZodaInk »

I've made my own string formatting code which is failing me for some unknown reason.
What I have achieved is a function that replaces certain syntax with strings (character names for example). That same function and syntax should also be able to change the color of the text, which it can, but is for some reason incompatible with \n.

Whenever i use \n and later change the color it behaves weirdly to say the least, it continues on the first line whenever the color is added.
I keep track on where the text is with a combination of three variables: ix, iy and font:getWidth()
Whenever it should go to the next line (which is detected by an empty string or "\n") I do:

Code: Select all

ix, iy = 0, iy+global.fontHeight
Whenever i go through a "normal" line i do:

Code: Select all

local width = font:getWidth(string)
ix = ix+width
tl;dr: I can either have \n or change colors in my text, I want both...
It feels like the code isn't obeying me... it should and probably is, but it doesn't feel like it.
Attachments
game.love
(4.05 KiB) Downloaded 60 times
User avatar
0x72
Citizen
Posts: 55
Joined: Thu Jun 18, 2015 9:02 am

Re: Changing lines and color on strings fails

Post by 0x72 »

v in the loop in the draw.string function is never equal to "\n"

So the scan.string fail somewhere: if you add

Code: Select all

elseif chr == '\n'
  table.insert(result, chr)
to the loop inside scan.string then it'll work

The part near `elseif skip then` in the same function assumes that you have "\\n" (two characters, the backslash escaped) in code instead of "\n" (one character) but I couldn't get it work with '\\n' and '\n' seams to be what you wanted anyway. When you write string "\n" it's escaped and is stored as a single character (not just printed as such).

Hope it helps :)

(and it's working without colors because it goes directly to love.graphics.printf in draw.string (type(text) == "string") instead to your loop print (type(text) == "table").
User avatar
ZodaInk
Prole
Posts: 18
Joined: Thu Jan 08, 2015 5:18 am

Re: Changing lines and color on strings fails

Post by ZodaInk »

0x72 wrote:v in the loop in the draw.string function is never equal to "\n"
This doesn't matter, because it should still detect an empty string (v == "\n" or v == ""), scan.string is the only function that needs to detect "\n".
So the scan.string fail somewhere: if you add

Code: Select all

elseif chr == '\n'
  table.insert(result, chr)
to the loop inside scan.string then it'll work
It does! I could've sworn I tested this before, oh well. Thank you for your help!

There's one thing though: Whenever I break a line before a color change it breaks two lines, any idea why?
Attachments
game.love
(4.04 KiB) Downloaded 55 times
User avatar
0x72
Citizen
Posts: 55
Joined: Thu Jun 18, 2015 9:02 am

Re: Changing lines and color on strings fails

Post by 0x72 »

tbh I'm not sure why you put an empty string to the result table in the fist place, I've changed in draw.string

Code: Select all

if v == "\n" or v == "" then
to

Code: Select all

if v == "\n" then
and it works as expected (i.e. it put only as many newlines as there is in the sting, because the later of the duplicated newlines is "")

I'm also not sure where it comes from :) I see you put it there in line <del>42</del> 38 of formatting.lua if the value is nil but but you also overwrite it in the next line unconditionally, so it must be the str variable somewhere. I wonder if it wouldn't be simpler to use lua patterns.

btw: you can take a look on this one: https://github.com/gvx/richtext
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], veethree and 88 guests