Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.17 KB | None | 0 0
  1. console = {}
  2. console.__index = console
  3.  
  4. utf8 = require("utf8")
  5.  
  6. require "Libraries/Stack"
  7.  
  8. width, height = love.graphics.getDimensions( )
  9.  
  10. function console:new()
  11.     local _console = {}
  12.  
  13.     setmetatable(_console,console)  -- make console handle lookup
  14.  
  15.     _console.active = false
  16.     _console.buffer = {}
  17.     _console.commandBuffer = {}
  18.     _console.inputBuffer = {}
  19.     _console.pos = {y=-300}
  20.     _console.lastCommand = ""
  21.     _console.cIndex = 1
  22.     _console.font = love.graphics.newFont("Assets/Modenine.ttf", 16)
  23.     _console.cursor = {pos = 1, char = "_"}
  24.  
  25.     return _console
  26. end
  27.  
  28. local function toBuffer(value)
  29.     local arr = {}
  30.  
  31.     for i = 1, string.len(value) do
  32.         table.insert(arr, value:sub(i, i))
  33.     end
  34.  
  35.     return arr
  36. end
  37.  
  38. function console:show()
  39.     self.active = true
  40.     love.graphics.setFont(self.font)
  41.  
  42.     love.keyboard.setKeyRepeat(true)
  43.  
  44.     Timer.every(0.5, function()
  45.         if self.cursor.char == "" then
  46.             self.cursor.char = "_"
  47.         else
  48.             self.cursor.char = ""
  49.         end
  50.     end)
  51.  
  52.     Timer.tween(0.3, self.pos, {y = 0}, 'out-cubic')
  53. end
  54.  
  55. function console:hide()
  56.     love.keyboard.setKeyRepeat(false)
  57.  
  58.     Timer.tween(0.3, self.pos, {y = -(height / 3 + 50)}, 'out-cubic', function() self.active = false end)
  59. end
  60.  
  61. function console:write(_text)
  62.     table.insert(self.buffer, {text=_text, color={1, 1, 1, 1}, type = "Command"})
  63.     table.insert(self.commandBuffer, self.buffer[#self.buffer])
  64. end
  65.  
  66. function console:writeError(_text)
  67.     table.insert(self.buffer, {text = ">>ERROR: " .. _text, color={1, 0, 0.301, 1}, type = "Error"})
  68. end
  69.  
  70. function console:writeWarning(_text)
  71.     table.insert(self.buffer, {text = ">>WARNING: " .. _text, color={1, 0.639, 0, 1}, type = "Warn"})
  72. end
  73.  
  74. function console:draw()
  75.     if self.active then
  76.         love.graphics.setLineStyle("rough")
  77.         love.graphics.setLineWidth(1)
  78.  
  79.         love.graphics.setColor(0.094, 0.149, 0.227, 0.9)
  80.         love.graphics.rectangle("fill", 0, 0 + self.pos.y, width, (height / 3) + 30)
  81.  
  82.         love.graphics.setColor(0.298, 0.298, 0.376, 1)
  83.         love.graphics.rectangle("line", 10, 10 + self.pos.y, width - 20, (height / 3) - 20)
  84.         love.graphics.rectangle("line", 9, (height / 3) + self.pos.y - 1, width - 18, 22)
  85.  
  86.         love.graphics.setColor(0.125, 0.196, 0.302, 1)
  87.         love.graphics.rectangle("fill", 9, (height / 3) + self.pos.y - 1, width - 19, 21)
  88.  
  89.         love.graphics.setColor(1,1,1,1)
  90.  
  91.         love.graphics.print("$ " .. table.concat(self.inputBuffer), 12, (height / 3) + 2 + self.pos.y)
  92.         love.graphics.print(self.cursor.char,12 + self.font:getWidth("$ ") + (self.cursor.pos - 1) * self.font:getWidth(" "), (height / 3) + 2 + self.pos.y)
  93.  
  94.         love.graphics.setScissor( 10, 10+self.pos.y, width-20, (height/3)-20)
  95.             for i = #self.buffer, 1, -1 do
  96.                 love.graphics.setColor(self.buffer[i].color)
  97.                 love.graphics.print(self.buffer[i].text, 15, (height / 3) - 20 - (#self.buffer - i + 1) * 14 + self.pos.y)
  98.             end
  99.         love.graphics.setScissor()
  100.     end
  101. end
  102.  
  103. function console:update(dt)
  104.  
  105. end
  106.  
  107. function console:Blink()
  108.     Timer.every(0.5, function()
  109.         if self.cursor.char == "" then
  110.             self.cursor.char = "_"
  111.         else
  112.             self.cursor.char = ""
  113.         end
  114.     end)
  115. end
  116.  
  117. function console:getInput()
  118.     if self.active then
  119.         function love.textinput(char)
  120.             table.insert(self.inputBuffer, self.cursor.pos, char)
  121.             self.cursor.pos = self.cursor.pos + 1
  122.         end
  123.     end
  124. end
  125.  
  126. function console:isActive()
  127.   if self.active then return true else return false end
  128. end
  129.  
  130. function console:keypressed(key,code)
  131.     if key == 'tab' then
  132.         if self.active then
  133.             self:hide()
  134.         else
  135.             self:show()
  136.         end
  137.     end
  138.  
  139.     if self.active then
  140.         if key == "backspace" then
  141.             if #self.inputBuffer > 0 then
  142.                 table.remove(self.inputBuffer, self.cursor.pos - 1)
  143.                 self.cursor.pos = self.cursor.pos - 1
  144.             end
  145.         end
  146.  
  147.         if key == "return" and #self.inputBuffer > 0 then
  148.             self:write(table.concat(self.inputBuffer))
  149.  
  150.             local f = loadstring (table.concat(self.inputBuffer))
  151.             status, err = pcall(f)
  152.  
  153.             if status == false then
  154.                 self:writeError(err)
  155.             end
  156.  
  157.             self.inputBuffer = {}
  158.             self.cursor.pos = 1
  159.  
  160.             self.cIndex = #self.commandBuffer + 1
  161.         end
  162.  
  163.         if key == 'v' and love.keyboard.isScancodeDown("lctrl") then
  164.             local input = table.concat(self.inputBuffer) .. love.system.getClipboardText():gsub("\n", " ")
  165.  
  166.             self.inputBuffer = toBuffer(input)
  167.  
  168.             self.cursor.pos = #self.inputBuffer + 1
  169.         end
  170.  
  171.         if key == "up" then
  172.             if self.cIndex > 1 then
  173.                 self.cIndex = self.cIndex - 1
  174.  
  175.                 self.inputBuffer = toBuffer(self.commandBuffer[self.cIndex].text)
  176.                 self.cursor.pos = #self.inputBuffer + 1
  177.             end
  178.         end
  179.  
  180.         if key == "down" then
  181.             if self.cIndex < #self.commandBuffer + 1 then
  182.                 self.cIndex = self.cIndex + 1
  183.  
  184.                 if self.cIndex > #self.commandBuffer then
  185.                     self.inputBuffer = {}
  186.                     self.cursor.pos = 1
  187.                 else
  188.                     self.inputBuffer = toBuffer(self.commandBuffer[self.cIndex].text)
  189.                     self.cursor.pos = #self.inputBuffer + 1
  190.                 end
  191.             end
  192.         end
  193.  
  194.         if key == "left" then
  195.             if self.cursor.pos > 1 then
  196.                 self.cursor.pos = self.cursor.pos - 1
  197.             end
  198.         end
  199.  
  200.         if key == "right" then
  201.             if self.cursor.pos < #self.inputBuffer + 1 then
  202.                 self.cursor.pos = self.cursor.pos + 1
  203.             end
  204.         end
  205.  
  206.         if key == "home" then
  207.             self.cursor.pos = 1
  208.         end
  209.  
  210.         if key == "end" then
  211.             self.cursor.pos = #self.inputBuffer + 1
  212.         end
  213.  
  214.         return true
  215.     end
  216. end
  217.  
  218. return console
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement