Guest User

Texter

a guest
Jan 24th, 2013
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- version: Unspeakable Crawling Rachis
  2.  
  3. Texter = {}
  4. Texter._LastInputStr  = ""
  5. Texter._LastInputArgs = "1, 3"
  6. Texter._LastInputFont = "7px"
  7. Texter._Fonts = {}
  8.  
  9. function Texter.Init()
  10.     -- Please don't let Texter too far away from font file :)
  11.     Texter.LoadFont("TexterFonts")
  12.     tpt.register_keypress(Texter._HotkeyHandler)
  13. end
  14.  
  15. -- Internal methods
  16. function Texter._IsFolder(path)
  17.     local _isFolder = false
  18.     if(path ~= nil) then
  19.         -- API bug, not fixed right now (ver 85.0)
  20.         -- if( fs.isDirectory(path)  ) then
  21.                 -- _isFolder = true
  22.         -- end
  23.         if( fs.isDirectory(path) == fs.isDirectory(".") ) then
  24.             _isFolder = true
  25.         end
  26.     end
  27.     return _isFolder
  28. end
  29. function Texter._FindFileInCurrentDir(fileName, rootPath, optionalSuffix, optionalPrefix)
  30.     local filePath = nil
  31.     if( fs.exists(rootPath.."\\"..fileName)) then
  32.         filePath = rootPath.."\\"..fileName
  33.     else if( fs.exists(rootPath.."\\"..fileName..optionalSuffix)) then
  34.             filePath = rootPath.."\\"..fileName..optionalSuffix
  35.         else if( fs.exists(rootPath.."\\"..optionalPrefix..fileName)) then
  36.                 filePath = rootPath.."\\"..optionalPrefix..fileName
  37.             else if( fs.exists(rootPath.."\\"..optionalPrefix..fileName..optionalSuffix)) then
  38.                     filePath = rootPath.."\\"..optionalPrefix..fileName..optionalSuffix
  39.                 end
  40.             end
  41.         end
  42.     end
  43.     return filePath
  44. end
  45. function Texter._FindFile(fileName, rootPath, optionalSuffix, optionalPrefix, depth)
  46.     if( rootPath == nil  ) then
  47.         rootPath = "."
  48.     end
  49.     if( optionalSuffix == nil  ) then
  50.         optionalSuffix = ".lua"
  51.     end
  52.     if( optionalPrefix == nil  ) then
  53.         optionalPrefix = ""
  54.     end
  55.     if( depth == nil  ) then
  56.         depth = 3 -- Find 3 level of subfolders in default
  57.     end
  58.     local subs = nil
  59.     local filePath = Texter._FindFileInCurrentDir(fileName, rootPath, optionalSuffix, optionalPrefix)
  60.    
  61.     -- Check if file was here. If not found, continue in subfolders
  62.     if( filePath == nil  ) then
  63.         subs = fs.list(rootPath)
  64.         for i=1, #subs do
  65.             if( Texter._IsFolder(rootPath.."\\"..subs[i]) and depth > 0  ) then
  66.                 filePath = Texter._FindFile(fileName, rootPath.."\\"..subs[i], optionalSuffix, optionalPrefix, depth - 1)
  67.             end
  68.             if( filePath ~= nil  ) then do break end end
  69.         end
  70.     end
  71.     return filePath
  72. end
  73. function Texter._GetFontHeight(fontName)
  74.     local height = 0
  75.     if( Texter._Fontmatrix[fontName] ~=nil and Texter._Fontmatrix[fontName].height ~= nil  ) then
  76.         height = Texter._Fontmatrix[fontName].height
  77.     else
  78.         height = 7 -- 7 is a lucky number
  79.     end
  80.     return height
  81. end
  82. function Texter._Input(eventHandler, fonts, paramsText, strText, fontText)
  83.     local controlHeight = 17
  84.     if( paramsText == nil  ) then paramsText = "" end
  85.     if( strText    == nil  ) then strText    = "" end
  86.     if( fontText   == nil  ) then fontText   = "" end
  87.     if( fonts      == nil  ) then fonts      = {} end
  88.    
  89.     local isContains = function(inTable, value)
  90.         local isContain = false
  91.         for i=1, #inTable do
  92.             if( value == inTable[i]  ) then
  93.                 isContain = true
  94.                 break
  95.             end
  96.         end
  97.         return isContain
  98.     end
  99.    
  100.     local TexterWin = Window:new(-1, -1, 500, 100)
  101.     local paramsBox = Textbox:new(10, 32, 252, controlHeight, paramsText, "ptype, hspc, vspc")
  102.     local paramsLabel = Label:new(11, 9, 232, controlHeight, "Enter the element name you want to use and\nthe spacing between letters (horizontal/vertical).")
  103.     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.")
  104.     local fontBtn = Button:new(282, 32, 208, controlHeight, "No font available")
  105.     local fontLabel = Label:new(283, 9, 150, controlHeight, "Select the font you want to use.")
  106.     local cancelBtn = Button:new(0, 83, 251, controlHeight, "Cancel")
  107.     cancelBtn:action(function(sender)interface.closeWindow(TexterWin)end)
  108.     local okayBtn = Button:new(250, 83, 250, controlHeight, "Okay")
  109.     okayBtn:action(
  110.         function(sender)
  111.             if( eventHandler ~= nil and type(eventHandler) == "function"  ) then
  112.                 -- Use event because I can't make things like tpt.input
  113.                 eventHandler(paramsBox:text(), stringBox:text(), fontBtn:text())
  114.             end
  115.             interface.closeWindow(TexterWin)
  116.         end
  117.     )
  118.    
  119.     if(#fonts > 0) then
  120.         if( isContains(fonts, fontText)  ) then
  121.             fontBtn:text(fontText)
  122.         else
  123.             fontBtn:text(fonts[1])
  124.         end
  125.         fontBtn:action(
  126.             function(sender)
  127.                 local winX, winY = TexterWin:position()
  128.                 local btnX, btnY = fontBtn:position()
  129.                 local btnW, btnH = fontBtn:size()
  130.                 local listWin = Window:new(winX+btnX, winY+btnY-8*(#fonts-1), btnW, (btnH-1)*#fonts)
  131.                 for i=1, #fonts do
  132.                     local optionBtn = Button:new(0, (btnH-1)*(i-1), btnW, btnH, fonts[i])
  133.                     optionBtn:action(
  134.                         function(sender)
  135.                             interface.closeWindow(listWin)
  136.                             fontBtn:text(optionBtn:text())
  137.                         end
  138.                     )
  139.                     listWin:addComponent(optionBtn)
  140.                 end
  141.                 interface.showWindow(listWin)
  142.             end
  143.         )
  144.     end
  145.    
  146.     TexterWin:addComponent(cancelBtn)
  147.     TexterWin:addComponent(okayBtn)
  148.     TexterWin:addComponent(stringBox)
  149.     TexterWin:addComponent(paramsBox)
  150.     TexterWin:addComponent(paramsLabel)
  151.     TexterWin:addComponent(fontBtn)
  152.     TexterWin:addComponent(fontLabel)
  153.    
  154.     interface.showWindow(TexterWin)
  155. end
  156.  
  157. -- Load the working font
  158. function Texter.LoadFont(fontName, rootPath, optionalSuffix, optionalPrefix, depth)
  159.     local realPath = Texter._FindFile(fontName, rootPath, optionalSuffix, optionalPrefix, depth)
  160.     if( realPath ~= nil  ) then
  161.         dofile(realPath)
  162.         if(Texter._Fontmatrix ~= nil) then
  163.             for fontName in pairs(Texter._Fontmatrix) do
  164.                 table.insert(Texter._Fonts, fontName)
  165.             end
  166.             tpt.log("Texter: Font file \""..realPath.."\" loaded, "..#Texter._Fonts.." font(s) found.")
  167.         else
  168.             tpt.log("Texter: File \""..realPath.."\" loaded, invalid font file.")
  169.         end
  170.     else
  171.         tpt.log("Texter: Font file not found, please check if the file path is correct")
  172.     end
  173.    
  174. end
  175. -- Draw a single character
  176. function Texter.Tchar(char, x, y, ptype, fontName)
  177.     local mtx     = {}
  178.     local letter  = {}
  179.     local voffset = 0
  180.     local hoffset = 0
  181.     -- if given font not available, use the first available one
  182.     if( fontName == nil or Texter._Fontmatrix[fontName] == nil  ) then
  183.         for font in pairs(Texter._Fontmatrix) do
  184.             fontName = font
  185.             break
  186.         end
  187.     end
  188.     -- if still not available, break
  189.     if( fontName == nil  ) then return 0 end
  190.    
  191.     -- get character data
  192.     letter = Texter._Fontmatrix[fontName][char]
  193.     if( letter == nil  ) then
  194.         letter = Texter._Fontmatrix[fontName]["nil"]
  195.     end
  196.     mtx = letter['mtx']
  197.     if( letter['voffset'] ~= nil  ) then
  198.         voffset = letter['voffset']
  199.     end
  200.     if( letter['hoffset'] ~= nil  ) then
  201.         voffset = letter['hoffset']
  202.     end
  203.  
  204.     local height = #mtx
  205.     local width = 3
  206.     for i=1,height do
  207.         if( mtx[i] ~= nil  ) then
  208.             width = #mtx[i]
  209.             for j=1,width do
  210.                 if( mtx[i][j]==1  ) then
  211.                     if( pcall(tpt.create, x+j-1+hoffset, y+i-1+voffset, ptype) == false  ) then
  212.                         width = 0
  213.                     end
  214.                 end
  215.             end
  216.         end
  217.     end
  218.     return width
  219. end
  220. -- Draw a string
  221. function Texter.Tstr(str, x, y, ptype, hspacing, vspacing, fontName)
  222.     local ox    = 0
  223.     local oy    = 0
  224.     local oy    = 0
  225.     local fontH = Texter._GetFontHeight(fontName)
  226.     if( hspacing == nil  ) then
  227.         hspacing = 1
  228.     end
  229.     if( vspacing == nil  ) then
  230.         vspacing = 3
  231.     end
  232.     for i=1,string.len(str) do
  233.         if( string.sub(str, i, i) == "\n"  ) then
  234.             oy = vspacing + oy + fontH
  235.             ox = 0
  236.         else
  237.             ox = hspacing + ox + Texter.Tchar(string.sub(str, i, i), x+ox, y+oy, ptype, fontName)
  238.         end
  239.     end
  240.     return string.len(str)
  241. end
  242.  
  243. -- Shortcut for better user experience
  244. function T(str, ptype, hspc, vspc, fontName)
  245.     if( ptype == nil  ) then
  246.         ptype = elements[tpt.selectedl] -- elements.property(tpt.selectedl, "Name")
  247.     end
  248.     if( str == nil  ) then str = "" end
  249.     if( ptype == nil  ) then
  250.         ptype = "DUST"
  251.     end
  252.     Texter.Tstr(str, tpt.mousex, tpt.mousey, ptype, hspc, vspc, fontName)
  253.     return string.len(str)
  254. end
  255. -- Hotkey handler
  256. function Texter._HotkeyHandler(key, keyNum, modifier, event)
  257.     if( event==1 and keyNum==116 and modifier==64  ) then -- Ctrl + t
  258.         -- Additional settings
  259.         local ptype = elements[tpt.selectedl]
  260.         if( ptype == nil  ) then
  261.             ptype = 1 --"DUST"
  262.         end
  263.         -- Prompt
  264.         Texter._Input(
  265.             Texter._InputHandler,
  266.             Texter._Fonts,
  267.             elements.property(ptype, "Name")..", "..Texter._LastInputArgs,
  268.             Texter._LastInputStr,
  269.             Texter._LastInputFont
  270.         )
  271.     end
  272. end
  273. -- Input handler
  274. function Texter._InputHandler(params, str, fontName)
  275.     local args = {}
  276.     if( string.len(str) > 0  ) then
  277.         Texter._LastInputStr = str
  278.         str = string.gsub(str, "([^\\]?)\\n", "%1\n")  -- Small trick
  279.         str = string.gsub(str, "([^\\]?)\\\\n", "%1\\n")
  280.     end
  281.    
  282.     if( string.len(params) > 0  ) then Texter._LastInputArgs = string.gsub(params, "%s*%w*%s*,%s*(.*)", "%1") end
  283.     local i=1
  284.     for arg in string.gmatch(params, "%s*(%w*)%s*,?") do
  285.         args[i] = arg
  286.         i = i+1
  287.     end
  288.     Texter._LastInputFont = fontName
  289.    
  290.     if( pcall(tpt.element, args[1]) == false  ) then
  291.         args[1] = "DUST"
  292.     end
  293.     Texter.Tstr(str, tpt.mousex, tpt.mousey, args[1], args[2], args[3], fontName)
  294. end
  295.  
  296. Texter.Init()
Advertisement
Add Comment
Please, Sign In to add comment