Auto-Sizing Text Windows
by RPG Advocate
saved from Phylomortis.Com
Introduction
This script adds the ability to show text windows whose dimensions conform to the text inside them. The width of the window is determined by the width of the longest line. The height is determined by the number of lines. To use this script, just add the Window_Autosize class provided after all the other window classes. The way the text box is a bit cumbersome. You initialize the window with an array of lines. The class does the processing from there. Please don't initialize the window with an empty array. This will cause the game to show an error message, as shown in the script. Also, be aware that the initial x and y coordinate of 0 are just dummy values. To use this script effectively, you should either specify explicit x and y coordinates for the window or create an algorithm to determine them, if you need to display the window in a different location depending on the circumstances.
Script
AutoSizing Text Windows
Code:
# Auto-Sizing Text Windows
# by RPG Advocate
#==============================================================================
# ** Window_Autosize
#------------------------------------------------------------------------------
# This message window autosizes to suit the text displayed.
#==============================================================================
class Window_Autosize < Window_Base
#--------------------------------------------------------------------------
# * Object Initialization
# lines : lines
#--------------------------------------------------------------------------
def initialize(lines)
@lines = []
@lines = lines
@line_width = []
if @lines.size == 0
print("Error: Auto-sizing windows must have at least one line")
exit
end
@tester = Window_Base.new(0, 0, 640, 480)
@tester.contents = Bitmap.new(@tester.width - 32, @tester.height - 32)
@tester.contents.font.name = "Arial"
@tester.contents.font.size = 18
max = 0
for i in 0..@lines.size - 1
t = @lines[i]
@line_width[i] = @tester.contents.text_size(t).width
if @line_width[i] > max
max = @line_width[i]
end
end
w = max + 40
h = @lines.size + 1
h *= 32
@tester.dispose
super(0, 0, w, h)
self.contents = Bitmap.new(width - 32, height - 32)
self.contents.font.name = "Arial"
self.contents.font.size = 18
refresh
end
#--------------------------------------------------------------------------
# * Refresh
#--------------------------------------------------------------------------
def refresh
self.contents.clear
self.contents.font.color = normal_color
self.opacity = 255
self.back_opacity = 255
self.contents_opacity = 255
for i in 0..@lines.size - 1
t = @lines[i]
self.contents.draw_text(4, i*32, @line_width[i], 32, t)
end
end
#--------------------------------------------------------------------------
# * Frame Update
#--------------------------------------------------------------------------
def update
super
end
end
Quote:Also, be aware that the initial x and y coordinate of 0 are just dummy values. To use this script effectively, you should either specify explicit x and y coordinates for the window or create an algorithm to determine them, if you need to display the window in a different location depending on the circumstances.
In a nutshell, the window is defaulted to the top left corner of the screen as (like you said) the x and y position is set to 0,0. Either you can replace the x/y values of 0,0 with values as noted here...
From
Code:
super(0, 0, w, h)
to
Code:
super(20, 40, w, h)
Or better yet, replace them with variables like x and y, and edit the initialization method to match:
Code:
#--------------------------------------------------------------------------
# * Object Initialization
# x : x-position
# y : y-position
# lines : lines
#--------------------------------------------------------------------------
def initialize(x,y,lines)
and
Code:
super(x, y, w, h)
DerVVulfman's 'regular Joe' account for testing and permissions use.
Who watches the Watchmen?
Okay... this is the rewrite of the initialize method I mentioned:
Code:
def initialize(x,y,lines)
@lines = []
@lines = lines
@line_width = []
if @lines.size == 0
print("Error: Auto-sizing windows must have at least one line")
exit
end
@tester = Window_Base.new(0, 0, 640, 480)
@tester.contents = Bitmap.new(@tester.width - 32, @tester.height - 32)
@tester.contents.font.name = "Arial"
@tester.contents.font.size = 18
max = 0
for i in 0..@lines.size - 1
t = @lines[i]
@line_width[i] = @tester.contents.text_size(t).width
if @line_width[i] > max
max = @line_width[i]
end
end
w = max + 40
h = @lines.size + 1
h *= 32
@tester.dispose
super(x, y, w, h)
self.contents = Bitmap.new(width - 32, height - 32)
self.contents.font.name = "Arial"
self.contents.font.size = 18
refresh
end
And this is a map event 'Script Call' I wrote.
Code:
Window_Autosize.new(20,20,
["Here","There"])
Rather than the window showing up at 0,0, it shows up at 20,20 (in pixels). The window resizes to fit the text... two small one-word lines.
Up is down, left is right and sideways is straight ahead. - Cord "Circle of Iron", 1978 (written by Bruce Lee and James Coburn... really...)
Thanks i got it to work, but the message stays on the screen for a few seconds and then disappears, also it only shows in the top right screen. Its just a script message its not the actual 'show message' text, i need the autosize in the show message window not as a script, or is this script only for script messages?? Your help is much obliged :D
Again, I only set a position of just 20 x 20 in that sample event code. If I wanted it near the bottom left, I coulda used 600 and 400 instead of 20 and 20.
Code:
Window_Autosize.new(600,400,
["Here","There"])
It's all based on a 640x480 pixel area.
The script was written in 2005 and was a basis that people used for their own custom message systems. It could be inserted into a custom message script if one knows what they're doing. By itself, it does make for a nice popup window.
Actually, that was unexpected. Guess I don't have to look for one.
There are other message scripts that have such a function. Personally, I use the UMS (Universal Message System) by ccoa. You can find both RPGMaker XP and RPGMaker VX versions in this forum. You can also check out the Forum Scripts Listings thread (stickied in here) that gives you an index of all the scripts we have... including message scripts.
Up is down, left is right and sideways is straight ahead. - Cord "Circle of Iron", 1978 (written by Bruce Lee and James Coburn... really...)