Popup Window

Popup Window Version: 1.21
By: Regi


Introduction

This pop-up window displays a small line of text. It will disappear after a customizable time and won't inhibit movement, unlike Window_Message. Useful for small messages like "world map location unlocked" or "obtained item x [quantity]!"

Features
  • automatically adjusts window size to text and centers
  • disappears after a customizable time, and does not inhibit movement
  • can display items, icons, and quantity

Screenshots

Image
shameless project plug~

Demo

none.

Script

  1. #==============================================================================
  2. # Popup Window by Regi
  3. #------------------------------------------------------------------------------
  4. # Version: 1.21  |  Last Updated: 12/30/11
  5. #------------------------------------------------------------------------------
  6. #  This pop-up window displays a small line of text. It will disappear after
  7. #  a customizable time and won't inhibit movement, unlike Window_Message.
  8. #------------------------------------------------------------------------------
  9. #  Syntax: popup(text, item, value)
  10. #==============================================================================
  11.  
  12. class Window_Popup < Window_Base
  13.   #--------------------------------------------------------------------------
  14.   # * Object Initialization
  15.   #--------------------------------------------------------------------------
  16.   def initialize
  17.     super(0, 200, 320, 64)
  18.     self.contents = Bitmap.new(width - 32, height - 32)
  19.     self.visible = false
  20.     self.z = 9998
  21. #=============================================================================
  22. # START CUSTOMIZATION
  23. #=============================================================================
  24.     self.back_opacity = 180                   # windowskin opacity
  25.     self.contents.font.name = "Verdana"       # font name
  26.     self.contents.font.color = normal_color   # font color
  27.     self.contents.font.size = 22              # font size
  28.   end
  29.  
  30.   #--------------------------------------------------------------------------
  31.   # * Predefined text
  32.   #--------------------------------------------------------------------------
  33.   def get_text(index)
  34.     return case index
  35.     # Define shortcuts for displaying text
  36.       when 0 then 'this is a popup window'
  37.       when 1 then 'Journal entry added'
  38.       when 2 then 'Acquired'
  39.     end
  40. #=============================================================================
  41. # END CUSTOMIZATION
  42. #=============================================================================
  43.   end
  44.  
  45.   #--------------------------------------------------------------------------
  46.   # * Draw popup text
  47.   #--------------------------------------------------------------------------
  48.   def refresh_popup_text(text, item, value)
  49.     self.contents.clear
  50.     # Get text
  51.     if text.is_a?(String)
  52.       string1 = text
  53.     elsif text.is_a?(Integer)
  54.       string1 = get_text(text)
  55.     end
  56.     if item.nil?
  57.       string2 = ''
  58.     else
  59.       string2 = item.name + (value > 0 ? ' x ' + value.to_s : '')
  60.     end
  61.     # Adjust width (fit window to text)
  62.     w1 = self.contents.text_size(string1).width
  63.     w2 = self.contents.text_size(string2).width + (item.nil? ? 0 : 32)
  64.     width = w1 + w2
  65.     self.width = width + 40
  66.     # Adjust x position (center window)
  67.     self.x = (640 - self.width) / 2
  68.     # Draw text
  69.     self.contents = Bitmap.new(self.width - 32, height - 32)
  70.     self.contents.draw_text(4, 0, w1, 32, string1)
  71.     unless item.nil?
  72.       # Draw icon and item value
  73.       bitmap = RPG::Cache.icon(item.icon_name)
  74.       self.contents.blt(w1 + 4, 4, bitmap, Rect.new(0, 0, 24, 24))
  75.       self.contents.draw_text(w1 + 32, 0, w2, 32, string2)
  76.     end
  77.   end
  78.  
  79.   def refresh
  80.     super
  81.   end
  82.  
  83. end
  84.  
  85. #==============================================================================
  86. # ** Scene Map
  87. #==============================================================================
  88. class Scene_Map
  89.  
  90.   attr_accessor :popup_window
  91.   attr_accessor :popup_count
  92.  
  93.   alias reg_popup_main main
  94.   def main
  95.     # Create window
  96.     @popup_window = Window_Popup.new
  97.     @popup_count = -1
  98.     # Normal Scene_Map processing
  99.     reg_popup_main
  100.     # Dispose of window
  101.     @popup_window.dispose
  102.   end
  103.  
  104.   alias reg_popup_update update
  105.   def update
  106.     reg_popup_update
  107.     # Update counter
  108.     if @popup_count >= 0
  109.       @popup_count -= 1
  110.     end
  111.     if @popup_count == 0
  112.       @popup_window.visible = false
  113.     end
  114.   end
  115. end
  116.  
  117. #==============================================================================
  118. # ** Interpreter (new command)
  119. #==============================================================================
  120. class Interpreter
  121.   def popup(text = '', item = nil, value = 0)
  122.     $scene.popup_window.refresh_popup_text(text, item, value)
  123.     $scene.popup_window.visible = true
  124.     $scene.popup_count = 80
  125.   end
  126. end
Expand to see the code.


Auto-item popup add-on:
  1.  #--------------------------------------------------------------------------
  2.   # * Change Items
  3.   #--------------------------------------------------------------------------
  4.   def command_126
  5.     # Get value to operate
  6.     value = operate_value(@parameters[1], @parameters[2], @parameters[3])
  7.     # Increase / decrease items
  8.     $game_party.gain_item(@parameters[0], value)
  9.     # Popup Window
  10.     text = (value > 0 ? 'Acquired' : 'Lost')
  11.     popup(text, $data_items[@parameters[0]], value)
  12.     # Continue
  13.     return true
  14.   end
Expand to see the code.


Instructions

To use the auto-item popup add-on, insert the scriptlet above line 126 in the main script. This will create a window that says "Acquired item" or "Lost item" whenever you used the Change Items command.

To create a popup window, use the following syntax through Call Script:
popup(text, item, value)

text = string you wish to display (in "quotations") or an integer for a predefined string
item = (optional): use $data_items[ID], replacing ID with the database item id.
value = (optional): the quantity of items. If set to 0, will not appear.

Example call:
popup("Journal entry added")
-> see above screenshot
popup(2, $data_items[1], 3)
-> "Acquired ID1 x 3"

To customize windowskin opacity, font name, color, size, and predefined text, go to line 23 and edit!

FAQ

none.

Compatibility

Should be compatible with most scripts, except for any that utilize "class Window_Popup".

Credits and Thanks

Thanks to BlueScope for feedback and helping to fix the width issue.

Author's Notes

fun fact: this is Regi's first script :!: (not including minor edits and scriptlets) inspired by the automatic "obtained item" window from MotW.

Terms and Conditions

Free for commercial and/or noncommercial use with credit! (If the former, a link to said game would be splendid.) Please do not redistribute to other forums without permission.
Yeah, this definately is useful little snippet... and it even comes without customization constants, I'm impressed! :D (seriously)
Overall, it's very readable and not too newbie-scripted, actually. What I can see are a little cheap variable names, such as wt or tx... definately not nice, as they're not speaking for themselves, and width or text aren't that much longer.

A definate bug you need to address is the width of the text drawing, because as your screenshot proves with the little arrow on the right, it is not correctly measured. As you set the window width to tx+64, and the text field width to tx+32, that means you leave a border of 32, or 16 on each side, while it should be 40, or 20 on each side, for RMXP.
Another thing is the font setting you did: Setting the font in a subwindow so it automatically doesn't inherit the default font set for the game is not going to work well, because those who choose a similar font won't notice it, and complain why people like me who juggle fonts all day will notice the difference. On the other hand, people with strange mumbo-jumbo-fonts will suddenly have a regular font different from what they wanted originally, which isn't too cool either.
Last but not least, it doesn't seem like you handle the case where the window's width is greater than 640, or the game window's width... might be helpful.

Another thing I'd like to mention is the fact that this is not very useable for non-English languages, for example German: You'd write 'Item x3 erhalten!'. So, you might think of a different way to handle things here if you might want to make it multi-language compatible... regular expressions might be helpful here?
This is pretty cool. Given the width of the text input area for call script I'd probably be tempted to commit programming sin and call the method a one-letter word, similar to p(), so that it can be called by something like:

m "This is a message. It is a long message. It is in a textbox."

Rather than;

popup_window("This...etc")

Since the latter has 13 potentially redundant characters there.

But that's just me and I totally understand why you kept it this way.
Thanks for the feedback! This was originally intended for my own project, hence the lack of much customization (font, language, etc.) But I'll work on your points.

@BlueScope, I never really figured out a good way to set width, because there's 5 different cases (just text, text + item, text + item + value, just item, and item + value) and they all screw with my window adjustment. If you have any pointers, that'd be really helpful! That explains the arrow though, it was bothering me and I couldn't figure how to remove it.
Also, I'm not too familiar with regular expressions, but I can look into that. When you're talking about different languages, you mean moving the item before the text, right?

@Amy Pond: good suggestion. I suppose it could easily shorten to popup() or pop() and still be readable. Or I could give the option of inputting an integer, with predefined strings (that way you don't have to keep typing the same one out).
Well, if you have problems with the multiple pats, just go the easy way: Put all the strings together, and then get the width of the text, and maybe add 32px for the icon. That way, there won't be any problem, but either way, I think this should be fixed by just changing the +64 and +32 values to have a difference of 40, rather than 32.

As for the language, yes, I'm talking about the possibility to put the string at the end of the field... an additional flag might do to trigger it, however you might be better off to just make a parameter for 'before item' and one for 'behind item'. That way, everyone can choose and individally decide what they want.
Updated the script: now script calls go under popup() and you can use predefined strings as well. I tried to fix the width issue, and everything centers correctly, but I can't get rid of the arrow unless I make self.width a ridiculously large number (width + 200). If I try to make the text widths smaller, the text will just be squished to fit. Any help there?
Indeed... first of all, line 64 should say
  1. self.width = width + 40
Expand to see the code.


Then, starting at 68, you can shorten this:
  1.    if item.nil?
  2.       self.contents.draw_text(8, 0, w1, 32, string1)
  3.     else
  4.       self.contents.draw_text(0, 0, w1, 32, string1)
  5.       # ...
Expand to see the code.

To this:
  1.    self.contents.draw_text(4, 0, width, 32, string1)
  2.     unless item.nil?
  3.       # ...
Expand to see the code.

That will get rid of another 4px difference you got, as RMXP's default for left alignment is usually 4px (to avoid cut letters such as 'W' that would appear at a distance of 0px).

As for the rest... just make sure your draws never exceed the width you set above (as in w1+w2), as that is what your window's width is calculated by in line 64 - if you draw contents larger than that, you will get the arrow - if you don't, you won't.

Sadly, I don't have any RMwhatever to try it out, so that's all the help I can give you at a first glance.
Thanks for the help. I applied your updates and it centers perfectly, but the arrow remained. Only after some tinkering did I realize it came from the Bitmap width (self.contents.width) itself, rather than the text width. I managed to fix the problem by adding the following to readjust the width.

self.contents = Bitmap.new(self.width - 32, height - 32)
Just added a scriptlet add-on for the "Change Items" command. It will automatically popup a window with "Acquired" or "Lost" plus the item. Just demonstrates how easily you could add a feature without having to manually type it out each time!
@regi - this popup window script just keeps getting better, good stuff man, I really like! ^,^

Why not include the add-on into the script? (verses editing the default interpreter...)

  1. #==============================================================================
  2. # Popup Window by Regi
  3. #------------------------------------------------------------------------------
  4. # Version: 1.2  |  Last Updated: 7/26/11
  5. #------------------------------------------------------------------------------
  6. #  This pop-up window displays a small line of text. It will disappear after
  7. #  a customizable time and won't inhibit movement, unlike Window_Message.
  8. #------------------------------------------------------------------------------
  9. #  Syntax: popup_window(text, item, value)
  10. #==============================================================================
  11.  
  12. class Window_Popup < Window_Base
  13.   #--------------------------------------------------------------------------
  14.   # * Object Initialization
  15.   #--------------------------------------------------------------------------
  16.   def initialize
  17.     super(0, 160, 320, 64)
  18.     self.contents = Bitmap.new(width - 32, height - 32)
  19.     self.visible = false
  20. #=============================================================================
  21. # START CUSTOMIZATION
  22. #=============================================================================
  23.     self.back_opacity = 180                   # windowskin opacity
  24.     self.contents.font.name = "Verdana"       # font name
  25.     self.contents.font.color = normal_color   # font color
  26.     self.contents.font.size = 22              # font size
  27.   end
  28.  
  29.   #--------------------------------------------------------------------------
  30.   # * Predefined text
  31.   #--------------------------------------------------------------------------
  32.   def get_text(index)
  33.     return case index
  34.     # Define shortcuts for displaying text
  35.       when 0 then 'this is a popup window'
  36.       when 1 then 'Journal entry added'
  37.       when 2 then 'Acquired'
  38.     end
  39. #=============================================================================
  40. # END CUSTOMIZATION
  41. #=============================================================================
  42.   end
  43.  
  44.   #--------------------------------------------------------------------------
  45.   # * Draw popup text
  46.   #--------------------------------------------------------------------------
  47.   def refresh_popup_text(text, item, value)
  48.     self.contents.clear
  49.     # Get text
  50.     if text.is_a?(String)
  51.       string1 = text
  52.     elsif text.is_a?(Integer)
  53.       string1 = get_text(text)
  54.     end
  55.     if item.nil?
  56.       string2 = ''
  57.     else
  58.       string2 = item.name + (value > 0 ? ' x ' + value.to_s : '')
  59.     end
  60.     # Adjust width (fit window to text)
  61.     w1 = self.contents.text_size(string1).width
  62.     w2 = self.contents.text_size(string2).width + (item.nil? ? 0 : 32)
  63.     width = w1 + w2
  64.     self.width = width + 40
  65.     # Adjust x position (center window)
  66.     self.x = (640 - self.width) / 2
  67.     # Draw text
  68.     self.contents = Bitmap.new(self.width - 32, height - 32)
  69.     self.contents.draw_text(4, 0, w1, 32, string1)
  70.     unless item.nil?
  71.       # Draw icon and item value
  72.       bitmap = RPG::Cache.icon(item.icon_name)
  73.       self.contents.blt(w1 + 4, 4, bitmap, Rect.new(0, 0, 24, 24))
  74.       self.contents.draw_text(w1 + 32, 0, w2, 32, string2)
  75.     end
  76.   end
  77.  
  78.   def refresh
  79.     super
  80.   end
  81.  
  82. end
  83.  
  84. #==============================================================================
  85. # ** Scene Map
  86. #==============================================================================
  87. class Scene_Map
  88.  
  89.   attr_accessor :popup_window
  90.   attr_accessor :popup_count
  91.  
  92.   alias reg_popup_main main
  93.   def main
  94.     # Create window
  95.     @popup_window = Window_Popup.new
  96.     @popup_count = -1
  97.     # Normal Scene_Map processing
  98.     reg_popup_main
  99.     # Dispose of window
  100.     @popup_window.dispose
  101.   end
  102.  
  103.   alias reg_popup_update update
  104.   def update
  105.     reg_popup_update
  106.     # Update counter
  107.     if @popup_count >= 0
  108.       @popup_count -= 1
  109.     end
  110.     if @popup_count == 0
  111.       @popup_window.visible = false
  112.     end
  113.   end
  114. end
  115.  
  116. #==============================================================================
  117. # ** Interpreter (new command)
  118. #==============================================================================
  119. class Interpreter
  120.   def popup(text = '', item = nil, value = 0)
  121.     $scene.popup_window.refresh_popup_text(text, item, value)
  122.     $scene.popup_window.visible = true
  123.     $scene.popup_count = 80
  124.   end
  125.   #--------------------------------------------------------------------------
  126.   # * Change Items
  127.   #--------------------------------------------------------------------------
  128.   alias reg_popup_com126 command_126
  129.   def command_126
  130.     reg_popup_com126
  131.     # Get value to operate
  132.     value = operate_value(@parameters[1], @parameters[2], @parameters[3])
  133.     # Increase / decrease items
  134.     $game_party.gain_item(@parameters[0], value)
  135.     # Popup Window
  136.     text = (value > 0 ? 'Acquired' : 'Lost')
  137.     popup(text, $data_items[@parameters[0]], value)
  138.     # Continue
  139.     return true
  140.   end
  141. end
Expand to see the code.
Thanks! I did mean putting it in the script, I just kept them separate so people would have the option of using it. Sorry if it wasn't clear.
~maybe it was just me reading too fast =-/

I did notice in your comment header that "Syntax: popup_window(text, item, value)" needs to be updated.
now that you are using just "popup()"

I would have to say some of my favorite scripts are simple and easy to use.
This would be one of them, your play with code is off to a great start being this is your first script.

I'm certain a lot of ABS users will find room for it in their projects. ^,^
Oops, lemme fix that up.
And I'm glad you like it! I've been messing around with some of the default and released scripts for awhile now, but edits can only take you so far. I'll be sure to release any other ideas I come up with!

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=75169