#==============================================================================
# ★ ExMenu_CustomCommand
#------------------------------------------------------------------------------
# 　菜单画面任意修改指令出现和允许使用与否。
#==============================================================================

# 菜单指令列表
# 依次指定：
# 开关 ID、指令位置、指令文字、场景名称、无效时是否出现、无人时能否使用。
# 
# 开关 ID：
# 　是否在菜单中出现的开关编号 (不需要的话设 0)
# 指令位置：
# 　指令所要插入的位置，通常菜单的最初指令 (默认的[物品] )为1。
# 　当两个（或以上）指令所指定的位置相同时，后指定的插在先指定的上面。
# 指令文字：
# 　请不要与其他指令重复，否则会出现判断错误。
#场景名称： 
# 　选取时调用的场景名称。
# 无效时是否出现、无人能否使用：
# 　当指令的开关为 OFF 时，依然在菜单中出现该指令
# 　当队伍中无人时，无效化该指令。
# 　以数字指定：(0:否 1:是) 。
# 
# 范例： 当21号开关为 ON 时，有效化「冒险日记」并配置在「状态」与「存档」之间。
#        选中时调用的场景叫做「Scene_Diary」。
#        无效时不出现、队伍为 0 人时依然可以选取。
# 　　　　　　　　　=> [21, 5, "冒险日记", "Scene_Diary", 1, 0],
EXMNU_CTCMD_COMMANDS = [
  [21, 5, "大意", "Scene_Outline", 1, 0],
  [22, 2, "物品2", "Scene_Item", 0, 0],
  [23, 5, "状态2", "Scene_Status", 0, 1],
  [24, 3, "物品3", "Scene_Item", 1, 1],
]

# 指令窗口最大行数
# 限制菜单指令数不超出这个数木目
# 指令窗口的高度依此而定，建议 7 ~ 13最适合。
EXMNU_CTCMD_ROWMAX = 10

#------------------------------------------------------------------------------

class Game_Party
  alias _exmctcmd_initialize initialize
  #--------------------------------------------------------------------------
  # ○ 定义实例变量 (追加定义)
  #--------------------------------------------------------------------------
  attr_accessor :last_menu_index          # 调用记忆用 : 菜单
  #--------------------------------------------------------------------------
  # ○ 初始化对象 (追加定义)
  #--------------------------------------------------------------------------
  def initialize
    _exmctcmd_initialize
    @last_menu_index = 0
  end
end

class Scene_Menu
  #--------------------------------------------------------------------------
  # ☆ 追加指令可使用化判断
  #--------------------------------------------------------------------------
  def command_inputable?(command)
    return false if command[5] == 1 and $game_party.members.size == 0
    return (command[0] == 0 or $game_switches[command[0]])
  end
  #--------------------------------------------------------------------------
  # ☆ 追加指令可视化判断
  #--------------------------------------------------------------------------
  def command_visible?(command)
    return command[4] == 1
  end
  #--------------------------------------------------------------------------
  # ○ 生成指令窗口 (重新定义)
  #--------------------------------------------------------------------------
  def create_command_window
    s1 = Vocab::item
    s2 = Vocab::skill
    s3 = Vocab::equip
    s4 = Vocab::status
    s5 = Vocab::save
    s6 = Vocab::game_end
    sa = [s1, s2, s3, s4, s5, s6]
    for cmd in EXMNU_CTCMD_COMMANDS
      if command_visible?(cmd) or command_inputable?(cmd)
        sa.insert cmd[1] - 1, cmd[2]
      end
    end
    @command_window = Window_Command.new(160, sa)
    @commands = {}
    for i in 0...sa.size
      @commands[sa[i]] = i
    end
    if sa.size >= EXMNU_CTCMD_ROWMAX
      @command_window.height = EXMNU_CTCMD_ROWMAX * 24 + 32
    end
    @command_window.index = @menu_index
    if $game_party.members.size == 0          # 人数为0的场合
      @command_window.draw_item(@commands[Vocab::item], false)
      @command_window.draw_item(@commands[Vocab::skill], false)
      @command_window.draw_item(@commands[Vocab::equip], false)
      @command_window.draw_item(@commands[Vocab::status], false)
    end
    if $game_system.save_disabled             # 禁止存档的场合
      @command_window.draw_item(@commands[Vocab::save], false)
    end
    for cmd in EXMNU_CTCMD_COMMANDS
      if command_visible?(cmd) and not command_inputable?(cmd)
        @command_window.draw_item(@commands[cmd[2]], false)
      end
    end
  end
  #--------------------------------------------------------------------------
  # ○ 更新指令选择 (重新定义)
  #--------------------------------------------------------------------------
  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 == @commands[Vocab::save]
          Sound.play_buzzer
          return
      elsif $game_system.save_disabled and 
        @command_window.index == @commands[Vocab::save]
        Sound.play_buzzer
        return
      else
        for cmd in EXMNU_CTCMD_COMMANDS
          if @command_window.index == @commands[cmd[2]] and 
            not command_inputable?(cmd)
            Sound.play_buzzer
            return
          end
        end
      end
      $game_party.last_menu_index = @command_window.index
      Sound.play_decision
      case @command_window.index
      when @commands[Vocab::item]
        $scene = Scene_Item.new
      when @commands[Vocab::skill], @commands[Vocab::equip], 
        @commands[Vocab::status]
        start_actor_selection
      when @commands[Vocab::save]
        $scene = Scene_File.new(true, false, false)
      when @commands[Vocab::game_end]
        $scene = Scene_End.new
      else
        for cmd in EXMNU_CTCMD_COMMANDS
          if @command_window.index == @commands[cmd[2]]
            eval("$scene = #{cmd[3]}.new")
          end
        end
      end
    end
  end
  #--------------------------------------------------------------------------
  # ○ 开始角色选取 (重新定义)
  #--------------------------------------------------------------------------
  def update_actor_selection
    if Input.trigger?(Input::B)
      Sound.play_cancel
      end_actor_selection
    elsif Input.trigger?(Input::C)
      $game_party.last_actor_index = @status_window.index
      Sound.play_decision
      case @command_window.index
      when @commands[Vocab::skill]
        $scene = Scene_Skill.new(@status_window.index)
      when @commands[Vocab::equip]
        $scene = Scene_Equip.new(@status_window.index)
      when @commands[Vocab::status]
        $scene = Scene_Status.new(@status_window.index)
      end
    end
  end
end

class Scene_Item
  #--------------------------------------------------------------------------
  # ○ 回到原画面 (重新定义)
  #--------------------------------------------------------------------------
  def return_scene
    $scene = Scene_Menu.new($game_party.last_menu_index)
  end
end

class Scene_Skill
  #--------------------------------------------------------------------------
  # ○ 回到原画面 (重新定义)
  #--------------------------------------------------------------------------
  def return_scene
    $scene = Scene_Menu.new($game_party.last_menu_index)
  end
end

class Scene_Equip
  #--------------------------------------------------------------------------
  # ○ 回到原画面 (重新定义)
  #--------------------------------------------------------------------------
  def return_scene
    $scene = Scene_Menu.new($game_party.last_menu_index)
  end
end

class Scene_Status
  #--------------------------------------------------------------------------
  # ○ 回到原画面 (重新定义)
  #--------------------------------------------------------------------------
  def return_scene
    $scene = Scene_Menu.new($game_party.last_menu_index)
  end
end

class Scene_File
  #--------------------------------------------------------------------------
  # ○ 回到原画面 (重新定义)
  #--------------------------------------------------------------------------
  def return_scene
    if @from_title
      $scene = Scene_Title.new
    elsif @from_event
      $scene = Scene_Map.new
    else
      $scene = Scene_Menu.new($game_party.last_menu_index)
    end
  end
end

class Scene_End
  #--------------------------------------------------------------------------
  # ○ 回到原画面 (重新定义)
  #--------------------------------------------------------------------------
  def return_scene
    $scene = Scene_Menu.new($game_party.last_menu_index)
  end
end
