Advertisement
Guest User

SimpleMenu implementation of Select Music Track by gir489

a guest
Mar 13th, 2014
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.84 KB | None | 0 0
  1. --[[ * SimpleMenu implementation of Select Music Track by gir489 v2.1
  2.      *
  3.      * Credits; Harfatus for SimpleMenu.
  4.      *
  5.      * Changelog:
  6.      *      v1: Initial release
  7.      *      v2: Fixed Music not being reset when chosen.
  8.      *      v2.1: Corrected list to sync with soundtrack instead of internal names.
  9.      *
  10.      *  Not for use in Pirate Perfagtion Trainer]]
  11.  
  12. if not SimpleMenu then
  13.     SimpleMenu = class()
  14.  
  15.     function SimpleMenu:init(title, message, options)
  16.         self.dialog_data = { title = title, text = message, button_list = {},
  17.                              id = tostring(math.random(0,0xFFFFFFFF)) }
  18.         self.visible = false
  19.         for _,opt in ipairs(options) do
  20.             local elem = {}
  21.             elem.text = opt.text
  22.             opt.data = opt.data or nil
  23.             opt.callback = opt.callback or nil
  24.             elem.callback_func = callback(self, self, "_do_callback",
  25.                                           { data = opt.data,
  26.                                             callback = opt.callback})
  27.             elem.cancel_button = opt.is_cancel_button or false
  28.             if opt.is_focused_button then
  29.                 self.dialog_data.focus_button = #self.dialog_data.button_list+1
  30.             end
  31.             table.insert(self.dialog_data.button_list, elem)
  32.         end
  33.         return self
  34.     end
  35.  
  36.     function SimpleMenu:_do_callback(info)
  37.         if info.callback then
  38.             if info.data then
  39.                 info.callback(info.data)
  40.             else
  41.                 info.callback()
  42.             end
  43.         end
  44.         self.visible = false
  45.     end
  46.  
  47.     function SimpleMenu:show()
  48.         if self.visible then
  49.             return
  50.         end
  51.         self.visible = true
  52.         managers.system_menu:show(self.dialog_data)
  53.     end
  54.  
  55.     function SimpleMenu:hide()
  56.         if self.visible then
  57.             managers.system_menu:close(self.dialog_data.id)
  58.             self.visible = false
  59.             return
  60.         end
  61.     end
  62. end
  63.  
  64. patched_update_input = patched_update_input or function (self, t, dt )
  65.     if self._data.no_buttons then
  66.         return
  67.     end
  68.    
  69.     local dir, move_time
  70.     local move = self._controller:get_input_axis( "menu_move" )
  71.  
  72.     if( self._controller:get_input_bool( "menu_down" )) then
  73.         dir = 1
  74.     elseif( self._controller:get_input_bool( "menu_up" )) then
  75.         dir = -1
  76.     end
  77.    
  78.     if dir == nil then
  79.         if move.y > self.MOVE_AXIS_LIMIT then
  80.             dir = 1
  81.         elseif move.y < -self.MOVE_AXIS_LIMIT then
  82.             dir = -1
  83.         end
  84.     end
  85.  
  86.     if dir ~= nil then
  87.         if( ( self._move_button_dir == dir ) and self._move_button_time and ( t < self._move_button_time + self.MOVE_AXIS_DELAY ) ) then
  88.             move_time = self._move_button_time or t
  89.         else
  90.             self._panel_script:change_focus_button( dir )
  91.             move_time = t
  92.         end
  93.     end
  94.  
  95.     self._move_button_dir = dir
  96.     self._move_button_time = move_time
  97.    
  98.     local scroll = self._controller:get_input_axis( "menu_scroll" )
  99.     -- local sdir
  100.     if( scroll.y > self.MOVE_AXIS_LIMIT ) then
  101.         self._panel_script:scroll_up()
  102.         -- sdir = 1
  103.     elseif( scroll.y < -self.MOVE_AXIS_LIMIT ) then
  104.         self._panel_script:scroll_down()
  105.         -- sdir = -1
  106.     end
  107. end
  108. managers.system_menu.DIALOG_CLASS.update_input = patched_update_input
  109. managers.system_menu.GENERIC_DIALOG_CLASS.update_input = patched_update_input
  110.  
  111. trackcallback = trackcallback or function(info)
  112.     if ( managers.player:player_unit() ) then
  113.         Global.music_manager.source:stop()
  114.     end
  115.     Global.music_manager.source:set_switch( "music_randomizer", info )
  116.     if ( managers.player:player_unit() ) then
  117.         Global.music_manager.source:post_event( Global.music_manager.current_event )
  118.     end
  119. end
  120.  
  121. opts = {
  122.     { text = "Time Window", callback = trackcallback, data = "track_08" },
  123.     { text = "Black Yellow Moebius", callback = trackcallback, data = "track_01" },
  124.     { text = "The Mark", callback = trackcallback, data = "track_05" },
  125.     { text = "Full Force Forward", callback = trackcallback, data = "track_02" },
  126.     { text = "Tick Tock", callback = trackcallback, data = "track_07" },
  127.     { text = "Fuse Box", callback = trackcallback, data = "track_03" },
  128.     { text = "Razormind", callback = trackcallback, data = "track_04" },
  129.     { text = "Calling All Units", callback = trackcallback, data = "track_06" },
  130.     { text = "Armed to the Teeth", callback = trackcallback, data = "track_09" },
  131.     { text = "Sirens in the Distance", callback = trackcallback, data = "track_10" },
  132.     { text = "Wanted Dead or Alive", callback = trackcallback, is_focused_button = true, data = "track_11" },
  133.     { text = "Death Wish", callback = trackcallback, data = "track_12" },
  134.     { text = "Close", is_cancel_button = true }
  135. }
  136. mymenu = SimpleMenu:new("Select Music Track", "Select the music track you want to play.", opts)
  137. mymenu:show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement