Advertisement
FeynmanTech

TPTLua TextIOLib

Oct 16th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.57 KB | None | 0 0
  1. --[[
  2.  
  3.     Usage:
  4.  
  5.     Prompt for text: text.read()
  6.  
  7.         Opens interface allowing the user to enter text
  8.  
  9.         Text entry point can be controlled by arrow keys
  10.  
  11.         Text entry is done by replacement, not insertion
  12.  
  13.     Read text: text.get()
  14.  
  15.         Returns a string consisting of all the lines a user has entered
  16.  
  17.         Only returns the number of lines a user has entered, only one extra newline
  18.  
  19. ]]
  20.  
  21. -- [Usage: Stores all the global TBGUI variables]
  22.  
  23. text = text or {}
  24.  
  25. -- [Usage: Table of entered characters (Setup: 58-65, Used: 183-195, drawText - 207-235, text.get - 241-251)]
  26.  
  27. text.chars = {}
  28.  
  29. -- [Usage: Number of lines the user has entered (Used: 231)]
  30.  
  31. local maxy
  32.  
  33. -- Prompt text
  34.  
  35. function text.read()
  36.  
  37.     -- Default maxy to 1 (Used: 231)
  38.  
  39.     maxy = 1
  40.  
  41.     -- Pause the simulation while the user enters text
  42.  
  43.     tpt.set_pause(1)
  44.  
  45.     -- Hide the HUD - it just gets in the way
  46.  
  47.     tpt.hud(0)
  48.  
  49.     --[[
  50.     out: [Purpose: Stores the current letter, Used: getKey - 67-202, Main: 214]
  51.     posx, posy: [Purpose: Current text entry points, Used: 183-195, 217-220]
  52.     ]]
  53.  
  54.     local out, posx, posy = '', 1, 1
  55.  
  56.     -- Set up text.chars{} to recieve data
  57.  
  58.     for k = 1, 35 do
  59.         text.chars[k] = {}
  60.  
  61.         for k2 = 1, 100 do
  62.             text.chars[k][k2] = ' '
  63.         end
  64.  
  65.     end
  66.  
  67.     --[[
  68.     [Purpose: activated when 'ESC' key is pressed, triggers closure of
  69.     text drawing and key handler
  70.     Used: 181-188, 238-240]
  71.     ]]
  72.  
  73.     local endDraw = nil
  74.  
  75.     --[[
  76.     Key handler
  77.     s: String key
  78.     k: Number keycode
  79.     m: Number modifier
  80.         m=1 - Shift key is held
  81.     e: Number event
  82.         e=1 - Key is pressed
  83.         e=2 - Key is released
  84.     ]]
  85.  
  86.     local function getKey(s, k, m, e)
  87.  
  88.         -- Key is pressed and key is a character, not a modifier
  89.  
  90.         if e == 1 and (k < 301 or k > 308) then
  91.  
  92.             -- out->pressed key
  93.  
  94.             out = s
  95.  
  96.             -- setChar - Purpose: controls whether keypress changes the selected character
  97.  
  98.             local setChar = true
  99.  
  100.             -- If Shift key is held
  101.  
  102.             if m == 1 then
  103.  
  104.                 if m == 1 and ({string.gsub(s, '[a-z]', 'MAT')})[1] == 'MAT' then -- Uppercase
  105.                     out = string.upper(s)
  106.                 elseif m == 1 and ({string.gsub(s, '[0-9]', 'MAT')})[1] == 'MAT' and k ~= 304 then -- Numbers to punctuation
  107.                     out = string.sub(')!@#$%^&*(', ({string.find('0123456789', s)})[2], ({string.find('0123456789', s)})[2])
  108.                 elseif m == 1 and ({string.gsub(s, '[`-=\\;\',./]', 'MAT')})[1] == 'MAT' then -- Punctuation to punctuation
  109.                     out = string.sub('~_+|:"<>?', ({string.find('[`-=\\;\',./]', s)})[1], ({string.find('[`-=\\;\',./]', s)})[1])
  110.                 elseif m == 1 and s == '[' then -- '[' to '{'
  111.                     out = '{'
  112.                 elseif m == 1 and s == ']' then -- ']' to '}'
  113.                     out = '}'
  114.                 end
  115.             end
  116.  
  117.             -- If Tab key is pressed
  118.  
  119.             if s == '\t' then
  120.                 if posx <= 95 then -- Move over 5 spaces
  121.                     posx = posx + 5
  122.                 else -- move down 1, back to first row
  123.                     posx = 1
  124.                     posy = posy + 1
  125.                 end
  126.  
  127.                 -- Don't change any characters
  128.  
  129.                 setChar = false
  130.             end
  131.  
  132.             if k == 13 then -- Enter key
  133.                 out = ' '
  134.                 setChar = false
  135.                 posx = 1 -- Move to first row
  136.  
  137.                 if posy < 35 then
  138.                     posy = posy + 1 -- Move down one if possible
  139.                 end
  140.  
  141.             elseif k == 273 then -- Up key
  142.  
  143.                 if posy > 1 then
  144.                     posy = posy - 1 -- Move up one if possible
  145.                 else
  146.                     posy = 35 -- If impossible, go to bottom
  147.                 end
  148.  
  149.                 setChar = false
  150.  
  151.             elseif k == 274 then -- Down key
  152.  
  153.                 if posy < 35 then
  154.                     posy = posy + 1 -- Move down one if possible
  155.                 else
  156.                     posy = 1 -- If impossible, go to top
  157.                 end
  158.  
  159.                 setChar = false
  160.  
  161.             elseif k == 275 then -- Right key
  162.  
  163.                 if posx < 100 then
  164.                     posx = posx + 1 -- Move right one if possible
  165.                 else
  166.                     posx = 1 -- Go to first row
  167.                     if posy < 35 then
  168.                         posy = posy + 1 -- If impossible, move down one
  169.                     else
  170.                         posy = 1 -- or move to the top
  171.                     end
  172.                 end
  173.  
  174.                 setChar = false
  175.  
  176.             elseif k == 276 and posx > 0 then -- Left key
  177.  
  178.                 if posx > 1 then
  179.                     posx = posx - 1 -- Move left one if possible
  180.                 else
  181.                     posx = 100 -- Go to last row
  182.                     if posy > 1 then
  183.                         posy = posy - 1 -- If impossible, go up one
  184.                     else
  185.                         posy = 35 -- or move to the bottom
  186.                     end
  187.                 end
  188.  
  189.                 setChar = false
  190.  
  191.             elseif k == 8 then -- Backspace
  192.  
  193.                 if posx > 1 then
  194.                     posx = posx - 1 -- Go back one
  195.                 else
  196.                     posx = 100 -- Or move to the right all the way
  197.                     posy = posy - 1 -- and move up one
  198.                 end
  199.  
  200.                 text.chars[posy][posx] = ' ' -- Delete a character
  201.  
  202.                 setChar = false
  203.  
  204.             elseif k == 27 then -- Escape key
  205.  
  206.                 tpt.unregister_keypress(getKey) -- Stop key handler
  207.                 endDraw = true -- Trigger closure of text drawing at 237-265
  208.  
  209.                 setChar = false
  210.  
  211.             end
  212.  
  213.             if setChar == true then -- Draw character unless otherwise stated
  214.                 text.chars[posy][posx] = out -- Reassign current character
  215.  
  216.                 maxy = posy -- Recalculate number of lines
  217.  
  218.                 if posx < 100 then
  219.                     posx = posx + 1 -- Move to the right one
  220.                 else
  221.                     posx = 1 -- or all the way to the left
  222.                     posy = posy + 1 -- and down one.
  223.                 end
  224.  
  225.             end
  226.  
  227.         -- If key is released
  228.  
  229.         elseif e == 2 then
  230.             out = '' -- Clear keypress data
  231.         end
  232.  
  233.         -- Ignore hotkeys
  234.  
  235.         return false
  236.  
  237.     end
  238.  
  239.     -- Loop getKey()
  240.  
  241.     tpt.register_keypress(getKey)
  242.  
  243.     -- Text rendering
  244.  
  245.     local function drawText()
  246.  
  247.         -- Current letter coordinates
  248.  
  249.         local cx, cy = 10, 10
  250.  
  251.         --Loop through the entered text
  252.  
  253.         for y = 1, 35 do
  254.             for x = 1, 100 do
  255.  
  256.                 -- Calculate y coordinates
  257.  
  258.                 cy = y * 9 + 10
  259.  
  260.                 -- Default text color
  261.  
  262.                 local col = 255
  263.  
  264.                 -- If the current letter is the entry point...
  265.  
  266.                 if x == posx and y == posy then
  267.  
  268.                     -- Black letter
  269.  
  270.                     col = 0
  271.  
  272.                     -- White background
  273.  
  274.                     tpt.fillrect(cx - 2, cy - 2, tpt.textwidth(text.chars[y][x]) + 3, 11, 255, 255, 255)
  275.                 end
  276.  
  277.                 -- Draw the letter
  278.  
  279.                 tpt.drawtext(cx, cy, text.chars[y][x], col, col, col, 255)
  280.  
  281.                 -- Move the drawing coordinates over by (width of current letter) + 2
  282.  
  283.                 cx = cx + tpt.textwidth(text.chars[y][x]) + 2
  284.  
  285.             end
  286.  
  287.             -- After each row, move down 7
  288.  
  289.             cx = 10
  290.  
  291.         end
  292.  
  293.         -- If the user has hit the escape key [Trigger: 204-211]
  294.  
  295.         if endDraw then
  296.  
  297.             -- Stop text rendering
  298.  
  299.             tpt.unregister_step(drawText)
  300.         end
  301.  
  302.     end
  303.  
  304.     -- Loop text drawing
  305.  
  306.     tpt.register_step(drawText)
  307.  
  308. end
  309.  
  310. -- Function - Retrieve user-entered text
  311.  
  312. function text.get()
  313.  
  314.     -- Returned at 322
  315.  
  316.     local ret = ''
  317.  
  318.     -- Loop through all the significant rows
  319.  
  320.     for k = 1, maxy do
  321.  
  322.         -- Stick row k to the end of the return string, along with a newline
  323.  
  324.         ret = ret .. table.concat(text.chars[k], '') .. '\n'
  325.     end
  326.  
  327.     -- Return the text
  328.  
  329.     return ret
  330.  
  331. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement