thiago_d_d

Advanced Message System v1.2

Jan 18th, 2013
2,336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 22.70 KB | None | 0 0
  1. #===============================================================================
  2. # http://thiagodd.blogspot.com.br/
  3. # TSDA Advanced Message System
  4. #   by thiago_d_d
  5. #   Version 1.2
  6. #
  7. # Any suggestions are apreciated, please make them here:
  8. # http://thiagodd.blogspot.com.br/
  9. #===============================================================================
  10. # Changelog of this version:
  11. #      - Fixed the bug with the y position of gold window.
  12. #      - Added an italic tag.
  13. #      - Added option to deactivate face window during game play. Now the face
  14. # window is activated by an switch, so if you don't want to use face outside
  15. # the window, deactivate that switch.
  16. #===============================================================================
  17. # Features
  18. #    - Created name window to show a name of your choice, only if you want.
  19. #    - The name window is customized, so you change color, etc.
  20. #    - Added support to hex colors.
  21. #    - Advanced letter by letter mode with advanced speed configuration.
  22. #    - You can choose if the player can skip the letter by letter mode
  23. #    - Added \Map tag, that shows the actual map name display
  24. #    - Added support to changes to the font name, size, and bold flag.
  25. #    - The face can be shown on a separate window.
  26. #    - Message window position and size can now be customized.
  27. #    - Message window opacity can be customized.
  28. #    - Possibility to play an sound when letter by letter is on.
  29. #    - Everything is customized, you can change everything with tags during the
  30. #      text draw.
  31. #===============================================================================
  32. # Changing window size and position
  33. #===============================================================================
  34. # To change height, width, and x position -> go to TSDA module and configure
  35. # To change message window y position, execute a script, there are 4 options
  36. #
  37. # $game_message.real_position = desired_y
  38. #     changes the position to the desired y position in the screen
  39. #
  40. # $game_message.real_position = "event_id"
  41. #     changes the position based on the specified event coordinates, so the
  42. #     window will not be above the event
  43. #
  44. # $game_message.real_position = "player"
  45. #     changes the position based on the player coordinates, so the
  46. #     window will not be above the player
  47. #
  48. # $game_message.real_position = nil
  49. #     the message window position will be normal, like if there is not
  50. #     this script
  51. #===============================================================================
  52. # Playing sound letter by letter
  53. #===============================================================================
  54. # Configure the switch that activates that mode on the TSDA module.
  55. # To determine the sound that will be player, execute the script:
  56. #     $game_message.letter_sound = "Sound name"
  57. # Sound should be on the SE folder
  58. #===============================================================================
  59. # OLD TAGS
  60. #    These tags already exists, only listing them to make things easy for you.
  61. #===============================================================================
  62. # \V[ID] - Shows the value of the variable with the specified ID.
  63. # \N[ID] - Shows the name of the hero with the specified ID.
  64. # \C[ID] - Changes the color to that color index of the windowskin.
  65. # \P[n] - Shows the name of the hero in the team that has the specified position
  66. # \$ - Shows gold window.
  67. # \I[index] - Show the icon with the specified index.
  68. # \G - Shows the gold name.
  69. # \{ - Increases font size.
  70. # \} - Decreases font size.
  71. # \. - waits for 0.25 seconds.
  72. # \| - waits for 1 second.
  73. # \\ - draws the \ character.
  74. # \< - Shows the next text imediately.
  75. # \> - Stops to show the text imediately
  76. # \! - Pauses the window, waiting for input.
  77. # \^ - Closes the window.
  78. #===============================================================================
  79. # NEWS TAGS
  80. #    You can still use old tags, they work normally.
  81. #===============================================================================
  82. # \c[#000000]
  83. #      changes text color by the specified hex color.
  84. #---------------------------------------
  85. # \M
  86. #      activates the letter by letter mode if it is activated, or deactivate is
  87. #      if it is activated.
  88. #---------------------------------------
  89. # \Q[speed]
  90. #      changes letter by letter by letter mode speed delay.
  91. #---------------------------------------
  92. # \W
  93. #      activates the jump letter by letter function if it is deactivate, or
  94. #      deactivates it if it is activated.
  95. #---------------------------------------
  96. # \Map
  97. #      shows the actual map name.
  98. #---------------------------------------
  99. # \S[Text]
  100. #      shows the name window above the message window, with the specified text.
  101. #      You can use other tags inside the text
  102. #---------------------------------------
  103. # \R
  104. #      closes the name window
  105. #---------------------------------------
  106. # \I[color]
  107. #      changes the name window text color(supports hex)
  108. #---------------------------------------
  109. # \F[font name]
  110. #      changes the message window font
  111. #---------------------------------------
  112. # \L
  113. #      sets the font to bold, if it's not bold, or set it to normal,
  114. #      if it is bold
  115. #---------------------------------------
  116. # \H[size]
  117. #      sets the font size
  118. #---------------------------------------
  119. # \X
  120. #      sets the font to italic if it's not italic, or set it to normal, if it
  121. #      is italic
  122. #---------------------------------------
  123. #===============================================================================
  124. #===============================================================================
  125. # module TSDA
  126. #===============================================================================
  127. module TSDA
  128.   #ID of the switch that activates letter by letter mode
  129.   AMS_LETTER_BY_LETTER_SWITCH = 1
  130.   #ID of the switch that indicates if the player can jump letter by letter mode
  131.   AMS_JUMP_LBL_SWITCH = 2
  132.   #ID of the variable that indicates letter by letter mode speed delay.
  133.   AMS_SPEED_DELAY_LBL_VARIABLE = 1
  134.   #The font used in the name box
  135.   # format:
  136.   # ["name",size,bold]
  137.   NAME_BOX_FONT = ["Trebuchet MS",Font.default_size,false]
  138.   #Number of lines of the message window, determines the window height
  139.   #Width of the window and the x pos of the window
  140.   MESSAGE_WINDOW_LINES = 3
  141.   MESSAGE_WINDOW_WIDTH = 400
  142.   MESSAGE_WINDOW_XPOS = 30
  143.   #Face window opacity
  144.   NAME_WINDOW_OPACITY = 160
  145.   NAME_WINDOW_BACK_OPACITY = 160
  146.   #Use face window?
  147.   USE_MESSAGE_FACE_WINDOW_SWITCH = 4
  148.   #Face window opacity
  149.   FACE_WINDOW_OPACITY = 160
  150.   FACE_WINDOW_BACK_OPACITY = 160
  151.   #id of the switch that determinates if the letter sound will be played
  152.   LETTER_SOUND_SWITCH = 3
  153. end
  154. #===============================================================================
  155. # Game_Message
  156. #===============================================================================
  157. class Game_Message
  158.   attr_accessor         :real_position
  159.   attr_accessor         :letter_sound
  160. end
  161. #===============================================================================
  162. # Window_Message
  163. #===============================================================================
  164. class Window_Message
  165.   include TSDA
  166.   #-----------------------------------------------------------------------------
  167.   # initialize - overwritten method
  168.   #-----------------------------------------------------------------------------
  169.   def initialize
  170.     super(MESSAGE_WINDOW_XPOS, 0, MESSAGE_WINDOW_WIDTH, window_height)
  171.     self.z = 200
  172.     self.openness = 0
  173.     create_all_windows
  174.     create_back_bitmap
  175.     create_back_sprite
  176.     clear_instance_variables
  177.   end
  178.   #-----------------------------------------------------------------------------
  179.   # visible_line_number - overwritten method
  180.   #-----------------------------------------------------------------------------
  181.   def visible_line_number
  182.     return MESSAGE_WINDOW_LINES
  183.   end
  184.   #-----------------------------------------------------------------------------
  185.   # new_line_x - aliased method
  186.   #-----------------------------------------------------------------------------
  187.   alias ams_new_line_x new_line_x
  188.   def new_line_x
  189.     $game_switches[USE_MESSAGE_FACE_WINDOW_SWITCH] ? 0 : ams_new_line_x
  190.   end
  191.   #-----------------------------------------------------------------------------
  192.   # obtain_escape_code - overwritten method
  193.   #-----------------------------------------------------------------------------
  194.   def obtain_escape_code(text)
  195.     text.slice!(/^[\$\.\|\^!><\{\}\\]|^[A-Z]{1}/i)
  196.   end
  197.   #-----------------------------------------------------------------------------
  198.   # new_page - overwritten method
  199.   #-----------------------------------------------------------------------------
  200.   def new_page(text, pos)
  201.     contents.clear
  202.     if $game_switches[USE_MESSAGE_FACE_WINDOW_SWITCH]
  203.       @face_window.set_face(self,$game_message.face_name, $game_message.face_index)
  204.     else
  205.       @face_window.visible = false
  206.       draw_face($game_message.face_name, $game_message.face_index, 0, 0)
  207.     end
  208.     reset_font_settings
  209.     pos[:x] = new_line_x
  210.     pos[:y] = 0
  211.     pos[:new_x] = new_line_x
  212.     pos[:height] = calc_line_height(text)
  213.     clear_flags
  214.   end
  215.   #-----------------------------------------------------------------------------
  216.   # obtain_real_escape_param - new method
  217.   #-----------------------------------------------------------------------------
  218.   def obtain_real_escape_param(text,hex=true)
  219.     if hex
  220.       text.slice!(/^\[[0123456789ABCDEF#]+\]/)[/[0123456789ABCDEF#]+/] rescue nil
  221.     else
  222.       text.slice!(/^\[[\w\s]+\]/)[/[\w\s]+/] rescue nil
  223.     end
  224.   end
  225.   #-----------------------------------------------------------------------------
  226.   # convert_escape_characters - overwritten method
  227.   #-----------------------------------------------------------------------------
  228.   def convert_escape_characters(text)
  229.     result = text.to_s.clone
  230.     result.gsub!(/\\/)            { "\e" }
  231.     result.gsub!(/\e\e/)          { "\\" }
  232.     result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i] }
  233.     result.gsub!(/\eN\[(\d+)\]/i) { actor_name($1.to_i) }
  234.     result.gsub!(/\eP\[(\d+)\]/i) { party_member_name($1.to_i) }
  235.     result.gsub!(/\eG/i)          { Vocab::currency_unit }
  236.     result.gsub!(/\eMap/i)        { $game_map.display_name }
  237.     result
  238.   end
  239.   #-----------------------------------------------------------------------------
  240.   # process_escape_character - aliased method
  241.   #-----------------------------------------------------------------------------
  242.   alias ams_process_escape_character process_escape_character
  243.   def process_escape_character(code, text, pos)
  244.     case code.upcase
  245.     when 'C'
  246.       param = obtain_real_escape_param(text)
  247.       if param[0,1] == "#" and param.size == 7
  248.         change_color(convert_to_hex(param[1,6]))
  249.       else
  250.         change_color(text_color(param.to_i))
  251.       end
  252.     when 'M'
  253.       sw = $game_switches[AMS_LETTER_BY_LETTER_SWITCH]
  254.       $game_switches[AMS_LETTER_BY_LETTER_SWITCH] = !sw
  255.       @letter_by_letter_mode = $game_switches[AMS_LETTER_BY_LETTER_SWITCH]
  256.     when 'Q'
  257.       param = obtain_escape_param(text)
  258.       $game_variables[AMS_SPEED_DELAY_LBL_VARIABLE] = param
  259.       @lbl_max = $game_variables[AMS_SPEED_DELAY_LBL_VARIABLE]
  260.     when 'W'
  261.       sw = $game_switches[AMS_JUMP_LBL_SWITCH]
  262.       $game_switches[AMS_JUMP_LBL_SWITCH] = !sw
  263.       @can_jump = $game_switches[AMS_JUMP_LBL_SWITCH]
  264.     when 'S'
  265.       param = obtain_real_escape_param(text,false)
  266.       @message_name_window.set_text(param,self)
  267.     when 'R'
  268.       @message_name_window.finish
  269.     when 'I'
  270.       param = obtain_real_escape_param(text)
  271.       if param[0,1] == "#" and param.size == 7
  272.         color = convert_to_hex(param[1,6])
  273.       else
  274.         color = text_color(param.to_i)
  275.       end
  276.       @message_name_window.font_color = color
  277.       @message_name_window.refresh
  278.     when 'F'
  279.       param = obtain_real_escape_param(text,false)
  280.       self.contents.font.name = param
  281.     when 'L'
  282.       self.contents.font.bold = !self.contents.font.bold
  283.     when 'H'
  284.       param = obtain_real_escape_param(text).to_i
  285.       self.contents.font.size = param
  286.     when 'X'
  287.       self.contents.font.italic = !self.contents.font.italic
  288.     else
  289.       ams_process_escape_character(code, text, pos)
  290.     end
  291.   end
  292.   #-----------------------------------------------------------------------------
  293.   # convert_to_hex - new method
  294.   #-----------------------------------------------------------------------------
  295.   def convert_to_hex(text)
  296.     return nil if text.size != 6
  297.     rgb = [0,0,0]
  298.     for index in 0...6
  299.       letter = text.slice!(/./m)
  300.       hex = hex_value(letter)
  301.       if index % 2 == 0
  302.         rgb[index / 2] += hex * 16
  303.       else
  304.         rgb[index / 2] += hex
  305.       end
  306.     end
  307.     return Color.new(rgb[0],rgb[1],rgb[2])
  308.   end
  309.   #-----------------------------------------------------------------------------
  310.   # hex_value - new method
  311.   #-----------------------------------------------------------------------------
  312.   def hex_value(text)
  313.     case text
  314.     when "0"
  315.       return 0
  316.     when "1"
  317.       return 1
  318.     when "2"
  319.       return 2
  320.     when "3"
  321.       return 3
  322.     when "4"
  323.       return 4
  324.     when "5"
  325.       return 5
  326.     when "6"
  327.       return 6
  328.     when "7"
  329.       return 7
  330.     when "8"
  331.       return 8
  332.     when "9"
  333.       return 9
  334.     when "A"
  335.       return 10
  336.     when "B"
  337.       return 11
  338.     when "C"
  339.       return 12
  340.     when "D"
  341.       return 13
  342.     when "E"
  343.       return 14
  344.     when "F"
  345.       return 15
  346.     end
  347.   end
  348.   #-----------------------------------------------------------------------------
  349.   # process_all_text - aliased method
  350.   #-----------------------------------------------------------------------------
  351.   alias ams_process_all_text process_all_text
  352.   def process_all_text
  353.     @letter_by_letter_mode = $game_switches[AMS_LETTER_BY_LETTER_SWITCH]
  354.     @lbl_max = $game_variables[AMS_SPEED_DELAY_LBL_VARIABLE]
  355.     @can_jump = $game_switches[AMS_JUMP_LBL_SWITCH]
  356.     ams_process_all_text
  357.   end
  358.   #-----------------------------------------------------------------------------
  359.   # wait_for_one_character - overwritten method
  360.   #-----------------------------------------------------------------------------
  361.   def wait_for_one_character
  362.     return if @show_fast
  363.     if @letter_by_letter_mode
  364.       if @can_jump
  365.         @show_fast = true if Input.press?(:B)
  366.         if Input.press?(:C)
  367.           if $game_variables[AMS_SPEED_DELAY_LBL_VARIABLE] > 1
  368.             @lbl_max = 1
  369.           else
  370.             @lbl_max = 0
  371.           end
  372.         else
  373.           @lbl_max = $game_variables[AMS_SPEED_DELAY_LBL_VARIABLE]
  374.         end
  375.       end
  376.       if @lbl_max > 0
  377.         play_sound = $game_switches[LETTER_SOUND_SWITCH]
  378.         Audio.se_play("Audio/SE/" + $game_message.letter_sound) if play_sound
  379.         for time in 0...@lbl_max
  380.           Fiber.yield
  381.         end
  382.       end
  383.     end
  384.   end
  385.   #-----------------------------------------------------------------------------
  386.   # update_placement - aliased method
  387.   #-----------------------------------------------------------------------------
  388.   alias ams_update_placement update_placement
  389.   def update_placement
  390.     @real_position = $game_message.real_position
  391.     @position = $game_message.position
  392.     if @real_position.nil?
  393.       self.y = @position * (Graphics.height - height) / 2
  394.     elsif @real_position.is_a?(Integer)
  395.       self.y = @real_position
  396.     elsif @real_position == "player"
  397.       y = $game_player.screen_y - 48
  398.       hei = calculate_total_component_height + 48
  399.       if y >= hei
  400.         self.y = $game_player.screen_y - height - 48
  401.       else
  402.         self.y = $game_player.screen_y
  403.       end
  404.     else
  405.       param = @real_position.to_i
  406.       event = $game_map.events[param]
  407.       y = event.screen_y - 48
  408.       hei = calculate_total_component_height + 48
  409.       if y >= hei
  410.         self.y = event.screen_y - height - 48
  411.       else
  412.         self.y = event.screen_y
  413.       end
  414.     end
  415.     @gold_window.y = ((self.y > @gold_window.height) ?
  416.       0 : Graphics.height - @gold_window.height)
  417.   end
  418.   #-----------------------------------------------------------------------------
  419.   # calculate_total_component_height - new method
  420.   #-----------------------------------------------------------------------------
  421.   def calculate_total_component_height
  422.     h = height + calculate_extra_component_height
  423.     h
  424.   end
  425.   #-----------------------------------------------------------------------------
  426.   # calculate_extra_component_height - new method
  427.   #-----------------------------------------------------------------------------
  428.   def calculate_extra_component_height
  429.     h = 0
  430.     if $game_switches[USE_MESSAGE_FACE_WINDOW_SWITCH] and !$game_message.face_name.empty?
  431.       h = 120
  432.     elsif @message_name_window.visible
  433.       h = @message_name_window.height
  434.     end
  435.     h
  436.   end
  437.   #-----------------------------------------------------------------------------
  438.   # settings_changed? - aliased method
  439.   #-----------------------------------------------------------------------------
  440.   alias ams_settings_changed? settings_changed?
  441.   def settings_changed?
  442.     return ams_settings_changed? ||
  443.       @real_position != $game_message.real_position
  444.   end
  445.   #-----------------------------------------------------------------------------
  446.   # create_all_windows - aliased method
  447.   #-----------------------------------------------------------------------------
  448.   alias ams_create_all_windows create_all_windows
  449.   def create_all_windows
  450.     ams_create_all_windows
  451.     @message_name_window = Window_MessageName.new
  452.     @face_window = Window_MessageFace.new
  453.   end
  454.   #-----------------------------------------------------------------------------
  455.   # dispose_all_windows - aliased method
  456.   #-----------------------------------------------------------------------------
  457.   alias ams_dispose_all_windows dispose_all_windows
  458.   def dispose_all_windows
  459.     ams_dispose_all_windows
  460.     @message_name_window.dispose
  461.     @face_window.dispose
  462.   end
  463.   #-----------------------------------------------------------------------------
  464.   # update_all_windows - aliased method
  465.   #-----------------------------------------------------------------------------
  466.   alias ams_update_all_windows update_all_windows
  467.   def update_all_windows
  468.     ams_update_all_windows
  469.     @message_name_window.update
  470.     @face_window.update
  471.   end
  472.   #-----------------------------------------------------------------------------
  473.   # close_and_wait - aliased method
  474.   #-----------------------------------------------------------------------------
  475.   alias ams_close_and_wait close_and_wait
  476.   def close_and_wait
  477.     @message_name_window.finish
  478.     @face_window.finish
  479.     ams_close_and_wait
  480.   end
  481. end
  482. #===============================================================================
  483. # Window_MessageName - new class
  484. #===============================================================================
  485. class Window_MessageName < Window_Base
  486.   include TSDA
  487.   #-----------------------------------------------------------------------------
  488.   attr_accessor          :font_color
  489.   #-----------------------------------------------------------------------------
  490.   # initialize - new method
  491.   #-----------------------------------------------------------------------------
  492.   def initialize
  493.     super(0,0,10,10)
  494.     self.visible = false
  495.     self.openness = 0
  496.     self.back_opacity = NAME_WINDOW_BACK_OPACITY
  497.     self.opacity = NAME_WINDOW_OPACITY
  498.     @font_color = text_color(0)
  499.   end
  500.   #-----------------------------------------------------------------------------
  501.   # set_text - new method
  502.   #-----------------------------------------------------------------------------
  503.   def set_text(text,window)
  504.     @text = text
  505.     bitmap = Bitmap.new(10,10)
  506.     bitmap.font.name = NAME_BOX_FONT[0]
  507.     bitmap.font.size = NAME_BOX_FONT[1]
  508.     bitmap.font.bold = NAME_BOX_FONT[2]
  509.     size = bitmap.text_size(text)
  510.     self.width = size.width + 24
  511.     self.height = size.height + 24
  512.     self.contents = Bitmap.new(size.width,size.height)
  513.     contents.font.name = NAME_BOX_FONT[0]
  514.     contents.font.size = NAME_BOX_FONT[1]
  515.     contents.font.bold = NAME_BOX_FONT[2]
  516.     h = window.calculate_extra_component_height
  517.     self.y = window.y > h ? window.y - height : window.y + window.height
  518.     self.x = ($game_message.face_name.empty? or !$game_switches[USE_MESSAGE_FACE_WINDOW_SWITCH]) ?
  519.       window.x : window.x + 120
  520.     self.visible = true
  521.     refresh
  522.     open
  523.     Fiber.yield until open?
  524.   end
  525.   #-----------------------------------------------------------------------------
  526.   # refresh - new method
  527.   #-----------------------------------------------------------------------------
  528.   def refresh
  529.     contents.clear
  530.     contents.font.color = @font_color
  531.     contents.draw_text(0,0,width - 24,height - 24,@text)
  532.   end
  533.   #-----------------------------------------------------------------------------
  534.   # finish - new method
  535.   #-----------------------------------------------------------------------------
  536.   def finish
  537.     close
  538.     until close?
  539.       update
  540.       Fiber.yield
  541.     end
  542.     self.openness = 0
  543.     self.visible = false
  544.   end
  545. end
  546. #===============================================================================
  547. # Window_MessageFace - new class
  548. #===============================================================================
  549. class Window_MessageFace < Window_Base
  550.   include TSDA
  551.   #---------------------------------------------------------------------------
  552.   # initialize - new method
  553.   #---------------------------------------------------------------------------
  554.   def initialize
  555.     super(0,0,120,120)
  556.     self.visible = false
  557.     self.openness = 0
  558.     self.back_opacity = FACE_WINDOW_BACK_OPACITY
  559.     self.opacity = FACE_WINDOW_OPACITY
  560.   end
  561.   #---------------------------------------------------------------------------
  562.   # set_face - new method
  563.   #---------------------------------------------------------------------------
  564.   def set_face(window,face_name,face_index)
  565.     self.y = window.y > 120 ? window.y - height : window.y + window.height
  566.     self.x = window.x
  567.     self.visible = true
  568.     refresh
  569.     open
  570.     Fiber.yield until open?
  571.   end
  572.   #---------------------------------------------------------------------------
  573.   # refresh - new method
  574.   #---------------------------------------------------------------------------
  575.   def refresh
  576.     contents.clear
  577.     draw_face($game_message.face_name, $game_message.face_index, 0, 0)
  578.   end
  579.   #---------------------------------------------------------------------------
  580.   # finish - new method
  581.   #---------------------------------------------------------------------------
  582.   def finish
  583.     close
  584.     until close?
  585.       update
  586.       Fiber.yield
  587.     end
  588.     self.openness = 0
  589.     self.visible = false
  590.   end
  591. end
Advertisement
Add Comment
Please, Sign In to add comment