Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Usage:
- Prompt for text: text.read()
- Opens interface allowing the user to enter text
- Text entry point can be controlled by arrow keys
- Text entry is done by replacement, not insertion
- Read text: text.get()
- Returns a string consisting of all the lines a user has entered
- Only returns the number of lines a user has entered, only one extra newline
- ]]
- -- [Usage: Stores all the global TBGUI variables]
- text = text or {}
- -- [Usage: Table of entered characters (Setup: 58-65, Used: 183-195, drawText - 207-235, text.get - 241-251)]
- text.chars = {}
- -- [Usage: Number of lines the user has entered (Used: 231)]
- local maxy
- -- Prompt text
- function text.read()
- -- Default maxy to 1 (Used: 231)
- maxy = 1
- -- Pause the simulation while the user enters text
- tpt.set_pause(1)
- -- Hide the HUD - it just gets in the way
- tpt.hud(0)
- --[[
- out: [Purpose: Stores the current letter, Used: getKey - 67-202, Main: 214]
- posx, posy: [Purpose: Current text entry points, Used: 183-195, 217-220]
- ]]
- local out, posx, posy = '', 1, 1
- -- Set up text.chars{} to recieve data
- for k = 1, 35 do
- text.chars[k] = {}
- for k2 = 1, 100 do
- text.chars[k][k2] = ' '
- end
- end
- --[[
- [Purpose: activated when 'ESC' key is pressed, triggers closure of
- text drawing and key handler
- Used: 181-188, 238-240]
- ]]
- local endDraw = nil
- --[[
- Key handler
- s: String key
- k: Number keycode
- m: Number modifier
- m=1 - Shift key is held
- e: Number event
- e=1 - Key is pressed
- e=2 - Key is released
- ]]
- local function getKey(s, k, m, e)
- -- Key is pressed and key is a character, not a modifier
- if e == 1 and (k < 301 or k > 308) then
- -- out->pressed key
- out = s
- -- setChar - Purpose: controls whether keypress changes the selected character
- local setChar = true
- -- If Shift key is held
- if m == 1 then
- if m == 1 and ({string.gsub(s, '[a-z]', 'MAT')})[1] == 'MAT' then -- Uppercase
- out = string.upper(s)
- elseif m == 1 and ({string.gsub(s, '[0-9]', 'MAT')})[1] == 'MAT' and k ~= 304 then -- Numbers to punctuation
- out = string.sub(')!@#$%^&*(', ({string.find('0123456789', s)})[2], ({string.find('0123456789', s)})[2])
- elseif m == 1 and ({string.gsub(s, '[`-=\\;\',./]', 'MAT')})[1] == 'MAT' then -- Punctuation to punctuation
- out = string.sub('~_+|:"<>?', ({string.find('[`-=\\;\',./]', s)})[1], ({string.find('[`-=\\;\',./]', s)})[1])
- elseif m == 1 and s == '[' then -- '[' to '{'
- out = '{'
- elseif m == 1 and s == ']' then -- ']' to '}'
- out = '}'
- end
- end
- -- If Tab key is pressed
- if s == '\t' then
- if posx <= 95 then -- Move over 5 spaces
- posx = posx + 5
- else -- move down 1, back to first row
- posx = 1
- posy = posy + 1
- end
- -- Don't change any characters
- setChar = false
- end
- if k == 13 then -- Enter key
- out = ' '
- setChar = false
- posx = 1 -- Move to first row
- if posy < 35 then
- posy = posy + 1 -- Move down one if possible
- end
- elseif k == 273 then -- Up key
- if posy > 1 then
- posy = posy - 1 -- Move up one if possible
- else
- posy = 35 -- If impossible, go to bottom
- end
- setChar = false
- elseif k == 274 then -- Down key
- if posy < 35 then
- posy = posy + 1 -- Move down one if possible
- else
- posy = 1 -- If impossible, go to top
- end
- setChar = false
- elseif k == 275 then -- Right key
- if posx < 100 then
- posx = posx + 1 -- Move right one if possible
- else
- posx = 1 -- Go to first row
- if posy < 35 then
- posy = posy + 1 -- If impossible, move down one
- else
- posy = 1 -- or move to the top
- end
- end
- setChar = false
- elseif k == 276 and posx > 0 then -- Left key
- if posx > 1 then
- posx = posx - 1 -- Move left one if possible
- else
- posx = 100 -- Go to last row
- if posy > 1 then
- posy = posy - 1 -- If impossible, go up one
- else
- posy = 35 -- or move to the bottom
- end
- end
- setChar = false
- elseif k == 8 then -- Backspace
- if posx > 1 then
- posx = posx - 1 -- Go back one
- else
- posx = 100 -- Or move to the right all the way
- posy = posy - 1 -- and move up one
- end
- text.chars[posy][posx] = ' ' -- Delete a character
- setChar = false
- elseif k == 27 then -- Escape key
- tpt.unregister_keypress(getKey) -- Stop key handler
- endDraw = true -- Trigger closure of text drawing at 237-265
- setChar = false
- end
- if setChar == true then -- Draw character unless otherwise stated
- text.chars[posy][posx] = out -- Reassign current character
- maxy = posy -- Recalculate number of lines
- if posx < 100 then
- posx = posx + 1 -- Move to the right one
- else
- posx = 1 -- or all the way to the left
- posy = posy + 1 -- and down one.
- end
- end
- -- If key is released
- elseif e == 2 then
- out = '' -- Clear keypress data
- end
- -- Ignore hotkeys
- return false
- end
- -- Loop getKey()
- tpt.register_keypress(getKey)
- -- Text rendering
- local function drawText()
- -- Current letter coordinates
- local cx, cy = 10, 10
- --Loop through the entered text
- for y = 1, 35 do
- for x = 1, 100 do
- -- Calculate y coordinates
- cy = y * 9 + 10
- -- Default text color
- local col = 255
- -- If the current letter is the entry point...
- if x == posx and y == posy then
- -- Black letter
- col = 0
- -- White background
- tpt.fillrect(cx - 2, cy - 2, tpt.textwidth(text.chars[y][x]) + 3, 11, 255, 255, 255)
- end
- -- Draw the letter
- tpt.drawtext(cx, cy, text.chars[y][x], col, col, col, 255)
- -- Move the drawing coordinates over by (width of current letter) + 2
- cx = cx + tpt.textwidth(text.chars[y][x]) + 2
- end
- -- After each row, move down 7
- cx = 10
- end
- -- If the user has hit the escape key [Trigger: 204-211]
- if endDraw then
- -- Stop text rendering
- tpt.unregister_step(drawText)
- end
- end
- -- Loop text drawing
- tpt.register_step(drawText)
- end
- -- Function - Retrieve user-entered text
- function text.get()
- -- Returned at 322
- local ret = ''
- -- Loop through all the significant rows
- for k = 1, maxy do
- -- Stick row k to the end of the return string, along with a newline
- ret = ret .. table.concat(text.chars[k], '') .. '\n'
- end
- -- Return the text
- return ret
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement