#==============================================================================
# ■ Picture_Blink_Cursor vol.1.60
#------------------------------------------------------------------------------
# 12/04/22
#==============================================================================
class Picture_Blink_Cursor
  #--------------------------------------------------------------------------
  # ■ 公開インスタンス変数
  #--------------------------------------------------------------------------
  attr_accessor :active                   # 更新フラグ
  attr_accessor :visible                  # 可視フラグ
  attr_accessor :blink                    # 点滅フラグ
  attr_accessor :blink_active             # 点滅更新フラグ
  attr_accessor :draw_back                # カーソル背景描画フラグ
  attr_reader   :index                    # カーソル位置
  attr_accessor :zero                     # カーソルゼロ位置フラグ
  attr_accessor :turn                     # カーソル折り返しフラグ
  attr_accessor :key                      # カーソル値
  attr_accessor :helps                    # ヘルプ項目の配列を格納
  attr_accessor :help_id                  # ヘルプ位置
  attr_accessor :help_bitmap              # ヘルプウィンドウのBitmapを格納
  attr_accessor :exchange                 # 通常色/無効色の切り替えフラグ
  attr_accessor :h_font                   # ヘルプフォントを格納
  attr_accessor :items                    # 項目を格納
  attr_accessor :font                     # 項目フォントを格納
  attr_accessor :d_color                  # 無効色
  attr_accessor :f_color                  # 点滅文字色
  attr_accessor :back_color               # 点滅カーソル色
  attr_accessor :back_alpha               # 点滅カーソル色の透明度
  attr_accessor :blink_stop               # 点滅ウィンドウの透明度(停止値)
  attr_accessor :blink_min                # 点滅ウィンドウの透明度(最小値)
  attr_accessor :blink_max                # 点滅ウィンドウの透明度(最大値)
  attr_accessor :blink_plus               # 点滅ウィンドウの透明度(加減値)
  attr_accessor :x                        # X座標を格納
  attr_accessor :y                        # Y座標を格納
  attr_accessor :z                        # Z座標を格納
  attr_accessor :width                    # 幅を格納
  attr_accessor :height                   # 高さを格納
  attr_reader   :item_max                 # 項目数
  attr_reader   :column_max               # 列数
  attr_accessor :cz                       # アイコンZ座標の補正値
  attr_accessor :bz                       # 点滅カーソルZ座標の補正値
  attr_accessor :hz                       # ヘルプウィンドウZ座標の補正値
  attr_accessor :cursor_x                 # アイコンX座標の補正値
  attr_accessor :index2                   # 第2カーソルアイコンインデックス
  attr_accessor :active2nd                # 第2カーソルアイコン更新フラグ
  attr_accessor :background               # 背景画像
  #--------------------------------------------------------------------------
  # ■ オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize(x = 0,y = 0,width = 640,height = 480,
    cx = 0,cy = 0,max = 1,column = 1,back = false)
    #…………………………………………………………………………………………………
    # ■初期設定(書き換え可)
    #…………………………………………………………………………………………………
    @blink_stop = 200
    @blink_min = 10
    @blink_max = 200
    @blink_plus = 10
    @d_color = Color.new(80,80,80,255)
    @f_color = Color.new(255,255,0,255)
    @back_alpha = 200
    @back_color = Color.new(255,255,255,255)
    @cursor_x = 16
    # □アイコンカーソルの初期値
    @csr3_animas = [0,1,2,3,4,4,3,2,1,0]
    @csr3_speed = 2
    @cz = 1 ; @bz = 1 ; @hz = 2
    #…………………………………………………………………………………………………
    # ■背景ウィンドの場合 x = Bitmap
    #…………………………………………………………………………………………………
    if x.is_a?(Numeric)
      @background = nil
    else
      background = x
      x = 0
    end
    #…………………………………………………………………………………………………
    @item_max = max
    @difference = 0
    if max%column > 0
      @difference = column-(max%column)
      @item_max += @difference
    end
    @column_max = column
    @px = x ; @py = y
    @width = width ; @height = height
    @ix = cx ; @iy = cy
    @cx = cx < 0 ? width : cx+width
    @cy = cy < 0 ? height : cy+height
    @line_max = @item_max/@column_max
    @back = back
    @draw_back = true
    @picture_back = nil
    #…………………………………………………………………………………………………
    # ■点滅背景ウィンドウの作成
    #…………………………………………………………………………………………………
    if @back
      @picture_back = Sprite.new(Viewport.new(x,y,@cx*@column_max+@cx,@cy*@line_max))
      @picture_back.bitmap = Bitmap.new(@cx*@column_max+@cx,@cy*@line_max)
      @picture_back.visible = false
    end
    #…………………………………………………………………………………………………
    # ■点滅ウィンドウの作成
    #…………………………………………………………………………………………………
    if background == nil
      @picture_blink = Sprite.new(Viewport.new(x,y,@cx,@cy))
      @picture_blink.bitmap = Bitmap.new(@cx*@column_max,@cy*@line_max)
      @picture_blink.visible = false
      @picture_blink.viewport.z + 1
      @picture_blink_2nd_viewport = Viewport.new(x,y,@cx,@cy)
      @picture_blink_2nd_viewport.z = @picture_blink.viewport.z
    #…………………………………………………………………………………………………
    # ■背景ウィンドウの作成
    #…………………………………………………………………………………………………
    else
      b = background
      @background = Bitmap.new(b.width,b.height)
      @background.blt(0,0,b,Rect.new(0,0,b.width,b.height))
      @picture_blink = Sprite.new(Viewport.new(0,0,b.width,b.height))
      @picture_blink.bitmap = Bitmap.new(b.width,b.height)
      @picture_blink.bitmap.blt(0,0,b,Rect.new(0,0,b.width,b.height))
    end
    #…………………………………………………………………………………………………
    @x = x
    @y = y
    @z = @picture_blink.viewport.z
    @picture_cursor = false
    @draw_blink = false
    @turn = true
    @blink = true
    @exchange = [false]*@item_max
    @helps = []
    @help_id = nil
    @items = [""]*max
    @active = false
    @visible = false
    @zero = false
    @key = nil
    @index = -1
    @index2 = -1
    @active2nd = false
    @csr2nd_set = false
    @blink2nd_set = false
    @a_color = nil
    # □点滅の初期化
    if @background == nil
      reset
    end
    @blink_active = true
  end
  #--------------------------------------------------------------------------
  # ■ 再描画
  #--------------------------------------------------------------------------
  def refresh
    @picture_blink.bitmap.clear
    unless nil?(@picture_back)
      @picture_back.bitmap.clear
    end
    if @draw_blink
      draw_color(@back_color)
    end
    if @picture_cursor
      draw_cursor(@picture)
    end
    draw_item(@items,@font)
  end
  #--------------------------------------------------------------------------
  # ■ 点滅色の設定
  #--------------------------------------------------------------------------
  def draw_color(color = @back_color,alpha = @back_alpha)
    color.alpha = 200
    for i in 0...@item_max
      x = (i % @column_max) * @cx
      y = (i / @column_max) * @cy
      @picture_blink.bitmap.fill_rect(Rect.new(x,y,@width,@height),color)
    end
    @draw_blink = true
    @back_color = color
  end
  #--------------------------------------------------------------------------
  # ■ アイコンカーソルの作成
  #--------------------------------------------------------------------------
  def draw_icon_cursor(pic,z = @picture_blink.viewport.z)
    @csr3_count = 0
    if nil?(@csr4_icon)
      wh = pic.height/2 ; @csr4_wh = wh
      # □第2カーソルアイコンViewportを作成
      @csr4_2nd_viewport = Viewport.new(0,0,wh,wh)
      @csr4_icon = Sprite.new(Viewport.new(0,0,wh,wh))
      @csr4_icon.visible = false
      @csr4_icon.bitmap = pic
      @csr4_icon.viewport.z = z + @cz
      # □カーソルの矩形更新
      update_cursor_rect
    end
  end
  #--------------------------------------------------------------------------
  # ■ 点滅色カーソルの描画
  #--------------------------------------------------------------------------
  def draw_color_cursor(color = @d_color,items = nil,font = nil)
    @blink_active = true
    @key = 0
    draw_color(color)
    if items != nil && font != nil
      draw_item(items,font)
    end
  end
  #--------------------------------------------------------------------------
  # ■ 点滅画像の描画
  #--------------------------------------------------------------------------
  def draw_flash(skin,x,y,w,h,f = 16)
    @blink_active = true
    draw_back_rect(@picture_blink.bitmap,skin,x,y,w,h,f)
  end
  #--------------------------------------------------------------------------
  # ■ 引き伸ばし画像の描画
  #--------------------------------------------------------------------------
  def draw_back_rect(window,skin,x,y,w,h,f = 16)
    sw = skin.width
    sh = skin.height
    # □カーソル画像の引き伸ばし描画（真中、左、右、上、下）
    window.stretch_blt(Rect.new(x+f,y+f,w-(f*2),h-(f*2)),skin,Rect.new(f,f,sw-(f*2),sh-(f*2)))
    window.stretch_blt(Rect.new(x,y+f,f,h-(f*2)),skin,Rect.new(0,f,f,sh-(f*2)))
    window.stretch_blt(Rect.new(x+w-f,y+f,f,h-(f*2)),skin,Rect.new(sw-f,f,f,sh-(f*2)))
    window.stretch_blt(Rect.new(x+f,y,w-(f*2),f),skin,Rect.new(f,0,sw-(f*2),f))
    window.stretch_blt(Rect.new(x+f,y+h-f,w-(f*2),f),skin,Rect.new(f,sh-f,sw-(f*2),f))
    # □カーソル画像の描画（左上、右上、左下、右下）
    window.blt(x,y,skin,Rect.new(0,0,f,f))
    window.blt(x+w-f,y,skin,Rect.new(sw-f,0,f,f))
    window.blt(x,y+h-f,skin,Rect.new(0,sh-f,f,f))
    window.blt(x+w-f,y+h-f,skin,Rect.new(sw-f,sh-f,f,f))
  end
  #--------------------------------------------------------------------------
  # ■ フレーム画像の描画(背景)
  #--------------------------------------------------------------------------
  def draw_background(skin,x,y,w,h,f = 16)
    draw_back_rect(@background,skin,x,y,w,h,f = 16)
    draw_back_rect(@picture_blink.bitmap,skin,x,y,w,h,f = 16)
  end
  #--------------------------------------------------------------------------
  # ■ 画像カーソルの描画
  #--------------------------------------------------------------------------
  def draw_cursor(pic,items = nil,font = nil,icon = nil,back = true)
    @key = 0 ; @draw_back = draw_back
    @back = back
    # □背景ウィンドウの作成
    if @back && nil?(@picture_back)
      @picture_back = Sprite.new(Viewport.new(@px,@py,@cx*@column_max+@cx,@cy*@line_max))
      @picture_back.bitmap = Bitmap.new(@cx*@column_max+@cx,@cy*@line_max)
      @picture_back.visible = false
      self.z = @z
    end
    for i in 0...@item_max
      # □点滅画像の描画
      x = (i % @column_max) * @cx
      y = (i / @column_max) * @cy
      pw = pic.width/3 ; ph = pic.height
      skin = Bitmap.new(pw,ph)
      skin.blt(0,0,pic,Rect.new(pw*2,0,pw,ph))
      draw_flash(skin,x,y,@width,@height,pw/3)
      # □背景描画フラグがONの場合
      unless nil?(@picture_back)
        if @draw_back
          x = (i % @column_max) * (@cx + @ix)
          y = (i / @column_max) * (@cy + @iy)
          # □背景画像の描画
          skin = Bitmap.new(pw,ph)
          # □カーソル背景
          skin.blt(0,0,pic,Rect.new(pw,0,pw,ph))
          # □カーソル枠
          skin.blt(0,0,pic,Rect.new(0,0,pw,ph))
          draw_back_rect(@picture_back.bitmap,skin,x,y,@width,@height,pw/3)
        else
          @picture_back.bitmap.clear
        end
      end
    end
    @picture_cursor = true
    @picture = pic
    # □項目の描画
    if items != nil && font != nil
      draw_item(items,font)
    end
    @icon = icon
    # □アイコンカーソルの作成
    if @icon != nil
      draw_icon_cursor(@icon)
    end
  end
  #--------------------------------------------------------------------------
  # ■ 項目の描画
  #--------------------------------------------------------------------------
  def draw_item(items,font = @font,color = @f_color)
    if font == nil
      return
    end
    # □フォントの通常色を格納
    unless @a_color.is_a?(Color)
      @a_color = font.color.dup
    end
    for i in 0...items.size
      # □テキストの描画
      draw_item_text(i,items,font,color = @f_color)
    end
    @font = font
    @items = items
  end
  #--------------------------------------------------------------------------
  # ■ テキストの項目の描画
  #--------------------------------------------------------------------------
  def draw_item_text(i,items,font,color = @f_color)
    font.color = @exchange[i] ? @d_color : @a_color
    text = items[i]
    x = (i % @column_max) * @cx
    y = (i / @column_max) * @cy
    a_font = font.dup
    a_font.shadow = false
    a_font.round = false
    a_font.color = color
    @picture_blink.bitmap.draw_cache_text(x,y,@width,@height,text,1,a_font)
    unless nil?(@picture_back)
      x = (i % @column_max) * (@cx + @ix)
      y = (i / @column_max) * (@cy + @iy)
      @picture_back.bitmap.draw_cache_text(x,y,@width,@height,text,1,font)
    end
  end
  #--------------------------------------------------------------------------
  # ■ テキストの追加描画
  #--------------------------------------------------------------------------
  def draw_text(x,y,w,h,text,align,fonts)
    w = w == 0 ? fonts.text_width(text) : w
    h = h == 0 ? fonts.text_height(text) : h
    y = y+(32/2)-(h/2)
    @picture_blink.bitmap.fill_rect(Rect.new(x,y,w,h),Color.new(0,0,0,0))
    if @background != nil
      @picture_blink.bitmap.blt(x,y,@background,Rect.new(x,y,w,h))
    end
    @picture_blink.bitmap.draw_cache_text(x,y,w,h,text,align,fonts)
  end
  #--------------------------------------------------------------------------
  # ■ 1枚画像の追加描画
  #--------------------------------------------------------------------------
  def draw_picture(pic,x = 0,y = 0,rect = Rect.new(0,0,640,480),opa = 255)
    if @background != nil
      @picture_blink.bitmap.blt(x,y,@background,Rect.new(x,y,rect.width,rect.height))
    else
      @picture_blink.bitmap.fill_rect(rect,Color.new(0,0,0,0))
    end
    @picture_blink.bitmap.blt(x,y,pic,rect,opa)
  end
  #--------------------------------------------------------------------------
  # ■ 点滅の起動
  #--------------------------------------------------------------------------
  def start(z = @picture_blink.viewport.z)
    self.index = 0
    self.active = true
    self.visible = true
    self.opacity = @blink_min
    @blink_loop = false
    self.z = z
  end
  #--------------------------------------------------------------------------
  # ■ 点滅の初期化
  #--------------------------------------------------------------------------
  def reset
    @picture_blink.bitmap.clear
    self.opacity = @blink_min
    @blink_loop = false
    @blink_active = false
    unless nil?(@picture_back)
      @picture_back.bitmap.clear
    end
  end
  #--------------------------------------------------------------------------
  # ■ カーソルウィンドウの可視化
  #--------------------------------------------------------------------------
  def visible=(visible)
    @visible = visible
    if @index != -1
      # □点滅ウィンドウの可視化
      @picture_blink.visible = visible
      # □第2カーソルアイコンの可視化
      unless nil?(@csr4_icon)
        @csr4_icon.visible = visible
      end
    else
      # □点滅ウィンドウの可視化
      @picture_blink.visible = visible
      # □第2カーソルの開放
      unless nil?(@picture_blink_2nd)
        @blink2nd_set = false
        dispose_blink2nd
      end
      # □第2カーソルアイコンの開放
      unless nil?(@csr4_icon_2nd)
        @csr2nd_set = false
        dispose_csr2nd
      end
    end
    unless nil?(@picture_back)
      @picture_back.visible = visible
    end
  end
  #--------------------------------------------------------------------------
  # ■ 可視/不可視を返す
  #--------------------------------------------------------------------------
  def visible
    @visible = @picture_blink.visible
    return @visible
  end
  #--------------------------------------------------------------------------
  # ■ ヘルプウィンドウの可視化
  #--------------------------------------------------------------------------
  def help_visible=(help_visible)
    unless nil?(@picture_help)
      @picture_help.visible = help_visible
    end
  end
  #--------------------------------------------------------------------------
  # ■ ウィンドウの更新
  #--------------------------------------------------------------------------
  def active=(active)
    @active = active
    unless nil?(@picture_help)
      @picture_help.visible = @active
    end
    if @active
      unless nil?(@picture_help)
        @picture_help.visible |= @index != -1
      end
      unless nil?(@csr4_icon)
        @csr4_icon.viewport.oy = 0
      end
    else
      self.opacity = @blink_stop
      unless nil?(@picture_help)
        @picture_help.visible = false
      end
      unless nil?(@csr4_icon)
        @csr4_icon.viewport.oy = @csr4_wh
      end
      # □第2カーソルの開放
      unless nil?(@picture_blink_2nd)
        @blink2nd_set = false
        dispose_blink2nd
      end
      # □第2カーソルアイコンの開放
      unless nil?(@csr4_icon_2nd)
        @csr2nd_set = false
        dispose_csr2nd
      end
    end
  end
  #--------------------------------------------------------------------------
  # ■ X座標の更新
  #--------------------------------------------------------------------------
  def x=(x)
    @picture_blink.x = x
    unless nil?(@picture_back)
      @picture_back.x = x
    end
    unless nil?(@csr4_icon)
      @csr4_icon.x = x
    end
    # □第2カーソル
    unless nil?(@picture_blink_2nd)
      @picture_blink_2nd.x = x
    end
    # □第2カーソルアイコン
    unless nil?(@csr4_icon_2nd)
      @csr4_icon_2nd.x = x
    end
    @x = x
  end
  #--------------------------------------------------------------------------
  # ■ Y座標の更新
  #--------------------------------------------------------------------------
  def y=(y)
    @picture_blink.y = y
    unless nil?(@picture_back)
      @picture_back.y = y
    end
    unless nil?(@csr4_icon)
      @csr4_icon.y = y
    end
    # □第2カーソル
    unless nil?(@picture_blink_2nd)
      @picture_blink_2nd.y = y
    end
    # □第2カーソルアイコン
    unless nil?(@csr4_icon_2nd)
      @csr4_icon_2nd.y = y
    end
    @y = y
  end
  #--------------------------------------------------------------------------
  # ■ Z座標の更新
  #--------------------------------------------------------------------------
  def z=(z)
    @picture_blink.viewport.z = z + @bz
    unless nil?(@picture_back)
      @picture_back.viewport.z = z
    end
    unless nil?(@csr4_icon)
      @csr4_icon.viewport.z = z + @cz
    end
    # □第2カーソル
    unless nil?(@picture_blink_2nd)
      @picture_blink_2nd.viewport.z = @picture_blink.viewport.z
    end
    # □第2カーソルアイコン
    unless nil?(@csr4_icon_2nd)
      @csr4_icon_2nd.viewport.z = @csr4_icon.viewport.z
    end
    @z = z
  end
  #--------------------------------------------------------------------------
  # ■ 透明度を更新
  #--------------------------------------------------------------------------
  def opacity=(opacity)
    @picture_blink.opacity = opacity
  end
  #--------------------------------------------------------------------------
  # ■ フラッシュの更新
  #--------------------------------------------------------------------------
  def flash(color = Color.new(0,0,0,255),opacity = 255)
    @picture_blink.flash(color,opacity)
  end
  #--------------------------------------------------------------------------
  # ■ bitmap
  #--------------------------------------------------------------------------
  def bitmap
    return @picture_blink.bitmap
  end
  #--------------------------------------------------------------------------
  # ■ back_bitmap
  #--------------------------------------------------------------------------
  def back_bitmap
    return @picture_back.bitmap
  end
  #--------------------------------------------------------------------------
  # ■ help_bitmap
  #--------------------------------------------------------------------------
  def help_bitmap
    return @picture_help.bitmap
  end
  #--------------------------------------------------------------------------
  # ■ カーソル位置の設定
  #--------------------------------------------------------------------------
  def index=(index)
    @index = index
    # □ヘルプテキストを更新
    update_help
    # □カーソルの矩形を更新
    update_cursor_rect
    # □カーソルの可視/不可視
    @picture_blink.visible = index != -1
    unless nil?(@csr4_icon)
      @csr4_icon.visible = index != -1
    end
    if @index < 0
      # □第2カーソルの開放
      unless nil?(@picture_blink_2nd)
        @blink2nd_set = false
        dispose_blink2nd
      end
      # □第2カーソルアイコンの開放
      unless nil?(@csr4_icon_2nd)
        @csr2nd_set = false
        dispose_csr2nd
      end
    end
  end
  #--------------------------------------------------------------------------
  # ■ ヘルプの再描画
  #--------------------------------------------------------------------------
  def helps=(helps)
    @helps = helps
    make_helps(@hx,@hy,@helps,@h_font)
  end
  #--------------------------------------------------------------------------
  # ■ カーソルX座標の取得
  #--------------------------------------------------------------------------
  def window_x(index = @index)
    return index % @column_max * (@cx + @ix) + @px
  end
  #--------------------------------------------------------------------------
  # ■ カーソルY座標の取得
  #--------------------------------------------------------------------------
  def window_y(index = @index)
    return index / @column_max * (@cy + @iy) + @py
  end
  #--------------------------------------------------------------------------
  # ■ カーソルの矩形更新
  #--------------------------------------------------------------------------
  def update_cursor_rect
    #…………………………………………………………………………………………………
    # ■カーソルの不可視
    #…………………………………………………………………………………………………
    if @zero
      self.active = false
      self.visible = false
      return
    end
    #…………………………………………………………………………………………………
    # ■カーソルの座標を計算
    #…………………………………………………………………………………………………
    x = window_x
    y = window_y
    @picture_blink.viewport.rect.x = x
    @picture_blink.viewport.rect.y = y
    @picture_blink.viewport.ox = @index % @column_max * @cx
    @picture_blink.viewport.oy = @index / @column_max * @cy
    #…………………………………………………………………………………………………
    # ■第2カーソルを作成
    #…………………………………………………………………………………………………
    if nil?(@picture_blink_2nd)
      if @index2 > -1 && @blink2nd_set == false
        @picture_blink_2nd = Sprite.new(@picture_blink_2nd_viewport)
        @picture_blink_2nd.bitmap = @picture_blink.bitmap.dup
        # □座標を取得
        @picture_blink_2nd.viewport.z = @picture_blink.viewport.z
        @picture_blink_2nd.viewport.rect.x = @picture_blink.viewport.rect.x
        @picture_blink_2nd.viewport.rect.y = @picture_blink.viewport.rect.y
        @picture_blink_2nd.viewport.ox = @picture_blink.viewport.ox
        @picture_blink_2nd.viewport.oy = @picture_blink.viewport.oy
        @picture_blink_2nd.opacity = @blink_stop
        @blink2nd_set = true
      end
    end
    #…………………………………………………………………………………………………
    # ■カーソルアイコンの座標を計算
    #…………………………………………………………………………………………………
    unless nil?(@csr4_icon)
      # □X座標を自動補正(画面外の場合)
      cx = window_x(0) - @cursor_x < 0 ? (window_x(0) - @cursor_x).abs : 0
      @csr4_icon.viewport.rect.x = x - @cursor_x + cx
      h0 = @picture_blink.viewport.rect.height / 2
      h1 = @csr4_icon.viewport.rect.height / 2
      # □Y座標を自動補正(画面外の場合)
      cy = window_y(0) + (h0 - h1) < 0 ? (window_y(0) + (h0 - h1)).abs : 0
      @csr4_icon.viewport.rect.y = y + (h0 - h1) + cy
    end
    #…………………………………………………………………………………………………
    # ■第2カーソルアイコンを作成
    #…………………………………………………………………………………………………
    if nil?(@csr4_icon_2nd)
      if @index2 > -1 && @csr2nd_set == false
        @csr4_icon_2nd = Sprite.new(@csr4_2nd_viewport)
        @csr4_icon_2nd.bitmap = @csr4_icon.bitmap.dup
        # □座標を取得
        @csr4_icon_2nd.viewport.z = @csr4_icon.viewport.z
        @csr4_icon_2nd.viewport.rect.x = @csr4_icon.viewport.rect.x
        @csr4_icon_2nd.viewport.rect.y = @csr4_icon.viewport.rect.y
        @csr4_icon_2nd.viewport.ox = @csr4_icon.viewport.ox
        @csr4_icon_2nd.viewport.oy = @csr4_icon.viewport.oy
        @csr2nd_set = true
      end
    end
  end
  #--------------------------------------------------------------------------
  # ■ 第2カーソルの開放
  #--------------------------------------------------------------------------
  def dispose_blink2nd
    # □第2カーソルアイコンを解放
    unless nil?(@picture_blink_2nd)
      if @picture_blink_2nd.bitmap != nil
        @picture_blink_2nd.bitmap.dispose
      end
      @picture_blink_2nd.dispose
    end
  end
  #--------------------------------------------------------------------------
  # ■ 第2カーソルアイコンの開放
  #--------------------------------------------------------------------------
  def dispose_csr2nd
    # □第2カーソルアイコンを解放
    unless nil?(@csr4_icon_2nd)
      if @csr4_icon_2nd.bitmap != nil
        @csr4_icon_2nd.bitmap.dispose
      end
      @csr4_icon_2nd.dispose
    end
  end
  #--------------------------------------------------------------------------
  # ■ 更新
  #--------------------------------------------------------------------------
  def update
    # □カウントの更新
    update_count
    # □カーソルアイコンの更新
    update_cursor
    # □点滅更新
    update_blink
    # □キー入力更新
    update_key
    # □キー入力の初期化
    if @key != nil
      @key = 0
    end
    # □ヘルプテキストを更新
    update_help
    # □カーソルを更新
    update_cursor_rect
  end
  #--------------------------------------------------------------------------
  # ■ カーソルアイコンの更新
  #--------------------------------------------------------------------------
  def update_cursor
    unless nil?(@csr4_icon)
      # □カーソルアニメの開始
      @csr4_icon.viewport.ox = @csr3_animas[@csr3_count/@csr3_speed]*@csr4_wh
      @csr3_count += 1
      if @csr3_count == @csr3_animas.size*@csr3_speed
        @csr3_count = 0
      end
    end
    # □第2カーソルアイコンの更新
    unless nil?(@csr4_icon_2nd)
      # □第2カーソルアイコンの開放
      if @index2 < 0 && @csr2nd_set
        @csr2nd_set = false
        dispose_csr2nd
        return
      end
      # □第2カーソルアイコンアニメの開始
      if @active2nd
        @csr4_icon_2nd.viewport.oy = 0
        @csr4_icon_2nd.viewport.ox = @csr4_icon.viewport.ox
      else
        @csr4_icon_2nd.viewport.oy = @csr4_wh
      end
    end
  end
  #--------------------------------------------------------------------------
  # ■ 点滅開始
  #--------------------------------------------------------------------------
  def blink_start
    @blink_loop = false
    self.opacity = @blink_min
  end
  #--------------------------------------------------------------------------
  # ■ 点滅更新
  #--------------------------------------------------------------------------
  def update_blink
    @blink_active = true
    if self.active && @blink && (@key == 0 or @key.nil?)
      if @blink_loop
        if @picture_blink.opacity > @blink_min
          @picture_blink.opacity -= @blink_plus
        else
          @blink_loop = false
        end
      else
        if @picture_blink.opacity < @blink_max
          @picture_blink.opacity += @blink_plus
        else
          @blink_loop = true
        end
      end
    end
    # □第2カーソルの更新
    unless nil?(@picture_blink_2nd)
      # □第2カーソルの開放
      if @index2 < 0 && @blink2nd_set
        @blink2nd_set = false
        dispose_blink2nd
        return
      end
      # □第2カーソル点滅更新
      if @active2nd
        @picture_blink_2nd.opacity = @picture_blink.opacity
      end
    end
  end
  #--------------------------------------------------------------------------
  # ■ 点滅更新の終了
  #--------------------------------------------------------------------------
  def blink_end
    if self.active && @blink && (@key == 0 or @key.nil?) && @blink_active
      if @blink_loop
        if @picture_blink.opacity > @blink_min
          @picture_blink.opacity -= @blink_plus
        else
          # □点滅の初期化
          reset
        end
      else
        if @picture_blink.opacity < @blink_max
          @picture_blink.opacity += @blink_plus
        else
          @blink_loop = true
        end
      end
    end
  end
  #--------------------------------------------------------------------------
  # ■ キー入力更新
  #--------------------------------------------------------------------------
  def update_key
    if self.active && @item_max > 0 && @index >= 0 && @zero == false && @key != nil
      #…………………………………………………………………………………………………
      # ■ 下 ボタンが押された場合
      #…………………………………………………………………………………………………
      if Input.repeat?(Input::DOWN)
        @key = 2 ; @se_index = @index
        i = (@index / @column_max + 1) * @column_max + @index % @column_max
        if @item_max - @difference > i
          @index = i
        elsif @turn
          @index = @index % @column_max
        else
          @zero = true
        end
        if @se_index != @index or @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
          i = @item_max - @column_max + @index % @column_max
          if i > @item_max - @difference - 1 
            @index = i - @column_max
          else
            @index = i
          end
        else
          @zero = true
        end
        if @se_index != @index or @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
        if @item_max - @difference - 1 < @index + 1
          @index = (@index - (@index%@column_max))
        else
          @index = (@index + 1) / @column_max == @index / @column_max ? @index + 1 : i0
        end
        if @se_index != @index or @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
        if @index%@column_max == 0 && (@index - 1) + @column_max > @item_max - @difference - 1
          @index = @item_max - @difference - 1
        else
          @index = (@index - 1) / @column_max == @index / @column_max ? @index - 1 : i0
        end
        if @se_index != @index or @zero
          $game_system.se_play($data_system.cursor_se)
        end
        return
      end
    end
  end
  #--------------------------------------------------------------------------
  # ■ カウントを返す
  #--------------------------------------------------------------------------
  def count?
    return @count
  end
  #--------------------------------------------------------------------------
  # ■ カウントを更新
  #--------------------------------------------------------------------------
  def update_count
    if Graphics.frame_count / Graphics.frame_rate != @time_count
      @count = true
    else
      @count = false
    end
    @time_count = Graphics.frame_count / Graphics.frame_rate
  end
  #--------------------------------------------------------------------------
  # ■ 点滅を返す(Bitmapの点滅)
  #--------------------------------------------------------------------------
  def opacity?
    return @picture_blink.opacity <= @blink_min
  end
  #--------------------------------------------------------------------------
  # ■ ヘルプBitmapを作成
  #--------------------------------------------------------------------------
  def make_helps(x,y,helps = @helps,font = @h_font)
    @hx = x ; @hy = y
    @helps = helps ; @h_font = font
    # □ヘルプウィンドウの作成
    if nil?(@picture_help)
      @picture_help = Sprite.new(Viewport.new(x,y,640,32))
      @picture_help.viewport.z = @picture_blink.viewport.z + @hz
    end
    # □ヘルプBitmapを作成/初期化
    if nil?(@picture_help.bitmap)
      @picture_help.bitmap = Bitmap.new(640-4,32*@helps.size)
    else
      @picture_help.bitmap.clear
    end
    @picture_help.oy = @index*32
    # □ヘルプを描画
    for i in 0...@helps.size
      draw_help(i)
    end
    update_help
  end
  #--------------------------------------------------------------------------
  # ■ ヘルプを描画
  #--------------------------------------------------------------------------
  def draw_help(i,helps = @helps,font = @h_font)
    @helps = helps ; @h_font = font
    @picture_help.bitmap.draw_cache_text(4,i*32,640,32,@helps[i],0,@h_font)
  end
  #--------------------------------------------------------------------------
  # ■ ヘルプテキスト更新
  #--------------------------------------------------------------------------
  def update_help
    unless nil?(@picture_help)
      index = @help_id == nil ? @index : @help_id
      @picture_help.oy = index*32
      @picture_help.visible = @active
    end
  end
  #--------------------------------------------------------------------------
  # ■ 解放を返す
  #--------------------------------------------------------------------------
  def nil?(data)
    return (data.nil? or data.disposed?)
  end
  #--------------------------------------------------------------------------
  # ■ 解放
  #--------------------------------------------------------------------------
  def dispose
    # □点滅ウィンドウを解放
    if @picture_blink.bitmap != nil
      @picture_blink.bitmap.dispose
    end
    unless nil?(@picture_blink)
      @picture_blink.viewport.dispose
      @picture_blink.dispose
      # □第2カーソルViewportを解放
      if @background == nil
        @picture_blink_2nd_viewport.dispose
      else
        @background.dispose
      end
    end
    # □第2カーソルを解放
    dispose_blink2nd
    # □背景ウィンドウを解放
    unless nil?(@picture_back)
      if @picture_back.bitmap != nil
        @picture_back.bitmap.dispose
      end
      @picture_back.viewport.dispose
      @picture_back.dispose
    end
    # □ヘルプウィンドウを解放
    unless nil?(@picture_help)
      if @picture_help.bitmap != nil
        @picture_help.bitmap.dispose
      end
      @picture_help.viewport.dispose
      @picture_help.dispose
    end
    # □カーソルアイコンを解放
    unless nil?(@csr4_icon)
      if @csr4_icon.bitmap != nil
        @csr4_icon.bitmap.dispose
      end
      @csr4_icon.viewport.dispose
      @csr4_icon.dispose
      # □第2カーソルアイコンViewportを解放
      @csr4_2nd_viewport.dispose
    end
    # □第2カーソルアイコンを解放
    dispose_csr2nd
  end
end