Bolt-Scripts / MHR-InGame-ModMenu-API

A user-friendly IMGUI inspired API for drawing in-game settings menus for REF mods in MHRise
13 stars 4 forks source link

[Feature Suggestion] Fit text in tooltips #7

Closed raff-run closed 2 years ago

raff-run commented 2 years ago

Hey! When I was implementing my mod options with your API, I noticed that the text will resize and become unreadable if too lengthy. This means that it needs carefully applied newlines, and also that the newlines need to be at different places if you're also using the usual REF UI since they have different default window sizes.

So I made a lua util function that automatically finds the best way to fit a text in a tooltip and caches the result, and wanted to share it with you if you in case want to incorporate it into the mod's API:

local minLineLength = 40
local lineBreakPattern = "(" .. ('.'):rep(minLineLength) .. ('.?'):rep(16) .. ") "  -- in regex: /(.{40,56}) /
local conversionCache = {} -- the UI will slow down without a cache

local function wrapText(text)
    text = text:gsub("\n", " "):gsub("  ", " ") -- this is here just so authors can feed their REF ui label strings straight into the api instead of having a version for each
    text = text:gsub(lineBreakPattern, "%1\n")
    return text
end

local function fitInBox(text)
    local newText = conversionCache[text]
    if newText ~= nil then return newText end

    newText = wrapText(text)
    conversionCache[text] = newText

    return newText
end

local function fitOptionsInBox(textTable)
    local newTextTable = conversionCache[textTable]
    if newTextTable ~= nil then return newTextTable end
    newTextTable = {}

    for _, text in ipairs(textTable) do
        table.insert(newTextTable, wrapText(text)) 
    end

    conversionCache[textTable] = newTextTable

    return newTextTable
end

Here are some example outputs: image image image image

Of course, if the string is long enough, it will overflow downwards, but I think that's better than seeing this and then having to place line breaks yourself: image

Bolt-Scripts commented 2 years ago

Neato. Was wondering if someone would bother to do something like this. I think you should make a PR to implement this. Though you shouldn't need the cache thing since they should basically be cached in the option data itself already. Should just need to look around the ModMenuApi.lua line 140: displayMessage = toolTip; and send the tooltip through the function and assign to the display message and bingo If you really don't wanna bother though, I can implement it.

raff-run commented 2 years ago

Sure! I'll give it a try.