Advertisement
FFGFlash

.menu

Sep 14th, 2021 (edited)
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.36 KB | None | 0 0
  1. local App = app.new()
  2. local missingIcon = "/missing.nfp"
  3.  
  4. local buttons = {}
  5. local textButtons = {}
  6.  
  7. local function addButton(x, y, w, h, s, b)
  8.   table.insert(buttons, {x,y,w,h,s,b})
  9.   return #buttons
  10. end
  11.  
  12. local function addTextButton(x, y, i, s, b, c)
  13.   textButtons[addButton(x, y, string.len(i), 1, s, b)] = {i,c}
  14. end
  15.  
  16. local function checkBB(x1,y1,x2,y2,x,y)
  17.   return x >= x1 and x < x2 and y >= y1 and y < y2
  18. end
  19.  
  20. local c,b = "00000877", "ffffffff"
  21. if term.isColor() then c, b = "00000bde", "77777777" end
  22.  
  23. local w,h = term.getSize()
  24.  
  25. local apps = data.load("/.apps")
  26.  
  27. function resize()
  28.   w,h = term.getSize()
  29.   buttons = {}
  30.   textButtons = {}
  31.  
  32.   addTextButton(w,1,"X",function() App:stop() end, colors.red, colors.black)
  33. end
  34.  
  35. resize()
  36.  
  37. App.EventHandler:connect("term_resize", resize)
  38.  
  39. App.EventHandler:connect("update_apps", function()
  40.   apps = data.load("/.apps")
  41. end)
  42.  
  43. App.EventHandler:connect("app_closed", function(app)
  44.   apps = data.load("/.apps")
  45.   local id = apps[app][3]
  46.   for code,data in pairs(apps) do
  47.     if data[3] and (not id or data[3] > id) then
  48.       apps[code][3] = data[3] - 1
  49.     end
  50.   end
  51.   if id then apps[app][3] = -1 end
  52.   data.save("/.apps", apps)
  53. end)
  54.  
  55. local selection = nil
  56. local dragging = false
  57. App.EventHandler:connect("mouse_click", function(btn,mx,my)
  58.   selection = nil
  59.   for code,pos in pairs(apps) do
  60.     local x,y = math.min(math.max(pos[1], 1), w - 2), math.min(math.max(pos[2], 1), h - 2)
  61.     if checkBB(x,y,x+3,y+3,mx,my) then
  62.       selection = code
  63.       break
  64.     end
  65.   end
  66. end)
  67.  
  68. App.EventHandler:connect("mouse_up", function(btn,mx,my)
  69.   if dragging then
  70.     dragging = false
  71.     data.save("/.apps", apps)
  72.     return
  73.   end
  74.  
  75.   for i,btn in ipairs(buttons) do
  76.     if checkBB(btn[1],btn[2],btn[1]+btn[3],btn[2]+btn[4],mx,my) then
  77.       btn[5]()
  78.       return
  79.     end
  80.   end
  81.  
  82.   for code,pos in pairs(apps) do
  83.     local x,y = math.min(math.max(pos[1], 1), w - 2), math.min(math.max(pos[2], 1), h - 2)
  84.     if checkBB(x,y,x+3,y+3,mx,my) then
  85.       shell.run(".app execute "..code)
  86.       return
  87.     end
  88.   end
  89. end)
  90.  
  91. App.EventHandler:connect("mouse_drag", function(btn,mx,my)
  92.   dragging = true
  93.   if not selection then return end
  94.   apps[selection] = {mx,my}
  95. end)
  96.  
  97. function App:draw()
  98.   term.setTextColor(_G.TextColor)
  99.   term.setBackgroundColor(_G.BackgroundColor)
  100.   term.clear()
  101.   term.setCursorPos(1,1)
  102.  
  103.   local cx,cy = term.getCursorPos()
  104.   local tc = term.getTextColor()
  105.   local tb = term.getBackgroundColor()
  106.  
  107.   for code,pos in pairs(apps) do
  108.     local icon = "/appdata/"..code.."/.nfp"
  109.     local image = paintutils.loadImage(fs.exists(icon) and icon or missingIcon)
  110.     local x,y = math.min(math.max(pos[1], 1), w - 2), math.min(math.max(pos[2], 1), h - 2)
  111.     paintutils.drawImage(image, x, y)
  112.   end
  113.  
  114.   for i,btn in ipairs(buttons) do
  115.     paintutils.drawFilledBox(btn[1],btn[2],btn[1]+btn[3]-1,btn[2]+btn[4]-1,btn[6])
  116.     local textBtn = textButtons[i]
  117.     if textBtn then
  118.       term.setCursorPos(btn[1],btn[2])
  119.       term.setTextColor(textBtn[2])
  120.       term.setBackgroundColor(btn[6])
  121.       term.write(textBtn[1])
  122.     end
  123.   end
  124.  
  125.   term.setCursorPos(cx,cy)
  126.   term.setTextColor(tc)
  127.   term.setBackgroundColor(tb)
  128.  
  129.   term.blit("nekOS///",c,b)
  130.   term.write(" ".._G.User.Username)
  131.   term.setCursorPos(1,2)
  132. end
  133.  
  134. function App:stopped()
  135.   shell.run(".login")
  136. end
  137.  
  138. App:start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement