#==============================================================================
# ** TDS Tales of Phantasia Like CMS - Main
#    Ver: 1.0
#------------------------------------------------------------------------------
#  * Description:
#  A simple plug and play menu that looks like the tales of phantasia menu.
#------------------------------------------------------------------------------
#  * Features: 
#  Changes menu interface.
#------------------------------------------------------------------------------
#  * Instructions:
#  None.
#------------------------------------------------------------------------------
#  * 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.
#==============================================================================


#==============================================================================
# ** Scene_Menu
#------------------------------------------------------------------------------
#  This class performs the menu screen processing.
#==============================================================================

class Scene_Menu < Scene_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     menu_index : command cursor's initial position
  #--------------------------------------------------------------------------
  def initialize(menu_index = 0)
    # Menu Index
    @menu_index = menu_index
    # Make Empty Actor Status Windows Array
    @actor_status_windows = []
    # Set Actor Status Selection Index
    @actor_status_index = -1
  end
  #--------------------------------------------------------------------------
  # * Start processing
  #--------------------------------------------------------------------------
  def start
    super
    # Create Menu Background
    create_menu_background
    # Create Command Window
    create_command_window
    # Create Actor Status Windows
    create_actor_status_windows
    # Create Game Stats Window
    @stats_window = Window_Game_Stats.new(0, 336) 
  end
  #--------------------------------------------------------------------------
  # * Termination Processing
  #--------------------------------------------------------------------------
  def terminate
    super
    dispose_menu_background
    @command_window.dispose
    # Dispose of Actor Status Windows
    @actor_status_windows.each {|window| window.dispose}
    # Dispose of Game Stats Window
    @stats_window.dispose
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    super
    # Updat Menu Background
    update_menu_background
    # Update Command Window
    @command_window.update    
    # Update Game Stats Window
    @stats_window.update
    # If Command Window is Active
    if @command_window.active
      # Update Command Selection
      update_command_selection
    elsif @actor_status_index >= 0
      # Update Actor Selection
      update_actor_selection
    end
  end
  #--------------------------------------------------------------------------
  # * Create Command Window
  #--------------------------------------------------------------------------
  def create_command_window
    s1 = Vocab::item
    s2 = Vocab::skill
    s3 = Vocab::equip
    s4 = Vocab::status
    s5 = Vocab::save
    s6 = Vocab::game_end
    @command_window = Window_Command.new(544, [s1, s2, s3, s4, s5, s6], 4)
    @command_window.index = @menu_index
    if $game_party.members.size == 0          # If number of party members is 0
      @command_window.draw_item(0, false)     # Disable item
      @command_window.draw_item(1, false)     # Disable skill
      @command_window.draw_item(2, false)     # Disable equipment
      @command_window.draw_item(3, false)     # Disable status
    end
    if $game_system.save_disabled             # If save is forbidden
      @command_window.draw_item(4, false)     # Disable save
    end
  end
  #--------------------------------------------------------------------------
  # * Create Actor Status Window
  #--------------------------------------------------------------------------
  def create_actor_status_windows  
    # Actor Status Windows Array
    @actor_status_windows = []
    # Go through Party Members Array and Make Status Windows
    for i in 0...4
      # Get Actor Object
      actor = $game_party.members.at(i)
      # Get Window X Position      
      x = (i % 2 ) * 272
      # Get Window Y Position
      y = 80 + (i / 2) * 128
      # Make Actor Status Window
      @actor_status_windows[i] = Window_Actor_Menu_Status.new(actor, x, y)       
    end    
  end
  #--------------------------------------------------------------------------
  # * Update Command Selection
  #--------------------------------------------------------------------------
  def update_command_selection
    if Input.trigger?(Input::B)
      Sound.play_cancel
      $scene = Scene_Map.new
    elsif Input.trigger?(Input::C)
      if $game_party.members.size == 0 and @command_window.index < 4
        Sound.play_buzzer
        return
      elsif $game_system.save_disabled and @command_window.index == 4
        Sound.play_buzzer
        return
      end
      Sound.play_decision
      case @command_window.index
      when 0      # Item
        $scene = Scene_Item.new
      when 1,2,3  # Skill, equipment, status
        start_actor_selection
      when 4      # Save
        $scene = Scene_File.new(true, false, false)
      when 5      # End Game
        $scene = Scene_End.new
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Start Actor Selection
  #--------------------------------------------------------------------------
  def start_actor_selection
    # Deactivate Command Window
    @command_window.active = false
    if $game_party.last_actor_index < $game_party.members.size      
       @actor_status_index = $game_party.last_actor_index
    else
       @actor_status_index = 0
    end
    # Set Selected Actor Status Window
    @actor_status_windows[@actor_status_index].selected(true)
  end
  #--------------------------------------------------------------------------
  # * End Actor Selection
  #--------------------------------------------------------------------------
  def end_actor_selection
    # Activate Command Window
    @command_window.active = true
    # Set Actor Status Selection Index
    @actor_status_index = -1    
    # Un Select All Actor Status Windows
    @actor_status_windows.each {|window| window.selected(false)}       
  end
  #--------------------------------------------------------------------------
  # * Update Actor Selection
  #--------------------------------------------------------------------------
  def update_actor_selection      
    # Update Actor Status Windows
    @actor_status_windows.each {|window| window.update}
    # Update Actor Selection Cursor Input
    update_actor_selection_cursor_input
    # If Input Trigger B (Cancel)
    if Input.trigger?(Input::B)
      # Play Cancel SE
      Sound.play_cancel
      # End Actor Selection
      end_actor_selection
    # If Input Trigger C (Confirm)
    elsif Input.trigger?(Input::C)
      # Set Game Party Last Actor Index
      $game_party.last_actor_index = @actor_status_index
      Sound.play_decision
      case @command_window.index
      when 1  # skill
        $scene = Scene_Skill.new(@actor_status_index)
      when 2  # equipment
        $scene = Scene_Equip.new(@actor_status_index)
      when 3  # status
        $scene = Scene_Status.new(@actor_status_index)
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Update Actor Selection Cursor Input
  #--------------------------------------------------------------------------
  def update_actor_selection_cursor_input
    # If Input Trigger or Repeat Up    
    if Input.trigger?(Input::UP) or Input.repeat?(Input::UP)
      # De Select Actor Status Window
      @actor_status_windows[@actor_status_index].selected(false)      
      # Decrease Actor Status Select Index
      @actor_status_index -= 2
      @actor_status_index %= $game_party.members.size      
      # Select Actor Status Window
      @actor_status_windows[@actor_status_index].selected(true)                  
      return
    end
    # If Input Trigger or Repeat Down
    if Input.trigger?(Input::DOWN) or Input.repeat?(Input::DOWN)
      # De Select Actor Status Window
      @actor_status_windows[@actor_status_index].selected(false)      
      # Increase Actor Status Select Index
      @actor_status_index += 2
      @actor_status_index %= $game_party.members.size      
      # Select Actor Status Window
      @actor_status_windows[@actor_status_index].selected(true)                  
      return
    end    
    # If Input Trigger or Repeat Right
    if Input.trigger?(Input::RIGHT) or Input.repeat?(Input::RIGHT)
      # De Select Actor Status Window
      @actor_status_windows[@actor_status_index].selected(false)      
      # Increase Actor Status Select Index
      @actor_status_index += 1
      @actor_status_index %= $game_party.members.size      
      # Select Actor Status Window
      @actor_status_windows[@actor_status_index].selected(true)      
      return
    end
    # If Input Trigger or Repeat Left
    if Input.trigger?(Input::LEFT) or Input.repeat?(Input::LEFT)
      # De Select Actor Status Window
      @actor_status_windows[@actor_status_index].selected(false)      
      # Decrease Actor Status Select Index
      @actor_status_index -= 1
      @actor_status_index %= $game_party.members.size      
      # Select Actor Status Window
      @actor_status_windows[@actor_status_index].selected(true)            
      return
    end
  end  
end


#==============================================================================
# ** Window_Actor_Menu_Status
#------------------------------------------------------------------------------
#  This window displays actor status on the menu screen.
#==============================================================================

class Window_Actor_Menu_Status < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     actor : actor object
  #     x     : window X coordinate
  #     y     : window Y coordinate
  #--------------------------------------------------------------------------
  def initialize(actor, x, y)
    super(x, y, 272, 128)
    # Set Actor Object
    @actor = actor
    # Refresh Window Contents
    refresh
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    # Clear Window Contents
    self.contents.clear
    # Return if Actor is nil
    return if @actor.nil?
    # Draw Actor Face
    draw_actor_face(@actor, 0, 0)    
    # Draw Actor Name
    draw_actor_name(@actor, 105, 0)    
    # Draw Actor Level
    draw_actor_level(@actor, 192, 0)    
    # Draw Actor State
    draw_actor_state(@actor, 105, 26, 120)    
    # Draw Actor HP
    draw_actor_hp(@actor, 105, 26 + WLH * 1, 135)    
    # Draw Actor MP
    draw_actor_mp(@actor, 105, 26 + WLH * 2, 135)
  end
  #--------------------------------------------------------------------------
  # * Set Selected
  #     select : selected (true = selected, false = unselected)
  #--------------------------------------------------------------------------
  def selected(select)
    # If Selected
    if select then self.cursor_rect = Rect.new(-10, -10, self.width-12, self.height-12) else self.cursor_rect.empty end    
  end
  #--------------------------------------------------------------------------
  # * Draw Level
  #     actor : actor
  #     x     : draw spot x-coordinate
  #     y     : draw spot y-coordinate
  #--------------------------------------------------------------------------
  def draw_actor_level(actor, x, y)
    self.contents.font.color = system_color
    self.contents.draw_text(x, y, 32, WLH, Vocab::level_a)
    self.contents.font.color = normal_color
    self.contents.draw_text(x + 24, y, 24, WLH, actor.level, 2)
  end  
end


#==============================================================================
# ** Window_Game_Stats
#------------------------------------------------------------------------------
#  This window displays the game stats such as play time, location and gold.
#==============================================================================

class Window_Game_Stats < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     x : window X coordinate
  #     y : window Y coordinate
  #--------------------------------------------------------------------------
  def initialize(x, y)
    super(x, y, 544, 80)
    refresh
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear  
    # Set Font Color
    self.contents.font.color = system_color
    self.contents.draw_text(0, 0, 120, 24, "Play time:")
    self.contents.draw_text(0, 24, 120, 24, "Location:")
    # Draw Currency
    draw_currency_value($game_party.gold, 390, 0, 120)    
    # Set Font Color
    self.contents.font.color = normal_color    
    # Get Map Name
    map = load_data("Data/MapInfos.rvdata")
    map_name = map[$game_map.map_id].name
    # Draw Map Name
    self.contents.draw_text(85, 24, 300, 24, map_name)
    # Draw Playtime
    draw_playtime(95, 0)
    # Update Self
    update
  end
  #--------------------------------------------------------------------------
  # * Draw Playtime
  #     x : x-coordinate
  #     y : y-coordinate  
  #--------------------------------------------------------------------------
  def draw_playtime(x, y)    
    self.contents.clear_rect(x, y, 100, 24)
    # Get Total Seconds Value
    total_sec = Graphics.frame_count / Graphics.frame_rate
    # Adjust Time
    hour = total_sec / 60 / 60
    min =  total_sec / 60 % 60
    sec =  total_sec % 60
    # Get Sprinf Format Text
    text = sprintf("%02d:%02d:%02d", hour, min, sec)    
    # Set Font Color
    self.contents.font.color = normal_color    
    # Draw Time Text
    self.contents.draw_text(x, y, 149, 24, text)        
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Draw Playtime
    draw_playtime(95, 0)    
  end
end