khanhdu

Victor Engine - Control Text

Jul 17th, 2017
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 11.17 KB | None | 0 0
  1. #==============================================================================
  2. # ** Victor Engine - Control Text
  3. #------------------------------------------------------------------------------
  4. # Author : Victor Sant
  5. #
  6. # Version History:
  7. #  v 1.00 - 2012.01.02 > First release
  8. #  v 1.01 - 2012.01.04 > Added break line (\n)
  9. #  v 1.02 - 2012.01.07 > Added constant to set the usage of control codes
  10. #                      > Added manual control text code
  11. #                      > Removed the new control codes, they're going to be
  12. #                        added with a separated script.
  13. #                      > Added text replace constant for small text box
  14. #  v 1.03 - 2012.01.10 > Added compatibility with Control Codes
  15. #  v 1.04 - 2012.01.15 > Added compatibility with Target Arrow
  16. #  v 1.05 - 2012.03.15 > Compatibility with ENG version/VXAce_SP1
  17. #  v 1.06 - 2012.08.18 > Fixed issue with some characters
  18. #------------------------------------------------------------------------------
  19. #  This script allows the use to use Control Codes (\c[x], \n[x] and such)
  20. # in every text in a window. By default, only the message box and help text
  21. # have this feature. But with this script you can use it in any window.
  22. #------------------------------------------------------------------------------
  23. # Compatibility
  24. #   Requires the script 'Victor Engine - Basic Module' v 1.07
  25. #
  26. # * Overwrite methods (Default)
  27. #   class Window_Base < Window
  28. #     def process_normal_character(c, pos)
  29. #
  30. # * Alias methods (Default)
  31. #   class Window_Base < Window
  32. #     def draw_text(*args)
  33. #     def convert_escape_characters(text)
  34. #
  35. #------------------------------------------------------------------------------
  36. # Instructions:
  37. #  To instal the script, open you script editor and paste this script on
  38. #  a new section bellow the Materials section. This script must also
  39. #  be bellow the script 'Victor Engine - Basic'
  40. #
  41. #------------------------------------------------------------------------------
  42. # Additional instructions:
  43. #
  44. #  Text with codes have a slow process, slower than the default draw text
  45. #  (wich is very slow by itself). Also, text with codes won't stretch to
  46. #  fit the text width. So you can set if all text will automatically use codes
  47. #  or if only specified text will do.
  48. #  If VE_AUTOMATIC_TEXT_CODE = true, all text will automatically use codes,
  49. #  if false you will need to add the code \# at the start of any text
  50. #  you might want to use control codes.
  51. #
  52. #  Some of the database boxes have limited space, and would be hard to add
  53. #  some codes on them, so you can have the script to replace these text
  54. #  by using the code \r[code], this will make the text be replaced with the
  55. #  text set on the script setting VE_TEXT_REPLACE.
  56. #
  57. #  Text within strings on the script editor using double quotation (""), like
  58. #  the ones on the Vocab module, needs two backslashes to use the codes.
  59. #  E.g.:
  60. #    Emerge = "%s \\c[1]emerged!\\c[0]"
  61. #
  62. #  The text will not stretch to fit the window if it's the text width is
  63. #  larger than the text width.
  64. #
  65. #==============================================================================
  66.  
  67. #==============================================================================
  68. # ** Victor Engine
  69. #------------------------------------------------------------------------------
  70. #   Setting module for the Victor Engine
  71. #==============================================================================
  72.  
  73. module Victor_Engine
  74.   #--------------------------------------------------------------------------
  75.   # * Set the automatic text control codes
  76.   #   when false, it's needed to add \# on the start of the text to use
  77.   #   control codes
  78.   #--------------------------------------------------------------------------
  79.   VE_AUTOMATIC_TEXT_CODE = false
  80.   #--------------------------------------------------------------------------
  81.   # * Set the automatic text raplace
  82.   #   The text here will replace the code when you use the code \r[code]
  83.   #   The code must be downcase and have no white space.
  84.   #--------------------------------------------------------------------------
  85.   VE_TEXT_REPLACE = {
  86.     new:  "\\#\\i[125]\\c[4]New Game\\c[0]",
  87.     load: "\\#\\i[126]\\c[3]Continue\\c[0]",
  88.     shut: "\\#\\i[127]\\c[2]Shutdown\\c[0]",
  89.   } # Don't remove
  90.   VE_TEXT_REPLACE.default("")
  91.   #--------------------------------------------------------------------------
  92.   # * required
  93.   #   This method checks for the existance of the basic module and other
  94.   #   VE scripts required for this script to work, don't edit this
  95.   #--------------------------------------------------------------------------
  96.   def self.required(name, req, version, type = nil)
  97.     if !$imported[:ve_basic_module]
  98.       msg = "The script '%s' requires the script\n"
  99.       msg += "'VE - Basic Module' v%s or higher above it to work properly\n"
  100.       msg += "Go to http://victorenginescripts.wordpress.com/ to download this script."
  101.       msgbox(sprintf(msg, self.script_name(name), version))
  102.       exit
  103.     else
  104.       self.required_script(name, req, version, type)
  105.     end
  106.   end
  107.   #--------------------------------------------------------------------------
  108.   # * script_name
  109.   #   Get the script name base on the imported value, don't edit this
  110.   #--------------------------------------------------------------------------
  111.   def self.script_name(name, ext = "VE")
  112.     name = name.to_s.gsub("_", " ").upcase.split
  113.     name.collect! {|char| char == ext ? "#{char} -" : char.capitalize }
  114.     name.join(" ")
  115.   end
  116. end
  117.  
  118. $imported ||= {}
  119. $imported[:ve_control_text] = 1.06
  120. Victor_Engine.required(:ve_control_text, :ve_basic_module, 1.07, :above)
  121. Victor_Engine.required(:ve_control_text, :ve_control_code, 1.00, :bellow)
  122.  
  123. #==============================================================================
  124. # ** Window_Base
  125. #------------------------------------------------------------------------------
  126. #  This is a superclass of all windows in the game.
  127. #==============================================================================
  128.  
  129. class Window_Base < Window
  130.   #--------------------------------------------------------------------------
  131.   # * Overwrite method: process_normal_character
  132.   #--------------------------------------------------------------------------
  133.   def process_normal_character(c, pos)
  134.     return unless c >= ' '
  135.     w = text_size(c).width
  136.     draw_text_ve_control_text(pos[:x], pos[:y], w * 2, pos[:height], c)
  137.     pos[:x] += w
  138.   end  
  139.   #--------------------------------------------------------------------------
  140.   # * Alias method: obtain_escape_code
  141.   #--------------------------------------------------------------------------
  142.   alias :obtain_escape_code_ve_control_text :obtain_escape_code
  143.   def obtain_escape_code(text)
  144.     text =~ /^#/i ? text.slice!(/^#/i) :
  145.     obtain_escape_code_ve_control_text(text)
  146.   end
  147.   #--------------------------------------------------------------------------
  148.   # * Alias method: draw_text
  149.   #--------------------------------------------------------------------------
  150.   alias :draw_text_ve_control_text :draw_text
  151.   def draw_text(*args)
  152.     text = args[0].is_a?(Rect) ? args[1].to_s : args[4].to_s
  153.     if VE_AUTOMATIC_TEXT_CODE || text =~ /\\[#]/i
  154.       draw_code_text(*args)
  155.     else
  156.       draw_text_ve_control_text(*args)
  157.     end
  158.   end
  159.   #--------------------------------------------------------------------------
  160.   # * Alias method: convert_escape_characters
  161.   #--------------------------------------------------------------------------
  162.   alias :convert_escape_characters_ve_control_text :convert_escape_characters
  163.   def convert_escape_characters(text)
  164.     result = text.to_s.clone
  165.     begin
  166.       result = text_replace(result)
  167.       result = convert_escape_characters_ve_control_text(result)
  168.     end until !result.include?("\\")
  169.     result.gsub!(/\a/) { "\\" }
  170.     result
  171.   end
  172.   #--------------------------------------------------------------------------
  173.   # * Alias method: draw_code_text
  174.   #--------------------------------------------------------------------------
  175.   def draw_code_text(*args)
  176.     is_rect = args[0].is_a?(Rect)
  177.     x = is_rect ? args[0].x      : args[0]
  178.     y = is_rect ? args[0].y      : args[1]
  179.     w = is_rect ? args[0].width  : args[2]
  180.     h = is_rect ? args[0].height : args[3]
  181.     t = is_rect ? args[1].to_s   : args[4].to_s
  182.     a = is_rect ? args[2]        : args[5]
  183.     @text_align  = a
  184.     @text_width  = w
  185.     @text_height = h
  186.     @original_text = t.dup
  187.     draw_text_ve(x, y, t)
  188.   end
  189.   #--------------------------------------------------------------------------
  190.   # * Alias method: text_replace
  191.   #--------------------------------------------------------------------------
  192.   alias :text_replace_ve_control_text :text_replace
  193.   def text_replace(result)
  194.     result = text_replace_ve_control_text(result)
  195.     result.gsub!(/\e\e/) { "\a" }
  196.     result.gsub!(/\e\#/) { "" }
  197.     result.gsub!(/\eR\[(\w+)\]/i) { VE_TEXT_REPLACE[$1.downcase.to_sym] }
  198.     result
  199.   end
  200.   #--------------------------------------------------------------------------
  201.   # * New method: reset_font_settings_ve
  202.   #--------------------------------------------------------------------------
  203.   def reset_font_settings_ve
  204.     contents.font.bold      = Font.default_bold
  205.     contents.font.italic    = Font.default_italic
  206.     contents.font.shadow    = Font.default_shadow
  207.     contents.font.outline   = Font.default_outline
  208.     contents.font.out_color = Font.default_out_color
  209.   end
  210.   #--------------------------------------------------------------------------
  211.   # * New method: process_character_ve
  212.   #--------------------------------------------------------------------------
  213.   def process_character_ve(c, t, p)
  214.     case c
  215.     when "\n" then process_new_line_ve(t, p)
  216.     when "\f" then process_new_page(t, p)
  217.     when "\e" then process_escape_character(obtain_escape_code(t), t, p)
  218.     else process_normal_character(c, p)
  219.     end
  220.   end
  221.   #--------------------------------------------------------------------------
  222.   # * New method: process_new_line_ve
  223.   #--------------------------------------------------------------------------
  224.   def process_new_line_ve(text, pos)
  225.     pos[:x] = get_x(pos[:new_x])
  226.     pos[:y] += pos[:height]
  227.     pos[:height] = calc_line_height(text)
  228.   end
  229.   #--------------------------------------------------------------------------
  230.   # * New method: draw_text_ve
  231.   #--------------------------------------------------------------------------
  232.   def draw_text_ve(x, y, text)
  233.     reset_font_settings_ve
  234.     text = convert_escape_characters(text)
  235.     pos  = {x: get_x(x), y: y, new_x: x, height: @text_height}
  236.     process_character_ve(text.slice!(0, 1), text, pos) until text.empty?
  237.   end
  238.   #--------------------------------------------------------------------------
  239.   # * New method: get_x
  240.   #--------------------------------------------------------------------------
  241.   def get_x(x)
  242.     pos = 0
  243.     begin
  244.       c = @original_text.slice!(0, 1)
  245.       text_width = text_size(c).width
  246.       pos += text_width unless ["\n", "\f", "\e", "\a"].include?(c)
  247.     end until c == "\n" || @original_text.empty?
  248.     case @text_align
  249.     when 1 then x + (@text_width - pos) / 2
  250.     when 2 then x + (@text_width - pos)
  251.     else x
  252.     end
  253.   end
  254. end
Advertisement
Add Comment
Please, Sign In to add comment