Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. function menu(...)
  2. --???? ????? ????????? < 3 ? ??? ???????? ????????? ?? TABLE, ?? ?????? ?????? ?? ????????????? API
  3. 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
  4.  
  5. --????????? ??????? ????????
  6. local xSize, ySize = term.getSize()
  7.  
  8. --??????? ??????????? ?????? ? ????????? ??????????? ? ????????? ??????
  9. local function text(x,y,text1,color)
  10. term.setTextColor(color)
  11. term.setCursorPos(x,y)
  12. term.write(text1)
  13. end
  14.  
  15. --???????? ???????? ? ?????? (? ????? ?????? ??????, ?????????? ?? ????? ???????? ????)
  16. local Objects = {}
  17. local function newObj(name,isHidden,xStart,xEnd,y)
  18. Objects[name]={}
  19. Objects[name]["isHidden"] = isHidden
  20. Objects[name]["xStart"] = xStart
  21. Objects[name]["xEnd"] = xEnd
  22. Objects[name]["y"] = y
  23. end
  24.  
  25. --??????????? ?????? ???????? ?? ???-?? ???????? ???????? ????
  26. local theLongestElement = #arg[3][1]
  27. for i=3,#arg do
  28. if arg[i] ~= "-" and theLongestElement < #arg[i][1] then
  29. theLongestElement = #arg[i][1]
  30. end
  31. end
  32.  
  33. --??????????? ??????? ????
  34. local xSizeOfMenu = theLongestElement + 4
  35. local ySizeOfMenu = #arg-2
  36.  
  37. local xStartToDisplay = nil
  38. local yStartToDisplay = nil
  39.  
  40. --????????? ???????? ? ????????
  41. local function square(x1,y1,width,height,color)
  42. local string = string.rep(" ",width)
  43. term.setBackgroundColor(color)
  44. for y=y1,(y1+height-1) do
  45. term.setCursorPos(x1,y)
  46. term.write(string)
  47. end
  48. end
  49.  
  50. --????????? ??????????? ? ????
  51. local function drawSeparator(x,y,size)
  52. term.setTextColor(colors.lightGray)
  53. term.setCursorPos(x,y)
  54. term.write(string.rep("-",size))
  55. end
  56.  
  57. --??????? ????????? ????? ????
  58. local function drawMenu(xMenu,yMenu)
  59.  
  60. --????????? ?????????? ????, ???? ?? ???? ?????? ?? ????????
  61. if yMenu+ySizeOfMenu - 1 >= ySize then yMenu = yMenu - (yMenu+ySizeOfMenu - 1 - ySize) - 1 end
  62. if xMenu+xSizeOfMenu - 1 >= xSize then xMenu = xMenu - (xMenu+xSizeOfMenu - 1 - xSize) - 1 end
  63.  
  64. --??????????? ????? ?????? ??????????? ?????? ? ????
  65. xStartToDisplay = xMenu + 2
  66. yStartToDisplay = yMenu
  67.  
  68. --????????? ???? ????
  69. square(xMenu+1,yMenu+1,xSizeOfMenu,ySizeOfMenu,colors.gray)
  70.  
  71. --????????? ????? ???????? ????
  72. square(xMenu,yMenu,xSizeOfMenu,ySizeOfMenu,colors.white)
  73.  
  74. --???? ???????? ???? ????????? ????, ????????????? ?????? ? ??????
  75. for i=3,#arg do
  76. --???? ????????? ??????? ?? ???????? ????????????, ??
  77. if arg[i] ~= "-" then
  78. --??????? ???????????? ????? ?????? ??? ???????? ????
  79. local contextColor = colors.black
  80. --???? ????????_2 (???????/?? ???????) ? ???????_3 (???? ?????? ????????) ??? ???????????????? ???????? ?? ???????, ??
  81. if arg[i][2] == nil and arg[i][3] == nil then
  82. contextColor = colors.black
  83. --???? ???????? 2 = FALSE ? ???????? 3 ?? ??????, ??
  84. elseif arg[i][2] == false and arg[i][3] == nil then
  85. contextColor = colors.black
  86. --???? ???????? 2 = FALSE ? ???????? 3 ??????, ??
  87. elseif arg[i][2] == false and arg[i][3] ~= nil then
  88. contextColor = arg[i][3]
  89. --???? ???????? 2 = TRUE, ?? ??????? ?????
  90. elseif arg[i][2] == true then
  91. contextColor = colors.lightGray
  92. end
  93.  
  94. --??????????? ?????? ???????? ? ????? ????
  95. text(xStartToDisplay,yStartToDisplay+i-3,arg[i][1],contextColor)
  96. --???????? ??????? ???????? ? ??????, ????? ????? ????? ? ???? ??????
  97. newObj(arg[i][1],arg[i][2],xMenu,xMenu+xSizeOfMenu-1,yStartToDisplay+i-3)
  98.  
  99. --? ???? ???-???? ????????, ??
  100. else
  101. drawSeparator(xMenu,yStartToDisplay+i-3,xSizeOfMenu)
  102. end
  103. end
  104. end
  105.  
  106. --????? ?????????, ?????? ????
  107. drawMenu(arg[1],arg[2])
  108.  
  109. --???????????? ????? ????
  110. sleep(0.3)
  111. local event,side,xClick,yClick = os.pullEvent()
  112. if event == "monitor_touch" then side = 1 end
  113. if event == "mouse_click" or event == "monitor_touch" then
  114. --??????? ???? ????????? ??????? ???????? ? ???? ?? ????????? ???????? (???? = ????????, KEY = VAL)
  115. for key,val in pairs(Objects) do
  116. --???? ?? ???????? ?? ??????, ? ???? ?????? ?? ???????? ???????, ??
  117. 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
  118. --?????????? ??????? ????????? ???????? ????
  119. for i=Objects[key]["xStart"],Objects[key]["xEnd"] do
  120. paintutils.drawPixel(i,Objects[key]["y"],colors.blue)
  121. end
  122. --?????????? ??? ?? ?????, ?????? ????? ??????
  123. text(xStartToDisplay,Objects[key]["y"],key,colors.white)
  124. --????? ????????? ?????
  125. sleep(0.3)
  126. --??????? ???????? KEY (?? ???? ???????? ?????????? ???????? ????) ? ???????? ????????? ????
  127. return key
  128. end
  129. --? ???? ??????? ??????? ???? ?? ??? ??????, ?? ??????? ?????? NIL
  130. end
  131. end
  132. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement