TheoAllen

Message Ballon fix

Dec 22nd, 2013
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 14.06 KB | None | 0 0
  1. # =============================================================================
  2. # TheoAllen - Message Ballon
  3. # Version : 1.1
  4. # Contact : www.rpgmakerid.com (or) http://theolized.blogspot.com
  5. # (This script documentation is written in informal indonesian language)
  6. # =============================================================================
  7. ($imported ||= {})[:Theo_MessageBallon] = true
  8. # =============================================================================
  9. # CHANGE LOGS:
  10. # -----------------------------------------------------------------------------
  11. # 2013.10.29 - Fixed unable to save bug
  12. # 2013.10.25 - Add <bm: id> as ballon setup
  13. #            - Unused escape char now do not count as window size
  14. # 2013.09.05 - Finished script
  15. # =============================================================================
  16. =begin
  17.  
  18.   ------------------------------------------------------------------------
  19.   Perkenalan :
  20.   ------------------------------------------------------------------------
  21.   Script ini ngebikin kamu nampilin dialog dalam ballon diatas karakter
  22.  
  23.   ------------------------------------------------------------------------
  24.   Cara penggunaan :
  25.   ------------------------------------------------------------------------
  26.   Pasang dibawah material namun diatas main.
  27.   Masukkan gambar untuk ballon pointer di Graphics/system. Dan jangan lupa
  28.   dinamain "ballonpointer".
  29.  
  30.   Edit konfigurasinya dan ikuti aturan script call berikut
  31.  
  32.   ------------------------------------------------------------------------
  33.   Script Calls :
  34.   ------------------------------------------------------------------------
  35.   - message.ballon
  36.   Panggil script call seperti ini untuk membuat text ditampilkan dalam model
  37.   ballon
  38.  
  39.   - message.normal
  40.   Panggil script call seperti ini untuk mengembalikan message seperti biasa
  41.  
  42.   - message.subject = angka
  43.   Panggil script call seperti ini untuk menentukan subject yang berbicara.
  44.   Angka disana adalah event ID. Jika kamu memasukkan 0, maka yang akan
  45.   berbicara adalah player. Jika event ID tidak ada, maka message akan
  46.   berubah seperti biasa
  47.  
  48.   ------------------------------------------------------------------------
  49.   Terms of use :
  50.   ------------------------------------------------------------------------
  51.   Credit gw, TheoAllen. Kalo semisal u bisa ngedit2 script gw trus jadi lebih
  52.   keren, terserah. Ane bebasin. Asal ngga ngeklaim aja. Kalo semisal mau
  53.   dipake buat komersil, jangan lupa, gw dibagi gratisannya.
  54.  
  55. =end
  56. # =============================================================================
  57. # Konfigurasi
  58. # =============================================================================
  59. module THEO
  60.   module Msg
  61.    
  62.     Buffer_Y    = 32
  63.   # Jarak keatas message ballon (default: 32)
  64.    
  65.     FontSize    = 22
  66.   # Ukuran font dalam ballon
  67.  
  68.     BackColor1   = Color.new(100,100,100,150)
  69.   # Background Color atas untuk ballon. Harus diisi dengan :
  70.   # Color.new(red,green,blue,alpha)
  71.  
  72.     BackColor2   = Color.new(0,0,0,150)
  73.   # Background Color bawah untuk ballon. Harus diisi dengan :
  74.   # Color.new(red,green,blue,alpha)
  75.  
  76.     BorderColor = Color.new(0,0,0,255)
  77.   # Border Color untuk ballon. Harus diisi dengan :
  78.   # Color.new(red,green,blue,alpha)
  79.    
  80.     BorderSize = 2
  81.   # Ketebalan border
  82.  
  83.   # -------------------------------------------------------------------------
  84.   # [Hard mode / Scripter only]
  85.   # Note : Konfigurasi ini cuman untuk scripter. Kalo kamu ngga paham, mending
  86.   # jangan diubah-ubah isinya
  87.   # -------------------------------------------------------------------------
  88.   # Dibawah sini adalah regular expression buat kode2 yang ngga digunain buat
  89.   # perhitungan lebar window. Seperti kode wait dan semacemnya. Bagi kamu yang
  90.   # mau ngedit, setidaknya kamu harus ngerti REGEXP dalam bahasa Ruby.
  91.   # -------------------------------------------------------------------------
  92.     Unused_EscapeChar = [
  93.    
  94.     /\eC\[(\d+)\]/i,  # Change text color \C[n]
  95.     /\e\./,           # Wait for 1/4 second \.
  96.     /\e\|/,           # Wait for 1 second \|
  97.     /\e\{/,           # Make font bigger \{
  98.     /\e\}/,           # Make font smaller \{
  99.     /\e\$/,           # Show gold window \$
  100.     /\e!/,            # Wait for input \!
  101.     /\e>/,            # Display remaining text at line \>
  102.     /\e</,            # Cancel display remaining text \<
  103.     /\e\^/,           # Do not wait for input \^
  104.    
  105.     ] # <-- jangan dihapus
  106.  
  107.   end
  108. end
  109. # =============================================================================
  110. # Akhir dari konfigurasi
  111. # =============================================================================
  112. class Bitmap
  113.   def border_fill(color,size = 1)
  114.     fill_rect(0,0,width,size,color)
  115.     fill_rect(0,0,size,height,color)
  116.     fill_rect(width-size,0,size,height,color)
  117.     fill_rect(0,height-size,width,size,color)
  118.   end
  119. end
  120.  
  121. class Game_Interpreter
  122.  
  123.   def message
  124.     $game_message
  125.   end
  126.  
  127. end
  128.  
  129. class Game_Temp
  130.  
  131.   def window_base
  132.     unless @window_base
  133.       @window_base = Window_Base.new(0,0,1,1)
  134.       @window_base.visible =  false
  135.     end
  136.     return @window_base
  137.   end
  138.  
  139. end
  140.  
  141. class Game_Message
  142.   attr_accessor :ballon_refresh
  143.   attr_accessor :subject
  144.   attr_reader :type
  145.  
  146.   alias theo_msgballon_init initialize
  147.   def initialize
  148.     theo_msgballon_init
  149.     @subject = 0
  150.     normal
  151.   end
  152.  
  153.   def ballon
  154.     @type = :ballon
  155.     @ballon_refresh = true
  156.   end
  157.  
  158.   def normal
  159.     @type = :normal
  160.     @ballon_refresh = true
  161.   end
  162.  
  163.   def ballon?
  164.     return false unless SceneManager.scene_is?(Scene_Map)
  165.     @type == :ballon && @subject > -1 && !get_char.nil?
  166.   end
  167.   # -------------------------------------------------------
  168.   # Destruct escape character for length calculation
  169.   # -------------------------------------------------------
  170.   def convert_escape_characters(text)
  171.     result = $game_temp.window_base.convert_escape_characters(text)
  172.     THEO::Msg::Unused_EscapeChar.each do |esc_code|
  173.       result.gsub!(esc_code) {""}
  174.     end
  175.     result.gsub!(/\eI\[(\d+)\]/i) { "      " }
  176.     result
  177.   end
  178.  
  179.   def actor_name(n)
  180.     actor = n >= 1 ? $game_actors[n] : nil
  181.     actor ? actor.name : ""
  182.   end
  183.  
  184.   def party_member_name(n)
  185.     actor = n >= 1 ? $game_party.members[n - 1] : nil
  186.     actor ? actor.name : ""
  187.   end
  188.  
  189.   def longest_text
  190.     test = Bitmap.new(1,1)
  191.     test.font.size = THEO::Msg::FontSize
  192.     txts = []
  193.     @texts.each do |txt|
  194.       txts.push(convert_escape_characters(txt))
  195.     end
  196.     longest = txts.sort do |a,b|
  197.       test.text_size(b).width <=> test.text_size(a).width
  198.     end[0]
  199.     result = test.text_size(longest).width
  200.     test.dispose
  201.     return result
  202.   end
  203.  
  204.   def get_char
  205.     if @subject == 0
  206.       player = $game_player
  207.       return Point.new(player.screen_x, player.screen_y)
  208.     else
  209.       event = $game_map.events[@subject]
  210.       return nil unless event
  211.       return Point.new(event.screen_x, event.screen_y)
  212.     end
  213.   end
  214.  
  215.   def total_text
  216.     @texts.size
  217.   end
  218.  
  219.   alias theo_msgballon_add add
  220.   def add(text)
  221.     text = text.gsub(/<bm:[ ]*(\d+)>/i) do
  222.       @subject = $1.to_i
  223.       ""
  224.     end
  225.     theo_msgballon_add(text)
  226.   end
  227.  
  228. end
  229.  
  230. class Point
  231.   attr_accessor :x
  232.   attr_accessor :y
  233.  
  234.   def initialize(x,y)
  235.     @x, @y = x, y
  236.   end
  237. end
  238.  
  239. class BallonPointer < Sprite
  240.   attr_reader :window
  241.   attr_reader :pos
  242.  
  243.   def initialize(viewport,window)
  244.     super(viewport)
  245.     @window = window
  246.     @pos = :upper
  247.     self.bitmap = Cache.system("ballonpointer")
  248.     to_center
  249.     update
  250.   end
  251.  
  252.   def pos=(pos)
  253.     @pos = pos
  254.     update_placement
  255.   end
  256.  
  257.   def to_center
  258.     self.ox = width/2
  259.     self.oy = height
  260.   end
  261.  
  262.   def update
  263.     super
  264.     if window.ballon?
  265.       self.opacity = window.openness
  266.       update_placement
  267.     else
  268.       self.opacity = 0
  269.     end
  270.   end
  271.  
  272.   def update_placement
  273.     self.x = window.char.x
  274.     if pos == :upper
  275.       self.y = window.char.y - THEO::Msg::Buffer_Y - 2
  276.       self.angle = 0
  277.     else
  278.       self.angle = 180
  279.       self.y = window.char.y
  280.     end
  281.   end
  282.    
  283. end
  284.  
  285. class Window_Message < Window_Base
  286.  
  287.   alias theo_msgballon_init initialize
  288.   def initialize
  289.     theo_msgballon_init
  290.     @pointer = BallonPointer.new(viewport,self)
  291.   end
  292.  
  293.   alias theo_msgballon_clear_instances clear_instance_variables
  294.   def clear_instance_variables
  295.     theo_msgballon_clear_instances
  296.     @ballon = false
  297.     @subject = -1
  298.   end
  299.  
  300.   alias theo_msgballon_settings_changed? settings_changed?
  301.   def settings_changed?
  302.     theo_msgballon_settings_changed? || ballon_setting_changed?
  303.   end
  304.  
  305.   def ballon_setting_changed?
  306.     @ballon != ballon? || @subject != $game_message.subject
  307.   end
  308.  
  309.   def setup_window
  310.     if ballon?
  311.       setup_ballon
  312.     else
  313.       setup_normal
  314.     end
  315.   end
  316.  
  317.   def setup_normal
  318.     h = fitting_height(visible_line_number)
  319.     w = Graphics.width
  320.     self.width = w
  321.     self.height = h
  322.   end
  323.  
  324.   def setup_ballon
  325.     h = fitting_height($game_message.total_text)
  326.     w = $game_message.longest_text + 10 + (standard_padding * 2)
  327.     self.width = w
  328.     self.height = h
  329.     self.opacity = 0
  330.   end
  331.  
  332.   def ballon?
  333.     $game_message.ballon?
  334.   end
  335.  
  336.   alias theo_msgballon_update_bg update_background
  337.   def update_background
  338.     recreate_choice if @ballon != ballon?
  339.     @ballon = ballon?
  340.     @subject = $game_message.subject
  341.     theo_msgballon_update_bg
  342.   end
  343.  
  344.   alias theo_msgballon_normal_placement update_placement
  345.   def update_placement
  346.     setup_window
  347.     theo_msgballon_normal_placement
  348.     if ballon?
  349.       update_ballon_placement
  350.     else
  351.       self.x = 0
  352.     end
  353.   end
  354.  
  355.   def char
  356.     $game_message.get_char
  357.   end
  358.  
  359.   def update_ballon_placement
  360.     xpos = [[char.x - width/2, - standard_padding].max,
  361.       Graphics.width - width + standard_padding].min
  362.     ypos = char.y - height - THEO::Msg::Buffer_Y
  363.     @pointer.pos = :upper
  364.     if ypos < 0
  365.       ypos = char.y - 2
  366.       @pointer.pos = :lower
  367.     end
  368.     self.x = xpos
  369.     self.y = ypos
  370.   end
  371.  
  372.   alias theo_msgballon_new_page new_page
  373.   def new_page(text, pos)
  374.     if ballon?
  375.       return ballon_new_page(text, pos)
  376.     end
  377.     theo_msgballon_new_page(text, pos)
  378.   end
  379.  
  380.   def ballon_new_page(text, pos)
  381.     setup_ballon
  382.     contents.clear
  383.     entire_fill
  384.     reset_font_settings
  385.     pos[:x] = new_line_x
  386.     pos[:y] = 0
  387.     pos[:new_x] = new_line_x
  388.     pos[:height] = calc_line_height(text)
  389.     clear_flags
  390.   end
  391.  
  392.   alias theo_ballon_new_line_x new_line_x
  393.   def new_line_x
  394.     if ballon?
  395.       return 4
  396.     else
  397.       theo_ballon_new_line_x
  398.     end
  399.   end
  400.  
  401.   alias theo_msgballon_update update
  402.   def update
  403.     theo_msgballon_update
  404.     @pointer.update
  405.     update_ballon_placement if ballon?
  406.     recreate_choice if $game_message.ballon_refresh
  407.   end
  408.  
  409.   def recreate_choice
  410.     @choice_window.dispose
  411.     @choice_window = Window_ChoiceList.new(self)
  412.     $game_message.ballon_refresh = false
  413.   end
  414.  
  415.   def entire_fill
  416.     contents.gradient_fill_rect(contents.rect,THEO::Msg::BackColor1,
  417.       THEO::Msg::BackColor2,true)
  418.     contents.border_fill(THEO::Msg::BorderColor,THEO::Msg::BorderSize)
  419.   end
  420.  
  421.   alias theo_msgballon_width= width=
  422.   def width=(width)
  423.     self.theo_msgballon_width = width
  424.     create_contents
  425.   end
  426.  
  427.   alias theo_msgballon_height= height=
  428.   def height=(height)
  429.     self.theo_msgballon_height = height
  430.     create_contents
  431.   end
  432.  
  433.   alias theo_msgballon_line_height line_height
  434.   def line_height
  435.     if ballon?
  436.       THEO::Msg::FontSize
  437.     else
  438.       theo_msgballon_line_height
  439.     end
  440.   end
  441.  
  442.   alias theo_msgballon_reset_font reset_font_settings
  443.   def reset_font_settings
  444.     theo_msgballon_reset_font
  445.     contents.font.size = THEO::Msg::FontSize if ballon?
  446.   end
  447.   # ----------------------------------------------------------------
  448.   # Overwrite input pause ~
  449.   # ----------------------------------------------------------------
  450.   def input_pause
  451.     self.pause = true unless ballon?
  452.     wait(10)
  453.     Fiber.yield until Input.trigger?(:B) || Input.trigger?(:C)
  454.     Input.update
  455.     self.pause = false unless ballon?
  456.   end
  457.  
  458. end
  459.  
  460. class Window_ChoiceList < Window_Command
  461.  
  462.   alias theo_msgballon_start start
  463.   def start
  464.     reset_window
  465.     theo_msgballon_start
  466.   end
  467.  
  468.   def reset_window
  469.     self.opacity = 255
  470.   end
  471.  
  472.   def setup_ballon
  473.     self.opacity = 0
  474.     entire_fill
  475.   end
  476.  
  477.   def entire_fill
  478.     dispose_background
  479.     @back_bitmap = Bitmap.new(contents.width,contents.height)
  480.     @back_sprite = Sprite.new(viewport)
  481.     @back_sprite.bitmap = @back_bitmap
  482.     @back_sprite.x = x + standard_padding
  483.     @back_sprite.y = y + standard_padding
  484.     bmp = @back_sprite.bitmap
  485.     bmp.gradient_fill_rect(contents.rect,THEO::Msg::BackColor1,
  486.       THEO::Msg::BackColor2,true)
  487.     bmp.border_fill(THEO::Msg::BorderColor,THEO::Msg::BorderSize)
  488.   end
  489.  
  490.   alias theo_msgballon_reset_font reset_font_settings
  491.   def reset_font_settings
  492.     theo_msgballon_reset_font
  493.     contents.font.size = THEO::Msg::FontSize if @message_window.ballon?
  494.   end
  495.  
  496.   alias theo_msgballon_draw_all draw_all_items
  497.   def draw_all_items
  498.     setup_ballon if @message_window.ballon?
  499.     theo_msgballon_draw_all
  500.   end
  501.  
  502.   alias theo_msgballon_update_placement update_placement
  503.   def update_placement
  504.     theo_msgballon_update_placement
  505.     if @message_window.ballon?
  506.       update_ballon_placement
  507.     end
  508.   end
  509.  
  510.   def update_ballon_placement
  511.     msg = @message_window
  512.     self.y = msg.y
  513.     self.x = msg.x + msg.width - standard_padding * 2
  514.     if (x + width) > Graphics.width
  515.       self.x = msg.x - width + standard_padding * 2
  516.     end
  517.   end
  518.  
  519.   alias theo_msgballon_line_height line_height
  520.   def line_height
  521.     if @message_window.ballon?
  522.       THEO::Msg::FontSize
  523.     else
  524.       theo_msgballon_line_height
  525.     end
  526.   end
  527.  
  528.   def dispose_background
  529.     @back_bitmap.dispose if @back_bitmap
  530.     @back_sprite.dispose if @back_sprite
  531.   end
  532.  
  533.   alias theo_msgballon_close close
  534.   def close
  535.     dispose_background
  536.     theo_msgballon_close
  537.   end
  538.  
  539. end
Advertisement
Add Comment
Please, Sign In to add comment