Home > RGSS Script Reference > Window_Selectable
Window_Selectable
Inherits from: Window_Base
Description: This class is the base class for most of the windows that allow you to select something. It has support for multicolumn windows as well.
class Window_Selectable < Window_Base
# ------------------------------------
attr_reader :index
attr_reader :help_window
# ------------------------------------
def initialize(x, y, width, height)
super(x, y, width, height)
@item_max = 1
@column_max = 1
@index = -1
end
# ------------------------------------
def index=(index)
@index = index
if self.active and @help_window != nil
update_help
end
update_cursor_rect
end
# ------------------------------------
def row_max
return (@item_max + @column_max - 1) / @column_max
end
# ------------------------------------
def top_row
return self.oy / 32
end
# ------------------------------------
def top_row=(row)
if row < 0
row = 0
end
if row > row_max - 1
row = row_max - 1
end
self.oy = row * 32
end
# ------------------------------------
def page_row_max
return (self.height - 32) / 32
end
# ------------------------------------
def page_item_max
return page_row_max * @column_max
end
# ------------------------------------
def help_window=(help_window)
@help_window = help_window
if self.active and @help_window != nil
update_help
end
end
# ------------------------------------
def update_cursor_rect
if @index < 0
self.cursor_rect.empty
return
end
row = @index / @column_max
if row < self.top_row
self.top_row = row
end
if row > self.top_row + (self.page_row_max - 1)
self.top_row = row - (self.page_row_max - 1)
end
cursor_width = self.width / @column_max - 32
x = @index % @column_max * (cursor_width + 32)
y = @index / @column_max * 32 - self.oy
self.cursor_rect.set(x, y, cursor_width, 32)
end
# ------------------------------------
def update
super
if self.active and @item_max > 0 and @index >= 0
if Input.repeat?(Input::DOWN)
if (@column_max == 1 and Input.trigger?(Input::DOWN)) or
@index < @item_max - @column_max
$game_system.se_play($data_system.cursor_se)
@index = (@index + @column_max) % @item_max
end
end
if Input.repeat?(Input::UP)
if (@column_max == 1 and Input.trigger?(Input::UP)) or
@index >= @column_max
$game_system.se_play($data_system.cursor_se)
@index = (@index - @column_max + @item_max) % @item_max
end
end
if Input.repeat?(Input::RIGHT)
if @column_max >= 2 and @index < @item_max - 1
$game_system.se_play($data_system.cursor_se)
@index += 1
end
end
if Input.repeat?(Input::LEFT)
if @column_max >= 2 and @index > 0
$game_system.se_play($data_system.cursor_se)
@index -= 1
end
end
if Input.repeat?(Input::R)
if self.top_row + (self.page_row_max - 1) < (self.row_max - 1)
$game_system.se_play($data_system.cursor_se)
@index = [@index + self.page_item_max, @item_max - 1].min
self.top_row += self.page_row_max
end
end
if Input.repeat?(Input::L)
if self.top_row > 0
$game_system.se_play($data_system.cursor_se)
@index = [@index - self.page_item_max, 0].max
self.top_row -= self.page_row_max
end
end
end
if self.active and @help_window != nil
update_help
end
update_cursor_rect
end
end
|
Item_Max: The maximum number of items the window can hold.
Column_Max: The number of columns used to display the items in the window.
Index: The ID of the item selected by the window's cursor.
Top_Row: The row number of the top row which is currently displayed in the window, where 0 is the topmost row of items.
Help_Window: A Window_Help object associated with the window. Some selectable windows don't use this supplementary window.
Initialize
Arguments:
X: The x coordinate of the window's upper-left pixel.
Y: The y coordinate of the window's upper-left pixel.
Width: The width of the window in pixels.
Height: The height of the window in pixels.
Local Variables: None
How it Works: This method initializes the window. The superclass method is called to create the window with the specified upper-left pixel coordinates, width, and height. The @item_max, @column_max, and @index variables to dummy values that will almost certainly be changed by the class that inherits from this one.
Index=
Arguments:
Index: The new value of the index.
Local Variables: None
How it Works: This method sets the @index instance variable to a new value, which will change which item the cursor has currently selected. It first sets the @index instance variable equal to the argument passed to this method. If the window is active and the supplementary help window object is not nil, then the update_help method is called in order to change the help window text in response to the movement of the cursor. Finally, the update_cursor_rect method is called to update the position of the cursor graphic.
Row_Max
Arguments: None
Local Variables: None
How it Works: This method determines the maximum number of rows for the window. This method returns the number of maximum number of items, plus the maximum number of columns, minus 1, divided by the maximum number of columns. Note that this formula is necessary because a partial row needs to be counted as a full row for purposes of this calculation.
Top_Row
Arguments: None
Local Variables: None
How it Works: This method returns the row number of the top row being shown in the window, based on how far down the window has been scrolled. The method returns the y offset of the window divided by 32.
Top_Row=
Arguments:
Row: The new top row (with 0 being the absolute top row).
Local Variables: None
How it Works: This method scrolls the window so that the row number passed to this method becomes the top row shown in the window. If the row number passed to this method is less than 0, it is treated as 0. If the row number passed to this method is greater than the maximum number of rows, minus 1, then the value is treated as the number of rows, minus 1. Once the bounding checks have been done, the window's oy value is set to 32 times the row value.
Page_Row_Max
Arguments: None
Local Variables: None
How it Works: This method returns the maximum number of rows that can be displayed in the window at once. This value is equal to the height of the window minus 32, divided by 32.
Page_Item_Max
Arguments: None
Local Variables: None
How it Works: This method returns the maximum number of items that can be displayed in the window at once. This value is equal to the page row max times the column max.
Help_Window=
Arguments:
Help_Window: A Window_Help object.
Local Variables: None
How it Works: This method updates the status of the supplementary help window associated with this window. The value of the @help_window instance variable is set equal to the Window_Help object passed to this method, and if the main window is active and the @help_window isn't equal to nil, then the update_help method is called to update the help window's contents.
Update_Cursor_Rect
Arguments: None
Local Variables:
Row: The row on which the cursor will be shown.
How it Works: This method updates the position of the cursor graphic. If the window's @index instance variable is less than 0, then the cursor is "outside the window" and thus its rectangle is set to be empty (in visible). If the value of @index is 0 or more, then the next thing to do is determine the row on which the cursor will be displayed and scroll the window, if necessary. To determine the row on which the cursor is displayed, the value of the @index instance variable is divided by the maximum number of columns. The if row < self.top_row clause determines if the window needs to be scrolled up. If it does, then the value of @top_row is set to the cursor's row. Similarly, the if row > self.top_row + (self.page_row_max - 1) clause checks to see if the window needs to be scrolled down. If it does, then @top_row is set equal to the cursor's row, minus one less than the page row maximum. The next statement sets the cursor's width. By default, this is equal to the window's width, divided by the maximum number of columns, minus 32. The next two statements determine the x and y coordinates for the cursor rectangle. Finally, the self.cursor_rect.set(x, y, cursor_width, 32) statement actually draws the cursor rectangle in its new location.
Update
Arguments: None
Local Variables: None
How it Works: This method updates the window each frame, and responds to user input. The call to super performs updating common to all window classes. The majority of the remainder of this method is only executed if the value of @item_max and @index are both greater than 0, since it @item_max is 0, there are no items in the menu, and if @index is 0, the cursor isn't selecting anything in the window. If the conditions have been met, then the input is evaluated. If the user has pressed the down arrow, then the method checks to see if the maximum number of columns is 1 or the index is less than the item max minus the column max. The first part of the condition is there because windows with only one column can "wrap around", that is, if the user pushes the down arrow when the last item on the list is selected, then the window will jump to the first item in the list. Windows with two or more columns won't wrap around in this way, so the condition will be false if the cursor has selected an item on the last row of the window. If either of these conditions is true, the cursor sound effect is played and the cursor's index is increased by the column max and then modded by the item max (to account for the "wrap around" effect just described). The response to the up arrow input also has this wrap-around effect. If the column max is 1 or the index is greater than or equal to the column max, then the index is decreased by the column max and modded by the item max to account for the wrap-around effect. If the user has pressed the right arrow key, then the method checks to see if the column max is at least 2 (since pressing right on a one-column window does nothing) and makes sure that the value of @index is less than the item max minus 1 (necessary to not allow for selecting empty columns in the last row of a multicolumn window). If these conditions are satisfied, then the index is incremented. If the user pressed the left arrow key, then the method checks to see if the window has two columns and the index is greater than 0. If both these conditions are true, then the index is decremented. If the user has pressed the "R" key, then the method checks to see if the window has been scrolled down as far as it can be. If it hasn't, then the index is increased by the page item max, but if this would cause the index to exceed the value of @item_max minus 1, then the index is set to this value instead. The top row is then increased by the number of rows that can be displayed on a page (note that if this would cause the top row to exceed the value such that there would be blank rows in the window, the problem will be corrected by the top_row= method, described above). The code for when the user has pressed "L" is similar. The index is decreased by the number of items on a page, but if this would cause the index to go below 0, the index is treated as 0 instead. The value of @top_row is reduced by the page row max, with a similar correction as described above. If the supplementary help window needs to be updated, then it is updated, and finally, the cursor rectangle is updated.
|
|