Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- version: Unspeakable Crawling Sticky Tentacle
- Texter = {}
- Texter._lastInputStr = ""
- Texter._lastInputArgs = "1, 3"
- function Texter.Init()
- -- Please don't let Texter too far away from font file :)
- if Texter._findFile("7px") ~= nil then
- Texter.SetFont("7px")
- else
- Texter.SetFont("5px")
- end
- tpt.register_keypress(Texter._hotkeyHandler)
- end
- -- Internal methods
- function Texter._isFolder(path)
- local _isFolder = false
- if path ~= nil then
- -- if fs.isDirectory(path) then
- -- _isFolder = true
- -- end
- -- API bug, not fixed right now (ver 85.0)
- if ( fs.isDirectory(path) == fs.isDirectory(".") ) then
- _isFolder = true
- end
- end
- return _isFolder
- end
- function Texter._findFileInCurrentDir(fileName, rootPath, optionalSuffix, optionalPrefix)
- local filePath = nil
- if fs.exists(rootPath.."\\"..fileName) then
- filePath = rootPath.."\\"..fileName
- else if fs.exists(rootPath.."\\"..fileName..optionalSuffix) then
- filePath = rootPath.."\\"..fileName..optionalSuffix
- else if fs.exists(rootPath.."\\"..optionalPrefix..fileName) then
- filePath = rootPath.."\\"..optionalPrefix..fileName
- else if fs.exists(rootPath.."\\"..optionalPrefix..fileName..optionalSuffix) then
- filePath = rootPath.."\\"..optionalPrefix..fileName..optionalSuffix
- end
- end
- end
- end
- return filePath
- end
- function Texter._findFile(fileName, rootPath, optionalSuffix, optionalPrefix, depth)
- if rootPath == nil then
- rootPath = "."
- end
- if optionalSuffix == nil then
- optionalSuffix = ".lua"
- end
- if optionalPrefix == nil then
- optionalPrefix = ""
- end
- if depth == nil then
- depth = 3 -- Find 3 level of subfolders in default
- end
- local subs = nil
- local filePath = Texter._findFileInCurrentDir(fileName, rootPath, optionalSuffix, optionalPrefix)
- -- Check if file was here. If not found, continue in subfolders
- if filePath == nil then
- subs = fs.list(rootPath)
- for i=1, #subs do
- if Texter._isFolder(rootPath.."\\"..subs[i]) and depth > 0 then
- filePath = Texter._findFile(fileName, rootPath.."\\"..subs[i], optionalSuffix, optionalPrefix, depth - 1)
- end
- if filePath ~= nil then do break end end
- end
- end
- return filePath
- end
- function Texter._getFontHeight()
- local height = 0
- if fontmatrix.height ~= nil then
- height = fontmatrix.height
- else
- height = 7 -- 7 is a lucky number
- end
- return height
- end
- -- Set the working font
- function Texter.SetFont(fontName, rootPath, optionalSuffix, optionalPrefix, depth)
- local realPath = Texter._findFile(fontName, rootPath, optionalSuffix, optionalPrefix, depth)
- if realPath ~= nil then
- dofile(realPath)
- tpt.log("Texter: Font file \""..realPath.."\" loaded.")
- else
- tpt.log("Texter: Font file not found, please check if the file path is correct")
- end
- end
- -- Draw a single character
- function Texter.Tchar(char, x, y, ptype)
- local mtx = {}
- local letter = {}
- local voffset = 0
- local hoffset = 0
- -- get character data
- letter = fontmatrix[char]
- if letter == nil then
- letter = fontmatrix["nil"]
- end
- mtx = letter['mtx']
- if letter['voffset'] ~= nil then
- voffset = letter['voffset']
- end
- if letter['hoffset'] ~= nil then
- voffset = letter['hoffset']
- end
- local height = #mtx
- local width = 3
- for i=1,height do
- if mtx[i] ~= nil then
- width = #mtx[i]
- for j=1,width do
- if mtx[i][j]==1 then
- tpt.create(x+j-1+hoffset, y+i-1+voffset, ptype)
- end
- end
- end
- end
- return width
- end
- -- Draw a string
- function Texter.Tstr(str, x, y, ptype, hspacing, vspacing)
- local ox = 0
- local oy = 0
- local oy = 0
- local fontH = Texter._getFontHeight()
- if hspacing == nil then
- hspacing = 1
- end
- if vspacing == nil then
- vspacing = 3
- end
- for i=1,string.len(str) do
- if string.sub(str, i, i) == "\n" then
- oy = vspacing + oy + fontH
- ox = 0
- else
- ox = hspacing + ox + Texter.Tchar(string.sub(str, i, i), x+ox, y+oy, ptype)
- end
- end
- return string.len(str)
- end
- -- Shortcut for better user experience
- function T(str, ptype, hspc, vspc)
- if ptype == nil then
- ptype = elements[tpt.selectedl] -- elements.property(tpt.selectedl, "Name")
- end
- if str == nil then str = "" end
- if ptype == nil then
- ptype = "DUST"
- end
- Texter.Tstr(str, tpt.mousex, tpt.mousey, ptype, hspc, vspc)
- return string.len(str)
- end
- -- Hotkey handler
- function Texter._hotkeyHandler(key, keyNum, modifier, event)
- local str = ""
- local argsStr = ""
- local args = {}
- if event==1 and keyNum==116 and modifier==64 then -- Ctrl + t
- -- String to create
- str = tpt.input(
- "Input text to create under mouse",
- "[Format: text]\nUse \\n for new line, \\\\n for \\n.\nSorry, you can't get \\\\n.",
- Texter._lastInputStr
- )
- if string.len(str) > 0 then
- Texter._lastInputStr = str
- str = string.gsub(str, "([^\\]?)\\n", "%1\n") -- Small trick
- str = string.gsub(str, "([^\\]?)\\\\n", "%1\\n")
- -- Additional settings
- local ptype = elements[tpt.selectedl]
- if ptype == nil then
- ptype = 1 --"DUST"
- end
- argsStr = tpt.input(
- "Input parameters for creating text",
- "[Format: ptype, hspc, vspc]\nptype is element used, e.g. DUST.\nh/vspc is horizontal/vertical spacing.",
- elements.property(ptype, "Name")..", "..Texter._lastInputArgs
- )
- if string.len(argsStr) > 0 then Texter._lastInputArgs = string.gsub(argsStr, "%s*%w*%s*,%s*(.*)", "%1") end
- local i=1
- for arg in string.gmatch(argsStr, "%s*(%w*)%s*,?") do
- args[i] = arg
- i = i+1
- end
- end
- if pcall(tpt.element, args[1]) == false then
- args[1] = "DUST"
- end
- Texter.Tstr(str, tpt.mousex, tpt.mousey, args[1], args[2], args[3])
- end
- return string.len(str)
- end
- Texter.Init()
Advertisement
Add Comment
Please, Sign In to add comment