Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Popup Window

regi

Sponsor

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

Popup1.png

shameless project plug~

Demo

none.

Script

Code:
#==============================================================================

# Popup Window by Regi

#------------------------------------------------------------------------------

# Version: 1.21  |  Last Updated: 12/30/11

#------------------------------------------------------------------------------

#  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.

#------------------------------------------------------------------------------

#  Syntax: popup(text, item, value)

#==============================================================================

 

class Window_Popup < Window_Base

  #--------------------------------------------------------------------------

  # * Object Initialization

  #--------------------------------------------------------------------------

  def initialize

    super(0, 200, 320, 64)

    self.contents = Bitmap.new(width - 32, height - 32)

    self.visible = false

    self.z = 9998

#=============================================================================

# START CUSTOMIZATION

#=============================================================================

    self.back_opacity = 180                   # windowskin opacity

    self.contents.font.name = "Verdana"       # font name

    self.contents.font.color = normal_color   # font color

    self.contents.font.size = 22              # font size

  end

  

  #--------------------------------------------------------------------------

  # * Predefined text

  #--------------------------------------------------------------------------

  def get_text(index)

    return case index

    # Define shortcuts for displaying text

      when 0 then 'this is a popup window'

      when 1 then 'Journal entry added'

      when 2 then 'Acquired'

    end

#=============================================================================

# END CUSTOMIZATION

#=============================================================================

  end

  

  #--------------------------------------------------------------------------

  # * Draw popup text

  #--------------------------------------------------------------------------

  def refresh_popup_text(text, item, value)

    self.contents.clear

    # Get text

    if text.is_a?(String)

      string1 = text

    elsif text.is_a?(Integer)

      string1 = get_text(text)

    end

    if item.nil?

      string2 = ''

    else

      string2 = item.name + (value > 0 ? ' x ' + value.to_s : '')

    end

    # Adjust width (fit window to text)

    w1 = self.contents.text_size(string1).width

    w2 = self.contents.text_size(string2).width + (item.nil? ? 0 : 32)

    width = w1 + w2

    self.width = width + 40

    # Adjust x position (center window)

    self.x = (640 - self.width) / 2

    # Draw text

    self.contents = Bitmap.new(self.width - 32, height - 32)

    self.contents.draw_text(4, 0, w1, 32, string1)

    unless item.nil?

      # Draw icon and item value

      bitmap = RPG::Cache.icon(item.icon_name)

      self.contents.blt(w1 + 4, 4, bitmap, Rect.new(0, 0, 24, 24))

      self.contents.draw_text(w1 + 32, 0, w2, 32, string2)

    end

  end

  

  def refresh

    super

  end

  

end

 

#==============================================================================

# ** Scene Map

#==============================================================================

class Scene_Map

  

  attr_accessor :popup_window

  attr_accessor :popup_count

  

  alias reg_popup_main main

  def main

    # Create window

    @popup_window = Window_Popup.new

    @popup_count = -1

    # Normal Scene_Map processing

    reg_popup_main

    # Dispose of window

    @popup_window.dispose

  end

  

  alias reg_popup_update update

  def update

    reg_popup_update

    # Update counter

    if @popup_count >= 0

      @popup_count -= 1

    end

    if @popup_count == 0

      @popup_window.visible = false

    end

  end

end

 

#==============================================================================

# ** Interpreter (new command)

#==============================================================================

class Interpreter

  def popup(text = '', item = nil, value = 0)

    $scene.popup_window.refresh_popup_text(text, item, value)

    $scene.popup_window.visible = true

    $scene.popup_count = 80

  end

end

Auto-item popup add-on:
Code:
  #--------------------------------------------------------------------------

  # * Change Items

  #--------------------------------------------------------------------------

  def command_126

    # Get value to operate

    value = operate_value(@parameters[1], @parameters[2], @parameters[3])

    # Increase / decrease items

    $game_party.gain_item(@parameters[0], value)

    # Popup Window

    text = (value > 0 ? 'Acquired' : 'Lost')

    popup(text, $data_items[@parameters[0]], value)

    # Continue

    return true

  end

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.
 

regi

Sponsor

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.
 

regi

Sponsor

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
Code:
self.width = width + 40

Then, starting at 68, you can shorten this:
Code:
    if item.nil?

      self.contents.draw_text(8, 0, w1, 32, string1)

    else

      self.contents.draw_text(0, 0, w1, 32, string1)

      # ...
To this:
Code:
    self.contents.draw_text(4, 0, width, 32, string1)

    unless item.nil?

      # ...
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.
 

regi

Sponsor

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)
 

regi

Sponsor

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...)

Code:
#==============================================================================

# Popup Window by Regi

#------------------------------------------------------------------------------

# Version: 1.2  |  Last Updated: 7/26/11

#------------------------------------------------------------------------------

#  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.

#------------------------------------------------------------------------------

#  Syntax: popup_window(text, item, value)

#==============================================================================

 

class Window_Popup < Window_Base

  #--------------------------------------------------------------------------

  # * Object Initialization

  #--------------------------------------------------------------------------

  def initialize

    super(0, 160, 320, 64)

    self.contents = Bitmap.new(width - 32, height - 32)

    self.visible = false

#=============================================================================

# START CUSTOMIZATION

#=============================================================================

    self.back_opacity = 180                   # windowskin opacity

    self.contents.font.name = "Verdana"       # font name

    self.contents.font.color = normal_color   # font color

    self.contents.font.size = 22              # font size

  end

 

  #--------------------------------------------------------------------------

  # * Predefined text

  #--------------------------------------------------------------------------

  def get_text(index)

    return case index

    # Define shortcuts for displaying text

      when 0 then 'this is a popup window'

      when 1 then 'Journal entry added'

      when 2 then 'Acquired'

    end

#=============================================================================

# END CUSTOMIZATION

#=============================================================================

  end

 

  #--------------------------------------------------------------------------

  # * Draw popup text

  #--------------------------------------------------------------------------

  def refresh_popup_text(text, item, value)

    self.contents.clear

    # Get text

    if text.is_a?(String)

      string1 = text

    elsif text.is_a?(Integer)

      string1 = get_text(text)

    end

    if item.nil?

      string2 = ''

    else

      string2 = item.name + (value > 0 ? ' x ' + value.to_s : '')

    end

    # Adjust width (fit window to text)

    w1 = self.contents.text_size(string1).width

    w2 = self.contents.text_size(string2).width + (item.nil? ? 0 : 32)

    width = w1 + w2

    self.width = width + 40

    # Adjust x position (center window)

    self.x = (640 - self.width) / 2

    # Draw text

    self.contents = Bitmap.new(self.width - 32, height - 32)

    self.contents.draw_text(4, 0, w1, 32, string1)

    unless item.nil?

      # Draw icon and item value

      bitmap = RPG::Cache.icon(item.icon_name)

      self.contents.blt(w1 + 4, 4, bitmap, Rect.new(0, 0, 24, 24))

      self.contents.draw_text(w1 + 32, 0, w2, 32, string2)

    end

  end

 

  def refresh

    super

  end

 

end

 

#==============================================================================

# ** Scene Map

#==============================================================================

class Scene_Map

 

  attr_accessor :popup_window

  attr_accessor :popup_count

 

  alias reg_popup_main main

  def main

    # Create window

    @popup_window = Window_Popup.new

    @popup_count = -1

    # Normal Scene_Map processing

    reg_popup_main

    # Dispose of window

    @popup_window.dispose

  end

 

  alias reg_popup_update update

  def update

    reg_popup_update

    # Update counter

    if @popup_count >= 0

      @popup_count -= 1

    end

    if @popup_count == 0

      @popup_window.visible = false

    end

  end

end

 

#==============================================================================

# ** Interpreter (new command)

#==============================================================================

class Interpreter

  def popup(text = '', item = nil, value = 0)

    $scene.popup_window.refresh_popup_text(text, item, value)

    $scene.popup_window.visible = true

    $scene.popup_count = 80

  end

  #--------------------------------------------------------------------------

  # * Change Items

  #--------------------------------------------------------------------------

  alias reg_popup_com126 command_126

  def command_126

    reg_popup_com126

    # Get value to operate

    value = operate_value(@parameters[1], @parameters[2], @parameters[3])

    # Increase / decrease items

    $game_party.gain_item(@parameters[0], value)

    # Popup Window

    text = (value > 0 ? 'Acquired' : 'Lost')

    popup(text, $data_items[@parameters[0]], value)

    # Continue

    return true

  end

end
 

regi

Sponsor

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. ^,^
 

regi

Sponsor

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!
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top