Image
WP_utils = {}
WP_utils.VERSION = { 1, 3, 0 }

------------------------------------
-- SCRIPT
------------------------------------

-- COMPARES VERSION WITH EXPECTED VERSION. VERSION LIKE { 0, 1, 2 }.
-- RETURNS:
-- -2 = WRONG FORMAT, -1 = LOADED VERSION IS LOWER, 0 = EXACT SAME VERSION, 1 = LOADED VERSION IS HIGHER THAN EXPECTED
function WP_utils:compareVersion(expectedVer, loadedVer)
    if (type(expectedVer) ~= 'table' or type(loadedVer) ~= 'table'
        or type(expectedVer[1]) ~= 'number' or type(expectedVer[2]) ~= 'number' or type(expectedVer[3]) ~= 'number'
        or type(loadedVer[1]) ~= 'number' or type(loadedVer[2]) ~= 'number' or type(loadedVer[3]) ~= 'number') then
        return -2
    elseif (loadedVer[1] == expectedVer[1] and loadedVer[2] == expectedVer[2]) then
        return 0
    elseif (loadedVer[1] > expectedVer[1] or loadedVer[2] > expectedVer[2]) then
        return 1
    else
        return -1
    end
end

-- TOSTRING FOR VERSION ARR (MUST HAVE FORMAT LIKE { 0, 1, 2 })
function WP_utils:ver2str(v)
    if (type(v) ~= 'table' or type(v[1]) ~= 'number' or type(v[2]) ~= 'number' or type(v[3]) ~= 'number') then
        return '?'
    else
        return tostring(v[1])..'.'..tostring(v[2])..'.'..tostring(v[3])
    end
end


------------------------------------
-- ARRAY
------------------------------------

function WP_utils:arrLen(arr)
    local c = 0
    for _ in pairs(arr) do c = c + 1 end
    return c
end

------------------------------------
-- TYPES
------------------------------------

function WP_utils:toInt(x)
    local num = tonumber(x)
    return num < 0 and math.ceil(num) or math.floor(num)
end

------------------------------------
-- MATH
------------------------------------

function WP_utils:round(float)
    local int, part = math.modf(float)
    if float == math.abs(float) and part >= .5 then return int + 1    -- positive float
    elseif part <= -.5 then return int - 1                            -- negative float
    end
    return int
end

function WP_utils:deg2rad(deg) return deg * (math.pi / 180) end

function WP_utils:rad2deg(rad) return rad * (180 / math.pi) end

-- RETURNS ANGLE OF VECTOR
function WP_utils:getAngleFromVec2Rad(vec2)
    local angle = 0
    if (vec2 and vec2.x and vec2.y) then
        angle = math.atan(vec2.x, vec2.y)
        if (angle < 0) then angle = angle + 2 * math.pi end
    end
    return angle
end


------------------------------------
-- STRINGS
------------------------------------

function WP_utils:trim(str) return string.gsub(str, '^%s*(.-)%s*$', '%1') end

function WP_utils:split(str, sep)
    if sep == nil then sep = '%s' end
    local t = {}
    for part in string.gmatch(str, '([^'..sep..']+)') do
        table.insert(t, part)
    end
    return t
end


------------------------------------
-- FILES AND FOLDERS
------------------------------------

-- RETURNS PATH TO FILE (WITH FORWARD SLASHES), BUT WITHOUT FILENAME (AND WITHOUT TRAILING SLASH)
function WP_utils:getPath(str)
    if (type(str) == 'string') then
        str = WP_utils:trim(str)
        if (str ~= '') then
            str = str:gsub('\\', '/'):match('(.*[/\\])')
            if (type(str) == 'string' and str ~= '') then
                if (str:sub(-1, -1) == '/') then
                    str = str:sub(1, -2)
                end
            else
                str = ''
            end
        else
            str = ''
        end
    else
        str = ''
    end

    return str
end

-- RETURNS FILENAME ONLY, BUT INCLUDING FILE EXTENSION
function WP_utils:getFilenameFromPath(str) return str:match('.*[/\\](.*)') end

function WP_utils:fileExists(file)
    local f = io.open(file, 'rb')
    if f then f:close() end
    return f ~= nil
end

function WP_utils:readFile(file)
    local f = assert(io.open(file, 'rb'))
    local content = f:read('*all')
    f:close()
    return content
end

------------------------------------
-- MOHO LAYERS
------------------------------------

-- RETURNS LAYER BY NAME
function WP_utils:getLayerByName(moho, name)
    local layer
    local i = -1
    repeat
        i = i + 1
        layer = moho.document:LayerByAbsoluteID(i)
        if layer then
            if (layer:Name() == name) then
                return layer
            end
        end
    until not layer
    return nil
end

-- CHECKS IF LAYER WITH THIS NAME EXISTS
function WP_utils:layerExists(moho, name)
    return WP_utils:getLayerByName(moho, name) ~= nil
end

-- FIND NEXT FREE LAYER NAME (RETURNS FALSE ON ISSUE)
function WP_utils:findNextFreeLayerName(moho, name)
    if (type(name) == 'string' and name:len() > 0) then
        local curNumber
        local baseName
        local newLayerName

        -- GET NEXT NUMBER
        local numberStr = name:match('%d+$')
        if (numberStr) then
            -- NAME ALREADY HAS A NUMBER
            curNumber = tonumber(numberStr)
            baseName = string.sub(name, 1, -numberStr:len() - 1) -- !! SUB() IS ONE-BASED!! (OMG LUA WHAT ARE YOU DOING TO ME)
        else
            -- NAME NO NUMBER YET, SO IS CONSIDERED ZERO
            curNumber = 0
            baseName = name
        end

        repeat
            curNumber = curNumber + 1
            newLayerName = baseName..tostring(curNumber)
        until WP_utils:layerExists(moho, newLayerName) == false

        return newLayerName
    else
        return false
    end
end


-- CONVERTS ABSOLUTE PIXEL SIZE TO MOHO SIZE FOR LAYER, RETURNS VECTOR2
function WP_utils:getMohoLayerSizeVec2(moho, xPx, yPx)
    local mohoSizeV2 = LM.Vector2:new_local()
    if (moho.document:Width() > 0 or moho.document:Height() > 0) then
        local sceneSize = { x = moho.document:Width(), y = moho.document:Height() }
        local aspect = sceneSize.x / sceneSize.y

        mohoSizeV2:Set(
            (xPx * aspect) / (sceneSize.x * 0.5),
            yPx / (sceneSize.y * 0.5))
    end
    return mohoSizeV2
end

-- CONVERTS ABSOLUTE PIXEL LOCATIONS TO MOHO RELATIVE TRANSLATION FOR LAYER, RETURNS VECTOR2
function WP_utils:getMohoRelLayerTransVec2(moho, xPx, yPx)
    local trans = LM.Vector2:new_local()
    local sceneSize = { x = moho.document:Width(), y = moho.document:Height() }
    local aspect = sceneSize.x / sceneSize.y
    trans:Set(
        ((xPx - sceneSize.x * 0.5) / (sceneSize.x * 0.5))  * aspect,
        (yPx - sceneSize.y * 0.5) / (sceneSize.y * 0.5))
    return trans
end

-- CONVERTS ABSOLUTE PIXEL LOCATIONS TO MOHO RELATIVE TRANSLATION FOR LAYER, RETURNS VECTOR3
function WP_utils:getMohoRelLayerTransVec3(moho, xPx, yPx)
    local trans3 = LM.Vector3:new_local()
    local trans2 = WP_utils:getMohoRelLayerTransVec2(moho, xPx, yPx)
    trans3:Set(trans2.x, trans2.y, 0)
    return trans3
end

------------------------------------
-- UI
------------------------------------

function WP_utils:alert(msg, allowCancel)
    local secBtnLbl = nil
    if (allowCancel ~= nil) then secBtnLbl = MOHO.Localize('/Scripts/Cancel=Cancel') end
    return LM.GUI.Alert(LM.GUI.ALERT_INFO, msg, nil, nil, MOHO.Localize('/Scripts/OK=OK'), secBtnLbl, nil)
end

WP Utils
Unlisted

Script type: Utility

Uploaded: Oct 16 2023, 07:11

Last modified: Dec 10 2023, 07:28

Script Version: 0

Wigglepixel Utilities Requirement
Functions etc. required to use within other Wigglepixel scripts.
This script, and all other scripts on this site are distributed as free software under the GNU General Public License 3.0 or later.
Downloads count: 861