#==============================================================================
# ** TDS Splash Screen
#    Ver: 1.5
#------------------------------------------------------------------------------
#  * Description:
#  This script lets you display images before the the title screen is shown.
#------------------------------------------------------------------------------
#  * Features: 
#  Show pictures before the title screen appears.
#  Play sounds along with the pictures.
#------------------------------------------------------------------------------
#  * Instructions:
#  To display a Splash Screen image edit this constant and add setting structs
#  to it.
#
#    Splash_Screens = [
#      Splash_Settings = Struct.new(name, fadein, fadeout, duration, skippable,
#      centered, start_SE, start_ME, end_wait)
#    ]
#
#    name      = "Picture Name"
#    fadein    = Splash Fade in duration. (0 for instant)
#    fadeout   = Splash Fade out duration. (0 for instant)
#    duration  = Pause Duration. (If nil pause will be infinite and require skip)
#    skippable = If true the Splash Screen can be skipped by pressing a key.
#    centered  = If true it will center the Splash Image to the screen.
#    start_SE  = SE to play after fade in. (["SE Name", Volume, Pitch]) (Ignored if nil.)
#    start_ME  = ME to play after fade in. (["SE Name", Volume, Pitch]) (Ignored if nil.)
#    end_wait  = Pause duration after fadeout before the next image is shown.
#
#    Example:
#
#    Single Splash Image (Simple Fade in and Fade out with infinite duration)
#
#    Splash_Screens = [
#      Splash_Settings.new("Splash1", 20, 20, nil, true, true, ["Wolf", 100, 80], nil, 40),
#    ]
#
#
#  To display the Splash Screen only once when the game loads set this constant
#  to true. If false the Splash Screen will appear whenever the title screen is
#  called.
#
#    Splash_Screen_Once = true/false
#
#  To display the Splash Screen while testing set this constant to true.
#
#    Show_On_Playtest = true/false
#------------------------------------------------------------------------------
#  * Notes:
#  None.
#------------------------------------------------------------------------------
# WARNING:
#
# Do not release, distribute or change my work without my expressed written 
# consent, doing so violates the terms of use of this work.
#
# If you really want to share my work please just post a link to the original
# site.
#
# * Not Knowing English or understanding these terms will not excuse you in any
#   way from the consequenses.
#==============================================================================
# * Import to Global Hash *
#==============================================================================
($imported ||= {})[:TDS_Splash_Screen] = true

#==============================================================================
# ** Scene_Base
#------------------------------------------------------------------------------
#  This is a super class of all scenes within the game.
#==============================================================================

class Scene_Base
  #--------------------------------------------------------------------------
  # * Class Variable
  #--------------------------------------------------------------------------
  # Splash Screen Used Flag
  @@splash_screen_used = false 
end

#==============================================================================
# ** Scene_Title
#------------------------------------------------------------------------------
#  This class performs the title screen processing.
#==============================================================================

class Scene_Title < Scene_Base
  #--------------------------------------------------------------------------
  # * Constants
  #-------------------------------------------------------------------------- 
  # Splash Settings Struct
  Splash_Settings = Struct.new(:name, :fadein, :fadeout, :duration, :skippable,
  :centered, :start_SE, :start_ME, :end_wait)  
  # Splash Screens Array
  Splash_Screens = [
    # First Splash Image (Simple Fade in and Fade out with infinite duration)
    Splash_Settings.new("Splash1", 20, 20, nil, true, true, ["Wolf", 100, 80], nil, 40),
    # Second Splash Image (Sound with Fade in and Fade out with limited duration)
    Splash_Settings.new("Splash2", 20, 20, 300, false, true, nil, ["Gag", 100, 80], 40),  
  ]  
  # If True it will only show the Splash Screen once when the game loads.
  Splash_Screen_Once = true
  # Show Splash Screen when testing. (Set to false to skip when testing game)
  Show_On_Playtest = true
  #--------------------------------------------------------------------------
  # * Alias Listing
  #--------------------------------------------------------------------------  
  alias tds_splash_screen_scene_title_start                        start
  #--------------------------------------------------------------------------
  # * Start Processing
  #--------------------------------------------------------------------------
  def start(*args, &block)    
    # Go Through Instance Variables (Prevents Window Update Errors)
    instance_variables.each { |varname|
      # Get Variable Object
      ivar = instance_variable_get(varname)
      # If Variable Object is a Window
      if ivar.is_a?(Window)
        # Dispose if not already disposed
        ivar.dispose if !ivar.disposed?
        # Remove Instance Variaible
        remove_instance_variable(varname)  
      end
    }    
    # If not testing or Testing and Can Show Splash Screen on Playtest
    if !$TEST or ($TEST and Show_On_Playtest)
      # If Splash Screen can be showned
      if !Splash_Screen_Once or (Splash_Screen_Once and !@@splash_screen_used)      
        # Start Splash Intro
        start_splash_intro
      end
    end
    # Set Splash Screen Used Flag to true
    @@splash_screen_used = true    
    # Run Original Method
    tds_splash_screen_scene_title_start(*args, &block)
  end
  #--------------------------------------------------------------------------
  # * Start Splash Intro
  #--------------------------------------------------------------------------
  def start_splash_intro
    # Freeze, Transition and Fade out graphics
    Graphics.freeze ; Graphics.transition(1) ; fadeout_all(100)
    # Go Through Splash Screens
    Splash_Screens.dup.each {|s| display_splash_image(s)}
  end
  #--------------------------------------------------------------------------
  # * Display Splash Image
  #     settings : splash image settings
  #--------------------------------------------------------------------------
  def display_splash_image(settings)
    # Return if settings are nil
    return if settings.nil?    
    # Create Background Sprite
    @background = Sprite.new ; @background.bitmap = Bitmap.new(Graphics.width, Graphics.height)
    @background.bitmap.fill_rect(@background.bitmap.rect, Color.new(0, 0, 0))    
    # Make Splash Image Sprite
    @splash_image = Sprite.new ; @splash_image.bitmap = Cache.picture(settings[:name])
    # Center Sprite if Applicable
    center_sprite(@splash_image) if settings[:centered]
    # Fade In
    Graphics.fadein(settings[:fadein])
    # If Start SE
    if settings[:start_SE]
      # Play SE if Applicable
      Audio.se_play(*settings[:start_SE].dup[0] = 'Audio/SE/' + settings[:start_SE][0])       
    end
    # If Start ME
    if settings[:start_ME]
      # Play ME if Applicable
      Audio.se_play(*settings[:start_ME].dup[0] = 'Audio/ME/' + settings[:start_ME][0])       
    end    
    # If Duration is nil (Infinite)
    if settings[:duration].nil?    
      # Wait for Input
      loop { update_basic ; break if Input.trigger?(:C) or Input.trigger?(:B) }
    else
      # Duration Wait
      settings[:duration].times {
        # Update Basic Components
        update_basic
        # Break if Skippable and Input Trigger C or B
        break if settings[:skippable] and Input.trigger?(:C) or Input.trigger?(:B)
      }
    end
    # Fadeout
    Graphics.fadeout(settings[:fadeout])
    # After Splash Image Display Delay
    settings[:end_wait].times { update_basic } if settings[:end_wait]
    # Stop SE and ME sounds
    RPG::SE.stop ; RPG::ME.stop
    # Dispose of Splash Image
    @splash_image.bitmap.dispose ; @splash_image.dispose ; @splash_image = nil
    # Dispose of Background Sprite
    @background.bitmap.dispose ; @background.dispose ; @background = nil
  end
end