#==============================================================================
# ■ Window_Message
#------------------------------------------------------------------------------
#  文章表示に使うメッセージウィンドウです。
#==============================================================================
 
class Window_Message < Window_Selectable
  #--------------------------------------------------------------------------
  # ● オブジェクト初期化
  #
  #修正:スクロール対応
  #      補助ウィンドウ作成
  #--------------------------------------------------------------------------
  def initialize
#   super(0, 288, 544, 128)
    super(24, 276, 496, 128)
 
    self.z = 200
    self.active = false
    self.index = -1
    self.openness = 0
    self.back_opacity = 200
 
    @opening = false            # ウィンドウのオープン中フラグ
    @closing = false            # ウィンドウのクローズ中フラグ
    @text = nil                 # 表示すべき残りの文章
 
    @text2 = []
    @scroll_flg = 0
    @skip_trigger = false
    @messageSE = 0
 
    @choice_count = 0           # 選択肢表示用
    @choice_skip  = 0           # 選択肢決定後に入力待ちしない
 
    @contents_x = 0             # 次の文字を描画する X 座標
    @contents_y = 0             # 次の文字を描画する Y 座標
    @line_count = 0             # 現在までに描画した行数
    @wait_count = 0             # ウェイトカウント
    @background = 0             # 背景タイプ
    @position = 2               # 表示位置
    @show_fast = false          # 早送りフラグ
    @line_show_fast = false     # 行単位早送りフラグ
    @pause_skip = false         # 入力待ち省略フラグ
 
    # 曜日ウィンドウ作成
    create_day_window
 
    create_gold_window
    create_number_input_window
    create_back_sprite
 
  end
  #--------------------------------------------------------------------------
  # ● 解放
  #
  #修正:曜日ウィンドウ解放
  #--------------------------------------------------------------------------
  def dispose
    super
 
    # 曜日ウィンドウ解放
    dispose_day_window
 
    dispose_gold_window
    dispose_number_input_window
    dispose_back_sprite
  end
  #--------------------------------------------------------------------------
  # ● メッセージスキップトリガー管理
  #
  #修正:曜日ウィンドウ解放
  #--------------------------------------------------------------------------
  def input_skip
    return true if Input.press?(Input::A)
    return false
  end
  #--------------------------------------------------------------------------
  # ● ウィンドウ透明
  #
  #新規作成
  #--------------------------------------------------------------------------
  def window_hide
    flg = (Input.trigger?(Input::L) or Input.trigger?(Input::R))
    if flg == true
      if $game_switches[2] == true
        $game_switches[2] = false
      else
        $game_switches[2] = true
      end
    end
 
    if $game_switches[2] == true
      self.y = 12
    else
      self.y = 276
    end
 
    if $game_switches[7] == true and Input.trigger?(Input::X)
      if self.opacity == 255
        Sound.play_cancel
        self.opacity = 0
        self.contents_opacity = 0
      elsif self.opacity != 255
        Sound.play_cancel
        self.opacity = 255
        self.contents_opacity = 255
      end
    end
  end
  #--------------------------------------------------------------------------
  # ● 文章送りの入力処理
  #
  #修正:ウィンドウ一時消去
  #      LRボタンで継続早送り
  #--------------------------------------------------------------------------
  def input_pause
 
    window_hide
 
    if self.opacity != 255
      # ウィンドウ一時消去中はメッセージ送りしない
 
    elsif input_skip
      # 継続早送り
      if @text != nil and not @text.empty?
        new_page if @line_count >= MAX_LINE
      else
        terminate_message
      end
 
#   if Input.trigger?(Input::B) or Input.trigger?(Input::C)
    elsif Input.trigger?(Input::B) or Input.trigger?(Input::C)
      self.pause = false
      if @text != nil and not @text.empty?
        new_page if @line_count >= MAX_LINE
      else
        terminate_message
      end
    end
  end
  #--------------------------------------------------------------------------
  # ● 選択肢の入力処理
  #
  #修正:ウィンドウ一時消去
  #--------------------------------------------------------------------------
  def input_choice
 
    window_hide
 
    if self.opacity != 255
      # ウィンドウ一時消去中はメッセージ送りしない
 
#   if Input.trigger?(Input::B)
    elsif Input.trigger?(Input::B)
      if $game_message.choice_cancel_type > 0
        Sound.play_cancel
        $game_message.choice_proc.call($game_message.choice_cancel_type - 1)
        terminate_message
      end
    elsif Input.trigger?(Input::C)
      Sound.play_decision
      $game_message.choice_proc.call(self.index)
      terminate_message
    end
  end
  #--------------------------------------------------------------------------
  # ● 早送りフラグの更新
  #
  #修正:早送りでのウェイト短縮は行わない
  #      ダッシュボタンで継続早送り
  #--------------------------------------------------------------------------
  def update_show_fast
    if self.pause or self.openness < 255
      @show_fast = false
    elsif input_skip
      # 継続早送り
      @show_fast = true
    elsif Input.trigger?(Input::C) #and @wait_count < 2
      @show_fast = true
    elsif not Input.press?(Input::C)
      @show_fast = false
    end
#  if @show_fast and @wait_count > 0
#     @wait_count -= 1
#   end
  end
  #--------------------------------------------------------------------------
  # ● メッセージの開始
  #
  #修正:1行だけ読み込む
  #      スクロール用配列に格納する
  #--------------------------------------------------------------------------
  def start_message
    if self.openness < 255
      # 最初の一行描写前は初期化する
      contents.clear
      @contents_y = 0
      @line_count = 0
      @scroll_flg = 0
      @text = ""
      @text2.clear
      contents.font.color = text_color(0)
    end
    @text = ""
#   @text += "  ・" if (@contents_y / WLH) >= $game_message.choice_start
    @text += "  ・" if $game_message.choice_start != 99
 
    @text += $game_message.texts.shift
 
    @text3 = ""
    @text3 += @text
    convert_special_characters_SCROLL
    @text2 << @text3
 
    @text += "\x00"
    convert_special_characters
 
    if @text2.size > 5
      hoge = @text2.shift
    end
 
    @item_max = $game_message.choice_max
    reset_window
    new_page
 
    # 既に4行描写している場合はスクロールフラグを立てる
    @scroll_flg = 1 if @line_count >= 4
 
  end
  #--------------------------------------------------------------------------
  # ● フレーム更新
  #
  #修正:スクロールフラグがONの場合の処理
  #      曜日ウィンドウ更新
  #--------------------------------------------------------------------------
  def update
    super
 
    # 曜日ウィンドウ更新
    update_day_window
 
    update_gold_window
    update_number_input_window
    update_back_sprite
    update_show_fast
    unless @opening or @closing             # ウィンドウの開閉中以外
      if @wait_count > 0                    # 文章内ウェイト中
        @wait_count -= 1
      elsif self.pause                      # 文章送り待機中
        input_pause
      elsif self.active                     # 選択肢入力中
        input_choice
 
        # 選択肢決定後にpause状態になるのを防ぐ
        @text = nil if @text != nil
 
      elsif @number_input_window.visible    # 数値入力中
        input_number
      elsif @scroll_flg != 0
        text_scroll
      elsif @text != nil                    # 残りの文章が存在
        update_message                        # メッセージの更新
      elsif continue?                       # 続ける場合
        start_message                         # メッセージの開始
        open                                  # ウィンドウを開く
        $game_message.visible = true
      else                                  # 続けない場合
        close                                 # ウィンドウを閉じる
        $game_message.visible = @closing
        @skip_trigger = false
      end
    end
  end
  #--------------------------------------------------------------------------
  # ● スクロール演出 半行分上にずらして4行描写
  #
  #新規作成
  #--------------------------------------------------------------------------
  def text_scroll
    contents.clear
 
    @contents_x = 0
    @contents_y = WLH * 3
    y = -12 * @scroll_flg
 
#~       contents.draw_text(@contents_x, y, 544, WLH, @text2[i])
#~       y += WLH
#~     end
 
    texts = Marshal.load(Marshal.dump(@text2))
    for i in 0...texts.size - 1
      c = texts[i].slice(/./m)
      case c
      when nil                          # 描画すべき文字がない
      when "\x01"                       # \C[n]  (文字色変更)
        texts[i].sub!(/\x01\[([0-9]+)\]/, "") 
         c1 = text_color($1.to_i)
        texts[i].sub!(/\x01\[([0-9]+)\]/, "")
         c2 = text_color($1.to_i)
        contents.font.color = c1
        contents.draw_text(@contents_x, y, 544, WLH, texts[i])
        contents.font.color = c2
      else                              # 普通の文字
        contents.draw_text(@contents_x, y, 544, WLH, texts[i])
      end
      y += WLH
    end
 
    @scroll_flg += 1
    @scroll_flg = 0 if @scroll_flg == 3
  end
  #--------------------------------------------------------------------------
  # ● 改ページ処理
  #
  #修正:Y座標を初期化しない
  #      顔グラ描写を立ち絵描写に変更
  #      早送りフラグはオフにしない
  #--------------------------------------------------------------------------
  def new_page
#   contents.clear
#   if $game_message.face_name.empty?
      @contents_x = 0
#   else
#     name = $game_message.face_name
#     index = $game_message.face_index
#     draw_face(name, index, 0, 0)
#     @contents_x = 112
#   end
#   @contents_y = 0
#   @line_count = 0
    if input_skip
      @show_fast = true
    elsif Input.press?(Input::A)
      @show_fast = false
    end
 
    @line_show_fast = false
    @pause_skip = false
#   contents.font.color = text_color(0)
  end
  #--------------------------------------------------------------------------
  # ● 改行処理
  #
  #修正:顔グラを無視
  #--------------------------------------------------------------------------
  def new_line
#   if $game_message.face_name.empty?
      @contents_x = 0
#   else
#     @contents_x = 112
#   end
#   @contents_y += WLH
#   @line_count += 1
    if @line_count >= 3
      # 4行目以降を描写する場合
      @contents_y = WLH * 3 
      @line_count += 1
      # 文章に続きがある場合はスクロールフラグを立てる
#      @scroll_flg = 1 if $game_message.texts.size != 0
    else
      # 1~3行目の場合
      @contents_y += WLH
      @line_count += 1
    end
    @line_show_fast = false
  end
  #--------------------------------------------------------------------------
  # ● メッセージの終了
  #
  #修正:曜日ウィンドウを閉じる
  #--------------------------------------------------------------------------
  def terminate_message
    self.active = false
    self.pause = false
    self.index = -1
 
    # 曜日ウィンドウを閉じる
    @day_window.close
 
    @gold_window.close
    @number_input_window.active = false
    @number_input_window.visible = false
    $game_message.main_proc.call if $game_message.main_proc != nil
    $game_message.clear
  end
  #--------------------------------------------------------------------------
  # ● メッセージの更新
  #
  #修正:曜日ウィンドウを開く
  #--------------------------------------------------------------------------
  def update_message
    loop do
      c = @text.slice!(/./m)            # 次の文字を取得
      case c
      when nil                          # 描画すべき文字がない
        finish_message                  # 更新終了
        break
      when "\x00"                       # 改行
        new_line
        if @line_count >= MAX_LINE      # 行数が最大のとき
          unless @text.empty?           # さらに続きがあるなら
            self.pause = true           # 入力待ちを入れる
            break
          end
        end
      when "\x01"                       # \C[n]  (文字色変更)
        @text.sub!(/\[([0-9]+)\]/, "")
        contents.font.color = text_color($1.to_i)
        next
      when "\x02"                       # \G  (所持金表示)
        @gold_window.refresh
        @gold_window.open
      when "\x03"                       # \.  (ウェイト 1/4 秒)
        @wait_count = 15
        break
      when "\x04"                       # \|  (ウェイト 1 秒)
        @wait_count = 60
        break
      when "\x05"                       # \!  (入力待ち)
        self.pause = true
        break
      when "\x06"                       # \>  (瞬間表示 ON)
        @line_show_fast = true
      when "\x07"                       # \<  (瞬間表示 OFF)
        @line_show_fast = false
      when "\x08"                       # \^  (入力待ちなし)
        @pause_skip = true
      when "\x09"                       # \Y  (曜日表示)
        @day_window.refresh
        @day_window.open
      else                              # 普通の文字
        @messageSE += 1
        if @messageSE >=2 and @choice_count == 0
          Sound.play_message if @line_show_fast == false
          @messageSE = 0
        end
 
        contents.draw_text(@contents_x, @contents_y, 40, WLH, c)
        c_width = contents.text_size(c).width
        @contents_x += c_width
      end
      break unless @show_fast or @line_show_fast
    end
  end
  #--------------------------------------------------------------------------
  # ● メッセージの更新終了
  #
  #修正: 残り文章がある場合は読み込む
  #--------------------------------------------------------------------------
  def finish_message
    if $game_message.choice_max > 0
      # 選択肢の場合
      @choice_count += 1
      if $game_message.texts.empty? == false
        # 残り文章がある場合は読み込む
        start_message
      elsif $game_message.choice_max > 0
        start_choice
        @choice_count = 0
      end
 
    elsif $game_message.num_input_variable_id > 0
      start_number_input
    elsif @pause_skip
      terminate_message
    elsif $game_message.texts.empty? == false
      # 残り文章がある場合は読み込む
      start_message
    else
      self.pause = true
      @text = nil
      @wait_count = 0
    end
  end
  #--------------------------------------------------------------------------
  # ● カーソルの更新
  #
  #修正:顔グラは無視
  #      カーソル形状変更
  #--------------------------------------------------------------------------
  def update_cursor
    if @index >= 0
#     x = $game_message.face_name.empty? ? 0 : 112
      x = 8
      y = ($game_message.choice_start + @index) * WLH
#     self.cursor_rect.set(x, y, contents.width - x, WLH)
      self.cursor_rect.set(x, y, WLH, WLH)
    else
      self.cursor_rect.empty
    end
  end
  #--------------------------------------------------------------------------
  # ● 曜日ウィンドウの作成
  #
  #新規作成
  #--------------------------------------------------------------------------
  def create_day_window
    @day_window = Window_Day.new(0, 0)
    @day_window.openness = 0
    @day_window.z = 199
  end
  #--------------------------------------------------------------------------
  # ● 曜日ウィンドウの解放
  #
  #新規作成
  #--------------------------------------------------------------------------
  def dispose_day_window
    @day_window.dispose
  end
  #--------------------------------------------------------------------------
  # ● 曜日ウィンドウの更新
  #
  #新規作成
  #--------------------------------------------------------------------------
  def update_day_window
    @day_window.update
  end
  #--------------------------------------------------------------------------
  # ● 特殊文字の変換(スクロール用)
  #
  #新規作成
  #--------------------------------------------------------------------------
  def convert_special_characters_SCROLL
    @text3.gsub!(/\\V\[([0-9]+)\]/i) { $game_variables[$1.to_i] }
    @text3.gsub!(/\\V\[([0-9]+)\]/i) { $game_variables[$1.to_i] }
    @text3.gsub!(/\\N\[([0-9]+)\]/i) { $game_actors[$1.to_i].name }
 
    # キャラ名前(文頭)
    @text3.gsub!(/^\\GN\[([0-9]+)\]/i) {
      "\x01[#{8}]" + Vocab::Char_Name[$1.to_i] + "\x01[#{0}]"}
 
    # キャラ名前
    @text3.gsub!(/\\GN\[([0-9]+)\]/i) {
      Vocab::Char_Name[$1.to_i]}
 
    # エネミー名前(文頭)
    @text3.gsub!(/^\\M\[([0-9]+)\]/i) { 
      "\x01[#{8}]" + $data_enemies[$1.to_i].name + "\x01[#{0}]"}
 
    # エネミー名前
    @text3.gsub!(/\\M\[([0-9]+)\]/i) { $data_enemies[$1.to_i].name }
 
    # 固有名詞
    @text3.gsub!(/\\TN\[([0-9]+)\]/i) { Vocab::Text_Name[$1.to_i] }
 
    # アイテム名前
    @text3.gsub!(/\\I\[([0-9]+)\]/i) { $data_items[$1.to_i].name }
    @text3.gsub!(/\\W\[([0-9]+)\]/i) { $data_weapons[$1.to_i].name }
    @text3.gsub!(/\\A\[([0-9]+)\]/i) { $data_armors[$1.to_i].name }
 
 
    # 先の曜日
    @text.gsub!(/\\D\[([0-9]+)\]/i) {
      Vocab::Day_Name[$1.to_i + $game_variables[5]]
    }
 
    @text3.gsub!(/\\C\[([0-9]+)\]/i) { "" }
 
    # 曜日画面を開く
    @text3.gsub!(/\\Y/)              { "" }
 
    @text3.gsub!(/\\G/)              { "" }
    @text3.gsub!(/\\\./)             { "" }
    @text3.gsub!(/\\\|/)             { "" }
    @text3.gsub!(/\\!/)              { "" }
    @text3.gsub!(/\\>/)              { "" }
    @text3.gsub!(/\\</)              { "" }
    @text3.gsub!(/\\\^/)             { "" }
    @text3.gsub!(/\\\\/)             { "" }
  end
  #--------------------------------------------------------------------------
  # ● 特殊文字の変換
  #
  #修正:固有名詞を表示する
  #--------------------------------------------------------------------------
  def convert_special_characters
    @text.gsub!(/\\V\[([0-9]+)\]/i) { $game_variables[$1.to_i] }
    @text.gsub!(/\\V\[([0-9]+)\]/i) { $game_variables[$1.to_i] }
    @text.gsub!(/\\N\[([0-9]+)\]/i) { $game_actors[$1.to_i].name }
 
    # キャラ名前(文頭)
    @text.gsub!(/^\\GN\[([0-9]+)\]/i) {
      "\x01[#{8}]" + Vocab::Char_Name[$1.to_i] + "\x01[#{0}]"}
 
    # キャラ名前
    @text.gsub!(/\\GN\[([0-9]+)\]/i) {
      Vocab::Char_Name[$1.to_i]}
 
    # エネミー名前(文頭)
    @text.gsub!(/^\\M\[([0-9]+)\]/i) { 
      "\x01[#{8}]" + $data_enemies[$1.to_i].name + "\x01[#{0}]"}
 
    # エネミー名前
    @text.gsub!(/\\M\[([0-9]+)\]/i) { $data_enemies[$1.to_i].name }
 
    # 固有名詞
    @text.gsub!(/\\TN\[([0-9]+)\]/i) { Vocab::Text_Name[$1.to_i] }
 
    # アイテム名前
    @text.gsub!(/\\I\[([0-9]+)\]/i) { $data_items[$1.to_i].name }
    @text.gsub!(/\\W\[([0-9]+)\]/i) { $data_weapons[$1.to_i].name }
    @text.gsub!(/\\A\[([0-9]+)\]/i) { $data_armors[$1.to_i].name }
 
    # 先の曜日
    @text.gsub!(/\\D\[([0-9]+)\]/i) {
      Vocab::Day_Name[$1.to_i + $game_variables[5]]
    }
 
    @text.gsub!(/\\C\[([0-9]+)\]/i) { "\x01[#{$1}]" }
 
    # 曜日画面を開く
    @text.gsub!(/\\Y/)             { "\x09" }
 
    @text.gsub!(/\\G/)              { "\x02" }
    @text.gsub!(/\\\./)             { "\x03" }
    @text.gsub!(/\\\|/)             { "\x04" }
    @text.gsub!(/\\!/)              { "\x05" }
    @text.gsub!(/\\>/)              { "\x06" }
    @text.gsub!(/\\</)              { "\x07" }
    @text.gsub!(/\\\^/)             { "\x08" }
    @text.gsub!(/\\\\/)             { "\\" }
  end
  #--------------------------------------------------------------------------
  # ● ウィンドウの背景と位置の設定
  #--------------------------------------------------------------------------
  def reset_window
    @background = $game_message.background
    @position = $game_message.position
#~     if @background == 0   # 通常ウィンドウ
#~       self.opacity = 255
#~     else                  # 背景を暗くする、透明にする
#~       self.opacity = 0
#~     end
    case @position
    when 0  # 上
#     self.y = 0
      window_hide
      @gold_window.y = 360
    when 1  # 中
#     self.y = 144
      window_hide
      @gold_window.y = 0
    when 2  # 下
#     self.y = 288
      window_hide
      @gold_window.y = 0
    end
  end
end