-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsole.lua
74 lines (61 loc) · 2.65 KB
/
console.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
--[[
#########################################################################
# #
# console.lua #
# #
# Love2D in-game console #
# #
# Copyright 2011 Josh Bothun #
# http://minornine.com #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License <http://www.gnu.org/licenses/> for #
# more details. #
# #
#########################################################################
--]]
require 'leaf.object'
require 'leaf.containers'
require 'leaf.context'
-- Default settings --
local HISTORY = 2000
local PADDING = 10
-- Console message --
local Message = leaf.Object:extend()
function Message:init(data, err)
self.data = data
self.err = err and true or false
end
-- Console --
Console = leaf.Context:extend()
function Console:init()
self.font = love.graphics.newFont(10)
self.queue = leaf.Queue(HISTORY)
end
function Console:write(data)
local message = Message(data)
self.queue:push(message)
end
function Console:error(data)
local message = Message(data, true)
self.queue:push(message)
end
function Console:draw()
local width = love.graphics.getWidth()
local height = love.graphics.getHeight()
love.graphics.setColor(255, 255, 255)
love.graphics.setFont(self.font)
for i, message in self.queue:iter_reverse() do
love.graphics.printf(message.data, PADDING,
height - PADDING - i * self.font:getHeight(),
width, 'left')
end
end