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

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
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

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

Post 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
Last edited by togFox on Fri Jun 28, 2024 10:24 pm, edited 1 time in total.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
User avatar
BrotSagtMist
Party member
Posts: 659
Joined: Fri Aug 06, 2021 10:30 pm

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

Post 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.
obey
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

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

Post 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. :)
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
User avatar
BrotSagtMist
Party member
Posts: 659
Joined: Fri Aug 06, 2021 10:30 pm

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

Post 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.
obey
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

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

Post 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)
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
User avatar
BrotSagtMist
Party member
Posts: 659
Joined: Fri Aug 06, 2021 10:30 pm

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

Post by BrotSagtMist »

The code for this isnt even half as big: https://ul.larskl.de/file/ce2c/test.mkv
obey
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

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

Post 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.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 7 guests