mrbubble

Rename Anything

Jul 31st, 2012
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 17.85 KB | None | 0 0
  1. #==============================================================================
  2. # ++ Rename Anything ++                                         v1.1 (7/31/12)
  3. #==============================================================================
  4. # Script by:
  5. #     Mr. Bubble ( http://mrbubblewand.wordpress.com/ )
  6. # Thanks:
  7. #     Mithran, regexp magician
  8. #     YF, code reference
  9. #     Tsukihime, save data correction
  10. #--------------------------------------------------------------------------
  11. # This script allows players to rename game objects in the database from
  12. # within a game.
  13. #
  14. # Why would you want to do that? I don't know, but now you can.
  15. #--------------------------------------------------------------------------
  16. #   ++ Changelog ++
  17. #--------------------------------------------------------------------------
  18. # v1.1 : Any renames are now properly saved and loaded from save files.
  19. #      : Actors are no longer renamable with this script (7/31/2012)
  20. # v1.0 : Initial release. (7/31/2012)
  21. #--------------------------------------------------------------------------
  22. #   ++ Installation ++
  23. #--------------------------------------------------------------------------
  24. # Install this script in the Materials section in your project's
  25. # script editor.
  26. #==============================================================================
  27. #   ++ Notetags ++
  28. #==============================================================================
  29. # Note: Some tags are given shorter tags for typing convenience. You only
  30. #       need to use one <tag> from a given group for a notebox.
  31. #       Use common sense.
  32. #
  33. # The following Notetag is for Classes, Skills, Items, Weapons, Armors,
  34. # Enemies, and States:
  35. #
  36. # <rename image: filename>
  37. #   This tag allows you to assign an image from the Graphics/Pictures/
  38. #   folder to represent the object in the renaming scene where filename
  39. #   is the name of the image without the extension. The recommended
  40. #   image resolution is [width: 96, height: 96]. If you have YEA Shop
  41. #   Options also installed in the same project, this script will use the
  42. #   image defined with the <image: filename> tag for any objects that
  43. #   have it.
  44. #
  45. #==============================================================================
  46. #   ++ Script Calls ++
  47. #==============================================================================
  48. # The following Script Calls are meant to be used in "Script..." event
  49. # commands found under Tab 3 when creating a new event.
  50. #
  51. # rename_object( :class,   id,  max_char)
  52. # rename_object( :skill,   id,  max_char)
  53. # rename_object( :item,    id,  max_char)
  54. # rename_object( :weapon,  id,  max_char)
  55. # rename_object( :armor,   id,  max_char)
  56. # rename_object( :enemy,   id,  max_char)
  57. # rename_object( :state,   id,  max_char)
  58. #   This script call opens up the rename anything scene with the object
  59. #   that you want to rename. The first argument is the type of object
  60. #   it is. The second argument is the ID number of the object found
  61. #   in your database. The third argument is the maximum limit of
  62. #   characters the player is allowed to name the object. If the object
  63. #   does not exist, this script call will do nothing.
  64. #
  65. #--------------------------------------------------------------------------
  66. #   ++ Compatibility ++
  67. #--------------------------------------------------------------------------
  68. # This script aliases the following default VXA methods:
  69. #
  70. #   DataManager#make_save_contents
  71. #   DataManager#extract_save_contents
  72. #
  73. # There are no default method overwrites.
  74. #
  75. # This script has built-in compatibility with the following scripts:
  76. #
  77. #   -Yanfly Engine Ace - Shop Options
  78. #
  79. # Requests for compatibility with other scripts are welcome.
  80. #--------------------------------------------------------------------------
  81. #   ++ Terms and Conditions ++
  82. #--------------------------------------------------------------------------
  83. # Please do not repost this script elsewhere without permission.
  84. # Free for non-commercial use. For commercial use, contact me first.
  85. #
  86. # Newest versions of this script can be found at
  87. #                                           http://mrbubblewand.wordpress.com/
  88. #==============================================================================
  89.  
  90. $imported ||= {}
  91. $imported["BubsRenameAnything"]
  92.  
  93.  
  94. #==========================================================================
  95. # ++ This script contains no customization module ++
  96. #==========================================================================
  97.  
  98.  
  99.  
  100. #==========================================================================
  101. # ++ Bubs::Regexp
  102. #==========================================================================
  103. module Bubs
  104.   module Regexp
  105.     RENAME_IMAGE_TAG = /<RENAME[_\s]?IMAGE:\s*(\w+)>/i
  106.   end # module Regexp
  107. end # module Bubs
  108.  
  109. #==========================================================================
  110. # ++ RPG::BaseItem
  111. #==========================================================================
  112. class RPG::BaseItem
  113.   #--------------------------------------------------------------------------
  114.   # public instance variables
  115.   #--------------------------------------------------------------------------
  116.   attr_accessor :renaming_image
  117.   #--------------------------------------------------------------------------
  118.   # common cache : rename_image
  119.   #--------------------------------------------------------------------------
  120.   def rename_image
  121.     @renaming_image ||= note =~ Bubs::Regexp::RENAME_IMAGE_TAG ? $1 : false
  122.   end
  123.  
  124. end # class RPG::BaseItem
  125.  
  126.  
  127. #==============================================================================
  128. # ++ DataManager
  129. #==============================================================================
  130. module DataManager
  131.   #--------------------------------------------------------------------------
  132.   # make_save_contents
  133.   #--------------------------------------------------------------------------
  134.   class << self; alias make_save_contents_bubs_rename make_save_contents; end
  135.   def self.make_save_contents
  136.     contents = make_save_contents_bubs_rename
  137.     contents[:renameanything] = save_rename_data
  138.     contents
  139.   end
  140.  
  141.   #--------------------------------------------------------------------------
  142.   # extract_save_contents
  143.   #--------------------------------------------------------------------------
  144.   class << self; alias extract_save_contents_bubs_rename extract_save_contents; end
  145.   def self.extract_save_contents(contents)
  146.     extract_save_contents_bubs_rename(contents)
  147.     load_rename_data(contents[:renameanything])
  148.   end
  149.  
  150.   #--------------------------------------------------------------------------
  151.   # save_rename_data
  152.   #--------------------------------------------------------------------------
  153.   def self.save_rename_data
  154.     keys = [:classes, :skills, :items, :weapons, :armors,
  155.       :enemies, :states]
  156.     groups = [$data_classes, $data_skills, $data_items, $data_weapons,
  157.       $data_armors, $data_enemies, $data_states]
  158.     hash = {}
  159.     for key, group in keys.zip(groups)
  160.       hash[key] = {}
  161.       for obj in group
  162.         next if obj.nil?
  163.         hash[key][obj.id] = obj.name
  164.       end # for obj
  165.     end # for group
  166.     return hash
  167.   end # def
  168.  
  169.   #--------------------------------------------------------------------------
  170.   # load_rename_data
  171.   #--------------------------------------------------------------------------
  172.   def self.load_rename_data(data)
  173.     keys = [:classes, :skills, :items, :weapons, :armors,
  174.       :enemies, :states]
  175.     groups = [$data_classes, $data_skills, $data_items, $data_weapons,
  176.       $data_armors, $data_enemies, $data_states]
  177.     for key, group in keys.zip(groups)
  178.       for obj in group
  179.         next if obj.nil?
  180.         obj.name = data[key][obj.id]
  181.       end # for obj
  182.     end # for group
  183.   end # def
  184.  
  185. end # module DataManager
  186.  
  187.  
  188. #==============================================================================
  189. # ++ Game_Interpreter
  190. #==============================================================================
  191. class Game_Interpreter
  192.   #--------------------------------------------------------------------------
  193.   # rename_object
  194.   #--------------------------------------------------------------------------
  195.   def rename_object(symbol, id, max_char = 20)
  196.     return if $game_party.in_battle
  197.     case symbol
  198.     when :actor
  199.       obj = nil
  200.     when :skill
  201.       obj = $data_skills[id]
  202.     when :item
  203.       obj = $data_items[id]
  204.     when :armor, :armour
  205.       obj = $data_armors[id]
  206.     when :weapon
  207.       obj = $data_weapons[id]
  208.     when :state
  209.       obj = $data_states[id]
  210.     when :enemy
  211.       obj = $data_enemies[id]
  212.     when :class
  213.       obj = $data_classes[id]
  214.     when :skilltype, :weapontype, :armortype, :element, :currency
  215.       obj = nil
  216.     end
  217.     if obj
  218.       SceneManager.call(Scene_RenameAnything)
  219.       SceneManager.scene.prepare(obj, max_char, symbol)
  220.       Fiber.yield
  221.     end
  222.   end
  223. end # class Game_Interpreter
  224.  
  225.  
  226. #==============================================================================
  227. # ++ Window_RenameAnythingEdit
  228. #==============================================================================
  229. class Window_RenameAnythingEdit < Window_Base
  230.   #--------------------------------------------------------------------------
  231.   # public instance variables
  232.   #--------------------------------------------------------------------------
  233.   attr_reader   :name                     # name
  234.   attr_reader   :index                    # cursor position
  235.   attr_reader   :max_char                 # maximum number of characters
  236.   #--------------------------------------------------------------------------
  237.   # initialize
  238.   #--------------------------------------------------------------------------
  239.   def initialize(obj, max_char, symbol)
  240.     x = (Graphics.width - 360) / 2
  241.     y = (Graphics.height - (fitting_height(4) + fitting_height(9) + 8)) / 2
  242.     super(x, y, 360, fitting_height(4))
  243.     @obj = obj
  244.     @type = symbol
  245.     @max_char = max_char
  246.     @default_name = @name = obj.name[0, @max_char]
  247.     @index = @name.size
  248.     deactivate
  249.     refresh
  250.   end
  251.  
  252.   #--------------------------------------------------------------------------
  253.   # restore_default
  254.   #--------------------------------------------------------------------------
  255.   def restore_default
  256.     @name = @default_name
  257.     @index = @name.size
  258.     refresh
  259.     return !@name.empty?
  260.   end
  261.  
  262.   #--------------------------------------------------------------------------
  263.   # add
  264.   #   ch : character to add
  265.   #--------------------------------------------------------------------------
  266.   def add(ch)
  267.     return false if @index >= @max_char
  268.     @name += ch
  269.     @index += 1
  270.     refresh
  271.     return true
  272.   end
  273.  
  274.   #--------------------------------------------------------------------------
  275.   # back
  276.   #--------------------------------------------------------------------------
  277.   def back
  278.     return false if @index == 0
  279.     @index -= 1
  280.     @name = @name[0, @index]
  281.     refresh
  282.     return true
  283.   end
  284.  
  285.   #--------------------------------------------------------------------------
  286.   # face_width
  287.   #--------------------------------------------------------------------------
  288.   def face_width
  289.     return 96
  290.   end
  291.  
  292.   #--------------------------------------------------------------------------
  293.   # char_width
  294.   #--------------------------------------------------------------------------
  295.   def char_width
  296.     text_size($game_system.japanese? ? "あ" : "A").width
  297.   end
  298.  
  299.   #--------------------------------------------------------------------------
  300.   # left
  301.   #--------------------------------------------------------------------------
  302.   def left
  303.     name_center = (contents_width + face_width) / 2
  304.     name_width = (@max_char + 1) * char_width
  305.     return [name_center - name_width / 2, contents_width - name_width].min
  306.   end
  307.  
  308.   #--------------------------------------------------------------------------
  309.   # item_rect
  310.   #--------------------------------------------------------------------------
  311.   def item_rect(index)
  312.     Rect.new(left + index * char_width, 36, char_width, line_height)
  313.   end
  314.  
  315.   #--------------------------------------------------------------------------
  316.   # underline_rect
  317.   #--------------------------------------------------------------------------
  318.   def underline_rect(index)
  319.     rect = item_rect(index)
  320.     rect.x += 1
  321.     rect.y += rect.height - 4
  322.     rect.width -= 2
  323.     rect.height = 2
  324.     rect
  325.   end
  326.  
  327.   #--------------------------------------------------------------------------
  328.   # underline_color
  329.   #--------------------------------------------------------------------------
  330.   def underline_color
  331.     color = normal_color
  332.     color.alpha = 48
  333.     color
  334.   end
  335.  
  336.   #--------------------------------------------------------------------------
  337.   # draw_underline
  338.   #--------------------------------------------------------------------------
  339.   def draw_underline(index)
  340.     contents.fill_rect(underline_rect(index), underline_color)
  341.   end
  342.  
  343.   #--------------------------------------------------------------------------
  344.   # draw_char
  345.   #--------------------------------------------------------------------------
  346.   def draw_char(index)
  347.     rect = item_rect(index)
  348.     rect.x -= 1
  349.     rect.width += 4
  350.     change_color(normal_color)
  351.     draw_text(rect, @name[index] || "")
  352.   end
  353.  
  354.   #--------------------------------------------------------------------------
  355.   # refresh
  356.   #--------------------------------------------------------------------------
  357.   def refresh
  358.     contents.clear
  359.     draw_obj_image(0, 0)
  360.     @max_char.times {|i| draw_underline(i) }
  361.     @name.size.times {|i| draw_char(i) }
  362.     cursor_rect.set(item_rect(@index))
  363.   end
  364.  
  365.   #--------------------------------------------------------------------------
  366.   # draw_obj_image
  367.   #--------------------------------------------------------------------------
  368.   def draw_obj_image(x, y)
  369.     case @type
  370.     when :skill, :item, :armor, :weapon, :state, :armour
  371.       draw_item_image
  372.     when :enemy
  373.       draw_enemy_image
  374.     when :class
  375.       draw_class_image
  376.     when :skilltype, :weapontype, :armortype, :element, :currency
  377.      
  378.     end
  379.   end
  380.    
  381.   #--------------------------------------------------------------------------
  382.   # draw_item_image               # Referenced from YF
  383.   #--------------------------------------------------------------------------
  384.   def draw_item_image
  385.     draw_bg_gradient
  386.     if @obj.rename_image
  387.       draw_rename_image
  388.     elsif $imported["YEA-ShopOptions"] && @obj.image
  389.       draw_shop_options_image
  390.     else
  391.       icon_index = @obj.icon_index
  392.       bitmap = Cache.system("Iconset")
  393.       rect = Rect.new(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)
  394.       target = Rect.new(0, 0, 96, 96)
  395.       contents.stretch_blt(target, bitmap, rect)
  396.     end
  397.   end
  398.  
  399.   #--------------------------------------------------------------------------
  400.   # draw_enemy_image
  401.   #--------------------------------------------------------------------------
  402.   def draw_enemy_image
  403.     draw_bg_gradient
  404.     if @obj.rename_image
  405.       draw_rename_image
  406.     elsif $imported["YEA-ShopOptions"] && @obj.image
  407.       draw_shop_options_image
  408.     else
  409.       bitmap = Cache.battler(@obj.battler_name, @obj.battler_hue)
  410.       target = Rect.new(0, 0, 96, 96)
  411.       contents.stretch_blt(target, bitmap, bitmap.rect)
  412.     end
  413.   end
  414.  
  415.   #--------------------------------------------------------------------------
  416.   # draw_class_image
  417.   #--------------------------------------------------------------------------
  418.   def draw_class_image
  419.     if @obj.rename_image
  420.       draw_rename_image
  421.     elsif $imported["YEA-ShopOptions"] && @obj.image
  422.       draw_shop_options_image
  423.     end
  424.   end
  425.  
  426.   #--------------------------------------------------------------------------
  427.   # draw_bg_gradient
  428.   #--------------------------------------------------------------------------
  429.   def draw_bg_gradient
  430.     color = Color.new(0, 0, 0, translucent_alpha / 2)
  431.     rect = Rect.new(1, 1, 94, 94)
  432.     contents.fill_rect(rect, color)
  433.   end
  434.  
  435.   #--------------------------------------------------------------------------
  436.   # draw_rename_image
  437.   #--------------------------------------------------------------------------
  438.   def draw_rename_image
  439.     bitmap = Cache.picture(@obj.rename_image)
  440.     contents.blt(0, 0, bitmap, bitmap.rect, 255)
  441.   end
  442.  
  443.   #--------------------------------------------------------------------------
  444.   # draw_shop_options_image
  445.   #--------------------------------------------------------------------------
  446.   def draw_shop_options_image
  447.     return unless $imported["YEA-ShopOptions"]
  448.     bitmap = Cache.picture(@obj.image)
  449.     contents.blt(0, 0, bitmap, bitmap.rect, 255)
  450.   end
  451.  
  452. end # class Window_RenameAnythingEdit
  453.  
  454.  
  455. #==============================================================================
  456. # ++ Scene_RenameAnything
  457. #==============================================================================
  458. class Scene_RenameAnything < Scene_MenuBase
  459.   #--------------------------------------------------------------------------
  460.   # prepare
  461.   #--------------------------------------------------------------------------
  462.   def prepare(obj, max_char, symbol)
  463.     @obj = obj
  464.     @max_char = max_char
  465.     @symbol = symbol
  466.   end
  467.   #--------------------------------------------------------------------------
  468.   # start
  469.   #--------------------------------------------------------------------------
  470.   def start
  471.     super
  472.     @edit_window = Window_RenameAnythingEdit.new(@obj, @max_char, @symbol)
  473.     @input_window = Window_NameInput.new(@edit_window)
  474.     @input_window.set_handler(:ok, method(:on_input_ok))
  475.   end
  476.   #--------------------------------------------------------------------------
  477.   # on_input_ok
  478.   #--------------------------------------------------------------------------
  479.   def on_input_ok
  480.     @obj.name = @edit_window.name
  481.     return_scene
  482.   end
  483. end # class Scene_RenameAnything
Advertisement
Add Comment
Please, Sign In to add comment