class Window_Reversi < Window_Selectable
  BOARDWIDTH = 8
  BOARDHEIGHT = 8
  
    #--------------------------------------------------------------------------
  # ● オブジェクト初期化
  #-------------------------------------------------------------------------
  def initialize(x, y, width, height)
    super
    @index = 0
    @handler = {}
    @cursor_fix = false
    @cursor_all = false
    update_padding
    activate
    
    @board = []

  end
  #--------------------------------------------------------------------------
  # ● 桁数の取得
  #--------------------------------------------------------------------------
  def col_max
    return BOARDWIDTH
  end
  #--------------------------------------------------------------------------
  # ● 横に項目が並ぶときの空白の幅を取得
  #--------------------------------------------------------------------------
  def spacing
    return 0
  end
  #--------------------------------------------------------------------------
  # ● 項目数の取得
  #--------------------------------------------------------------------------
  def item_max
    return BOARDWIDTH * BOARDHEIGHT
  end
  #--------------------------------------------------------------------------
  # ● 項目の幅を取得
  #--------------------------------------------------------------------------
  def item_width
    return 2
  end
  #--------------------------------------------------------------------------
  # ● 項目の高さを取得
  #--------------------------------------------------------------------------
  def item_height
    return 32
  end
  #--------------------------------------------------------------------------
  # ● 項目の描画
  #--------------------------------------------------------------------------
  def draw_item(index)
    
    if @board
      draw_storn( index % BOARDWIDTH , index / BOARDHEIGHT) 
    end
    
  end
  #現在の盤情報の取得
  def get_board_info(board)
    @board = board.dup
  end  
  
  #--------------------------------------------------------------------------
  # ● 石の描画
  #--------------------------------------------------------------------------
  def draw_storn( x , y )
    stonecolor = @board[y * BOARDWIDTH + x ]
    if (stonecolor != 0 && stonecolor != nil) 
      bitmap = Cache.load_stone(stonecolor)
      rect = Rect.new(0, 0, 32, 32)
      contents.blt(item_height * x , item_height * y, bitmap, rect, 255)
      bitmap.dispose
    end  
  end  
  #--------------------------------------------------------------------------
  # ● 盤の描画
  #--------------------------------------------------------------------------
  def draw_board
    bitmap = Cache.reversi("Reversi_Board.png")
    rect = Rect.new(0, 0, BOARDWIDTH * item_height, BOARDHEIGHT * item_height)
    contents.blt(item_height * x , item_height * y, bitmap, rect, 255)
    bitmap.dispose
  end    
  #--------------------------------------------------------------------------
  # ● カーソルの描画
  #--------------------------------------------------------------------------
  def draw_board_curosr
    bitmap = Cache.reversi("board_cursor.png")
    rect = Rect.new(0, 0, 32, 32)
    
    cx = @index % BOARDWIDTH * item_height
    cy = @index / BOARDHEIGHT * item_height
    contents.blt( cx , cy , bitmap, rect, 255)
    bitmap.dispose
  end  
  #--------------------------------------------------------------------------
  # ● リフレッシュ
  #--------------------------------------------------------------------------
  def refresh
    contents.clear
    draw_board
    draw_all_items
    draw_players_info
    draw_board_curosr
  end  
  #--------------------------------------------------------------------------
  # ● カーソルを下に移動
  #--------------------------------------------------------------------------
  def cursor_down(wrap = false)
    if index < item_max - col_max || (wrap && col_max == 1)
      select((index + col_max) % item_max)
    end
    refresh
  end
  #--------------------------------------------------------------------------
  # ● カーソルを上に移動
  #--------------------------------------------------------------------------
  def cursor_up(wrap = false)
    if index >= col_max || (wrap && col_max == 1)
      select((index - col_max + item_max) % item_max)
    end
    refresh
  end
  #--------------------------------------------------------------------------
  # ● カーソルを右に移動
  #--------------------------------------------------------------------------
  def cursor_right(wrap = false)
    if col_max >= 2 && (index < item_max - 1 || (wrap && horizontal?))
      select((index + 1) % item_max)
    end
    refresh
  end
  #--------------------------------------------------------------------------
  # ● カーソルを左に移動
  #--------------------------------------------------------------------------
  def cursor_left(wrap = false)
    if col_max >= 2 && (index > 0 || (wrap && horizontal?))
      select((index - 1 + item_max) % item_max)
    end
    refresh
  end
   
  
  def get_players(actor,actor2)
    
    @p1_player = actor
    @p2_player = actor2
  end  
    
  def draw_players_info
    draw_player(BOARDWIDTH * 32 + 10 , 140, @p1_player)
    draw_player(BOARDWIDTH * 32 + 10 , 0, @p2_player)
  end  
  
  def draw_player(x , y, actor)
    draw_actor_name(actor, x + 100 , y)
    draw_actor_face(actor, x , y )
  end

  #--------------------------------------------------------------------------
  # ● 名前の描画
  #--------------------------------------------------------------------------
  def draw_actor_name(actor, x, y, width = 112)
    draw_text(x, y, width, line_height, actor.name)
  end  

  
end  