Advertisement
Szyu

Simple Loading Bar

Oct 20th, 2015
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.52 KB | None | 0 0
  1. #== Szyu Scripts presents =====================================================
  2. # * Szyu Scripts' Simple Loading Bar
  3. #   Version 1.0
  4. #   by Szyu
  5. #
  6. #== About =====================================================================
  7. # * This script adds a pseudo loading bar/screen to your game.
  8. #
  9. #== How to Use ================================================================
  10. # * Where you want to use the loading bar, create initiate a new object from
  11. #   Window_LoadingBar:
  12. #   @window_loading_bar = Window_LoadingBar.new((Graphics.width-120)/2,
  13. #         (Graphics.height-48)/2,120,48,method(:on_finished))
  14. #
  15. # * In the on_finished method, dispose of the object like
  16. #   def on_finished
  17. #     @window_loading_bar.close.dispose
  18. #     @window_loading_bar = nil
  19. #     # do other stuff here when finished loading
  20. #   end
  21. #
  22. # * You can adjust the speed of the loading bar by setting the progress_speed:
  23. #   @window_loading_bar.progress_speed = x # default is 2
  24. #  
  25. #== Terms of Use ==============================================================
  26. # * You are free to use this script for commercial and non-commercial projects.
  27. #   However I'd like you to inform me of the projects you are planning to use it
  28. #   in, so I can keep track of where my scripts are used.
  29. #
  30. #== Import ====================================================================
  31. $imported = {} if $imported.nil?
  32. $imported["SZ_LoadingBar"] = true
  33. #==============================================================================
  34. #==============================================================================
  35. # ** Window_Loading
  36. #==============================================================================
  37. class Window_LoadingBar < Window_Base
  38.   attr_accessor :progress
  39.   attr_reader :finished
  40.   #--------------------------------------------------------------------------
  41.   # * Initialize
  42.   #--------------------------------------------------------------------------
  43.   def initialize(x,y,w, h, on_finished_prc)
  44.     super(x,y,w,h)
  45.     @progress = 0
  46.     @finished = false
  47.     @on_finished = on_finished_prc
  48.     @progress_speed = 2
  49.     refresh
  50.   end
  51.   #--------------------------------------------------------------------------
  52.   # * Set Progress Speed
  53.   #--------------------------------------------------------------------------
  54.   def progress_speed=(speed)
  55.     @progress_speed = speed
  56.   end
  57.   #--------------------------------------------------------------------------
  58.   # * Update
  59.   #--------------------------------------------------------------------------
  60.   def update
  61.     super
  62.     return if @finished
  63.     @progress += @progress_speed
  64.     refresh
  65.     if @progress >= 100
  66.       @finished = true
  67.       @on_finished.call if @on_finished
  68.     end
  69.   end
  70.   #--------------------------------------------------------------------------
  71.   # * Refresh
  72.   #--------------------------------------------------------------------------
  73.   def refresh
  74.     contents.clear
  75.     h = line_height
  76.     draw_loading_gauge(0,(contents.height-h)/2,width,h, @progress.to_f/100, hp_gauge_color1, hp_gauge_color2)
  77.   end
  78.   #--------------------------------------------------------------------------
  79.   # * Draw Loading Gauge
  80.   #--------------------------------------------------------------------------
  81.   def draw_loading_gauge(x, y, width, height, rate, color1, color2)
  82.     fill_w = (width * rate).to_i
  83.     gauge_y = y
  84.     contents.fill_rect(x, gauge_y, width, line_height, gauge_back_color)
  85.     contents.gradient_fill_rect(x, gauge_y, fill_w, line_height, color1, color2)
  86.   end
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement