TheoAllen

Theo - Message Ballon (ENG)

Sep 6th, 2013
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 12.79 KB | None | 0 0
  1. # =============================================================================
  2. # TheoAllen - Message Ballon
  3. # Version : 1.0
  4. # Contact : www.rpgmakerid.com (or) http://theolized.blogspot.com
  5. # (English Documentation)
  6. # =============================================================================
  7. ($imported ||= {})[:Theo_MessageBallon] = true
  8. # =============================================================================
  9. # CHANGE LOGS:
  10. # -----------------------------------------------------------------------------
  11. # 2013.09.05 - Finished script
  12. # =============================================================================
  13. =begin
  14.  
  15.   ------------------------------------------------------------------------
  16.   Introdurction :
  17.   ------------------------------------------------------------------------
  18.   This script allow you to show the dialog inside ballon box right above
  19.   character
  20.  
  21.   ------------------------------------------------------------------------
  22.   How to use :
  23.   ------------------------------------------------------------------------
  24.   Put this script bellow material but above main
  25.   Put the image for tail pointer box in Graphics/system. And don't forget to
  26.   name it as "ballonpointer"
  27.  
  28.   Edit the configuration if necessary. And follow the script call rules
  29.  
  30.   ------------------------------------------------------------------------
  31.   Script Calls :
  32.   ------------------------------------------------------------------------
  33.   - message.ballon
  34.   Call this script to make message window shown as ballon message
  35.  
  36.   - message.normal
  37.   Call this script to make window back to normal
  38.  
  39.   - message.subject = number
  40.   Call this script to determine who is speak. Number is same as event ID.
  41.   If you put 0, then the player will be the speaker. If the event ID didn't
  42.   exist, then message will be back to usual message window.
  43.  
  44.   ------------------------------------------------------------------------
  45.   Terms of use :
  46.   ------------------------------------------------------------------------
  47.   Credit me, TheoAllen. You are free to edit this script by your own. As long
  48.   as you don't claim it yours. For commercial purpose, don't forget to give me
  49.   a free copy of the game.
  50.  
  51. =end
  52. # =============================================================================
  53. # Configuration :
  54. # =============================================================================
  55. module THEO
  56.   module Msg
  57.    
  58.     Buffer_Y    = 32
  59.   # Distance Y from sprite (default: 32)
  60.    
  61.     FontSize    = 22
  62.   # Font size for ballon mode
  63.  
  64.     BackColor1   = Color.new(100,100,100,150)
  65.   # Upper background color. Must be filled by :
  66.   # Color.new(red,green,blue,alpha)
  67.  
  68.     BackColor2   = Color.new(0,0,0,150)
  69.   # Lower background color. Must be filled by :
  70.   # Color.new(red,green,blue,alpha)
  71.  
  72.     BorderColor = Color.new(0,0,0,255)
  73.   # Border Color for ballon. Must be filled by :
  74.   # Color.new(red,green,blue,alpha)
  75.    
  76.     BorderSize = 2
  77.   # Border size
  78.  
  79.   end
  80. end
  81. # =============================================================================
  82. # End of configuration
  83. # =============================================================================
  84. class Bitmap
  85.   def border_fill(color,size = 1)
  86.     fill_rect(0,0,width,size,color)
  87.     fill_rect(0,0,size,height,color)
  88.     fill_rect(width-size,0,size,height,color)
  89.     fill_rect(0,height-size,width,size,color)
  90.   end
  91. end
  92.  
  93. class Game_Interpreter
  94.  
  95.   def message
  96.     $game_message
  97.   end
  98.  
  99. end
  100.  
  101. class Game_Message
  102.   attr_accessor :ballon_refresh
  103.   attr_accessor :subject
  104.   attr_reader :type
  105.  
  106.   alias theo_msgballon_init initialize
  107.   def initialize
  108.     theo_msgballon_init
  109.     @subject = 0
  110.     normal
  111.   end
  112.  
  113.   def ballon
  114.     @type = :ballon
  115.     @ballon_refresh = true
  116.   end
  117.  
  118.   def normal
  119.     @type = :normal
  120.     @ballon_refresh = true
  121.   end
  122.  
  123.   def ballon?
  124.     return false unless SceneManager.scene_is?(Scene_Map)
  125.     @type == :ballon && @subject > -1 && !get_char.nil?
  126.   end
  127.   # -------------------------------------------------------
  128.   # Destruct escape character for length calculation
  129.   # -------------------------------------------------------
  130.   def convert_escape_characters(text)
  131.     result = text.to_s.clone
  132.     result.gsub!(/\\/)            { "\e" }
  133.     result.gsub!(/\e\e/)          { "\\" }
  134.     result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i] }
  135.     result.gsub!(/\eN\[(\d+)\]/i) { actor_name($1.to_i) }
  136.     result.gsub!(/\eP\[(\d+)\]/i) { party_member_name($1.to_i) }
  137.     result.gsub!(/\eG/i)          { Vocab::currency_unit }
  138.     result.gsub!(/\eC\[(\d+)\]/i) { "" }        # Destroy change color code
  139.     result.gsub!(/\eI\[(\d+)\]/i) { "      " }  # Destroy draw icon
  140.     result
  141.   end
  142.  
  143.   def actor_name(n)
  144.     actor = n >= 1 ? $game_actors[n] : nil
  145.     actor ? actor.name : ""
  146.   end
  147.  
  148.   def party_member_name(n)
  149.     actor = n >= 1 ? $game_party.members[n - 1] : nil
  150.     actor ? actor.name : ""
  151.   end
  152.  
  153.   def longest_text
  154.     test = Bitmap.new(1,1)
  155.     test.font.size = THEO::Msg::FontSize
  156.     txts = []
  157.     @texts.each do |txt|
  158.       txts.push(convert_escape_characters(txt))
  159.     end
  160.     longest = txts.sort do |a,b|
  161.       test.text_size(b).width <=> test.text_size(a).width
  162.     end[0]
  163.     result = test.text_size(longest).width
  164.     test.dispose
  165.     return result
  166.   end
  167.  
  168.   def get_char
  169.     if @subject == 0
  170.       player = $game_player
  171.       return Point.new(player.screen_x, player.screen_y)
  172.     else
  173.       event = $game_map.events[@subject]
  174.       return nil unless event
  175.       return Point.new(event.screen_x, event.screen_y)
  176.     end
  177.   end
  178.  
  179.   def total_text
  180.     @texts.size
  181.   end
  182.  
  183. end
  184.  
  185. class Point
  186.   attr_accessor :x
  187.   attr_accessor :y
  188.  
  189.   def initialize(x,y)
  190.     @x, @y = x, y
  191.   end
  192. end
  193.  
  194. class BallonPointer < Sprite
  195.   attr_reader :window
  196.   attr_reader :pos
  197.  
  198.   def initialize(viewport,window)
  199.     super(viewport)
  200.     @window = window
  201.     @pos = :upper
  202.     self.bitmap = Cache.system("ballonpointer")
  203.     to_center
  204.     update
  205.   end
  206.  
  207.   def pos=(pos)
  208.     @pos = pos
  209.     update_placement
  210.   end
  211.  
  212.   def to_center
  213.     self.ox = width/2
  214.     self.oy = height
  215.   end
  216.  
  217.   def update
  218.     super
  219.     if window.ballon?
  220.       self.visible = window.open?
  221.       update_placement
  222.     else
  223.       self.visible = false
  224.     end
  225.   end
  226.  
  227.   def update_placement
  228.     self.x = window.char.x
  229.     if pos == :upper
  230.       self.y = window.char.y - THEO::Msg::Buffer_Y - 2
  231.       self.angle = 0
  232.     else
  233.       self.angle = 180
  234.       self.y = window.char.y
  235.     end
  236.   end
  237.    
  238. end
  239.  
  240. class Window_Message < Window_Base
  241.  
  242.   alias theo_msgballon_init initialize
  243.   def initialize
  244.     theo_msgballon_init
  245.     @pointer = BallonPointer.new(viewport,self)
  246.   end
  247.  
  248.   alias theo_msgballon_clear_instances clear_instance_variables
  249.   def clear_instance_variables
  250.     theo_msgballon_clear_instances
  251.     @ballon = false
  252.     @subject = -1
  253.   end
  254.  
  255.   alias theo_msgballon_settings_changed? settings_changed?
  256.   def settings_changed?
  257.     theo_msgballon_settings_changed? || ballon_setting_changed?
  258.   end
  259.  
  260.   def ballon_setting_changed?
  261.     @ballon != ballon? || @subject != $game_message.subject
  262.   end
  263.  
  264.   def setup_window
  265.     if ballon?
  266.       setup_ballon
  267.     else
  268.       setup_normal
  269.     end
  270.   end
  271.  
  272.   def setup_normal
  273.     h = fitting_height(visible_line_number)
  274.     w = Graphics.width
  275.     self.width = w
  276.     self.height = h
  277.   end
  278.  
  279.   def setup_ballon
  280.     h = fitting_height($game_message.total_text) + 2
  281.     w = $game_message.longest_text + 10 + (standard_padding * 2)
  282.     self.width = w
  283.     self.height = h
  284.     self.opacity = 0
  285.   end
  286.  
  287.   def ballon?
  288.     $game_message.ballon?
  289.   end
  290.  
  291.   alias theo_msgballon_update_bg update_background
  292.   def update_background
  293.     recreate_choice if @ballon != ballon?
  294.     @ballon = ballon?
  295.     @subject = $game_message.subject
  296.     theo_msgballon_update_bg
  297.   end
  298.  
  299.   alias theo_msgballon_normal_placement update_placement
  300.   def update_placement
  301.     setup_window
  302.     theo_msgballon_normal_placement
  303.     if ballon?
  304.       update_ballon_placement
  305.     else
  306.       self.x = 0
  307.     end
  308.   end
  309.  
  310.   def char
  311.     $game_message.get_char
  312.   end
  313.  
  314.   def update_ballon_placement
  315.     xpos = [[char.x - width/2, - standard_padding].max,
  316.       Graphics.width - width + standard_padding].min
  317.     ypos = char.y - height - THEO::Msg::Buffer_Y
  318.     @pointer.pos = :upper
  319.     if ypos < 0
  320.       ypos = char.y - 2
  321.       @pointer.pos = :lower
  322.     end
  323.     self.x = xpos
  324.     self.y = ypos
  325.   end
  326.  
  327.   alias theo_msgballon_new_page new_page
  328.   def new_page(text, pos)
  329.     if ballon?
  330.       return ballon_new_page(text, pos)
  331.     end
  332.     theo_msgballon_new_page(text, pos)
  333.   end
  334.  
  335.   def ballon_new_page(text, pos)
  336.     setup_ballon
  337.     update_ballon_placement
  338.     contents.clear
  339.     entire_fill
  340.     reset_font_settings
  341.     pos[:x] = new_line_x
  342.     pos[:y] = 0
  343.     pos[:new_x] = new_line_x
  344.     pos[:height] = calc_line_height(text)
  345.     clear_flags
  346.   end
  347.  
  348.   alias theo_ballon_new_line_x new_line_x
  349.   def new_line_x
  350.     if ballon?
  351.       return 4
  352.     else
  353.       theo_ballon_new_line_x
  354.     end
  355.   end
  356.  
  357.   alias theo_msgballon_update update
  358.   def update
  359.     theo_msgballon_update
  360.     @pointer.update
  361.     update_ballon_placement if ballon? && !@opening && !@closing
  362.     recreate_choice if $game_message.ballon_refresh
  363.   end
  364.  
  365.   def recreate_choice
  366.     @choice_window.dispose
  367.     @choice_window = Window_ChoiceList.new(self)
  368.     $game_message.ballon_refresh = false
  369.   end
  370.  
  371.   def entire_fill
  372.     contents.gradient_fill_rect(contents.rect,THEO::Msg::BackColor1,
  373.       THEO::Msg::BackColor2,true)
  374.     contents.border_fill(THEO::Msg::BorderColor,THEO::Msg::BorderSize)
  375.   end
  376.  
  377.   alias theo_msgballon_width= width=
  378.   def width=(width)
  379.     self.theo_msgballon_width = width
  380.     create_contents
  381.   end
  382.  
  383.   alias theo_msgballon_height= height=
  384.   def height=(height)
  385.     self.theo_msgballon_height = height
  386.     create_contents
  387.   end
  388.  
  389.   alias theo_msgballon_line_height line_height
  390.   def line_height
  391.     if ballon?
  392.       THEO::Msg::FontSize
  393.     else
  394.       theo_msgballon_line_height
  395.     end
  396.   end
  397.  
  398.   alias theo_msgballon_reset_font reset_font_settings
  399.   def reset_font_settings
  400.     theo_msgballon_reset_font
  401.     contents.font.size = THEO::Msg::FontSize if ballon?
  402.   end
  403.   # ----------------------------------------------------------------
  404.   # Overwrite input pause ~
  405.   # ----------------------------------------------------------------
  406.   def input_pause
  407.     self.pause = true unless ballon?
  408.     wait(10)
  409.     Fiber.yield until Input.trigger?(:B) || Input.trigger?(:C)
  410.     Input.update
  411.     self.pause = false unless ballon?
  412.   end
  413.  
  414. end
  415.  
  416. class Window_ChoiceList < Window_Command
  417.  
  418.   alias theo_msgballon_start start
  419.   def start
  420.     reset_window
  421.     theo_msgballon_start
  422.   end
  423.  
  424.   def reset_window
  425.     self.opacity = 255
  426.   end
  427.  
  428.   def setup_ballon
  429.     self.opacity = 0
  430.     entire_fill
  431.   end
  432.  
  433.   def entire_fill
  434.     dispose_background
  435.     @back_bitmap = Bitmap.new(contents.width,contents.height)
  436.     @back_sprite = Sprite.new(viewport)
  437.     @back_sprite.bitmap = @back_bitmap
  438.     @back_sprite.x = x + standard_padding
  439.     @back_sprite.y = y + standard_padding
  440.     bmp = @back_sprite.bitmap
  441.     bmp.gradient_fill_rect(contents.rect,THEO::Msg::BackColor1,
  442.       THEO::Msg::BackColor2,true)
  443.     bmp.border_fill(THEO::Msg::BorderColor,THEO::Msg::BorderSize)
  444.     @back_sprite.visible = false
  445.   end
  446.  
  447.   alias theo_msgballon_reset_font reset_font_settings
  448.   def reset_font_settings
  449.     theo_msgballon_reset_font
  450.     contents.font.size = THEO::Msg::FontSize if @message_window.ballon?
  451.   end
  452.  
  453.   alias theo_msgballon_draw_all draw_all_items
  454.   def draw_all_items
  455.     setup_ballon if @message_window.ballon?
  456.     theo_msgballon_draw_all
  457.   end
  458.  
  459.   alias theo_msgballon_update_placement update_placement
  460.   def update_placement
  461.     theo_msgballon_update_placement
  462.     if @message_window.ballon?
  463.       update_ballon_placement
  464.     end
  465.   end
  466.  
  467.   def update_ballon_placement
  468.     msg = @message_window
  469.     self.y = msg.y
  470.     self.x = msg.x + msg.width - standard_padding * 2
  471.     if (x + width) > Graphics.width
  472.       self.x = msg.x - width + standard_padding * 2
  473.     end
  474.   end
  475.  
  476.   alias theo_msgballon_line_height line_height
  477.   def line_height
  478.     if @message_window.ballon?
  479.       THEO::Msg::FontSize
  480.     else
  481.       theo_msgballon_line_height
  482.     end
  483.   end
  484.  
  485.   def dispose_background
  486.     @back_bitmap.dispose if @back_bitmap
  487.     @back_sprite.dispose if @back_sprite
  488.   end
  489.  
  490.   alias theo_msgballon_close close
  491.   def close
  492.     dispose_background
  493.     theo_msgballon_close
  494.   end
  495.  
  496.   alias theo_msgballon_update update
  497.   def update
  498.     theo_msgballon_update
  499.     @back_sprite.visible = open? if @back_sprite && !@back_sprite.disposed?
  500.   end
  501.  
  502. end
Advertisement
Add Comment
Please, Sign In to add comment