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.
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
|
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.
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.
|
|