forked from SimonLarsen/duckmarines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextInput.lua
76 lines (61 loc) · 1.83 KB
/
textInput.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
local Component = require("component")
local TextInput = {}
TextInput.__index = TextInput
setmetatable(TextInput, Component)
function TextInput.create(x, y, width, height)
local self = setmetatable({}, TextInput)
self.x, self.y = x, y
self.width = width
self.height = height or 21
self.text = ""
self.backgroundColor = {0, 0, 0}
self.active = false
return self
end
function TextInput:draw()
love.graphics.setColor(love.math.colorFromBytes(self.backgroundColor))
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
love.graphics.setColor(love.math.colorFromBytes(255, 194, 49))
love.graphics.rectangle("line", self.x+0.5, self.y+0.5, self.width-1, self.height-1)
love.graphics.setColor(love.math.colorFromBytes(255, 255, 255))
if self.active == true then
love.graphics.print(self.text.."|", self.x+4, self.y+((self.height-8)/2))
else
love.graphics.print(self.text, self.x+4, self.y+((self.height-8)/2))
end
end
function TextInput:click(x, y)
if x >= self.x and x <= self.x+self.width
and y >= self.y and y <= self.y+self.height then
self.active = true
else
self.active = false
end
end
function TextInput:getText()
return self.text
end
function TextInput:setText(text)
self.text = text
end
function TextInput:setActive(active)
self.active = active
end
function TextInput:keypressed(k)
if self.active == false then return end
if k == "backspace" and self.text:len() > 0 then
self.text = self.text:sub(1, self.text:len()-1)
return true
end
end
function TextInput:textinput(text)
if self.active == false then return end
local uni = text:byte(1)
if (uni >= string.byte("A") and uni <= string.byte("Z"))
or (uni >= string.byte("a") and uni <= string.byte("z"))
or (uni >= string.byte("0") and uni <= string.byte("9")) then
self.text = self.text .. text:upper()
return true
end
end
return TextInput