Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #==============================================================================
- # Iavra Generic Popup 1.01
- # -----------------------------------------------------------------------------
- # Description:
- # Provides a generic way to display popup messages.
- # -----------------------------------------------------------------------------
- # Prerequisites:
- # None
- # -----------------------------------------------------------------------------
- # How to Use:
- # To show a new popup message, call the following method:
- #
- # IAVRA::POPUP.show("abc")
- #
- # The window height will automatically fit to the number of text lines.
- #
- # The method takes an optional hash, which can be used to override the default
- # settings on a per-window basis, like this:
- #
- # IAVRA::POPUP.show("abc", {:width => 200})
- #
- # Also, the method can take an optional block, that will be called once the
- # popup window is created. "self" inside the proc is set to the window itself:
- #
- # IAVRA::POPUP.show("abc") { p self }
- #
- # You can use all message codes in your text, but need to double backslashes
- # for everything except newlines (\n).
- #
- # The popup will fade out over time and dispose itself afterwards.
- #
- # Popups are global and will persist between scenes. Certain scenes can be set
- # to dispose existing popups by modifying the DISPOSE_ON_SCENE constant.
- # -----------------------------------------------------------------------------
- # Terms of Use:
- # Free to use for both commercial and non-commercial games. Please give credit.
- # -----------------------------------------------------------------------------
- # Credits:
- # Iavra
- # -----------------------------------------------------------------------------
- # Changelog:
- # - 1.00: Release version.
- # - 1.01: Changed the way popup options are handled. The show method now takes
- # an optional hash, that can be used to override the global defaults
- # on a per-window basis. Everything except the z-index can be modified.
- #==============================================================================
- ($imported ||= {})[:iavra_generic_popup] = true
- #==============================================================================
- # ▼ IAVRA::POPUP
- #==============================================================================
- module IAVRA
- module POPUP
- #==========================================================================
- # ■ ■ ■ ■ ■ CONFIGURATION ■ ■ ■ ■ ■
- #==========================================================================
- #==========================================================================
- # Default options when creating new windows. Can be overriden on a per-
- # window basis by using the "options" parameter:
- #
- # :width => Window width
- # :min_lines => Minimum number of lines to display. Can be nil.
- # :max_lines => Maximum number of lines to display. Can be nil.
- # :line_height => Height of a single line. Set to nil for default.
- # :padding => Window padding. Set to nil for default.
- # :spacing => Spacing between windows. The update method can use this.
- # :windowskin => Windowskin to be used. Set to nil for default.
- # :fade_delay => Number of frames before the window starts fading.
- # :fade_rate => Rate at which the window fades out after the delay.
- # :update_method => Update method to be used.
- #==========================================================================
- DEFAULT_OPTIONS = {
- :width => 400,
- :min_lines => nil,
- :max_lines => nil,
- :line_height => nil,
- :padding => nil,
- :spacing => 10,
- :windowskin => nil,
- :fade_delay => 120,
- :fade_rate => 10,
- :update_method => :push_older_up
- }
- #==========================================================================
- # Z-index of the popup viewport.
- #==========================================================================
- Z_INDEX = 1
- #==========================================================================
- # When a scene listed in here becomes active, all popups will be disposed.
- # Add Scene_Base to dispose all popups whenever a scene changes.
- #==========================================================================
- DISPOSE_ON_SCENE = [
- Scene_Title, Scene_Gameover, Scene_File
- ]
- #==========================================================================
- # ■ ■ ■ ■ ■ CONFIGURATION ■ ■ ■ ■ ■
- #==========================================================================
- #==========================================================================
- # A number of predefined update methods, accessible by symbol. You can add
- # new ones if you know what to do. The update method takes 2 parameters:
- # - The list of all currently displayed windows.
- # - The index of the window, which is updated.
- #
- # Also, "self" inside the proc is set to the window itself.
- #==========================================================================
- UPDATE_PROCS = {
- # This can be used in combination with the callback proc to animate the
- # popups by other means, like "Iavra Animate Everything".
- :none => lambda {|list, index|},
- # This method displays popups in the lower left corner and adds new ones
- # on top. Once an older popup fades out, the remaining ones move down.
- :add_on_top => lambda {|list, index|
- self.y = Graphics.height - list.map.with_index{|w, i|
- (i <= index) && !w.disposed? ? (w.height + w.spacing) : 0
- }.reduce(:+)
- },
- # This method displays popups in the lower left corner and adds new ones
- # to the bottom, pushing older ones upward.
- :push_older_up => lambda {|list, index|
- self.y = Graphics.height - list.map.with_index{|w, i|
- (i >= index) && !w.disposed? ? (w.height + w.spacing) : 0
- }.reduce(:+)
- }
- # Be free to add your own methods here.
- }
- #==========================================================================
- # All popups are displayed in their own viewport. The viewport never gets
- # disposed, but since it's used throughout the whole game, this shouldn't
- # be too much of a problem.
- #==========================================================================
- VIEWPORT = Viewport.new
- VIEWPORT.z = Z_INDEX
- #==========================================================================
- # For storing the active popups.
- #==========================================================================
- @popups = []
- #==========================================================================
- # Adds a new popup window with the given text. Message codes can be used.
- #
- # An optional hash be can given to overwrite some of the default options.
- #
- # Can be called with an optional block that will be called, once the popup
- # is created.
- #==========================================================================
- def self.show(text, options = {}, &callback)
- @popups << Window_Popup.new(text, options, &callback)
- end
- #==========================================================================
- # Called every frame by Scene_Base to update popup position and opacity.
- #==========================================================================
- def self.update
- @popups.each {|popup| popup.update}
- end
- #==========================================================================
- # Called on scene exit to dispose all popups an clear the list.
- #==========================================================================
- def self.cleanup
- return unless DISPOSE_ON_SCENE.map{|scene| SceneManager.scene.is_a?(scene)}.any?
- @popups.each {|popup| popup.dispose}
- @popups.clear
- end
- #==========================================================================
- # Toggles visibility on or off for all popups.
- #==========================================================================
- def self.hide_all
- @popups.each {|popup| popup.visible = false}
- end
- def self.show_all
- @popups.each {|popup| popup.visible = true}
- end
- #==========================================================================
- # Returns the list of all currently displayed popups.
- #==========================================================================
- def self.list
- @popups
- end
- #==========================================================================
- # ▼ IAVRA::POPUP::Window_Popup
- # -------------------------------------------------------------------------
- # The popup window class. Namespaced to prevent possible conflicts with
- # other popup scripts.
- #==========================================================================
- class Window_Popup < ::Window_Base
- attr_reader :spacing
- #========================================================================
- # Initializes a new popup window. First merges the default options with
- # the given overrides (if any) and then initializes the window:
- #
- # - Set line_height, padding, spacing, fade delay and fade rate according
- # to the options.
- #
- # - Calculates the number of text lines needed by using a dummy window.
- # Then caps that value at the lower and upper bounds, if any.
- #
- # - Initializes the window, sets the viewport and windowskin. The window
- # is created outside the visible part of the screen, since the update
- # method will define it's position during the first update.
- #
- # - Draws the text (again).
- #
- # - Defines the given update method on the singleton class.
- #
- # - Executes the given callback proc, if any.
- #========================================================================
- def initialize(text, options = {}, &callback)
- o = DEFAULT_OPTIONS.merge(options)
- # window settings
- @line_height, @padding, @spacing, @fade_delay, @fade_rate =
- o.values_at(:line_height, :padding, :spacing, :fade_delay, :fade_rate)
- # calculate height
- dummy = Window_Dummy.new(text)
- lines = dummy.iavra_popup_num_lines
- dummy.dispose
- lines = [(min = o[:min_lines] || lines), (o[:max_lines] || min), lines].sort[1]
- height = fitting_height(lines)
- # initialize
- super(0, -height, o[:width], height)
- self.viewport = VIEWPORT
- self.windowskin = Cache.system(o[:windowskin]) if o[:windowskin]
- # draw text
- draw_text_ex(0, 0, text)
- # set update method
- define_singleton_method(:update_position, UPDATE_PROCS[o[:update_method]])
- # execute callback, if any
- instance_eval &callback if block_given?
- end
- #========================================================================
- # Line height
- #========================================================================
- def line_height
- @line_height || super
- end
- #========================================================================
- # Padding
- #========================================================================
- def standard_padding
- @padding || super
- end
- #========================================================================
- # Updates the position and opacity of the window
- #========================================================================
- def update
- super
- list = IAVRA::POPUP.list
- update_position(list, list.index(self))
- update_opacity
- end
- #========================================================================
- # Updates the opacity. Once it hits 0, the window is disposed and removes
- # itself from the list.
- #========================================================================
- def update_opacity
- if((@fade_delay -= 1) <= 0)
- self.opacity -= @fade_rate
- self.contents_opacity -= @fade_rate
- end
- if(self.opacity == 0)
- dispose
- IAVRA::POPUP.list.delete(self)
- end
- end
- end
- #==========================================================================
- # ▼ IAVRA::POPUP::Window_Dummy
- # -------------------------------------------------------------------------
- # A dummy window used to calculate the number of lines needed.
- #==========================================================================
- class Window_Dummy < ::Window_Base
- attr_reader :iavra_popup_num_lines
- def initialize(text)
- super(0, 0, 0, 0)
- @iavra_popup_num_lines = 1
- draw_text_ex(0, 0, text)
- end
- def process_new_line(text, pos)
- super(text, pos)
- @iavra_popup_num_lines += 1
- end
- end
- end
- end
- #==============================================================================
- # ▼ Scene_Base
- #==============================================================================
- class Scene_Base
- alias :iavra_popup_update_all_windows :update_all_windows
- alias :iavra_popup_dispose_all_windows :dispose_all_windows
- #============================================================================
- # Updates all popup windows.
- #============================================================================
- def update_all_windows(*args)
- iavra_popup_update_all_windows(*args)
- IAVRA::POPUP.update
- end
- #============================================================================
- # Cleanup on scene end.
- #============================================================================
- def dispose_all_windows(*args)
- IAVRA::POPUP.cleanup
- iavra_popup_dispose_all_windows(*args)
- end
- end
- #==============================================================================
- # ▼ SceneManager
- #==============================================================================
- module SceneManager
- class << self
- alias :iavra_popup_snapshot_for_background :snapshot_for_background
- end
- #============================================================================
- # Hide all popups before creating the background image, so they don't appear
- # on it.
- #============================================================================
- def self.snapshot_for_background
- IAVRA::POPUP.hide_all
- iavra_popup_snapshot_for_background
- IAVRA::POPUP.show_all
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement