Author Topic: Know the width of a listbox item when using magic tokens?  (Read 2645 times)

Bgoulette

  • Sr. Member
  • ****
  • Posts: 116
  • I wrote a book.
    • View Profile
    • BlakeGoulette.com
Know the width of a listbox item when using magic tokens?
« on: January 18, 2019, 02:19:38 PM »
Hey all,

I'm using magic tokens in the entries for a listbox. Mostly just so I can force them to uppercase. But it got me thinking...

I don't want to have to use the smallest common denominator when choosing char_size based on the longest title in my list. Initially, I added a block in my function that chopped off anything over x characters and appended an ellipsis character. It works, but because proportional fonts means, say, a string 50 Ms would be wider than a string of 50 Is (for example), that approach left me with a weird case where an entry with plenty of space left over might get the ellipsis treatment. Clear as mud? Sorry! I'll attach a picture to show what I'm talking about.

I know there's a "msg_width" property for fe.Text instances, but a) is it accessible from fe.Listbox instances, and b) is it accurate? I searched and found another post where the indication was that it wasn't quite up to snuff. If those properties are barking up the wrong tree, does anyone have any other suggestions on how to accurately gauge the width of a non-word-wrapped list item? Thanks!

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: Know the width of a listbox item when using magic tokens?
« Reply #1 on: May 12, 2019, 10:20:37 PM »
Revival of an old thread to help others. I was having the same exact issue, finding a text objects length in layout coordinates. Using a transition to dynamically set something similar to text.width = text.msg_width was cutting off characters, and would get smaller and smaller with each transition until only 1 letter of the message was visible.

The following resolved the cutting down size issue, but would consistently be noticbly larger than the text width. I tried multiplying it by a factor since I was using a monospace font, but results were inconsistent.

Code: [Select]
text.len()*text.char_size.tofloat();
Then I figured it out. Results were accurate when the margin is set to zero. If you require a margin, perhaps you could set a default value of -1, store the calculated margin value in a different variable, change the margin back to 0, and then add the stored margin value twice to your message width.? Otherwise, perhaps create another text object, hide it, and find it’s margin size?

Code: [Select]
text.margin = 0;
text.width = msg_width;

Hopefully this is helpful to others struggling like I was. https://github.com/mickelson/attract/issues/180

Edit: NOPE. It still doesn’t work and I need to go to bed. It only has correct width for the start layout. Each transition after shortens it. Maybe I try the copy method I suggested another time. I need sleep.

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: Know the width of a listbox item when using magic tokens?
« Reply #2 on: May 13, 2019, 03:29:02 AM »
Code: [Select]
// text
local surf = fe.addSurface(flw/2, fly/2);
  surf.x = flw/2-surf.width/2;
  surf.y = fly/2-surf.height/2;
local text = surf.add_text("[Title]", 0, 0, surf.width, surf.height/10);
  text.margin = 0;

// probeText
local probeText = fe.fe.add_text("", 0, 0, flw, text.height); // Must be in layout coordinates, height within a surface might not work
  probeText.visibile = false;
  probeText.margin = 0;

// transitions
function transitions(ttype, var, ttime) {
  if (ttype == "Transition.ToNewList“) {
    probeText = text.msg;
    text.width = probeText.msg_width;
  }
}
fe.add_transition_callback("transitions“);

I got this to work in my picknmix layout that I will try to release today. To save explanation, or posting a full layout, I tried to create a small example of what worked. Untested. Will test another time. Very tired. Work schedule has me messed up all the time now. See video to compare platform text (fixed) and filter text (normal msg_width and no margin applied).

https://youtu.be/EgS_PmTsul8