SHOW:
|
|
- or go back to the newest paste.
| 1 | -- version: Unspeakable Crawling Sticky Tentacle | |
| 2 | ||
| 3 | Texter = {}
| |
| 4 | Texter._lastInputStr = "" | |
| 5 | Texter._lastInputArgs = "1, 3" | |
| 6 | ||
| 7 | function Texter.Init() | |
| 8 | -- Please don't let Texter too far away from font file :) | |
| 9 | if Texter._findFile("7px") ~= nil then
| |
| 10 | Texter.SetFont("7px")
| |
| 11 | else | |
| 12 | Texter.SetFont("5px")
| |
| 13 | end | |
| 14 | tpt.register_keypress(Texter._hotkeyHandler) | |
| 15 | end | |
| 16 | ||
| 17 | -- Internal methods | |
| 18 | function Texter._isFolder(path) | |
| 19 | local _isFolder = false | |
| 20 | if path ~= nil then | |
| 21 | -- if fs.isDirectory(path) then | |
| 22 | -- _isFolder = true | |
| 23 | -- end | |
| 24 | -- API bug, not fixed right now (ver 85.0) | |
| 25 | - | if ( fs.isDirectory(path) == fs.isDirectory("C:/") ) then
|
| 25 | + | if ( fs.isDirectory(path) == fs.isDirectory(".") ) then
|
| 26 | _isFolder = true | |
| 27 | end | |
| 28 | end | |
| 29 | return _isFolder | |
| 30 | end | |
| 31 | function Texter._findFileInCurrentDir(fileName, rootPath, optionalSuffix, optionalPrefix) | |
| 32 | local filePath = nil | |
| 33 | if fs.exists(rootPath.."\\"..fileName) then | |
| 34 | filePath = rootPath.."\\"..fileName | |
| 35 | else if fs.exists(rootPath.."\\"..fileName..optionalSuffix) then | |
| 36 | filePath = rootPath.."\\"..fileName..optionalSuffix | |
| 37 | else if fs.exists(rootPath.."\\"..optionalPrefix..fileName) then | |
| 38 | filePath = rootPath.."\\"..optionalPrefix..fileName | |
| 39 | else if fs.exists(rootPath.."\\"..optionalPrefix..fileName..optionalSuffix) then | |
| 40 | filePath = rootPath.."\\"..optionalPrefix..fileName..optionalSuffix | |
| 41 | end | |
| 42 | end | |
| 43 | end | |
| 44 | end | |
| 45 | return filePath | |
| 46 | end | |
| 47 | function Texter._findFile(fileName, rootPath, optionalSuffix, optionalPrefix, depth) | |
| 48 | if rootPath == nil then | |
| 49 | rootPath = "." | |
| 50 | end | |
| 51 | if optionalSuffix == nil then | |
| 52 | optionalSuffix = ".lua" | |
| 53 | end | |
| 54 | if optionalPrefix == nil then | |
| 55 | optionalPrefix = "" | |
| 56 | end | |
| 57 | if depth == nil then | |
| 58 | depth = 3 -- Find 3 level of subfolders in default | |
| 59 | end | |
| 60 | local subs = nil | |
| 61 | local filePath = Texter._findFileInCurrentDir(fileName, rootPath, optionalSuffix, optionalPrefix) | |
| 62 | ||
| 63 | -- Check if file was here. If not found, continue in subfolders | |
| 64 | if filePath == nil then | |
| 65 | subs = fs.list(rootPath) | |
| 66 | for i=1, #subs do | |
| 67 | if Texter._isFolder(rootPath.."\\"..subs[i]) and depth > 0 then | |
| 68 | filePath = Texter._findFile(fileName, rootPath.."\\"..subs[i], optionalSuffix, optionalPrefix, depth - 1) | |
| 69 | end | |
| 70 | if filePath ~= nil then do break end end | |
| 71 | end | |
| 72 | end | |
| 73 | return filePath | |
| 74 | end | |
| 75 | function Texter._getFontHeight() | |
| 76 | local height = 0 | |
| 77 | if fontmatrix.height ~= nil then | |
| 78 | height = fontmatrix.height | |
| 79 | else | |
| 80 | height = 7 -- 7 is a lucky number | |
| 81 | end | |
| 82 | return height | |
| 83 | end | |
| 84 | ||
| 85 | ||
| 86 | -- Set the working font | |
| 87 | function Texter.SetFont(fontName, rootPath, optionalSuffix, optionalPrefix, depth) | |
| 88 | local realPath = Texter._findFile(fontName, rootPath, optionalSuffix, optionalPrefix, depth) | |
| 89 | if realPath ~= nil then | |
| 90 | dofile(realPath) | |
| 91 | tpt.log("Texter: Font file \""..realPath.."\" loaded.")
| |
| 92 | else | |
| 93 | tpt.log("Texter: Font file not found, please check if the file path is correct")
| |
| 94 | end | |
| 95 | ||
| 96 | end | |
| 97 | ||
| 98 | -- Draw a single character | |
| 99 | function Texter.Tchar(char, x, y, ptype) | |
| 100 | local mtx = {}
| |
| 101 | local letter = {}
| |
| 102 | local voffset = 0 | |
| 103 | local hoffset = 0 | |
| 104 | ||
| 105 | -- get character data | |
| 106 | letter = fontmatrix[char] | |
| 107 | if letter == nil then | |
| 108 | letter = fontmatrix["nil"] | |
| 109 | end | |
| 110 | mtx = letter['mtx'] | |
| 111 | if letter['voffset'] ~= nil then | |
| 112 | voffset = letter['voffset'] | |
| 113 | end | |
| 114 | if letter['hoffset'] ~= nil then | |
| 115 | voffset = letter['hoffset'] | |
| 116 | end | |
| 117 | ||
| 118 | local height = #mtx | |
| 119 | local width = 3 | |
| 120 | for i=1,height do | |
| 121 | if mtx[i] ~= nil then | |
| 122 | width = #mtx[i] | |
| 123 | for j=1,width do | |
| 124 | if mtx[i][j]==1 then | |
| 125 | tpt.create(x+j-1+hoffset, y+i-1+voffset, ptype) | |
| 126 | end | |
| 127 | end | |
| 128 | end | |
| 129 | end | |
| 130 | return width | |
| 131 | end | |
| 132 | ||
| 133 | -- Draw a string | |
| 134 | function Texter.Tstr(str, x, y, ptype, hspacing, vspacing) | |
| 135 | local ox = 0 | |
| 136 | local oy = 0 | |
| 137 | local oy = 0 | |
| 138 | local fontH = Texter._getFontHeight() | |
| 139 | if hspacing == nil then | |
| 140 | hspacing = 1 | |
| 141 | end | |
| 142 | if vspacing == nil then | |
| 143 | vspacing = 3 | |
| 144 | end | |
| 145 | for i=1,string.len(str) do | |
| 146 | if string.sub(str, i, i) == "\n" then | |
| 147 | oy = vspacing + oy + fontH | |
| 148 | ox = 0 | |
| 149 | else | |
| 150 | ox = hspacing + ox + Texter.Tchar(string.sub(str, i, i), x+ox, y+oy, ptype) | |
| 151 | end | |
| 152 | end | |
| 153 | return string.len(str) | |
| 154 | end | |
| 155 | ||
| 156 | ||
| 157 | -- Shortcut for better user experience | |
| 158 | function T(str, ptype, hspc, vspc) | |
| 159 | if ptype == nil then | |
| 160 | ptype = elements[tpt.selectedl] -- elements.property(tpt.selectedl, "Name") | |
| 161 | end | |
| 162 | if str == nil then str = "" end | |
| 163 | if ptype == nil then | |
| 164 | ptype = "DUST" | |
| 165 | end | |
| 166 | Texter.Tstr(str, tpt.mousex, tpt.mousey, ptype, hspc, vspc) | |
| 167 | return string.len(str) | |
| 168 | end | |
| 169 | ||
| 170 | -- Hotkey handler | |
| 171 | function Texter._hotkeyHandler(key, keyNum, modifier, event) | |
| 172 | local str = "" | |
| 173 | local argsStr = "" | |
| 174 | local args = {}
| |
| 175 | if event==1 and keyNum==116 and modifier==64 then -- Ctrl + t | |
| 176 | -- String to create | |
| 177 | str = tpt.input( | |
| 178 | "Input text to create under mouse", | |
| 179 | "[Format: text]\nUse \\n for new line, \\\\n for \\n.\nSorry, you can't get \\\\n.", | |
| 180 | Texter._lastInputStr | |
| 181 | ) | |
| 182 | if string.len(str) > 0 then | |
| 183 | Texter._lastInputStr = str | |
| 184 | str = string.gsub(str, "([^\\]?)\\n", "%1\n") -- Small trick | |
| 185 | str = string.gsub(str, "([^\\]?)\\\\n", "%1\\n") | |
| 186 | -- Additional settings | |
| 187 | local ptype = elements[tpt.selectedl] | |
| 188 | if ptype == nil then | |
| 189 | ptype = 1 --"DUST" | |
| 190 | end | |
| 191 | argsStr = tpt.input( | |
| 192 | "Input parameters for creating text", | |
| 193 | "[Format: ptype, hspc, vspc]\nptype is element used, e.g. DUST.\nh/vspc is horizontal/vertical spacing.", | |
| 194 | elements.property(ptype, "Name")..", "..Texter._lastInputArgs | |
| 195 | ) | |
| 196 | if string.len(argsStr) > 0 then Texter._lastInputArgs = string.gsub(argsStr, "%s*%w*%s*,%s*(.*)", "%1") end | |
| 197 | local i=1 | |
| 198 | for arg in string.gmatch(argsStr, "%s*(%w*)%s*,?") do | |
| 199 | args[i] = arg | |
| 200 | i = i+1 | |
| 201 | end | |
| 202 | end | |
| 203 | if pcall(tpt.element, args[1]) == false then | |
| 204 | args[1] = "DUST" | |
| 205 | end | |
| 206 | Texter.Tstr(str, tpt.mousex, tpt.mousey, args[1], args[2], args[3]) | |
| 207 | end | |
| 208 | return string.len(str) | |
| 209 | end | |
| 210 | ||
| 211 | ||
| 212 | Texter.Init() |