class Window_Puzzle15 < Window_Selectable


  
  #--------------------------------------------------------------------------
  # ● オブジェクト初期化
  #-------------------------------------------------------------------------
  def initialize(x, y, width, height , puzzle)
    @puzzle = puzzle
    @ncolumn = @puzzle.ncolumn
    @nrow = @puzzle.nrow
    @bwidth = $data_puzzle15_board_width
    @bheight = $data_puzzle15_board_height
    super(x, y, width, height )
    @index = 0
    @handler = {}
    @cursor_fix = false
    @cursor_all = false
    @count = 0
    update_padding
    activate
    set_bit_map
    make_bitmap_curosor
    @empty_cell = item_max - 1
    @game_end = false
    
  end
  
  def set_bit_map
    @bitmap = Cache.picture(@puzzle.pic_name)
    color = Color.new(0,0,0)
    @empty_bitmap = Bitmap.new(item_width , item_height)
    rect= @empty_bitmap.rect
    @empty_bitmap.fill_rect(rect, color) 

  end  
  
  def get_count(count)
    @count = count
  end  
  def get_max_count(count)
    @max_count = count
  end  
  #--------------------------------------------------------------------------
  # ● 桁数の取得
  #--------------------------------------------------------------------------
  def col_max
    return @ncolumn
  end  
  

  #--------------------------------------------------------------------------
  # ● 横に項目が並ぶときの空白の幅を取得
  #--------------------------------------------------------------------------
  def spacing
    return 0
  end
  #--------------------------------------------------------------------------
  # ● 項目数の取得
  #--------------------------------------------------------------------------
  def item_max
    return @puzzle.size
  end
  #--------------------------------------------------------------------------
  # ● 項目の幅を取得
  #--------------------------------------------------------------------------
  def item_width
    return @bwidth / @ncolumn
  end
  #--------------------------------------------------------------------------
  # ● 項目の高さを取得
  #--------------------------------------------------------------------------
  def item_height
    return @bheight / @nrow
  end
  #--------------------------------------------------------------------------
  # ● 項目の描画
  #--------------------------------------------------------------------------
  def draw_item(index)
    
    if @board
      set_bit_map
      @board.size.times {|i| draw_tile(i)}
#~       @bitmap.dispose
    end
    
  end
  #現在の盤情報の取得
  def get_board_info(board)
    @board = board.dup
  end  

  
  #--------------------------------------------------------------------------
  # ● タイルの描画
  #--------------------------------------------------------------------------
  def draw_tile(index ,x = 0 ,y = 0)
    return unless @board
    tx = index % @ncolumn 
    ty = index / @ncolumn
    tile_tip = @board[index]
    if tile_tip.empty_tile && !@game_end
      @empty_bitmap
      rect = @empty_bitmap.rect
      contents.blt(tx * item_width , ty * item_height ,@empty_bitmap, rect, 255)      
    else  
      rect = Rect.new(item_width * tile_tip.dposX , item_height * tile_tip.dposY , item_width, item_height)
      contents.blt(x + tx * item_width , y + ty * item_height ,@bitmap, rect, 255)
    end
  end  
  #--------------------------------------------------------------------------
  # ● 最後のタイルの描画
  #--------------------------------------------------------------------------
  def draw_last_tile
    return draw_last_tile_end if @game_end
    tx = (item_max - 1) % @ncolumn
    ty = (item_max - 1) / @ncolumn 
    return unless @board

    rect = Rect.new(item_width * (@ncolumn - 1) , item_height * (@nrow - 1) , item_width, item_height)
    contents.blt((tx + 1) * item_width , ty * item_height ,@bitmap, rect, 255)
    
  end 
  #--------------------------------------------------------------------------
  # ● ゲーム終了時の最後のタイルの描画
  #--------------------------------------------------------------------------
  def draw_last_tile_end
    
    tx = (item_max - 1) % @ncolumn
    ty = (item_max - 1) / @ncolumn
    return unless @board
    rect = @empty_bitmap.rect
    contents.blt((tx + 1) * item_width , ty * item_height ,@empty_bitmap, rect, 255)
    
  end   
  
  # ● ゲームの終了 
  def game_end
    @game_end = true
  end  
  #--------------------------------------------------------------------------
  # ● フレームの描画
  #--------------------------------------------------------------------------
  def draw_frame
    color = Color.new(0,0,0)
    bitmap = Bitmap.new(@ncolumn * item_width , @nrow * item_height) 
    for i in 0..@ncolumn * item_width 
      for j in 0..@nrow * item_height 
        bitmap.set_pixel(i,j,color) if i % item_width == 0 || j % item_height == 0
      end
    end  
    
    bitmap.set_pixel(x, y, color) 

    rect = bitmap.rect
    contents.blt(0 , 0 ,bitmap, rect, 255)
    
  end   
  # ● カーソルのBitmapの作成
  
  def make_bitmap_curosor
    color = Color.new(255,0,0)
    @cursor_bitmap = Bitmap.new( @ncolumn * item_width , @nrow * item_height) 
    for i in 0..@ncolumn * item_width
      for j in 0..@nrow * item_height
        if draw_board_curosr_line_thick( i , j)
          @cursor_bitmap.set_pixel(i, j, color) 
        end  
      end
    end         
  end  
  #--------------------------------------------------------------------------
  # ● カーソルの描画
  #--------------------------------------------------------------------------
  def draw_board_curosor
    rect = @cursor_bitmap.rect
    cx = index % @ncolumn 
    cy = index / @ncolumn
    contents.blt( cx * item_width  , cy * item_height, @cursor_bitmap, rect, 255)
  end 
  #--------------------------------------------------------------------------
  # ● カウントの描画
  #--------------------------------------------------------------------------
  def draw_count(x,y)
    left_count = @max_count - @count
    
    change_color(system_color)
    draw_text(x , y, 100 , line_height, "カウント", 0) 
    change_color(normal_color)
    draw_text(x + 100, y , 100 , line_height, @count, 2)
    change_color(system_color)
    draw_text(x , y + line_height , 100 , line_height, "残りカウント", 0) 
    change_color(count_color(left_count))
    draw_text(x + 100, y + line_height , 100 , line_height, left_count, 2)    
  end   
  #--------------------------------------------------------------------------
  # ● カウントの文字色を取得
  #--------------------------------------------------------------------------
  def count_color(lcount)
    return knockout_color if lcount < 0
    return crisis_color if lcount < @max_count / 5
    return normal_color
  end    
  #--------------------------------------------------------------------------
  # ● 座標の描画
  #--------------------------------------------------------------------------
  def draw_coordinate(x,y)
    return unless @board
    
    tile = @board[index]
    change_color(system_color)
    draw_text(x , y, 100 , line_height, "元の座標", 0) 
    coord = sprintf("%s , %s" , tile.id % @ncolumn , tile.id / @ncolumn )
    change_color(normal_color)
    draw_text(x + 100 , y, 100 , line_height, coord, 2)    
  
  end     
  #--------------------------------------------------------------------------
  # ● 太目のカーソルのライン
  #--------------------------------------------------------------------------
  def draw_board_curosr_line_thick(x,y)
    [- 1 , 0 ,1].each do |i|
      [- 1 , 0 ,1].each do |j|
        return true if draw_board_curosr_line(x + i,y + j)
      end  
      
    end  
    return false
  end      
  #--------------------------------------------------------------------------
  # ● カーソルのライン
  #--------------------------------------------------------------------------
  def draw_board_curosr_line(x,y)
    if x == 0 || x ==  item_width
      return y >= 0  && y <=  item_height
    end  
    if y == 0 || y ==  item_height
      return x >= 0  && x <= item_width
    end  
    return false
  end    
  #--------------------------------------------------------------------------
  # ● リフレッシュ
  #--------------------------------------------------------------------------
  def refresh
    contents.clear
#~     draw_board
    draw_all_items
    draw_frame unless @game_end
    draw_board_curosor unless @game_end
    draw_coordinate(320,0)
    draw_count(320 , line_height * 2) if @puzzle.count_flag
    draw_last_tile
#~     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
   
  


  
end  