Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  2. #_/    ◆ カーソルアニメーション - KMS_CursorAnimation ◆ VX Ace ◆
  3. #_/    ◇ Last update : 2012/02/19 (TOMY@Kamesoft) ◇
  4. #_/----------------------------------------------------------------------------
  5. #_/  カーソルの位置にアニメーションを表示します。
  6. #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  7.  
  8. #==============================================================================
  9. # ★ 設定項目 - BEGIN Setting ★
  10. #==============================================================================
  11.  
  12. module KMS_CursorAnimation
  13.   # ◆ 最初からカーソルアニメを表示する
  14.   #  true  : タイトルなどでもアニメを表示する。
  15.   #  false : ゲーム中に明示的にオンするまで表示しない。
  16.   DEFAULT_ANIMATION = true
  17.  
  18.   # ◆ アニメファイル名
  19.   #  "Graphics/System" から読み込む。
  20.   ANIMATION_FILE = "CursorAnimation"
  21.   # ◆ アニメーションのフレーム数
  22.   FRAME_COUNT    = 12
  23.   # ◆ アニメーションウェイト
  24.   #  数値が大きいほどアニメが遅くなる。
  25.   ANIMATION_WAIT = 4
  26.  
  27.   # ◆ 不透明度
  28.   OPACITY       = 224
  29.   # ◆ 合成方法
  30.   #  0..通常  1..加算  2..減算
  31.   BLEND_TYPE    = 1
  32.   # ◆ 基準位置
  33.   #  0..上  1..中央  2..下
  34.   BASE_POSITION = 1
  35.   # ◆ 表示位置補正 [x, y]
  36.   POSITION_REV  = [-4, 0]
  37. end
  38.  
  39. #==============================================================================
  40. # ☆ 設定ここまで - END Setting ☆
  41. #==============================================================================
  42.  
  43. $kms_imported = {} if $kms_imported == nil
  44. $kms_imported["CursorAnimation"] = true
  45.  
  46. # *****************************************************************************
  47.  
  48. #==============================================================================
  49. # □ KMS_Commands
  50. #==============================================================================
  51.  
  52. module KMS_Commands
  53.   module_function
  54.   #--------------------------------------------------------------------------
  55.   # ○ カーソルアニメを表示
  56.   #--------------------------------------------------------------------------
  57.   def show_cursor_animation
  58.     $game_system.cursor_animation_visible = true
  59.   end
  60.   #--------------------------------------------------------------------------
  61.   # ○ カーソルアニメを非表示
  62.   #--------------------------------------------------------------------------
  63.   def hide_cursor_animation
  64.     $game_system.cursor_animation_visible = false
  65.   end
  66.   #--------------------------------------------------------------------------
  67.   # ○ カーソルアニメ表示状態の取得
  68.   #--------------------------------------------------------------------------
  69.   def cursor_animation_visible?
  70.     return $game_system.cursor_animation_visible
  71.   end
  72. end
  73.  
  74. #==============================================================================
  75. # ■ Game_Interpreter
  76. #==============================================================================
  77.  
  78. class Game_Interpreter
  79.   # イベントコマンドから直接コマンドを叩けるようにする
  80.   include KMS_Commands
  81. end
  82.  
  83. #==============================================================================
  84. # ■ Window_Base
  85. #==============================================================================
  86.  
  87. class Window_Base < Window
  88.   #--------------------------------------------------------------------------
  89.   # ○ クラス変数
  90.   #--------------------------------------------------------------------------
  91.   @@__cursor_animation = nil  # カーソルアニメ
  92.   #--------------------------------------------------------------------------
  93.   # ● オブジェクト初期化
  94.   #     x       : ウィンドウの X 座標
  95.   #     y       : ウィンドウの Y 座標
  96.   #     width   : ウィンドウの幅
  97.   #     height  : ウィンドウの高さ
  98.   #--------------------------------------------------------------------------
  99.   alias initialize_KMS_CursorAnimation initialize
  100.   def initialize(x, y, width, height)
  101.     initialize_KMS_CursorAnimation(x, y, width, height)
  102.  
  103.     @@__cursor_animation.add_window(self)
  104.   end
  105.   #--------------------------------------------------------------------------
  106.   # ● 解放
  107.   #--------------------------------------------------------------------------
  108.   unless method_defined?(:dispose_KMS_CursorAnimation)
  109.     alias dispose_KMS_CursorAnimation dispose
  110.   end
  111.   def dispose
  112.     @@__cursor_animation.remove_window(self)
  113.  
  114.     dispose_KMS_CursorAnimation
  115.   end
  116.   #--------------------------------------------------------------------------
  117.   # ○ カーソルアニメを生成
  118.   #--------------------------------------------------------------------------
  119.   def self.create_cursor_animation
  120.     @@__cursor_animation = Cursor_Animation.new
  121.   end
  122.   #--------------------------------------------------------------------------
  123.   # ○ カーソルアニメを破棄
  124.   #--------------------------------------------------------------------------
  125.   def self.dispose_cursor_animation
  126.     @@__cursor_animation.dispose
  127.   end
  128.   #--------------------------------------------------------------------------
  129.   # ○ カーソルアニメを表示
  130.   #--------------------------------------------------------------------------
  131.   def self.show_cursor_animation
  132.     @@__cursor_animation.visible = true
  133.     @@__cursor_animation.update
  134.   end
  135.   #--------------------------------------------------------------------------
  136.   # ○ カーソルアニメを隠す
  137.   #--------------------------------------------------------------------------
  138.   def self.hide_cursor_animation
  139.     @@__cursor_animation.visible = false
  140.     @@__cursor_animation.update
  141.   end
  142.   #--------------------------------------------------------------------------
  143.   # ○ カーソルアニメを更新
  144.   #--------------------------------------------------------------------------
  145.   def self.update_cursor_animation
  146.     @@__cursor_animation.update
  147.   end
  148. end
  149.  
  150. #==============================================================================
  151. # ■ Game_System
  152. #==============================================================================
  153.  
  154. class Game_System
  155.   #--------------------------------------------------------------------------
  156.   # ○ 公開インスタンス変数
  157.   #--------------------------------------------------------------------------
  158.   attr_writer :cursor_animation_visible
  159.   #--------------------------------------------------------------------------
  160.   # ○ オブジェクト初期化
  161.   #--------------------------------------------------------------------------
  162.   alias initialize_KMS_CursorAnimation initialize
  163.   def initialize
  164.     initialize_KMS_CursorAnimation
  165.  
  166.     @cursor_animation_visible = KMS_CursorAnimation::DEFAULT_ANIMATION
  167.   end
  168.   #--------------------------------------------------------------------------
  169.   # ○ カーソルアニメ可否フラグを取得
  170.   #--------------------------------------------------------------------------
  171.   def cursor_animation_visible
  172.     if @cursor_animation_visible.nil?
  173.       @cursor_animation_visible = KMS_CursorAnimatin::DEFAULT_ANIMATION
  174.     end
  175.     return @cursor_animation_visible
  176.   end
  177. end
  178.  
  179. #==============================================================================
  180. # □ Sprite_CursorAnimation
  181. #------------------------------------------------------------------------------
  182. #  カーソルアニメーション用の処理を追加したスプライトのクラスです。
  183. #==============================================================================
  184.  
  185. class Sprite_CursorAnimation < Sprite
  186.   #--------------------------------------------------------------------------
  187.   # ● オブジェクト初期化
  188.   #     viewport : ビューポート
  189.   #--------------------------------------------------------------------------
  190.   def initialize(viewport = nil)
  191.     super(viewport)
  192.     @duration    = 0
  193.     @frame_count = 0
  194.  
  195.     self.bitmap = Cache.system(KMS_CursorAnimation::ANIMATION_FILE)
  196.     self.src_rect.width  = bitmap.width / 8
  197.     self.src_rect.height = bitmap.height /
  198.       ([KMS_CursorAnimation::FRAME_COUNT - 1, 0].max / 8 + 1)
  199.     self.ox         = src_rect.width  / 2
  200.     self.oy         = src_rect.height / 2
  201.     self.opacity    = KMS_CursorAnimation::OPACITY
  202.     self.blend_type = KMS_CursorAnimation::BLEND_TYPE
  203.   end
  204.   #--------------------------------------------------------------------------
  205.   # ● フレーム更新
  206.   #--------------------------------------------------------------------------
  207.   def update
  208.     super
  209.     return unless visible
  210.  
  211.     @frame_count += 1
  212.     return if @frame_count % KMS_CursorAnimation::ANIMATION_WAIT != 0
  213.  
  214.     @frame_count  = 0
  215.     @duration    -= 1
  216.     if @duration < 0
  217.       @duration = KMS_CursorAnimation::FRAME_COUNT - 1
  218.     end
  219.     update_animation
  220.   end
  221.   #--------------------------------------------------------------------------
  222.   # ○ アニメーションを更新
  223.   #--------------------------------------------------------------------------
  224.   def update_animation
  225.     self.src_rect.x = src_rect.width  * (@duration % 8)
  226.     self.src_rect.y = src_rect.height * (@duration / 8)
  227.   end
  228. end
  229.  
  230. #==============================================================================
  231. # □ Cursor_Animation
  232. #------------------------------------------------------------------------------
  233. #  カーソル周りのアニメーションを扱うクラスです。
  234. #==============================================================================
  235.  
  236. class Cursor_Animation
  237.   #--------------------------------------------------------------------------
  238.   # ○ 公開インスタンス変数
  239.   #--------------------------------------------------------------------------
  240.   attr_accessor :visible
  241.   #--------------------------------------------------------------------------
  242.   # ○ オブジェクト初期化
  243.   #--------------------------------------------------------------------------
  244.   def initialize
  245.     reset
  246.   end
  247.   #--------------------------------------------------------------------------
  248.   # ○ 破棄
  249.   #--------------------------------------------------------------------------
  250.   def dispose
  251.     @target_sprite.dispose
  252.     @target_sprite = nil
  253.     @viewport.dispose
  254.     @viewport = nil
  255.   end
  256.   #--------------------------------------------------------------------------
  257.   # ○ リセット
  258.   #--------------------------------------------------------------------------
  259.   def reset
  260.     @visible = false
  261.  
  262.     @viewport      = Viewport.new(0, 0, 640, 480)
  263.     @windows       = []
  264.     @target_sprite = Sprite_CursorAnimation.new(@viewport)
  265.     @active_window = nil
  266.  
  267.     @viewport.visible = false
  268.     @viewport.z = 30000
  269.   end
  270.   #--------------------------------------------------------------------------
  271.   # ○ ウィンドウ追加
  272.   #--------------------------------------------------------------------------
  273.   def add_window(*window)
  274.     @windows |= window.find_all { |w|
  275.       w.is_a?(Window_Selectable) || w.is_a?(Window_SaveFile)
  276.     }
  277.     @windows.flatten!
  278.   end
  279.   #--------------------------------------------------------------------------
  280.   # ○ ウィンドウ削除
  281.   #--------------------------------------------------------------------------
  282.   def remove_window(*window)
  283.     @windows -= window
  284.   end
  285.   #--------------------------------------------------------------------------
  286.   # ○ フレーム更新
  287.   #--------------------------------------------------------------------------
  288.   def update
  289.     return if @viewport.nil?
  290.  
  291.     @viewport.update
  292.     @target_sprite.update
  293.  
  294.     # 座標調整
  295.     dest_x, dest_y = get_cursor_pos
  296.     if @target_sprite.x != dest_x
  297.       if (dest_x - @target_sprite.x).abs < 4
  298.         @target_sprite.x = dest_x
  299.       else
  300.         dist = (dest_x - @target_sprite.x) / 4
  301.         dist = (dist > 0 ? [dist, 4].max : [dist, -4].min)
  302.         @target_sprite.x += dist
  303.       end
  304.     end
  305.     if @target_sprite.y != dest_y
  306.       if (dest_y - @target_sprite.y).abs < 4
  307.         @target_sprite.y = dest_y
  308.       else
  309.         dist = (dest_y - @target_sprite.y) / 4
  310.         dist = (dist > 0 ? [dist, 4].max : [dist, -4].min)
  311.         @target_sprite.y += dist
  312.       end
  313.     end
  314.   end
  315.   #--------------------------------------------------------------------------
  316.   # ○ カーソル位置取得
  317.   #    [x, y] の形で返す。
  318.   #--------------------------------------------------------------------------
  319.   def get_cursor_pos
  320.     dx = dy = 0
  321.  
  322.     # 可視状態のアクティブウィンドウを取得
  323.     unless window_active?(@active_window)
  324.       @active_window = search_active_window
  325.     end
  326.  
  327.     # アクティブウィンドウがなければ非表示
  328.     if @active_window.nil? || !KMS_Commands.cursor_animation_visible?
  329.       @viewport.visible = false
  330.       dx = Graphics.width  / 2
  331.       dy = Graphics.height / 2
  332.       return [dx, dy]
  333.     end
  334.     @viewport.visible = @visible
  335.  
  336.     # カーソル位置を計算
  337.     rect = @active_window.cursor_rect
  338.     dx   = rect.x + 16 + KMS_CursorAnimation::POSITION_REV[0]
  339.     dy   = rect.y + 16 + KMS_CursorAnimation::POSITION_REV[1]
  340.     vp   = @active_window.viewport
  341.     if vp != nil
  342.       dx += vp.rect.x - vp.ox
  343.       dy += vp.rect.y - vp.oy
  344.     end
  345.     dx += @active_window.x - @active_window.ox
  346.     dy += @active_window.y - @active_window.oy
  347.  
  348.     case KMS_CursorAnimation::BASE_POSITION
  349.     when 0  # 上
  350.       dy += @target_sprite.oy
  351.     when 1  # 中央
  352.       dy += rect.height / 2
  353.     when 2  # 下
  354.       dy += rect.height - @target_sprite.oy
  355.     end
  356.     return [dx, dy]
  357.   end
  358.   #--------------------------------------------------------------------------
  359.   # ○ ウィンドウの可視・アクティブ状態判定
  360.   #--------------------------------------------------------------------------
  361.   def window_active?(window)
  362.     return false if window.nil?
  363.     return false if window.disposed?
  364.     return false unless window.visible
  365.  
  366.     if window.is_a?(Window_Selectable)
  367.       return window.active
  368.     elsif window.is_a?(Window_SaveFile)
  369.       return window.selected
  370.     end
  371.     return false
  372.   end
  373.   #--------------------------------------------------------------------------
  374.   # ○ アクティブウィンドウを探す
  375.   #--------------------------------------------------------------------------
  376.   def search_active_window
  377.     return @windows.find { |w|
  378.       if !w.visible
  379.         false
  380.       elsif w.is_a?(Window_Selectable)
  381.         w.active && w.index >= 0
  382.       elsif w.is_a?(Window_SaveFile)
  383.         w.selected
  384.       else
  385.         false
  386.       end
  387.     }
  388.   end
  389. end
  390.  
  391. #==============================================================================
  392. # ■ Scene_Base
  393. #==============================================================================
  394.  
  395. class Scene_Base
  396.   #--------------------------------------------------------------------------
  397.   # ● 開始処理
  398.   #--------------------------------------------------------------------------
  399.   alias start_KMS_CursorAnimation start
  400.   def start
  401.     Window_Base.create_cursor_animation
  402.     Window_Base.show_cursor_animation
  403.  
  404.     start_KMS_CursorAnimation
  405.   end
  406.   #--------------------------------------------------------------------------
  407.   # ● 終了前処理
  408.   #--------------------------------------------------------------------------
  409.   alias pre_terminate_KMS_CursorAnimation pre_terminate
  410.   def pre_terminate
  411.     Window_Base.dispose_cursor_animation
  412.  
  413.     pre_terminate_KMS_CursorAnimation
  414.   end
  415.   #--------------------------------------------------------------------------
  416.   # ● フレーム更新(基本)
  417.   #--------------------------------------------------------------------------
  418.   alias update_basic_KMS_CursorAnimation update_basic
  419.   def update_basic
  420.     update_basic_KMS_CursorAnimation
  421.  
  422.     # カーソルアニメを更新
  423.     Window_Base.update_cursor_animation
  424.   end
  425. end