-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInput.lua
603 lines (480 loc) · 17.3 KB
/
Input.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
-- Temporary input class yoinked somewhere from my legacy codebase. Will be removed at some point :]
local class = require "com.class"
local Input = class:derive("Input")
local utf8 = require("utf8")
local Color = require("Color")
function Input:new()
local vertices = {}
for i = 0, 200 do
local t = i / 200
local r, g, b = self:getPrimaryInputColor(t, 0)
table.insert(vertices, {i, 0, 0, 0, r, g, b})
table.insert(vertices, {i, 200, 0, 0, 0.5, 0.5, 0.5})
end
self.COLOR_MESH = love.graphics.newMesh(vertices, "strip", "static")
self.SIDE_COLOR_MESHES = {love.graphics.newMesh(42, "strip", "dynamic"), love.graphics.newMesh(42, "strip", "dynamic")}
self.font = love.graphics.newFont()
self.bigFont = love.graphics.newFont(18)
self.inputType = nil
self.inputText = ""
self.inputColor = {x = 0, y = 0, z = 0.5}
self.colorDragging = nil
self.inputExtensions = nil
self.fileList = nil
self.fileListOffset = 0
self.FILE_LIST_ENTRY_HEIGHT = 20
self.FILE_LIST_MAX_ENTRIES = 16
self.fileWarnWhenExists = false
self.fileWarningActive = false
self.shortcut = nil
self.error = nil
self:updateSideColorPickerMeshes()
end
function Input:update(dt)
if self.colorDragging == 1 then
local posX, posY = self:getPos()
self.inputColor.x = math.min(math.max((_MousePos.x - posX - 20) / 200, 0), 1)
self.inputColor.y = math.min(math.max((_MousePos.y - posY - 50) / 200, 0), 1)
self:updateSideColorPickerMeshes()
elseif self.colorDragging == 2 then
local posX, posY = self:getPos()
self.inputColor.z = math.min(math.max((_MousePos.y - posY - 50) / 200, 0), 1)
end
end
function Input:mousepressed(x, y, button, istouch, presses)
if not self.inputType then
return false
end
if button == 1 then
if self:isColorMeshHovered() then
self.colorDragging = 1
return true
elseif self:isSideColorMeshHovered() then
self.colorDragging = 2
return true
elseif self:isFileInputBoxHovered() then
local entry = self:getHoveredFileEntryIndex()
if entry then
self.inputText = self.fileList[entry]
if presses >= 2 then
self:inputAccept()
end
end
return true
elseif self:isConfirmButtonHovered() then
self:inputAccept()
return true
elseif self:isCancelButtonHovered() then
self:inputCancel()
return true
elseif self:isOverwriteYesButtonHovered() then
self:inputAccept()
return true
elseif self:isOverwriteNoButtonHovered() then
self:inputCancel()
return true
elseif not self:isHovered() then
self:inputCancel()
return true
end
end
return false
end
function Input:mousereleased(x, y, button)
if button == 1 then
self.colorDragging = nil
end
end
function Input:wheelmoved(x, y)
if self.inputType == "file" then
local maxY = math.max(#self.fileList - self.FILE_LIST_MAX_ENTRIES, 0)
self.fileListOffset = math.min(math.max(self.fileListOffset - y * 3, 0), maxY)
end
end
function Input:keypressed(key)
if not self.inputType then
return false
end
if key == "backspace" then
if self.inputType == "string" or self.inputType == "number" or self.inputType == "file" then
local offset = utf8.offset(self.inputText, -1)
if offset then
self.inputText = self.inputText:sub(1, offset - 1)
self.error = nil
end
end
return true
elseif key == "return" then
self:inputAccept()
return true
elseif key == "escape" then
self:inputCancel()
return true
end
if self.inputType then
if self.inputType == "shortcut" and not (key == "lctrl" or key == "lshift" or key == "rctrl" or key == "rshift") then
self.shortcut = {key = key, ctrl = _IsCtrlPressed(), shift = _IsShiftPressed()}
end
-- Do not let anything else catch the keyboard input if the input box is currently active.
return true
end
return false
end
function Input:textinput(text)
if self.inputType == "string" or self.inputType == "number" or self.inputType == "file" then
self.inputText = self.inputText .. text
self.error = nil
end
end
function Input:inputAsk(type, value, extensions, warnWhenFileExists)
self.inputType = type
if value then
if type == "string" then
self.inputText = value
elseif type == "number" then
self.inputText = tostring(value)
elseif type == "color" then
self:setInputColor(value.r, value.g, value.b)
elseif type == "file" then
self.inputText = value
self.inputExtensions = extensions
self.fileWarnWhenExists = warnWhenFileExists
self.fileList = self:getFileList()
self.fileListOffset = 0
elseif type == "shortcut" then
self.shortcut = value
end
end
end
function Input:inputCancel()
if self.fileWarningActive then
self.fileWarningActive = false
else
self:inputExit()
end
end
function Input:inputExit()
self.inputType = nil
self.inputText = ""
self.inputColor = {x = 0, y = 0, z = 0.5}
self:updateSideColorPickerMeshes()
self.fileWarningActive = false
end
function Input:inputAccept()
local result = nil
if self.inputType == "string" or self.inputType == "file" then
result = self.inputText
elseif self.inputType == "number" then
result = tonumber(self.inputText)
if not result then
self.error = "Invalid number. Please enter a valid number."
return
end
elseif self.inputType == "color" then
result = Color(self:getInputColor())
elseif self.inputType == "shortcut" then
result = self.shortcut
end
if self.fileWarnWhenExists and not self.fileWarningActive and _Utils.isValueInTable(self.fileList, result) then
self.fileWarningActive = true
else
_EDITOR:onInputReceived(result)
self:inputExit()
end
end
---Color that has been selected in the big picker, not taking the side picker into the account.
function Input:getPrimaryInputColor(x, y)
x, y = x or self.inputColor.x, y or self.inputColor.y
local r = math.min(_Utils.interpolate2Clamped(2, 0, 0, 1/3, x) + _Utils.interpolate2Clamped(0, 2, 2/3, 1, x), 1)
local g = math.min(x < 1/3 and _Utils.interpolate2Clamped(0, 2, 0, 1/3, x) or _Utils.interpolate2Clamped(2, 0, 1/3, 2/3, x), 1)
local b = math.min(x < 2/3 and _Utils.interpolate2Clamped(0, 2, 1/3, 2/3, x) or _Utils.interpolate2Clamped(2, 0, 2/3, 1, x), 1)
return _Utils.interpolate(r, 0.5, y), _Utils.interpolate(g, 0.5, y), _Utils.interpolate(b, 0.5, y)
end
function Input:getInputColor()
local r, g, b = self:getPrimaryInputColor()
local z = 1 - self.inputColor.z
if z > 0.5 then
local t = (z - 0.5) * 2
return _Utils.interpolate(r, 1, t), _Utils.interpolate(g, 1, t), _Utils.interpolate(b, 1, t)
else
local t = z * 2
return _Utils.interpolate(0, r, t), _Utils.interpolate(0, g, t), _Utils.interpolate(0, b, t)
end
end
function Input:setInputColor(r, g, b)
-- Step 1: Determine the Z (brightness): it's (lowest + highest) / 2.
local lowest = math.min(math.min(r, g), b)
local highest = math.max(math.max(r, g), b)
local z = (lowest + highest) / 2
-- Next, modify the color so that Z=0.5...
if z == 0 or z == 1 then
-- Fix a NaN when Z=0 or Z=1. In these scenarios (pure white or pure black, respectively) this value does not matter, so we set it to 0.
r, g, b = 0.5, 0.5, 0.5
elseif z > 0.5 then
local m = 0.5 / (1 - z)
r, g, b = 1 - (1 - r) * m, 1 - (1 - g) * m, 1 - (1 - b) * m
elseif z < 0.5 then
local m = 0.5 / z
r, g, b = r * m, g * m, b * m
end
-- Now check the lowest/average (gray) ratio. This will be the Y value (vibrance).
lowest = math.min(math.min(r, g), b)
highest = math.max(math.max(r, g), b)
local y = lowest / ((lowest + highest) / 2)
-- Modify the color so that Y=0...
local m = (1 - highest) * 2
r, g, b = (r - 0.5 * m) / (1 - m), (g - 0.5 * m) / (1 - m), (b - 0.5 * m) / (1 - m)
-- Finally, extract the X (hue).
-- We start with determining a base value, and then where it goes towards.
local x = 0
if r == 1 then
x = g > 0.001 and (0 + g * 1/6) or (1 - b * 1/6)
elseif g == 1 then
x = b > 0.001 and (1/3 + b * 1/6) or (1/3 - r * 1/6)
elseif b == 1 then
x = r > 0.001 and (2/3 + r * 1/6) or (2/3 - g * 1/6)
end
self.inputColor = {x = x, y = y, z = 1 - z}
self:updateSideColorPickerMeshes()
--return x, y, 1 - z
end
function Input:updateSideColorPickerMeshes()
local vertices = {}
for i = 0, 20 do
local r, g, b = self:getPrimaryInputColor()
table.insert(vertices, {i, 0, 0, 0, 1, 1, 1})
table.insert(vertices, {i, 100, 0, 0, r, g, b})
end
self.SIDE_COLOR_MESHES[1]:setVertices(vertices)
vertices = {}
for i = 0, 20 do
local r, g, b = self:getPrimaryInputColor()
table.insert(vertices, {i, 100, 0, 0, r, g, b})
table.insert(vertices, {i, 200, 0, 0, 0, 0, 0})
end
self.SIDE_COLOR_MESHES[2]:setVertices(vertices)
end
function Input:getFileList()
local files = {}
for i, extension in ipairs(self.inputExtensions) do
local fileList = _Utils.getDirListing("", "file", extension, true)
for j, file in ipairs(fileList) do
table.insert(files, file)
end
end
return files
end
function Input:getPos()
local sizeX, sizeY = self:getSize()
return (_WINDOW_SIZE.x - sizeX) / 2, (_WINDOW_SIZE.y - sizeY) / 2
end
function Input:getSize()
if not self.inputType then
return 0, 0
elseif self.inputType == "string" or self.inputType == "number" or self.inputType == "shortcut" then
return 400, 150
elseif self.inputType == "color" then
return 450, 300
elseif self.inputType == "file" then
return 400, 500
end
end
function Input:getOverwritePos()
local sizeX, sizeY = self:getOverwriteSize()
return (_WINDOW_SIZE.x - sizeX) / 2, (_WINDOW_SIZE.y - sizeY) / 2
end
function Input:getOverwriteSize()
return 500, 110
end
function Input:isHovered()
if not self.inputType then
return false
end
local posX, posY = self:getPos()
local sizeX, sizeY = self:getSize()
return _MousePos.x >= posX and _MousePos.y >= posY and _MousePos.x <= posX + sizeX and _MousePos.y <= posY + sizeY
end
function Input:isColorMeshHovered()
if self.inputType ~= "color" then
return false
end
local posX, posY = self:getPos()
return _MousePos.x >= posX + 20 and _MousePos.y >= posY + 50 and _MousePos.x <= posX + 220 and _MousePos.y <= posY + 250
end
function Input:isSideColorMeshHovered()
if self.inputType ~= "color" then
return false
end
local posX, posY = self:getPos()
return _MousePos.x >= posX + 240 and _MousePos.y >= posY + 50 and _MousePos.x <= posX + 270 and _MousePos.y <= posY + 250
end
function Input:isFileInputBoxHovered()
if self.inputType ~= "file" or self.fileWarningActive then
return false
end
local posX, posY = self:getPos()
return _MousePos.x >= posX + 30 and _MousePos.y >= posY + 75 and _MousePos.x <= posX + 370 and _MousePos.y <= posY + 405
end
function Input:getHoveredFileEntryIndex()
if self.inputType ~= "file" or self.fileWarningActive then
return nil
end
local posX, posY = self:getPos()
-- Too far on the left/right.
if not self:isFileInputBoxHovered() then
return nil
end
local idx = math.floor((_MousePos.y - posY - 75) / self.FILE_LIST_ENTRY_HEIGHT) + 1
-- No entry here.
if idx > #self.fileList then
return nil
end
return idx + self.fileListOffset
end
function Input:isConfirmButtonHovered()
if not self.inputType or self.fileWarningActive then
return false
end
local posX, posY = self:getPos()
local sizeX, sizeY = self:getSize()
return _MousePos.x >= posX + sizeX - 380 and _MousePos.y >= posY + sizeY - 30 and _MousePos.x <= posX + sizeX - 200 and _MousePos.y <= posY + sizeY - 10
end
function Input:isCancelButtonHovered()
if not self.inputType or self.fileWarningActive then
return false
end
local posX, posY = self:getPos()
local sizeX, sizeY = self:getSize()
return _MousePos.x >= posX + sizeX - 180 and _MousePos.y >= posY + sizeY - 30 and _MousePos.x <= posX + sizeX and _MousePos.y <= posY + sizeY - 10
end
function Input:isOverwriteYesButtonHovered()
if not self.fileWarningActive then
return false
end
local posX, posY = self:getOverwritePos()
local sizeX, sizeY = self:getOverwriteSize()
return _MousePos.x >= posX + 80 and _MousePos.y >= posY + sizeY - 30 and _MousePos.x <= posX + 200 and _MousePos.y <= posY + sizeY - 10
end
function Input:isOverwriteNoButtonHovered()
if not self.fileWarningActive then
return false
end
local posX, posY = self:getOverwritePos()
local sizeX, sizeY = self:getOverwriteSize()
return _MousePos.x >= posX + 280 and _MousePos.y >= posY + sizeY - 30 and _MousePos.x <= posX + 400 and _MousePos.y <= posY + sizeY - 10
end
function Input:draw()
if not self.inputType then
return
end
local posX, posY = self:getPos()
local sizeX, sizeY = self:getSize()
love.graphics.setLineWidth(3)
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle("fill", posX, posY, sizeX, sizeY)
love.graphics.setColor(1, 1, 1)
love.graphics.rectangle("line", posX, posY, sizeX, sizeY)
love.graphics.setFont(self.bigFont)
love.graphics.print(string.format("Enter Variable type = %s", self.inputType), posX + 10, posY + 10)
if self.inputType == "string" or self.inputType == "number" then
if self.error then
love.graphics.setColor(1, 0, 0)
love.graphics.setFont(self.font)
love.graphics.print(self.error, posX + 20, posY + 100)
end
love.graphics.rectangle("line", posX + 20, posY + 70, sizeX - 40, 25)
love.graphics.setColor(1, 1, 1)
love.graphics.setFont(self.bigFont)
love.graphics.print(string.format("%s_", self.inputText), posX + 30, posY + 70)
elseif self.inputType == "color" then
-- Main picker
love.graphics.draw(self.COLOR_MESH, posX + 20, posY + 50)
local cx, cy = posX + self.inputColor.x * 200 + 20, posY + self.inputColor.y * 200 + 50
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle("fill", cx - 1, cy - 10, 3, 8)
love.graphics.rectangle("fill", cx - 10, cy - 1, 8, 3)
love.graphics.rectangle("fill", cx - 1, cy + 3, 3, 8)
love.graphics.rectangle("fill", cx + 3, cy - 1, 8, 3)
-- Side picker
love.graphics.setColor(1, 1, 1)
love.graphics.draw(self.SIDE_COLOR_MESHES[1], posX + 240, posY + 50)
love.graphics.draw(self.SIDE_COLOR_MESHES[2], posX + 240, posY + 50)
local cx, cy = posX + 262, posY + self.inputColor.z * 200 + 50
love.graphics.polygon("fill", cx, cy, cx + 8, cy + 8, cx + 8, cy - 8)
-- Details
local r, g, b = self:getInputColor()
love.graphics.setColor(r, g, b)
love.graphics.rectangle("fill", posX + 300, posY + 50, 60, 40)
love.graphics.setColor(1, 1, 1)
love.graphics.rectangle("line", posX + 300, posY + 50, 60, 40)
love.graphics.print(string.format("R: %d", r * 255), posX + 300, posY + 100)
love.graphics.print(string.format("G: %d", g * 255), posX + 300, posY + 120)
love.graphics.print(string.format("B: %d", b * 255), posX + 300, posY + 140)
--local x, y, z = self:setInputColor(r, g, b)
--love.graphics.print(string.format("X: %s", self.inputColor.x), posX + 300, posY + 180)
--love.graphics.print(string.format("Y: %s", self.inputColor.y), posX + 300, posY + 200)
--love.graphics.print(string.format("Z: %s", self.inputColor.z), posX + 300, posY + 220)
--love.graphics.print(string.format("X: %s", x), posX + 350, posY + 180)
--love.graphics.print(string.format("Y: %s", y), posX + 350, posY + 200)
--love.graphics.print(string.format("Z: %s", z), posX + 350, posY + 220)
love.graphics.print(string.format("Hex: %02x%02x%02x", r * 255, g * 255, b * 255), posX + 300, posY + 180)
elseif self.inputType == "file" then
local hoveredEntry = self:getHoveredFileEntryIndex()
-- File list
love.graphics.rectangle("line", posX + 20, posY + 70, sizeX - 40, 330)
for i = 1, math.min(#self.fileList, self.FILE_LIST_MAX_ENTRIES) do
local file = self.fileList[i + self.fileListOffset]
if hoveredEntry == i + self.fileListOffset then
love.graphics.setColor(0, 1, 1)
else
love.graphics.setColor(1, 1, 1)
end
love.graphics.print(file, posX + 30, posY + 75 + (i - 1) * self.FILE_LIST_ENTRY_HEIGHT)
end
-- Input box
love.graphics.setColor(1, 1, 1)
love.graphics.rectangle("line", posX + 20, posY + 420, sizeX - 40, 25)
love.graphics.setFont(self.bigFont)
love.graphics.print(string.format("%s_", self.inputText), posX + 30, posY + 420)
if self.fileWarningActive then
-- File overwrite warning
local posBX, posBY = self:getOverwritePos()
local sizeBX, sizeBY = self:getOverwriteSize()
love.graphics.setLineWidth(3)
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle("fill", posBX, posBY, sizeBX, sizeBY)
love.graphics.setColor(1, 1, 1)
love.graphics.rectangle("line", posBX, posBY, sizeBX, sizeBY)
love.graphics.print(string.format("File \"%s\" already exists.", self.inputText), posBX + 10, posBY + 10)
love.graphics.print("Overwrite it?", posBX + 10, posBY + 30)
love.graphics.setColor(1, 1, 1)
if self:isOverwriteYesButtonHovered() then
love.graphics.setColor(0, 1, 1)
end
love.graphics.print("Yes [ Enter ]", posBX + 80, posBY + sizeBY - 30)
love.graphics.setColor(1, 1, 1)
if self:isOverwriteNoButtonHovered() then
love.graphics.setColor(0, 1, 1)
end
love.graphics.print("No [ Esc ]", posBX + 280, posBY + sizeBY - 30)
end
elseif self.inputType == "shortcut" then
if self.shortcut then
love.graphics.print(_Utils.getShortcutString(self.shortcut), posX + 150, posY + 70)
else
love.graphics.setColor(0.5, 0.5, 0.5)
love.graphics.print("[None set]", posX + 150, posY + 70)
end
end
love.graphics.setColor(1, 1, 1)
if self:isConfirmButtonHovered() then
love.graphics.setColor(0, 1, 1)
end
love.graphics.print("[ Enter ] = Confirm", posX + sizeX - 380, posY + sizeY - 30)
love.graphics.setColor(1, 1, 1)
if self:isCancelButtonHovered() then
love.graphics.setColor(0, 1, 1)
end
love.graphics.print("[ Esc ] = Cancel", posX + sizeX - 180, posY + sizeY - 30)
end
return Input