#==============================================================================
# ■ Picture_Selectable  vol.1.02
#------------------------------------------------------------------------------
# ※『Roulette』より下に挿入
# 11/10/10
#==============================================================================
class Picture_Selectable  < Window_Selectable
  include HIDE_COIN_GAME_BASE
  include HIDE_ROULETTE
  #--------------------------------------------------------------------------
  # ■ 公開インスタンス変数
  #--------------------------------------------------------------------------
  attr_reader   :index                    # カーソル位置
  attr_reader   :help_window              # ヘルプウィンドウ
  attr_accessor :zero                     # ゼロ位置フラグ位置
  attr_reader   :key                      # カーソル値
  #--------------------------------------------------------------------------
  # ■ オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize(x,y,width,height,max,line,pw,ph,px = 2,py = 2,
    turn = false,color = Color.new(255,255,255,255))
    super(x,y,width,height)
    @pw = pw ; @ph = ph ; @px = px ; @py = py
    @column_max = line
    @item_max = max
    refresh
    self.index = 0
    self.visible = false
    self.active = false
    @zero = false
    @key = 0
    @turn = turn
    if HIDE_COIN_GAME_BASE::BLINK or HIDE_ROULETTE::BLINK
      self.windowskin = Bitmap.new(1,1)
      make_blink_window?(ph,x = self.x+16,y = self.y+16,pw,width,row_max*ph)
      @blink_window.visible = self.visible
      draw_color(color)
    else
      self.opacity = 0
    end
  end
  #--------------------------------------------------------------------------
  # ■ ヘルプテキスト更新
  #--------------------------------------------------------------------------
  def update_help
  end
  #--------------------------------------------------------------------------
  # ■ 点滅色の設定
  #--------------------------------------------------------------------------
  def draw_color(color)
    if HIDE_COIN_GAME_BASE::BLINK or HIDE_ROULETTE::BLINK
      @blink_window.bitmap.clear
      color.alpha = 200
      w = @blink_window.bitmap.width ; h = @blink_window.bitmap.height
      @blink_window.bitmap.fill_rect(Rect.new(0,0,w,h),color)
    end
  end
  #--------------------------------------------------------------------------
  # ■ カーソル位置の設定
  #     index : 新しいカーソル位置
  #--------------------------------------------------------------------------
  def index=(index)
    @index = index
    # カーソルの矩形を更新
    update_cursor_rect
  end
  #--------------------------------------------------------------------------
  # ■ ヘルプウィンドウの設定
  #--------------------------------------------------------------------------
  def help_window=(help_window)
    @help_window = help_window
    # ヘルプテキストを更新 (update_help は継承先で定義される)
    if self.active and @help_window != nil
      update_help
    end
  end
  #--------------------------------------------------------------------------
  # ■ テキストを描画
  #--------------------------------------------------------------------------
  def set_text(text)
    font = HIDE_ROULETTE::H_FONT
    # テキストを再描画
    @help_window.contents.clear
    @help_window.contents.draw_cache_text(8,0,640,32,text,0,font)
  end
  #--------------------------------------------------------------------------
  # ■ カーソルの可視化
  #--------------------------------------------------------------------------
  def zero=(zero)
    @zero = zero
    if @zero
      self.index = -1
      self.visible = false
      self.active = false
    end
  end
  #--------------------------------------------------------------------------
  # ■ カーソルの矩形更新
  #--------------------------------------------------------------------------
  def update_cursor_rect
    # カーソルの座標を計算
    x = (@index % @column_max * @pw) - (@index % @column_max * @px)
    y = @index / @column_max * @ph - (@index / @column_max * @py) - self.oy
    # カーソルの矩形を更新
    self.cursor_rect.set(x, y,@pw,@ph)
    if HIDE_COIN_GAME_BASE::BLINK or HIDE_ROULETTE::BLINK
      # □点滅文字ウィンドウの更新
      set_blink_window?(self.index)
    end
  end
  #--------------------------------------------------------------------------
  # ■ フレーム更新
  #--------------------------------------------------------------------------
  def update
    if self.active && @item_max > 0 && @index >= 0 && self.zero == false
      #…………………………………………………………………………………………………
      # ■ 下 ボタンが押された場合
      #…………………………………………………………………………………………………
      if Input.repeat?(Input::DOWN)
        @key = 2 ; @se_index = @index
        i = (@index / @column_max + 1) * @column_max + @index % @column_max
        if @item_max > i
          @index = i
        elsif @turn
          @index = @index % @column_max
        else
          self.zero = true
        end
        if @se_index != @index or self.zero
          $game_system.se_play($data_system.cursor_se)
        end
        return
      end
      #…………………………………………………………………………………………………
      # ■ 上 ボタンが押された場合
      #…………………………………………………………………………………………………
      if Input.repeat?(Input::UP)
        @key = 8 ; @se_index = @index
        i = (@index / @column_max - 1) * @column_max + @index % @column_max
        if 0 <= i
          @index = i
        elsif @turn
          @index = @item_max - @column_max + @index % @column_max
        else
          self.zero = true
        end
        if @se_index != @index or self.zero
          $game_system.se_play($data_system.cursor_se)
        end
        return
      end
      #…………………………………………………………………………………………………
      # ■ 右 ボタンが押された場合
      #…………………………………………………………………………………………………
      if Input.repeat?(Input::RIGHT)
        @key = 6 ; @se_index = @index
        i0 = (@index + 1) - @column_max
        @index = (@index + 1) / @column_max == @index / @column_max ? @index + 1 : i0
        if @se_index != @index or self.zero
          $game_system.se_play($data_system.cursor_se)
        end
        return
      end
      #…………………………………………………………………………………………………
      # ■ 左 ボタンが押された場合
      #…………………………………………………………………………………………………
      if Input.repeat?(Input::LEFT)
        @key = 4 ; @se_index = @index
        i0 = (@index - 1) + @column_max
        @index = (@index - 1) / @column_max == @index / @column_max ? @index - 1 : i0
        if @se_index != @index or self.zero
          $game_system.se_play($data_system.cursor_se)
        end
        return
      end
    end
    # カーソルの矩形を更新
    update_cursor_rect
    # 元の処理を開始
    super
  end
  #--------------------------------------------------------------------------
  # ■ リフレッシュ
  #--------------------------------------------------------------------------
  def refresh
    if self.contents != nil
      self.contents.dispose
      self.contents = nil
    end
    self.contents = Bitmap.new(width - 32,(@item_max/@column_max)* @ph)
  end
end