-
Notifications
You must be signed in to change notification settings - Fork 0
/
kons.lua
118 lines (94 loc) · 3.03 KB
/
kons.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
--[[
Object-oriented module for drawing multiline text.
-- import table
local kons = require "kons"
-- create an object
local buf = kons.new()
-- other object creation style.
local linesbuffer = kons() -- initial coordinates of drawing.
Usage:
* linesbuffer:draw() - draw first lines pushed by push_text_i(). After it
drawing lines pushed by push()
* linesbuffer:push("hello", 1) - push text to screeen for 1 second
* linesbuffer:pushi("fps" .. fps) -- push text to screen for one frame
* linesbuffer:clear() - full clear of console content
* linesbuffer:show() - show or hide text output by changing internal flag
* linesbuffer:update() - internal mechanics computation. Paste to love.update()
Internal variables:
* linesbuffer:height - height in pixels of drawed text. Updated by :draw() call.
Calls of push() and pushi() can be chained:
linesbuf:pushi("dd"):pushi("bbb")
--]]
local g = love.graphics
local kons = {}
kons.__index = kons
function kons.new()
local self = {
color = {1, 1, 1},
show = true,
strings = {},
strings_i = {},
strings_num = 0,
strings_i_num = 0,
}
return setmetatable(self, kons)
end
function kons:clear()
self.strings_i = {}
self.strings_i_num = 0
self.strings = {}
self.strings_num = 0
end
function kons:push(lifetime, text, ...)
if type(lifetime) ~= "number" then
error("First argument - cardinal value of text lifetime.")
end
assert(lifetime >= 0, string.format("Error: lifetime = %d < 0", lifetime))
self.strings[self.strings_num + 1] = {
text = string.format(text, ...),
lifetime = lifetime,
timestamp = love.timer.getTime()
}
self.strings_num = self.strings_num + 1
return self
end
function kons:pushi(text, ...)
self.strings_i[self.strings_i_num + 1] = string.format(text, ...)
self.strings_i_num = self.strings_i_num + 1
return self
end
function kons:draw(x0, y0)
if not y0 then y0 = 0 end
if not x0 then x0 = 0 end
if not self.show then return end
local y = y0
for k, v in pairs(self.strings_i) do
g.print(v, x0, y)
y = y + g.getFont():getHeight()
self.strings_i[k] = nil -- XXX
end
self.strings_i_num = 0
for _, v in pairs(self.strings) do
--print("v.text " .. v.text)
g.print(v.text, x0, y)
y = y + g.getFont():getHeight()
end
self.height = math.abs(y - y0)
end
function kons:update()
for k, v in pairs(self.strings) do
local time = love.timer.getTime()
v.lifetime = v.lifetime - (time - v.timestamp)
if v.lifetime <= 0 then
self.strings[k] = self.strings[self.strings_num]
self.strings[self.strings_num] = nil
self.strings_num = self.strings_num - 1
else
v.timestamp = time
end
end
end
--return kons
return setmetatable(kons, { __call = function(cls, ...)
return cls.new(...)
end})