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

 Username:
 Password:
   Not a member? Register!



Home > RGSS Script Reference > Window_NameEdit

Window_NameEdit


Inherits from: Window_Base

Description: This window shows the name the player has entered as he selects letters from the name input window.

Code


class Window_NameEdit < Window_Base
# ------------------------------------
attr_reader   :name
attr_reader   :index
# ------------------------------------
  def initialize(actor, max_char)
    super(0, 0, 640, 128)
    self.contents = Bitmap.new(width - 32, height - 32)
    @actor = actor
    @name = actor.name
    @max_char = max_char
    name_array = @name.split(//)[0...@max_char]
    @name = ""
    for i in 0...name_array.size
      @name += name_array[i]
    end
    @default_name = @name
    @index = name_array.size
    refresh
    update_cursor_rect
  end
# ------------------------------------
  def restore_default
    @name = @default_name
    @index = @name.split(//).size
    refresh
    update_cursor_rect
  end
# ------------------------------------
  def add(character)
    if @index < @max_char and character != ""
      @name += character
      @index += 1
      refresh
      update_cursor_rect
    end
  end
# ------------------------------------
  def back
    if @index > 0
      name_array = @name.split(//)
      @name = ""
      for i in 0...name_array.size-1
        @name += name_array[i]
      end
      @index -= 1
      refresh
      update_cursor_rect
    end
  end
# ------------------------------------
  def refresh
    self.contents.clear
    name_array = @name.split(//)
    for i in 0...@max_char
      c = name_array[i]
      if c == nil
        c = "Q"
      end
      x = 320 - @max_char * 14 + i * 28
      self.contents.draw_text(x, 32, 28, 32, c, 1)
    end
    draw_actor_graphic(@actor, 320 - @max_char * 14 - 40, 80)
  end
# ------------------------------------ 
  def update_cursor_rect
    x = 320 - @max_char * 14 + @index * 28
    self.cursor_rect.set(x, 32, 28, 32)
  end
# ------------------------------------
  def update
    super
    update_cursor_rect
  end
end

Properties


Actor: The actor whose name is being entered.

Name: The name the player has entered so far.

Name_Array: This variable is used to split the name into substrings of one letter. The string is then reconstituted into a single string in the variable @name with whatever change the player made (entering or deleting a letter).

Default_Name: The default name for the character, as defined in the database.

Max_Char: The maximum number of letters allowed in the name.

Methods


Initialize

Arguments:
Actor: The actor whose name is being changed.
Max_Char: The maximum number of letters allowed in the name.
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 set to 0. The width of the window is set to 640, and height is set to 128. The @actor, @name, and @max_char instance variables are initialized to the Id of the actor whose name is being changed, the actor's default name, and the maximum number of letters allowed, respectively. The next set of statements make it so that the actor's default name is shown in the window when it first appears. The character's default name is split into substrings of one character each, and the for loop reconstitutes the string in the @name variable. The value @default_name is set equal to the character's name (for use in the restore_default method), and the window and cursor rectangle are updated.

Restore_Default

Arguments: None
Local Variables: None

How it Works: This method restores the name shown to the default name. First, the value of @name to the value stored in @default_name. The cursor's index is set equal to the size of the default name string, in characters (remember that the first letter is index 0). The window is then refreshed.

Add

Arguments:
Character: The letter to be added to the name.
Local Variables: None

How it Works: This method adds a new character to the name. It is called when the player selects a letter from the name input window. If the index is less than the maximum number of characters, and the character chosen isn't "" (null string), then the character passed to this method is appended to the end of the string, and the index is advanced by one. The window contents and cursor rectangle are then refreshed to reflect the change in the name entered.

Back

Arguments: None
Local Variables: None

How it Works: This method erases the last letter of the name and backs up the cursor one step. The name is split into substrings of one character each and put into @name_array. The main @name string is set to a null string, and then the for loop goes through each letter except the last letter in @name_array and appends it to the end of the @name string. Once the loop is done executing, the @name string will be as it was at the start of the method, sans the last letter. The index is decremented, and the window and cursor rectangle are updated to reflect the change.

Refresh

Arguments: None
Local Variables: None

How it Works: This method draws the contents of the name editing window. The first thing done is that the contents are cleared. The @name string is split into substrings of one character each. The for loop takes each substring and draws it in the name editing window. If the number of substrings is less than the maximum number of letters allowed in the name, the extra spaces are padded by the string "__". The method then draws the character's graphic using the Window_Base#Draw_Actor_Graphic method. The forumla for the coordinates at which both the graphic and letters are drawn depends on the maximum number of letters allowed in the name. The effect is that the contents will be centered within the window.

Update_Cursor_Rect

Arguments:
Local Variables: None

How it Works: This method updates the position of the cursor rectangle. The cursor's x coordinate is determined by using the formula 320 - @max_char * 14 + @index * 28. The forumla is rather complicated because the name display is centered in the name editing window. The cursor's y coordinate is always 32. The cursor's width is always 28, and the cursor's height is always 32.

Update

Arguments: None
Local Variables: None

How it Works: This method updates the window each frame. It calls the superclass method to perform updates common to all windows, and then calls the update_cursor_rect method to update the position of the cursor rectangle. 
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