-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuttonAPI
More file actions
165 lines (137 loc) · 4.32 KB
/
buttonAPI
File metadata and controls
165 lines (137 loc) · 4.32 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
-- vim: set filetype=lua:
-- a basic draw function, used by draw
local rawDraw = function(term, startx, starty, width, height, textx, texty, text, isVertical)
c = 0
y = starty
while y < starty + height do
term.setCursorPos(startx, y)
x = startx
while x < startx + width do
if text and not isVertical and y == texty and x == textx + c and c < #text then
term.write(text:sub(c + 1, c + 1))
c = c + 1
elseif text and isVertical and y == texty + c and x == textx and c < #text then
term.write(text:sub(c + 1, c + 1))
c = c + 1
else
term.write(" ")
end
x = x + 1
end
y = y + 1
end
end
local button = {
get = function(self, term, ...)
ret = {}
termx, termy = term.getSize()
for _, par in ipairs(arg) do
table.insert(ret, type(self[par]) == "function" and self[par](self, termx, termy) or self[par])
end
return unpack(ret)
end,
-- draw the button
-- takes the term and the panel offset
draw = function(self, term, offx, offy)
isSticky, startx, starty, width, height, text, isVertical, textColor, backgroundColor = self:get(term, "isSticky", "x", "y", "width", "height", "text", "isVertical", "textColor", "backgroundColor")
if isSticky then
offx = 0
offy = 0
end
if isVertical then
textX = startx + math.floor(width / 2)
textY = starty + math.floor((height - #text) / 2)
else
textX = startx + math.floor((width - #text) / 2)
textY = starty + math.floor(height / 2)
end
if textColor then
term.setTextColor(textColor)
end
if backgroundColor then
term.setBackgroundColor(backgroundColor)
end
rawDraw(term, startx + offx, starty + offy, width, height, textX + offx, textY + offy, text, isVertical)
end
}
local function isOneOfTypes(var, ...)
for _, v in ipairs(arg) do
if type(var) == v then
return true
end
end
return false
end
function new(options)
assert(isOneOfTypes(options.x, "number", "function"), "button.new: x is not a number or function")
assert(isOneOfTypes(options.y, "number", "function"), "button.new: y is not a number or function")
assert(isOneOfTypes(options.width, "number", "function"), "button.new: width is not a number or function")
assert(isOneOfTypes(options.height, "number", "function"), "button.new: height is not a number or function")
local o = {
x = options.x,
y = options.y,
width = options.width,
height = options.height,
text = options.text or "",
isSticky = options.isSticky or false,
isVertical = options.isVertical or false,
textColor = options.textColor or colors.black,
backgroundColor = options.backgroundColor or colors.white,
callback = options.callback
}
setmetatable(o, {__index = button})
return o
end
function newText(options)
assert(isOneOfTypes(options.x, "number", "function"), "button.newLabel: x is not a number or function")
assert(isOneOfTypes(options.y, "number", "function"), "button.newLabel: y is not a number or function")
assert(isOneOfTypes(options.width, "number", "function"), "button.newLabel: width is not a number or function")
assert(isOneOfTypes(options.height, "number", "function"), "button.newLabel: height is not a number or function")
assert(isOneOfTypes(options.text, "string"), "button.newLabel: text is not a string")
local o = {
x = options.x,
y = options.y,
width = options.width,
height = options.height,
text = options.text,
isSticky = options.isSticky or false,
isVertical = options.isVertical or false,
textColor = options.textColor or colors.white,
backgroundColor = options.backgroundColor or colors.black,
}
setmetatable(o, {__index = button})
return o
end
function newLabel(options)
assert(isOneOfTypes(options.button, "table"), "button.newLabel: button is not a button")
o = newText(options)
o.button = options.button;
o.callback = function(self, xPos, yPos)
if type(self.button.callback) == "function" then
return self.button:callback(xPos, yPos)
end
end
return o
end
--
-- some usefull helper functions
anchorTop = 1
anchorBottom = function(element, termx, termy)
return termy
end
anchorLeft = 1
anchorRight = function(element, termx, termy)
return termx
end
width = function(percent)
return function(element, termx, termy)
return math.floor(termx * percent)
end
end
maxWidth = width(1)
height = function(percent)
return function(element, termx, termy)
return math.floor(termy * percent)
end
end
maxHeight = height(1)