Submit Your Article Guild Wars 2 Forum RPG Maker VX.com
 
RPG Maker
 

 Username:
 Password:
   Not a member? Register!



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.

Code


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

Properties


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.

Methods


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. 
Syntax
@
@@
$
alias
[array index]
attr_accessor
attr_reader
attr_writer
class
def
do
ensure
for
if
[iterator]
key => value
new
next
nil
redo
require
return
rescue
self
super
undef
unless
until
while
yield

Classes
Arrow_Actor
Arrow_Base
Arrow_Enemy
Game_Actor
Game_Actors
Game_BattleAct
Game_Battler
Game_Character
Game_Common
Game_Enemy
Game_Event
Game_Map
Game_Party
Game_Picture
Game_Player
Game_Screen
Game_SlfSwitch
Game_Switches
Game_System
Game_Troop
Game_Variables
Interpreter
Scene_Debug
Scene_End
Scene_Equip
Scene_File
Scene_Gameover
Scene_Item
Scene_Load
Scene_Map
Scene_Menu
Scene_Name
Scene_Save
Scene_Shop
Scene_Skill
Scene_Status
Scene_Title
Sprite_Battler
Sprite_Character
Sprite_Picture
Sprite_Timer
Spriteset_Battle
Spriteset_Map
Window_Base
Window_Battleresult
Window_Battlestatus
Window_Command
Window_DebugLeft
Window_DebugRight
Window_EquipItem
Window_EquipLeft
Window_EquipRight
Window_Gold
Window_Help
Window_InputNumb
Window_Item
Window_MenuStatus
Window_Message
Window_NameEdit
Window_NameInput
Window_PartyCom
Window_PlayTime
Window_SaveFile
Window_Selectable
Window_ShopBuy
Window_ShopCom
Window_ShopNum
Window_ShopSell
Window_ShopStatus
Window_Skill
Window_SkillStatus
Window_Status
Window_Steps
Window_Target

Other
Class Hierarchy
Global Variables


RPG RPG Revolution
RPG RPG Revolution is your #1 stop for game development and console RPG games, as well as those created by people like you. Link to us to support us, so we may grow to be better website community for you.

RPG RPG Revolution is an Privacy Policy and Legal