Guest User

Andrew - Message Enhancements

a guest
Nov 19th, 2012
97
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #==============================================================================
  2. # ** Sprite_Picture
  3. #------------------------------------------------------------------------------
  4. #  This sprite is used to display pictures. It observes an instance of the
  5. # Game_Picture class and automatically changes sprite states.
  6. #==============================================================================
  7.  
  8. class Sprite_Face < Sprite
  9.   #--------------------------------------------------------------------------
  10.   # * Public Instance Variables
  11.   #--------------------------------------------------------------------------
  12.   attr_reader   :face_name
  13.   attr_reader   :face_index
  14.   #--------------------------------------------------------------------------
  15.   # * Object Initialization
  16.   #     picture : Game_Picture
  17.   #--------------------------------------------------------------------------
  18.   def initialize(viewport, face_name = nil, face_index = 0)
  19.     super(viewport)
  20.     @face_name = face_name
  21.     @face_index = face_index
  22.     update
  23.   end
  24.   #--------------------------------------------------------------------------
  25.   # * Set Face
  26.   #--------------------------------------------------------------------------
  27.   def set_face(face_name, face_index)
  28.     @face_name = face_name
  29.     @face_index = face_index
  30.     update
  31.   end
  32.   #--------------------------------------------------------------------------
  33.   # * Free
  34.   #--------------------------------------------------------------------------
  35.   def dispose
  36.     bitmap.dispose if bitmap
  37.     super
  38.   end
  39.   #--------------------------------------------------------------------------
  40.   # * Frame Update
  41.   #--------------------------------------------------------------------------
  42.   def update
  43.     super
  44.     update_bitmap
  45.   end
  46.   #--------------------------------------------------------------------------
  47.   # * Update Transfer Origin Bitmap
  48.   #--------------------------------------------------------------------------
  49.   def update_bitmap
  50.     if @face_name == nil
  51.       self.bitmap = nil
  52.     else
  53.       self.bitmap = Cache.face(@face_name)
  54.       w = self.bitmap.width
  55.       h = self.bitmap.height
  56.       rect = Rect.new(@face_index % 4 * w/4, @face_index / 4 * h/2, w/4, h/2)
  57.       self.src_rect = rect
  58.     end
  59.   end
  60. end
  61.  
  62. #==============================================================================
  63. # ** Game_Message
  64. #------------------------------------------------------------------------------
  65. #  This class handles the state of the message window that displays text or
  66. # selections, etc. The instance of this class is referenced by $game_message.
  67. #==============================================================================
  68.  
  69. class Game_Message
  70.   #--------------------------------------------------------------------------
  71.   # * Public Instance Variables
  72.   #--------------------------------------------------------------------------
  73.   attr_reader   :shortcuts
  74.   #--------------------------------------------------------------------------
  75.   # * Initialize
  76.   #--------------------------------------------------------------------------
  77.   alias :pre_me_initialize  :initialize unless $@
  78.   def initialize
  79.     @shortcuts = {}
  80.     pre_me_initialize # original function
  81.   end
  82.   #--------------------------------------------------------------------------
  83.   # * Shortcuts getter
  84.   #--------------------------------------------------------------------------
  85.   def shortcuts; return @shortcuts; end
  86.   #--------------------------------------------------------------------------
  87.   # * Add a Shortcut
  88.   #--------------------------------------------------------------------------
  89.   def add_shortcut(symbol, text)
  90.     text.gsub!(/\\/)            { "\e" }
  91.     text.gsub!(/\e\e/)          { "\\" }
  92.     shortcuts[symbol] = text if !shortcuts.include?(symbol)
  93.   end
  94.   #--------------------------------------------------------------------------
  95.   # * Apply Shortcuts
  96.   #--------------------------------------------------------------------------
  97.   def apply_shortcuts(source)
  98.     shortcuts.each { |symbol, text| source.gsub!(symbol) { text } }
  99.     return source
  100.   end
  101. end
  102. #==============================================================================
  103. # ** Window_Base
  104. #------------------------------------------------------------------------------
  105. #  This is a super class of all windows within the game.
  106. #==============================================================================
  107.  
  108. class Window_Base < Window
  109.   #--------------------------------------------------------------------------
  110.   # * Public Instance Variables
  111.   #--------------------------------------------------------------------------
  112.   attr_reader :skip_new_lines
  113.   #--------------------------------------------------------------------------
  114.   # * Object Initialization
  115.   #--------------------------------------------------------------------------
  116.   alias :pre_me_initialize  :initialize unless $@
  117.   def initialize(x, y, width, height)
  118.     @skip_new_lines = 0
  119.     pre_me_initialize(x, y, width, height) # original function
  120.   end
  121.   #--------------------------------------------------------------------------
  122.   # * Character Processing
  123.   #     c    : Characters
  124.   #     text : A character string buffer in drawing processing (destructive)
  125.   #     pos  : Draw position {:x, :y, :new_x, :height}
  126.   #--------------------------------------------------------------------------
  127.   alias :pre_me_process_character :process_character  unless $@
  128.   def process_character(c, text, pos)
  129.     case c
  130.     when "\x10" # \ignr
  131.       @skip_new_lines += 1
  132.     when "\n"   # New line
  133.       process_new_line(text, pos) if @skip_new_lines <= 0
  134.       @skip_new_lines -= 1 if @skip_new_lines > 0
  135.     when "\f"   # New page
  136.       process_new_page(text, pos)
  137.     when "\e"   # Control character
  138.       process_escape_character(obtain_escape_code(text), text, pos)
  139.     else        # Normal character
  140.       process_normal_character(c, pos)
  141.     end
  142.   end
  143.   #--------------------------------------------------------------------------
  144.   # * Preconvert Control Characters
  145.   #    As a rule, replace only what will be changed into text strings before
  146.   #    starting actual drawing. The character "\" is replaced with the escape
  147.   #    character (\e).
  148.   #--------------------------------------------------------------------------
  149.   alias :pre_me_convert_escape_characters  :convert_escape_characters unless $@
  150.   def convert_escape_characters(text)
  151.     # Create the result string
  152.     result = text.to_s.clone
  153.     # Add in shortcut codes
  154.     result = $game_message.apply_shortcuts(result)
  155.     # Run the normal conversion
  156.     result.gsub!(/\\/)            { "\e" }
  157.     result.gsub!(/\e\e/)          { "\\" }
  158.     result.gsub!(/\e[Ii][Gg][Nn][Rr]/i) { "\x10" }
  159.     result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i] }
  160.     result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i] }
  161.     result.gsub!(/\eN\[(\d+)\]/i) { actor_name($1.to_i) }
  162.     result.gsub!(/\eP\[(\d+)\]/i) { party_member_name($1.to_i) }
  163.     result.gsub!(/\eG/i)          { Vocab::currency_unit }
  164.     # Custom
  165.     result.gsub!(/\e[Nn][Mm]\[(.*?)\]/i) { @name_window.set($1) }
  166.     result.gsub!(/\e[Nn][Cc]\[([0123456789abcdef]+)\]/i) {
  167.         color_code = $1
  168.         r   = ("0x" + color_code.slice(0..1)).hex
  169.         g = ("0x" + color_code.slice(2..3)).hex
  170.         b  = ("0x" + color_code.slice(4..5)).hex
  171.         @name_window.font_color = Color.new(r, g, b)
  172.         @name_window.refresh
  173.     }
  174.     # Return the result
  175.     result
  176.   end
  177.   #--------------------------------------------------------------------------
  178.   # * Preconvert Control Characters
  179.   #    As a rule, replace only what will be changed into text strings before
  180.   #    starting actual drawing. The character "\" is replaced with the escape
  181.   #    character (\e).
  182.   #--------------------------------------------------------------------------
  183.   alias :pre_me_convert_escape_characters  :convert_escape_characters unless $@
  184.   def convert_escape_characters_spec(text)
  185.     # Create the result string
  186.     result = text.to_s.clone
  187.     # Add in shortcut codes
  188.     result = $game_message.apply_shortcuts(result)
  189.     # Run the normal conversion
  190.     result.gsub!(/\\/)            { "\e" }
  191.     result.gsub!(/\e\e/)          { "\\" }
  192.     result.gsub!(/\e[Ii][Gg][Nn][Rr]/i) { "\x10" }
  193.     result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i] }
  194.     result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i] }
  195.     result.gsub!(/\eN\[(\d+)\]/i) { actor_name($1.to_i) }
  196.     result.gsub!(/\eP\[(\d+)\]/i) { party_member_name($1.to_i) }
  197.     result.gsub!(/\eG/i)          { Vocab::currency_unit }
  198.     # Custom
  199.     result.gsub!(/\e[Nn][Mm]\[(.*?)\]/i) { @name_window.set($1) }
  200.     result.gsub!(/\e[Nn][Cc]\[([0123456789abcdef]+)\]/i) {
  201.         color_code = $1
  202.         r   = ("0x" + color_code.slice(0..1)).hex
  203.         g = ("0x" + color_code.slice(2..3)).hex
  204.         b  = ("0x" + color_code.slice(4..5)).hex
  205.         @name_window.font_color = Color.new(r, g, b)
  206.         @name_window.refresh
  207.     }
  208.     result.gsub!(/\e./)            { "" }
  209.     result.gsub!(/\e|/)            { "" }
  210.     result.gsub!(/\e>/)            { "" }
  211.     result.gsub!(/\e$/)            { "" }
  212.     result.gsub!(/\e!/)            { "" }
  213.     result.gsub!(/\e</)            { "" }
  214.     result.gsub!(/\e^/)            { "" }
  215.     # Return the result
  216.     result
  217.   end
  218. end
  219.  
  220. #==============================================================================
  221. # ** Window_Message
  222. #------------------------------------------------------------------------------
  223. #  This message window is used to display text.
  224. #==============================================================================
  225.  
  226. class Window_Message < Window_Base
  227.   #--------------------------------------------------------------------------
  228.   # * Public instance variables
  229.   #--------------------------------------------------------------------------
  230.   attr_reader     :face_sprite
  231.   #--------------------------------------------------------------------------
  232.   # * Object Initialization
  233.   #--------------------------------------------------------------------------
  234.   alias :pre_me_window_message_initialize  :initialize unless $@
  235.   def initialize
  236.     $game_system.init_shortcuts # init
  237.     pre_me_window_message_initialize # original function
  238.     # Custom Settings for this game
  239.     self.windowskin = Cache.system("MessageBox")
  240.     self.back_opacity = 255
  241.     contents.font.out_color = Color.new(0,0,0,16)
  242.   end
  243.   #--------------------------------------------------------------------------
  244.   # * Draw Face Graphic
  245.   #     enabled : Enabled flag. When false, draw semi-transparently.
  246.   #--------------------------------------------------------------------------
  247.   def draw_face(face_name, face_index, x, y, enabled = true)
  248.     @face_sprite.set_face(face_name, face_index)
  249.     @face_sprite.x = self.x + x + 5
  250.     @face_sprite.y = self.y + self.height + y - @face_sprite.height - 5
  251.     @face_sprite.visible = true
  252.   end
  253.   #--------------------------------------------------------------------------
  254.   # * Create All Windows
  255.   #--------------------------------------------------------------------------
  256.   alias :pre_me_create_all_windows  :create_all_windows unless $@
  257.   def create_all_windows
  258.     # Original function
  259.     pre_me_create_all_windows
  260.     # New 'face' sprite
  261.     @face_sprite = Sprite_Face.new(self.viewport)
  262.     @face_sprite.z = self.z + 100
  263.     # New 'name' window
  264.     @name_window = Window_MiniText.new(0, 0, "", "Forte")
  265.     @name_window.visible = false
  266.     @name_window.z = self.z + 200
  267.     @name_window.font_name = "Forte"
  268.     @name_window.refresh
  269.     @name_window.close
  270.   end
  271.   #--------------------------------------------------------------------------
  272.   # * Update All Windows
  273.   #--------------------------------------------------------------------------
  274.   alias :pre_me_update_all_windows  :update_all_windows unless $@
  275.   def update_all_windows
  276.     pre_me_update_all_windows # original function
  277.     @name_window.update
  278.   end
  279.   #--------------------------------------------------------------------------
  280.   # * Open Window and Wait for It to Fully Open
  281.   #--------------------------------------------------------------------------
  282.   alias :pre_me_open_and_wait :open_and_wait  unless $@
  283.   def open_and_wait
  284.     # Blank the name window
  285.     @name_window.set("")
  286.     # Get the final text
  287.     sizer_text = convert_escape_characters_spec($game_message.all_text)
  288.     draw_face($game_message.face_name, $game_message.face_index, 0, 0)
  289.     # Get the new height and width
  290.     nheight = 0
  291.     nwidth = 0
  292.     # Loop through text
  293.     sizer_text.each_line { |line|
  294.       nheight += line_height if !line.include?("\x10")
  295.       tw = contents.text_size(line).width
  296.       nwidth = tw if tw > nwidth
  297.     }
  298.     # Adjust the heights
  299.     self.width = nwidth + new_line_x + 24
  300.     self.height = nheight + 24
  301.     self.x = (Graphics.width - self.width) / 2
  302.     self.y = Graphics.height - self.height - line_height
  303.     update_placement
  304.     draw_face($game_message.face_name, $game_message.face_index, 0, 0)
  305.     pre_me_open_and_wait # original function
  306.   end
  307.   #--------------------------------------------------------------------------
  308.   # * Update Window Position
  309.   #--------------------------------------------------------------------------
  310.   alias :pre_me_update_placement  :update_placement unless $@
  311.   def update_placement
  312.     @position = $game_message.position
  313.     case @position
  314.     when 0
  315.       self.y = [@face_sprite.height - self.height, 24].max + line_height
  316.     when 1
  317.       self.y = (Graphics.height/2) - (self.height / 2) + (line_height / 2)
  318.     when 2
  319.       self.y = Graphics.height - self.height - line_height
  320.     end
  321.     @gold_window.y = y > 0 ? 0 : Graphics.height - @gold_window.height
  322.   end
  323.   #--------------------------------------------------------------------------
  324.   # * New Page
  325.   #--------------------------------------------------------------------------
  326.   alias :pre_me_new_page  :new_page  unless $@
  327.   def new_page(text, pos)
  328.     pre_me_new_page(text, pos) # original function
  329.     gx = self.x + 6 + new_line_x
  330.     if gx + @name_window.width > self.x + self.width
  331.       gx = self.x + self.width - @name_window.width
  332.     end
  333.     @name_window.move_window(gx, self.y + 11, true, false)
  334.     if !@name_window.text.empty?
  335.       @name_window.visible = true
  336.       @name_window.open
  337.     end
  338.   end
  339.   #--------------------------------------------------------------------------
  340.   # * Get New Line Position
  341.   #--------------------------------------------------------------------------
  342.   def new_line_x
  343.     $game_message.face_name.empty? ? 0 : @face_sprite.width
  344.   end
  345.   #--------------------------------------------------------------------------
  346.   # * Close Window and Wait for It to Fully Close
  347.   #--------------------------------------------------------------------------
  348.   alias :pre_me_close_and_wait  :close_and_wait unless $@
  349.   def close_and_wait
  350.     @name_window.close
  351.     @face_sprite.visible = false
  352.     pre_me_close_and_wait # original function
  353.   end
  354.   #--------------------------------------------------------------------------
  355.   # * Free All Windows
  356.   #--------------------------------------------------------------------------
  357.   alias :pre_me_dispose_all_windows :dispose_all_windows  unless $@
  358.   def dispose_all_windows
  359.     pre_me_dispose_all_windows # original function
  360.     @face_sprite.dispose
  361.     @name_window.dispose
  362.   end
  363. end
  364.  
  365. #==============================================================================
  366. # ** Window_MiniText
  367. #------------------------------------------------------------------------------
  368. #  This window displays a small bit of text and fits to it's size
  369. #  you can have it with no windowskin.
  370. #==============================================================================
  371.  
  372. class Window_MiniText < Window_Base
  373.   #--------------------------------------------------------------------------
  374.   # * Public instance variables
  375.   #--------------------------------------------------------------------------
  376.   attr_reader     :lh
  377.   attr_reader     :use_windowskin
  378.   attr_reader     :text
  379.   attr_accessor   :font_size
  380.   attr_accessor   :font_name
  381.   attr_accessor   :font_bold
  382.   attr_accessor   :font_italic
  383.   attr_accessor   :font_color
  384.   #--------------------------------------------------------------------------
  385.   # * Object Initialization
  386.   #--------------------------------------------------------------------------
  387.   def initialize(x, y, text, font_name = Font.default_name, font_size = 24, use_windowskin = false)
  388.     # Set all our local values
  389.     @lh = font_size
  390.     @font_size = font_size
  391.     @use_windowskin = use_windowskin
  392.     @text = text
  393.     @font_name = font_name
  394.     @font_bold = false
  395.     @font_italic = false
  396.     # Create the window
  397.     super(0, 0, window_width, fitting_height(1))
  398.     # Modify based on initial values
  399.     contents.font.size = @font_size
  400.     @font_color = normal_color
  401.     self.windowskin = Cache.system("WindowBlank") if use_windowskin
  402.   end
  403.   #--------------------------------------------------------------------------
  404.   # * Simple value functions
  405.   #--------------------------------------------------------------------------
  406.   def text; return @text; end
  407.   def text=(text); @text = text; end
  408.   def use_windowskin; return @use_windowskin; end
  409.   #--------------------------------------------------------------------------
  410.   # * Line Height
  411.   #--------------------------------------------------------------------------
  412.   def line_height
  413.     return 24 if @lh == nil
  414.     return @lh
  415.   end
  416.   #--------------------------------------------------------------------------
  417.   # * Get Window Width
  418.   #--------------------------------------------------------------------------
  419.   def window_width
  420.     # Create temp bitmap and get text sizes
  421.     bitmap = Bitmap.new(1,1)
  422.     bitmap.font = Font.new(@font_name, @font_size)
  423.     bitmap.font.bold = @font_bold
  424.     bitmap.font.italic = @font_italic
  425.     ts = bitmap.text_size(text).width + 26
  426.     bitmap.dispose
  427.     return ts
  428.   end
  429.   #--------------------------------------------------------------------------
  430.   # * Move Function
  431.   #--------------------------------------------------------------------------
  432.   def move_window(x, y, baseline = true, consider_windowskin = true)
  433.     self.x = x
  434.     self.y = y
  435.     self.y -= (self.height - 12) if baseline
  436.     if consider_windowskin
  437.       self.y -= 12 if baseline
  438.       self.y -= 24 if !baseline
  439.     end
  440.   end
  441.   #--------------------------------------------------------------------------
  442.   # * Set a new location and text, use -1, -1 for x/y to not move it
  443.   #--------------------------------------------------------------------------
  444.   def set(new_text)
  445.     # Modify x/y
  446.     self.text = new_text
  447.     self.width = window_width
  448.     self.height = fitting_height(1)
  449.     recreate
  450.     refresh
  451.   end
  452.   #--------------------------------------------------------------------------
  453.   # * Recreate the object
  454.   #--------------------------------------------------------------------------
  455.   def recreate
  456.     # Save position
  457.     tx = self.x
  458.     ty = self.y
  459.     # Remake the window and set texts
  460.     self.windowskin = Cache.system("Window") if use_windowskin
  461.     self.windowskin = Cache.system("WindowBlank") if !use_windowskin
  462.     update_padding
  463.     update_tone
  464.     create_contents
  465.     self.x = tx
  466.     self.y = ty
  467.     @opening = @closing = false
  468.   end
  469.   #--------------------------------------------------------------------------
  470.   # * Refresh
  471.   #--------------------------------------------------------------------------
  472.   def update
  473.     super
  474.   end
  475.   #--------------------------------------------------------------------------
  476.   # * Refresh
  477.   #--------------------------------------------------------------------------
  478.   def refresh
  479.     contents.clear
  480.     # Re-make our contents to fit
  481.     recreate
  482.     # Set our font
  483.     contents.font.size = @font_size
  484.     contents.font.bold = @font_bold
  485.     contents.font.italic = @font_italic
  486.     contents.font.name = @font_name
  487.     contents.font.color = @font_color
  488.     # Draw the text
  489.     draw_text(0, 0, window_width, line_height, text)
  490.   end
  491.   #--------------------------------------------------------------------------
  492.   # * Open Window
  493.   #--------------------------------------------------------------------------
  494.   def open
  495.     refresh
  496.     super
  497.   end
  498. end
RAW Paste Data