Advertisement
FeynmanTech

Lua Text Plotter

Sep 29th, 2013
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.43 KB | None | 0 0
  1. if not texter_font then dofile('texter_basic.txt') end
  2.  
  3. function setFont(name)
  4.     dofile(name .. '.txt')
  5.     texter_font = _G[name]
  6. end
  7.  
  8. local newmatrix = function(...)
  9.     local data = {...}
  10.     local get = function(x, y)
  11.         return data[y][x]
  12.     end
  13.     local set = function(n, x, y)
  14.         data[y][x] = n
  15.         return n
  16.     end
  17.     return {data = data, get = get, set = set} or 'ERROR: UNABLE TO CREATE 2-D MATRIX'
  18. end
  19.  
  20. function drawLetter(l, x, y)
  21.     for cx = 0, texter_font['width'] - 1 + texter_font[l]['kerning'] do
  22.         for cy = texter_font[l]['descender'], texter_font['height'] - 1 do
  23.             if texter_font[l]['pixels'][cy + 1 - texter_font[l]['descender']][cx + 1] == 1 then
  24.                 tpt.create(x + cx, y + cy - texter_font[l]['descender'], 'dmnd')
  25.             end
  26.         end
  27.     end
  28. end
  29.  
  30. function drawText(t, x, y)
  31.     local cl
  32.     local ox = x
  33.     for p = 1, #t do
  34.         cl = string.sub(t, p, p)
  35.         if cl == '\n' then
  36.             x = ox
  37.             y = y + texter_font['height'] + 4
  38.         elseif texter_font[cl] then
  39.             drawLetter(cl, x, y)
  40.             x = x + texter_font['width'] + 1 + texter_font[cl]['kerning']
  41.         else
  42.             drawLetter('NULL', x, y)
  43.             x = x + texter_font['width'] + 1 + texter_font['NULL']['kerning']
  44.         end
  45.     end
  46. end
  47.  
  48. function texter_key_test(a)
  49.     if a == 't' then
  50.         local cmx, cmy = tpt.mousex, tpt.mousey
  51.         local texterWindow = Window:new(-1, -1, 525, 75)
  52.  
  53.         local currentY = 10
  54.         local texterstr, texterstr2 = '', ''
  55.  
  56.         local testTextbox = Textbox:new(10, currentY, (select(1, texterWindow:size()))-20, 16, "", "[place text here]")
  57.         testTextbox:onTextChanged(
  58.             function(sender)
  59.                 texterstr = testTextbox:text()
  60.             end
  61.         )
  62.  
  63.         currentY = currentY + 20
  64.  
  65.         local testTextbox2 = Textbox:new(10, currentY, (select(1, texterWindow:size()))-20, 16, "", "[and here, if you need more space]")
  66.         testTextbox2:onTextChanged(
  67.             function(sender)
  68.                 texterstr2 = testTextbox2:text();
  69.             end
  70.         )
  71.  
  72.         local closeButton = Button:new(10, select(2, texterWindow:size())-26, 100, 16, "Draw text")
  73.  
  74.         closeButton:action(function() drawText(string.gsub(texterstr, '\\n', '\n') .. '\n' .. string.gsub(texterstr2, '\\n', '\n'), cmx, cmy); interface.closeWindow(texterWindow) end)
  75.  
  76.         texterWindow:onTryExit(function() interface.closeWindow(texterWindow) end) -- Allow the default exit events
  77.  
  78.         texterWindow:addComponent(testTextbox)
  79.         texterWindow:addComponent(testTextbox2)
  80.         texterWindow:addComponent(closeButton)
  81.  
  82.         interface.showWindow(texterWindow)
  83.     end
  84. end
  85.  
  86. tpt.register_keypress(texter_key_test)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement