SuperMcBrent

app_settings

Aug 17th, 2025 (edited)
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.04 KB | Gaming | 0 0
  1. -- IDs for buttons
  2. local restartBtnId = "settings_restart"
  3. local yesBtnId = "confirm_yes"
  4. local noBtnId = "confirm_no"
  5.  
  6. local applicationName = "settings"
  7. local rootViewName = "root"
  8. local confirmViewName = "confirm"
  9.  
  10. -- views
  11. local function mainView(ctx)
  12.     return {
  13.         init = function()
  14.             local W, H = ctx.os.size()
  15.             local btnW, btnH = 13, 3
  16.             local x = math.floor((W - btnW) / 2)
  17.             local y = math.floor(H / 2)
  18.  
  19.             ctx.libs().button.create({
  20.                 app = applicationName,
  21.                 view = rootViewName,
  22.                 name = restartBtnId,
  23.                 x = x,
  24.                 y = y,
  25.                 w = btnW,
  26.                 h = btnH,
  27.                 colorOn = colors.red,
  28.                 colorOff = colors.gray,
  29.                 state = true,
  30.                 textOn = "Restart",
  31.                 textOff = "",
  32.                 textX = x + 3,
  33.                 textY = y + 1,
  34.                 header = ""
  35.             })
  36.         end,
  37.  
  38.         draw = function(mon)
  39.             ctx.libs().button.draw(restartBtnId, mon)
  40.         end,
  41.  
  42.         touch = function(x, y)
  43.             if ctx.libs().button.isWithinBoundingBox(x, y, restartBtnId) then
  44.                 ctx.os.navigate("settings", "confirm")
  45.             end
  46.         end
  47.     }
  48. end
  49.  
  50. local function confirmView(ctx)
  51.     return {
  52.         init = function()
  53.             local W, H = ctx.os.size()
  54.             local btnYesX = math.floor(W / 2) - 10
  55.             local btnNoX = math.floor(W / 2) + 3
  56.             local btnY = math.floor(H / 2) + 2
  57.  
  58.             ctx.libs().button.create({
  59.                 app = applicationName,
  60.                 view = confirmViewName,
  61.                 name = yesBtnId,
  62.                 x = btnYesX,
  63.                 y = btnY,
  64.                 w = 8,
  65.                 h = 3,
  66.                 colorOn = colors.lime,
  67.                 colorOff = colors.gray,
  68.                 state = true,
  69.                 textOn = "Yes",
  70.                 textX = btnYesX + 1,
  71.                 textY = btnY + 1
  72.             })
  73.  
  74.             ctx.libs().button.create({
  75.                 app = applicationName,
  76.                 view = confirmViewName,
  77.                 name = noBtnId,
  78.                 x = btnNoX,
  79.                 y = btnY,
  80.                 w = 8,
  81.                 h = 3,
  82.                 colorOn = colors.red,
  83.                 colorOff = colors.gray,
  84.                 state = true,
  85.                 textOn = "No",
  86.                 textX = btnNoX + 1,
  87.                 textY = btnY + 1
  88.             })
  89.         end,
  90.  
  91.         draw = function(mon)
  92.             local W, H = ctx.os.size()
  93.             ctx.libs().draw.drawTitle(1 + math.floor(W / 2) - 7, math.floor(H / 2) - 2, "Are you sure?", colors.white,
  94.                 colors.gray, mon)
  95.             ctx.libs().button.draw(yesBtnId, mon)
  96.             ctx.libs().button.draw(noBtnId, mon)
  97.         end,
  98.  
  99.         touch = function(x, y)
  100.             if ctx.libs().button.isWithinBoundingBox(x, y, yesBtnId) then
  101.                 os.reboot()
  102.             elseif ctx.libs().button.isWithinBoundingBox(x, y, noBtnId) then
  103.                 ctx.os.back()
  104.             end
  105.         end
  106.     }
  107. end
  108.  
  109. local views = {
  110.     root = mainView,
  111.     confirm = confirmView
  112. }
  113.  
  114. return {
  115.     id = "settings",
  116.     name = "Settings",
  117.     protocol = "settings",
  118.  
  119.     receive = function(ctx, sender, message) end,
  120.  
  121.     create = function(ctx)
  122.         for k, v in pairs(views) do
  123.             local view = v(ctx)
  124.             if view and view.init then
  125.                 view.init()
  126.             end
  127.         end
  128.     end,
  129.  
  130.     destroy = function(ctx) end,
  131.     resume = function(ctx) end,
  132.     suspend = function(ctx) end,
  133.     update = function(ctx, dt) end,
  134.  
  135.     draw = function(ctx, mon, viewId)
  136.         local v = views[viewId]
  137.         if v and v(ctx).draw then
  138.             v(ctx).draw(mon)
  139.         end
  140.     end,
  141.  
  142.     touch = function(ctx, x, y, viewId)
  143.         local v = views[viewId]
  144.         if v and v(ctx).touch then
  145.             v(ctx).touch(x, y)
  146.         end
  147.     end
  148. }
  149.  
Advertisement
Add Comment
Please, Sign In to add comment