#==============================================================================
# ■ Blink_Cursor 2.20    点滅を設定できるカーソル
# 07/03/16
#==============================================================================
class Window_Base < Window
  #--------------------------------------------------------------------------
  # ■ カーソル点滅の初期設定
  #     blink_min      : 一番薄い状態(透明度0〜255)
  #     blink_max      : 一番濃い状態(透明度0〜255)
  #     blink_speed    : 変化する速さ(0=点滅なし 大＝速い　小＝遅い)
  #     blink_stop     : 点滅の停止状態の透明度(カーソル選択済み)
  #--------------------------------------------------------------------------
  alias hidesp_initialize initialize
  def initialize(x, y, width, height)
    @blink_min = 50
    @blink_max = 250
    @blink_speed = 0
    @blink_stop = 160
    hidesp_initialize(x, y, width, height)
  end
  #--------------------------------------------------------------------------
  # ■ ウィンドウの更新
  #--------------------------------------------------------------------------
  alias hidesp_cursor_rect cursor_rect
  def cursor_rect=(cursor_rect)
    if self.cursor_rect.width == 0 or self.cursor_rect.height == 0 or 
      @index == -1 or self.visible == false
      # □カーソルウィンドウの削除
      break_cursor_icon_window
    else
      if @cursor_icon == nil or @cursor_icon.disposed?
        # □カーソルウィンドウの作成
        make_cursor_icon_window
        update_cursor_icon
      end
      # □カーソルの更新
      update_cursor_icon
    end
    #super
  end
  #--------------------------------------------------------------------------
  # ■ カーソルの更新
  #--------------------------------------------------------------------------
  def update
    # □ショップのアイテム個数バグの修正
    if @number == 1
      self.cursor_rect.width = self.cursor_rect.width
    end
    if @cursor_icon != nil && @cursor_icon.disposed? == false
      # □カーソル点滅アニメの開始
      if self.active == true
        if @count_icon == nil
          @cursor_icon.viewport.z = self.z
          @count_icon = @blink_max
        end
        @cursor_icon.opacity = @count_icon
        if @count_icon >= @blink_max
          @cop = -@blink_speed
        elsif @count_icon <= @blink_min
          @cop = @blink_speed
        end
        @count_icon += @cop
      # □カーソル点滅アニメの停止
      elsif (self.active == false or @index == 0) && @count_icon != nil
        @cursor_icon.opacity = @blink_stop
        @count_icon = nil
      end
    end
    @crs = @index
    super
  end
  #--------------------------------------------------------------------------
  # ■ X座標の更新
  #--------------------------------------------------------------------------
  def x=(x)
    make_cursor_icon(x,self.y)
    super
  end
  #--------------------------------------------------------------------------
  # ■ Y座標の更新
  #--------------------------------------------------------------------------
  def y=(y)
    make_cursor_icon(self.x,y)
    super
  end
  #--------------------------------------------------------------------------
  # ■ カーソルの開放
  #--------------------------------------------------------------------------
  def dispose_cursor
    if @cursor_icon != nil
      if @cursor_icon.bitmap != nil
        @cursor_icon.bitmap.dispose
      end
      @viewport.dispose
      @cursor_icon.dispose
      @cursor_icon = nil
    end
  end
  #--------------------------------------------------------------------------
  # ■ カーソルの削除
  #--------------------------------------------------------------------------
  def break_cursor_icon_window
    dispose_cursor
    @crs = nil
  end
  #--------------------------------------------------------------------------
  # ■ カーソルウィンドウの作成
  #--------------------------------------------------------------------------
  def make_cursor_icon_window
    @crs = nil
    @viewport = Viewport.new(0,0,640,480)
    @cursor_icon = Sprite.new(@viewport)
    @cursor_icon.bitmap = Bitmap.new(640,480)
    @cursor_icon.viewport.z = self.z
    @cursor_icon.opacity = @blink_max
    make_cursor_icon(self.x,self.y)
  end
  #--------------------------------------------------------------------------
  # ■ カーソルの作成
  #--------------------------------------------------------------------------
  def make_cursor_icon(x,y)
    if @cursor_icon != nil
      @cursor_icon.bitmap.clear
      x += 16
      y += 16
      w = self.cursor_rect.width
      h = self.cursor_rect.height
      skin = RPG::Cache.windowskin($game_system.windowskin_name)
      wsx,wsy,wwh,opa = 128,64,32,255
      # □カーソル画像の引き伸ばし描画（真中、左、右、上、下）
      @cursor_icon.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)
      @cursor_icon.bitmap.stretch_blt(Rect.new(x,y+2,2,h-4),skin,Rect.new(wsx,wsy+2,2,wwh-4),opa)
      @cursor_icon.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)
      @cursor_icon.bitmap.stretch_blt(Rect.new(x+2,y,w-4,2),skin,Rect.new(wsx+2,wsy,wwh-4,2),opa)
      @cursor_icon.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)
      # □カーソル画像の描画（左上、右上、左下、右下）
      @cursor_icon.bitmap.blt(x,y,skin,Rect.new(wsx,wsy,2,2),opa)
      @cursor_icon.bitmap.blt(x+w-2,y,skin,Rect.new(wsx+wwh-2,wsy,2,2),opa)
      @cursor_icon.bitmap.blt(x,y+h-2,skin,Rect.new(wsx,wsy+wwh-2,2,2),opa)
      @cursor_icon.bitmap.blt(x+w-2,y+h-2,skin,Rect.new(wsx+wwh-2,wsy+wwh-2,2,2),opa)
    end
  end
  #--------------------------------------------------------------------------
  # ■ カーソルの更新
  #--------------------------------------------------------------------------
  def update_cursor_icon
    if @cursor_icon != nil && (@crs != @index or @index == nil)
      @cursor_icon.y = self.cursor_rect.y
      @cursor_icon.x = self.cursor_rect.x
    end
  end
  #--------------------------------------------------------------------------
  # ■ カーソルウィンドウの削除
  #--------------------------------------------------------------------------
  alias hidesp_dispose dispose
  def dispose
    dispose_cursor
    super
  end
  #--------------------------------------------------------------------------
  # ■ カーソルウィンドウの可視化
  #--------------------------------------------------------------------------
  def visible=(visible)
    if visible == false
      dispose_cursor
    end
    super
  end
end
