-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlfeditor.lua
291 lines (260 loc) · 8.77 KB
/
lfeditor.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
local loveframes = require "loveframes"
---@class Diagnostic
local Diagnostic = {
---@type string
msg=nil,
---@type number
line=nil,
---@type number
start=nil,
---@type number
finish=nil,
---@type string|nil
type=nil
}
---@class lfeditor
local editor = loveframes.NewObject("editor", "lfeditor_editor", true)
editor.errorColors = {
info={0, 0.58, 1},
warn={1, 1, 0},
error={1, 0, 0}
}
function editor:initialize()
self.type = "editor"
self.internal = false
self.children = {}
self.internals = {}
self.width = 360
self.height = 240
self.font = love.graphics.getFont()
self.maxLineLength = 70
--- This editor will be limited to the amount of lines in self.lines, the editor user can not add/remove lines
self.lines = {}
self.readOnly = false
--- The colors for each line (loops back to start)
self.lineColors = {{1, 1, 1, 0}}
self.textColor = {1, 1, 1, 1}
---@type Diagnostic[]
self.diagnostics = {}
self.focus = false
self.cursory = 1
self.cursorx = 1
self:SetDrawFunc()
self:update(0)
end
function editor:update(dt)
local parent = self.parent
local base = loveframes.base
local font = self.font
-- move to parent if there is a parent
if parent ~= nil and parent ~= base then
local parentx = parent.x
local parenty = parent.y
local staticx = self.staticx
local staticy = self.staticy
self.x = parentx + staticx
self.y = parenty + staticy
end
self.lineHeight = font:getHeight()
self.lineWidth = self.maxLineLength * font:getWidth("A")
self.lineNumbersWidth = font:getWidth("AA") + 4
self.width = self.lineWidth + self.lineNumbersWidth
self.height = self.lineHeight * #self.lines
end
function editor:draw()
love.graphics.push()
love.graphics.translate(self.x, self.y)
local lineCount = #self.lines
local lineColors = self.lineColors
local prevFont = love.graphics.getFont()
love.graphics.setFont(self.font)
love.graphics.setColor(0.35, 0.35, 0.35, 1)
love.graphics.rectangle("fill", 0, 0, self.lineNumbersWidth, lineCount*self.lineHeight)
love.graphics.setColor(0.9, 0.9, 0.9, 1)
for i=1,lineCount do
love.graphics.printf(tostring(i), 0, (i-1)*self.lineHeight, self.lineNumbersWidth, "center")
end
for i=1,lineCount do
local color = lineColors[(i-1) % #lineColors + 1]
love.graphics.setColor(unpack(color))
love.graphics.rectangle("fill", self.lineNumbersWidth, (i-1)*self.lineHeight, self.lineWidth, self.lineHeight)
end
-- Font already set above
-- local prevFont = love.graphics.getFont()
-- love.graphics.setFont(self.font)
love.graphics.setColor(unpack(self.textColor))
for _, diagnostic in pairs(self.diagnostics) do
local alpha = 0.25
local errorType = diagnostic.type or "error"
local errorColor = {unpack(self.errorColors[errorType] or self.errorColors["error"] or {1, 0, 1})}
table.insert(errorColor, alpha)
love.graphics.setColor(unpack(errorColor))
local textBefore = self.lines[diagnostic.line]:sub(0, diagnostic.start-1)
local text = self.lines[diagnostic.line]:sub(diagnostic.start, diagnostic.finish-1)
local diagX = self.font:getWidth(textBefore)
local diagWidth = self.font:getWidth(text)
if diagWidth <= 0 then
diagWidth = self.font:getWidth("A")
end
love.graphics.rectangle("fill", self.lineNumbersWidth + diagX, (diagnostic.line-1)*self.lineHeight, diagWidth, self.font:getHeight())
end
love.graphics.setColor(1, 1, 1, 1)
for i=1,lineCount do
love.graphics.print(self.lines[i], self.lineNumbersWidth, (i-1)*self.lineHeight)
end
if not self.readOnly then
love.graphics.rectangle("fill", self.lineNumbersWidth + self.font:getWidth(self.lines[self.cursory]:sub(0, self.cursorx-1)), (self.cursory-1)*self.lineHeight + 2, 2, self.lineHeight - 4)
end
local hoveredDiagnostics = {}
local diagnosticsX, diagnosticsY
for _, diagnostic in pairs(self.diagnostics) do
local textBefore = self.lines[diagnostic.line]:sub(0, diagnostic.start-1)
local text = self.lines[diagnostic.line]:sub(diagnostic.start, diagnostic.finish-1)
local diagX = self.font:getWidth(textBefore)
local diagWidth = self.font:getWidth(text)
if diagWidth <= 0 then
diagWidth = self.font:getWidth("A")
end
local x, y = self.lineNumbersWidth + diagX, (diagnostic.line-1)*self.lineHeight
local w, h = diagWidth, self.font:getHeight()
if IsInside(self.x + x, self.y + y, self.x + x + w, self.y + y + h, love.mouse.getX(), love.mouse.getY()) then
if diagnosticsX == nil then
diagnosticsX, diagnosticsY = x, y + h + 2
end
table.insert(hoveredDiagnostics, diagnostic)
end
end
for _, diagnostic in pairs(hoveredDiagnostics) do
local msg = diagnostic.msg
love.graphics.setColor(0.4, 0.4, 0.5, 0.85)
local width = self.font:getWidth(msg)+4
local xOff = 0
if diagnosticsX + width > self.width then
xOff = (diagnosticsX + width) - self.width
end
love.graphics.rectangle("fill", diagnosticsX-xOff, diagnosticsY, width, self.font:getHeight()+4)
love.graphics.setColor(1, 1, 1, 1)
love.graphics.print(msg, diagnosticsX+2-xOff, diagnosticsY+2)
diagnosticsY = diagnosticsY + self.font:getHeight()
end
love.graphics.setFont(prevFont)
love.graphics.pop()
end
function editor:validateCursorY()
self.cursory = ((self.cursory-1) % #self.lines)+1
end
function editor:validateCursor(dontWrapCursor)
self:validateCursorY()
local lineLength = #self.lines[self.cursory] + 1
if self.cursorx > lineLength then
if dontWrapCursor ~= true then
self.cursory = self.cursory + 1
self:validateCursorY()
self.cursorx = 1
else
self.cursorx = #self.lines[self.cursory] + 1
end
elseif self.cursorx < 1 then
if dontWrapCursor ~= true then
self.cursory = self.cursory - 1
self:validateCursorY()
self.cursorx = #self.lines[self.cursory] + 1
else
self.cursorx = 1
end
end
end
function editor:mousepressed(x, y, button)
x, y = x - self.x, y - self.y
if x < 0 or y < 0 or x > self.width or y > self.height or not self.visible or loveframes.state ~= self.state or self.readOnly then
self.focus = false
return
end
self.focus = true
local charSize = self.font:getWidth("A")
local line = math.floor(y / self.lineHeight) + 1
local columnPixels = x-self.lineNumbersWidth+(charSize/2)
if columnPixels < 0 then
return
end
local column = math.floor(columnPixels / charSize) + 1
self.cursory, self.cursorx = line, column
self:validateCursor(true)
end
function editor:keypressed(key, isrepeat)
if not self.visible or not self.focus or loveframes.state ~= self.state or self.readOnly then
return
end
if key == "left" then
self.cursorx = self.cursorx - 1
self:validateCursor()
elseif key == "right" then
self.cursorx = self.cursorx + 1
self:validateCursor()
elseif key == "up" then
local oldCursorY = self.cursory
self.cursory = self.cursory - 1
self:validateCursor(true)
if love.keyboard.isDown("lalt") then
-- TODO Maybe: might want to move diagnostics along with the line
local curLine = self.lines[oldCursorY]
self.lines[oldCursorY] = self.lines[self.cursory]
self.lines[self.cursory] = curLine
self:linesChanged({line=oldCursorY, text=self.lines[oldCursorY]}, {line=self.cursory, text=self.lines[self.cursory]})
end
elseif key == "down" then
local oldCursorY = self.cursory
self.cursory = self.cursory + 1
self:validateCursor(true)
if love.keyboard.isDown("lalt") then
-- TODO Maybe: might want to move diagnostics along with the line
local curLine = self.lines[oldCursorY]
self.lines[oldCursorY] = self.lines[self.cursory]
self.lines[self.cursory] = curLine
self:linesChanged({line=oldCursorY, text=self.lines[oldCursorY]}, {line=self.cursory, text=self.lines[self.cursory]})
end
elseif key == "backspace" then
local line = self.lines[self.cursory]
if #line > 0 and self.cursorx > 1 then
local newLine = line:sub(0, self.cursorx-2) .. line:sub(self.cursorx)
self.lines[self.cursory] = newLine
self.cursorx = self.cursorx - 1
self:validateCursor(true)
self:linesChanged({line=self.cursory, text=newLine})
end
elseif key == "delete" then
local line = self.lines[self.cursory]
if #line > 0 then
local newLine = line:sub(0, self.cursorx-1) .. line:sub(self.cursorx+1)
self.lines[self.cursory] = newLine
self:validateCursor(true)
self:linesChanged({line=self.cursory, text=newLine})
end
end
end
function editor:textinput(char)
if not self.visible or not self.focus or loveframes.state ~= self.state or self.readOnly then
return
end
local line = self.lines[self.cursory]
if #line + #char > self.maxLineLength then
-- Do nothing
else
local newLine = line:sub(0, self.cursorx-1) .. char .. line:sub(self.cursorx)
self.lines[self.cursory] = newLine
self.cursorx = self.cursorx + 1
self:validateCursor(true)
self:linesChanged({line=self.cursory, text=newLine})
end
end
-- Events
---@class lfeditor_LineChange
local LineChange = {
---@type number
line=nil,
---@type string
text=nil
}
---@vararg lfeditor_LineChange[]
function editor:linesChanged(...)
end