-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.lua
118 lines (99 loc) · 2.93 KB
/
render.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
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
renderer = {
headerColor = colors.white,
optionColor = colors.white,
options = {},
optionClicks = {},
selectedItem = 1,
debug = 0,
header = "Renderer Header",
setHeader = function(self, header)
self.header = header
end,
addOption = function(self, option, onClick)
table.insert(self.options, option)
table.insert(self.optionClicks, onClick)
if self.debug == 1 then
print("DEBUG | Inserted " .. option .." into renderer")
end
end,
setHeaderColor = function(self, color)
self.headerColor = color
end,
setOptionColor = function(self, color)
self.optionColor = color
end,
render = function(self)
while true do
term.setTextColor(self.headerColor)
term.clear()
term.setCursorPos(1,1)
printCenter(self.header)
print()
term.setTextColor(self.optionColor)
for i = 1, #self.options do
if i == self.selectedItem then
printCenter("[ " .. self.options[i] .. " ]")
else
printCenter(self.options[i])
end
print("")
end
if self.debug == 1 then
print("DEBUG | Rendered")
end
local event, key = os.pullEvent("key")
if key == keys.down then
if self.selectedItem == #self.options then
self.selectedItem = 1
else
self.selectedItem = self.selectedItem + 1
end
elseif key == keys.up then
if self.selectedItem == 1 then
self.selectedItem = #self.options
else
self.selectedItem = self.selectedItem - 1
end
elseif key == keys.enter then
click(self)
end
end
end,
new = function(self, o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
}
function click(obj)
if obj.debug == 1 then
print(obj.options[1])
print(obj.optionClicks[1])
end
selected = obj.selectedItem
funcString = obj.optionClicks[selected]
func = loadstring(funcString)
setfenv(func, getfenv())
func()
sleep(1)
end
function printCenter(text)
local w, h = term.getSize()
local x, y = term.getCursorPos()
term.setCursorPos(math.floor(w-string.len(text))/2, y)
print(text)
end
----------Do everything below this line----------
--Example uses--
--function hey()
-- print("Works")
-- sleep(3)
--end
--screen = renderer:new{debug = 0}
--screen:addOption("Exit", "break")-
--screen:addOption("Call Func", "hey()")
--screen:addOption("Lua Shell", "shell.run('lua')")
--screen:setHeader("Please Select an Option")
--screen:setHeaderColor(colors.green)
--screen:render()