TheoAllen

Theo - Message Ballon

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