Guest User

Untitled

a guest
Aug 26th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.70 KB | None | 0 0
  1. function clearArea(x,y,w,h,char)
  2. local sChar = char or " "
  3. for yy=0,h do
  4. for xx=0,w do
  5. rawWriteLoc(x+xx,y+yy,sChar)
  6. end
  7. end
  8. end
  9.  
  10. function writeLoc(x,y,sText)
  11. term.setCursorPos(x,y)
  12. term.clearLine()
  13. term.write(sText)
  14. end
  15.  
  16. function rawWriteLoc(x,y,sText)
  17. term.setCursorPos(x,y)
  18. term.write(sText)
  19. end
  20.  
  21. function tCopy(t)
  22. local u = { }
  23. for k, v in pairs(t) do u[k] = v end
  24. return setmetatable(u, getmetatable(t))
  25. end
  26.  
  27. function showWindow(x,y,w,h,title,text)
  28. local sTitle = title or ""
  29. local sText = text or ""
  30. width, height = term.getSize()
  31. sTitle = string.sub(sTitle,1,w-2)
  32. iTitleStart = ((w/2)-1)-((#sTitle-1)/2)
  33. if (not x) then
  34. x=(width-w)/2
  35. end
  36. if (not y) then
  37. y=(height-h)/2
  38. end
  39. if (w > width) then w = width end
  40. if (h > height) then h = height end
  41. local line=""
  42. local char=""
  43. local curChar=1
  44. local waitForLine=1
  45. if (sTitle ~= "") then waitForLine = 2 end
  46. for ty=0,h do
  47. for tx=0,w do
  48. char=" "
  49. if (ty==0) or ((ty==2) and (sTitle ~= "")) or (ty==h) then --horizonal line
  50. if (tx==0 or tx==w) then
  51. char="+"
  52. else
  53. char="-"
  54. end
  55. elseif (ty==1) then
  56. if (tx==0 or tx==w) then
  57. char="|"
  58. else
  59. --if (tx-iTitleStart < #sTitle+1) and (tx > iTitleStart) then
  60. -- char = string.sub(sTitle,tx-iTitleStart,tx-iTitleStart)
  61. --end
  62. end
  63. else--normal line
  64. if (tx==0 or tx==w) then
  65. char="|"
  66. end
  67. end
  68. line=line..char
  69. end
  70. rawWriteLoc(x,y+ty,line)
  71. line=""
  72. end
  73. if (sTitle ~= "") then
  74. rawWriteLoc(x+iTitleStart+1,y+1,sTitle)
  75. end
  76. if (sText ~= "") then
  77. term.setCursorPos(x+1,y+3)
  78. local wrapedText = getWrapedText(x+1,y+2,x+w-2,y+h-1,sText)
  79. local offset = 0
  80. if (sTitle ~= "") then offset=2 end
  81. for ty=1,#wrapedText do
  82. rawWriteLoc(x+1,ty+y+offset,wrapedText[ty])
  83. if (ty>h-offset-2) then break end
  84. end
  85. end
  86. end
  87.  
  88. function getWrapedText(sx,sy,w,h, sText )
  89. local x,y = sx,sy
  90. local line = ""
  91. local wraped = { }
  92. local function newLine()
  93. table.insert(wraped,line)
  94. line=""
  95. x=sx
  96. end
  97.  
  98. local function storeText(text)
  99. line=line..text
  100. return #text
  101. end
  102.  
  103. -- Print the line with proper word wrapping
  104. while string.len(sText) > 0 do
  105. local whitespace = string.match( sText, "^[ \t]+" )
  106. if whitespace then
  107. -- Print whitespace
  108. x = x + storeText(whitespace)
  109. sText = string.sub( sText, string.len(whitespace) + 1 )
  110. end
  111.  
  112. local newline = string.match( sText, "^\n" )
  113. if newline then
  114. -- Print newlines
  115. newLine()
  116. sText = string.sub( sText, 2 )
  117. end
  118.  
  119. local text = string.match( sText, "^[^ \t\n]+" )
  120. if text then
  121. sText = string.sub( sText, string.len(text) + 1 )
  122. if string.len(text) > w then
  123. -- Print a multiline word
  124. while string.len( text ) > 0 do
  125. if x > w then
  126. newLine()
  127. end
  128. x = x + storeText( text )
  129. text = string.sub( text, (w-x) + 2 )
  130. end
  131. else
  132. -- Print a word normally
  133. if x + string.len(text) > w then
  134. newLine()
  135. end
  136. x = x + storeText( text )
  137. end
  138. end
  139. end
  140. newLine()
  141. return wraped
  142. end
  143.  
  144. function selectWindow(x,y,title,tOptions,iSelected)
  145. local w=#title
  146. local h=1
  147. local selected=iSelected or 1
  148. local tOptionsOrg=tCopy(tOptions)
  149. local width, height = term.getSize()
  150. for n=1,#tOptions do
  151. tOptions[n]=" "..tOptionsOrg[n].." "
  152. if (string.len(tOptions[n])>w) then w=string.len(tOptions[n]) end
  153. end
  154. w=w+2
  155. local sTitle = title or ""
  156. if (sTitle ~= "") then h = h + 2 end
  157. h = h + #tOptions
  158. tOptions[selected]="*"..tOptionsOrg[selected].."*"
  159. if (not x) then
  160. x=(width-w)/2
  161. end
  162. if (not y) then
  163. y=(height-h)/2
  164. end
  165. showWindow(x,y,w,h,sTitle,table.concat(tOptions,"\n"))
  166.  
  167. --loop and capture arrow keys for selecting output.
  168. bExit = false
  169. while not bExit do
  170. event, param = os.pullEvent()
  171. if event == "key" then
  172. if param == 197 then --Exit on pause/break
  173. bExit = true
  174. elseif param == 208 then
  175. tOptions[selected]=" "..tOptionsOrg[selected].." " --Clear selection
  176. selected=selected+1
  177. if (selected>#tOptions) then selected=1 end
  178. tOptions[selected]="*"..tOptionsOrg[selected].."*"
  179. showWindow(x,y,w,h,sTitle,table.concat(tOptions,"\n"))
  180. elseif param == 200 then
  181. tOptions[selected]=" "..tOptionsOrg[selected].." " --Clear selection
  182. selected=selected-1
  183. if (selected<1) then selected=#tOptions end
  184. tOptions[selected]="*"..tOptionsOrg[selected].."*"
  185. showWindow(x,y,w,h,sTitle,table.concat(tOptions,"\n"))
  186. elseif param == 28 then
  187. return selected,tOptionsOrg[selected]
  188. end
  189. end
  190.  
  191. end
  192. end
  193.  
  194. function textBox(x,y,len,size,focus,hideChar,default)
  195. local h=2
  196. local w=len+3
  197. local char=""
  198. local line=""
  199. if size==1 then
  200. y=y
  201. w=len
  202. for tx=0,w do
  203. rawWriteLoc(x+tx,y,"_")
  204. end
  205. elseif size==2 then
  206. for tx=0,w do
  207. rawWriteLoc(x+tx,y+2,"-")
  208. end
  209. else
  210. for ty=0,h do
  211. for tx=0,w do
  212. char=" "
  213. if (ty==0) or (ty==h) then --horizonal line
  214. if (tx==0 or tx==w) then
  215. char="+"
  216. else
  217. char="-"
  218. end
  219. elseif (ty==1) then
  220. if (tx==0 or tx==w) then
  221. char="|"
  222. end
  223. else--normal line
  224. if (tx==0 or tx==w) then
  225. char="|"
  226. end
  227. end
  228. line=line..char
  229. end
  230. rawWriteLoc(x,y+ty,line)
  231. line=""
  232. end
  233. end
  234. if (focus) then
  235. if size==2 then
  236. y=y+1
  237. elseif size==3 then
  238. x=x+2
  239. y=y+1
  240. end
  241. term.setCursorPos(x,y)
  242. term.setCursorBlink(true)
  243. sText=default or ""
  244. rawWriteLoc(x,y,sText)
  245. bExit = false
  246. local dispText=""
  247. local clearChar=" "
  248. if size==1 then clearChar="_" end
  249. while not bExit do
  250. local sEvent, param = os.pullEvent()
  251. if sEvent == "key" then
  252. if param == 28 then
  253. return sText,true
  254. elseif param==15 then
  255. return sText,false
  256. elseif param == 14 then
  257. sText = string.sub(sText,1,string.len(sText)-1)
  258. end
  259. end
  260. if sEvent == "char" then
  261. sText = sText .. param
  262. end
  263.  
  264. local dispChars=string.len(sText)
  265. if dispChars > len then dispChars=len end
  266. if not hideChar then
  267. dispText = string.sub(sText,-len)
  268. else
  269. dispText=""
  270. for n=1,dispChars do
  271. dispText=dispText.."*"
  272. end
  273. dispText = string.sub(dispText,-len)
  274. end
  275. dispChars=string.len(dispText)
  276. for n=dispChars,len do
  277. dispText=dispText..clearChar
  278. end
  279. rawWriteLoc(x,y,dispText)
  280. term.setCursorPos(x+dispChars,y)
  281. end
  282. end
  283. end
  284.  
  285. --if (globalsEnabled) then
  286. windows = {
  287. ["showWindow"] = function( ... )
  288. return showWindow( ... )
  289. end,
  290. ["selectWindow"] = function( ... )
  291. return selectWindow( ... )
  292. end,
  293. ["textBox"] = function( ... )
  294. return textBox( ... )
  295. end,
  296. ["clearArea"] = function( ... )
  297. return clearArea( ... )
  298. end,
  299. }
  300. --end
  301.  
  302. --[[Uncomment to see a demo
  303. term.clear()
  304. showWindow(3,3,40,10,"Testing!","\nUsername:\n\nPassword:")
  305. textBox(14,7,20,1,false,false,"xlilcasper")
  306. textBox(14,8,20,2,false,true,"")
  307.  
  308.  
  309. local enterPressed = false
  310. local username,password="",""
  311. while not enterPressed do
  312. username, enterPressed=textBox(14,7,20,1,true,false,"xlilcasper")
  313. password, enterPressed=textBox(14,8,20,2,true,true,"")
  314. end
  315. term.clear()
  316. term.setCursorPos(1,1)
  317. print(username..":"..password)
  318. local width, height = term.getSize()
  319. term.setCursorPos(1,height)
  320. --]]
  321.  
  322. --[[
  323. term.clear()
  324.  
  325. local iSelected, sText = windows.selectWindow(2,4,"Options",{"Pulse Time", "Side", "Activiate", "Exit"})
  326. print(sText)
  327. local width, height = term.getSize()
  328. term.setCursorPos(1,height)
  329. --]]
  330.  
  331. --[[
  332. local sText, focus=textBox(5,5,20,true,true,"Testing")
  333. term.clear()
  334. term.setCursorPos(1,1)
  335. print(sText)
  336. print("Enter "..tostring(focus))
  337. local width, height = term.getSize()
  338. term.setCursorPos(1,height)
  339. --]]
Add Comment
Please, Sign In to add comment