Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local App = app.new(...)
- local function checkBB(x1,y1,x2,y2,x,y)
- return x >= x1 and x < x2 and y >= y1 and y < y2
- end
- App.Apps = {}
- App.Installed = app.list()
- App.Offset = {0,0}
- App.Buttons = {}
- App.Server = rednet.lookup("appstore", "server")
- function App:addButton(x,y,w,h,func)
- table.insert(self.Buttons, {x,y,w,h,func})
- end
- function App:handleMessage(sender, res, protocol)
- if protocol ~= "appstore" then return
- elseif res.Type == "apps" then
- for code,manifest in pairs(res.Data) do
- self:registerApp(code, manifest)
- end
- elseif res.Type == "app" then self:registerApp(res.Data.Code, res.Data.Manifest)
- end
- end
- function App:registerApp(code, manifest)
- local icon
- if manifest.Files[".nfp"] then
- self:delete("temp.nfp")
- self:download(manifest.Files[".nfp"].Code, "temp.nfp")
- icon = paintutils.loadImage(App:resolve("temp.nfp"))
- else
- icon = paintutils.loadImage("/missing.nfp")
- end
- table.insert(self.Apps, {Code = code, Name = manifest.Name, Icon = icon})
- local index = #self.Apps
- local w,h = term.getSize()
- self:addButton(w,(index - 1) * 3 + 1,1,1,function()
- app[not self.Installed[code] and "install" or "uninstall"](code)
- end)
- end
- function App:updateApps()
- self.Installed = app.list()
- end
- function App:handleClick(button, x, y)
- x,y = x - self.Offset[1],y - self.Offset[2]
- for i,btn in ipairs(self.Buttons) do
- if checkBB(btn[1],btn[2],btn[1]+btn[3],btn[2]+btn[4],x,y) then
- btn[5]()
- break
- end
- end
- end
- function App:handleScroll(direction, x, y)
- App.Offset[2] = App.Offset[2] - direction
- end
- function App:draw()
- local w,h = term.getSize()
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- for i,data in ipairs(self.Apps) do
- local x1,y1 = 1 + self.Offset[1],1 + 3 * (i - 1) + self.Offset[2]
- local x2,y2 = x1 + w - 1, y1 + 2
- local btn = not self.Installed[data.Code] and {"O","5"} or {"X","e"}
- paintutils.drawFilledBox(x1, y1, x2, y2, colors.lightGray)
- paintutils.drawImage(data.Icon, x1, y1)
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.lightGray)
- term.setCursorPos(x2, y1)
- term.blit(btn[1], "f", btn[2])
- term.setCursorPos(x1 + 3, y1)
- term.write(data.Name)
- end
- end
- if App.Server then
- App.EventHandler:connect("update_apps", function(...) App:updateApps(...) end)
- App.EventHandler:connect("mouse_scroll", function(...) App:handleScroll(...) end)
- App.EventHandler:connect("mouse_click", function(...) App:handleClick(...) end)
- App.EventHandler:connect("rednet_message", function(...) App:handleMessage(...) end)
- rednet.send(App.Server, { Protocol="get" }, "appstore")
- App:start()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement