Solistra

SES Note Replacement

Jan 5th, 2013
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 6.31 KB | None | 0 0
  1. #═╦═════════════════════════════════════════════════════════════════════════════
  2. # ║ § Note Replacement (v1.0) by Solistra                [License: CC BY-SA 3.0]
  3. # ║                                                            <RMVX Ace>
  4. #═╬═════════════════════════════════════════════════════════════════════════════
  5. # ║ § Change Log
  6. #─╫─────────────────────────────────────────────────────────────────────────────
  7. # ║ v1.0 (January 5th, 2013) - Initial release
  8. # ║
  9. #═╬═════════════════════════════════════════════════════════════════════════════
  10. # ║ § Summary
  11. #─╫─────────────────────────────────────────────────────────────────────────────
  12. # ║ This is a simple script that allows you to replace any text that you define
  13. # ║ with another set of text in any note box. You may use strings, symbols, or
  14. # ║ even regular expressions as tags, then define the text that replaces them
  15. # ║ with a string or a block of Ruby code that returns a string (for the truly
  16. # ║ insane). If you need to access the original state of the note box before
  17. # ║ replacements are done, you can use the "original_note" method on the object.
  18. # ║
  19. # ║ By default, this works with maps, actors, classes, skills, items, weapons,
  20. # ║ armors, enemies, states, and tilesets -- basically, anything with notes.
  21. # ║
  22. #═╬═════════════════════════════════════════════════════════════════════════════
  23. # ║ § Installation
  24. #─╫─────────────────────────────────────────────────────────────────────────────
  25. # ║ For best compatibility, place this script below Materials, but above all
  26. # ║ other custom scripts.
  27. # ║
  28. #═╬═════════════════════════════════════════════════════════════════════════════
  29. # ║ ▼ module SES::Notes
  30. #═╩═════════════════════════════════════════════════════════════════════════════
  31. module SES module Notes
  32.   # Define the tags you want to use and their replacements here.
  33.   TAGS = {
  34.     # An example tag -- replaces <example> with "This is a simple example tag."
  35.     # "<tag>" => "replacement text"
  36.     "<example>" => "This is a simple example tag.",
  37.    
  38.     # A more advanced example using regular expressions and a Proc object to
  39.     # create the replacement text. Ignore this if you don't know how it works.
  40.     # /\<tag\>/ => Proc.new{"Make sure this always returns a string."}
  41.     /\<example (\d+)\>/ => Proc.new do
  42.       # $~ and its relatives aren't actually global, so use them this way if you
  43.       # want to make use of them for match groups.
  44.       @note =~ /\<example (\d+)\>/ ? "This is example number #{$1}." : ""
  45.     end
  46.   }
  47.  
  48.   # Used to define the note() method in the RPG data structures. This basically
  49.   # overwrites the default reader method for @note in each class.
  50.   def self.note() Proc.new{SES::Notes.replace(@note.clone) if !@note.nil?} end
  51.  
  52.   # Performs the substitutions. The @note instance variable is defined here so
  53.   # that the tags above can access the content of the note (if using a Proc).
  54.   def self.replace(note)
  55.     @note = note
  56.     TAGS.each{|k, v| note.gsub!(k, v.is_a?(Proc) ? v.call : v)}
  57.     return note
  58.   end
  59. end end
  60. #═╦═════════════════════════════════════════════════════════════════════════════
  61. # ║ ▲ module SES::Notes
  62. #═╩═════════════════════════════════════════════════════════════════════════════
  63.                      ($imported ||= {})["SES - Notes"] = 1.0
  64. #═╦═════════════════════════════════════════════════════════════════════════════
  65. # ║ ▼ module RPG
  66. #═╩═════════════════════════════════════════════════════════════════════════════
  67. module RPG
  68.   # Add the methods note() and original_note() to all of the RPG data structures
  69.   # that make use of note boxes.
  70.   [Map, BaseItem, Tileset].each do |base_class|
  71.     base_class.send(:define_method, :note, &SES::Notes.note)
  72.     base_class.send(:define_method, :original_note, &Proc.new{@note})
  73.   end
  74. end
  75. #═╦═════════════════════════════════════════════════════════════════════════════
  76. # ║ ▲ module RPG
  77. #═╩═════════════════════════════════════════════════════════════════════════════
Advertisement
Add Comment
Please, Sign In to add comment