- Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuttonAPI
More file actions
Latest commit
165 lines (137 loc) · 4.32 KB
/
Copy pathbuttonAPI
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
localrawDraw=function(term, startx, starty, width, height, textx, texty, text, isVertical)
c=0
y=starty
whiley<starty+heightdo
term.setCursorPos(startx, y)
x=startx
whilex<startx+widthdo
iftextandnotisVerticalandy==textyandx==textx+candc<#textthen
term.write(text:sub(c+1, c+1))
c=c+1
elseiftextandisVerticalandy==texty+candx==textxandc<#textthen
term.write(text:sub(c+1, c+1))
c=c+1
else
term.write("")
end
x=x+1
end
y=y+1
end
end
localbutton= {
get=function(self, term, ...)
ret= {}
termx, termy=term.getSize()
for_, parinipairs(arg) do
table.insert(ret, type(self[par]) =="function" andself[par](self, termx, termy) orself[par])
end
returnunpack(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")
ifisStickythen
offx=0
offy=0
end
ifisVerticalthen
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
iftextColorthen
term.setTextColor(textColor)
end
ifbackgroundColorthen
term.setBackgroundColor(backgroundColor)
end
rawDraw(term, startx+offx, starty+offy, width, height, textX+offx, textY+offy, text, isVertical)
end
}
localfunctionisOneOfTypes(var, ...)
for_, vinipairs(arg) do
iftype(var) ==vthen
returntrue
end
end
returnfalse
end
functionnew(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")
localo= {
x=options.x,
y=options.y,
width=options.width,
height=options.height,
text=options.textor"",
isSticky=options.isStickyorfalse,
isVertical=options.isVerticalorfalse,
textColor=options.textColororcolors.black,
backgroundColor=options.backgroundColororcolors.white,
callback=options.callback
}
setmetatable(o, {__index=button})
returno
end
functionnewText(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")
localo= {
x=options.x,
y=options.y,
width=options.width,
height=options.height,
text=options.text,
isSticky=options.isStickyorfalse,
isVertical=options.isVerticalorfalse,
textColor=options.textColororcolors.white,
backgroundColor=options.backgroundColororcolors.black,
}
setmetatable(o, {__index=button})
returno
end
functionnewLabel(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)
iftype(self.button.callback) =="function" then
returnself.button:callback(xPos, yPos)
end
end
returno
end
--
-- some usefull helper functions
anchorTop=1
anchorBottom=function(element, termx, termy)
returntermy
end
anchorLeft=1
anchorRight=function(element, termx, termy)
returntermx
end
width=function(percent)
returnfunction(element, termx, termy)
returnmath.floor(termx*percent)
end
end
maxWidth=width(1)
height=function(percent)
returnfunction(element, termx, termy)
returnmath.floor(termy*percent)
end
end
maxHeight=height(1)