-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.lua
More file actions
222 lines (196 loc) · 7.27 KB
/
Copy pathhelpers.lua
File metadata and controls
222 lines (196 loc) · 7.27 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
local helpers = {
fontHeading = love.graphics.newFont(42),
fontSubheading = love.graphics.newFont(32),
fontBody = love.graphics.newFont(24),
bodyOffset = 130,
codeContext = {},
codeLeft = false,
codeLeftAlign = 50,
}
function helpers.reset()
love.graphics.setColor(Theme.Text)
love.graphics.setFont(helpers.fontBody)
helpers.bodyOffset = 130
end
function helpers.heading(text)
helpers.reset()
love.graphics.setColor(Theme.Text)
love.graphics.setFont(helpers.fontHeading)
love.graphics.printf(text, 0, 50, love.graphics.getWidth(), "center")
end
function helpers.subheading(text)
helpers.bodyOffset = helpers.bodyOffset + 20
love.graphics.setColor(Theme.Text)
love.graphics.setFont(helpers.fontSubheading)
love.graphics.printf(text, helpers.codeLeft and helpers.codeLeftAlign or 0, helpers.bodyOffset, love.graphics.getWidth(), helpers.codeLeft and "left" or "center")
helpers.bodyOffset = helpers.bodyOffset + 50
end
local colorMap = {
B = Theme.Blue,
G = Theme.Green,
R = Theme.Red,
Y = Theme.Yellow,
W = Theme.Text,
}
function helpers.body(text)
local text = tostring(text)
love.graphics.setFont(helpers.fontBody)
local defaultColor = Theme.Text
local segments = {}
local i = 1
local currentColor = defaultColor
while i <= #text do
local s, e, code = text:find("%$([A-Z])", i)
if not s then
table.insert(segments, {
text = text:sub(i),
color = currentColor
})
break
end
if s > i then
table.insert(segments, {
text = text:sub(i, s - 1),
color = currentColor
})
end
currentColor = colorMap[code] or defaultColor
i = e + 1
end
local x
if helpers.codeLeft then
x = helpers.codeLeftAlign
else
local totalWidth = 0
for _, segment in ipairs(segments) do
totalWidth = totalWidth + helpers.fontBody:getWidth(segment.text)
end
x = (love.graphics.getWidth() - totalWidth) / 2
end
local y = helpers.bodyOffset
for _, segment in ipairs(segments) do
love.graphics.setColor(segment.color)
love.graphics.print(segment.text, x, y)
x = x + helpers.fontBody:getWidth(segment.text)
end
love.graphics.setColor(Theme.Text)
helpers.bodyOffset = helpers.bodyOffset + 30
end
local function Set(list)
local set = {}
for _, l in ipairs(list) do set[l] = true end
return set
end
function helpers.drawWord(text, x, y)
local keywords = Set{"function", "end", "if", "then", "else", "elseif", "for", "in", "do", "while", "repeat", "until", "return"}
local color = Theme.Variable
if keywords[text] then
color = Theme.Keyword
end
if text == "(" and helpers.codeContext[#helpers.codeContext] == "function" then
table.remove(helpers.codeContext)
end
if helpers.codeContext[#helpers.codeContext] == "function" then
color = Theme.Function
end
if text == "function" then
table.insert(helpers.codeContext, "function")
elseif text:sub(1, 2) == "--" then
love.graphics.setColor(Theme.Comment)
love.graphics.print(text, x, y)
return
elseif text == "local" then
color = Theme.Local
elseif text:match("^%d+$") then
color = Theme.Number
elseif (text:sub(1, 1) == "\"" and text:sub(-1) == "\"") or (text:sub(1, 1) == "'" and text:sub(-1) == "'") then
color = Theme.String
end
helpers.drawCharacters(text, x, y, color)
end
function helpers.drawCharacters(text, x, y, color)
local operators = Set{"+", "-", "*", "/", "=", "==", "~=", "<", ">", "<=", ">=", "%", "^", "#", "..", ".", ":", ",", ";", "(", ")", "{", "}", "[", "]"}
for char in text:gmatch(".") do
if operators[char] then
love.graphics.setColor(Theme.Text)
else
love.graphics.setColor(color)
end
love.graphics.print(char, x, y)
x = x + helpers.fontBody:getWidth(char)
end
end
function helpers.code(text)
love.graphics.setFont(helpers.fontBody)
helpers.codeContext = {}
local operators = Set{"+", "-", "*", "/", "=", "==", "~=", "<", ">", "<=", ">=", "%", "^", "#", "..", ".", ":", ",", ";", "(", ")", "{", "}", "[", "]"}
local leftAlign = helpers.codeLeft and helpers.codeLeftAlign or love.graphics.getWidth() / 7 * 2
-- draw line by line
for line in text:gmatch("[^\n]+") do
helpers.codeContext = {}
x = leftAlign
local currentToken = ""
local inString = false
local stringChar = nil
for i = 1, #line do
local char = line:sub(i, i)
local nextChar = line:sub(i + 1, i + 1)
-- Handle strings
if (char == "\"" or char == "'") and not inString then
inString = true
stringChar = char
if #currentToken > 0 then
helpers.drawWord(currentToken, x, helpers.bodyOffset)
x = x + helpers.fontBody:getWidth(currentToken)
currentToken = ""
end
currentToken = char
elseif inString and char == stringChar then
currentToken = currentToken .. char
helpers.drawWord(currentToken, x, helpers.bodyOffset)
x = x + helpers.fontBody:getWidth(currentToken)
currentToken = ""
inString = false
stringChar = nil
elseif inString then
currentToken = currentToken .. char
-- Check if we're starting a comment
elseif char == "-" and nextChar == "-" then
-- Draw accumulated token first
if #currentToken > 0 then
helpers.drawWord(currentToken, x, helpers.bodyOffset)
x = x + helpers.fontBody:getWidth(currentToken)
currentToken = ""
end
-- Draw the rest of the line as a comment
helpers.drawWord(line:sub(i), x, helpers.bodyOffset)
break
elseif char == " " then
if #currentToken > 0 then
helpers.drawWord(currentToken, x, helpers.bodyOffset)
x = x + helpers.fontBody:getWidth(currentToken)
currentToken = ""
end
x = x + helpers.fontBody:getWidth(" ")
elseif operators[char] then
-- Draw accumulated token first
if #currentToken > 0 then
helpers.drawWord(currentToken, x, helpers.bodyOffset)
x = x + helpers.fontBody:getWidth(currentToken)
currentToken = ""
end
-- Draw operator
helpers.drawWord(char, x, helpers.bodyOffset)
x = x + helpers.fontBody:getWidth(char)
else
currentToken = currentToken .. char
end
end
if #currentToken > 0 then
helpers.drawWord(currentToken, x, helpers.bodyOffset)
end
helpers.bodyOffset = helpers.bodyOffset + 30
end
helpers.bodyOffset = helpers.bodyOffset + 30
end
return helpers