enenra / space-engineers-utilities

A Blender 4.0+ addon to streamline working on assets for Space Engineers.
https://spaceengineers.wiki.gg/wiki/Modding/Tools/Space_Engineers_Utilities
GNU General Public License v3.0
47 stars 12 forks source link

Rework text wrapping code #348

Open enenra opened 1 year ago

enenra commented 1 year ago

This works reportedly correctly:

def wrap_text(text: str, width: int) -> List[str]:
    '''
    Wrap the given text to the given width.

    Args:
        width: The maximum pixel width of each row.
        text: The text to be split and returned.

    Returns:
        A list of the split up text and empty space if necessary.
    '''
    return_text = []
    row_text = ''

    system = bpy.context.preferences.system
    dpi = 72 if system.ui_scale >= 1 else system.dpi
    blf.size(0, 10, dpi)

    for word in text.split():
        word = f' {word}'
        line_len, _ = blf.dimensions(0, row_text + word)

        if line_len <= (width - 16):
            row_text += word
        else:
            return_text.append(row_text)
            row_text = word

    if row_text:
        return_text.append(row_text)

    return return_text

def draw(self, context):
    col = self.layout.column()
    width = context.region.width
    ui_scale = context.preferences.system.ui_scale
    MSG = f"Or move your already purchased {ds_props.product} pack into this folder:"
    for text in wrap_text(MSG, 0.8*(width/ui_scale)):
        col.label(text=text)

I'm thinking the main issue of my attempts has been forgetting to include uiscale.

enenra commented 1 year ago

Also this: https://b3d.interplanety.org/en/multiline-text-in-blender-interface-panels/