#==============================================================================
# ■ 2nd_Cursor_Icon 2.20
# 07/03/16
# ※include HIDESP_CRS2 を追加したウィンドウのみ
#==============================================================================
class Window_Selectable < Window_Base
  #--------------------------------------------------------------------------
  # ■ index2を追加
  #--------------------------------------------------------------------------
  attr_reader   :index2
  def index2=(index2)
    @index2 = index2
  end
end

module HIDESP_CRS2
  #--------------------------------------------------------------------------
  # ■ オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize(x, y, width, height)
    super(x, y, width, height)
    @cursor2_on = false
    @index2 = -1
  end
  #--------------------------------------------------------------------------
  # ■ ウィンドウの更新
  #--------------------------------------------------------------------------
  def update
    if self.contents != nil && @cursor2 == nil
      if self.index2 >= 0
        # □第2カーソルウィンドウを作成
        @viewport = Viewport.new(self.x+16, self.y+16, self.width-32, self.height-32)
        @cursor2 = Sprite.new(@viewport)
        @cursor2.viewport.z = self.z
        @cursor2.bitmap = Bitmap.new(self.contents.width,self.contents.height)
        # □第2カーソルアイコン
        @viewport2 = Viewport.new(self.x, self.y+16, self.width-16, self.height-32)
        @cursor_icon2 = Sprite.new(@viewport2)
        @cursor_icon2.viewport.z = self.z + 2
        @cursor_icon2.bitmap = Bitmap.new(self.contents.width,self.contents.height)
      end
    elsif self.contents != nil
      if @cursor2.bitmap != nil
        # □カーソルindex2で分岐
        if self.index2 < 0 && @cursor2_on == true
          @cursor2_on = false
          @cursor2.bitmap.clear
          @cursor_icon2.bitmap.clear
        elsif self.index2 >= 0 && @cursor2_on == false
          # □途中でzを変更している場合の不具合を解消
          @cursor2.viewport.z = self.z
          x = self.cursor_rect.x + self.ox
          y = self.cursor_rect.y + self.oy
          w = self.cursor_rect.width
          h = self.cursor_rect.height
          # □カーソル画像の切り出し(x,y,(w&h),opacity)
          skin = RPG::Cache.windowskin($game_system.windowskin_name)
          wsx,wsy,wwh,opa = 128,64,32,160
          # □カーソル画像の引き伸ばし描画（真中、左、右、上、下）
          @cursor2.bitmap.stretch_blt(Rect.new(x+2,y+2,w-4,h-4),skin,Rect.new(wsx+2,wsy+2,wwh-4,wwh-4),opa)
          @cursor2.bitmap.stretch_blt(Rect.new(x,y+2,2,h-4),skin,Rect.new(wsx,wsy+2,2,wwh-4),opa)
          @cursor2.bitmap.stretch_blt(Rect.new(x+w-2,y+2,2,h-4),skin,Rect.new(wsx+wwh-2,wsy+2,2,wwh-4),opa)
          @cursor2.bitmap.stretch_blt(Rect.new(x+2,y,w-4,2),skin,Rect.new(wsx+2,wsy,wwh-4,2),opa)
          @cursor2.bitmap.stretch_blt(Rect.new(x+2,y+h-2,w-4,2),skin,Rect.new(wsx+2,wsy+wwh-2,wwh-4,2),opa)
          # □カーソル画像の描画（左上、右上、左下、右下）
          @cursor2.bitmap.blt(x,y,skin,Rect.new(wsx,wsy,2,2),opa)
          @cursor2.bitmap.blt(x+w-2,y,skin,Rect.new(wsx+wwh-2,wsy,2,2),opa)
          @cursor2.bitmap.blt(x,y+h-2,skin,Rect.new(wsx,wsy+wwh-2,2,2),opa)
          @cursor2.bitmap.blt(x+w-2,y+h-2,skin,Rect.new(wsx+wwh-2,wsy+wwh-2,2,2),opa)
          # □カーソルアイコンの描画
          crb = RPG::Cache.windowskin("cursor")
          @cursor_icon2.viewport.z = self.z + 2
          @cursor_icon2.bitmap.blt(x,y,crb,Rect.new(0,0,crb.width, crb.height))
          @cursor2_on = true
        end
      end
    end
    super
    # □カーソル位置の更新
    if @cursor2 != nil
      @cursor2.oy = self.oy
      @cursor_icon2.oy = self.oy
    end
  end
  #--------------------------------------------------------------------------
  # ■ ウィンドウの可視状態
  #--------------------------------------------------------------------------
  def visible=(visible)
    super
    if @cursor2 != nil
      @cursor2.visible = visible
      @cursor_icon2.visible = visible
    end
  end
  #--------------------------------------------------------------------------
  # ■ ウィンドウの削除
  #--------------------------------------------------------------------------
  def dispose
    super
    if @cursor2 != nil
      @viewport.dispose
      @viewport2.dispose
      if @cursor2.bitmap != nil
        @cursor2.bitmap.dispose
      end
      if @cursor_icon2.bitmap != nil
        @cursor_icon2.bitmap.dispose
      end
      @cursor2.dispose
      @cursor_icon2.dispose
    end
  end
end
