#==============================================================================
# ■ Trash_Box/Skill_Sort vol.2.00
#------------------------------------------------------------------------------
# スキルの並び替え/スキルを忘れる
# ※『A』ボタンは、スキルの自動整列(ID順に並び替え)
# 07/09/07
#==============================================================================
module Trash_Box
  #…………………………………………………………………………………………………
  # ■スキルを忘れる機能を使う場合は[TRASH_BOX = true]
  #…………………………………………………………………………………………………
  TRASH_BOX = false                    # turu以外は↓設定は無効
  NAME = "ゴミ箱"                     # ゴミ箱の名前
  ICON = "trash_box"                  # ゴミ箱アイコンの名前
  HELP = "スキルを忘れます。"         # ゴミ箱を選択した場合のヘルプ
  PLUS = "を忘れます。"               # ゴミ箱に捨てる場合のスキルのヘルプ(スキル名＋)
  COLOR = Color.new(255,0,0,255)      # ゴミ箱を選択した場合のスキル色
  TRASH = Color.new(128,255,255,255)  # ゴミ箱の文字色
end
#==============================================================================
# ■ Game_Temp
#==============================================================================
class Game_Temp
  attr_accessor :trash_box              # ゴミ箱フラグ
  #--------------------------------------------------------------------------
  # ■ オブジェクト初期化
  #--------------------------------------------------------------------------
  if !$@;alias hidesp_initialize initialize;end
  def initialize
    # □元の処理を開始
    hidesp_initialize
    @trash_box = false
  end
end
#==============================================================================
# ■ Game_Actor < Game_Battler
#==============================================================================
class Game_Actor < Game_Battler
  #--------------------------------------------------------------------------
  # ■ スキルを覚える(再定義)
  #--------------------------------------------------------------------------
  def learn_skill(skill_id)
    if skill_id > 0 and not skill_learn?(skill_id)
      @skills.push(skill_id)
      # □スキル配列をソートしない
      ##@skills.sort!
    end
  end
end
#==============================================================================
# ■ Window_Skill < Window_Selectable
# 『2nd_Cursor_Icon』の導入が必須
#==============================================================================
class Window_Skill < Window_Selectable
  include HIDESP_CSR2ND
  #--------------------------------------------------------------------------
  # ■ リフレッシュ(再定義)
  #--------------------------------------------------------------------------
  def refresh
    if self.contents != nil
      self.contents.dispose
      self.contents = nil
    end
    @data = []
    for i in 0...@actor.skills.size
      skill = $data_skills[@actor.skills[i]]
      if skill != nil
        @data.push(skill)
      end
    end
    #…………………………………………………………………………………………………
    # ■ゴミ箱を追加(戦闘中以外)
    #…………………………………………………………………………………………………
    unless $game_temp.in_battle
      if Trash_Box::TRASH_BOX
        @data.push(Trash_Box::NAME)
        max = @data.size - 1
      else
        max = @data.size
      end
    else
      max = @data.size
    end
    @item_max = @data.size
    # 項目数が 0 でなければビットマップを作成し、全項目を描画
    if @item_max > 0
      self.contents = Bitmap.new(width - 32, row_max * 32)
      for i in 0...max
        draw_item(i)
      end
      #…………………………………………………………………………………………………
      # ■ゴミ箱を描画(戦闘中以外)
      #…………………………………………………………………………………………………
      unless $game_temp.in_battle
        if Trash_Box::TRASH_BOX
          draw_trash_box(max)
        end
      end
    end
  end
  #--------------------------------------------------------------------------
  # ■ ゴミ箱の描画
  #--------------------------------------------------------------------------
  def draw_trash_box(index)
    self.contents.font.color = Trash_Box::TRASH
    x = 4 + index % 2 * (288 + 32)
    y = index / 2 * 32
    self.contents.draw_text(x + 28,y,204,32,Trash_Box::NAME,0)
    # □ゴミ箱アイコンの描画
    icon = RPG::Cache.icon(Trash_Box::ICON)
    self.contents.blt(x, y + 4,icon,Rect.new(0,0,24,24))
  end
  #--------------------------------------------------------------------------
  # ■ 項目の描画(再定義)
  #--------------------------------------------------------------------------
  def draw_item(index)
    skill = @data[index]
    #--------------------------------------------------------------------------
    # ■ゴミ箱フラグがONの場合
    #--------------------------------------------------------------------------
    if $game_temp.trash_box == true
      self.contents.font.color = Trash_Box::COLOR
      opacity = 255
    elsif @actor.skill_can_use?(skill.id)
      self.contents.font.color = normal_color
      opacity = 255
    else
      self.contents.font.color = disabled_color
      opacity = 128
    end
    x = 4 + index % 2 * (288 + 32)
    y = index / 2 * 32
    rect = Rect.new(x, y, self.width / @column_max - 32,32)
    self.contents.fill_rect(rect, Color.new(0,0,0,0))
    bitmap = RPG::Cache.icon(skill.icon_name)
    self.contents.blt(x, y + 4, bitmap, Rect.new(0,0,24,24),opacity)
    self.contents.draw_text(x + 28, y, 204, 32, skill.name, 0)
    self.contents.draw_text(x + 232, y, 48, 32, skill.sp_cost.to_s,2)
  end
  #--------------------------------------------------------------------------
  # ■ ヘルプテキスト更新(再定義)
  #--------------------------------------------------------------------------
  def update_help
    if $game_temp.trash_box == true && self.skill != Trash_Box::NAME && self.skill != nil
      @help_window.set_text(skill.name + Trash_Box::PLUS)
    elsif self.skill == Trash_Box::NAME
      @help_window.set_text(Trash_Box::HELP)
    else
      @help_window.set_text(self.skill == nil ? "" : self.skill.description)
    end
  end
end
#==============================================================================
# ■ Scene_Skill
#==============================================================================
class Scene_Skill
  #--------------------------------------------------------------------------
  # ■ フレーム更新 (再定義)
  #--------------------------------------------------------------------------
  def update_skill
    # A ボタンが押された場合
    if Input.trigger?(Input::A)
      #…………………………………………………………………………………………………
      # ■スキルの自動整列(ID順)
      #…………………………………………………………………………………………………
      if $game_temp.trash_box == true or @skill_window.index2 != -1
        # ブザー SE を演奏
        $game_system.se_play($data_system.buzzer_se)
      else
        # カーソル SE を演奏
        $game_system.se_play($data_system.cursor_se)
        # □スキル配列をソート
        @actor.skills.sort!
        @skill_window.refresh
      end
      return
    end
    # B ボタンが押された場合
    if Input.trigger?(Input::B)
      # キャンセル SE を演奏
      $game_system.se_play($data_system.cancel_se)
      #…………………………………………………………………………………………………
      # ■ゴミ箱がONの場合、第二カーソルをOFF
      #…………………………………………………………………………………………………
      if ($game_temp.trash_box && Trash_Box::TRASH_BOX) or @skill_window.index2 != -1
        @skill_window.index2 = -1
        # □ゴミ箱フラグをOFF
        $game_temp.trash_box = false
        @skill_window.refresh
      else
        # メニュー画面に切り替え
        $scene = Scene_Menu.new(1)
      end
      return
    end
    # C ボタンが押された場合
    if Input.trigger?(Input::C)
      # スキルウィンドウで現在選択されているデータを取得
      @skill = @skill_window.skill
      #…………………………………………………………………………………………………
      # ■第2カーソルがOFFの場合
      #…………………………………………………………………………………………………
      if @skill_window.index2 == -1
        # 決定 SE を演奏
        $game_system.se_play($data_system.decision_se)
        @skill_window.index2 = @skill_window.index
        # ■ゴミ箱を選択した場合
        if @skill == Trash_Box::NAME && ($game_temp.trash_box == false && Trash_Box::TRASH_BOX)
          $game_temp.trash_box = true
          @skill_window.refresh
        end
        return
      #…………………………………………………………………………………………………
      # ■スキルの交換
      #…………………………………………………………………………………………………
      elsif @skill_window.index != @skill_window.index2 && $game_temp.trash_box == false
        if (@skill == Trash_Box::NAME && Trash_Box::TRASH_BOX)
          # ブザー SE を演奏
          $game_system.se_play($data_system.buzzer_se)
          return
        end
        # 決定 SE を演奏
        $game_system.se_play($data_system.decision_se)
        # □スキルの交換
        @actor.skills[@skill_window.index],@actor.skills[@skill_window.index2] = 
        @actor.skills[@skill_window.index2],@actor.skills[@skill_window.index]
        @skill_window.refresh
        @skill_window.index2 = -1
        return
      #…………………………………………………………………………………………………
      # ■ゴミ箱がONの場合(スキルを忘れる)
      #…………………………………………………………………………………………………
      elsif ($game_temp.trash_box && Trash_Box::TRASH_BOX)
        if @skill_window.index != @skill_window.index2
          # ブザー SE を演奏
          $game_system.se_play($data_system.equip_se)
          #…………………………………………………………………………………………………
          # ■スキルを忘れる
          #…………………………………………………………………………………………………
          @actor.forget_skill(@skill.id)
          # □ゴミ箱の位置にカーソルを戻す
          @skill_window.index = @skill_window.index2 - 1
          @skill_window.index2 = -1
          # □ゴミ箱フラグをOFF
          $game_temp.trash_box = false
          @skill_window.refresh
          return
        else
          # ブザー SE を演奏
          $game_system.se_play($data_system.buzzer_se)
          return
        end
      end
      #…………………………………………………………………………………………………
      # ■index2を不可視
      #…………………………………………………………………………………………………
      @skill_window.index2 = -1
      
      # 使用できない場合
      if @skill == nil or not @actor.skill_can_use?(@skill.id)
        # ブザー SE を演奏
        $game_system.se_play($data_system.buzzer_se)
        return
      end
      # 決定 SE を演奏
      $game_system.se_play($data_system.decision_se)
      # 効果範囲が味方の場合
      if @skill.scope >= 3
        # ターゲットウィンドウをアクティブ化
        @skill_window.active = false
        @target_window.x = (@skill_window.index + 1) % 2 * 304
        @target_window.visible = true
        @target_window.active = true
        # 効果範囲 (単体/全体) に応じてカーソル位置を設定
        if @skill.scope == 4 || @skill.scope == 6
          @target_window.index = -1
        elsif @skill.scope == 7
          @target_window.index = @actor_index - 10
        else
          @target_window.index = 0
        end
      # 効果範囲が味方以外の場合
      else
        # コモンイベント ID が有効の場合
        if @skill.common_event_id > 0
          # コモンイベント呼び出し予約
          $game_temp.common_event_id = @skill.common_event_id
          # スキルの使用時 SE を演奏
          $game_system.se_play(@skill.menu_se)
          # SP 消費
          @actor.sp -= @skill.sp_cost
          # 各ウィンドウの内容を再作成
          @status_window.refresh
          @skill_window.refresh
          @target_window.refresh
          # マップ画面に切り替え
          $scene = Scene_Map.new
          return
        end
      end
      return
    end
    # R ボタンが押された場合
    if Input.trigger?(Input::R) && @skill_window.index2 == -1
      # カーソル SE を演奏
      $game_system.se_play($data_system.cursor_se)
      # 次のアクターへ
      @actor_index += 1
      @actor_index %= $game_party.actors.size
      # 別のスキル画面に切り替え
      $scene = Scene_Skill.new(@actor_index)
      return
    end
    # L ボタンが押された場合
    if Input.trigger?(Input::L) && @skill_window.index2 == -1
      # カーソル SE を演奏
      $game_system.se_play($data_system.cursor_se)
      # 前のアクターへ
      @actor_index += $game_party.actors.size - 1
      @actor_index %= $game_party.actors.size
      # 別のスキル画面に切り替え
      $scene = Scene_Skill.new(@actor_index)
      return
    end
  end
end