#encoding:utf-8
#==============================================================================
# ■ Window_NameInput
#------------------------------------------------------------------------------
#  名字输入画面中,选择文字的窗口。
#==============================================================================

class Window_NameInput < Window_Selectable
  #--------------------------------------------------------------------------
  # ● 文字表
  #--------------------------------------------------------------------------
  TABLE1 = [ 'A','B','C','D','E',  'a','b','c','d','e',
             'F','G','H','I','J',  'f','g','h','i','j',
             'K','L','M','N','O',  'k','l','m','n','o',
             'P','Q','R','S','T',  'p','q','r','s','t',
             'U','V','W','X','Y',  'u','v','w','x','y',
             'Z','[',']','^','_',  'z','{','}','|','~',
             '0','1','2','3','4',  '!','#','$','%','&',
             '5','6','7','8','9',  '(',')','*','+','-',
             '/','=','@','<','>',  ':',';',' ','半角','确定']
  TABLE2 = [ 'A','B','C','D','E',  'a','b','c','d','e',
             'F','G','H','I','J',  'f','g','h','i','j',
             'K','L','M','N','O',  'k','l','m','n','o',
             'P','Q','R','S','T',  'p','q','r','s','t',
             'U','V','W','X','Y',  'u','v','w','x','y',
             'Z','[',']','^','_',  'z','{','}','|','~',
             '0','1','2','3','4',  '!','#','$','%','&',
             '5','6','7','8','9',  '(',')','*','+','-',
             '/','=','@','<','>',  ':',';',' ','其他','确定']
  TABLE3 = [ '〇','一','二','三','四',  '五','六','七','八','九',
             '十','百','千','万','亿',  '兆','吉','太','拍','艾',
             '贤','者','游','侠','的',  '里','克','龙','马','之',
             '娜','塔','丽','瑞','战',  '泰','伦','斯','爱','丝',
             '阿','奈','思','特','士',  '布','达','诺','亚','金',
             '伊','萨','贝','拉','师',  '斗','魔','法','导','银',
             '赤','橙','黄','绿','青',  '蓝','紫','黑','白','色',
             '骑','斧','剑','弓','枪',  '刀','铳','爪','锤','杖',
             '圣','暗','炎','水','风',  '地','电','冰','全角','确定']
  #--------------------------------------------------------------------------
  # ● 初始化对象
  #--------------------------------------------------------------------------
  def initialize(edit_window)
    super(edit_window.x, edit_window.y + edit_window.height + 8,
          edit_window.width, fitting_height(9))
    @edit_window = edit_window
    @page = 0
    @index = 0
    refresh
    update_cursor
    activate
  end
  #--------------------------------------------------------------------------
  # ● 获取字表
  #--------------------------------------------------------------------------
  def table
    return [TABLE1, TABLE2, TABLE3]
  end
  #--------------------------------------------------------------------------
  # ● 获取文字
  #--------------------------------------------------------------------------
  def character
    @index < 88 ? table[@page][@index] : ""
  end
  #--------------------------------------------------------------------------
  # ● 判定光标位置是否在“切换”上(平假/片假)
  #--------------------------------------------------------------------------
  def is_page_change?
    @index == 88
  end
  #--------------------------------------------------------------------------
  # ● 判定光标位置是否在“确定”上
  #--------------------------------------------------------------------------
  def is_ok?
    @index == 89
  end
  #--------------------------------------------------------------------------
  # ● 获取项目的绘制矩形
  #--------------------------------------------------------------------------
  def item_rect(index)
    rect = Rect.new
    rect.x = index % 10 * 32 + index % 10 / 5 * 16
    rect.y = index / 10 * line_height
    rect.width = 32
    rect.height = line_height
    rect
  end
  #--------------------------------------------------------------------------
  # ● 刷新
  #--------------------------------------------------------------------------
  def refresh
    contents.clear
    change_color(normal_color)
    90.times {|i| draw_text(item_rect(i), table[@page][i], 1) }
  end
  #--------------------------------------------------------------------------
  # ● 更新光标
  #--------------------------------------------------------------------------
  def update_cursor
    cursor_rect.set(item_rect(@index))
  end
  #--------------------------------------------------------------------------
  # ● 判定光标是否可以移动
  #--------------------------------------------------------------------------
  def cursor_movable?
    active
  end
  #--------------------------------------------------------------------------
  # ● 光标向下移动
  #     wrap : 允许循环
  #--------------------------------------------------------------------------
  def cursor_down(wrap)
    if @index < 80 or wrap
      @index = (index + 10) % 90
    end
  end
  #--------------------------------------------------------------------------
  # ● 光标向上移动
  #     wrap : 允许循环
  #--------------------------------------------------------------------------
  def cursor_up(wrap)
    if @index >= 10 or wrap
      @index = (index + 80) % 90
    end
  end
  #--------------------------------------------------------------------------
  # ● 光标向右移动
  #     wrap : 允许循环
  #--------------------------------------------------------------------------
  def cursor_right(wrap)
    if @index % 10 < 9
      @index += 1
    elsif wrap
      @index -= 9
    end
  end
  #--------------------------------------------------------------------------
  # ● 光标向左移动
  #     wrap : 允许循环
  #--------------------------------------------------------------------------
  def cursor_left(wrap)
    if @index % 10 > 0
      @index -= 1
    elsif wrap
      @index += 9
    end
  end
  #--------------------------------------------------------------------------
  # ● 向下一页移动
  #--------------------------------------------------------------------------
  def cursor_pagedown
    @page = (@page + 1) % table.size
    refresh
  end
  #--------------------------------------------------------------------------
  # ● 向上一页移动
  #--------------------------------------------------------------------------
  def cursor_pageup
    @page = (@page + table.size - 1) % table.size
    refresh
  end
  #--------------------------------------------------------------------------
  # ● 处理光标的移动
  #--------------------------------------------------------------------------
  def process_cursor_move
    last_page = @page
    super
    update_cursor
    Sound.play_cursor if @page != last_page
  end
  #--------------------------------------------------------------------------
  # ● “确定”、“删除字符”和“取消输入”的处理
  #--------------------------------------------------------------------------
  def process_handling
    return unless open? && active
    process_jump if Input.trigger?(:A)
    process_back if Input.repeat?(:B)
    process_ok   if Input.trigger?(:C)
  end
  #--------------------------------------------------------------------------
  # ● 跳转“确定”
  #--------------------------------------------------------------------------
  def process_jump
    if @index != 89
      @index = 89
      Sound.play_cursor
    end
  end
  #--------------------------------------------------------------------------
  # ● 后退一个字符
  #--------------------------------------------------------------------------
  def process_back
    Sound.play_cancel if @edit_window.back
  end
  #--------------------------------------------------------------------------
  # ● 按下确定键时的处理
  #--------------------------------------------------------------------------
  def process_ok
    if !character.empty?
      on_name_add
    elsif is_page_change?
      Sound.play_ok
      cursor_pagedown
    elsif is_ok?
      on_name_ok
    end
  end
  #--------------------------------------------------------------------------
  # ● 添加名字字符
  #--------------------------------------------------------------------------
  def on_name_add
    if @edit_window.add(character)
      Sound.play_ok
    else
      Sound.play_buzzer
    end
  end
  #--------------------------------------------------------------------------
  # ● 确定名字
  #--------------------------------------------------------------------------
  def on_name_ok
    if @edit_window.name.empty?
      if @edit_window.restore_default
        Sound.play_ok
      else
        Sound.play_buzzer
      end
    else
      Sound.play_ok
      call_ok_handler
    end
  end
end