-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextbox.lua
More file actions
113 lines (99 loc) · 4.13 KB
/
Copy pathtextbox.lua
File metadata and controls
113 lines (99 loc) · 4.13 KB
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
local widget = require("widget")
local textBox = {}
textBox.__index = textBox
setmetatable(textBox, widget)
textBox.new = function(app, x, y, width, height, multiline)
local self = {}
setmetatable(self, textBox)
textBox.init(self, app, x, y, width, height, multiline)
return self
end
textBox.init = function(self, app, x, y, width, height, multiline)
widget.init(self, app)
self.width = width
self.height = height
self.x = x
self.y = y
self.text = ""
self.cursorPos = 0
self.maxLen = 1024
self.radius = {x=2, y=2}
if multiline == nil then
self.multiLine = false
else
self.multiLine = multiline
end
self.colors = {
released = {text={0,0,0,1}, fill= {1,1,1,1}, stroke = {0.5,0.5,0.5,1}},
pressed = {text={0,0,0,1}, fill= {1,1,1,1}, stroke={0.5,0.5,0.5,1}},
hovered = {text={0,0,0,1}, fill= {1,1,1,1}, stroke={0.5,0.5,0.5,1}},
focused = {text={0,0,0,1}, fill= {1,1,1,1}, stroke = {0.5,0.5,1,1}},
}
end
textBox.draw = function(self)
local currentColor = self:getColor()
love.graphics.setColor(currentColor.fill)
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height, self.radius.x,self.radius.y)
love.graphics.setColor(currentColor.stroke)
love.graphics.rectangle("line", self.x, self.y, self.width, self.height, self.radius.x,self.radius.y)
love.graphics.setColor(currentColor.text)
local text = string.sub(self.text, 1, self.cursorPos)
local cursor = ''
if self.focused and (self.clock % 1 > 0.5) then
cursor = '|'
end
love.graphics.printf(self.text, self.x, self.y, self:getClientSize().x, "left", 0, 1, 1, self.padding.x *-1, self.padding.y * -1)
local textBeforeCursor = string.sub(self.text, 1, self.cursorPos)
local _,textLines = love.graphics.getFont():getWrap(textBeforeCursor, self:getClientSize().x)
local lastLine = textLines[#textLines]
if string.sub(self.text, #self.text, #self.text) == "\n" then
love.graphics.printf(
cursor,
self.x-love.graphics.getFont():getWidth(cursor)/2,
self.y + love.graphics.getFont():getHeight() * #textLines,
self.width - self.padding.x * 2, "left", 0, 1, 1, self.padding.x *-1, self.padding.y * -1
)
elseif #self.text == 0 then
love.graphics.printf(cursor, self.x, self.y, self.width - self.padding.x * 2, "left", 0, 1, 1, self.padding.x *-1, self.padding.y * -1 )
else
love.graphics.printf(
cursor,
self.x + love.graphics.getFont():getWidth(lastLine)-love.graphics.getFont():getWidth(cursor)/2,
self.y + love.graphics.getFont():getHeight() * (#textLines - 1),
self.width - self.padding.x * 2, "left", 0, 1, 1, self.padding.x *-1, self.padding.y * -1
)
end
--text = text .. string.sub(self.text, self.cursorPos + 1)
--love.graphics.printf(text, self.x, self.y, self.width - self.padding.x * 2, "left", 0, 1, 1, self.padding.x *-1, self.padding.y * -1)
end
function textBox.keyPressed(self, handled, key, keycode, isRepeat)
if key == "backspace" and self.cursorPos > 0 then
self.text = string.sub(self.text, 1, self.cursorPos-1) .. string.sub(self.text, self.cursorPos+1)
self.cursorPos = self.cursorPos-1
elseif key == "left" then
self.cursorPos = math.max(0, self.cursorPos-1)
elseif key == "right" then
self.cursorPos = math.min(self.text:len(), self.cursorPos+1)
elseif key == "delete" then
self.text = string.sub(self.text, 1, self.cursorPos) .. string.sub(self.text, self.cursorPos+2)
elseif key == "return" then
print(self.multiLine)
if self.multiLine then
self:appendText("\n")
else
--self.callback()
end
end
print (key)
end
function textBox.textInput(self, handled, key)
if self.focused then
self.text = string.sub(self.text, 1, self.cursorPos) .. key .. string.sub(self.text, self.cursorPos + 1)
self.cursorPos = self.cursorPos + 1
end
end
function textBox.appendText(self, text)
self.text = self.text .. text
self.cursorPos = self.cursorPos + #text
end
return textBox