Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- console = {}
- console.__index = console
- utf8 = require("utf8")
- require "Libraries/Stack"
- width, height = love.graphics.getDimensions( )
- function console:new()
- local _console = {}
- setmetatable(_console,console) -- make console handle lookup
- _console.active = false
- _console.buffer = {}
- _console.commandBuffer = {}
- _console.inputBuffer = {}
- _console.pos = {y=-300}
- _console.lastCommand = ""
- _console.cIndex = 1
- _console.font = love.graphics.newFont("Assets/Modenine.ttf", 16)
- _console.cursor = {pos = 1, char = "_"}
- return _console
- end
- local function toBuffer(value)
- local arr = {}
- for i = 1, string.len(value) do
- table.insert(arr, value:sub(i, i))
- end
- return arr
- end
- function console:show()
- self.active = true
- love.graphics.setFont(self.font)
- love.keyboard.setKeyRepeat(true)
- Timer.every(0.5, function()
- if self.cursor.char == "" then
- self.cursor.char = "_"
- else
- self.cursor.char = ""
- end
- end)
- Timer.tween(0.3, self.pos, {y = 0}, 'out-cubic')
- end
- function console:hide()
- love.keyboard.setKeyRepeat(false)
- Timer.tween(0.3, self.pos, {y = -(height / 3 + 50)}, 'out-cubic', function() self.active = false end)
- end
- function console:write(_text)
- table.insert(self.buffer, {text=_text, color={1, 1, 1, 1}, type = "Command"})
- table.insert(self.commandBuffer, self.buffer[#self.buffer])
- end
- function console:writeError(_text)
- table.insert(self.buffer, {text = ">>ERROR: " .. _text, color={1, 0, 0.301, 1}, type = "Error"})
- end
- function console:writeWarning(_text)
- table.insert(self.buffer, {text = ">>WARNING: " .. _text, color={1, 0.639, 0, 1}, type = "Warn"})
- end
- function console:draw()
- if self.active then
- love.graphics.setLineStyle("rough")
- love.graphics.setLineWidth(1)
- love.graphics.setColor(0.094, 0.149, 0.227, 0.9)
- love.graphics.rectangle("fill", 0, 0 + self.pos.y, width, (height / 3) + 30)
- love.graphics.setColor(0.298, 0.298, 0.376, 1)
- love.graphics.rectangle("line", 10, 10 + self.pos.y, width - 20, (height / 3) - 20)
- love.graphics.rectangle("line", 9, (height / 3) + self.pos.y - 1, width - 18, 22)
- love.graphics.setColor(0.125, 0.196, 0.302, 1)
- love.graphics.rectangle("fill", 9, (height / 3) + self.pos.y - 1, width - 19, 21)
- love.graphics.setColor(1,1,1,1)
- love.graphics.print("$ " .. table.concat(self.inputBuffer), 12, (height / 3) + 2 + self.pos.y)
- love.graphics.print(self.cursor.char,12 + self.font:getWidth("$ ") + (self.cursor.pos - 1) * self.font:getWidth(" "), (height / 3) + 2 + self.pos.y)
- love.graphics.setScissor( 10, 10+self.pos.y, width-20, (height/3)-20)
- for i = #self.buffer, 1, -1 do
- love.graphics.setColor(self.buffer[i].color)
- love.graphics.print(self.buffer[i].text, 15, (height / 3) - 20 - (#self.buffer - i + 1) * 14 + self.pos.y)
- end
- love.graphics.setScissor()
- end
- end
- function console:update(dt)
- end
- function console:Blink()
- Timer.every(0.5, function()
- if self.cursor.char == "" then
- self.cursor.char = "_"
- else
- self.cursor.char = ""
- end
- end)
- end
- function console:getInput()
- if self.active then
- function love.textinput(char)
- table.insert(self.inputBuffer, self.cursor.pos, char)
- self.cursor.pos = self.cursor.pos + 1
- end
- end
- end
- function console:isActive()
- if self.active then return true else return false end
- end
- function console:keypressed(key,code)
- if key == 'tab' then
- if self.active then
- self:hide()
- else
- self:show()
- end
- end
- if self.active then
- if key == "backspace" then
- if #self.inputBuffer > 0 then
- table.remove(self.inputBuffer, self.cursor.pos - 1)
- self.cursor.pos = self.cursor.pos - 1
- end
- end
- if key == "return" and #self.inputBuffer > 0 then
- self:write(table.concat(self.inputBuffer))
- local f = loadstring (table.concat(self.inputBuffer))
- status, err = pcall(f)
- if status == false then
- self:writeError(err)
- end
- self.inputBuffer = {}
- self.cursor.pos = 1
- self.cIndex = #self.commandBuffer + 1
- end
- if key == 'v' and love.keyboard.isScancodeDown("lctrl") then
- local input = table.concat(self.inputBuffer) .. love.system.getClipboardText():gsub("\n", " ")
- self.inputBuffer = toBuffer(input)
- self.cursor.pos = #self.inputBuffer + 1
- end
- if key == "up" then
- if self.cIndex > 1 then
- self.cIndex = self.cIndex - 1
- self.inputBuffer = toBuffer(self.commandBuffer[self.cIndex].text)
- self.cursor.pos = #self.inputBuffer + 1
- end
- end
- if key == "down" then
- if self.cIndex < #self.commandBuffer + 1 then
- self.cIndex = self.cIndex + 1
- if self.cIndex > #self.commandBuffer then
- self.inputBuffer = {}
- self.cursor.pos = 1
- else
- self.inputBuffer = toBuffer(self.commandBuffer[self.cIndex].text)
- self.cursor.pos = #self.inputBuffer + 1
- end
- end
- end
- if key == "left" then
- if self.cursor.pos > 1 then
- self.cursor.pos = self.cursor.pos - 1
- end
- end
- if key == "right" then
- if self.cursor.pos < #self.inputBuffer + 1 then
- self.cursor.pos = self.cursor.pos + 1
- end
- end
- if key == "home" then
- self.cursor.pos = 1
- end
- if key == "end" then
- self.cursor.pos = #self.inputBuffer + 1
- end
- return true
- end
- end
- return console
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement