Home > RGSS Script Reference > Window_InputNumber
Window_InputNumber
Inherits from: Window_Base
Description: This is the number input window, shown when the "Input Number" event command is used.
class Window_InputNumber < Window_Base
# ------------------------------------
def initialize(digits_max)
@digits_max = digits_max
@number = 0
dummy_bitmap = Bitmap.new(32, 32)
@cursor_width = dummy_bitmap.text_size("0").width + 8
dummy_bitmap.dispose
super(0, 0, @cursor_width * @digits_max + 32, 64)
self.contents = Bitmap.new(width - 32, height - 32)
self.z += 9999
self.opacity = 0
@index = 0
refresh
update_cursor_rect
end
# ------------------------------------
def number
return @number
end
# ------------------------------------
def number=(number)
@number = [[number, 0].max, 10 ** @digits_max - 1].min
refresh
end
# ------------------------------------
def update_cursor_rect
self.cursor_rect.set(@index * @cursor_width, 0, @cursor_width, 32)
end
# ------------------------------------
def update
super
if Input.repeat?(Input::UP) or Input.repeat?(Input::DOWN)
$game_system.se_play($data_system.cursor_se)
place = 10 ** (@digits_max - 1 - @index)
n = @number / place % 10
@number -= n * place
n = (n + 1) % 10 if Input.repeat?(Input::UP)
n = (n + 9) % 10 if Input.repeat?(Input::DOWN)
@number += n * place
refresh
end
if Input.repeat?(Input::RIGHT)
if @digits_max >= 2
$game_system.se_play($data_system.cursor_se)
@index = (@index + 1) % @digits_max
end
end
if Input.repeat?(Input::LEFT)
if @digits_max >= 2
$game_system.se_play($data_system.cursor_se)
@index = (@index + @digits_max - 1) % @digits_max
end
end
update_cursor_rect
end
# ------------------------------------
def refresh
self.contents.clear
self.contents.font.color = normal_color
s = sprintf("%0*d", @digits_max, @number)
for i in 0...@digits_max
self.contents.draw_text(i * @cursor_width + 4, 0, 32, 32, s[i,1])
end
end
end
|
Digits_Max: The number of digits allowed to be entered in the number input window.
Number: The number currently shown in the window. This value is updated every time the user changes the number shown by pushing the up and down arrow keys.
Initialize
Arguments:
Digits_Max: The maximum number of digits allowed to be entered into the input window.
Local Variables:
Dummy_Bitmap: This bitmap is created to determine the cursor width.
How it Works: This method initializes the window. The digit max instance variable is set to the number of digits passed to this method, and the value of @number is set to 0. The next three statements are somewhat atypical due to the characteristics of this window.
The automatic cursor setup provided by the Window_Selectable class is not suitable for this window, so it inherits from Window_Base instead, and constructs the cursor explicitly. Thus, it creates the dummy_bitmap, and uses the bitmap to test the width in pixels of the character "0" in the current font. The width of the cursor is set to this value. There is no longer any use for the dummy bitmap, so it is disposed. The x and y coordinates of the window are both set to 0, but these are just dummy values, since the actual coordinates of the number input window will be set at the time the window is shown, based on the position of the message window. The width of the window is 32, plus (the cursor width x the maximum number of digits). The height of the window is always 64. Now, the "real" window bitmap that will be used for actual processing is created. The z-index of the window is set to 9999, in order to be shown above the message window, which has a z-index of 9998 by default. The self.opacity = 0 statement may seem curious at first, but keep in mind that this window isn't really supposed to be shown as a window at all. Rather, it is shown on top of the message window as an integrated unit with the message window, so the window's borders are hidden with this statement to maintain the illusion. The window's contents and cursor are then drawn.
Number
Arguments: None
Local Variables: None
How it Works: This method returns the value of the @number instance variable.
Number=
Arguments:
Number: The new number to set.
Local Variables:
How it Works: This is a custom method to set the value of the @number instance variable. It sets the @number instance variable equal to the number passed to this method. The first statement makes sure the number is not negative and not greater than (10x - 1), where x is the value of the @digits_max instance variable. The second statement refreshes the window, so that the change is reflected immediately.
Update_Cursor_Rect
Arguments: None
Local Variables: None
How it Works: This method updates the position of the cursor rectangle. The new x coordinate of the cursor rectangle is equal to the cursor width times the index, the y coordinate is always 0. The width is equal to the @cursor_width instance variable, and the height is always 32.
Update
Arguments: None
Local Variables:
Place: The place value multiplier to use when adjusting the number. This value is equal to 10x where x is (maximum number of digits - 1) times the cursor index.
n: The value of the of the digit currently selected by the cursor.
How it Works: This method responds to user input within the window and updates the @number instance variable, which in turn refreshes the window. It first calls the superclass method in order to do updating common to all windows. Next, the input response routines are enumerated. If the user's input is the up arrow or down arrow, then the cursor sound effect is played, and then the new value of the number is determined. This code is a little tricky, so please read carefully. The value of place is determined by takning 10 to the power of the forumla (@digits_max - 1 - @index). Remember that unlike other windows, the cursor index is lower on the right side of the window rather than the left. The n = @number / place % 10 statement acquires the current value of the digit currently selected by the cursor. The @number -= n * place statement resets that digit to 0 temporarily, so that the equation that changes the value of that digit has a uniform starting point, regardless of the current value of the digit. If the user is pressing the up key, n is incremented, and if the value is now 10, it is reset to 0. If the user is pressing the down key, then the value of n is decremented, and if the value is now -1, it is reset to 9. Then, the new value of the digit is added back into the number to balance out the temprary reset to 0 above. The next two conditionals process cursor movement to the left or right, provided the maximum number of digits is two or greater. If the user's input is the right key, then the cursor sound effect is played, and the index is incremented. If the user's input is left, then the index is decremented. The modulous operations on the new index are to prevent the cursor index from straying outside the range of (0..@digits_max-1. Finally, the cursor rectangle is updated.
Refresh
Arguments: None
Local Variables: None
How it Works: This method updates the window's contents. First, the existing contents are cleared, and the font is set to the normal color. The value show in the window is determined by the s = sprintf("%0*d", @digits_max, @number) statement. The "%0*d" means that a variable number of integers will be shown, determined by the value of @digits_max. The actual number to show is determined by @number. The final for loop draws the digits of the number, one at a time.
|
|