Advertisement
DragonZBW

softwareLib

Apr 16th, 2021 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.20 KB | None | 0 0
  1. -- PASTE MT6gfL6e
  2. local mov = term.setCursorPos
  3. local write = term.write
  4. local color = function(color)
  5. if term.isColor then
  6. term.setTextColor(color)
  7. end
  8. end
  9. local fill = function(color)
  10. if term.isColor then
  11. term.setBackgroundColor(color)
  12. end
  13. end
  14.  
  15. local WIDTH, HEIGHT = term.getSize()
  16.  
  17. -- Getters
  18. local function getWidth()
  19. return WIDTH
  20. end
  21.  
  22. local function getHeight()
  23. return HEIGHT
  24. end
  25.  
  26. -- Draw a panel
  27. local function panel(x, y, width, height)
  28. x = x or 1
  29. y = y or 1
  30. width = width or 1
  31. height = height or 1
  32.  
  33. for r = y, y + height - 1 do
  34. mov(x, r)
  35. local str = ""
  36. for c = x, x + width - 1 do
  37. if (r == y or r == y + height - 1) and (c == x or c == x + width - 1) then
  38. str = str .. string.char(143)
  39. elseif r == y or r == y + height - 1 then
  40. str = str .. "-"
  41. elseif c == x or c == x + width - 1 then
  42. str = str .. "|"
  43. else
  44. str = str .. " "
  45. end
  46. end
  47. write(str)
  48. end
  49. end
  50.  
  51. -- Make text
  52. local function text(x, y, txt)
  53. term.setCursorPos(x, y)
  54. term.write(txt)
  55. end
  56.  
  57. -- Make text centered along the x axis
  58. local function xCenteredText(y, txt)
  59. local x = WIDTH / 2 - string.len(txt) / 2
  60. text(x, y, txt)
  61. end
  62.  
  63. -- Make text centered along the y axis
  64. local function yCenteredText(x, txt)
  65. local y = HEIGHT / 2
  66. text(x, y, txt)
  67. end
  68.  
  69. -- Make text centered in the middle of the screen
  70. local function xyCenteredText(txt)
  71. local x = WIDTH / 2 - string.len(txt) / 2
  72. local y = HEIGHT / 2
  73. text(x, y, txt)
  74. end
  75.  
  76. -- Make a menu option
  77. local function menuOption(name, func)
  78. return {name = name, func = func}
  79. end
  80.  
  81. -- Make a menu object
  82. local function menu(x, y, defaultOption, options)
  83. x = x or 1
  84. y = y or 1
  85. local curOpt = defaultOption or 1
  86. local menuOptions = options or {menuOption("default option", nil)}
  87.  
  88. -- Show the menu until an option gets selected
  89. local event, param
  90. while param ~= keys.enter do
  91. -- Draw the menu
  92. local tempFill = term.getBackgroundColor()
  93. local tempColor = term.getTextColor()
  94. for i, opt in ipairs(menuOptions) do
  95. mov(x, y + i - 1)
  96. if i == curOpt then
  97. term.setBackgroundColor(tempColor)
  98. term.setTextColor(tempFill)
  99. else
  100. term.setBackgroundColor(tempFill)
  101. term.setTextColor(tempColor)
  102. end
  103. write(opt.name)
  104. end
  105. term.setBackgroundColor(tempFill);
  106. term.setTextColor(tempColor)
  107. -- Detect key presses
  108. event, param = os.pullEvent("key")
  109. if param == keys.up then
  110. curOpt = curOpt - 1
  111. if curOpt < 1 then
  112. curOpt = #menuOptions
  113. end
  114. elseif param == keys.down then
  115. curOpt = curOpt + 1
  116. if curOpt > #menuOptions then
  117. curOpt = 1
  118. end
  119. end
  120. end
  121. menuOptions[curOpt].func()
  122. end
  123.  
  124. -- Make a menu centered on the x axis
  125. local function xCenteredMenu(y, defaultOption, options)
  126. local longest = string.len(options[1].name)
  127. for i,v in ipairs(options) do
  128. local len = string.len(v.name)
  129. if len > longest then
  130. longest = len
  131. end
  132. end
  133. local x = WIDTH / 2 - longest / 2
  134. menu(x, y, defaultOption, options)
  135. end
  136.  
  137. -- Make a menu centered on the y axis
  138. local function yCenteredMenu(x, defaultOption, options)
  139. local y = HEIGHT / 2 - #options / 2
  140. menu(x, y, defaultOptions, options)
  141. end
  142.  
  143. -- Make a menu centered in the middle of the screen
  144. local function xyCenteredMenu(defaultOption, options)
  145. local longest = string.len(options[1].name)
  146. for i,v in ipairs(options) do
  147. local len = string.len(v.name)
  148. if len > longest then
  149. longest = len
  150. end
  151. end
  152. local x = WIDTH / 2 - longest / 2
  153. local y = HEIGHT / 2 - #options / 2
  154. menu(x, y, defaultOptions, options)
  155. end
  156.  
  157. -- Make a string input field
  158. local function textField(x, y, width, maxLen)
  159. local text = ""
  160. local caret = true
  161. -- Get keyboard input in loop until enter is pressed
  162. while true do
  163. -- The timer is for a blinking caret
  164. os.startTimer(.5)
  165. local event, key = os.pullEvent()
  166. if event == "key" then
  167. -- Enter ends input
  168. if key == keys.enter or key == keys.numPadEnter then
  169. break
  170. -- Backspace to delete
  171. elseif key == keys.backspace then
  172. if string.len(text) > 0 then
  173. text = string.sub(text, 1, string.len(text) - 1)
  174. end
  175. end
  176. elseif event == "char" and string.len(text) < maxLen then
  177. text = text .. key
  178. end
  179. caret = not caret
  180.  
  181. -- Display the text
  182. mov(x, y)
  183. write(string.rep(" ", width))
  184. mov(x, y)
  185. local dispText = text
  186. if string.len(dispText) > width - 1 then
  187. dispText = string.sub(dispText, -width + 1)
  188. end
  189. write(dispText)
  190. if caret == true then
  191. write("_")
  192. end
  193. end
  194. return text
  195. end
  196.  
  197. -- Make a string input field centered on the x axis
  198. local function xCenteredTextField(y, width, maxLen)
  199. local x = WIDTH / 2 - width / 2
  200. textField(x, y, width, maxLen)
  201. end
  202.  
  203. -- Make a string input field centered on the y axis
  204. local function yCenteredTextField(x, width, maxLen)
  205. local y = HEIGHT / 2
  206. textField(x, y, width, maxLen)
  207. end
  208.  
  209. -- Make a string input field centered in the middle of the screen
  210. local function xyCenteredTextField(width, maxLen)
  211. local x = WIDTH / 2 - width / 2
  212. local y = HEIGHT / 2
  213. textField(x, y, width, maxLen)
  214. end
  215.  
  216. -- Make a number input field
  217. local function numField(x, y, width)
  218. local text = ""
  219. local caret = true
  220. local hasDecimal = false
  221. -- Get keyboard input in loop until enter is pressed
  222. while true do
  223. -- The timer is for a blinking caret
  224. os.startTimer(.5)
  225. local event, key = os.pullEvent()
  226. if event == "key" then
  227. -- Enter ends input
  228. if key == keys.enter or key == keys.numPadEnter then
  229. break
  230. -- Backspace to delete
  231. elseif key == keys.backspace then
  232. if string.len(text) > 0 then
  233. text = string.sub(text, 1, string.len(text) - 1)
  234. end
  235. end
  236. elseif event == "char" and
  237. ((hasDecimal == false and key == ".") or
  238. (string.len(text) == 0 and key == "-") or
  239. (tonumber(key))) then
  240. text = text .. key
  241. end
  242. caret = not caret
  243.  
  244. -- Display the text
  245. mov(x, y)
  246. write(string.rep(" ", width))
  247. mov(x, y)
  248. local dispText = text
  249. if string.len(dispText) > width - 1 then
  250. dispText = string.sub(dispText, -width + 1)
  251. end
  252. write(dispText)
  253. if caret == true then
  254. write("_")
  255. end
  256. end
  257. return tonumber(text)
  258. end
  259.  
  260. -- Make a number input field centered on the x axis
  261. local function xCenteredNumField(y, width)
  262. local x = WIDTH / 2 - width / 2
  263. numField(x, y, width)
  264. end
  265.  
  266. -- Make a number input field centered on the y axis
  267. local function yCenteredNumField(x, width)
  268. local y = HEIGHT / 2
  269. numField(x, y, width)
  270. end
  271.  
  272. -- Make a number input field centered in the middle of the screen
  273. local function xyCenteredNumField(width)
  274. local x = WIDTH / 2 - width / 2
  275. local y = HEIGHT / 2
  276. numField(x, y, width)
  277. end
  278.  
  279. -- Return library
  280. return {
  281. color = color,
  282. fill = fill,
  283. getWidth = getWidth,
  284. getHeight = getHeight,
  285. panel = panel,
  286. text = text,
  287. xCenteredText = xCenteredText,
  288. yCenteredText = yCenteredText,
  289. xyCenteredText = xyCenteredText,
  290. menuOption = menuOption,
  291. menu = menu,
  292. xCenteredMenu = xCenteredMenu,
  293. yCenteredMenu = yCenteredMenu,
  294. xyCenteredMenu = xyCenteredMenu,
  295. textField = textField,
  296. xCenteredTextField = xCenteredTextField,
  297. yCenteredTextField = yCenteredTextField,
  298. xyCenteredTextField = xyCenteredTextField,
  299. numField = numField,
  300. xCenteredNumField = xCenteredNumField,
  301. yCenteredNumField = yCenteredNumField,
  302. xyCenteredNumField = xyCenteredNumField
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement