Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- terminal = { }
- terminal.cx = 10 --X origin
- terminal.cy = 10 --Y origin
- terminal.cpw = 530 --total width
- terminal.cph = 310 --total height
- terminal.bw = 24 --titlebar width, minus X button
- terminal.bh = 22 --titlebar height
- terminal.bwidth = 3 --boarder width
- terminal.render = true --if terminal should render
- terminal.focus = true --if terminal has focus
- terminal.inputt = "" --terminal text
- terminal.command = "" --command to interpret
- terminal.curstimer = 0 --cursor blinking timer
- terminal.dir = "C:\\"
- terminal.prompt = "\n" .. terminal.dir .. "> \0"
- terminal.tcursor = "█" --the blinking cursor
- terminal.cursor = " "
- terminal.linebreaks = 0
- terminal.copyright = "Command Prompt [Version 6.0.1]\n"
- terminal.env = {} --lua enviornment
- terminal.dragging = { active = false, diffX = 0, diffY = 0 }
- local utf8 = require("utf8")
- function terminal:update(dt)
- if self.render == true then
- self.prompt = "\n" .. self.dir .. "> \0"
- self.curstimer = self.curstimer - dt*100
- if self.dragging.active then
- self.focus = true
- local xx, yy
- xx = love.mouse.getX() - self.dragging.diffX
- yy = love.mouse.getY() - self.dragging.diffY
- --check if X/Y is even, for more reliable rendering
- if (xx % 2 == 0) then else
- xx = xx - 1 end
- if (yy % 2 == 0) then else
- yy = yy - 1 end
- self.cx = xx
- self.cy = yy
- end
- --check if the terminal is leaving the upper/left part of the screen
- --don't remove this, the terminal renders incorrectly if it's upperleft corner
- --leaves the screen.
- if self.cx <= 4 then self.cx = 4 end
- if self.cy <= 4 then self.cy = 4 end
- --check if the terminal leaves the lower/right side of the screen.
- --this is optional, and can be disabled
- --if self.cx >= love.window.getWidth()-self.cpw then self.cx = love.window.getWidth()-self.cpw-1 end
- --if self.cy >= love.window.getHeight()-self.cph then self.cy = love.window.getHeight()-self.cph-1 end
- else end
- end
- function terminal:mousepress(x, y, button)
- if self.render == true then
- --boarder dragging
- if button == "l"
- and ((x > self.cx and x < self.cx + self.cpw-self.bw)
- and (y > self.cy and y < self.cy + self.bh))
- then
- self.dragging.active = true
- self.dragging.diffX = x - self.cx
- self.dragging.diffY = y - self.cy
- end
- --lose focus if clicking anywhere that isn't the window
- if button == "l"
- and ((x > self.cx or x < self.cx + self.cpw)
- or (y < self.cy or y > self.cy + self.cph))
- then
- self.focus = false
- end
- --get focus if clicking anywhere on the window
- if button == "l"
- and ((x > self.cx and x < self.cx + self.cpw)
- and (y > self.cy and y < self.cy + self.cph))
- then
- self.focus = true
- end
- --X button functionality
- if button == "l"
- and ((x > self.cx+self.cpw-self.bw and x < self.cx+self.cpw)
- and (y > self.cy and y < self.cy + self.bh))
- then
- self.render = false
- end
- else end
- end
- function terminal:mouserelease(x, y, button)
- if self.render == true then
- if button == "l" then self.dragging.active = false end
- else end
- end
- function terminal:print(text)
- self.inputt = self.inputt .. text
- end
- function terminal:printn(text)
- self.inputt = self.inputt .. "\n" .. text
- self.linebreaks = self.linebreaks + 1
- end
- function terminal:commands()
- local cc = function(s)
- if string.find(self.command, s, 1, string.len(s)) then
- return 1
- end
- return nil
- end
- if cc("sing") then
- self:printn("la la la")
- self:print(string.sub(self.command, 5, -1))
- elseif cc("help") then
- self:printn("= taller\n- shorter\n. wider\n, thinner\nF1 spawn/toggle terminal\nControls only work when terminal doesn't have focus.")
- else
- return nil
- end
- return 1
- end
- function terminal:interp()
- --check for obvious blunders like redefining love callbacks
- --and do not allow them unless the string contains "sudo"
- --in which point ask if they're sure they want to do that.
- --
- --use pcall here.
- if self.command ~= "" and not self:commands() then
- local rs = "return " .. self.command
- if string.find(self.command, "=") then
- rs = self.command .. " return \"" .. self.command .. "\""
- end
- local pout,err = load(rs)
- if pout ~= nil then
- local out = setfenv(pout, getfenv(0))
- self:printn(tostring(out()))
- else
- self:printn("nil\n" .. err)
- end
- end
- end
- function terminal:textinput(t)
- if self.render == true and self.focus == true then
- if t == "\"" then t = "'" end
- self.inputt = self.inputt .. t
- self.command = self.command .. t
- else end
- end
- function terminal:keyboard(key)
- if key == "f1" then
- self.render = not self.render
- end
- if self.render == true and self.focus == true then
- if key == "backspace" and string.sub(self.inputt, -1) ~= '\0' then
- -- get the byte offset to the last UTF-8 character in the string.
- local byteoffset1 = utf8.offset(self.inputt, -1)
- local byteoffset2 = utf8.offset(self.command, -1)
- if byteoffset1 then
- -- remove the last UTF-8 character.
- -- string.sub operates on bytes rather than UTF-8 characters, so we couldn't do string.sub(text, 1, -2).
- self.inputt = string.sub(self.inputt, 1, byteoffset1 - 1)
- end
- if byteoffset2 then
- self.command = string.sub(self.command, 1, byteoffset2 - 1)
- end
- elseif key == "return" then
- self:interp()
- self:print(self.prompt)
- self.command = ""
- end
- else
- if key == "." then self.cpw = self.cpw + 10 end
- if key == "," then if self.cpw > 100 then self.cpw = self.cpw - 10 else end end
- if key == "=" then self.cph = self.cph + 10 end
- if key == "-" then if self.cph > 100 then self.cph = self.cph - 10 else end end
- end
- end
- function terminal:init(x,y,o)
- o.cx = x
- o.cy = y
- return o
- end
- function terminal:spawn(x,y)
- return self:init(x,y,table.copy(terminal))
- end
- function terminal:drawterm()
- local x, y = self.cx, self.cy
- local pw,ph = self.cpw, self.cph
- local w, h = x+pw,y+ph
- local name = "Command Prompt" --.. " X:" .. self.cx .. " Y:" .. self.cy
- local font = love.graphics.newFont("assets/console.ttf", 14)
- local inputfont = love.graphics.newFont("assets/console.ttf", 15)
- local canvas = love.graphics.newCanvas(w, h)
- local textcanvas = love.graphics.newCanvas(w-self.bwidth-1, h-self.bwidth-1)
- love.graphics.setCanvas(canvas)
- canvas:clear()
- love.graphics.setFont(font)
- --boarder
- love.graphics.setColor(60, 60, 60, 255)
- love.graphics.rectangle('fill', x-self.bwidth, y-self.bwidth, w, h)
- --terminal
- love.graphics.setColor(20, 20, 20, 255)
- love.graphics.rectangle('fill', x, y, pw-self.bwidth, ph-self.bwidth)
- --title bar
- love.graphics.setColor(60, 60, 60, 255)
- love.graphics.rectangle('fill', x, y, w, self.bh)
- if self.focus == true then love.graphics.setColor(200, 200, 200, 255)
- else love.graphics.setColor(120, 120, 120, 255) end
- love.graphics.print(name, x+4, y+4)
- --X button
- love.graphics.setColor(100, 20, 20, 255)
- love.graphics.rectangle('fill', x+pw-22, y+2, 16, 15)
- love.graphics.setColor(180, 0, 0, 255)
- love.graphics.rectangle('fill', x+pw-21, y+3, 14, 13)
- love.graphics.setColor(200, 100, 100, 255)
- love.graphics.line(x+pw-21, y+3, x+pw-7, y+16)
- love.graphics.line(x+pw-7, y+3, x+pw-21, y+16)
- --terminal canvas
- love.graphics.setCanvas(textcanvas)
- textcanvas:clear()
- love.graphics.setFont(font)
- love.graphics.setColor(200, 200, 200, 255)
- --blinking cursor
- if self.curstimer <= 0 then
- self.curstimer = 100
- elseif self.curstimer <= 60 and self.focus == true then
- self.cursor = self.tcursor
- else
- self.cursor = " "
- end
- --text scrolling
- local nlcheck = function(s)
- local nln = 1 --number of newlines
- for i=1,string.len(s) do
- if string.sub(s, i, i) == "\n" then
- nln = nln + 1 end
- end
- return nln
- end
- local textwrap = function(stra, limit, indent)
- limit = limit or 72
- indent = indent or ""
- return stra:gsub("(%s+)()(%S+)()",
- function(sp, st, word, fi)
- if (fi-1-#"") > limit then
- here = st - #indent
- return "\n"..indent..word
- end
- end)
- end
- local llb = self.copyright .. self.prompt .. self.inputt .. self.cursor
- while nlcheck(llb) > self.cph / inputfont:getHeight("A") do
- llb = string.sub(llb, self.cph / inputfont:getHeight("A"))
- --llb = textwrap(llb, self.cpw / inputfont:getWidth("A"))
- end
- --print everything
- love.graphics.printf(llb, x+self.bwidth+2, y+self.bh+3, self.cpw-2, "left")
- --love.graphics.printf(str .. self.prompt .. self.inputt .. self.cursor, x+self.bwidth+2, y+self.bh+3, self.cpw-2, "left")
- --render everything
- love.graphics.reset()
- if self.render == true then
- love.graphics.draw(canvas)
- love.graphics.draw(textcanvas)
- end
- love.graphics.setCanvas()
- end
- --[[
- --maybe forget all this and just make a function to check how many newlines there are
- if string.len(string.sub(llb, self.cpw / inputfont:getWidth("A"))) >= (self.cpw / inputfont:getWidth("A")) then --perhaps while/do instead of if/then?
- llb = string.sub(llb, self.cpw / inputfont:getWidth("A"))
- end
- if string.len(self.command) > self.cpw / inputfont:getWidth("A") then
- llb = string.sub(llb, string.len(llb) - self.cpw / inputfont:getWidth("A")) .. "\n" ..
- string.sub(llb, -(self.cpw / inputfont:getWidth("A")))
- end
- if string.len(self.command) > self.cpw / inputfont:getWidth("A") then
- --get self.command up to self.cpw and put a newline there
- llb = string.sub(llb, -(string.len(self.command))) ..
- string.sub(self.command, self.cpw / inputfont:getWidth("A")) .. "\n" ..
- string.sub(self.command, -(string.len(self.command)/2))
- end
- local textwrap = function(stra, limit, indent)
- limit = limit or 72
- indent = indent or ""
- return stra:gsub("(%s+)()(%S+)()",
- function(sp, st, word, fi)
- if (fi-1-#"") > limit then
- here = st - #indent
- return "\n"..indent..word
- end
- end)
- end
- --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement