-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialog.lua
87 lines (74 loc) · 1.93 KB
/
dialog.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
---@class Dialog
local Dialog = {
prompt = "",
input = "",
cb = nil,
isOpen = false,
ignoreKeypress = false,
}
Dialog.__index = Dialog
---@param cb (fun(text: string))
function Dialog.create(prompt, cb)
local instance = setmetatable({
prompt = prompt,
input = "",
cb = cb,
isOpen = false,
ignoreKeypress = true,
}, Dialog)
return instance
end
function Dialog:open()
self.isOpen = true
self.ignoreKeypress = true
self.input = ""
end
function Dialog:close()
self.isOpen = false
end
function Dialog:draw()
if self.isOpen then
local dialogWidth = 400
local dialogHeight = 200
local windowWidth = love.graphics.getWidth()
local windowHeight = love.graphics.getHeight()
local dialogX = (windowWidth - dialogWidth) / 2
local dialogY = (windowHeight - dialogHeight) / 2
love.graphics.setColor(0, 0, 0, 0.7)
love.graphics.rectangle("fill", 0, 0, windowWidth, windowHeight)
love.graphics.setColor(1, 1, 1)
love.graphics.rectangle("fill", dialogX, dialogY, dialogWidth, dialogHeight)
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle("line", dialogX, dialogY, dialogWidth, dialogHeight)
love.graphics.print(self.prompt, dialogX + 20, dialogY + 20)
love.graphics.rectangle("line", dialogX + 20, dialogY + 50, dialogWidth - 40, 30)
love.graphics.print(self.input, dialogX + 25, dialogY + 55)
love.graphics.print("[Enter] Save [Esc] Cancel", dialogX + 20, dialogY + 100)
end
end
---@param text string
function Dialog:textinput(text)
if self.isOpen and not self.ignoreKeypress then
self.input = self.input .. text
else
self.input = ""
end
end
---@param key string
function Dialog:keypressed(key)
if self.isOpen then
if self.ignoreKeypress then
self.ignoreKeypress = false
return
end
if key == "backspace" then
self.input = self.input:sub(1, -2)
elseif key == "return" then
self:close()
self.cb(self.input)
elseif key == "escape" then
self:close()
end
end
end
return Dialog