khanhdu

CSCA Toast Manager

Jun 2nd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 10.19 KB | None | 0 0
  1. =begin
  2. CSCA Toast Manager
  3. version: 1.1.1 (Released: April 27, 2013)
  4. Created by: Casper Gaming (http://www.caspergaming.com/)
  5.  
  6. Compatibility:
  7. Made for RPGVXAce
  8. IMPORTANT: ALL CSCA Scripts should be compatible with each other unless
  9. otherwise noted.
  10. Requires CSCA Core Script v1.0.4+
  11. Suggested order: Paste below CSCA Core script, but above all other CSCA Scripts.
  12.  
  13. FFEATURES
  14. Creates an easy-to-use toast system. Mainly a scripting tool, but you can create
  15. and call your own basic toasts as well. More information on how to do so in the
  16. script call section below.
  17.  
  18. SETUP
  19. Set up required. Instructions below.
  20.  
  21. Scripters: it is recommended to alias and add your custom display codes in the
  22. refresh method of CSCA_Window_Toast. If relying on this script to display toasts
  23. in your own script, please link to original CSCA Toast Manager script topic on
  24. rpgmakervxace.net
  25. ================================================================================
  26. UPDATES:
  27. Version 1.0.0
  28. -Original Script
  29.  
  30. Version 1.1.0
  31. -Toasts now global (not confined to the map scene). Certain scenes are excluded.
  32.  
  33. Version 1.1.1
  34. -Toasts now have a z value of 1000.
  35. ================================================================================
  36. CREDIT:
  37. Free to use in noncommercial games if credit is given to:
  38. Casper Gaming (http://www.caspergaming.com/)
  39.  
  40. To use in a commercial game, please purchase a license here:
  41. http://www.caspergaming.com/licenses.html
  42.  
  43. TERMS:
  44. http://www.caspergaming.com/terms_of_use.html
  45. =end
  46. module CSCA
  47.   module TOASTS
  48. #==============================================================================
  49. # ** Important Script Calls
  50. #==============================================================================
  51. # bt_reserve_toast(text1, text2)
  52. # Reserves a toast with 2 lines of centered text on it.
  53. # text1 is the first line, text2 is the second line.
  54. #==============================================================================
  55. # ** Begin Setup
  56. #==============================================================================
  57.     SHOW_COUNT = 160 # Amount of frames to show each toast. Recommended 160.
  58.     FADE_SPEED = 16 # Speed of fade in/out. Recommended 16.
  59. #==============================================================================
  60. # ** End Setup
  61. #==============================================================================
  62.   end
  63. end
  64. $imported = {} if $imported.nil?
  65. $imported["CSCA-ToastManager"] = true
  66. #==============================================================================
  67. # ** Game_Interpreter
  68. #------------------------------------------------------------------------------
  69. #  Adds basic toast reservation method for non-scripters
  70. #==============================================================================
  71. class Game_Interpreter
  72.   #--------------------------------------------------------------------------
  73.   # Basic Text toast reservation, creates a toast with 2 lines of text.
  74.   #--------------------------------------------------------------------------
  75.   def bt_reserve_toast(text1, text2)
  76.     $csca.reserve_toast([:csca_bt, text1, text2])
  77.   end
  78. end
  79. #==============================================================================
  80. # ** CSCA_Window_Toast
  81. #------------------------------------------------------------------------------
  82. # This window handles toast data.
  83. #==============================================================================
  84. class CSCA_Window_Toast < Window_Base
  85.   attr_reader :show_count
  86.   attr_reader :toast_gone
  87.   #--------------------------------------------------------------------------
  88.   # Object Initialization
  89.   #--------------------------------------------------------------------------
  90.   def initialize(order)
  91.     x = Graphics.width / 4
  92.     width = Graphics.width / 2
  93.     height = line_height * 3
  94.     y = ((Graphics.height - (height * 2)) / 2) + line_height * get_order_modifier(order)
  95.     super(x, y, width, height)
  96.     self.opacity = 0
  97.     self.contents_opacity = 0
  98.     self.z = 1000
  99.     @show_count = 0
  100.     @toast_gone = true
  101.   end
  102.   #--------------------------------------------------------------------------
  103.   # Get order height modifier
  104.   #--------------------------------------------------------------------------
  105.   def get_order_modifier(order)
  106.     return case order
  107.     when :bottom; 6
  108.     when :middle; 3
  109.     when :top; 0
  110.     end
  111.   end
  112.   #--------------------------------------------------------------------------
  113.   # Frame Update
  114.   #--------------------------------------------------------------------------
  115.   def update
  116.     super
  117.     if @show_count > 0
  118.       update_fadein
  119.       @show_count -= 1
  120.     else
  121.       update_fadeout unless @toast_gone
  122.     end
  123.   end
  124.   #--------------------------------------------------------------------------
  125.   # Update Fadein
  126.   #--------------------------------------------------------------------------
  127.   def update_fadein
  128.     self.opacity += CSCA::TOASTS::FADE_SPEED
  129.     self.contents_opacity += CSCA::TOASTS::FADE_SPEED
  130.   end
  131.   #--------------------------------------------------------------------------
  132.   # Update Fadeout
  133.   #--------------------------------------------------------------------------
  134.   def update_fadeout
  135.     self.opacity -= CSCA::TOASTS::FADE_SPEED
  136.     self.contents_opacity -= CSCA::TOASTS::FADE_SPEED
  137.     @toast_gone = true if self.opacity <= 0 && self.contents_opacity <= 0
  138.   end
  139.   #--------------------------------------------------------------------------
  140.   # Writer method
  141.   #--------------------------------------------------------------------------
  142.   def show_count=(amount)
  143.     @show_count = amount
  144.     @toast_gone = false
  145.   end
  146.   #--------------------------------------------------------------------------
  147.   # Refresh
  148.   #--------------------------------------------------------------------------
  149.   def refresh(params)
  150.     contents.clear
  151.     if params[0] == :csca_bt
  152.       draw_text(0,0,contents.width,line_height,params[1],1)
  153.       draw_text(0,line_height,contents.width,line_height,params[2],1)
  154.     end
  155.   end
  156. end
  157. #==============================================================================
  158. # ** CSCA_Core
  159. #------------------------------------------------------------------------------
  160. # Handles toast data.
  161. #Aliases: initialize
  162. #==============================================================================
  163. class CSCA_Core
  164.   attr_reader :toasts
  165.   #--------------------------------------------------------------------------
  166.   # Alias Method; object initialization
  167.   #--------------------------------------------------------------------------
  168.   alias :csca_toast_init :initialize
  169.   def initialize
  170.     csca_toast_init
  171.     @toasts = []
  172.   end
  173.   #--------------------------------------------------------------------------
  174.   # Reserve Toast for display
  175.   #--------------------------------------------------------------------------
  176.   def reserve_toast(params)
  177.     return if SceneManager.no_toast_scene?
  178.     @toasts.push(params)
  179.   end
  180. end
  181. #==============================================================================
  182. # ** SceneManager
  183. #------------------------------------------------------------------------------
  184. # Determines if the scene creates toasts.
  185. #==============================================================================
  186. module SceneManager
  187.   #--------------------------------------------------------------------------
  188.   # Don't create toasts?
  189.   #--------------------------------------------------------------------------
  190.   def self.no_toast_scene?
  191.     scene_is?(Scene_Title) || scene_is?(Scene_Gameover) || scene_is?(Scene_Debug) ||
  192.     scene_is?(Scene_File) || scene_is?(Scene_Save) || scene_is?(Scene_Load) ||
  193.     scene_is?(Scene_End) || scene_is?(Scene_Name)
  194.   end
  195. end
  196. #==============================================================================
  197. # ** Scene_Map
  198. #------------------------------------------------------------------------------
  199. # Handles display of toasts
  200. #Aliases: create_all_windows, update
  201. #==============================================================================
  202. class Scene_Base
  203.   #--------------------------------------------------------------------------
  204.   # Alias Method; Create All Windows
  205.   #--------------------------------------------------------------------------
  206.   alias :csca_create_toast_windows :start
  207.   def start
  208.     csca_create_toast_windows
  209.     create_toast_windows unless SceneManager.no_toast_scene?
  210.   end
  211.   #--------------------------------------------------------------------------
  212.   # Alias Method; Frame Update
  213.   #--------------------------------------------------------------------------
  214.   alias :csca_toast_update :update
  215.   def update
  216.     csca_toast_update
  217.     update_toasts
  218.   end
  219.   #--------------------------------------------------------------------------
  220.   # Create Toast Windows
  221.   #--------------------------------------------------------------------------
  222.   def create_toast_windows
  223.     @toast_bottom = CSCA_Window_Toast.new(:bottom)
  224.     @toast_middle = CSCA_Window_Toast.new(:middle)
  225.     @toast_top = CSCA_Window_Toast.new(:top)
  226.     @toast_list = [@toast_bottom, @toast_middle, @toast_top]
  227.     @toast_bottom.viewport = @viewport
  228.     @toast_middle.viewport = @viewport
  229.     @toast_top.viewport = @viewport
  230.   end
  231.   #--------------------------------------------------------------------------
  232.   # Update Toast Display
  233.   #--------------------------------------------------------------------------
  234.   def update_toasts
  235.     $csca.toasts.each do |params|
  236.       break if params.nil? || no_toast_possible?
  237.       create_toast(params)
  238.     end
  239.   end
  240.   #--------------------------------------------------------------------------
  241.   # Check if all 3 toasts in use
  242.   #--------------------------------------------------------------------------
  243.   def no_toast_possible?
  244.     return !@toast_bottom.toast_gone && !@toast_middle.toast_gone && !@toast_top.toast_gone
  245.   end
  246.   #--------------------------------------------------------------------------
  247.   # Create Toast
  248.   #--------------------------------------------------------------------------
  249.   def create_toast(params)
  250.     $csca.toasts.delete(params)
  251.     for toast in @toast_list.each
  252.       if toast.toast_gone
  253.         toast.refresh(params)
  254.         toast.show_count = CSCA::TOASTS::SHOW_COUNT
  255.         break
  256.       end
  257.     end
  258.   end
  259. end
Add Comment
Please, Sign In to add comment