FFGFlash

apps/nekos/helpers/menu.lua

Sep 29th, 2021 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.99 KB | None | 0 0
  1. return function(a,u)
  2.   local helper = { App = a, Conns = {}, User = u }
  3.   function helper:connect(e,c,t) self.Conns[#self.Conns+1]=self.App:connect(e,c,t or self) end
  4.   function helper:destroy() for i,conn in ipairs(self.Conns) do self.App:disconnect(conn) end end
  5.   function helper:build()
  6.     self.Dragging = false
  7.     self.Selection = nil
  8.     self.Buttons = {}
  9.     self:connect("mouse_click",self.handleMousePressed)
  10.     self:connect("mouse_up",self.handleMouseReleased)
  11.     self:connect("mouse_drag",self.handleMouseDragged)
  12.     self:connect("term_resize",self.handleResize)
  13.     self:connect("terminate", self.handleTerminate)
  14.     self:handleResize()
  15.   end
  16.  
  17.   function helper:handleTerminate()
  18.     self.App:activate("login")
  19.   end
  20.  
  21.   function helper:draw()
  22.     term.setTextColor(system:getTextColor())
  23.     term.setBackgroundColor(system:getBackgroundColor())
  24.     term.clear()
  25.     term.setCursorPos(1,1)
  26.  
  27.     local cx,cy = term.getCursorPos()
  28.     local tc = term.getTextColor()
  29.     local tb = term.getBackgroundColor()
  30.  
  31.     for code,app in pairs(app.Apps.Data) do
  32.       if not app[4] then
  33.         local icon = "/nekOS/Apps/"..code.."/icon.nfp"
  34.         local image = paintutils.loadImage(fs.exists(icon) and icon or system:getMissingIcon())
  35.         local x,y = math.min(math.max(app[1],1),self.Width-2),math.min(math.max(app[2],1),self.Height-2)
  36.         paintutils.drawImage(image,x,y)
  37.       end
  38.     end
  39.  
  40.     for i,btn in ipairs(self.Buttons) do
  41.       paintutils.drawFilledBox(btn[1],btn[2],btn[1]+btn[3]-1,btn[2]+btn[4]-1,btn[6])
  42.       local textBtn = self.TextButtons[i]
  43.       if textBtn then
  44.         term.setCursorPos(btn[1],btn[2])
  45.         term.setTextColor(textBtn[2])
  46.         term.setBackgroundColor(btn[6])
  47.         term.write(textBtn[1])
  48.       end
  49.     end
  50.  
  51.     term.setCursorPos(cx,cy)
  52.     term.setTextColor(tc)
  53.     term.setBackgroundColor(tb)
  54.  
  55.     term.blit("nekOS///","00000bde","77777777")
  56.     term.write(" "..self.User.Username)
  57.     term.setCursorPos(1,2)
  58.   end
  59.  
  60.   function helper:handleResize()
  61.     self.Width,self.Height = term.getSize()
  62.     self.Buttons = {}
  63.     self.TextButtons = {}
  64.  
  65.     self:addTextButton(self.Width,1,"X",function() os.queueEvent("terminate") end, colors.red, colors.black)
  66.     self:addTextButton(self.Width-2,1,"> ",function() self.App:activate("term") end, colors.black, colors.white)
  67.   end
  68.  
  69.   function helper.checkBB(x1,y1,x2,y2,x,y)
  70.     return x >= x1 and x < x2 and y >= y1 and y < y2
  71.   end
  72.  
  73.   function helper:addButton(x,y,w,h,s,b)
  74.     self.Buttons[#self.Buttons+1] = {x,y,w,h,s,b}
  75.     return #self.Buttons
  76.   end
  77.  
  78.   function helper:addTextButton(x,y,i,s,b,c)
  79.     local index = self:addButton(x,y,string.len(i),1,s,b)
  80.     self.TextButtons[index] = {i,c}
  81.     return index
  82.   end
  83.  
  84.   function helper:handleMousePressed(b,mx,my)
  85.     self.Selection = nil
  86.     for code,app in pairs(app.Apps.Data) do
  87.       if not app[4] then
  88.         local x,y = math.min(math.max(app[1],1),self.Width-2),math.min(math.max(app[2],1),self.Height-2)
  89.         if self.checkBB(x,y,x+3,y+3,mx,my) then self.Selection={code,x-mx,y-my} break end
  90.       end
  91.     end
  92.   end
  93.  
  94.   function helper:handleMouseReleased(b,mx,my)
  95.     if self.Dragging then
  96.       self.Dragging = false
  97.       app.Apps:save()
  98.       return
  99.     end
  100.  
  101.     for i,btn in ipairs(self.Buttons) do
  102.       if self.checkBB(btn[1],btn[2],btn[1]+btn[3],btn[2]+btn[4],mx,my) then
  103.         btn[5]()
  104.         return
  105.       end
  106.     end
  107.  
  108.     for code,app in pairs(app.Apps.Data) do
  109.       if not app[4] then
  110.         local x,y = math.min(math.max(app[1],1),self.Width-2),math.min(math.max(app[2],1),self.Height-2)
  111.         if self.checkBB(x,y,x+3,y+3,mx,my) then
  112.           -- TODO: Execute app in new shell
  113.           return
  114.         end
  115.       end
  116.     end
  117.   end
  118.  
  119.   function helper:handleMouseDragged(b,mx,my)
  120.     self.Dragging = true
  121.     if not self.Selection then return end
  122.     local s = app.Apps[self.Selection[1]]
  123.     s[1] = mx + self.Selection[2]
  124.     s[2] = my + self.Selection[3]
  125.   end
  126.  
  127.   return helper
  128. end
Add Comment
Please, Sign In to add comment