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
119
120
121
122
123
124
125
126
127
128
|
--[[
for debug purpose
+-------------------+
|debug msg old |
|... |
|... |
|... |
|debug msg new |
+-------------------+
]]
jin.debug = jin.debug or {}
-- render panel
local panel = nil
local debug = false
-- debug msg buffer
local buffer = {}
-- configure
local bsize = 10
local fsize = 15
local lheight = 18
local alpha = 220
local margin = 10
-- refresh buffer or not
local refresh = true
function jin.debug.init()
debug = true
panel = jin.graphics.Canvas(jin.graphics.size())
end
-- set buffer size
function jin.debug.size(c)
bsize = c
end
function jin.debug.print(msg)
if not debug then return end
msg = tostring(msg)
local tp = type(msg)
if tp ~= "string" and tp ~= "number" then
msg = string.format("print failed, expect string or number but get a %s", tp)
end
-- remove the first one (old msg)
if #buffer >= bsize then
table.remove(buffer, 1)
end
buffer[#buffer + 1] = msg
refresh = true
end
-- clear debug buffer
function jin.debug.clear()
buffer = {}
end
local function getStrHeight(str, lheight)
local h = lheight
if #str == 0 then
h = 0
end
for i = 1, #str do
local c = string.sub(str, i, i)
if c == '\n' then
h = h + lheight
end
end
return h
end
local function getBgQuad()
local width, height = 0, 0
for i = 1, #buffer do
local w, h = jin.graphics.box( buffer[i], fsize, 1, lheight)
height = height + h
if width < w then
width = w
end
end
return width, height
end
-- render to screen
function jin.debug.render()
if not debug then return end
if refresh then
jin.graphics.bind(panel)
jin.graphics.clear(0, 0, 0, 0)
jin.graphics.study()
local ww, wh = jin.graphics.size()
local bgw, bgh = getBgQuad()
jin.graphics.color(0, 0, 0, alpha)
jin.graphics.rect("fill", 0, wh - bgh - margin, bgw + margin, bgh + margin)
jin.graphics.color()
local y = wh
for i = #buffer, 1, -1 do
local msg = buffer[i]
local h = getStrHeight(msg, lheight)
y = y - h
jin.graphics.write(msg, margin / 2, y - margin/ 2, fsize, 1, lheight)
end
jin.graphics.bind()
refresh = false
end
jin.graphics.color()
jin.graphics.draw(panel, 0, 0)
end
function jin.debug.status()
return debug
end
|