Home > RGSS Script Reference > Window_SaveFile
Window_SaveFile
Inherits from: Window_Base
Description: This window shows the status of an individual save file within the Save or Load scenes.
class Window_SaveFile < Window_Base
# ------------------------------------
attr_reader :filename
attr_reader :selected
# ------------------------------------
def initialize(file_index, filename)
super(0, 64 + file_index % 4 * 104, 640, 104)
self.contents = Bitmap.new(width - 32, height - 32)
@file_index = file_index
@filename = "Save#{@file_index + 1}.rxdata"
@time_stamp = Time.at(0)
@file_exist = FileTest.exist?(@filename)
if @file_exist
file = File.open(@filename, "r")
@time_stamp = file.mtime
@characters = Marshal.load(file)
@frame_count = Marshal.load(file)
@game_system = Marshal.load(file)
@game_switches = Marshal.load(file)
@game_variables = Marshal.load(file)
@total_sec = @frame_count / Graphics.frame_rate
file.close
end
refresh
@selected = false
end
# ------------------------------------
def refresh
self.contents.clear
self.contents.font.color = normal_color
name = "File #{@file_index + 1}"
self.contents.draw_text(4, 0, 600, 32, name)
@name_width = contents.text_size(name).width
if @file_exist
for i in 0...@characters.size
bitmap = RPG::Cache.character(@characters[i][0], @characters[i][1])
cw = bitmap.rect.width / 4
ch = bitmap.rect.height / 4
src_rect = Rect.new(0, 0, cw, ch)
x = 300 - @characters.size * 32 + i * 64 - cw / 2
self.contents.blt(x, 68 - ch, bitmap, src_rect)
end
hour = @total_sec / 60 / 60
min = @total_sec / 60 % 60
sec = @total_sec % 60
time_string = sprintf("%02d:%02d:%02d", hour, min, sec)
self.contents.font.color = normal_color
self.contents.draw_text(4, 8, 600, 32, time_string, 2)
self.contents.font.color = normal_color
time_string = @time_stamp.strftime("%Y/%m/%d %H:%M")
self.contents.draw_text(4, 40, 600, 32, time_string, 2)
end
end
# ------------------------------------
def selected=(selected)
@selected = selected
update_cursor_rect
end
# ------------------------------------
def update_cursor_rect
if @selected
self.cursor_rect.set(0, 0, @name_width + 8, 32)
else
self.cursor_rect.empty
end
end
end
|
File_Index: The index of the save file to which this window points, where index 0 is the first save file.
Filename: The filename of the save file to which this window points, by default, this is the string "Save", plus the file index + 1, plus ".rxdata".
Time_Stamp: The time at which the file to which this window points was last saved.
File_Exist: This flag is set to true if the file to which this winodw points exists.
Characters: An array of the characters in the party at the time the file to which this window points was last saved.
Frame_Count: The total number of frames that had been shown at the time the file to which this window points was last saved.
Game_System: Not used in this class, as far as I can tell.
Game_Switches: Not used in this class, as far as I can tell.
Game_Variables: Not used in this class, as far as I can tell.
Total_Sec: The total number of seconds elapsed since the start of the saved game to which this window points. Found by taking the value of @frame_count and dividing it by the current frame rate.
Selected: This flag is set to true if the user has selected this window on the save or load screen to which this window belongs.
Name_Width: The width of the string "File [number]". Used to determine the width of the cursor when the window is selected.
Initialize
Arguments:
File_Index: The index of the save file to which this window points, where index 0 is the first save file.
Filename: The filename of the save file to which this window points, by default, this is the string "Save", plus the file index + 1, plus ".rxdata".
Local Variables: None
How it Works: This method initializes the window. The window's x coordinate is set to 0, its y coordinate is set to 64, plus the file's index multiplied by 104. The width of the window is set to 640, and the height is set to 104. The value of @file_index is set to the index of the save file to which this window points, with the first file being index 0. @filename instance variable is initialized to the name of the save file to which this window points. The @time_stamp instance variable is initialized to a dummy value for now, since it's not know at this point whether the file to which the window points exists. The @file_exist flag is set to true if the file "Save[n].rxdata" exists, where n is the file index, plus one. The next part of the method is only executed if the @file_exist flag is set. The file is opened, and the @time_stamp instance variable is set to the time at which the file was last modified. The next few statements load various instance variables. I'm not certain why the system, switches, and variables from the file are loaded, since they don't appear to be used in this class. @characters is used to determine which character graphics to show in the window, and @frame_count is used to determine the total number of seconds played by dividing it by the current frame rate. Note that because of this calculation method, the play time shown may differ from the actual play time if the current frame rate differs from the average frame rate while playing the saved game. Once the play time is determined, the file is closed and the window's contents are drawn using the refresh method.
Refresh
Arguments: None
Local Variables: None
How it Works: This method draws the window's contents. The contents of the window are first cleared, and the font color is changed to the normal color. The name is then set to be "File n", where n is the index of the file to which this window points, plus one. This name is then drawn at x coordinate 4 and y coordinate 0. The value of @name_width is set to the width of the string "File n", in pixels. This is used to determine the width of the cursor in the update_cursor_rect method. The rest of the method is only executed if the @file_exist flag is true. For each character in the @characters array, the character's sprite is stored in the bitmap variable. The next two statments isolate the down-facing stationary frame from the group of sixteen animations within the character set file. The x coordinate of the bitmap is set to be 300, minus 32 times the size of the character array, plus the index times 64, minus the width of the animation divided by 2. The sprite bitmap is then drawn at the x coordinate determined above, and the y coordinate equal to 68 minus the character height. Next, the total seconds of play time for the save file is split into hours, minutes, and seconds for display purposes. The number of hours is the total seconds divided by 3600. The number of minutes is the total seconds divided by 60, modded by 60. The number of seconds is the total number of seconds modded by 60. These values are formatted into a string by the time_string = sprintf("%02d:%02d:%02d", hour, min, sec) statement. The formatted string is in the format xx:yy:zz, where xx is the number of hours, yy is the number of minutes, and zz is the number of seconds. This string is drawn in the normal color at x coordinate 4 and y coordinate 8 with a width of 600, and a height of 32. The optional argument "2" in the draw_text statement means that the string will be drawn right-aligned in its bounding box, so the string will appear on the right side of the window, despite its low x coordinate. The time string is then set to be @time_stamp.strftime("%Y/%m/%d %H:%M"). The "strf" in "strftime" means "string formatted". In this case, the value will look like "yy/mm/dd hh:mm", where yy is the year in which the file was last saved, mm is the month, dd is the day, hh is the hour, and mm is the minute. This string is drawn at x coordinate 4 and y coordinate 40, with the same bounding box and alignment as the first time string. Thus, the last save date will appear directly underneath the play time for this file.
Selected=
Arguments:
Selected: The new value of the @selected instance variable.
Local Variables: None
How it Works: This method sets the value of the @selected instance variable to the value passed to this method, and then calls the update_cursor_rect method to reflect this change.
Update_Cursor_Rect
Arguments: None
Local Variables: None
How it Works: This method updates the cursor rectangle. If the @selected instance variable is true, then the cursor rectangle's position is drawn at x and y coordinates 0. The width of the rectangle is equal to the value of @name_width as determined in the refresh method. If @selected is false, then the cursor rectangle is set to be empty (invisible).
|
|