Advertisement
ShicKla

PlayerV2

Apr 12th, 2022 (edited)
1,354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.82 KB | None | 0 0
  1. --[[ Program Info
  2.     Stargate Theme player
  3.     v 2.0
  4.     Created by: Fredyman_95 - Only stole others work
  5.                 Shickla - Developer, buttons, troubleshooting
  6.                 TRC - Developer, troubleshooting, tester
  7.  
  8.     System requirements:
  9.     Tier 3 GPU
  10.     Tier 3 Screen
  11.     Redstone IO
  12.  
  13.     Music disc change system requiremets:
  14.     EnderIO
  15.     Extra Utilities 2
  16.     Custom Records - not required (only for custom music discs)
  17.     --]]
  18.  
  19. --[[ TO-DO-LIST:
  20. - line numbers: 261; 393
  21. - Inspection by TRC, ShicKla or any other Lua expert
  22. - Logos
  23. - Graphic for playScreen()  => line 366
  24. - Add colors to logos on play screen
  25. - Add color to mainHeader on mainScreen
  26. - troubleshooting => fix of errors caused by Fredyman_95
  27. - troubleshooting Events
  28. - Set right values to SGUI => after Fredyman_95 prepare new sound file (MineDragonCZ_ request it)
  29. - Buttons not in screen functions
  30. --]]
  31.  
  32.  
  33. --
  34. local term = require("term")
  35. local component = require("component")
  36. local computer = require("computer")
  37. local event = require("event")
  38. local unicode = require("unicode")
  39. local colors = require("colors")
  40. local sides = require("sides")
  41.  
  42. local gpu = component.gpu
  43. local screen = component.screen
  44. local rs = component.redstone
  45. local creators = [[Augur ShicKla, TheRedCreations, Fredyman_95]]
  46. local version = [[1.2.0]]
  47.  
  48. local redColor = 0xAA0000
  49. local blueColor = 0x00B6FF
  50. local whiteColor = 0xFFFFFF
  51. local greyColor = 0xB4B4B4
  52.  
  53. -- Declarations --
  54. local ActiveButtons = {}
  55. local buttons = {}
  56. local User = ""
  57. local mainScreen = function() end
  58. local ErrorMessage = ""
  59. local StopPlaying = false
  60.  
  61. -- User settings
  62.  
  63. local song1 = sides.north
  64. local song2 = sides.south
  65. local song3 = sides.top
  66. local song4 = sides.west
  67. local stopSong = sides.bottom
  68.  
  69. local logoColor = 0xAA0000 --Default 0xAA0000 Red
  70.  
  71. local AdminList = {"Example_Name","Fredyman_95","ShicKla","TheRedCreations","MineDragonCZ_","Matousss"}
  72.  
  73. -- Changelog
  74.  
  75. local changelog = [[
  76.    version 1.2.0
  77.   - New source code (buttons, functions etc...)
  78.   - Loading screen
  79.   - Selection screen
  80.   - Theme screen with stop button
  81.   - Interuption ban for not authorised persons
  82.   - Added system requirements]]
  83.  
  84. -- Header + logo
  85.  
  86. -- FYI When using [[]] brackets to declare a string it will also include all leading spaces
  87. local mainHeader = [[
  88.    ████████╗██╗  ██╗███████╗███╗   ███╗███████╗    ██████╗ ██╗      █████╗ ██╗   ██╗███████╗██████╗
  89.    ╚══██╔══╝██║  ██║██╔════╝████╗ ████║██╔════╝    ██╔══██╗██║     ██╔══██╗╚██╗ ██╔╝██╔════╝██╔══██╗
  90.       ██║   ███████║█████╗  ██╔████╔██║█████╗      ██████╔╝██║     ███████║ ╚████╔╝ █████╗  ██████╔╝
  91.       ██║   ██╔══██║██╔══╝  ██║╚██╔╝██║██╔══╝      ██╔═══╝ ██║     ██╔══██║  ╚██╔╝  ██╔══╝  ██╔══██╗
  92.       ██║   ██║  ██║███████╗██║ ╚═╝ ██║███████╗    ██║     ███████╗██║  ██║   ██║   ███████╗██║  ██║
  93.       ╚═╝   ╚═╝  ╚═╝╚══════╝╚═╝     ╚═╝╚══════╝    ╚═╝     ╚══════╝╚═╝  ╚═╝   ╚═╝   ╚══════╝╚═╝  ╚═╝]]
  94.  
  95. local SGlogo = "WIP"
  96.  
  97. --[[      
  98.    local logoStargateTheme
  99.     -- add later - WIP
  100.    local logoAtlantisTheme
  101.     -- add later - WIP
  102.    local logoUniverseTheme
  103.     -- add later - WIP
  104.    local logoUniverseOpening
  105.     -- add later - WIP
  106. --]]
  107.  
  108. -- necessary stuff -
  109.  
  110. gpu.setResolution(104, 43)
  111. -- screen.setTouchModeInverted(true) -- Turn on touch screen!  
  112. -- term.setCursorBlink(false) -- You do not have to do this
  113. local mainloop = true
  114.  
  115.  -- restart of RedstoneIO -> outputs
  116. rs.setOutput(song1, 0)
  117. rs.setOutput(song2, 0)
  118. rs.setOutput(song3, 0)
  119. rs.setOutput(song4, 0)
  120. rs.setOutput(stopSong, 0)
  121.  
  122. -- System requirements check ---
  123. if gpu.maxResolution() < 100 then
  124.     io.stderr:write("Tier 3 GPU and Screen Required")
  125.     computer.beep()
  126.     os.sleep(0.2)
  127.     computer.beep()
  128.     os.exit(1)
  129. end
  130.  
  131. if not component.isAvailable("redstone") then
  132.     io.stderr:write("No RedstoneIO Connected.")
  133.     computer.beep()
  134.     os.sleep(0.2)
  135.     computer.beep()
  136.     os.sleep(0.2)
  137.     computer.beep()
  138.     os.exit(1)
  139. end
  140.  
  141. -- Buttons Objects === by Augur ShicKla ====
  142.  
  143. local Button = {}
  144. Button.__index = Button
  145. function Button.new(xPos, yPos, width, height, label, func, border)
  146.   local self = setmetatable({}, Button)
  147.   if xPos < 1 or xPos > term.window.width then xPos = 1 end
  148.   if yPos < 1 or yPos > term.window.height then yPos = 1 end
  149.   if (width-2) < unicode.len(label) then width = unicode.len(label)+2 end
  150.   if height < 3 then height = 3 end
  151.   if border == nil then
  152.     self.border = true
  153.   else
  154.     self.border = border
  155.   end
  156.   self.xPos = xPos
  157.   self.yPos = yPos
  158.   self.width = width
  159.   self.height = height
  160.   self.label = label
  161.   self.func = func
  162.   self.visible = false
  163.   self.disabled = false
  164.   return self
  165. end
  166.  
  167. function Button.display(self, x, y)
  168.   table.insert(ActiveButtons, 1, self)
  169.   if (self.width-2) < unicode.len(self.label) then self.width = unicode.len(self.label)+2 end
  170.   if x ~= nil and y ~= nil then
  171.     self.xPos = x
  172.     self.yPos = y
  173.   end
  174.   if self.border then
  175.     gpu.fill(self.xPos+1, self.yPos, self.width-2, 1, "─")
  176.     gpu.fill(self.xPos+1, self.yPos+self.height-1, self.width-2, 1, "─")
  177.     gpu.fill(self.xPos, self.yPos+1, 1, self.height-2, "│")
  178.     gpu.fill(self.xPos+self.width-1, self.yPos+1, 1, self.height-2, "│")
  179.     gpu.set(self.xPos, self.yPos, "┌")
  180.     gpu.set(self.xPos+self.width-1, self.yPos, "┐")
  181.     gpu.set(self.xPos, self.yPos+self.height-1, "└")
  182.     gpu.set(self.xPos+self.width-1, self.yPos+self.height-1, "┘")
  183.   end
  184.   gpu.set(self.xPos+1, self.yPos+1, self.label)
  185.   self.visible = true
  186. end
  187.  
  188. function Button.hide(self)
  189.   self.visible = false
  190.   for i,v in ipairs(ActiveButtons) do
  191.     if v == self then table.remove(ActiveButtons, i) end
  192.   end
  193.   if self.border then
  194.     gpu.fill(self.xPos, self.yPos, self.width, self.height, " ")
  195.   else
  196.     gpu.fill(self.xPos+1, self.yPos+1, self.width-2, 1, " ")
  197.   end
  198. end
  199.  
  200. function Button.disable(self, bool)
  201.   if bool == nil then
  202.     self.disabled = false
  203.   else
  204.     self.disabled = bool
  205.   end
  206.   if self.disabled then gpu.setForeground(0x0F0F0F) end
  207.   if self.visible then self:display() end
  208.   gpu.setForeground(0xFFFFFF)
  209. end
  210.  
  211. function Button.touch(self, x, y)
  212.   local wasTouched = false
  213.   if self.visible and not self.disabled then  
  214.     if self.border then
  215.       if x >= self.xPos and x <= (self.xPos+self.width-1) and y >= self.yPos and y <= (self.yPos+self.height-1) then wasTouched = true end
  216.     else
  217.       if x >= self.xPos+1 and x <= (self.xPos+self.width-2) and y >= self.yPos+1 and y <= (self.yPos+self.height-2) then wasTouched = true end
  218.     end
  219.   end
  220.   if wasTouched then
  221.     gpu.setBackground(0x878787)
  222.     gpu.set(self.xPos+1, self.yPos+1, self.label)
  223.     gpu.setBackground(0x000000)
  224.     if self.visible then gpu.set(self.xPos+1, self.yPos+1, self.label) end
  225.     self.func()
  226.   end
  227.   return wasTouched
  228. end
  229.  
  230. function Button.forceTouch(self)
  231.   self:touch(self.xPos+1, self.yPos+1)
  232. end
  233. -- End of Button --
  234.  
  235. local function isAuthorized(name)
  236.   local authorized = false
  237.   if #AdminList == 0 then
  238.     authorized = true
  239.   else
  240.     for _,v in ipairs(AdminList) do
  241.       if string.lower(v) == string.lower(name) then
  242.         authorized = true
  243.         break
  244.       end
  245.     end
  246.   end
  247.   return authorized
  248. end
  249.  
  250. --Events
  251. local Events = {}
  252. Events.key_down = event.listen("key_down", function(_, keyboardAddress, chr, code, playerName)
  253.   User = playerName
  254. end)
  255.  
  256. Events.key_up = event.listen("key_up", function(_, keyboardAddress, chr, code, playerName)
  257.   User = ""
  258. end)
  259.  
  260. Events.interrupted = event.listen("interrupted", function()
  261.   if isAuthorized(User) then
  262.     mainloop = false
  263.   end
  264.   -- local shouldExit = false
  265.   -- for i,v in ipairs(AdminList) do
  266.     -- if v == User then
  267.       -- shouldExit = true
  268.     -- end
  269.   -- end
  270.   -- if shouldExit then
  271.     -- MainLoop = false
  272.   -- end
  273. end)
  274.  
  275. Events.touch = event.listen("touch", function(_, screenAddress, x, y, button, playerName)
  276.   local success, msg = xpcall(function()
  277.     if button == 0 then
  278.       for i,v in ipairs(ActiveButtons) do
  279.         if v:touch(x,y) then break end
  280.       end
  281.     end
  282.   end, debug.traceback)
  283.   if not success then
  284.     ErrorMessage = msg
  285.     mainloop = false
  286.   end
  287. end)
  288.  
  289.  
  290. -- Functions
  291.  
  292. local function screenClear()
  293.   buttons.SG1:hide()
  294.   buttons.SGA:hide()
  295.   buttons.SGUT:hide()
  296.   buttons.SGUO:hide()
  297.   buttons.STOP:hide()
  298.   -- You do not have to disable if the button is hidden
  299.   -- buttons.SG1:disable(true)
  300.   -- buttons.SGA:disable(true)
  301.   -- buttons.SGUT:disable(true)
  302.   -- buttons.SGUO:disable(true)
  303.   -- buttons.STOP:disable(true)
  304.   term.clear()
  305. end
  306.  
  307. local function screenHeader(ScreenLogoColor)
  308.   term.setCursor(1,2)
  309.   gpu.setForeground(ScreenLogoColor)
  310.   print(mainHeader)
  311.   gpu.setForeground(whiteColor)
  312.   -- os.sleep(0.1)
  313. end
  314.  
  315. local function clickPulse(timer)
  316.   local num1 = 0
  317.   while (num1 < timer + 0.1) do  
  318.     -- click (touch) event
  319.     os.sleep(0.1)
  320.     num1 = num1 + 0.1
  321.   end
  322. end    
  323.  
  324. local function dots(posX, posY)
  325.   term.setCursor(posX, posY)
  326.   print(".")
  327.   posX = posX + 1
  328.   clickPulse(0.5)
  329.   term.setCursor(posX, posY)
  330.   print(".")
  331.   posX = posX + 1
  332.   clickPulse(0.5)
  333.   term.setCursor(posX, posY)
  334.   print(".")
  335.   clickPulse(0.5)
  336.   posX = posX - 2
  337.   term.setCursor(posX, posY)
  338.   print("   ")
  339.   clickPulse(0.5)
  340. end
  341.  
  342. local function removeDisc(pause4)
  343.   rs.setOutput(stopSong, 15)
  344.   os.sleep(pause4)
  345.   rs.setOutput(stopSong, 0)
  346. end
  347.  
  348. local function jukebox(sideDisc, pause, pause2)
  349.   removeDisc(0.5)
  350.   os.sleep(pause)
  351.   rs.setOutput(sideDisc, 15)
  352.   os.sleep(pause2)
  353.   rs.setOutput(sideDisc, 0)
  354. end            
  355.  
  356. -- Screens
  357.  
  358. local function loadingScreen(bootTime, pause3)
  359.   screenClear()
  360.   screenHeader(logoColor)
  361.   computer.beep()
  362.   -- term.setCursor(45, 8) -- Old
  363.   -- os.sleep(0.5) -- Why?
  364.   -- print("Version:") -- Old
  365.   gpu.set(45, 8, "Version:") -- New
  366.   -- term.setCursor(54, 8) -- Old
  367.   -- print(version)
  368.   gpu.set(54, 8, version) -- New
  369.   -- term.setCursor(15, 2) -- Old
  370.   -- os.sleep(1) -- Why?
  371.   -- print("Created by:") -- Old
  372.   gpu.set(2, 9, "Created by:") -- New
  373.   -- term.setCursor(16, 2) -- Old
  374.   -- os.sleep(0.5) -- Why?
  375.   -- print(creators) -- Old
  376.   gpu.set(2, 10, creators) -- New
  377.   os.sleep(pause3) -- Why?
  378.   -- term.setCursor(20,2) -- Old
  379.   term.setCursor(2, 12) -- New
  380.   print("Changelog:")
  381.   print(changelog)
  382.   local num0 = bootTime - pause3 - 2
  383.   os.sleep(num0)
  384. end
  385.  
  386. function mainScreen()
  387.   screenClear()
  388.   screenHeader(logoColor)
  389.   buttons.SG1:display()
  390.   buttons.SGA:display()
  391.   buttons.SGUT:display()
  392.   buttons.SGUO:display()
  393.   buttons.STOP:display()
  394.   -- You do not have to re-enable the button if you never disable it in the first place ;)
  395.   -- buttons.SG1:disable(false)
  396.   -- buttons.SGA:disable(false)
  397.   -- buttons.SGUT:disable(false)
  398.   -- buttons.SGUO:disable(false)
  399.   -- buttons.STOP:disable(false)
  400. end
  401.  
  402. local function playScreen(line1, line2, duration)
  403.   screenClear()
  404.   screenHeader(greyColor)
  405.   term.setCursor(10, 20) -- need to be set to right position
  406.   print("Now is playing:")
  407.   term.setCursor(10, 22) -- need to be set to right position
  408.   print(SGlogo)
  409.   term.setCursor(10, 23) -- need to be set to right position
  410.   print(line1)
  411.   term.setCursor(10, 24) -- need to be set to right position
  412.   print(line2)
  413.   buttons.STOP:display()
  414.   -- buttons.STOP:disable(false)
  415.   -- local num3 = 0
  416.   -- while (num3 < duration + 1) do
  417.       -- dots("10", "10") --need to be set to right position
  418.       -- num3 = num3 + 1
  419.   -- end
  420.   StopPlaying = false
  421.   for i=0,duration,1 do
  422.     dots(10, 10) --need to be set to right position
  423.     os.sleep(1)
  424.     if StopPlaying then break end
  425.   end
  426.   mainScreen()
  427. end
  428.  
  429. -- buttons (xPos, yPos, width, height, label, func, border)
  430. buttons = {
  431.     SG1 = Button.new(4, 10, 96, 5, "Stargate SG-1 Theme", function()
  432.         jukebox(song1, 1, 0.5)
  433.         playScreen("Stargate SG-1", "THEME" , 59)
  434.     end),
  435.  
  436.     SGA = Button.new(4, 16, 96, 5, "Stargate Atlantis Theme", function()
  437.         jukebox(song2, 1, 0.5)
  438.         playScreen("Stargate Atlantis", "THEME" , 61)
  439.     end),
  440.  
  441.     SGUT = Button.new(4, 22, 96, 5, "Stargate Universe Theme", function()
  442.         jukebox(song3, 1, 0.5)
  443.         playScreen("Stargate Universe", "THEME" , 51)
  444.     end),
  445.  
  446.     SGUO = Button.new(4, 28, 96, 5, "Stargate Universe - Gauntlet", function()
  447.         jukebox(song4, 1, 0.5)
  448.         playScreen("Stargate Universe", "Gauntlet" , 51)
  449.     end),
  450.  
  451.     STOP = Button.new(4, 37, 96, 5, "STOP", function()
  452.         removeDisc(0.5)
  453.         StopPlaying = true
  454.         -- mainScreen()
  455.     end),
  456. }      
  457.  
  458. -- Program
  459.  
  460. loadingScreen(10, 1)
  461. mainScreen()
  462.  
  463. while mainloop do
  464.   -- click (touch) event
  465.   os.sleep(0.1)
  466. end
  467.  
  468. -- maybe all must be in loop ... hmmm
  469.  
  470.  
  471. --[[ Notes
  472.  
  473. Old buttons API if Fredymna_95 didn't understand Ausgur's buttons => chances that he will understand 1: 10e99999 xD
  474.  
  475. 4, 100, 10, 15
  476. 4, 100, 19, 24
  477. 4, 100, 28, 33
  478. 4, 100, 37, 42
  479.  
  480. --]]
  481.  
  482. term.clear()
  483. for k,v in pairs(Events) do
  484.   print("Canceling Event Listener: "..k) -- For debug
  485.   event.cancel(v)
  486. end
  487. io.stderr:write(ErrorMessage.."\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement