Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local BACKGROUNDCOLOR = colors.lightGray
- local TEXTCOLOR = colors.white
- local tbl = {}
- tbl = {
- menu = function( self )
- local content = self.content
- local maxS=0
- for k,v in pairs( content ) do
- if v.text then
- if string.len( v.text ) > maxS then
- maxS = string.len( v.text )
- end
- end
- end
- local menLength = maxS+2
- local menHeight = #content
- for k,v in pairs(content) do
- if not v.text then
- v.text = string.rep( v.repeatString, menLength )
- end
- end
- local width, height = term.getSize()
- local _X, _Y = nil, nil
- if self.x > ( width - menLength + 1 ) then
- _X = width - menLength + 1;
- end
- if self.y > ( height - menHeight + 1 ) then
- _Y = height - menHeight + 1;
- end
- term.setCursorPos( _X or self.x, _Y or self.y )
- local position = { term.getCursorPos() }
- local curY = position[2]
- for k,v in ipairs( content ) do
- term.setBackgroundColor( v.backgroundColor )
- term.setTextColor( v.textColor )
- term.setCursorPos( math.floor( position[1] ), curY )
- local startPos = math.floor( position[1] )
- local xPos = math.floor( position[1] + menLength / 2 - string.len(v.text) / 2 )
- local endPos = math.floor( position[1] + menLength )
- local currentPos = { term.getCursorPos() }
- local yMin = curY;
- local yMax = curY;
- local xMin = startPos;
- local xMax = endPos;
- for x = startPos, endPos do
- term.setCursorPos( currentPos[1], currentPos[2] )
- if currentPos[1] < xPos then
- term.write(" ")
- elseif currentPos[1] > xPos then
- term.write(" ")
- elseif currentPos[1] == xPos then
- write(v.text)
- end
- currentPos = { term.getCursorPos() }
- x = currentPos[1]
- yPos = currentPos[2]
- if x >= endPos then
- break
- end
- end
- v["position"] = {}
- v["position"]["xPos"] = xPos;
- v["position"]["xMin"] = startPos;
- v["position"]["xMax"] = endPos;
- v["position"]["yMin"] = yMin;
- v["position"]["yMax"] = yMax;
- curY = curY + 1
- end
- return {
- initialize = function( x, y )
- local con = self.content;
- local flash = function(menu)
- term.setBackgroundColor( colors.gray )
- term.setTextColor( colors.white )
- term.setCursorPos( con[menu]["position"]["xMin"], con[menu]["position"]["yMin"] )
- local currentPos = { term.getCursorPos() }
- for y = con[menu]["position"]["yMin"], con[menu]["position"]["yMax"] do
- for x = con[menu]["position"]["xMin"], con[menu]["position"]["xMax"] do
- term.setCursorPos( currentPos[1], currentPos[2] )
- if x == con[menu]["position"]["xPos"] then
- write(con[menu].text)
- else
- term.write(" ")
- end
- currentPos = { term.getCursorPos() }
- x = currentPos[1]
- if x >= con[menu]["position"]["xMax"] then
- break
- end
- end
- end
- sleep(0.1)
- term.setBackgroundColor( con[menu].backgroundColor )
- term.setTextColor( con[menu].textColor )
- term.setCursorPos( con[menu]["position"]["xMin"], con[menu]["position"]["yMin"] )
- local currentPos = { term.getCursorPos() }
- for y = con[menu]["position"]["yMin"], con[menu]["position"]["yMax"] do
- for x = con[menu]["position"]["xMin"], con[menu]["position"]["xMax"] do
- term.setCursorPos( currentPos[1], currentPos[2] )
- if x == con[menu]["position"]["xPos"] then
- write(con[menu].text)
- else
- term.write(" ")
- end
- currentPos = { term.getCursorPos() }
- x = currentPos[1]
- if x >= con[menu]["position"]["xMax"] then
- break
- end
- end
- end
- sleep(0.1)
- end
- for k,v in ipairs(con) do
- if x >= v["position"]["xMin"]
- and x < v["position"]["xMax"]
- and y >= v["position"]["yMin"]
- and y <= v["position"]["yMax"]
- then
- if v["func"] then
- flash(k)
- --v["func"]()
- return true
- end
- end
- end
- self.content = nil
- term.setBackgroundColor( BACKGROUNDCOLOR )
- term.clear()
- return false
- end;
- remove = function()
- local con = self.content;
- term.setBackgroundColor( BACKGROUNDCOLOR )
- term.setTextColor( TEXTCOLOR )
- for menu, v in pairs(con) do
- term.setCursorPos( con[menu]["position"]["xMin"], con[menu]["position"]["yMin"] )
- local currentPos = { term.getCursorPos() }
- for y = con[menu]["position"]["yMin"], con[menu]["position"]["yMax"] do
- for x = con[menu]["position"]["xMin"], con[menu]["position"]["xMax"] do
- term.setCursorPos( currentPos[1], currentPos[2] )
- term.write(" ")
- currentPos = { term.getCursorPos() }
- x = currentPos[1]
- if x >= con[menu]["position"]["xMax"] then
- break
- end
- end
- end
- end
- self.content = nil
- return true
- end;
- ["content"]=self.content;
- }
- end;
- }
- term.setBackgroundColor( BACKGROUNDCOLOR )
- term.setTextColor( TEXTCOLOR )
- term.clear()
- while true do
- e = { coroutine.yield() }
- if e[1] == "terminate" then
- os.reboot()
- elseif e[1] == "mouse_click" then
- --term.setBackgroundColor( colors.gray )
- --term.clear()
- if e[2] == 1 then
- if clickMenu ~= nil then
- local resp = clickMenu.initialize( e[3], e[4] )
- if resp == true then
- clickMenu.remove()
- clickMenu = nil
- end
- else
- end
- elseif e[2] == 2 then
- if clickMenu ~= nil then
- clickMenu.remove()
- clickMenu = nil
- end
- mytbl={
- x=e[3];
- y=e[4];
- content = {
- {
- text="New";
- backgroundColor=colors.white;
- textColor=colors.lime;
- func=true;
- };
- {
- text=false;
- repeatString="-";
- backgroundColor=colors.red;
- textColor=colors.white;
- func=false;
- };
- {
- text="Nsd";
- backgroundColor=colors.white;
- textColor=colors.green;
- func=true;
- };
- {
- text="Set";
- backgroundColor=colors.white;
- textColor=colors.orange;
- func=true;
- };
- {
- text="stp";
- backgroundColor=colors.white;
- textColor=colors.red;
- func=true;
- };
- };
- }
- setmetatable( mytbl, { __index = tbl } )
- clickMenu = mytbl:menu()
- end
- end
- end
- term.setCursorPos(1,18)
Advertisement
Add Comment
Please, Sign In to add comment