-
Notifications
You must be signed in to change notification settings - Fork 1
/
dialog.lua
122 lines (100 loc) · 3.71 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
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
require "textbox"
Dialog = {}
Dialog.__index = Dialog
-- TODO: change dialog to accept a dialog type or name, draw everything based on that
function Dialog.create(type, x, y)
local dialog = {} -- our new object
setmetatable(dialog,Dialog)
dialog.type = type;
dialog.x = x
dialog.y = y
dialog.nameTextbox = Textbox.create("Type Name: ", 0, 0)
dialog.fileTextbox = Textbox.create("File Name: ", 0, 50)
dialog.visible = false
dialog.selectedTextbox = dialog.nameTextbox
dialog.closeButtonX = 30
dialog.addButtonX = 140
dialog.buttonY = 100
dialog.buttonWidth = 60
dialog.buttonHeight = 25
return dialog
end
function Dialog:draw()
if self.type == "addBlockType" then
local r, g, b, a = love.graphics.getColor()
-- grey out the background so the user knows they can't interact with it currently
love.graphics.setColor(0, 0, 0, 150)
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
love.graphics.setColor(r, g, b, a)
love.graphics.push()
love.graphics.translate(self.x, self.y)
-- draw dialog background
love.graphics.setColor(70, 70, 70)
love.graphics.rectangle("fill", -10, -10, 250, 150)
love.graphics.setColor(r, g, b, a)
-- draw Textboxes
self.nameTextbox:draw()
self.fileTextbox:draw()
-- draw add and close buttons
love.graphics.rectangle("fill", self.closeButtonX, self.buttonY, self.buttonWidth, self.buttonHeight)
love.graphics.rectangle("fill", self.addButtonX, self.buttonY, self.buttonWidth, self.buttonHeight)
love.graphics.setColor(0, 0, 0)
love.graphics.print("Add", self.addButtonX + 18, self.buttonY + 6)
love.graphics.print("Close", self.closeButtonX + 13, self.buttonY + 6)
love.graphics.setColor(r, g, b, a)
love.graphics.pop()
end
end
function Dialog:mousepressed(x, y)
-- check if the textboxes have been clicked and if so, select them
if self.nameTextbox:mousepressed(x - self.x, y - self.y) then
self.selectedTextbox = self.nameTextbox
end
if self.fileTextbox:mousepressed(x - self.x, y - self.y) then
self.selectedTextbox = self.fileTextbox
end
if y >= self.y + self.buttonY and y <= self.y + self.buttonY + self.buttonHeight then
-- if add button clicked
if x >= self.x + self.addButtonX and x <= self.x + self.addButtonX + self.buttonWidth then
self:addBlockType()
end
if x >= self.x + self.closeButtonX and x <= self.x + self.closeButtonX + self.buttonWidth then
self.visible = false
self.nameTextbox.content = ""
self.fileTextbox.content = ""
end
end
end
function Dialog:addBlockType()
if Terrain:addBlockType(self.nameTextbox.content, self.fileTextbox.content) then
self.visible = false
self.nameTextbox.content = ""
self.fileTextbox.content = ""
alert = "File added successfully!"
else
-- report error to the user and prompt them to try again
alert = "Couldn't find a file with this name. Please try again."
end
end
function Dialog:keypressed(key, unicode)
if key == "backspace" then
if self.selectedTextbox ~= nil then
self.selectedTextbox.content = string.sub(self.selectedTextbox.content, 1, string.len(self.selectedTextbox.content) - 1)
end
end
if key == "tab" then
if self.selectedTextbox == self.nameTextbox then
self.selectedTextbox = self.fileTextbox
end
end
if key == "return" then
if self.nameTextbox.content ~= "" and self.fileTextbox.content ~= "" then
self:addBlockType()
end
end
if unicode > 31 and unicode < 127 then
if self.selectedTextbox ~= nil then
self.selectedTextbox.content = self.selectedTextbox.content .. string.char(unicode)
end
end
end