Advertisement
RodrickLord

iWolf

Apr 17th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.24 KB | None | 0 0
  1. --[[ iWolf
  2.   Lua Browser ]]
  3.  
  4. browserID = "wolf"
  5.  
  6. if not term.isColor() then
  7.   error("iWolf needs a Advanced Computer/PDA",0)
  8. end
  9.  
  10. local modemSide
  11. for _,v in pairs(peripheral.getNames()) do
  12.   if peripheral.getType(v) == "modem" and peripheral.call(v,"isWireless") == true then
  13.     modemSide = v
  14.     break
  15.   end
  16. end
  17. if modemSide then
  18.   rednet.open(modemSide)
  19. else
  20.   error("iWolf needs a Wireless Modem!",0 )
  21. end
  22.  
  23. local logo = {
  24.   {{1,2,1,512,32768,1,1,1,1,512,32768,1,1,1,1,1,512,32768,1,1,512,512,32768,},},
  25.   {{1,1,1,512,32768,1,1,1,1,512,32768,1,1,1,1,1,512,32768,1,512,32768,1,1},},
  26.   {{1,2,1,512,32768,1,1,1,1,512,32768,1,512,512,32768,1,512,32768,512,512,512,512,32768,},},
  27.   {{1,2,1,512,32768,1,512,32768,1,512,32768,512,32768,1,512,32768,512,32768,1,512,32768,},},
  28.   {{1,2,1,512,32768,512,32768,512,32768,512,32768,512,32768,1,512,32768,512,32768,1,512,32768,},},
  29.   {{1,2,1,512,512,32768,1,1,512,512,32768,1,512,512,32768,1,1,512,32768,512,32768,},},
  30. }
  31.  
  32. local termX,termY = term.getSize()
  33. local centerX,centerY = termX/2+1,termY/2+1
  34.  
  35. function cWrite(text,y)
  36.   local _,cy = term.getCursorPos()
  37.   term.setCursorPos(centerX-#text/2,y or cy)
  38.   write(text.."\n")
  39. end
  40.  
  41. function rWrite(text,y)
  42.   local _,cy = term.getCursorPos()
  43.   term.setCursorPos(termX-#text,y or cy)
  44.   write(text)
  45. end
  46.  
  47. local function wait(t)
  48.   parallel.waitForAny(
  49.   function() sleep(t or 3) end,
  50.   function() os.pullEvent("key") end,
  51.   function() os.pullEvent("mouse_click") end)
  52. end
  53.  
  54. local function fadeIn()
  55.   term.setBackgroundColor(colors.black)
  56.   term.clear() sleep(.1)
  57.   term.setBackgroundColor(colors.gray)
  58.   term.clear() sleep(.1)
  59.   term.setBackgroundColor(colors.lightGray)
  60.   term.clear() sleep(.1)
  61.   term.setBackgroundColor(colors.white)
  62.   term.clear() sleep(.1)
  63. end
  64.  
  65. local function fadeOut()
  66.   term.setBackgroundColor(colors.white)
  67.   term.clear() sleep(.1)
  68.   term.setBackgroundColor(colors.lightGray)
  69.   term.clear() sleep(.1)
  70.   term.setBackgroundColor(colors.gray)
  71.   term.clear() sleep(.1)
  72.   term.setBackgroundColor(colors.black)
  73.   term.clear() sleep(.1)
  74. end
  75.  
  76. local settings
  77. local function getSettings()
  78.   if fs.exists(".wolfSettings") then
  79.     local settingsF = fs.open(".wolfSettings","r")
  80.     settings = textutils.unserialize(settingsF.readAll())
  81.   else
  82.     settings = {home="wolf/home"}
  83.     local settingsF = fs.open(".wolfSettings","w")
  84.     settingsF.write(textutils.serialize(settings))
  85.   end
  86.   settingsF.close()
  87. end
  88.  
  89. local function welcomeScreen()
  90.   fadeIn()
  91.   local startX = centerY-5
  92.   for i = 1,#logo do
  93.     sleep(.1)
  94.     paintutils.drawImage(logo[i],centerX-12,startX-1+i)
  95.   end
  96.   term.setBackgroundColor(colors.white)
  97.   term.setTextColor(colors.black)
  98.   for i = termX,centerX-5,-3 do
  99.     term.setCursorPos(i,centerY+2)
  100.     term.write("Web Browser   ")
  101.     sleep(.01)
  102.   end
  103.   term.setCursorPos(centerX-5,centerY+2)
  104.   term.write("Web Browser   ")
  105.   wait(3)
  106.   fadeOut()
  107. end
  108.  
  109. --# Local Sites #--
  110. local wolfPages = {
  111.   home = [[
  112.     termX,termY = term.getSize()
  113.     cx,cy = termX/2+1,termY/2+1
  114.     term.setBackgroundColor(colors.gray) term.clear()
  115.     term.setTextColor(colors.white)
  116.     cWrite("iWolf Homepage",1)
  117.     paintutils.drawFilledBox(1,3,cx+5,9,colors.blue)
  118.     term.setTextColor(colors.white)
  119.     term.setCursorPos(1,4)
  120.     write("  With iWolf, you can access\n Lua based websites, in your")
  121.     write("\n Rednet network OR in REAL\n WebServers!")
  122.   ]],
  123.   notFound = [[
  124.     termX,termY = term.getSize()
  125.     cx,cy = termX/2+1,termY/2+1
  126.     term.setBackgroundColor(colors.white) term.clear()
  127.     paintutils.drawFilledBox(10,2,termX-9,4,colors.red)
  128.     term.setTextColor(colors.white)
  129.     cWrite("This website isn't available",3)
  130.     term.setTextColor(colors.black)
  131.     term.setBackgroundColor(colors.white)
  132.     cWrite("Sorry, but I couldn't connect")
  133.     cWrite("to the requested page.")
  134.     cWrite("Are you sure the server is online,")
  135.     cWrite("or it's in range?")
  136.   ]],
  137.   settings = [[
  138.     term.setBackgroundColor(colors.white) term.clear()
  139.     term.setCursorPos
  140.   ]],
  141. }
  142.  
  143. --# Browser Core #--
  144.  
  145. -- Windows
  146. local wSite
  147. local wUrl
  148. local cTerm = term.current()
  149.  
  150. function goto(url)
  151.   local loadedPage = loadstring(wolfPages["notFound"])
  152.   domain = url:match("^(.-)/")
  153.   page = url:match("/(.+)$")
  154.   if domain == "wolf" then
  155.     for k,v in pairs(wolfPages) do
  156.       if k == page then
  157.         loadedPage = loadstring(v)
  158.       end
  159.     end
  160.   else
  161.     rednet.broadcast(page,domain)
  162.    
  163.   end
  164.   setfenv(loadedPage, getfenv())
  165.   term.redirect(wSite)
  166.   loadedPage()
  167.   term.redirect(cTerm)
  168. end
  169.  
  170. local function enterUrl()
  171.   term.redirect(wUrl)
  172.   term.clear()
  173.   term.setCursorPos(1,1)
  174.   local url = read()
  175.   goto(url)
  176.   if term.current() ~= wUrl then term.redirect(wUrl) end
  177.   term.setCursorPos(1,1)
  178.   if #url>38 then
  179.     write(url:sub(1,38))
  180.     write "..."
  181.   else
  182.     write(url)
  183.   end
  184.   term.redirect(cTerm)
  185. end
  186.  
  187. local guiReady
  188.  
  189. local function createGui()
  190.   fadeIn()
  191.   paintutils.drawFilledBox(1,1,termX,2,colors.gray)
  192.   term.setTextColor(colors.white)
  193.   term.setCursorPos(1,1)
  194.   write "iWolf"
  195.   term.setCursorPos(termX,1)
  196.   write "X"
  197.  
  198.   paintutils.drawFilledBox(7,1,termX-2,1,colors.lightGray)
  199.   wUrl = window.create(term.current(),8,1,termX-10,1,true)
  200.   wUrl.setBackgroundColor(colors.lightGray)
  201.   wUrl.clear()
  202.  
  203.   wSite = window.create(term.current(),1,3,termX,termY-2,true)
  204.   wSite.setBackgroundColor(colors.white)
  205.   wSite.clear()
  206.  
  207.   guiReady = true
  208. end
  209.  
  210. function events()
  211.   while true do
  212.     local event = {os.pullEventRaw()}
  213.     if event[1] == "mouse_click" and guiReady then
  214.       x, y = event[3], event[4]
  215.       if x>=7 and x<=termX-2 and y==1 then
  216.         enterUrl()
  217.       elseif x==termX and y==1 then
  218.         os.queueEvent("terminate")
  219.       end
  220.     elseif event[1] == "terminate" then
  221.       if term.current() ~= cTerm then term.redirect(cTerm) end
  222.       term.setBackgroundColor(colors.black)
  223.       term.clear()
  224.       term.setTextColor(colors.white)
  225.       cWrite("Thanks for using iWolf!",1)
  226.       error("\n",0)
  227.     end
  228.   end
  229. end
  230.  
  231. local args = {...}
  232.  
  233. local function start()
  234.   if args[1] == "q" or args[1] == "quick" then
  235.     createGui()
  236.   else
  237.     welcomeScreen()
  238.     createGui()
  239.   end
  240.   goto("wolf/home")
  241. end
  242.  
  243. parallel.waitForAll(events, start)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement