Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function menu(...)
- --???? ????? ????????? < 3 ? ??? ???????? ????????? ?? TABLE, ?? ?????? ?????? ?? ????????????? API
- if #arg < 3 or type(arg[3]) ~= "table" then error("Usage: menu(number x,number y,table Element1,table Element2, table Element3...) \nReturns: string Action \n<Element> structure: {string Name[, boolean isElementHidden][,number colorOfText]}") end
- --????????? ??????? ????????
- local xSize, ySize = term.getSize()
- --??????? ??????????? ?????? ? ????????? ??????????? ? ????????? ??????
- local function text(x,y,text1,color)
- term.setTextColor(color)
- term.setCursorPos(x,y)
- term.write(text1)
- end
- --???????? ???????? ? ?????? (? ????? ?????? ??????, ?????????? ?? ????? ???????? ????)
- local Objects = {}
- local function newObj(name,isHidden,xStart,xEnd,y)
- Objects[name]={}
- Objects[name]["isHidden"] = isHidden
- Objects[name]["xStart"] = xStart
- Objects[name]["xEnd"] = xEnd
- Objects[name]["y"] = y
- end
- --??????????? ?????? ???????? ?? ???-?? ???????? ???????? ????
- local theLongestElement = #arg[3][1]
- for i=3,#arg do
- if arg[i] ~= "-" and theLongestElement < #arg[i][1] then
- theLongestElement = #arg[i][1]
- end
- end
- --??????????? ??????? ????
- local xSizeOfMenu = theLongestElement + 4
- local ySizeOfMenu = #arg-2
- local xStartToDisplay = nil
- local yStartToDisplay = nil
- --????????? ???????? ? ????????
- local function square(x1,y1,width,height,color)
- local string = string.rep(" ",width)
- term.setBackgroundColor(color)
- for y=y1,(y1+height-1) do
- term.setCursorPos(x1,y)
- term.write(string)
- end
- end
- --????????? ??????????? ? ????
- local function drawSeparator(x,y,size)
- term.setTextColor(colors.lightGray)
- term.setCursorPos(x,y)
- term.write(string.rep("-",size))
- end
- --??????? ????????? ????? ????
- local function drawMenu(xMenu,yMenu)
- --????????? ?????????? ????, ???? ?? ???? ?????? ?? ????????
- if yMenu+ySizeOfMenu - 1 >= ySize then yMenu = yMenu - (yMenu+ySizeOfMenu - 1 - ySize) - 1 end
- if xMenu+xSizeOfMenu - 1 >= xSize then xMenu = xMenu - (xMenu+xSizeOfMenu - 1 - xSize) - 1 end
- --??????????? ????? ?????? ??????????? ?????? ? ????
- xStartToDisplay = xMenu + 2
- yStartToDisplay = yMenu
- --????????? ???? ????
- square(xMenu+1,yMenu+1,xSizeOfMenu,ySizeOfMenu,colors.gray)
- --????????? ????? ???????? ????
- square(xMenu,yMenu,xSizeOfMenu,ySizeOfMenu,colors.white)
- --???? ???????? ???? ????????? ????, ????????????? ?????? ? ??????
- for i=3,#arg do
- --???? ????????? ??????? ?? ???????? ????????????, ??
- if arg[i] ~= "-" then
- --??????? ???????????? ????? ?????? ??? ???????? ????
- local contextColor = colors.black
- --???? ????????_2 (???????/?? ???????) ? ???????_3 (???? ?????? ????????) ??? ???????????????? ???????? ?? ???????, ??
- if arg[i][2] == nil and arg[i][3] == nil then
- contextColor = colors.black
- --???? ???????? 2 = FALSE ? ???????? 3 ?? ??????, ??
- elseif arg[i][2] == false and arg[i][3] == nil then
- contextColor = colors.black
- --???? ???????? 2 = FALSE ? ???????? 3 ??????, ??
- elseif arg[i][2] == false and arg[i][3] ~= nil then
- contextColor = arg[i][3]
- --???? ???????? 2 = TRUE, ?? ??????? ?????
- elseif arg[i][2] == true then
- contextColor = colors.lightGray
- end
- --??????????? ?????? ???????? ? ????? ????
- text(xStartToDisplay,yStartToDisplay+i-3,arg[i][1],contextColor)
- --???????? ??????? ???????? ? ??????, ????? ????? ????? ? ???? ??????
- newObj(arg[i][1],arg[i][2],xMenu,xMenu+xSizeOfMenu-1,yStartToDisplay+i-3)
- --? ???? ???-???? ????????, ??
- else
- drawSeparator(xMenu,yStartToDisplay+i-3,xSizeOfMenu)
- end
- end
- end
- --????? ?????????, ?????? ????
- drawMenu(arg[1],arg[2])
- --???????????? ????? ????
- sleep(0.3)
- local event,side,xClick,yClick = os.pullEvent()
- if event == "monitor_touch" then side = 1 end
- if event == "mouse_click" or event == "monitor_touch" then
- --??????? ???? ????????? ??????? ???????? ? ???? ?? ????????? ???????? (???? = ????????, KEY = VAL)
- for key,val in pairs(Objects) do
- --???? ?? ???????? ?? ??????, ? ???? ?????? ?? ???????? ???????, ??
- if xClick >= Objects[key]["xStart"] and xClick <= Objects[key]["xEnd"] and yClick == Objects[key]["y"] and Objects[key]["isHidden"] == false or xClick >= Objects[key]["xStart"] and xClick <= Objects[key]["xEnd"] and yClick == Objects[key]["y"] and Objects[key]["isHidden"] == nil then
- --?????????? ??????? ????????? ???????? ????
- for i=Objects[key]["xStart"],Objects[key]["xEnd"] do
- paintutils.drawPixel(i,Objects[key]["y"],colors.blue)
- end
- --?????????? ??? ?? ?????, ?????? ????? ??????
- text(xStartToDisplay,Objects[key]["y"],key,colors.white)
- --????? ????????? ?????
- sleep(0.3)
- --??????? ???????? KEY (?? ???? ???????? ?????????? ???????? ????) ? ???????? ????????? ????
- return key
- end
- --? ???? ??????? ??????? ???? ?? ??? ??????, ?? ??????? ?????? NIL
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement