Home > RGSS Script Reference > Window_Command
Window_Command
Inherits from: Window_Selectable
Description: This window shows one column of commands. Unlike other classes that inherit from Window_Selectable, it is designed to be flexible, allowing a good-looking window to be easily constructed for any situation where you might want to prompt the user to select from between 2 and 8 static commands.
class Window_Command < Window_Selectable
# ------------------------------------
def initialize(width, commands)
super(0, 0, width, commands.size * 32 + 32)
@item_max = commands.size
@commands = commands
self.contents = Bitmap.new(width - 32, @item_max * 32)
refresh
self.index = 0
end
# ------------------------------------
def refresh
self.contents.clear
for i in 0...@item_max
draw_item(i, normal_color)
end
end
# ------------------------------------
def draw_item(index, color)
self.contents.font.color = color
rect = Rect.new(4, 32 * index, self.contents.width - 8, 32)
self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
self.contents.draw_text(rect, @commands[index])
end
# ------------------------------------
def disable_item(index)
draw_item(index, disabled_color)
end
end
|
Commands: An array of strings representing the commands from which the user can select.
Initialize
Arguments:
Width: The width of the command window, in pixels.
Commands: An array of strings representing the commands from which the user can select.
Local Variables: None
How it Works: This method initializes the window. The x and y coordinates of the upper-left pixel of the window are both set at 0, though in most cases these will be changed by the class manipulating this object. The width is set according to the first argument passed to this method, and the height is set to 32 pixels, plus an additional 32 pixels for each command in the array passed as the second argument to this method. The item max of the window is set to the number of commands in the array passed to this method, and the @commands array is set equal to the second argument to this method. The index is then set to 0, so the cursor will appear at the top of the command window.
Refresh
Arguments: None
Local Variables: None
How it Works: This method draws the contents of the window. Like most other Refresh methods, the first thing this method does is clear the window's current contents. For each item in the array of strings in @commands, the method calls the draw_item method with that index and the normal text color. Note that this method can't draw any items disabled. This must be done seperately within the class that is manipulating this object.
Draw_Item
Arguments:
Index: The index of the item to be drawn.
Color: A Color object representing the text color of the item to be drawn.
Local Variables: None
How it Works: This method draws an individual command within the command window. The first thing the method does is set the font color to the color passed to the method. It then determines the drawing rectangle that will be used to place the command within the window. The x coordinate of the drawing rectangle is always 4. The y coordinate depends on the index of the command being drawn. For index zero, the y coordinate is 0. For every index above zero, the y coordinate is increased by 32. The width of the drawing rectangle is the width of the window bitmap, minus 8 pixels. The height of the drawing rectangle is always 32 pixels. I'm not particularly certain of the reason for the self.contents.fill_rect(rect, Color.new(0, 0, 0, 0)) statement, as it simply fills the bitmap with an invisible color. The final command draws the text of the command at the array index of @commands passed to this method using the rectangle defined above.
Disable_Item
Arguments:
Index: The index of the item to be disabled.
Local Variables: None
How it Works: This method is a shortcut for saying "draw_item(index, Color.new(255, 255, 255, 128)" (or whatever color you've set the to be the disabled color).
|
|