5bitesofcookies

GlassV0.3BETA2

Oct 18th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.09 KB | None | 0 0
  1. --[[
  2. If you're wanting to make an application for
  3. CCGlass, please read the guide on the forums.
  4. Every time a developer doesn't read the guide,
  5. an innocent computer crashes
  6.  
  7. Update information:
  8. ===
  9. v0.3.0
  10. Version Nickname: Crystal-Clear
  11. *Added Okay Glass feature, which works a lot
  12. ^like the real Glass.
  13. *Removed Garbage Collectors and defined
  14. ^variables as local when needed instead, as
  15. ^suggested by TheOriginalBit
  16. *Added mPrint(text,line,color) which is like
  17. ^tinyPrint but fits 10 lines
  18. *Added tinyCPrint(text,line,color) which is
  19. ^just like the normal tinyPrint but is
  20. ^centered. Also done with
  21. ^mCPrint(text,line,color)!
  22. *Improved how tinyPrint works
  23. *Fixed the PixelAPI to display pixels at the
  24. ^right coords.
  25. *Added drawLineX(startx, endx,y,color,trans)
  26. ^and drawLineY(starty,endy,x,color,trans)
  27. ^which basicly draw lines
  28. *Added sleepmode, which can be turned on by
  29. ^doing $$sleep on the home screen.
  30. *Added reboot, which can be done by doing
  31. ^$$reboot at the home screen
  32. *Added options, which includes an updater :D
  33. *Added Books. Books go in CCGBooks/ and need
  34. ^no sepcial format whatsoever. Just text.
  35. *Removed bookPrint(an empty function)
  36. *Added to the versioning system more
  37. *Removed Herobrine
  38.  
  39. ===
  40. v0.2.1: Fixes the glitch where the terminal
  41. would always need to be on the left side, no
  42. matter what
  43. Adds pixelClear(), which clears the pixels on
  44. the pixelAPI.
  45. Redid the versioning system to a more popular
  46. way of versioning applications.
  47. ===
  48. v0.2.0: Adds the Pixel API, aswell as the
  49. "Garbage Collectors" that set variables
  50. that are no longer needed at the ends of funct-
  51. ions to nil.
  52. ===
  53. v0.1.0
  54. Initial Release
  55.  
  56.  
  57.  
  58.  
  59. CREDITS:
  60. (MC IGN: Hithere__)Icanbreathecode - Making
  61. glass
  62. TheOriginalBIT for kindly making the RGB Color
  63. converter, and being awesome
  64. GravityScore for making LuaIDE, which has saved
  65. me a lot of time.
  66. The community, for helping me with figuring out
  67. a lot of stuff about Terminal Glasses.
  68. Dan200, for making computercraft
  69. Mikeemoo for making OpenPeripherals
  70. ===
  71. DEVMODE CONFIGURATION
  72. If you're just looking at the code, don't touch this
  73. Application makers, this is pointless for you too
  74. ]]
  75. devmode = true
  76. newuserdev = false
  77. on = true
  78. --load goroutines
  79. if not gouroutine then
  80. os.loadAPI("goroutine")
  81. end
  82. --first we set ye color values
  83. glcolors = {}
  84. glcolors.red = 0xff3333
  85. glcolors.blue = 0x7dd2e4
  86. glcolors.yellow = 0xffff4d
  87. glcolors.green = 0x4dff4d
  88. glcolors.gray = 0xe0e0e0
  89. glcolors.textGray = 0x676767
  90. glcolors.text = 0x5a5a5
  91. glcolors.otherBlue = 0x2e679f
  92. --colors not stolen from Plastic
  93. --:p
  94. glcolors.black = 0x000000
  95. glcolors.purple = 0xff00ff
  96. glcolors.blueGreen = 0x00ffff
  97. glcolors.orange = 0xff6600
  98. glcolors.darkBlue = 0x000066
  99. glcolors.brown = 0x663300
  100. glcolors.otherGreen = 0x00ff00
  101. glcolors.lightOrange = 0xffcc00
  102. glcolors.white = 0xffffff
  103. --second, we ensure the user isn't wasting an advanced cpu
  104. if term.isColor() == true and devmode == false then
  105. print("Error: You are on an advanced computer. This program will not allow other programs to be ran. Do you wish to continue anyway?(y/n, caps count)")
  106. local answer = read()
  107. if answer == "n" then
  108. error("User opted out")
  109. end
  110. end
  111. function getSide()
  112. tran = fs.open("glasstrans", "w")
  113. tran.writeLine("0.75")
  114. tran.close()
  115. print("Which side is the Terminal Glasses Bridge on?(top bottom left right back, caps count) ")
  116. side = read()
  117. sw = fs.open("glassside", "w")
  118. sw.writeLine(side)
  119. sw.close()
  120. print("What is your desired username?")
  121. username = read()
  122. sw = fs.open("username", "w")
  123. sw.writeLine(username)
  124. sw.close()
  125. print("Now, if you havn't already, take the terminal glasses you're going to use and right-click the terminal bridge.")
  126. end
  127. function loadPixelAPI()
  128. print("Loading PixelAPI.")
  129. pixelsOne = {}
  130. pixelsTwo = {}
  131. pixelsThree = {}
  132. pixelsFour = {}
  133. pixelsFive = {}
  134. pixelsSix = {}
  135. pixelsSeven = {}
  136. pixelsEight = {}
  137. pixelsNine = {}
  138. pixelsTen = {}
  139. local i
  140. for i = 1,30 do
  141. vI = i-1
  142. vI = vI * 5
  143. vI = vI + 10
  144. pixelsOne[i] = glass.addBox(vI, 5, 5, 5, glcolors.red, 0)
  145. pixelsOne[i].setZIndex(4)
  146. end
  147. for i = 1,30 do
  148. vI = i-1
  149. vI = vI * 5
  150. vI = vI + 10
  151. pixelsTwo[i] = glass.addBox(vI, 10, 5, 5, glcolors.red, 0)
  152. pixelsTwo[i].setZIndex(4)
  153. end
  154. for i = 1,30 do
  155. vI = i-1
  156. vI = vI * 5
  157. vI = vI + 10
  158. pixelsThree[i] = glass.addBox(vI, 15, 5, 5, glcolors.red, 0)
  159. pixelsThree[i].setZIndex(4)
  160. end
  161. for i = 1,30 do
  162. vI = i-1
  163. vI = vI * 5
  164. vI = vI + 10
  165. pixelsFour[i] = glass.addBox(vI, 20, 5, 5, glcolors.red, 0)
  166. pixelsFour[i].setZIndex(4)
  167. end
  168. for i = 1,30 do
  169. vI = i-1
  170. vI = vI * 5
  171. vI = vI + 10
  172. pixelsFive[i] = glass.addBox(vI, 25, 5, 5, glcolors.red, 0)
  173. pixelsFive[i].setZIndex(4)
  174. end
  175. for i = 1,30 do
  176. vI = i-1
  177. vI = vI * 5
  178. vI = vI + 10
  179. pixelsSix[i] = glass.addBox(vI, 30, 5, 5, glcolors.red, 0)
  180. pixelsSix[i].setZIndex(4)
  181. end
  182. for i = 1,30 do
  183. vI = i-1
  184. vI = vI * 5
  185. vI = vI + 10
  186. pixelsSeven[i] = glass.addBox(vI, 35, 5, 5, glcolors.red, 0)
  187. pixelsSeven[i].setZIndex(4)
  188. end
  189. for i = 1,30 do
  190. vI = i-1
  191. vI = vI * 5
  192. vI = vI + 10
  193. pixelsEight[i] = glass.addBox(vI, 40, 5, 5, glcolors.red, 0)
  194. pixelsEight[i].setZIndex(4)
  195. end
  196. for i = 1,30 do
  197. vI = i-1
  198. vI = vI * 5
  199. vI = vI + 10
  200. pixelsNine[i] = glass.addBox(vI, 45, 5, 5, glcolors.red, 0)
  201. pixelsNine[i].setZIndex(4)
  202. end
  203. for i = 1,30 do
  204. vI = i-1
  205. vI = vI * 5
  206. vI = vI + 10
  207. pixelsTen[i] = glass.addBox(vI, 50, 5, 5, glcolors.red, 0)
  208. pixelsTen[i].setZIndex(4)
  209. end
  210. end
  211. function drawPixel(x, y, color, trans)
  212. --example usage: 2, 4, colors.red, 1
  213. --this would make a red pixel appear at 2, 4.
  214. if y ==1 then
  215. pixelsOne[x].setColor(color)
  216. pixelsOne[x].setOpacity(trans)
  217. elseif y ==2 then
  218. pixelsTwo[x].setColor(color)
  219. pixelsTwo[x].setOpacity(trans)
  220. elseif y ==3 then
  221. pixelsThree[x].setColor(color)
  222. pixelsThree[x].setOpacity(trans)
  223. elseif y ==4 then
  224. pixelsFour[x].setColor(color)
  225. pixelsFour[x].setOpacity(trans)
  226. elseif y ==5 then
  227. pixelsFive[x].setColor(color)
  228. pixelsFive[x].setOpacity(trans)
  229. elseif y ==6 then
  230. pixelsSix[x].setColor(color)
  231. pixelsSix[x].setOpacity(trans)
  232. elseif y ==7 then
  233. pixelsSeven[x].setColor(color)
  234. pixelsSeven[x].setOpacity(trans)
  235. elseif y ==8 then
  236. pixelsEight[x].setColor(color)
  237. pixelsEight[x].setOpacity(trans)
  238. elseif y ==9 then
  239. pixelsNine[x].setColor(color)
  240. pixelsNine[x].setOpacity(trans)
  241. elseif y ==10 then
  242. pixelsTen[x].setColor(color)
  243. pixelsTen[x].setOpacity(trans)
  244. end
  245. end
  246. function pixelClear()
  247. for i = 1,30 do
  248. drawPixel(i,1,glcolors.red,0)
  249. end
  250. for i = 1,30 do
  251. drawPixel(i,2,glcolors.red,0)
  252. end
  253. for i = 1,30 do
  254. drawPixel(i,3,glcolors.red,0)
  255. end
  256. for i = 1,30 do
  257. drawPixel(i,4,glcolors.red,0)
  258. end
  259. for i = 1,30 do
  260. drawPixel(i,5,glcolors.red,0)
  261. end
  262. for i = 1,30 do
  263. drawPixel(i,6,glcolors.red,0)
  264. end
  265. for i = 1,30 do
  266. drawPixel(i,7,glcolors.red,0)
  267. end
  268. for i = 1,30 do
  269. drawPixel(i,8,glcolors.red,0)
  270. end
  271. for i = 1,30 do
  272. drawPixel(i,9,glcolors.red,0)
  273. end
  274. for i = 1,30 do
  275. drawPixel(i,10,glcolors.red,0)
  276. end
  277. end
  278. function drawLineX(startx,endx,y,colors,transpa)
  279. local i
  280. for i=startx,endx do
  281. drawPixel(i,y,colors,transpa)
  282. -- print("drew pixel at x "..i.." y "..y.." trans "..transpa.." ")
  283. end
  284. end
  285. function drawLineY(x,starty,endy,colory,transpar)
  286. local i
  287. for i=starty,endy do
  288. drawPixel(x,i,colory,transpar)
  289. end
  290. end
  291. function load()
  292. dir = "CCGlass/"
  293. glassVersion = "0.31"
  294. glassVer = 31
  295. --note that this still runs on first time
  296. on = true
  297. gs = fs.open("glassside", "r")
  298. side = gs.readLine()
  299. gs.close()
  300. glass = peripheral.wrap(side)
  301. gs = fs.open("username", "r")
  302. username = gs.readLine()
  303. gs.close()
  304. tran = fs.open("glasstrans", "r")
  305. transp = tran.readLine()
  306. tran.close()
  307. transp = tonumber(transp)
  308. shell.run('glassTheme')
  309. end
  310. --Ye first thing where we draw stuff
  311. --(besides the PixelAPI)
  312. function firstDraw()
  313. tinyText = {}
  314. local tinyY
  315. local i
  316. for i = 0,4 do
  317. tinyY = i *10
  318. tinyY = tinyY + 5
  319. i = i+1
  320. tinyText[i]= glass.addText(10, tinyY, " ", glcolors.red)
  321. tinyText[i].setZIndex(5)
  322. end
  323. miniPrint = {}
  324. for i = 0,9 do
  325. local yset = i * 5
  326. yset = yset + 5
  327. i = i+1
  328. miniPrint[i] = glass.addText(10,yset," ",glcolors.red)
  329. miniPrint[i].setZIndex(5)
  330. miniPrint[i].setScale(0.5)
  331. end
  332. midText= glass.addText(29, 27.9, " ", glcolors.green)
  333. highText= glass.addText(29, 27.9, " ", glcolors.red)
  334. lowText= glass.addText(29, 27.9, " ", glcolors.otherBlue)
  335. leftBox = glass.addBox(5, 0, 5, 60, sideColors, transp)
  336. bigBox = glass.addBox(10, 5, 150, 50, bigBoxColor, transp)
  337. --note that the width really is 150
  338. --add 10 to x cords, 5 to y cords
  339. for i = 1, 20 do
  340. bigBox.setWidth(i * 7.5)
  341. sleep(0.01)
  342. end
  343. leftBox = glass.addBox(160, 0, 5, 60, sideColors, transp)
  344. end
  345. --The way glass prints stuffs to the screen
  346. function midPrint(text)
  347. local w
  348. local tw
  349. local posx
  350. local posy
  351. midText.setText(text)
  352. midText.setScale(2)
  353. midText.setZIndex(5)
  354. w = bigBox.getWidth()
  355. tw = midText.getWidth()
  356. tw = tw*2
  357. posx = math.floor(w-tw)/2, 2
  358. posx = posx +10
  359. midText.setX(posx)
  360. posy = 20
  361. midText.setY(posy)
  362. end
  363. function highPrint(text)
  364. local w
  365. local hposx
  366. local posy
  367. highText.setZIndex(5)
  368. local w = bigBox.getWidth() --=150
  369. highText.setText(text)
  370. tw = highText.getWidth()
  371. hposx = math.floor(w-tw)/2, 2
  372. hposx = hposx+10
  373. highText.setX(hposx)
  374. posy = 10
  375. highText.setY(posy)
  376. end
  377. function lowPrint(text)
  378. local w
  379. local tw
  380. local posx
  381. local w = bigBox.getWidth()
  382. lowText.setText(text)
  383. lowText.setZIndex(5)
  384. tw = lowText.getWidth()
  385. posx = math.floor(w-tw)/2, 2
  386. posx = posx +10
  387. lowText.setX(posx)
  388. lowText.setY(40)
  389. end
  390. function mPrint(text,line,color)
  391. if line < 11 then
  392. miniPrint[line].setText(text)
  393. miniPrint[line].setColor(color)
  394. miniPrint[line].setX(10)
  395. local correctly = true
  396. else
  397. local correctly = false
  398. end
  399. return correctly
  400. end
  401. --now we define print functions for books and such
  402. function tinyPrint(text,line,color)
  403. if line < 6 then
  404. tinyText[line].setText(text)
  405. tinyText[line].setColor(color)
  406. tinyText[line].setX(10)
  407. --^^ to counteract tinyCPrint
  408. local correctly = true
  409. else
  410. local correctly = false
  411. end
  412. return correctly
  413. end
  414. function tinyCPrint(text,line,color)
  415. if line <6 then
  416. local w = bigBox.getWidth()
  417. tinyText[line].setText(text)
  418. tw = tinyText[line].getWidth()
  419. tX = math.floor(w-tw)/2, 2
  420. tX = tX + 10 --take in account for the offset
  421. tinyText[line].setX(tX)
  422. tinyText[line].setColor(color)
  423. local correctly = true
  424. else
  425. local correctly = false
  426. end
  427. return correctly --I sure hope it does. :3
  428. end
  429. function splitUp(text)
  430. repeat
  431. endSplit = endSplit+1
  432. --^^increase where it splits each time,
  433. --done in characters
  434. cLeft = text:sub(endSplit+1, #text)
  435. --^^ set the leftover text to the rest of
  436. --the line
  437. cText = text:sub(1,endSplit)
  438. --^^cut off the extra text
  439. until glass.getStringWidth(cText) < 151
  440. --^^ keep trying till it gets it under 151
  441. return cText, cLeft
  442. --^^ return the variables
  443. end
  444. function getTinyPrint(line)
  445. if line <6 then
  446. return tinyText[line].getText()--, tinyText[line].getColor()
  447. else
  448. local text = "Line must be <6."
  449. return text
  450. end
  451. end
  452.  
  453. function pushTinyPrint(text,color)
  454. local sl, sc = getTinyPrint(2)
  455. local tl, tc = getTinyPrint(3)
  456. local fl, fc = getTinyPrint(4)
  457. local fil, fic = getTinyPrint(5)
  458. tinyPrint(text,5,color)
  459. tinyPrint(fil,4,glcolors.red)
  460. tinyPrint(fl,3,glcolors.red)
  461. tinyPrint(tl,2,glcolors.red)
  462. tinyPrint(sl,1,glcolors.red)
  463. end
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473. function mCPrint(text,line,color)
  474. if line <11 then
  475. local w = bigBox.getWidth()
  476. miniText[line].setText(text)
  477. tw = miniText[line].getWidth()
  478. tX = math.floor(w-tw)/2, 2
  479. tX = tX +10 --take in account for the offset
  480. miniText[line].setX(tX)
  481. miniText[line].setColor(color)
  482. local correctly = true
  483. else
  484. local correctly = false
  485. end
  486. return correctly
  487. end
  488.  
  489. function tinyClear()
  490. local i
  491. for i=1,5 do
  492. tinyPrint(" ", i, glcolors.red)
  493. end
  494. end
  495. function bigClear()
  496. highPrint(" ")
  497. midPrint(" ")
  498. lowPrint(" ")
  499. end
  500. function mClear()
  501. local i
  502. for i= 1,10 do
  503. mPrint(" ",i,glcolors.red)
  504. end
  505. end
  506. function clearAll()
  507. bigClear()
  508. tinyClear()
  509. mClear()
  510. if loaded == nil then
  511. pixelClear()
  512. end
  513. end
  514. function sleepMode()
  515. glass.clear()
  516. local sleeped = true
  517. while sleeped == true do
  518. local evt, cmd = os.pullEvent("chat_command")
  519. cmd = string.lower(cmd)
  520. if cmd == "wake" then
  521. sleeped = false
  522. reboot()
  523. end
  524. end
  525. end
  526. --now we launch apps based on what the user inputs
  527. function homeScreenLaunch()
  528. while true do
  529. time = true
  530. mClear()
  531. tinyClear()
  532. pixelClear()
  533. local evt, cmd = os.pullEvent("chat_command")
  534. cmd = string.lower(cmd)
  535. if cmd == "apps" then
  536. time = false
  537. launchApps()
  538. elseif cmd == "shutdown" then
  539. time = false
  540. bigClear()
  541. midPrint("Goodbye.")
  542. sleep(1)
  543. goodShutdown = true
  544. on = false
  545. elseif cmd == "help" then
  546. time = false
  547. shell.run('CCGlass/ccghelp')
  548. elseif cmd == "reboot" then
  549. time = false
  550. bigClear()
  551. midPrint("Rebooting.")
  552. sleep(2)
  553. reboot()
  554. --we can call reboot directly because it
  555. --is not the parrent goroutine, it's
  556. --simply a function that loads Glass
  557. elseif cmd == "options" then
  558. time = false
  559. shell.run('CCGlass/options')
  560. elseif cmd == "omg" then
  561. highPrint("What happened?")
  562. sleep(2)
  563. reprintHome()
  564. elseif cmd == "ping" then
  565. highPrint("Pong!")
  566. sleep(2)
  567. reprintHome()
  568. elseif cmd == "sleep" then
  569. time = false
  570. bigClear()
  571. highPrint("Do $$wake to wake.")
  572. sleep(2)
  573. sleepMode()
  574. elseif cmd == "crash" then
  575. bigBox.delete()
  576. on = false
  577. error("lol")
  578. else
  579. reprintHome()
  580. end
  581. reprintHome()
  582. end
  583. end
  584. --now we launch 3rd party and default apps
  585. function launchApps()
  586. tinyClear()
  587. tinyPrint("test :3",1,glcolors.red)
  588. highPrint("Launcher v1")
  589. midPrint(" ")
  590. lowPrint("Usage: $$appname")
  591. local evt, cmd = os.pullEvent("chat_command")
  592. cmd = string.lower(cmd)
  593. if cmd == "exit" then
  594. else
  595. shell.run("CCGlass/"..cmd.." ")
  596. we = fs.open("glassErrors", "w")
  597. we.writeLine(error)
  598. we.close()
  599. reprintHome()
  600. end
  601. end
  602. --now we can reprint the homescreen
  603. function reprintHome()
  604. clearAll()
  605. highPrint("Welcome, "..username.."!")
  606. midPrint(textutils.formatTime(os.time(),false))
  607. lowPrint("Do $$help for help.")
  608. end
  609. --firsttime?
  610. if fs.exists("glassside") == false or newuserdev == true then
  611. getSide()
  612. end
  613. --this function was kindly created by TheOriginalBIT
  614. --who is also awesome.
  615. function rgbColor( _r, _g, _b )
  616. return bit.bor( bit.blshift( bit.bor( bit.blshift( _r, 8 ), _g ), 8 ), _b )
  617. end
  618. --[[begin execution]]
  619. loaded = false
  620. function reboot()
  621. load()
  622. glass.clear()
  623. firstDraw()
  624. reprintHome()
  625. if loaded == false then
  626. loadPixelAPI()
  627. goroutine.run(parrent)
  628. end
  629. end
  630. --[[while on == true do
  631. homeScreenLaunch()
  632. end]]
  633. function getTime()
  634. while true do
  635. if time then
  636. midPrint(textutils.formatTime(os.time(),false))
  637. end
  638. sleep(0.32)
  639. end
  640. end
  641. function parrent()
  642. --new in v0.3BETA2
  643. --free up memory & prevent from reloading
  644. loaded = nil
  645. goroutine.spawn("TimeDisplay",getTime)
  646. goroutine.spawn("cmdManage",homeScreenLaunch)
  647. while on do sleep(0) end
  648. end
Advertisement
Add Comment
Please, Sign In to add comment