View difference between Paste ID: RKxvF6Ub and Aik4Ld80
SHOW: | | - or go back to the newest paste.
1
require("class")
2
require("engine")
3
4
-- GUIElement class
5
6
engine.GUIElement = newClass(engine.Entity)
7
8
-- GUIElement methods
9
10
function engine.GUIElement:setHandler(handler, ...)
11
	self.handler = handler
12
	self.handleArgs = ...
13
end
14
15
-- Protected methods
16
17
function engine.GUIElement:handle(...)
18
	self:action(...)
19
	if self.handler then self.handler(self, self.handleArgs) end
20
end
21
22
-- Virtual methods
23
24
function engine.GUIElement:action(...)
25
end
26
27
-- Container class
28
29
engine.Container = newClass(engine.GUIElement)
30
31
-- Container methods
32
33
function engine.Container:setup(width, height)
34
	self:setSize(width or 100, height or 100)
35
	self:setBackgroundColor({200, 200, 200})
36
	self:setBorderColor()
37
	self.borderThickness = 2
38
	self.hasBorder = true
39
	self.hasBackground = true
40
	self.selectable = true
41
end
42
43
function engine.Container:setBorderColor(color)
44
	self.borderColor = color or {50, 50, 50}
45
end
46
47
function engine.Container:draw()
48
	if self.hasBorder then
49
		love.graphics.setLineWidth(self.borderThickness)
50
		love.graphics.setColor(self.borderColor[1], self.borderColor[2], self.borderColor[3])
51
		local a = self.borderThickness / 2
52
		love.graphics.polygon("line", a, a, self.width - a, a, self.width - a, self.height - a, a, self.height - a)
53
	end
54
end
55
56
-- Label class
57
58
engine.Label = newClass(engine.GUIElement)
59
60
-- Label methods
61
62
function engine.Label:setup(text)
63
	self:setSize(1, 1)
64
	self:setTextColor()
65
	self:setFont()
66
	self:setText(text)
67
	self.hasShadow = false
68
	self.shadowOffsetX = -2
69
	self.shadowOffsetY = 2
70
	self.shadowAlpha = 127
71
end
72
73
function engine.Label:setText(text)
74
	self.text = text
75
	self:updateSize()
76
end
77
78
function engine.Label:setTextColor(color)
79
	self.textColor = color or {50, 50, 50}
80
end
81
82
function engine.Label:setFont(font)
83
	self.font = font or love.graphics.newFont(12)
84
	if self.text then self:updateSize() end
85
end
86
87
function engine.Label:preDraw()
88
	love.graphics.setFont(self.font)
89
	if self.hasShadow then
90
		love.graphics.setColor(0, 0, 0, self.shadowAlpha)
91
		love.graphics.print(self.text, self.shadowOffsetX, self.shadowOffsetY)
92
	end
93
	love.graphics.setColor(self.textColor[1], self.textColor[2], self.textColor[3])
94
	love.graphics.print(self.text, 0, 0)
95
end
96
97
-- Private methods
98
99
function engine.Label:updateSize()
100
	self:setSize(self.font:getWidth(self.text), self.font:getHeight())
101
end
102
103
-- Button class
104
105
engine.Button = newClass(engine.Label)
106
107
function engine.Button:setup(text, handler, ...)
108
	self:setHandler(handler, ...)
109
	self.borderThickness = 2
110
	self:getSuperClass():setup(text)
111
	self:setBackgroundColor({200, 200, 200})
112
	self:setBorderColor()
113
	self.hasBackground = true
114
	self.selectable = true
115
end
116
117
engine.Button.setBorderColor = engine.Container.setBorderColor
118
119
function engine.Button:focus(f)
120
	if f then
121
		self:setBackgroundColor({150, 150, 255})
122
	else
123
		self:setBackgroundColor({200, 200, 200})
124
	end
125
	self:refresh()
126
end
127
128
function engine.Button:mousepressed(x, y, button)
129
	self:refresh()
130
end
131
132
engine.Button.mousereleased = engine.Button.mousepressed
133
134
function engine.Button:mouseclicked(x, y, button)
135
	self:handle()
136
end
137
138
function engine.Button:draw()
139
	love.graphics.setBlendMode("alpha")
140
	love.graphics.setLineWidth(self.borderThickness)
141
	love.graphics.setColor(self.borderColor[1], self.borderColor[2], self.borderColor[3])
142
	local a = self.borderThickness / 2
143
	love.graphics.polygon("line", a, a, self.width - a, a, self.width - a, self.height - a, a, self.height - a)
144
	if love.mouse.isDown("l") and self.selected then
145
		love.graphics.setBlendMode("multiplicative")
146
		love.graphics.setColor(0, 0, 0, 127)
147
		love.graphics.polygon("fill", 0, 0, self.width, 0, self.width, self.height, 0, self.height)
148
	end
149
end
150
151
function engine.Button:preDraw()
152
	love.graphics.setColor(self.textColor[1], self.textColor[2], self.textColor[3])
153
	love.graphics.setFont(self.font)
154
	love.graphics.print(self.text, self.borderThickness * 2, self.borderThickness * 2)
155
end
156
157
-- Private methods
158
159
function engine.Button:updateSize()
160
	self:setSize(self.font:getWidth(self.text) + self.borderThickness * 4, self.font:getHeight() + self.borderThickness * 4)
161
end
162
163
-- TextField class
164
165
engine.TextField = newClass(engine.Button)
166
167
function engine.TextField:setup(text, width, height, handler, ...)
168
	self:setHandler(handler, ...)
169
	self.borderThickness = 2
170
	self:getSuperClass():getClass():setup(text)
171
	self:setBackgroundColor({200, 200, 200})
172
	self:setBorderColor()
173
	self.hasBackground = true
174
	self.selectable = true
175
	self:setSize(width or 125, height or 25)
176
end
177
178
function engine.TextField:keypressed(key, unicode)
179
	if key == "return" then
180
		self:handle()
181
	elseif key == "backspace" then
182
		self:setText(string.sub(self.text, 1, #self.text - 1))
183
	elseif unicode > 31 and unicode < 127 then
184
		self:setText(self.text .. string.char(unicode))
185
	end
186
	self:refresh()
187
end
188
189
function engine.TextField:mouseclicked(x, y, button)
190
end
191
192
-- Private methods
193
194
function engine.TextField:updateSize()
195
end