Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function printf(t, x, y, w, l, a)
- t = string.gsub(t, "\r\n", "\n")
- local font = love.graphics.getFont()
- local ret = {t}
- local spacesplit = {}
- local lineskip = 0
- local align = a or "left"
- for i = string.len(ret[#ret]), 1, -1 do
- local v = string.sub(ret[#ret], i, i)
- if v == "\n" then
- lineskip = lineskip+1
- end
- end
- local TEST = 0
- while font:getWidth(ret[#ret]) > w or lineskip > 0 do
- TEST = TEST + 1
- local nn = false
- local skip = 0
- for i = 1, string.len(ret[#ret]) do
- local v = string.sub(ret[#ret], i, i)
- if v == " " then
- if font:getWidth(string.sub(ret[#ret], 1, i-1)) <= w then
- nn = i
- skip = 0
- end
- end
- if v == "\n" and font:getWidth(string.sub(ret[#ret], 1, i-1)) <= w then
- nn = i
- lineskip = lineskip-1
- break
- end
- end
- if not nn then
- for i = 1, string.len(ret[#ret]) do
- if font:getWidth(string.sub(ret[#ret], 1, i)) <= w then
- nn = i
- else
- break
- end
- end
- end
- table.insert(ret, string.sub(ret[#ret], nn+1, -1))
- ret[#ret-1] = string.sub(ret[#ret-1], 1, nn-skip)
- end
- if l then
- local rem = false
- while #ret > l do
- rem = true
- table.remove(ret)
- end
- local r = "..."
- if rem then
- local off = 1
- if font:getWidth(ret[#ret] .. r .. " ") > w then
- off = string.len(r)+1
- end
- ret[#ret] = string.sub(ret[#ret], 1, -off) .. "..."
- end
- end
- for i, v in ipairs(ret) do
- local xx = x
- if align == "right" then
- xx = x-font:getWidth(string.gsub(v, "\n", ""))
- elseif align == "center" then
- xx = x-font:getWidth(string.gsub(v, "\n", ""))/2
- end
- love.graphics.print(string.gsub(v, "\n", ""), xx, y+(i-1)*font:getHeight())
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement