Advertisement
Oysi

Console

Oct 15th, 2014
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.07 KB | None | 0 0
  1.  
  2. -- Script made by Oysi
  3.  
  4. local this = {}
  5.  
  6. -- Declarations
  7. local open = false
  8.  
  9. local text = ""
  10. local textData = {}
  11. local textLimit = 10
  12.  
  13. local nextBackspace = 0
  14. local backspaceTime = 0.04
  15.  
  16. -- Setup environment
  17. local env = {}
  18. for i, v in pairs(_G) do
  19.     env[i] = v
  20. end
  21.  
  22. -- Settings
  23. function this.setOpen(o) open = o end
  24. function this.getOpen() return open end
  25.  
  26. function this.setTextLimit(t) textLimit = t end
  27. function this.getTextLimit() return textLimit end
  28.  
  29. function this.getEnv() return env end
  30.  
  31. -- Functions
  32. function this.addText(t)
  33.     table.insert(textData, 1, t)
  34.     table.remove(textData, textLimit)
  35. end
  36.  
  37. env.print = this.addText
  38.  
  39. -- Draw
  40. function this.draw()
  41.     -- Draw background
  42.     love.graphics.setColor(50, 50, 50, 200)
  43.     love.graphics.rectangle("fill", 0, 0, love.window.getWidth(), 10 + textLimit*20 + 10)
  44.    
  45.     -- Draw text data
  46.     love.graphics.setColor(255, 255, 255, 255)
  47.     for i = 1, #textData do
  48.         love.graphics.print(tostring(textData[i]), 10, (textLimit - i)*20)
  49.     end
  50.    
  51.     -- Draw text
  52.     love.graphics.print("> " .. text, 10, textLimit*20)
  53. end
  54.  
  55. -- Update
  56. function this.update(delta)
  57.     if love.keyboard.isDown("backspace") then
  58.         nextBackspace = nextBackspace + delta
  59.         if nextBackspace > backspaceTime then
  60.             nextBackspace = nextBackspace - backspaceTime
  61.             text = text:sub(1, -2)
  62.         end
  63.     end
  64. end
  65.  
  66. -- Key pressed
  67. function this.keypressed(k)
  68.     if k == "return" then
  69.         this.addText("> " .. text)
  70.        
  71.         local func, err = loadstring("return " .. text)
  72.         if not func then
  73.             func, err = loadstring(text)
  74.         end
  75.         if func then
  76.             setfenv(func, env)
  77.             local success, result = coroutine.resume(coroutine.create(func))
  78.             if success then
  79.                 if result then
  80.                     this.addText(result)
  81.                 end
  82.             else
  83.                 this.addText(result)
  84.             end
  85.         else
  86.             this.addText(err)
  87.         end
  88.        
  89.         text = ""
  90.     elseif k == "backspace" then
  91.         text = text:sub(1, -2)
  92.         nextBackspace = -0.4
  93.     end
  94. end
  95.  
  96. -- Text input
  97. function this.textinput(t)
  98.     if t == "|" then
  99.         open = not open
  100.     else
  101.         if open then
  102.             text = text .. t
  103.         end
  104.     end
  105. end
  106.  
  107. -- Return
  108. return this
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement