forked from Mari0-CE/Mari0-Community-Edition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialogbox.lua
125 lines (109 loc) · 3.62 KB
/
dialogbox.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
119
120
121
122
123
124
125
dialogbox = class("dialogbox")
function dialogbox:init(text, speaker)
self.text = string.lower(text)
self.textsplit = self.text:split(" ")
self.speaker = string.lower(speaker)
self.timer = 0
self.curchar = 0
self.chartimer = 0
self.lifetime = 5
self.chardelay = 0.05
--initialize prefixes
self.hasaprefix = false
for n, i in pairs(self.textsplit) do
local prefix = string.sub(i,1,3)
if prefix == "db:" then
print(i)
self.textsplit[n] = string.sub(i,4)
self.hasaprefix = true
elseif prefix == "gb:" then
print(i, "bool")
self.textsplit[n] = globoolSH(string.sub(i,4), "check")
self.hasaprefix = true
elseif prefix == "gi:" then
print(i, "int")
self.textsplit[n] = globints[string.sub(i,4)] or "uninit"
self.hasaprefix = true
elseif prefix == "bi:" then
print(i, "g_")
self.textsplit[n] = _G[string.sub(i,4)] or "typo"
self.hasaprefix = true
elseif prefix == "bb:" then
local splitup = string.sub(i,4)
splitup = splitup:split("/")
if globoolSH(splitup[1], "check") then
self.textsplit[n] = splitup[2]
else
self.textsplit[n] = splitup[2]
end
self.hasaprefix = true
end
end
if self.hasaprefix then
self.text = table.concat(self.textsplit, " ")
end
--initialize colors
local curcolor = {255, 255, 255}
local i = 1
self.textcolors = {}
while i <= #self.text do
if string.sub(self.text, i, i) == "%" then
local j = i
repeat
j = j + 1
until string.sub(self.text, j, j) == "%" or j > #self.text
curcolor = string.sub(self.text, i+1, j-1):split(",")
--take out that string
self.text = string.sub(self.text, 1, i-1) .. string.sub(self.text, j+1)
else
self.textcolors[i] = {tonumber(curcolor[1]) / COLORCONVERT, tonumber(curcolor[2]) / COLORCONVERT, tonumber(curcolor[3]) / COLORCONVERT}
i = i + 1
end
end
end
function dialogbox:update(dt)
if self.curchar < #self.text then
self.chartimer = self.chartimer + dt
while self.chartimer > self.chardelay do
self.curchar = self.curchar + 1
self.chartimer = self.chartimer - self.chardelay
end
else
self.timer = self.timer + dt
if self.timer > self.lifetime then
return true
end
end
end
function dialogbox:draw()
local boxheight = 45
local margin = 4
local lineheight = 10
love.graphics.setColor(0, 0, 0, 0.5)
love.graphics.rectangle("fill", scale*margin, (height*16-boxheight-margin)*scale, (width*16-margin*2)*scale, boxheight*scale)
love.graphics.setColor(1, 1, 1)
drawrectangle(5, (height*16-margin-boxheight+1), (width*16-margin*2-2), boxheight-2)
local availablepixelsx = width*16-margin*2-6
local availablepixelsy = boxheight-5
local charsx = math.floor(availablepixelsx / 8)
local charsy = math.floor(availablepixelsy / lineheight)
for i = 1, self.curchar do
local x = math.mod(i-1, charsx)+1
local y = math.ceil(i/charsx)
if y <= charsy then
love.graphics.setColor(self.textcolors[i])
properprint(string.sub(self.text, i, i), (7+(x-1)*8)*scale, (height*16-boxheight-margin+4+(y-1)*lineheight)*scale)
else
--abort!
self.curchar = #self.text
end
end
if self.speaker then
love.graphics.setColor(0, 0, 0, 0.5)
love.graphics.rectangle("fill", scale*margin, (height*16-boxheight-margin-10)*scale, (5+#self.speaker*8)*scale, 10*scale)
--love.graphics.setColor(1, 1, 1)
--drawrectangle(5, (height*16-margin-boxheight+1-10), (3+#self.speaker*8), 11)
love.graphics.setColor(self.color or {232/255, 130/255, 30/255})
properprint(self.speaker, (margin+2)*scale, (height*16-margin-boxheight+1-9)*scale)
end
end