#==============================================================================
# [VX] < Sound Test > by badnomis
#------------------------------------------------------------------------------
# ? by badnomis [
badnomis@hotmail.com]
# ? Released on: 17/02/2009 (D-M-Y)
# ? Version: 1.1
#---------------------------------------------------------------------------
#+++ [FEATURES] ++++
# - Reintegrates bgm and bgs memorize function from RPGXP (not used for the vehicles)
#   + Audio.bgm_memorize (for memorize BGM)
#   + Audio.bgs_memorize (for memorize BGS)
#   + Audio.bgm_and_bgs_memorize (for memorize BGM and BGS)
#   + Audio.play_music_memorize (for play memorize music)
# - Automatically adds the music in the database as soon as the music is played
# - Compatible with a different size screen
# - The possibility to know if the actors has all sounds (with a conditions)
#------------------------------------------------------------------------------
#### Instruction ####
# For call Sound Test script, write in event, Event Commands, third page,
# Script... (in Advanced) :
# $scene = Scene_SoundTest.new
#
# For put a condition for know if you have all sounds or not, #have_all_sounds?
#  1. Go on event
#  2. Put a condition branch where you want it
#  3. In condion, go to page 4 and check script box
#  4. Write in box :
#     have_all_sounds?
#  5. The first branch => if you have all sounds
#     The second branch => if you not have all sounds
#
# -The buttons in Sound Test-
#  + Space (C) : To choose music
#  + Shift (A) : To stop music
#  + ESC (A) : To quit Sound Test
#------------------------------------------------------------------------------
#==============================================================================
# ** Data SoundTest
#------------------------------------------------------------------------------
#  Configure the factors and details of sound.
#------------------------------------------------------------------------------
#  [" A "," B ", C , D ] 
#  Exemple : ["Main Title", "Town5"] or ["Main Title", "Town5", 75, 150]
#  A = The name of the music that will be show in the Sound Test list {text}
#  B = The exact name of music file (without extansion) {text}
#  C = The volume of the music that is played in the Sound Test {number} (OPTIONAL)
#  D = The pitch of the music that is played in the Sound Test {number} (OPTIONAL)
#==============================================================================
def sound_list
  # [" Name "," filename ", Volume , Pitch ]
  data = [
    # Sound list
    ["Main Title", "Town5"],
    ["World Map", "Field1"],
    ["Normal Battle", "Battle1"],
    ["Sad Story", "Scene3"]
    ]
  return data
end
#==============================================================================
# ** Audio
#------------------------------------------------------------------------------
#  This module add audio functions and modifies existing functions.
#------------------------------------------------------------------------------
#  (-) = Modified Function
#  (+) = New Function
#==============================================================================
module Audio
  #--------------------------------------------------------------------------
  # * Memorize BGM (+)
  #--------------------------------------------------------------------------
  def self.bgm_memorize
    @memorized_bgm = @playing_bgm
  end  
  #--------------------------------------------------------------------------
  # * Memorize BGS (+)
  #--------------------------------------------------------------------------
  def self.bgs_memorize
    @memorized_bgs = @playing_bgs
  end  
  #--------------------------------------------------------------------------
  # * Memorize BGM and BGS (+)
  #--------------------------------------------------------------------------
  def self.bgm_and_bgs_memorize
    self.bgm_memorize
    self.bgs_memorize
  end
  #--------------------------------------------------------------------------
  # * Play Memorize BGM (+)
  #--------------------------------------------------------------------------
  def self.play_music_memorize
    Audio.bgm_play(@memorized_bgm.name, @memorized_bgm.volume,
    @memorized_bgm.pitch) if @memorized_bgm != nil
    Audio.bgs_play(@memorized_bgs.name, @memorized_bgs.volume,
    @memorized_bgs.pitch) if @memorized_bgs != nil
  end  
  #--------------------------------------------------------------------------
  # * Return Playing BGM (+)
  #--------------------------------------------------------------------------
  def self.playing_bgm
    return @playing_bgm
  end  
  #--------------------------------------------------------------------------
  # * Play Memorize BGM (-)
  #--------------------------------------------------------------------------
  # Module to class
  class << self
  # Fix a Stack Level too deep problems
  if @Audio.nil?
    # Renames bgm and bgs functions
    alias bgm_play_alias bgm_play
    alias bgm_stop_alias bgm_stop
    alias bgm_fade_alias bgm_fade
    alias bgs_play_alias bgs_play
    alias bgs_stop_alias bgs_stop
    alias bgs_fade_alias bgs_fade
    @Audio = true
  end
    #-----------BGM-----------
    # Defines a new bgm_play
    def Audio.bgm_play(*arg)
      # Run the basic method
      bgm_play_alias(*arg)
      @playing_bgm = RPG::AudioFile.new(*arg)
      name = @playing_bgm.name.gsub(/Audio\/BGM\//) {""}
      $game_party.played_bgm[name] = true
    end
    # Defines a new bgm_stop
    def Audio.bgm_stop
      # Run the basic method
      bgm_stop_alias
      @playing_bgm = nil
    end
    # Defines a new bgm_fade
    def Audio.bgm_fade(arg)
      # Run the basic method
      bgm_fade_alias(arg)
      @playing_bgm = nil
    end
    #-----------BGS-----------
    def Audio.bgs_play(*arg)
      # Run the basic method
      bgs_play_alias(*arg)
      @playing_bgs = RPG::AudioFile.new(*arg)
    end
    # Defines a new bgm_stop
    def Audio.bgs_stop
      # Run the basic method
      bgs_stop_alias
      @playing_bgs = nil
    end
    # Defines a new bgm_fade
    def Audio.bgs_fade(arg)
      # Run the basic method
      bgs_fade_alias(arg)
      @playing_bgs = nil
    end    
  end
end
#==============================================================================
# ** Game_Party
#==============================================================================
class Game_Party
  attr_accessor :played_bgm
  alias initialize_game_party_alias initialize
  def initialize
    initialize_game_party_alias
    @played_bgm = {}
  end
end
#==============================================================================
# ** Game_Interpreter
#==============================================================================
class Game_Interpreter
  def have_all_sounds?
    for i in 0...sound_list.size
      data = sound_list
      soundsfiles = 0 if soundsfiles == nil
      soundsfiles += 1 if $game_party.played_bgm[data[1]]
    end
    return true if soundsfiles == sound_list.size
    return false if soundsfiles != sound_list.size
  end
end
#==============================================================================
# ** Window_SoundTest
#------------------------------------------------------------------------------
#  This window displays a list of sounds
#==============================================================================
class Window_SoundTest < Window_Selectable
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     x      : window x-coordinate
  #     y      : window y-coordinate
  #     width  : window width
  #     height : window height
  #--------------------------------------------------------------------------
  def initialize(x, y, width, height)
    super(x, y, width, height)
    @column_max = 1
    @width = width
    self.index = 0
    refresh
  end
  #--------------------------------------------------------------------------
  # * Get Sound
  #--------------------------------------------------------------------------
  def sound
    return @data[self.index]
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    @data = sound_list
    @item_max = @data.size
    create_contents
    for i in 0...@item_max
      draw_item(i)
    end
  end
  #--------------------------------------------------------------------------
  # * Draw Sound
  #     index : sound number
  #--------------------------------------------------------------------------
  def draw_item(index)
    sound = @data[index]
    rect = item_rect(index)
    if $game_party.played_bgm[sound[1]]
      self.contents.font.color = normal_color
      self.contents.draw_text(rect, sound[0], 1)
    else
      self.contents.font.color = text_color(7)
      self.contents.draw_text(rect, "? ? ? ? ? ? ? ?", 1)
    end
  end
end
#==============================================================================
# ** Scene_SoundTest
#------------------------------------------------------------------------------
#  SoundTest Scene
#==============================================================================
class Scene_SoundTest < Scene_Base
  #--------------------------------------------------------------------------
  # * Start processing
  #--------------------------------------------------------------------------
  def start
    super
    Audio.bgm_and_bgs_memorize
    Audio.bgm_stop
    Audio.bgs_stop
    create_menu_background
    @help_window = Window_Base.new(Graphics.width/2, 0, Graphics.width/2, 24+32)
    @help_window.contents = Bitmap.new(Graphics.width/2-32, 24)
    set_text(@help_window, "- None -", 1)
    @complete_window = Window_Base.new(Graphics.width/2,
    Graphics.height - (24+32), Graphics.width/2, 24+32)
    @complete_window.contents = Bitmap.new(Graphics.width/2-32, 24)
    @pi = 3.14159265
    @cd = Sprite.new
    @cd.bitmap = Cache.picture("CD")
    @cd.ox = @cd.bitmap.width/2
    @cd.oy = @cd.bitmap.height/2
    @cd.zoom_x = 0
    @cd.y = Graphics.height/2 - @cd.bitmap.height/2 + 120
    @zoom_cd = 0
    @turn_cd = 0
    @cd_play = false
    @sound_window = Window_SoundTest.new(0, 0, Graphics.width/2, Graphics.height)
    #=Calcule % of the sound=
    @pc_complete = 0
    for i in 0...sound_list.size
    data = sound_list
    @pc_complete += 1 if $game_party.played_bgm[data[1]]
    end
    text = @pc_complete * 100 / sound_list.size
    text = "Complete: " + text.to_s + "%"
    set_text(@complete_window, text, 1)
    #========================
  end
  #--------------------------------------------------------------------------
  # * Termination Processing
  #--------------------------------------------------------------------------
  def terminate
    super
    dispose_menu_background
    @help_window.dispose
    @complete_window.dispose
    @cd.dispose
    @sound_window.dispose
  end
  #--------------------------------------------------------------------------
  # * Return to Original Screen
  #--------------------------------------------------------------------------
  def return_scene
    $scene = Scene_Map.new
  end
  #--------------------------------------------------------------------------
  # * Set_text
  #--------------------------------------------------------------------------
  def set_text(window, text, align = 0)
    window.contents.clear
    window.contents.draw_text(0,0, window.width - 32, 24, text, align)
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    super
    update_menu_background
    @help_window.update
    @complete_window.update
    @cd.update
    @zoom_cd += 1
    if @cd_play == true
    @turn_cd += 14
    @cd.zoom_x = 1
    @cd.angle = @turn_cd
    else
    @cd.angle = 0
    @turn_cd = 0
    @cd.zoom_x = (1 * Math.sin(@zoom_cd/120.0*@pi)).abs
    @cd.x = (Graphics.width/1.33) - @cd.bitmap.width/2 * (Math.sin(@zoom_cd/@cd.bitmap.width/2*@pi).abs)
    end
    @sound_window.update
    # If input B
    if Input.trigger?(Input::
      Sound.play_cancel
      Audio.bgm_stop
      Audio.play_music_memorize
      return_scene
    end
    # If input A
    if Input.trigger?(Input::A)
      # Play SE (Decision)
      Sound.play_decision
      # Stop BGM
      Audio.bgm_stop
      @cd_play = false
      set_text(@help_window, "- None -", 1)
    end
    # If input C
    if Input.trigger?(Input::C)
      sound = @sound_window.sound
      # If not playing
      if !$game_party.played_bgm[sound[1]]
        # SE play (Buzzer)
        Sound.play_buzzer
        return
      end
      Graphics.update
      # Play SE (Decision)
      Sound.play_decision
      # Play BGM
      Audio.bgm_play("Audio/BGM/" + sound[1], sound[2] == nil ? 100 : sound[2],
      sound[3] == nil ? 100 : sound[3])
      @cd_play = true
      text = sprintf("%02d", @sound_window.index + 1) + ":" + sound[0]
      set_text(@help_window, text, 1)
      return
    end
  end
end