Guest User

RMXP Popup Windows 2.1

a guest
Mar 4th, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.01 KB | None | 0 0
  1. #==============================================================================
  2. # ** Event Popups
  3. #------------------------------------------------------------------------------
  4. #  made by: Silverwind (Metaclaw from GDU forums)
  5. #  version: 2.0
  6. #
  7. #  you have my permission to use this in comercial games.
  8. #  please credit me if you use this script.
  9. #==============================================================================
  10. =begin
  11.  
  12.  
  13.  Instructions:
  14.  
  15.  1) To show some text above an event, add a script command, saying:
  16.     text = "place your text here"
  17.     popup(text)
  18.    
  19.  2) To show an icon in the popup, add a script command, saying:
  20.     text = "place your text here" *
  21.     icon = "the icon's name" **
  22.     popup(text, icon)
  23.  
  24.  3) To automatically get the icon of an Armor, Item, or Weapon
  25.     replace the line saying 'icon = something', with:
  26.     icon = get_icon(item_type, item_number) ***
  27.    
  28.     ie:
  29.     icon = get_icon('Armor', 2)
  30.    
  31.  4) You can draw two lines of text, by using:
  32.     line1 = "Headline"
  33.     line2 = "some text"
  34.     icon = "icon name"
  35.     popup(line1, icon, line2)
  36.    
  37.     the 2nd line will have smaller font and a white color.
  38.     it will be displayed above the first.
  39.     if you don't want an icon, set it to: nil
  40.      
  41.  5) To set the font size, color and the window opacity
  42.     as well as the time the popup stays on the screen,
  43.     see line 95 and below.
  44.    
  45.    
  46.    Notes:
  47.    *   To show only an icon, set the text to ""
  48.    **  The icon-name should not include ".png", ".jpg" etc.
  49.    *** You can also use 'skill' and a skill number.  
  50.    
  51. =end
  52.  
  53.  
  54.  
  55. class Window_Base < Window
  56.  
  57.   # edited this method,
  58.   # to not re-set the window skin to default.
  59.   def update
  60.     super
  61.   end
  62.  
  63. end
  64.  
  65.  
  66. class Interpreter
  67.  
  68.   def popup(text, icon=nil, line2=nil)
  69.     return unless $scene.is_a?(Scene_Map)
  70.     $scene.spriteset.popup(@event_id, text, icon, line2)
  71.   end
  72.  
  73.   def get_icon(type, id)
  74.     case type
  75.     when 'Armor', 'armor'
  76.       return $data_armors[id].icon_name
  77.     when 'Item', 'item'
  78.       return $data_items[id].icon_name
  79.     when 'Skill', 'skill'
  80.       return $data_skills[id].icon_name
  81.     when 'Weapon', 'weapon'
  82.       return $data_weapons[id].icon_name
  83.     else
  84.       return nil
  85.     end
  86.    
  87.   end
  88.  
  89. end
  90.  
  91.  
  92.  
  93. class Window_Popup < Window_Base
  94.  
  95.   FONT = "Arial Black"         # the font name
  96.   OPACITY = 160                # window opacity. set to 0 to make it invisible
  97.   DURATION = 100               # duration, in frames.
  98.   FONT_SIZE = 32               # font size for the first text line
  99.   FONT_SMALL_SIZE = 22         # font size for the second text line
  100.   FONT_COLOR = [80,120,220]    # for colors, use: [red, green, blue]
  101.   WINDOWSKIN = "001-Blue01"    # windowskin name.
  102.   LINE_HEIGHT = 32             # change this value is your text is too big
  103.                                # for the popup / or the popup size is too big.
  104.   POPUP_X = 200                 # set where the popup shows. set it to -1
  105.   POPUP_Y = 100                 # , to draw it above the event.
  106.                                
  107.   def initialize(char_sprite, width2, height2)
  108.     x = char_sprite.x
  109.     y = char_sprite.y
  110.     y -= (char_sprite.bitmap.height / 4)
  111.     is_tile = (char_sprite.character.tile_id != 0 )
  112.     w = [width2 + 32, 640].min
  113.     h = height2
  114.     x -= w / 2
  115.     y -= h - 16
  116.     x = POPUP_X if POPUP_X > 0
  117.     y = POPUP_Y if POPUP_Y > 0
  118.     super(x,y,w,h)
  119.     @windowskin_name = WINDOWSKIN
  120.     self.windowskin = RPG::Cache.windowskin(@windowskin_name)
  121.     self.contents = Bitmap.new(width - 32, height - 32)
  122.     self.opacity = OPACITY
  123.     self.back_opacity = OPACITY
  124.     @countdown = DURATION
  125.    
  126.   end
  127.  
  128.  
  129.   def draw_popup_text(text, text2=nil)
  130.     x = 0
  131.     y = -16
  132.     w = 160
  133.     h = 64
  134.     w1 = self.contents.text_size(text).width
  135.     unless text2.nil?
  136.       w2 = self.contents.text_size(text2).width
  137.       w = [w1, w2].max
  138.     else
  139.       w = w1
  140.     end
  141.     w = [w, width].min
  142.     self.contents.font.name = FONT
  143.     # draw the headline
  144.     self.contents.font.size = FONT_SIZE
  145.     self.contents.font.color.set(0,0,0)
  146.     self.contents.draw_text(x-1, y-1, w, h, text, 1)
  147.     self.contents.draw_text(x+1, y-1, w, h, text, 1)
  148.     self.contents.draw_text(x-1, y+1, w, h, text, 1)
  149.     self.contents.draw_text(x+1, y+1, w, h, text, 1)
  150.     r = FONT_COLOR[0]
  151.     g = FONT_COLOR[1]
  152.     b = FONT_COLOR[2]
  153.     self.contents.font.color.set(r,g,b)
  154.     self.contents.draw_text(x, y, w, h, text, 1)
  155.     # draw the second line
  156.     return if text2.nil?
  157.     y += 24
  158.     self.contents.font.size = FONT_SMALL_SIZE
  159.     self.contents.font.color.set(0,0,0)
  160.     self.contents.draw_text(x-1, y-1, w, h, text2, 1)
  161.     self.contents.draw_text(x+1, y-1, w, h, text2, 1)
  162.     self.contents.draw_text(x-1, y+1, w, h, text2, 1)
  163.     self.contents.draw_text(x+1, y+1, w, h, text2, 1)
  164.     self.contents.font.color.set(r,g,b)
  165.     self.contents.draw_text(x, y , w, h, text2, 1)
  166.   end
  167.  
  168.   def draw_icon(icon_name)
  169.     bitmap = RPG::Cache.icon(icon_name)
  170.     opacity = 255
  171.     x = (width / 2) - 24 - 6
  172.     lines = height / LINE_HEIGHT
  173.     y = LINE_HEIGHT * (lines - 2)
  174.     self.contents.blt(x, y, bitmap, Rect.new(0, 0, 24, 24), opacity)
  175.   end
  176.  
  177.   alias popup_update update
  178.   def update
  179.     popup_update
  180.     @countdown -= 1 if @countdown > 0
  181.     if @countdown < 30
  182.       self.contents_opacity = self.contents_opacity - 20
  183.     end
  184.   end
  185.  
  186.   def hide?
  187.     return @countdown == 0
  188.   end
  189.  
  190. end
  191.  
  192.  
  193. class Scene_Map
  194.   attr_reader   :spriteset
  195. end
  196.  
  197. class Spriteset_Map
  198.  
  199.   #------------------------------------------------------#
  200.   # > show a popup above the specified event,
  201.   # with the specified text, and the icon (if any).
  202.   # showing a second line of text is optional.
  203.   #------------------------------------------------------#
  204.   def popup(event_id, text, icon=nil, line2=nil)
  205.     for char_sprite in @character_sprites
  206.       if char_sprite.character.id == event_id
  207.         show_popup_box(text, line2, char_sprite, icon)
  208.         return
  209.       end
  210.     end
  211.   end
  212.  
  213.   def show_popup_box(text, text2, char_sprite, icon)
  214.     bit = Bitmap.new(1,1)
  215.     w1 = bit.text_size(text).width
  216.     unless text2.nil?
  217.       w2 = bit.text_size(text2).width
  218.       w = [w1, w2].max
  219.     else
  220.       w = w1
  221.     end
  222.     bit.dispose
  223.     w = [w, 640].min
  224.     w = [w, 64].max
  225.     lines = 0
  226.     lines += 1 unless text == ""
  227.     lines += 1 unless text2.nil?
  228.     lines += 1 unless icon.nil?
  229.     lines = [lines, 1].max
  230.     line_h = Window_Popup::LINE_HEIGHT
  231.     h = (lines+1) * line_h
  232.     @popups = [] if @popups.nil?
  233.     popup = Window_Popup.new(char_sprite, w, h)
  234.     popup.draw_popup_text(text, text2)
  235.     unless icon.nil?
  236.       popup.draw_icon(icon)
  237.     end
  238.     @popups.push(popup)
  239.    
  240.   end
  241.  
  242.   alias popup_icon_update update
  243.   def update
  244.     popup_icon_update    
  245.     if !@popups.nil?
  246.       for pop in @popups
  247.         pop.update
  248.         pop.dispose if pop.hide?
  249.       end
  250.       @popups.delete_if {|pop| pop.disposed? }
  251.     end
  252.   end
  253.  
  254. end
Advertisement
Add Comment
Please, Sign In to add comment