CrowMakerVX

StringKeeper

Jan 20th, 2013
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 18.95 KB | None | 0 0
  1. ################################################################################
  2. #                       STRING KEEPER
  3. #                         By: Ixfuru
  4. #                      Released 1/20/13
  5. ################################################################################
  6. #
  7. # This Script does several things.  All of which are in regard to handling of
  8. # strings.   Strings, for those of you who don't know, are most often words.  
  9. # Usually strings are difficult to use in projects without the direct use of
  10. # scripts.  As the only source of strings for the eventer is the Name
  11. # Processing event command.  This script can be used in the following ways:
  12. #
  13. #  1) Store Strings for random use:  The script enables you to allow player
  14. #      input of strings, which you can use in a variety of ways.  The strings
  15. #       are stored in an array, and can be accessed through a script call:
  16. #
  17. #                $game_stringkeeper.strings[string_id]
  18. #      
  19. #      With string_id being the string's index in the array that you want to
  20. #     retrieve.  As strings are saved by editting existing strings or by adding
  21. #     new ones, the strings are either pushed into the array, extending the
  22. #    list of strings, or if editting, they replace the string at the given
  23. #     index.  For example, if you allowed the player to randomly put in a bunch
  24. #    of strings, and they wrote "Hat" for one, "Juice" for one, "Block" for
  25. #    another.  The array of strings when stored would look like this:
  26. #              
  27. #                     ["Hat", "Juice", "Block"]
  28. #
  29. #    Knowing that arrays operate on a 0-index basis, you would then access them
  30. #     like this:
  31. #
  32. #               $game_stringkeeper.strings[0] for "Hat"
  33. #               $game_stringkeeper.strings[1] for "Juice"
  34. #               $game_stringkeeper.strings[2] for "Block"
  35. #
  36. #     You can delete strings with the script call:
  37. #        
  38. #               $game_stringkeeper.delete_string(string_id)
  39. #
  40. # 2) The script comes with a new scene, much like the Name Processing scene,
  41. #    which allows the player to input the string at runtime.  You, as the
  42. #     creator, have complete control over the way the input string is handled
  43. #     when calling the scene.  There are two ways to handle the scene when
  44. #    calling it.  You can call it at any time using a script call from the
  45. #     event commands 'script' feature.  The two ways are this:
  46. #
  47. #     If you want to call the script for a new string, they you place:
  48. #
  49. #               $scene = Scene_StringKeeper.new(character_limit)
  50. #
  51. #     The character_limit should be replaced with the amount of characters
  52. #     (or letters) you wish for the player to be able to input. When called this
  53. #     way, the string is saved as a NEW STRING.  It is added onto the strings
  54. #    already created.
  55. #
  56. #     However, if you wish to edit an existing string, which is ideal for
  57. #     passwords and such, you can use this command instead.
  58. #
  59. #          $scene = Scene_StringKeeper.new(character_limit, true, string_id)
  60. #
  61. #     This will tell the system that you are overwriting an existing string, and
  62. #     will subsequently replace the old string in 'string_id' location with the
  63. #      new string.  
  64. #
  65. #     So, if you were creating a password, you can edit an existing string, and
  66. #    then immediately following the scene, check to see if the string matches
  67. #     the PASSWORDS, which you've set up in the settings of the script below.
  68. #      Just use the following script in a conditional branch.
  69. #
  70. #           $game_stringkeeper.password_match(string, password_id)
  71. #
  72. #       This will return true if the password matches, and false if it does not.
  73. #
  74. #  3) In addition to these ways, the script has another tool which can be used
  75. #       to replace the Name Processing.  You can use the script call below to
  76. #      name an actor one of the existing strings:
  77. #
  78. #           $game_stringkeeper.string_to_name(actor_id, string_id)
  79. #
  80. #      Replace the actor_id part with the database ID of the actor you wish to
  81. #   change, and the string_id, with the string you wish to become the actor's
  82. #    name.
  83. #
  84. #  4) Finally, for those game makers out there, the script offers a stringlist
  85. #      debug tool which can be used during playtesting, to see what the existing
  86. #      scripts are.  This debugger can be opened via Input from the map scene.
  87. #      You can define which button to press in the settings below.
  88. #
  89. ################################################################################
  90. #                          INSTRUCTIONS
  91. # Besides what we covered above, the script can be used plug and play if you
  92. # stick it in the script editor below materials and above main.  However, without
  93. # proper use of the script calls, you won't get much besides a bunch of randomly
  94. # input strings and the ability to check them during playtest.
  95. #
  96. # During the input scene, you can use these inputs:
  97. #
  98. #              C (action button): enters letters
  99. #              B (back button) : backs out of scene or from input to commands
  100. #              Y (S on your keyboard): deletes a character
  101. #              L (Q on your keyboard): jumps one space left
  102. #              R (W on your keyboard): jumps one space right
  103. #              
  104. ################################################################################
  105.  
  106. ################################################################################
  107. #                         CREDITS
  108. # Use this script in your project.  Just make sure you give credit to Ixfuru. If
  109. # you wish to use commercially, you'll have to contact me via the RPGMakerVX.net
  110. # forums.
  111. ################################################################################
  112.  
  113. module Ixfuru
  114.  
  115.   module StringKeeper
  116.    
  117.     ############################################################################
  118.     #                     SETTINGS START
  119.     ############################################################################
  120.    
  121.     # This is a hash storing passwords you may want to compare with input
  122.     # strings later on.  Place them like this:
  123.     #
  124.     #    password_id => password
  125.     #
  126.     # The password_ids must be in numerical order starting from 0.  The passwords
  127.     # can be anything you want them to be.  Just make sure you don't forget the
  128.     # commas after each one.
  129.    
  130.     PASSWORDS = {#<<<Don't delete this!
  131.    
  132.     0 => "Ralph",
  133.     1 => "Swords",
  134.     2 => "Potions",
  135.     3 => "Enemies",
  136.    
  137.     }#<<<Don't delete this!
  138.    
  139.     # This is the settings for the debugger during playtest.  Make sure you set
  140.     # this to false if you don't want the player having access to the strings.
  141.     VIEW_STRINGS = true
  142.     VIEW_STRINGS_INPUT = Input::F5
  143.    
  144.     # This is the default string character.  It is the character placed inside
  145.     # the string at each empty position in the string or at the beginning of the
  146.     # StringKeeper input scene.
  147.     DEFAULT_STRING = " "
  148.    
  149.     # This is the default character limit.  I'm not sure what will happen if you
  150.     # go above 12.  But 12 fits nicely.
  151.     DEFAULT_STRING_LIMIT = 12
  152.    
  153.     # These are the sound effects for the Input scene.  
  154.     ENTER_SE = "Switch2"
  155.     ERASE_SE = "Stare"
  156.     LETTERJUMP_SE = "Saint3"
  157.    
  158.   end
  159.  
  160. end
  161.  
  162.  
  163. ################################################################################
  164. #*******************************************************************************
  165. #                      GAME STRING KEEPER
  166. ################################################################################
  167. class Game_StringKeeper
  168.  
  169.   attr_accessor :strings
  170.   attr_accessor :passwords
  171.  
  172.   def initialize
  173.     @strings = []
  174.     @passwords = Ixfuru::StringKeeper::PASSWORDS
  175.   end
  176.  
  177.   def add_string(string)
  178.     @strings.push(string)
  179.   end
  180.  
  181.   def password_match(string, key)
  182.     return true if string == Ixfuru::StringKeeper::PASSWORDS[key]
  183.     return false
  184.   end
  185.  
  186.   def string_to_name(actor_id, string_id)
  187.     actor = $game_actors[actor_id]
  188.     string = @strings[string_id]
  189.     actor.name = string
  190.   end
  191.  
  192.   def delete_string(string_id)
  193.     @strings.delete(string_id)
  194.   end
  195.  
  196.  
  197. end
  198.  
  199. ################################################################################
  200. #*******************************************************************************
  201. #                       SCENE TITLE
  202. ################################################################################
  203. class Scene_Title < Scene_Base
  204.  
  205.   #--------------------------------------------------------------------------
  206.   # * Create Game Objects
  207.   #--------------------------------------------------------------------------
  208.   alias ix_skeeper_sctitle_create_game_objects create_game_objects unless $@
  209.   def create_game_objects
  210.     ix_skeeper_sctitle_create_game_objects
  211.     $game_stringkeeper  = Game_StringKeeper.new
  212.   end
  213.  
  214. end
  215.  
  216. ################################################################################
  217. #*******************************************************************************
  218. #                    SCENE FILE
  219. ################################################################################
  220. class Scene_File < Scene_Base
  221.  
  222.   #--------------------------------------------------------------------------
  223.   # * Write Save Data
  224.   #     file : write file object (opened)
  225.   #--------------------------------------------------------------------------
  226.   alias ix_skeeper_write_save_data write_save_data unless $@
  227.   def write_save_data(file)
  228.     ix_skeeper_write_save_data(file)
  229.     Marshal.dump($game_stringkeeper, file)
  230.   end
  231.  
  232.   #--------------------------------------------------------------------------
  233.   # * Read Save Data
  234.   #     file : file object for reading (opened)
  235.   #--------------------------------------------------------------------------
  236.   alias ix_skeeper_read_save_data read_save_data unless $@
  237.   def read_save_data(file)
  238.     $game_stringkeeper = Marshal.load(file)
  239.     ix_skeeper_read_save_data(file)
  240.   end
  241.  
  242. end
  243.  
  244. ################################################################################
  245. #*******************************************************************************
  246. #                      WINDOW STRING COMMANDS
  247. ################################################################################
  248. class Window_StringCommands < Window_Selectable
  249.  
  250.   STRINGCENTER_X = 544 / 2 - 80
  251.  
  252.   def initialize
  253.     super(STRINGCENTER_X, 336, 160, 80)
  254.     # enter string, finished
  255.     c1 = "Enter String"
  256.     c2 = "Finished"
  257.     @commands = [c1, c2]
  258.     @item_max = @commands.size
  259.     refresh
  260.     self.index = 0
  261.   end
  262.  
  263.   def refresh
  264.     self.contents.clear
  265.     for i in 0...@item_max
  266.       draw_items(i)
  267.     end
  268.   end
  269.  
  270.   def draw_items(index)
  271.     rect = item_rect(index)
  272.     rect.x += 4
  273.     rect.width -= 8
  274.     self.contents.clear_rect(rect)
  275.     self.contents.draw_text(rect, @commands[index], 1)
  276.   end
  277.  
  278. end
  279.  
  280. ################################################################################
  281. #*******************************************************************************
  282. #                         WINDOW STRING STATUS
  283. ################################################################################
  284. class Window_StringStatus < Window_Selectable
  285.  
  286.   STATUSCENTER_X = 544 / 2 - 172
  287.  
  288.   attr_accessor :characters
  289.   attr_accessor :string
  290.  
  291.   def initialize(limit = Ixfuru::StringKeeper::DEFAULT_STRING_LIMIT)
  292.     super(STATUSCENTER_X, 0, 344, 64)
  293.     @string_limit = limit
  294.     @characters = []
  295.     for i in 0...@string_limit
  296.       @characters.push(Ixfuru::StringKeeper::DEFAULT_STRING)
  297.     end
  298.     @column_max = @string_limit
  299.     refresh
  300.     self.index = 0
  301.     self.active = false
  302.   end
  303.  
  304.   def refresh
  305.     self.contents.clear
  306.     for i in 0...@string_limit
  307.       draw_characters(i)
  308.     end
  309.     get_string
  310.   end
  311.  
  312.   def draw_characters(index)
  313.     rect = item_rect(index)
  314.     self.contents.clear_rect(rect)
  315.     self.contents.draw_text(rect, @characters[index])
  316.   end
  317.  
  318.   def item_rect(index)
  319.     rect = Rect.new(0, 0, 0, 0)
  320.     rect.width = contents.width / @column_max
  321.     rect.height = WLH
  322.     rect.x = index % @column_max * rect.width
  323.     rect.y = index / @column_max * WLH
  324.     return rect
  325.   end
  326.  
  327.   def get_string
  328.     @string = @characters.to_s
  329.   end
  330.  
  331. end
  332.  
  333. ################################################################################
  334. #*******************************************************************************
  335. #                  Window String Input
  336. ################################################################################
  337. class Window_StringInput < Window_Selectable
  338.  
  339.   INPUTCENTER_X = 544 / 2 - 172
  340.  
  341.   attr_accessor :letters
  342.  
  343.   def initialize
  344.     super(INPUTCENTER_X, 65, 344, 272)
  345.     @letters = ["A", "a", "B", "b", "C", "c", "D", "d", "E", "e", "F", "f", "G",
  346.     "g", "H", "h", "I", "i", "J", "j", "K", "k", "L", "l", "M", "m", "N", "n",
  347.     "O", "o", "P", "p", "Q", "q", "R", "r", "S", "s", "T", "t", "U", "u", "V",
  348.     "v", "W", "w", "X", "x", "Y", "y", "Z", "z", "1", "2", "3", "4", "5", "6",
  349.     "7", "8", "9", "0", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-",
  350.     "_", "=", "+", "~", "<", ">"]
  351.     @item_max = @letters.size
  352.     @column_max = 9
  353.     refresh
  354.     self.index = 0
  355.     self.active = false
  356.   end
  357.  
  358.   def refresh
  359.     self.contents.clear
  360.     for i in 0...@item_max
  361.       draw_letters(i)
  362.     end
  363.   end
  364.  
  365.   def draw_letters(index)
  366.     rect = item_rect(index)
  367.     self.contents.draw_text(rect, @letters[index])
  368.   end
  369.  
  370.   #--------------------------------------------------------------------------
  371.   # * Get rectangle for displaying items
  372.   #     index : item number
  373.   #--------------------------------------------------------------------------
  374.   def item_rect(index)
  375.     rect = Rect.new(0, 0, 0, 0)
  376.     @spacing = 24
  377.     rect.width = (contents.width + @spacing) / @column_max - @spacing
  378.     rect.height = WLH
  379.     rect.x = index % @column_max * (rect.width + @spacing)
  380.     rect.y = index / @column_max * WLH
  381.     return rect
  382.   end
  383.  
  384. end
  385.  
  386. ################################################################################
  387. #*******************************************************************************
  388. #                       SCENE STRINGKEEPER
  389. ################################################################################
  390. class Scene_StringKeeper < Scene_Base
  391.  
  392.   def initialize(limit, edit = false, id = 0)
  393.     @limit = limit
  394.     @edit = edit
  395.     @id = id
  396.   end
  397.  
  398.   def start
  399.     super
  400.     @win_commands = Window_StringCommands.new
  401.     @win_status = Window_StringStatus.new(@limit)
  402.     @win_input = Window_StringInput.new
  403.   end
  404.  
  405.   def update
  406.     super
  407.     if @win_commands.active
  408.       update_commands
  409.     elsif @win_input.active
  410.       update_input
  411.     end
  412.   end
  413.  
  414.   def update_commands
  415.     @win_commands.update
  416.     if Input.trigger?(Input::C)
  417.       case @win_commands.index
  418.       when 0
  419.         Sound.play_decision
  420.         activate_input
  421.       when 1
  422.         Sound.play_decision
  423.         store_new_string
  424.         $scene = Scene_Map.new
  425.       end
  426.     elsif Input.trigger?(Input::B)
  427.       Sound.play_cancel
  428.       $scene = Scene_Map.new
  429.     end
  430.   end
  431.  
  432.   def activate_input
  433.     @win_input.active = true
  434.     @win_commands.active = false
  435.   end
  436.  
  437.   def update_input
  438.     @win_input.update
  439.     if Input.trigger?(Input::C)
  440.       RPG::SE.new(Ixfuru::StringKeeper::ENTER_SE, 80, 100).play
  441.       @win_status.characters[@win_status.index] = @win_input.letters[@win_input.index]
  442.       if @win_status.index < @limit - 1
  443.         @win_status.index += 1
  444.       end
  445.       @win_status.refresh
  446.     elsif Input.trigger?(Input::B)
  447.       Sound.play_cancel
  448.       @win_status.refresh
  449.       back_to_commands
  450.     elsif Input.trigger?(Input::Y)
  451.       RPG::SE.new(Ixfuru::StringKeeper::ERASE_SE, 80, 100).play
  452.       if @win_status.index == @limit - 1
  453.         @win_status.characters[@win_status.index] = Ixfuru::StringKeeper::DEFAULT_STRING
  454.       end
  455.       @win_status.index -= 1 if @win_status.index > 0
  456.       @win_status.characters[@win_status.index] = Ixfuru::StringKeeper::DEFAULT_STRING
  457.       @win_status.refresh
  458.     elsif Input.trigger?(Input::L)
  459.       RPG::SE.new(Ixfuru::StringKeeper::LETTERJUMP_SE, 80, 100).play
  460.       @win_status.index -= 1 if @win_status.index > 0
  461.     elsif Input.trigger?(Input::R)
  462.       RPG::SE.new(Ixfuru::StringKeeper::LETTERJUMP_SE, 80, 100).play
  463.       @win_status.index += 1 if @win_status.index < @limit - 1
  464.     end
  465.   end
  466.  
  467.   def store_new_string
  468.     string = @win_status.string
  469.     if @edit
  470.       $game_stringkeeper.strings[@id] = string
  471.     else
  472.       $game_stringkeeper.add_string(string)
  473.     end
  474.   end
  475.  
  476.   def back_to_commands
  477.     @win_commands.active = true
  478.     @win_input.active = false
  479.   end
  480.  
  481.   def terminate
  482.     @win_commands.dispose
  483.     @win_status.dispose
  484.     @win_input.dispose
  485.   end
  486.  
  487. end
  488.  
  489. ################################################################################
  490. #*******************************************************************************
  491. #                        WINDOW STRINGSLIST
  492. ################################################################################
  493. class Window_Stringslist < Window_Selectable
  494.  
  495.   def initialize
  496.     super(142, 0, 220, 416)
  497.     @item_max = $game_stringkeeper.strings.size
  498.     refresh
  499.     self.index = 0
  500.   end
  501.  
  502.   def refresh
  503.     self.contents.clear
  504.     create_contents
  505.     for i in 0...@item_max
  506.       draw_items(i)
  507.     end
  508.   end
  509.  
  510.   def draw_items(index)
  511.     rect = item_rect(index)
  512.     rect.x += 4
  513.     rect.width -= 8
  514.     self.contents.clear_rect(rect)
  515.     self.contents.draw_text(rect.x, rect.y, self.width, WLH, index.to_s)
  516.     self.contents.draw_text(rect.x + 30, rect.y, self.width - 30, WLH, $game_stringkeeper.strings[index])
  517.   end
  518.  
  519. end
  520.  
  521. ################################################################################
  522. #*******************************************************************************
  523. #                        SCENE STRINGSLIST
  524. ################################################################################
  525. class Scene_Stringslist < Scene_Base
  526.  
  527.   def start
  528.     super
  529.     @win_strings = Window_Stringslist.new
  530.     @win_strings.active = true
  531.   end
  532.  
  533.   def update
  534.     super
  535.     @win_strings.update
  536.     if Input.trigger?(Input::C) or Input.trigger?(Input::B)
  537.       Sound.play_cancel
  538.       $scene = Scene_Map.new
  539.     end
  540.   end
  541.  
  542.   def terminate
  543.     @win_strings.dispose
  544.   end
  545.  
  546. end
  547.  
  548. ################################################################################
  549. #*******************************************************************************
  550. #                       SCENE MAP
  551. ################################################################################
  552. class Scene_Map < Scene_Base
  553.  
  554.   alias ix_skeeper_scmap_update update unless $@
  555.   def update
  556.     ix_skeeper_scmap_update
  557.     update_call_stringslist
  558.   end
  559.  
  560.   def update_call_stringslist
  561.     if Input.trigger?(Ixfuru::StringKeeper::VIEW_STRINGS_INPUT)
  562.       Sound.play_decision
  563.       $scene = Scene_Stringslist.new
  564.     end
  565.   end
  566.  
  567. end
Advertisement
Add Comment
Please, Sign In to add comment