Advertisement
ForeverZer0

[RMXP] Scene_Audio

Sep 2nd, 2012
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 14.58 KB | None | 0 0
  1. #+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
  2. # Scene Audio
  3. # Author: ForeverZer0
  4. # Version: 1.0
  5. # Date: 9.2.2012
  6. #+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
  7. #
  8. # This script is for allowing the player to sample and play the game's audio.
  9. # It has control for allowing adjustement of volume and pitch. All local and RTP
  10. # resources are automatically found and categorized by type.
  11. #
  12. #   To call the scene:  $scene = Scene_Audio.new
  13. #
  14. #+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
  15.  
  16. #===============================================================================
  17. # ** Resources
  18. #===============================================================================
  19.  
  20. module Resources
  21.   #-----------------------------------------------------------------------------
  22.   # * Constants
  23.   #-----------------------------------------------------------------------------
  24.   AUDIO_EXT = ['.mp3', '.ogg', '.wav', '.mid', '.wma']
  25.   GRAPHIC_EXT = ['.png', '.jpg', '.bmp']
  26.   #-----------------------------------------------------------------------------
  27.   # * Gets an array of the game's RTP paths
  28.   #-----------------------------------------------------------------------------
  29.   def self.rtp_paths
  30.     ini = Win32API.new('kernel32', 'GetPrivateProfileStringA', 'PPPPLP', 'L')
  31.     library = "\0" * 256
  32.     ini.call('Game', 'Library', '', library, 256, '.\\Game.ini')
  33.     library.delete!("\0")
  34.     rtp_path = Win32API.new(library, 'RGSSGetRTPPath', 'L', 'L')
  35.     path_with_rtp = Win32API.new(library, 'RGSSGetPathWithRTP', 'L', 'P')
  36.     paths = [1, 2, 3].collect {|id| path_with_rtp.call(rtp_path.call(id)) }
  37.     return paths.find_all {|path| path != '' && File.directory?(path) }
  38.   end
  39.   #-----------------------------------------------------------------------------
  40.   # * Gets a list of files found in the RTP directories
  41.   #-----------------------------------------------------------------------------
  42.   def self.rtp_files
  43.     if @rtp_files == nil
  44.       @rtp_files = []
  45.       self.rtp_paths.each {|rtp| @rtp_files += Dir.glob(rtp + '/**/*') }
  46.     end
  47.     return @rtp_files
  48.   end
  49.   #-----------------------------------------------------------------------------
  50.   # * Gets a list of files found in the local directories
  51.   #-----------------------------------------------------------------------------
  52.   def self.local_files
  53.     if @local_files == nil
  54.       @local_files = Dir.glob('./**/*')
  55.     end
  56.     return @local_files
  57.   end
  58.   #-----------------------------------------------------------------------------
  59.   # * Gets a list of all audio files found locally and in the RTPs
  60.   #-----------------------------------------------------------------------------
  61.   def self.audio_files
  62.     files = rtp_files.find_all {|f| AUDIO_EXT.include?(File.extname(f)) }
  63.     files += local_files.find_all {|f| AUDIO_EXT.include?(File.extname(f))}
  64.     return files
  65.   end
  66.   #-----------------------------------------------------------------------------
  67.   # * Gets a list of all graphic files found locally and in the RTPs
  68.   #-----------------------------------------------------------------------------
  69.   def self.graphic_files
  70.     files = rtp_files.find_all {|f| GRAPHIC_EXT.include?(File.extname(f)) }
  71.     files += local_files.find_all {|f| GRAPHIC_EXT.include?(File.extname(f))}
  72.     return files
  73.   end
  74. end
  75.  
  76. #===============================================================================
  77. # ** Window_AudioOptions
  78. #===============================================================================
  79.  
  80. class Window_AudioOptions < Window_Selectable
  81.   #-----------------------------------------------------------------------------
  82.   # * Object Initialization
  83.   #-----------------------------------------------------------------------------
  84.   def initialize
  85.     super(320, 128, 320, 352)
  86.     @column_max = 1
  87.     @commands = ['BGM Volume', 'BGS Volume', 'ME Volume', 'SE Volume',
  88.       'BGM Pitch', 'BGS Pitch', 'ME Pitch', 'SE Pitch']
  89.     @params = [100, 80, 100, 80, 100, 100, 100, 100]
  90.     @item_max = @commands.size
  91.     refresh
  92.     self.active = false
  93.     self.index = -1
  94.   end
  95.   #-----------------------------------------------------------------------------
  96.   # * Clear and redraw the window contents
  97.   #-----------------------------------------------------------------------------
  98.   def refresh
  99.     if self.contents != nil
  100.       self.contents = self.contents.dispose
  101.     end
  102.     self.contents = Bitmap.new(width - 32, @commands.size * 32)
  103.     @commands.each_index {|i| draw_item(i) }
  104.   end
  105.   #-----------------------------------------------------------------------------
  106.   # * Draw an item on the window
  107.   #-----------------------------------------------------------------------------
  108.   def draw_item(i)
  109.     rect = Rect.new(0, i * 32, self.contents.width, 32)
  110.     self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
  111.     self.contents.font.color = system_color
  112.     self.contents.draw_text(4, i * 32, 280, 32, @commands[i])
  113.     self.contents.font.color = normal_color
  114.     self.contents.draw_text(4, i * 32, 280, 32, @params[i].to_s, 2)
  115.   end
  116.   #-----------------------------------------------------------------------------
  117.   # * Get the volume for the given category
  118.   #-----------------------------------------------------------------------------
  119.   def volume(category)
  120.     return @params[category]
  121.   end
  122.   #-----------------------------------------------------------------------------
  123.   # * Get the pitch for the given category
  124.   #-----------------------------------------------------------------------------
  125.   def pitch(category)
  126.     return @params[category + 4]
  127.   end
  128.   #-----------------------------------------------------------------------------
  129.   # * Frame Update
  130.   #-----------------------------------------------------------------------------
  131.   def update
  132.     super
  133.     mod = nil
  134.     mod = -5 if Input.repeat?(Input::LEFT)
  135.     mod = -1 if Input.trigger?(Input::LEFT)
  136.     mod = 5 if Input.repeat?(Input::RIGHT)
  137.     mod = 1 if Input.trigger?(Input::RIGHT)
  138.     if mod != nil
  139.       i = self.index
  140.       if self.index < 4
  141.         @params[i] = [[@params[i] + mod, 100].min, 0].max
  142.       else
  143.         @params[i] = [[@params[i] + mod, 150].min, 50].max
  144.       end
  145.       $game_system.se_play($data_system.cursor_se)
  146.       draw_item(i)
  147.     end
  148.   end
  149.   #-----------------------------------------------------------------------------
  150.   # * Update Help Text
  151.   #-----------------------------------------------------------------------------
  152.   def update_help
  153.     if @help_window != nil
  154.       @help_window.set_text('Use LEFT and RIGHT to change values')
  155.     end
  156.   end
  157. end
  158.  
  159. #===============================================================================
  160. # ** Window_AudioCommand
  161. #===============================================================================
  162.  
  163. class Window_AudioCommand < Window_Selectable
  164.   #-----------------------------------------------------------------------------
  165.   # * Object Initialization
  166.   #-----------------------------------------------------------------------------
  167.   def initialize
  168.     super(0, 64, 640, 64)
  169.     self.contents = Bitmap.new(width - 32, height - 32)
  170.     @item_max = 5
  171.     @column_max = 5
  172.     @commands = ['BGM', 'BGS', 'ME', 'SE', 'Options']
  173.     refresh
  174.     self.index = 0
  175.   end
  176.   #-----------------------------------------------------------------------------
  177.   # * Clear and redraw the window contents
  178.   #-----------------------------------------------------------------------------
  179.   def refresh
  180.     self.contents.clear
  181.     for i in 0...@item_max
  182.       draw_item(i)
  183.     end
  184.   end
  185.   #-----------------------------------------------------------------------------
  186.   # * Draw an item on the window
  187.   #-----------------------------------------------------------------------------
  188.   def draw_item(index)
  189.     x = 4 + index * 128
  190.     self.contents.draw_text(x, 0, 96, 32, @commands[index])
  191.   end
  192.   #-----------------------------------------------------------------------------
  193.   # * Update the help text
  194.   #-----------------------------------------------------------------------------
  195.   def update_help
  196.     text = case self.index
  197.     when 0 then 'Background Music'
  198.     when 1 then 'Background Sounds'
  199.     when 2 then 'Musical Effects'
  200.     when 3 then 'Sound Effects'
  201.     when 4 then 'Change Audio Options'
  202.     else ''
  203.     end
  204.     @help_window.set_text(text)
  205.   end
  206. end
  207.  
  208. #===============================================================================
  209. # ** Window_Audio
  210. #===============================================================================
  211.  
  212. class Window_Audio < Window_Selectable
  213.   #-----------------------------------------------------------------------------
  214.   # * Object Initialization
  215.   #-----------------------------------------------------------------------------
  216.   def initialize(filenames)
  217.     super(0, 128, 320, 352)
  218.     @filenames = filenames
  219.     @current_files = []
  220.     self.index = -1
  221.     self.active = false
  222.     change_category(0)
  223.   end
  224.   #-----------------------------------------------------------------------------
  225.   # * Clear and redraw the window contents
  226.   #-----------------------------------------------------------------------------
  227.   def refresh
  228.     self.contents = Bitmap.new(width - 32, @current_files.size * 32)
  229.     @item_max = @current_files.size
  230.     @current_files.each_index {|i|
  231.       file = File.basename(@current_files[i], File.extname(@current_files[i]))
  232.       self.contents.draw_text(4, i * 32, 288, 32, file)
  233.     }
  234.   end
  235.   #-----------------------------------------------------------------------------
  236.   # * Changes the audio type to list and refresh
  237.   #-----------------------------------------------------------------------------
  238.   def change_category(category)
  239.     @subfolder = case category
  240.     when 0 then 'Audio/BGM'
  241.     when 1 then 'Audio/BGS'
  242.     when 2 then 'Audio/ME'
  243.     when 3 then 'Audio/SE'
  244.     else ''
  245.     end
  246.     if category == 4
  247.       self.contents.clear
  248.       return
  249.     end
  250.     @current_files = @filenames.find_all {|f| f.include?(@subfolder) }
  251.     refresh
  252.   end
  253.   #-----------------------------------------------------------------------------
  254.   # * Get the currently selected filename
  255.   #-----------------------------------------------------------------------------
  256.   def audio_file
  257.     if @subfolder == ''
  258.       return ''
  259.     end
  260.     return @current_files[self.index]
  261.   end
  262.  
  263.   def update_help
  264.     if audio_file == ''
  265.       @help_window.set_text('')
  266.       return
  267.     end
  268.     @help_window.set_text(@subfolder + '/' + File.basename(audio_file))
  269.   end
  270. end
  271.  
  272.  
  273. #===============================================================================
  274. # ** Scene_Audio
  275. #===============================================================================
  276.  
  277. class Scene_Audio
  278.   #-----------------------------------------------------------------------------
  279.   # * Clear and redraw the window contents
  280.   #-----------------------------------------------------------------------------
  281.   def main
  282.     $game_system.bgm_memorize
  283.     $game_system.bgs_memorize
  284.     $game_system.bgm_stop
  285.     $game_system.bgs_stop
  286.     @help_window = Window_Help.new
  287.     @command_window = Window_AudioCommand.new
  288.     @command_window.active = true
  289.     @command_window.help_window = @help_window
  290.     @audio_window = Window_Audio.new(Resources.audio_files)
  291.     @audio_window.help_window = @help_window
  292.     @options_window = Window_AudioOptions.new
  293.     @options_window.help_window = @help_window
  294.     Graphics.transition
  295.     loop { Graphics.update; Input.update; update; break if $scene != self }
  296.     Graphics.freeze
  297.     @audio_window.dispose
  298.     @command_window.dispose
  299.     @options_window.dispose
  300.     @help_window.dispose
  301.     $game_system.bgm_restore
  302.     $game_system.bgs_restore
  303.   end
  304.   #-----------------------------------------------------------------------------
  305.   # * Get the current category index
  306.   #-----------------------------------------------------------------------------
  307.   def category
  308.     return @command_window.index
  309.   end
  310.   #-----------------------------------------------------------------------------
  311.   # * Frame Update
  312.   #-----------------------------------------------------------------------------
  313.   def update
  314.     @help_window.update
  315.     if @command_window.active
  316.       update_command
  317.       return
  318.     elsif @audio_window.active
  319.       update_audio
  320.       return
  321.     elsif @options_window.active
  322.       update_options
  323.       return
  324.     end
  325.   end
  326.   #-----------------------------------------------------------------------------
  327.   # * Update the command window
  328.   #-----------------------------------------------------------------------------
  329.   def update_command
  330.     @command_window.update
  331.     if Input.trigger?(Input::B)
  332.       $game_system.se_play($data_system.cancel_se)
  333.       $scene = Scene_Map.new
  334.     elsif Input.trigger?(Input::C)
  335.       $game_system.se_play($data_system.decision_se)
  336.       @command_window.active = false
  337.       if category == 4
  338.         @options_window.active = true
  339.         @options_window.index = 0
  340.       else
  341.         @audio_window.active = true
  342.         @audio_window.index = 0
  343.       end
  344.     elsif Input.trigger?(Input::LEFT) || Input.trigger?(Input::RIGHT) ||
  345.       Input.trigger?(Input::L) || Input.trigger?(Input::R)
  346.       @audio_window.change_category(category)
  347.     end
  348.   end
  349.   #-----------------------------------------------------------------------------
  350.   # * Update the audio selection window
  351.   #-----------------------------------------------------------------------------
  352.   def update_audio
  353.     @audio_window.update
  354.     if Input.trigger?(Input::B)
  355.       @audio_window.active = false
  356.       @audio_window.index = -1
  357.       @command_window.active = true
  358.       $game_system.se_play($data_system.cancel_se)
  359.     elsif Input.trigger?(Input::C)
  360.       volume = @options_window.volume(category)
  361.       pitch = @options_window.pitch(category)
  362.       file = @audio_window.audio_file
  363.       case category
  364.       when 0 then Audio.bgm_play(file, volume, pitch)
  365.       when 1 then Audio.bgs_play(file, volume, pitch)
  366.       when 2 then Audio.me_play(file, volume, pitch)
  367.       when 3 then Audio.se_play(file, volume, pitch)
  368.       end
  369.     end
  370.   end
  371.   #-----------------------------------------------------------------------------
  372.   # * Update the audio options window
  373.   #-----------------------------------------------------------------------------
  374.   def update_options
  375.     @options_window.update
  376.     if Input.trigger?(Input::B)
  377.       @options_window.active = false
  378.       @options_window.index = -1
  379.       @command_window.active = true
  380.       $game_system.se_play($data_system.cancel_se)
  381.     end
  382.   end
  383. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement