Guest User

MBS - Event Text Display

a guest
Jul 26th, 2016
16
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #==============================================================================
  2. # MBS - Event Text Display
  3. #------------------------------------------------------------------------------
  4. # por Masked
  5. #==============================================================================
  6. #==============================================================================
  7. # Instruções
  8. #------------------------------------------------------------------------------
  9. # Para mostrar o texto acima do personagem/event escreva nas notas/comentário
  10. # dele:
  11. # <TEXT "texto" #hex above/below>
  12. # Sendo o texto a mostrar entre aspas, o #hex a cor do texto em hexadecimal e
  13. # above para o texto acima do personagem e below para abaixo,
  14. # por exemplo:
  15. # <TEXT "maçã" #f00 above> = 'maçã' escrito em vermelho acima do personagem
  16. # Os dois últimos valores são opcionais
  17. # Você pode usar as tags \V, \N, \P e \G igual às mensagens no texto.
  18. #
  19. # Para adicionar/deletar textos por script call, use a propriedade .texts
  20. # do character;
  21. # Deleta o texto nº X:
  22. # character.texts.delete_at(x)
  23. # Insere o texto "texto" com a cor "cor" acima do personagem:
  24. # character.texts.insert(x, ["texto","cor",:above])
  25. # Muda o texto número X para "texto" com a cor "cor" abaixo do personagem:
  26. # character.texts[x] = ["texte","cor",:below]
  27. #
  28. # O character pode ser o jogador ou algum evento:
  29. # $game_player.texts.delete_at(x)
  30. # $game_map.events[id].texts.insert(x, ["texto","cor",:above])
  31. #
  32. # Também pode ser algum dos seguidores do jogador:
  33. # $game_player.followers[n].texts.delete_at(x)
  34. #==============================================================================
  35. #==============================================================================
  36. # ** MBS
  37. #==============================================================================
  38. module MBS
  39.   #============================================================================
  40.   # ** EventText
  41.   #============================================================================
  42.   module EventText
  43.     #==========================================================================
  44.     # * Configurações
  45.     #==========================================================================
  46.     # Tamanho da fonte do texto
  47.     FONT_SIZE = 18
  48.    
  49.     # Caso queira usar os textos das notas dos seguidores deixe como true,
  50.     # se não mude para false
  51.     USE_FOLLOWER_NOTE = true
  52.    
  53.     # Caso queira que o texto mude quando o evento mudar de página deixe como
  54.     # true, se não mude para false
  55.     RESET_ON_PAGE_SETUP = true
  56.     #==========================================================================
  57.     # * Fim das configurações
  58.     #==========================================================================
  59.     REGEX = /<TEXT\s*"(.+)"\s*(#[a-f0-9]{3,})?\s*(above|below)?\s*>/i
  60.   end
  61. end
  62. #==============================================================================
  63. # ** Game_Character
  64. #------------------------------------------------------------------------------
  65. # Modificações para guardar os textos pra cada character
  66. #==============================================================================
  67. class Game_Character < Game_CharacterBase
  68.   #--------------------------------------------------------------------------
  69.   # * Definição dos atributos
  70.   #--------------------------------------------------------------------------
  71.   attr_reader :texts
  72.   #--------------------------------------------------------------------------
  73.   # * Alias
  74.   #--------------------------------------------------------------------------
  75.   alias mbseventtext_initpublicmems init_public_members
  76.   #--------------------------------------------------------------------------
  77.   # * Inicialização de variáveis públicas
  78.   #--------------------------------------------------------------------------
  79.   def init_public_members(*a, &b)
  80.     mbseventtext_initpublicmems(*a, &b)
  81.     @texts = []
  82.   end
  83. end
  84. #==============================================================================
  85. # ** Sprite_Character
  86. #------------------------------------------------------------------------------
  87. # Modificações para mostrar o texto no personagem
  88. #==============================================================================
  89. class Sprite_Character < Sprite_Base
  90.   #--------------------------------------------------------------------------
  91.   # * Alias
  92.   #--------------------------------------------------------------------------
  93.   alias mbseventtext_updatebitmap   update_bitmap
  94.   alias mbseventtext_updateposition update_position
  95.   alias mbseventtext_dispose        dispose
  96.   #--------------------------------------------------------------------------
  97.   # * Atualização do bitmap de origem
  98.   #--------------------------------------------------------------------------
  99.   def update_bitmap(*a,&b)
  100.     mbseventtext_updatebitmap(*a,&b)
  101.     update_texts if texts_changed?
  102.     @spr_txt.update if @spr_txt
  103.   end
  104.   #--------------------------------------------------------------------------
  105.   # * Atualização da posição do sprite
  106.   #--------------------------------------------------------------------------
  107.   def update_position
  108.     mbseventtext_updateposition
  109.     @spr_txt.x = self.x if @spr_txt
  110.     @spr_txt.y = self.y if @spr_txt
  111.   end
  112.   #--------------------------------------------------------------------------
  113.   # * Verificação de quando os textos mudaram
  114.   #--------------------------------------------------------------------------
  115.   def texts_changed?
  116.     @texts != @character.texts
  117.   end
  118.   #--------------------------------------------------------------------------
  119.   # * Aquisição de uma cor através de um hexadecimal
  120.   #--------------------------------------------------------------------------
  121.   def parse_color(hex)
  122.     r = g = b = a = 255
  123.     case hex.to_s.size
  124.     when 3
  125.       r, g, b = *hex.split('')
  126.       r = (r*2).to_i(16)
  127.       g = (g*2).to_i(16)
  128.       b = (b*2).to_i(16)
  129.     when 4
  130.       r, g, b, a = *hex.split('')
  131.       r = (r*2).to_i(16)
  132.       g = (g*2).to_i(16)
  133.       b = (b*2).to_i(16)
  134.       a = (a*2).to_i(16)
  135.     when 6
  136.       r = hex[0..1].to_i(16)
  137.       g = hex[2..3].to_i(16)
  138.       b = hex[4..5].to_i(16)
  139.     when 8
  140.       r = hex[0..1].to_i(16)
  141.       g = hex[2..3].to_i(16)
  142.       b = hex[4..5].to_i(16)
  143.       a = hex[6..7].to_i(16)
  144.     end
  145.     return Color.new(r,g,b,a)
  146.   end
  147.   private :parse_color
  148.   #--------------------------------------------------------------------------
  149.   # * Aquisição dos textos abaixo do char
  150.   #--------------------------------------------------------------------------
  151.   def below_texts
  152.     @texts.select {|txt| txt[2] == :below}
  153.   end
  154.   #--------------------------------------------------------------------------
  155.   # * Aquisição dos textos acima do char
  156.   #--------------------------------------------------------------------------
  157.   def above_texts
  158.     @texts.select {|txt| txt[2] == :above}
  159.   end
  160.   #--------------------------------------------------------------------------
  161.   # * Atualização dos textos
  162.   #--------------------------------------------------------------------------
  163.   def update_texts
  164.     @spr_txt ||= Sprite.new
  165.     @texts = @character.texts.dup
  166.    
  167.     bigger = (@texts.sort {|a, b| a[0].size <=> b[0].size}.last || [])[0]
  168.     bitmap.font.size &&= MBS::EventText::FONT_SIZE
  169.     w = bitmap.text_size(bigger).width+8
  170.     h = bitmap.text_size(bigger).height+2
  171.     @spr_txt.bitmap = Bitmap.new([@cw, w].max,@ch+h*@texts.size)
  172.     @spr_txt.bitmap.font.size &&= MBS::EventText::FONT_SIZE
  173.     @spr_txt.ox = @spr_txt.width/2
  174.     @spr_txt.oy = self.oy + above_texts.size*h
  175.    
  176.     above_texts.compact.each_with_index do |text, i|
  177.       @spr_txt.bitmap.font.color = parse_color(text[1])
  178.       @spr_txt.bitmap.draw_text(0,i*h,w,h,convert_escape_characters(text[0]),1)
  179.     end
  180.    
  181.     below_texts.compact.each_with_index do |text, i|
  182.       @spr_txt.bitmap.font.color = parse_color(text[1])
  183.       @spr_txt.bitmap.draw_text(0,above_texts.size*h + @ch + i*h,w,h,convert_escape_characters(text[0]),1)
  184.     end
  185.   end
  186.   #--------------------------------------------------------------------------
  187.   # * Conversão dos caracteres de controle
  188.   #--------------------------------------------------------------------------
  189.   def convert_escape_characters(text)
  190.     result = text.to_s.clone
  191.     result.gsub!(/\\/)            { "\e" }
  192.     result.gsub!(/\e\e/)          { "\\" }
  193.     result.gsub!(/%var(\d+)%/i)   { $game_variables[$1.to_i] }
  194.     result.gsub!(/%actor(\d+)%/i) { $data_actors[$1.to_i].name }
  195.     result.gsub!(/%party(\d+)%/i) { $game_party.members[$1.to_i].name }
  196.     result.gsub!(/%currency%/i)   { Vocab::currency_unit }
  197.     result.gsub!(/%name%/i)       do
  198.       character.is_a?(Game_Player) || character.is_a?(Game_Follower) ? character.actor.name : character.event.name
  199.     end
  200.     result.gsub!(/%level%/i)      do
  201.      
  202.     end
  203.     result
  204.   end
  205.   #--------------------------------------------------------------------------
  206.   # * Disposição
  207.   #--------------------------------------------------------------------------
  208.   def dispose
  209.     @spr_txt.dispose if @spr_txt
  210.     mbseventtext_dispose
  211.   end
  212. end
  213. #==============================================================================
  214. # ** Game_Event
  215. #------------------------------------------------------------------------------
  216. # Aqui são feitas as modificações para pegar os textos dos comentários do
  217. # evento
  218. #==============================================================================
  219. class Game_Event < Game_Character
  220.   #--------------------------------------------------------------------------
  221.   # * Definição dos atributos
  222.   #--------------------------------------------------------------------------
  223.   attr_reader :event
  224.   #--------------------------------------------------------------------------
  225.   # * Alias
  226.   #--------------------------------------------------------------------------
  227.   alias mbseventtext_setuppage setup_page_settings
  228.   #--------------------------------------------------------------------------
  229.   # * Aplicação das configurações da página
  230.   #--------------------------------------------------------------------------
  231.   def setup_page_settings(*a, &b)
  232.     mbseventtext_setuppage(*a, &b)
  233.     @texts.clear if MBS::EventText::RESET_ON_PAGE_SETUP
  234.     @list.select {|cmd| cmd.code == 108 || cmd.code == 408}.each do |comment|
  235.       comment.parameters[0].scan(MBS::EventText::REGEX) do
  236.         color = ($2 || '#fff')
  237.         pos = ($3 || 'above').to_sym
  238.         @texts << [$1, color[/[a-f0-9]+/], pos]
  239.       end
  240.     end
  241.   end
  242. end
  243. #==============================================================================
  244. # ** Game_Player
  245. #------------------------------------------------------------------------------
  246. # Aqui são feitas as alterações para pegar os textos do jogador direto das
  247. # notas
  248. #==============================================================================
  249. class Game_Player < Game_Character
  250.   #--------------------------------------------------------------------------
  251.   # * Alias
  252.   #--------------------------------------------------------------------------
  253.   alias mbseventtext_refresh refresh
  254.   #--------------------------------------------------------------------------
  255.   # * Renovação
  256.   #--------------------------------------------------------------------------
  257.   def refresh(*a, &b)
  258.     mbseventtext_refresh
  259.     refresh_texts
  260.   end
  261.   #--------------------------------------------------------------------------
  262.   # * Renovação dos textos
  263.   #--------------------------------------------------------------------------
  264.   def refresh_texts
  265.     @texts.clear
  266.     return unless actor
  267.     $data_actors[actor.id].note.scan(MBS::EventText::REGEX) do
  268.       color = ($2 || '#fff')
  269.       pos = ($3 || 'above').to_sym
  270.       @texts << [$1, color[/[a-f0-9]+/], pos]
  271.     end
  272.   end
  273. end
  274. #==============================================================================
  275. # ** Game_Follower
  276. #------------------------------------------------------------------------------
  277. # Aqui são feitas as alterações para pegar os textos dos seguidores direto
  278. # das notas
  279. #==============================================================================
  280. if MBS::EventText::USE_FOLLOWER_NOTE
  281.   class Game_Follower < Game_Character
  282.     #--------------------------------------------------------------------------
  283.     # * Alias
  284.     #--------------------------------------------------------------------------
  285.     alias mbseventtext_refresh refresh
  286.     #--------------------------------------------------------------------------
  287.     # * Renovação
  288.     #--------------------------------------------------------------------------
  289.     def refresh(*a, &b)
  290.       mbseventtext_refresh
  291.       refresh_texts
  292.     end
  293.     #--------------------------------------------------------------------------
  294.     # * Renovação dos textos
  295.     #--------------------------------------------------------------------------
  296.     def refresh_texts
  297.       @texts.clear
  298.       return unless actor
  299.       $data_actors[actor.id].note.scan(MBS::EventText::REGEX) do
  300.         color = ($2 || '#fff')
  301.         pos = ($3 || 'above').to_sym
  302.         @texts << [$1, color[/[a-f0-9]+/], pos]
  303.       end
  304.     end
  305.   end
  306. end
RAW Paste Data