Page 1 of 1

How to fix this so it accounts for different font sizes? (solved)

Posted: Fri Jun 28, 2024 12:27 pm
by togFox
So Refreezed doesn't visit this forum any more but he has this very simple input field library here: https://github.com/ReFreezed/InputField

Works great - but only on the default font. For any font that is not default, the cursor is always misplaced. I suspect it is doing a character count and placing the cursor assuming a fixed width character, which obviously fails when changing fonts.

I've looked into the code and it's a level higher than my current knowledge. I've attached a working project hoping someone can work out how to make it work with any font size. I'm thinking the issue is on line 1133 where it literally counts the number of characters without any regard to font:

Code: Select all

-- length = field:getTextLength( )
-- Returns the number of characters in the UTF-8 text string.
function InputField.getTextLength(field)
	return utf8.len(field.text)
end
What's the correct way to account for the width of a character (or word) that is using a given font? Demo attached. Thanks.
temp.zip
(24 KiB) Downloaded 65 times

Re: How to fix this so it accounts for different font sizes?

Posted: Fri Jun 28, 2024 12:38 pm
by BrotSagtMist
Nope, the thing works as intented, but your init order is wrong, you start the text box with the default font and THEN switch it.
The lib is never notified about the font switching.

Re: How to fix this so it accounts for different font sizes?

Posted: Fri Jun 28, 2024 12:45 pm
by togFox
Thanks! It's that simple! I've re-ordered and it works.

For my learning, how to make that more dynamic? If I have multiple fonts drawing in different sequences, how to adjust inputfield.lua to account for that?

Edit:

In this library, there is a function that does as I questioned above:

Code: Select all

field:setFont( )
For this exercise, I reverted the order of declaring and then changed the code to this:

Code: Select all

thisfont = love.graphics.newFont("aliee13.ttf", 48)
love.graphics.setFont(thisfont)

field:setFont( thisfont )
I'll now dive into that code to learn how/why that works. :)

Re: How to fix this so it accounts for different font sizes?

Posted: Fri Jun 28, 2024 1:17 pm
by BrotSagtMist
2000 lines for such simple text thing.
Honestly, just do it yourself from scratch, the whole thing gives me a headache from its complexity.

Re: How to fix this so it accounts for different font sizes? (solved)

Posted: Fri Jun 28, 2024 10:27 pm
by togFox
I have to agree. It's not documented and I spasm when I see things like

LG = love.graphics

as it is so unnecessary. I'll see if I can do a lib with half as many rows. 8)

Re: How to fix this so it accounts for different font sizes? (solved)

Posted: Sat Jun 29, 2024 11:55 am
by pgimeno
Remember it's a library aimed at offering all features a standard text editor has, including text selection, cut/copy/paste, international support, multiple font support, double-click to select words, double-click-and-drag to select multiple words, triple-click to select everything, ctrl+arrows to move word by word, plus shift to select while moving, etc. etc. etc.

Oh, and multi-line editing. That alone requires a lot of code. It's a feature that very few Löve GUIs offer.

Of course you can implement something with far fewer features in far less code. Gspöt has a simple single-line edit control which isn't nearly so powerful, and the whole library, including the edit control, buttons, windows, styling, etc. is about 1,200 lines. If you don't want to offer the full set of features to the user, you don't need InputField.

Re: How to fix this so it accounts for different font sizes? (solved)

Posted: Sat Jun 29, 2024 12:58 pm
by BrotSagtMist
The code for this isnt even half as big: https://ul.larskl.de/file/ce2c/test.mkv

Re: How to fix this so it accounts for different font sizes? (solved)

Posted: Sat Jun 29, 2024 11:26 pm
by togFox
I had an issue with gspot that I couldn't resolve back in 2022. Probably my noobness or a library conflict - can't recall. I might revisit. Thanks.