JustDoesGames

MOS (Mobile OS)

Apr 30th, 2021 (edited)
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 37.71 KB | None | 0 0
  1. -- Mobile OS --
  2. -- Koga --
  3. -- Episode 8 Youkoso -- (ignore this... it's an easter egg)
  4.  
  5. local isMobile = (mobile ~= nil)
  6. local native = term.current()
  7. local resizing = true
  8. local MOS_dir = "/MOS"
  9. --[[
  10.  
  11. Mobile (Normal) -> 44,55
  12. Mobile (Flipped) -> 86, 27
  13.  
  14. --]]
  15.  
  16. local function getSize() return term.getSize() end
  17. local w,h = getSize()
  18. if w < 30 and h < 30 then return error("Screen size too small: 30x30 minimum (yours:"..w.."x"..h..")") end
  19.  
  20. local main = window.create(term.current(), 1,1,w,h) term.redirect(main)
  21. local app_screen = {
  22.     display = window.create(term.current(), 5,5,w-6,(h/1.5)-6),
  23.     mode = 3,
  24.     defaults = {
  25.         x=1, y=1,
  26.         w=w, h=math.floor(h/1.5)-6,
  27.     },
  28.     extended = {
  29.         x=1, y=1,
  30.         w=w, h=h-4,
  31.     }
  32. }
  33.  
  34. local clr, cp, st, sb = term.clear, term.setCursorPos, term.setTextColor, term.setBackgroundColor
  35.  
  36. local apps = {
  37.     screen = 1,
  38.     screen_length = 0,
  39.     screen_height = 0,
  40.     screen_length_limit = 10,
  41.     screen_height_limit = 10,
  42.     icons = {
  43.         ["phone_logo"] = {{0,0,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768},{0,32768,1,1,1,256,256,256,256,256,256,256,256,256,256,128,128,128,32768},{32768,1,1,1,1,256,256,256,32768,32768,32768,32768,256,256,256,128,128,128,128,32768},{32768,1,1,1,1,256,256,256,256,256,256,256,256,256,256,128,128,128,128,32768},{32768,1,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,128,32768},{32768,1,32768,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32768,128,32768},{32768,1,32768,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32768,128,32768},{32768,1,32768,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32768,128,32768},{32768,1,32768,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32768,128,32768},{32768,1,32768,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32768,128,32768},{32768,1,32768,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32768,128,32768},{32768,1,32768,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32768,128,32768},{32768,1,32768,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32768,128,32768},{32768,1,32768,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32768,128,32768},{32768,1,32768,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32768,128,32768},{32768,1,32768,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32768,128,32768},{32768,1,32768,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32768,128,32768},{32768,1,32768,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32768,128,32768},{32768,1,32768,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32768,128,32768},{32768,1,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,128,32768},{32768,1,1,1,256,256,256,256,256,256,256,256,256,256,256,256,128,128,128,32768},{32768,1,1,1,256,256,256,256,32768,32768,32768,256,256,256,256,256,128,128,128,32768},{32768,1,1,1,256,256,256,256,32768,256,32768,256,256,256,256,256,128,128,128,32768},{32768,1,1,1,256,256,256,256,32768,32768,32768,256,256,256,256,256,128,128,128,32768},{0,32768,1,1,256,256,256,256,256,256,256,256,256,256,256,256,128,128,32768},{0,0,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768},{}},
  44.         ["no_app_icon"] = {{0,0,256,256,256,256,256,256,256,256,256,256,256},{0,256,128,128,128,128,128,128,128,128,128,128,128,256},{256,128,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,128,256},{256,128,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,128,256},{256,128,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,128,256},{256,128,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,128,256},{256,128,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,128,256},{256,128,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,128,256},{256,128,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,128,256},{256,128,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,128,256},{256,128,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,128,256},{256,128,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,128,256},{256,128,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,128,256},{0,256,128,128,128,128,128,128,128,128,128,128,128,256},{0,0,256,256,256,256,256,256,256,256,256,256,256},{}},
  45.        
  46.         ["arrow_up"] = {{0,0,0,128},{0,0,128,256,128},{0,128,256,256,256,128},{128,256,256,256,256,256,128},{}},
  47.         ["arrow_down"] = {{128,256,256,256,128},{0,128,256,128},{0,0,128},{}},
  48.        
  49.         ["scroll_left"] = {{128},{256,128},{256,128},{256,128},{128},{}},
  50.         ["scroll_right"] = {{0,128},{128,256},{128,256},{128,256},{0,128},{}},
  51.        
  52.         ["dpad_arrow_up"] = {{0,128,128},{128,256,256,128},{128,256,256,128},{}},
  53.         ["dpad_arrow_down"] = {{128,256,256,128},{128,256,256,128},{0,128,128},{}},
  54.         ["dpad_arrow_left"] = {{0,128,128},{128,256,256},{128,256,256},{0,128,128},{}},
  55.         ["dpad_arrow_right"] = {{128,128},{256,256,128},{256,256,128},{128,128},{}},
  56.         ["dpad_button_center"] = {{0,128,128},{128,256,256,128},{128,256,256,128},{0,128,128},{}},
  57.        
  58.         ["button_function"] = {{0,128,128,128},{128,256,256,256,128},{0,128,128,128},{}}, -- Enter / Ctrl
  59.         ["button_power"] = {{16384},{16384,16384},{16384,16384,16384},{}},
  60.         ["button_a"] = {{0,8192,8192,8192},{8192,32,32,32,8192},{8192,32,32,32,8192},{8192,32,32,32,8192},{0,8192,8192,8192},{}},
  61.         ["button_b"] = {{0,1024,1024,1024},{1024,4,4,4,1024},{1024,4,4,4,1024},{1024,4,4,4,1024},{0,1024,1024,1024},{}},
  62.        
  63.         ["app_icon_missing"] = {{0,0,128,128,128,128,128,128,128,128,128,128,128},{0,128,128,256,256,256,256,256,256,256,256,256,128,128},{128,128,256,256,256,256,256,256,256,256,256,256,256,128,128},{128,256,256,256,256,256,256,256,256,256,256,256,256,256,128},{128,256,256,256,128,128,128,128,128,128,128,256,256,256,128},{128,256,256,128,16384,16384,128,128,128,16384,16384,128,256,256,128},{128,256,256,128,16384,128,16384,128,16384,128,16384,128,256,256,128},{128,256,256,128,16384,128,16384,128,16384,128,16384,128,256,256,128},{128,256,256,128,16384,128,128,16384,128,128,16384,128,256,256,128},{128,256,256,128,16384,128,128,128,128,128,16384,128,256,256,128},{128,256,256,256,128,128,128,128,128,128,128,256,256,256,128},{128,256,256,256,256,256,256,256,256,256,256,256,256,256,128},{128,128,256,256,256,256,256,256,256,256,256,256,256,128,128},{0,128,128,256,256,256,256,256,256,256,256,256,128,128},{0,0,128,128,128,128,128,128,128,128,128,128,128},{}},
  64.         ["app_icon_invalid"] = {{0,0,128,128,128,128,128,128,128,128,128,128,128},{0,128,128,256,256,256,256,256,256,256,256,256,128,128},{128,128,256,256,256,256,256,256,256,256,256,256,256,128,128},{128,256,256,256,256,256,256,256,256,256,256,256,256,256,128},{128,256,256,256,128,128,128,128,128,128,128,256,256,256,128},{128,256,256,128,128,16384,16384,16384,16384,16384,128,128,256,256,128},{128,256,256,128,128,128,128,16384,128,128,128,128,256,256,128},{128,256,256,128,128,128,128,16384,128,128,128,128,256,256,128},{128,256,256,128,128,128,128,16384,128,128,128,128,256,256,128},{128,256,256,128,128,16384,16384,16384,16384,16384,128,128,256,256,128},{128,256,256,256,128,128,128,128,128,128,128,256,256,256,128},{128,256,256,256,256,256,256,256,256,256,256,256,256,256,128},{128,128,256,256,256,256,256,256,256,256,256,256,256,128,128},{0,128,128,256,256,256,256,256,256,256,256,256,128,128},{0,0,128,128,128,128,128,128,128,128,128,128,128},{}},
  65.        
  66.         ["app_icon_template"] = {{0,0,128,128,128,128,128,128,128,128,128,128,128,},{0,128,128,0,0,0,0,0,0,0,0,0,128,128,},{128,128,0,0,0,0,0,0,0,0,0,0,0,128,128,},{128,0,0,0,0,0,0,0,0,0,0,0,0,0,128,},{128,0,0,0,0,0,0,0,0,0,0,0,0,0,128,},{128,0,0,0,0,0,0,0,0,0,0,0,0,0,128,},{128,0,0,0,0,0,0,0,0,0,0,0,0,0,128,},{128,0,0,0,0,0,0,0,0,0,0,0,0,0,128,},{128,0,0,0,0,0,0,0,0,0,0,0,0,0,128,},{128,0,0,0,0,0,0,0,0,0,0,0,0,0,128,},{128,0,0,0,0,0,0,0,0,0,0,0,0,0,128,},{128,0,0,0,0,0,0,0,0,0,0,0,0,0,128,},{128,128,0,0,0,0,0,0,0,0,0,0,0,128,128,},{0,128,128,0,0,0,0,0,0,0,0,0,128,128,},{0,0,128,128,128,128,128,128,128,128,128,128,128,},{},},
  67.     },
  68.     size = {18,18}, real_app_size = {15,16},
  69.     ["invalid_app"] = {
  70.         name = "Invalid App",
  71.         --icon = {{0,0,2,2,2,2,2,2,2,2,2,2,2},{0,2,16,16,16,16,16,16,16,16,16,16,16,2},{2,16,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,16,2},{2,16,32768,1,32768,32768,32768,32768,32768,32768,32768,32768,32768,16,2},{2,16,32768,32768,1,32768,32768,32768,32768,32768,32768,32768,32768,16,2},{2,16,32768,1,32768,32768,1,1,1,32768,32768,32768,32768,16,2},{2,16,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,16,2},{2,16,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,16,2},{2,16,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,16,2},{2,16,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,16,2},{2,16,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,16,2},{2,16,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,16,2},{2,16,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,16,2},{0,2,16,16,16,16,16,16,16,16,16,16,16,2},{0,0,2,2,2,2,2,2,2,2,2,2,2},{}},
  72.         --exe = "",
  73.         extendable = false,
  74.         extended = false,
  75.     },
  76.     ["shell"] = {
  77.         name = "Shell",
  78.         icon = {{0,0,2,2,2,2,2,2,2,2,2,2,2},{0,2,16,16,16,16,16,16,16,16,16,16,16,2},{2,16,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,16,2},{2,16,32768,1,32768,32768,32768,32768,32768,32768,32768,32768,32768,16,2},{2,16,32768,32768,1,32768,32768,32768,32768,32768,32768,32768,32768,16,2},{2,16,32768,1,32768,32768,1,1,1,32768,32768,32768,32768,16,2},{2,16,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,16,2},{2,16,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,16,2},{2,16,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,16,2},{2,16,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,16,2},{2,16,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,16,2},{2,16,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,16,2},{2,16,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,16,2},{0,2,16,16,16,16,16,16,16,16,16,16,16,2},{0,0,2,2,2,2,2,2,2,2,2,2,2},{}},
  79.         exe = "rom/programs/shell.lua",
  80.         extendable = true,
  81.         extended = false,
  82.     },
  83.     ["lua"] = {
  84.         name = "Lua",
  85.         icon = {{0,0,128,128,128,128,128,128,128,128,128,128,128},{0,128,1,1,1,1,1,1,1,1,1,1,1,128},{128,1,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,1,128},{128,1,32768,1,32768,32768,32768,32768,32768,32768,32768,32768,32768,1,128},{128,1,32768,32768,1,32768,32768,32768,32768,32768,32768,32768,32768,1,128},{128,1,32768,1,32768,32768,1,1,1,32768,32768,32768,32768,1,128},{128,1,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,1,128},{128,1,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,1,128},{128,1,32768,128,32768,128,32768,128,32768,128,128,128,32768,1,128},{128,1,32768,128,32768,128,32768,128,32768,128,32768,128,32768,1,128},{128,1,32768,128,32768,128,32768,128,32768,128,128,128,32768,1,128},{128,1,32768,128,32768,128,128,128,32768,128,32768,128,32768,1,128},{128,1,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,1,128},{0,128,1,1,1,1,1,1,1,1,1,1,1,128},{0,0,128,128,128,128,128,128,128,128,128,128,128},{}},
  86.         exe = "rom/programs/lua.lua",
  87.         extendable = true,
  88.         extended = false,
  89.     },
  90.     ["taskmngr"] = {
  91.         name = "Task Manager",
  92.         icon = {{0,0,128,128,128,128,128,128,128,128,128,128,128},{0,128,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,128},{128,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,128},{128,32768,32768,16384,32768,32768,32768,32,32768,32768,32768,2048,32768,32768,128},{128,32768,32768,16384,32768,32768,256,256,256,32768,32768,2048,32768,32768,128},{128,32768,32768,16384,32768,32768,256,256,256,32768,32768,2048,32768,32768,128},{128,32768,32768,16384,32768,32768,32768,32,32768,32768,32768,2048,32768,32768,128},{128,32768,256,256,256,32768,32768,32,32768,32768,32768,2048,32768,32768,128},{128,32768,256,256,256,32768,32768,32,32768,32768,32768,2048,32768,32768,128},{128,32768,32768,16384,32768,32768,32768,32,32768,32768,256,256,256,32768,128},{128,32768,32768,16384,32768,32768,32768,32,32768,32768,256,256,256,32768,128},{128,32768,32768,16384,32768,32768,32768,32,32768,32768,32768,2048,32768,32768,128},{128,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,128},{0,128,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,128},{0,0,128,128,128,128,128,128,128,128,128,128,128},{}},
  93.         exe = "",
  94.         extendable = true,
  95.         extended = false,
  96.     },
  97.     ["updater"] = {
  98.         name = "Updater",
  99.         icon = {{0,0,2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,2048},{0,2048,2048,256,256,256,256,256,256,256,256,256,2048,2048},{2048,2048,256,256,1,1,1,1,1,1,1,256,256,2048,2048},{2048,256,256,1,1,1,1,32,1,1,1,1,256,256,2048},{2048,256,1,1,1,1,32,32,32,1,1,1,1,256,2048},{2048,256,1,1,1,32,32,32,32,32,1,1,1,256,2048},{2048,256,1,1,32,32,32,32,32,32,32,1,1,256,2048},{2048,256,1,1,1,1,32,32,32,1,1,1,1,256,2048},{2048,256,1,1,1,1,32,32,32,1,1,1,1,256,2048},{2048,256,1,1,1,1,32,32,32,1,1,1,1,256,2048},{2048,256,1,1,1,1,32,32,32,1,1,1,1,256,2048},{2048,256,256,1,1,1,32,32,32,1,1,1,256,256,2048},{2048,2048,256,256,1,1,1,1,1,1,1,256,256,2048,2048},{0,2048,2048,256,256,256,256,256,256,256,256,256,2048,2048},{0,0,2048,2048,2048,2048,2048,2048,2048,2048,2048,2048,2048},{}},
  100.         exe = "",
  101.         extendable = false,
  102.         extended = false,
  103.     },
  104.     ["power"] = {
  105.         name = "Power",
  106.         icon = {{0,0,0,128,128,128,128,128,128,128,128,128},{0,0,128,256,256,256,256,256,256,256,256,256,128},{0,128,256,16384,16384,16384,16384,16384,16384,16384,16384,16384,256,128},{128,256,16384,16384,16384,16384,16384,1,16384,16384,16384,16384,16384,256,128},{128,256,16384,16384,1,1,16384,1,16384,1,1,16384,16384,256,128},{128,256,16384,1,1,16384,16384,1,16384,16384,1,1,16384,256,128},{128,256,16384,1,16384,16384,16384,1,16384,16384,16384,1,16384,256,128},{128,256,16384,1,16384,16384,16384,16384,16384,16384,16384,1,16384,256,128},{128,256,16384,1,16384,16384,16384,16384,16384,16384,16384,1,16384,256,128},{128,256,16384,1,16384,16384,16384,16384,16384,16384,16384,1,16384,256,128},{128,256,16384,1,1,16384,16384,16384,16384,16384,1,1,16384,256,128},{128,256,16384,16384,1,1,1,1,1,1,1,16384,16384,256,128},{0,128,256,16384,16384,16384,16384,16384,16384,16384,16384,16384,256,128},{0,0,128,256,256,256,256,256,256,256,256,256,128},{0,0,0,128,128,128,128,128,128,128,128,128},{}},
  107.         exe = "",
  108.         extendable = false,
  109.         extended = false,
  110.     },
  111.     ["reboot"] = {
  112.         name = "Reboot",
  113.         icon = {{0,0,0,128,128,128,128,128,128,128,128,128},{0,0,128,256,256,256,256,256,256,256,256,256,128},{0,128,256,8192,8192,8192,8192,8192,8192,8192,8192,8192,256,128},{128,256,8192,8192,8192,8192,8192,1,8192,8192,8192,8192,8192,256,128},{128,256,8192,8192,1,1,8192,1,8192,1,1,8192,8192,256,128},{128,256,8192,1,1,8192,8192,1,8192,8192,1,1,8192,256,128},{128,256,8192,1,8192,8192,8192,1,8192,8192,8192,1,8192,256,128},{128,256,8192,1,8192,8192,8192,8192,8192,8192,8192,1,8192,256,128},{128,256,8192,1,8192,8192,8192,8192,8192,8192,8192,1,8192,256,128},{128,256,8192,1,8192,8192,8192,8192,8192,8192,8192,1,8192,256,128},{128,256,8192,1,1,8192,8192,8192,8192,8192,1,1,8192,256,128},{128,256,8192,8192,1,1,1,1,1,1,1,8192,8192,256,128},{0,128,256,8192,8192,8192,8192,8192,8192,8192,8192,8192,256,128},{0,0,128,256,256,256,256,256,256,256,256,256,128},{0,0,0,128,128,128,128,128,128,128,128,128},{}},
  114.         exe = "",
  115.         extendable = false,
  116.         extended = false,
  117.     },
  118.     ["worm"] = {
  119.         name = "Worm",
  120.         icon = {{0,0,128,128,128,128,128,128,128,128,128,128,128},{0,128,128,32768,32768,32768,32768,32768,32768,32768,32768,32768,128,128},{128,128,32768,16384,32768,32768,32768,32768,32768,32768,32768,32768,32768,128,128},{128,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,128},{128,32768,32768,8192,32768,32768,32768,32768,32768,32768,32768,8192,32768,32768,128},{128,32768,32768,8192,32768,32768,32768,32768,32768,32768,32768,8192,32768,32768,128},{128,32768,32768,8192,32768,32768,32768,32768,32768,32768,32768,8192,32768,32768,128},{128,32768,32768,8192,32768,32768,32768,32768,32768,32768,32768,8192,32768,32768,128},{128,32768,32768,8192,32768,32768,8192,8192,8192,32768,32768,8192,32768,32768,128},{128,32768,32768,8192,32768,32768,8192,32768,8192,32768,32768,8192,32768,32768,128},{128,32768,32768,8192,32768,32768,8192,32768,8192,32768,32768,8192,32768,32768,128},{128,32768,32768,8192,8192,8192,8192,32768,8192,8192,8192,8192,32768,32768,128},{128,128,32768,32768,32768,32768,32768,32768,32768,16384,32768,32768,32768,128,128},{0,128,128,32768,32768,32768,32768,32768,32768,32768,32768,32768,128,128},{0,0,128,128,128,128,128,128,128,128,128,128,128},{}},
  121.         exe = "rom/programs/fun/worm.lua",
  122.         extendable = false,
  123.         extended = false,
  124.     },
  125.     ["events"] = {
  126.         name = "Events",
  127.         icon = {{0,0,128,128,128,128,128,128,128,128,128,128,128},{0,128,128,256,256,256,256,256,256,256,256,256,128,128},{128,128,256,256,128,128,128,128,128,128,128,256,256,128,128},{128,256,256,1,1,1,1,1,1,1,1,1,256,256,128},{128,256,128,1,1,1,1,1,1,1,1,128,128,256,128},{128,256,128,1,128,128,128,128,128,128,128,128,128,256,128},{128,256,128,1,128,128,128,128,128,128,128,128,128,256,128},{128,256,128,1,1,1,1,1,128,128,128,128,128,256,128},{128,256,128,1,128,128,128,128,128,128,128,128,128,256,128},{128,256,128,1,128,128,128,128,128,128,128,128,128,256,128},{128,256,128,1,1,1,1,1,1,1,1,128,128,256,128},{128,256,256,1,1,1,1,1,1,1,1,1,256,256,128},{128,128,256,256,128,128,128,128,128,128,128,256,256,128,128},{0,128,128,256,256,256,256,256,256,256,256,256,128,128},{0,0,128,128,128,128,128,128,128,128,128,128,128},{}},
  128.         exe = "",
  129.         extendable = true,
  130.         extended = false,
  131.     },
  132.     ["app_manager"] = {
  133.         name = "App Manager",
  134.         icon = {{0,0,128,128,128,128,128,128,128,128,128,128,128,},{0,128,128,256,256,256,256,256,256,256,256,256,128,128,},{128,128,1,1,1,256,1,1,1,256,1,1,1,128,128,},{128,256,1,1,1,256,1,1,1,256,1,1,1,256,128,},{128,256,1,1,1,256,1,1,1,256,1,1,1,256,128,},{128,256,256,256,256,256,256,256,256,256,256,256,256,256,128,},{128,256,1,1,1,256,1,1,1,256,1,1,1,256,128,},{128,256,1,1,1,256,1,1,1,256,1,1,1,256,128,},{128,256,1,1,1,256,1,1,1,256,1,1,1,256,128,},{128,256,256,256,256,256,256,256,256,256,256,256,256,256,128,},{128,256,1,1,1,256,1,1,1,256,1,1,1,256,128,},{128,256,1,1,1,256,1,1,1,256,1,1,1,256,128,},{128,128,1,1,1,256,1,1,1,256,1,1,1,128,128,},{0,128,128,256,256,256,256,256,256,256,256,256,128,128,},{0,0,128,128,128,128,128,128,128,128,128,128,128,},{},},
  135.         exe = "",
  136.         extendable = false,
  137.         extended = false,
  138.     },
  139. }
  140.  
  141. local function grabFileData(f)
  142.     if not fs.exists(f) then return error("'"..f.."' does not exists.") end
  143.     local f_handle = fs.open(f, "r")
  144.     if not f_handle then return error("Failed to open '"..f.."'.") end
  145.     f = f_handle.readAll() f_handle.close()
  146.     return f
  147. end
  148.  
  149.  
  150.  
  151. local system = {}
  152. if fs.exists(MOS_dir.."/system.cfg") then
  153.     system = textutils.unserialize(grabFileData(MOS_dir.."/system.cfg"))
  154. end
  155.  
  156. if type(system) ~= "table" then system = {} end
  157. system = system or {}
  158. system.colors = system.colors or {["background"] = "cyan",["text"] = "white",}
  159. system.splash = system.splash or {
  160.     text = "MOS",
  161.     colors = {
  162.         --["background"] = colors.cyan,
  163.         ["text"] = "gray",
  164.     },
  165.     position = {1,1},
  166. }
  167. system.app_order = system.app_order or {"shell", "lua", "updater", "power", "reboot", "worm", "events", "app_manager"}
  168. if not fs.exists(MOS_dir.."/system.cfg") then
  169.     local f = fs.open(MOS_dir.."/system.cfg", "w")
  170.     f.write(textutils.serialize(system)) f.close()
  171. end
  172.  
  173.  
  174. local function validateIcon(icon)
  175.     if not icon then return apps.icons["app_icon_missing"] end
  176.     if type(icon) ~= "table" then return apps.icons["app_icon_invalid"] end
  177.     local w = 0
  178.     for i=1, #icon do
  179.         if type(icon[i]) ~= "table" then return apps.icons["app_icon_invalid"] end
  180.         w = math.max(#icon[i], w)
  181.     end
  182.     if w == apps.real_app_size[1] and #icon == apps.real_app_size[2] then return icon end
  183.     return apps.icons["app_icon_invalid"]
  184. end
  185.  
  186. local userapps_dir = MOS_dir.."/userapps"
  187. if not fs.exists(userapps_dir) then fs.makeDir(userapps_dir) end
  188. local f = fs.list(userapps_dir)
  189. for i=1, #f do
  190.     if fs.isDir(userapps_dir.."/"..f[i]) then
  191.         if fs.exists(userapps_dir.."/"..f[i].."/"..f[i]..".cfg") then
  192.             tmp = textutils.unserialize(grabFileData(userapps_dir.."/"..f[i].."/"..f[i]..".cfg"))
  193.             if type(tmp) == "table" then
  194.                 if tmp.load == nil then tmp.load = true end
  195.                 if tmp.name and tmp.load then
  196.                     tmp.icon = validateIcon(tmp.icon)
  197.                     apps[tmp.name:lower():gsub(" ", "_")] = tmp
  198.                     system.app_order[tmp.orderID or #system.app_order+1] = tmp.name:lower():gsub(" ", "_")
  199.                 end
  200.             end
  201.         end
  202.     end
  203. end
  204.  
  205. local function doStartupSplash()
  206.     local ps = {(w/2-10)+2,(h/2-13)+4, (w/2-10)+17,(h/2-13)+19}
  207.     st(colors.white) sb(colors.black) clr()
  208.     paintutils.drawImage(apps.icons["phone_logo"], w/2-10, h/2-13) sleep(1)
  209.     paintutils.drawFilledBox(ps[1],ps[2],ps[3],ps[4],colors.gray) sleep(.1)
  210.     paintutils.drawFilledBox(ps[1],ps[2],ps[3],ps[4],colors.lightGray) sleep(.05)
  211.     paintutils.drawFilledBox(ps[1],ps[2],ps[3],ps[4],colors.white) sleep(1)
  212. end doStartupSplash()
  213.  
  214. local function doSplash() st(colors[system.splash.colors["text"] or system.colors["text"]]) sb(colors[system.splash.colors["background"] or system.colors["background"]]) cp(system.splash.position[1], system.splash.position[2]) write(system.splash.text) end
  215.  
  216. --[[
  217. if w < apps.size[1]*2 or h < apps.size[2]*3 then
  218.     error("Screen needs to be at least "..(apps.size[1]*2).."x"..(apps.size[2]*3))
  219. end
  220. --]]
  221.  
  222. local function findFTE(t) local i=0 repeat i=i+1 until t[i] == nil return i end
  223. local function centerWrite(t) local x,y = term.getCursorPos() cp(w/2-string.len(t or "Invalid Centered Text: "..type(t))/2, y) write(t or "Invalid Centered Text: "..type(t)) end
  224.  
  225. --==[[ Coroutine ]]==--
  226.  
  227. local tasks, living = {}, 0
  228.  
  229. local function addTask(...)
  230.     local tFns = table.pack(...)
  231.     local tCos = {}
  232.     for i = 1, tFns.n, 1 do
  233.         local fn = tFns[i]
  234.         if type(fn) ~= "function" then
  235.             error("bad argument #" .. i .. " (expected function, got " .. type(fn) .. ")", 3)
  236.         end
  237.         tCos[#tCos+1] = findFTE(tasks)
  238.         tasks[findFTE(tasks)] = coroutine.create(fn)
  239.         living = living + 1
  240.     end
  241.     return table.unpack(tCos)
  242. end
  243.  
  244. local function removeTask(id)
  245.     tasks[id] = nil
  246.     living = living - 1
  247. end
  248.  
  249. local function runTasks(_limit)
  250.     local tFilters = {}
  251.     local eventData = { n = 0 }
  252.     while true do
  253.         for n = 1, #tasks do
  254.             local r = tasks[n]
  255.             if r then
  256.                 if tFilters[r] == nil or tFilters[r] == eventData[1] or eventData[1] == "terminate" then
  257.                     local ok, param = coroutine.resume(r, table.unpack(eventData, 1, eventData.n))
  258.                     if not ok then
  259.                         return ok, param
  260.                     else
  261.                         tFilters[r] = param
  262.                     end
  263.                     if coroutine.status(r) == "dead" then
  264.                         table.remove(tasks, n)
  265.                         living = living - 1
  266.                         if living <= _limit then
  267.                             return n
  268.                         end
  269.                     end
  270.                 end
  271.             end
  272.         end
  273.         for n = 1, #tasks do
  274.             local r = tasks[n]
  275.             if r and coroutine.status(r) == "dead" then
  276.                 tasks[n] = nil
  277.                 living = living - 1
  278.                 if living <= _limit then
  279.                     return n
  280.                 end
  281.             end
  282.         end
  283.         eventData = table.pack(os.pullEventRaw())
  284.     end
  285. end
  286.  
  287. --[[
  288. local function waitForAny(...)
  289.     local routines = create(...)
  290.     return runUntilLimit(routines, #routines - 1)
  291. end
  292.  
  293. local function waitForAll(...)
  294.     local routines = create(...)
  295.     return runUntilLimit(routines, 0)
  296. end
  297. --]]
  298.  
  299. local function initTasks(...)
  300.     addTask(...)
  301.     return runTasks(1) -- 1 because RESIZE has os.pullEvent("term_resize") hanging it. so SCREEN ends the loop
  302. end
  303.  
  304.  
  305. --==[[ Coroutine ]]==--
  306.  
  307. local function safeRedirect(m)
  308.     m.restoreCursor()
  309.     m.setCursorBlink(false)
  310.     term.redirect(m)
  311. end
  312.  
  313. local function waitForClick() print("Tap screen to go back...") os.pullEvent("mouse_click") end
  314.  
  315. local function runNoProgram() print("This app does not have a program associated with it.") sleep(2) waitForClick() end
  316.  
  317. local drawControllerUI
  318. local function runProgram(app)
  319.    
  320.     if app.extendable == nil then app.extendable = true end -- I know there is a better way of doing this but I just wanna keep it like this 4 now
  321.     if app.extended == nil then app.extended = false end
  322.    
  323.    
  324.     if app_screen.mode == 3 then if app.extended then app_screen.mode = 1 else app_screen.mode = 2 end end
  325.    
  326.     local function setAppScreenRes()
  327.         if app_screen.mode == 1 then
  328.             app_screen.display.reposition(app_screen.extended.x, app_screen.extended.y, app_screen.extended.w, app_screen.extended.h)
  329.         else
  330.             app_screen.display.reposition(app_screen.defaults.x, app_screen.defaults.y, app_screen.defaults.w, app_screen.defaults.h)
  331.         end
  332.     end
  333.    
  334.     local red_button_coords, arrow_coords, dpad_coords, inputs
  335.     local function setValues()
  336.         red_button_coords = {1,h-2,3,h}
  337.         arrow_coords = {
  338.             --{math.floor(w/2)-3, h-3, math.floor(w/2)+3, h}, -- EXTENDED
  339.             --{math.floor(w/2)-3, app_screen.defaults.h+1, math.floor(w/2)+3, app_screen.defaults.h+4}
  340.             {5, h-3, 4, 3}, -- EXTENDED
  341.             {5, app_screen.defaults.h+1, 4, 3}
  342.         }
  343.         inputs = {
  344.             {
  345.                 icon = apps.icons["button_function"], -- ENTER
  346.                 name = string.char(16),
  347.                 text_color = colors.white,
  348.                 background_color = colors.lightGray,
  349.                 text_offset_x = 2, text_offset_y = 1,
  350.                 x=w-5,y=app_screen.defaults.h+2,w=4,h=2,
  351.                 exe_a = function() os.queueEvent("key", 28, false) end,
  352.                 exe_b = function() os.queueEvent("key_up", 28) end
  353.             },
  354.             {
  355.                 icon = apps.icons["button_function"], -- CTRL
  356.                 name = string.char(17),
  357.                 text_color = colors.white,
  358.                 background_color = colors.lightGray,
  359.                 text_offset_x = 2, text_offset_y = 1,
  360.                 x=w-15,y=app_screen.defaults.h+2,w=4,h=2,
  361.                 exe_a = function() os.queueEvent("key", 29, false) end,
  362.                 exe_b = function() os.queueEvent("key_up", 29) end,
  363.             },
  364.             {
  365.                 icon = apps.icons["button_a"],
  366.                 name = "A",
  367.                 text_color = colors.white,
  368.                 background_color = colors.lime,
  369.                 text_offset_x = 2, text_offset_y = 2,
  370.                 x=3,y=h-11,w=4,h=4,
  371.                 exe_a = function() os.queueEvent("key", 30, false) os.queueEvent("char", "a") end,
  372.                 exe_b = function() os.queueEvent("key_up", 30) end
  373.             },
  374.             {
  375.                 icon = apps.icons["button_b"],
  376.                 name = "B",
  377.                 text_color = colors.white,
  378.                 background_color = colors.magenta,
  379.                 text_offset_x = 2, text_offset_y = 2,
  380.                 x=8,y=h-7,w=4,h=4,
  381.                 exe_a = function() os.queueEvent("key", 48, false) os.queueEvent("char", "b") end,
  382.                 exe_b = function() os.queueEvent("key_up", 48) end
  383.             },
  384.            
  385.         }
  386.     end setValues()
  387.    
  388.     local function drawController()
  389.         local center = {w-9, h-8} setValues()
  390.         paintutils.drawImage(apps.icons["dpad_button_center"], center[1], center[2])
  391.         paintutils.drawImage(apps.icons["dpad_arrow_up"], center[1], center[2]-4)
  392.         paintutils.drawImage(apps.icons["dpad_arrow_down"], center[1], center[2]+5)
  393.         paintutils.drawImage(apps.icons["dpad_arrow_left"], center[1]-4, center[2])
  394.         paintutils.drawImage(apps.icons["dpad_arrow_right"], center[1]+5, center[2])
  395.         dpad_coords = {
  396.             {center[1], center[2]-5, center[1]+3, center[2]-2, function() os.queueEvent("key", 200, false) end}, -- up
  397.             {center[1], center[2]+5, center[1]+3, center[2]+8, function() os.queueEvent("key", 208, false) end}, -- down
  398.             {center[1]-5, center[2], center[1]-2, center[2]+3, function() os.queueEvent("key", 203, false) end}, -- left
  399.             {center[1]+5, center[2], center[1]+8, center[2]+3, function() os.queueEvent("key", 205, false) end}, -- right
  400.         }
  401.        
  402.         for i=1, #inputs do
  403.             paintutils.drawImage(inputs[i].icon, inputs[i].x, inputs[i].y)
  404.             cp(inputs[i].x+inputs[i].text_offset_x, inputs[i].y+inputs[i].text_offset_y) st(inputs[i].text_color) sb(inputs[i].background_color) write(inputs[i].name)
  405.         end
  406.        
  407.     end
  408.    
  409.     local function drawUI()
  410.         main.setVisible(false) safeRedirect(main) setValues()
  411.         paintutils.drawFilledBox(1,app_screen.defaults.h+1, w, h, colors[system.colors["background"] or colors.cyan])
  412.         if app_screen.mode ~= 1 then drawController() end
  413.         if app.extendable then
  414.             arrow_coords = {
  415.                 --{math.floor(w/2)-3, h-3, math.floor(w/2)+3, h}, -- EXTENDED
  416.                 --{math.floor(w/2)-3, app_screen.defaults.h+1, math.floor(w/2)+3, app_screen.defaults.h+4}
  417.                 {5, h-3, 4, 3}, -- EXTENDED
  418.                 {5, app_screen.defaults.h+1, 4, 2}
  419.                
  420.             }
  421.             paintutils.drawImage(apps.icons["arrow_down"], arrow_coords[app_screen.mode][1], arrow_coords[app_screen.mode][2])
  422.         end
  423.         paintutils.drawImage(apps.icons["button_power"], red_button_coords[1], red_button_coords[2])
  424.         main.setVisible(true)
  425.     end
  426.     drawControllerUI = drawUI
  427.    
  428.     --[[ DEBUG --
  429.     drawUI()
  430.     for i=1, #inputs do
  431.         paintutils.drawImage(inputs[i].icon, inputs[i].x, inputs[i].y)
  432.     end
  433.     for i=1, #inputs do
  434.         paintutils.drawBox(inputs[i].x, inputs[i].y, inputs[i].x+inputs[i].w, inputs[i].y+inputs[i].h, colors.red)
  435.     end
  436.     paintutils.drawBox(arrow_coords[2][1], arrow_coords[2][2], arrow_coords[2][3]+arrow_coords[2][1], arrow_coords[2][4]+arrow_coords[2][2])
  437.     sleep(2)
  438.     -- DEBUG --]]
  439.    
  440.     local function a() -- Manage Controller / Resizing / Other
  441.         drawUI()
  442.        
  443.         while true do
  444.             local e = {os.pullEvent("mouse_click")}
  445.  
  446.             if e[3] >= red_button_coords[1] and e[3] <= red_button_coords[3] and e[4] >= red_button_coords[2] and e[4] <= red_button_coords[4] then
  447.                 break
  448.             elseif e[3] >= arrow_coords[app_screen.mode][1] and e[3] <= arrow_coords[app_screen.mode][3]+arrow_coords[app_screen.mode][1] and e[4] >= arrow_coords[app_screen.mode][2] and e[4] <= arrow_coords[app_screen.mode][4]+arrow_coords[app_screen.mode][2] and app.extendable then
  449.                 if app_screen.mode == 1 then app_screen.mode = 2 else app_screen.mode = 1 end
  450.                 setAppScreenRes()
  451.                 drawUI()
  452.                 app_screen.display.redraw()
  453.                 safeRedirect(app_screen.display)
  454.             else
  455.                 if app_screen.mode == 2 then
  456.                     for i=1, #dpad_coords do
  457.                         if e[3] >= dpad_coords[i][1] and e[3] <= dpad_coords[i][3] and e[4] >= dpad_coords[i][2] and e[4] <= dpad_coords[i][4] then
  458.                             dpad_coords[i][5]()
  459.                         end
  460.                     end
  461.                     for i=1, #inputs do
  462.                         if e[3] >= inputs[i].x and e[3] <= inputs[i].x+inputs[i].w and e[4] >= inputs[i].y and e[4] <= inputs[i].y+inputs[i].h then
  463.                             inputs[i].exe_a()
  464.                             os.pullEvent("mouse_up")
  465.                             inputs[i].exe_b()
  466.                         end
  467.                     end
  468.                 end
  469.             end
  470.         end
  471.     end
  472.     local function b() -- Manage Running Program
  473.         sleep(.01)
  474.         app_screen.display.setVisible(true)
  475.         setAppScreenRes()
  476.         safeRedirect(app_screen.display) st(colors.white) sb(colors.black) clr() cp(1,1)
  477.         local r,e = false, "Failed to Run."
  478.         if type(app.exe) == "function" then
  479.             r,e = pcall(app.exe)
  480.         elseif type(app.exe) == "string" then
  481.             if not fs.exists(app.exe) then print("'"..app.exe.."' could not be located.") sleep(2) waitForClick() r = true else r,e = shell.run(app.exe) end
  482.         else
  483.             runNoProgram()
  484.             r = true
  485.         end
  486.         if not r then
  487.             printError(e) sleep(2) waitForClick()
  488.         end
  489.         app_screen.display.setVisible(false)
  490.     end
  491.     local ah,bh = addTask(a,b)
  492.     while true do
  493.         if tasks[ah] == nil or tasks[bh] == nil then break end sleep(.1)
  494.     end
  495.     if tasks[ah] then removeTask(ah) end
  496.     if tasks[bh] then removeTask(bh) end
  497.     app_screen.mode = 3
  498. end
  499.  
  500. local function runAppManager()
  501.     --[[
  502.     local sel = 1
  503.    
  504.     local function drawUI()
  505.         st(colors.white) sb(colors.black) clr()
  506.         paintutils.drawLine(1,1,app_screen.defaults.w,1,colors.gray)
  507.         paintutils.drawLine(1,app_screen.defaults.h,app_screen.defaults.w,app_screen.defaults.h,colors.gray)
  508.     end
  509.    
  510.     while true do
  511.         drawUI() sleep(5)
  512.     end
  513.     --]]
  514.     print("Program is not setup.") sleep(2) waitForClick()
  515. end
  516.  
  517. local function runUpdater()
  518.     print("Updating...")
  519.     local h = http.get("https://pastebin.com/raw/3XuFvnY3")
  520.     if not h then
  521.         printError("Failed to get update.") sleep(2) waitForClick()
  522.     else
  523.         local u = h.readAll() h.close()
  524.         fs.delete("/startup.lua")
  525.         local f = fs.open("startup.lua", "w")
  526.         f.write(u) f.close()
  527.         print("Updated! Rebooting...") sleep(1) os.reboot()
  528.     end
  529. end
  530.  
  531. local function runTaskMngr()
  532.     print("Program is not setup.") sleep(2) waitForClick()
  533. end
  534.  
  535. local run = true
  536. local function runExit()
  537.     print("Exiting...") sleep(1) run = false
  538. end
  539. local function runReboot()
  540.     print("Rebooting...") sleep(1) os.reboot()
  541. end
  542.  
  543. local function runEvents()
  544.     local last = {e={""}}
  545.     while true do
  546.         local e = {os.pullEvent()}
  547.         if e[1] ~= last.e[1] then
  548.             last = {e=e, c=1}
  549.             for i=1, #e do
  550.                 write("["..i.."] "..tostring(e[i]).." ")
  551.             end print("")
  552.         else
  553.             local x,y = term.getCursorPos()
  554.             cp(1,y-1)
  555.             for i=1, #last.e do
  556.                 write("["..i.."] "..tostring(last.e[i]).." ")
  557.             end print(" ("..last.c..")")
  558.             last.c = last.c + 1
  559.         end
  560.     end
  561. end
  562.  
  563. apps["taskmngr"].exe = runTaskMngr
  564. apps["updater"].exe = runUpdater
  565. apps["power"].exe = runExit
  566. apps["reboot"].exe = runReboot
  567. apps["events"].exe = runEvents
  568. apps["app_manager"].exe = runAppManager
  569.  
  570. local buttons
  571. local function SCREEN()
  572.     --sleep(.1)
  573.     local update = true
  574.     while run do
  575.         -- updates --
  576.         if update then
  577.             while resizing do sleep(.1) end sleep(.01)
  578.             safeRedirect(main)
  579.             update = false
  580.             st(colors.white) sb(colors[system.colors["background"] or colors.cyan]) clr()
  581.             local cnt = ((apps.screen_length*apps.screen_height)*(apps.screen-1))+1
  582.             buttons = {}
  583.             for i=1, apps.screen_height do
  584.                 for ii=1, apps.screen_length do
  585.                     local app_cal = {
  586.                         math.floor((w-(apps.screen_length*apps.size[1]))/2+2)+((ii-1)*apps.size[1]),
  587.                         math.floor((h-(apps.screen_height*apps.size[2]))/2+2)+((i-1)*apps.size[2]),
  588.                     }
  589.                     if apps[system.app_order[cnt]] then
  590.                         paintutils.drawImage(apps[system.app_order[cnt]].icon or apps.icons["app_icon_missing"], app_cal[1],app_cal[2])
  591.                         buttons[#buttons+1] = {app_cal[1], app_cal[2], app_cal[1]+apps.size[1]-3,app_cal[2]+apps.size[2]-3}
  592.                         --paintutils.drawPixel(buttons[#buttons][1]+8, buttons[#buttons][4], colors.red)
  593.                         sb(colors[system.colors["background"] or colors.cyan]) cp((buttons[#buttons][1]+8)-math.ceil(string.len(apps[system.app_order[cnt]].name)/2), buttons[#buttons][4]+1) write(apps[system.app_order[cnt]].name)
  594.                     elseif system.app_order[cnt] ~= nil then
  595.                         paintutils.drawImage(apps.icons["app_icon_invalid"], app_cal[1],app_cal[2])
  596.                         buttons[#buttons+1] = {app_cal[1], app_cal[2], app_cal[1]+apps.size[1]-3,app_cal[2]+apps.size[2]-3}
  597.                         --paintutils.drawPixel(buttons[#buttons][1]+8, buttons[#buttons][4], colors.red)
  598.                         sb(colors[system.colors["background"] or colors.cyan]) cp((buttons[#buttons][1]+8)-math.ceil(string.len(apps["invalid_app"].name)/2), buttons[#buttons][4]+1) write(apps["invalid_app"].name)
  599.                     else
  600.                         paintutils.drawImage(apps.icons["no_app_icon"], app_cal[1],app_cal[2])
  601.                     end
  602.                     cnt=cnt+1
  603.                 end
  604.             end
  605.             --[[ Debug --
  606.             paintutils.drawLine(1,h/2,w,h/2, colors.gray)
  607.             paintutils.drawLine(w/2,1,w/2,h, colors.gray)
  608.             paintutils.drawPixel(w/2, h/2, colors.gray)
  609.             paintutils.drawBox(w/2-1, h/2-1, w/2+1, h/2+1, colors.lightGray)
  610.             -- Debug --]]
  611.             paintutils.drawImage(apps.icons["scroll_left"], 1,h/2-2)
  612.             paintutils.drawImage(apps.icons["scroll_right"], w-1,h/2-2)
  613.             doSplash()
  614.         end
  615.        
  616.         -- hitboxes --
  617.         --
  618.         for i=1, #buttons do
  619.             --paintutils.drawBox(buttons[i][1], buttons[i][2], buttons[i][3], buttons[i][4], colors.red) sleep(.1)
  620.         end
  621.         --]]
  622.        
  623.         -- events --
  624.         e = {os.pullEvent()}
  625.        
  626.         if e[1] == "term_resize" then
  627.             update = true
  628.         elseif e[1] == "mouse_click" then
  629.             local e2 = {}
  630.             while e2[1] ~= "mouse_up" do
  631.                 e2 = {os.pullEvent()}
  632.             end
  633.             if e2[3] >= e[3]-3 and e2[3] <= e[3]+3 then -- Clicked an app
  634.                 for i=1, #buttons do
  635.                     if e[3] >= buttons[i][1] and e[4] >= buttons[i][2] and e[3] <= buttons[i][3] and e[4] <= buttons[i][4] then
  636.                         runProgram(apps[system.app_order[((apps.screen_length*apps.screen_height)*(apps.screen-1))+i]] or apps["invalid_app"])
  637.                         update = true
  638.                     end
  639.                 end
  640.             else
  641.                 if e2[3] >= e[3]+3 and apps.screen ~= 1 then -- Swipe Left
  642.                     apps.screen = apps.screen-1
  643.                     update = true
  644.                 elseif e2[3] <= e[3]-3 and apps.screen ~= math.ceil(#system.app_order/(apps.screen_length*apps.screen_height)) then -- Swipe Right
  645.                     apps.screen = apps.screen+1
  646.                     update = true
  647.                 end
  648.             end
  649.         end
  650.     end
  651. end
  652.  
  653. local function RESIZE()
  654.     while run do
  655.         resizing = true
  656.         local c = term.current()
  657.         term.redirect(term.native())
  658.         if {getSize()} ~= {w,h} then
  659.             w,h = getSize()
  660.             apps.screen_length, apps.screen_height = math.min(math.floor(w/apps.size[1]), apps.screen_length_limit), math.min(math.floor(h/apps.size[1]), apps.screen_height_limit) -- 17 because of text and space under app
  661.             apps.screen = math.min(apps.screen, math.ceil(#system.app_order/(apps.screen_length*apps.screen_height)))
  662.             app_screen.defaults, app_screen.extended = {
  663.                 x=1, y=1,
  664.                 w=w, h=math.floor(h/1.5)-6,
  665.             },{
  666.                 x=1, y=1,
  667.                 w=w, h=h-4,
  668.             }
  669.            
  670.             if app_screen.mode ~= 3 then -- not in use
  671.                 if app_screen.mode == 1 then -- Extended
  672.                     app_screen.display.reposition(app_screen.extended.x, app_screen.extended.y, app_screen.extended.w, app_screen.extended.h)
  673.                 else -- Default or not in use
  674.                     app_screen.display.reposition(app_screen.defaults.x, app_screen.defaults.y, app_screen.defaults.w, app_screen.defaults.h)
  675.                 end
  676.             end
  677.            
  678.            
  679.             main.reposition(1,1,w,h) -- REDO CURSOR BLINK FOR SHELL
  680.             main.redraw()
  681.             if app_screen.mode ~= 3 then drawControllerUI() end
  682.             app_screen.display.redraw()
  683.         end
  684.         term.redirect(c) resizing = false
  685.         os.pullEvent("term_resize")
  686.     end
  687. end
  688.  
  689. local r,e = initTasks(SCREEN, RESIZE)
  690.  
  691. main.setVisible(true)
  692. safeRedirect(main)
  693.  
  694. if not r then
  695.     sb(colors.cyan) st(colors.white) clr() cp(1,h/3) sleep(.5)
  696.     centerWrite("Mobile OS has Crashed!") cp(1,h/3+2)
  697.     centerWrite("=== Error Code ===") print("")
  698.     st(colors.red) centerWrite(e) st(colors.white) sleep(2) cp(1,h-2)
  699.     centerWrite("Tap the screen to continue...") os.pullEvent("mouse_click")
  700. end
  701.  
  702. st(colors.white) sb(colors.black) clr() cp(1,1)
Advertisement
Add Comment
Please, Sign In to add comment