Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- version: Unspeakable Crawling Rachis
- Texter = {}
- Texter._LastInputStr = ""
- Texter._LastInputArgs = "1, 3"
- Texter._LastInputFont = "7px"
- Texter._Fonts = {}
- function Texter.Init()
- -- Please don't let Texter too far away from font file :)
- Texter.LoadFont("TexterFonts")
- tpt.register_keypress(Texter._HotkeyHandler)
- end
- -- Internal methods
- function Texter._IsFolder(path)
- local _isFolder = false
- if(path ~= nil) then
- -- API bug, not fixed right now (ver 85.0)
- -- if( fs.isDirectory(path) ) then
- -- _isFolder = true
- -- end
- 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(fontName)
- local height = 0
- if( Texter._Fontmatrix[fontName] ~=nil and Texter._Fontmatrix[fontName].height ~= nil ) then
- height = Texter._Fontmatrix[fontName].height
- else
- height = 7 -- 7 is a lucky number
- end
- return height
- end
- function Texter._Input(eventHandler, fonts, paramsText, strText, fontText)
- local controlHeight = 17
- if( paramsText == nil ) then paramsText = "" end
- if( strText == nil ) then strText = "" end
- if( fontText == nil ) then fontText = "" end
- if( fonts == nil ) then fonts = {} end
- local isContains = function(inTable, value)
- local isContain = false
- for i=1, #inTable do
- if( value == inTable[i] ) then
- isContain = true
- break
- end
- end
- return isContain
- end
- local TexterWin = Window:new(-1, -1, 500, 100)
- local paramsBox = Textbox:new(10, 32, 252, controlHeight, paramsText, "ptype, hspc, vspc")
- local paramsLabel = Label:new(11, 9, 232, controlHeight, "Enter the element name you want to use and\nthe spacing between letters (horizontal/vertical).")
- local stringBox = Textbox:new(10, 57, 480, controlHeight, strText, "Input text to create under mouse. Use \\n for new line, \\\\n for \\n. Sorry, you can't get \\\\n.")
- local fontBtn = Button:new(282, 32, 208, controlHeight, "No font available")
- local fontLabel = Label:new(283, 9, 150, controlHeight, "Select the font you want to use.")
- local cancelBtn = Button:new(0, 83, 251, controlHeight, "Cancel")
- cancelBtn:action(function(sender)interface.closeWindow(TexterWin)end)
- local okayBtn = Button:new(250, 83, 250, controlHeight, "Okay")
- okayBtn:action(
- function(sender)
- if( eventHandler ~= nil and type(eventHandler) == "function" ) then
- -- Use event because I can't make things like tpt.input
- eventHandler(paramsBox:text(), stringBox:text(), fontBtn:text())
- end
- interface.closeWindow(TexterWin)
- end
- )
- if(#fonts > 0) then
- if( isContains(fonts, fontText) ) then
- fontBtn:text(fontText)
- else
- fontBtn:text(fonts[1])
- end
- fontBtn:action(
- function(sender)
- local winX, winY = TexterWin:position()
- local btnX, btnY = fontBtn:position()
- local btnW, btnH = fontBtn:size()
- local listWin = Window:new(winX+btnX, winY+btnY-8*(#fonts-1), btnW, (btnH-1)*#fonts)
- for i=1, #fonts do
- local optionBtn = Button:new(0, (btnH-1)*(i-1), btnW, btnH, fonts[i])
- optionBtn:action(
- function(sender)
- interface.closeWindow(listWin)
- fontBtn:text(optionBtn:text())
- end
- )
- listWin:addComponent(optionBtn)
- end
- interface.showWindow(listWin)
- end
- )
- end
- TexterWin:addComponent(cancelBtn)
- TexterWin:addComponent(okayBtn)
- TexterWin:addComponent(stringBox)
- TexterWin:addComponent(paramsBox)
- TexterWin:addComponent(paramsLabel)
- TexterWin:addComponent(fontBtn)
- TexterWin:addComponent(fontLabel)
- interface.showWindow(TexterWin)
- end
- -- Load the working font
- function Texter.LoadFont(fontName, rootPath, optionalSuffix, optionalPrefix, depth)
- local realPath = Texter._FindFile(fontName, rootPath, optionalSuffix, optionalPrefix, depth)
- if( realPath ~= nil ) then
- dofile(realPath)
- if(Texter._Fontmatrix ~= nil) then
- for fontName in pairs(Texter._Fontmatrix) do
- table.insert(Texter._Fonts, fontName)
- end
- tpt.log("Texter: Font file \""..realPath.."\" loaded, "..#Texter._Fonts.." font(s) found.")
- else
- tpt.log("Texter: File \""..realPath.."\" loaded, invalid font file.")
- end
- 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, fontName)
- local mtx = {}
- local letter = {}
- local voffset = 0
- local hoffset = 0
- -- if given font not available, use the first available one
- if( fontName == nil or Texter._Fontmatrix[fontName] == nil ) then
- for font in pairs(Texter._Fontmatrix) do
- fontName = font
- break
- end
- end
- -- if still not available, break
- if( fontName == nil ) then return 0 end
- -- get character data
- letter = Texter._Fontmatrix[fontName][char]
- if( letter == nil ) then
- letter = Texter._Fontmatrix[fontName]["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
- if( pcall(tpt.create, x+j-1+hoffset, y+i-1+voffset, ptype) == false ) then
- width = 0
- end
- end
- end
- end
- end
- return width
- end
- -- Draw a string
- function Texter.Tstr(str, x, y, ptype, hspacing, vspacing, fontName)
- local ox = 0
- local oy = 0
- local oy = 0
- local fontH = Texter._GetFontHeight(fontName)
- 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, fontName)
- end
- end
- return string.len(str)
- end
- -- Shortcut for better user experience
- function T(str, ptype, hspc, vspc, fontName)
- 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, fontName)
- return string.len(str)
- end
- -- Hotkey handler
- function Texter._HotkeyHandler(key, keyNum, modifier, event)
- if( event==1 and keyNum==116 and modifier==64 ) then -- Ctrl + t
- -- Additional settings
- local ptype = elements[tpt.selectedl]
- if( ptype == nil ) then
- ptype = 1 --"DUST"
- end
- -- Prompt
- Texter._Input(
- Texter._InputHandler,
- Texter._Fonts,
- elements.property(ptype, "Name")..", "..Texter._LastInputArgs,
- Texter._LastInputStr,
- Texter._LastInputFont
- )
- end
- end
- -- Input handler
- function Texter._InputHandler(params, str, fontName)
- local args = {}
- 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")
- end
- if( string.len(params) > 0 ) then Texter._LastInputArgs = string.gsub(params, "%s*%w*%s*,%s*(.*)", "%1") end
- local i=1
- for arg in string.gmatch(params, "%s*(%w*)%s*,?") do
- args[i] = arg
- i = i+1
- end
- Texter._LastInputFont = fontName
- 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], fontName)
- end
- Texter.Init()
Advertisement
Add Comment
Please, Sign In to add comment