Advertisement
FFGFlash

.main

Sep 16th, 2021 (edited)
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.75 KB | None | 0 0
  1. local App = app.new(...)
  2.  
  3. local function checkBB(x1,y1,x2,y2,x,y)
  4.   return x >= x1 and x < x2 and y >= y1 and y < y2
  5. end
  6.  
  7. App.Apps = {}
  8. App.Installed = app.list()
  9. App.Offset = {0,0}
  10. App.Buttons = {}
  11. App.Server = rednet.lookup("appstore", "server")
  12.  
  13. function App:addButton(x,y,w,h,func)
  14.   table.insert(self.Buttons, {x,y,w,h,func})
  15. end
  16.  
  17. function App:handleMessage(sender, res, protocol)
  18.   if protocol ~= "appstore" then return
  19.   elseif res.Type == "apps" then
  20.     for code,manifest in pairs(res.Data) do
  21.       self:registerApp(code, manifest)
  22.     end
  23.   elseif res.Type == "app" then self:registerApp(res.Data.Code, res.Data.Manifest)
  24.   end
  25. end
  26.  
  27. function App:registerApp(code, manifest)
  28.   local icon
  29.   if manifest.Files[".nfp"] then
  30.     self:delete("temp.nfp")
  31.     self:download(manifest.Files[".nfp"].Code, "temp.nfp")
  32.     icon = paintutils.loadImage(App:resolve("temp.nfp"))
  33.   else
  34.     icon = paintutils.loadImage("/missing.nfp")
  35.   end
  36.   table.insert(self.Apps, {Code = code, Name = manifest.Name, Icon = icon})
  37.   local index = #self.Apps
  38.   local w,h = term.getSize()
  39.   self:addButton(w,(index - 1) * 3 + 1,1,1,function()
  40.     app[not self.Installed[code] and "install" or "uninstall"](code)
  41.   end)
  42. end
  43.  
  44. function App:updateApps()
  45.   self.Installed = app.list()
  46. end
  47.  
  48. function App:handleClick(button, x, y)
  49.   x,y = x - self.Offset[1],y - self.Offset[2]
  50.   for i,btn in ipairs(self.Buttons) do
  51.     if checkBB(btn[1],btn[2],btn[1]+btn[3],btn[2]+btn[4],x,y) then
  52.       btn[5]()
  53.       break
  54.     end
  55.   end
  56. end
  57.  
  58. function App:handleScroll(direction, x, y)
  59.   App.Offset[2] = App.Offset[2] - direction
  60. end
  61.  
  62. function App:draw()
  63.   local w,h = term.getSize()
  64.   term.setBackgroundColor(colors.gray)
  65.   term.setTextColor(colors.white)
  66.   term.clear()
  67.   term.setCursorPos(1,1)
  68.   for i,data in ipairs(self.Apps) do
  69.     local x1,y1 = 1 + self.Offset[1],1 + 3 * (i - 1) + self.Offset[2]
  70.     local x2,y2 = x1 + w - 1, y1 + 2
  71.     local btn = not self.Installed[data.Code] and {"O","5"} or {"X","e"}
  72.     paintutils.drawFilledBox(x1, y1, x2, y2, colors.lightGray)
  73.     paintutils.drawImage(data.Icon, x1, y1)
  74.     term.setTextColor(colors.black)
  75.     term.setBackgroundColor(colors.lightGray)
  76.     term.setCursorPos(x2, y1)
  77.     term.blit(btn[1], "f", btn[2])
  78.     term.setCursorPos(x1 + 3, y1)
  79.     term.write(data.Name)
  80.   end
  81. end
  82.  
  83. if App.Server then
  84.   App.EventHandler:connect("update_apps", function(...) App:updateApps(...) end)
  85.   App.EventHandler:connect("mouse_scroll", function(...) App:handleScroll(...) end)
  86.   App.EventHandler:connect("mouse_click", function(...) App:handleClick(...) end)
  87.   App.EventHandler:connect("rednet_message", function(...) App:handleMessage(...) end)
  88.   rednet.send(App.Server, { Protocol="get" }, "appstore")
  89.   App:start()
  90. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement