#==============================================================================
# ** Key_Config
#------------------------------------------------------------------------------
# This module holds the key components to be shared elsewhere
#==============================================================================
module Key_Config
@keys = nil
def self.keys=(keys)
@keys = keys
end
def self.keys
return @keys
end
end
#==============================================================================
# ** Window_Command
#------------------------------------------------------------------------------
# This window deals with general command choices.
#==============================================================================
class Window_Command < Window_Selectable
#--------------------------------------------------------------------------
# * Public Instance Variables
#--------------------------------------------------------------------------
attr_accessor :commands
end
#==============================================================================
# ** Window_TextFit
#------------------------------------------------------------------------------
# This window shows some text and it will fit to it.
#==============================================================================
class Window_TextFit < Window_Base
#--------------------------------------------------------------------------
# * Object Initialization
# text : the text you want printed in the window.
#--------------------------------------------------------------------------
def initialize(text = 'Text')
# No need to continue if someone is feeding us bad data.
return if text.nil? || !text.is_a?(String)
# Determine the text dimensions.
temp_bitmap = Bitmap.new(1, 1)
t_width = temp_bitmap.text_size(text).width
t_height = temp_bitmap.text_size(text).height
temp_bitmap.dispose
# Call the window creation code with the adjusted dimensions.
super(0, 0, t_width + 36, t_height + 36)
# Draw the text after initializing the bitmap.
self.contents = Bitmap.new(width - 32, height - 32)
@text = text
refresh
end
#--------------------------------------------------------------------------
# * Refresh
#--------------------------------------------------------------------------
def refresh
self.contents.draw_text(0, 0, width - 32, 32, @text, 1)
end
end
#==============================================================================
# ** Scene_Title
#------------------------------------------------------------------------------
# This class performs the title screen processing.
#==============================================================================
class Scene_Title < Scene_Base
#--------------------------------------------------------------------------
# * Constant Variables
#--------------------------------------------------------------------------
Config = 'Configuration'
Config_File = './Data/Config.rvdata'
#--------------------------------------------------------------------------
# * Create Command Window !OVERRIDE!
#--------------------------------------------------------------------------
def create_command_window
s1 = Vocab::new_game
s2 = Vocab::continue
s3 = Vocab::shutdown
@command_window = Window_Command.new(172, [s1, s2, Config, s3])
@command_window.x = (544 - @command_window.width) / 2
@command_window.y = 288
if @continue_enabled # If continue is enabled
@command_window.index = 1 # Move cursor over command
else # If disabled
@command_window.draw_item(1, false) # Make command semi-transparent
end
@command_window.openness = 0
@command_window.open
# Our soon to be array of configured keys.
keys = nil
if FileTest.exist?(Config_File)
# Clear out the keys as we have an existing config file.
keys = []
# Open said file.
config = File.open(Config_File)
begin
# Populate the array with the saved settings.
config.each_line { |line| keys << line.chop.to_i }
rescue
keys = nil
ensure
# Ensure that the file is closed.
config.close
end
end
Key_Config.keys = keys
end
#--------------------------------------------------------------------------
# * Frame Update !OVERRIDE!
#--------------------------------------------------------------------------
def update
super
@command_window.update
if Input.trigger?(Input::C)
case @command_window.index
when 0 #New game
command_new_game
when 1 # Continue
command_continue
when 2
command_configuration
when 3 # Shutdown
command_shutdown
end
end
end
#--------------------------------------------------------------------------
# * Command: Configuration
#--------------------------------------------------------------------------
def command_configuration
$scene = Scene_Configuration.new
end
end
#==============================================================================
# ** Scene_Configuration
#------------------------------------------------------------------------------
# This class performs the configuration screen processing.
#==============================================================================
class Scene_Configuration < Scene_Base
#--------------------------------------------------------------------------
# * Constant Variables
#--------------------------------------------------------------------------
Config_File = './Data/Config.rvdata'
Confirm_Changes = 'Save Changes?'
Catching_Key = 'Press RMVX key now...'
# The strings that explain what each key does.
Key_Strings = [
'Talk/Confirm',
'Cancel Action',
'Menu',
'Attack',
'Skill',
'Up',
'Down',
'Left',
'Right',
'Default',
'Exit'
]
# When no keys can be found.
Default_Keys = [
Input::A,
Input::B,
Input::C,
Input::X,
Input::Y,
Input::UP,
Input::DOWN,
Input::LEFT,
Input::RIGHT
]
# All the valid input
Keys = [
Input::A,
Input::B,
Input::C,
Input::X,
Input::Y,
Input::Z,
Input::L,
Input::R,
Input::UP,
Input::DOWN,
Input::LEFT,
Input::RIGHT
]
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize
# Our soon to be array of configured keys.
@keys = Key_Config.keys
# Set the keys to the defaults if there was no config file.
@keys = Default_Keys.dup if @keys.nil?
@changing_index = @caught_key = nil
@keys_unsaved = false
# Create our windows
@choices = Window_Command.new(544, strings_from_keys)
@choices.y = 416 / 2 - @choices.height / 2
# This window will only show a piece of text to suggest yes or no saving.
@confirm_text = Window_TextFit.new(Confirm_Changes)
# This window will present a yes no choice to the user.
@confirm_command = Window_Command.new(150, ['Yes', 'No'], 2, 2)
@confirm_command.x = 544 / 2 - @confirm_command.width / 2
@confirm_command.y = 416 / 2 - @confirm_command.height / 2
@confirm_text.x = @confirm_command.x + @confirm_command.width / 2 -
@confirm_text.width / 2
@confirm_text.y = @confirm_command.y - @confirm_text.height
# This window will be displayed when the user is trying to change a key.
@catching_key = Window_TextFit.new(Catching_Key)
@catching_key.x = 544 / 2 - @catching_key.width / 2
@catching_key.y = 416 / 2 - @catching_key.height / 2
@choices.openness = 0
@choices.open
focus_choices
end
#--------------------------------------------------------------------------
# * Frame Update
#--------------------------------------------------------------------------
def update
# If we are polling the user for a key.
if @catching_key.visible
# Loop through to determine what key is being hit.
Keys.each do |key|
# Get the key and break out B.
if Input.trigger?(key)
@caught_key = key
break
end
end
# If we have caught a key
unless @caught_key.nil?
confirm_change_key
# Sorry I had to cheat input was being to fast for me and automatically
# came here.
sleep(0.1)
end
return
end
# If we are trying to exit but not sure if we want to save the
# current configuration.
if @confirm_command.active
@confirm_command.update
if Input.trigger?(Input::C)
case @confirm_command.index
when 0 # Yes
command_confirm_keys
when 1 # No
@keys_unsaved = false
command_exit
end
end
return
end
# If the key configuration screen is up.
if @choices.active
@choices.update
# If we are not trying to catch a key.
if Input.trigger?(Input::C)
case @choices.index
when 0...@choices.commands.size - 2 # Any valid key
command_change_key(@choices.index)
when @choices.commands.size - 2 # Default Keys.
command_default_keys
when @choices.commands.size - 1 # Last Option Exit.
command_exit
end
end
end
end
#--------------------------------------------------------------------------
# * Command: Change Key
#--------------------------------------------------------------------------
def command_change_key(index)
@keys_unsaved = true
@changing_index = index
focus_polling
end
#--------------------------------------------------------------------------
# * Command: Confirm Keys
#--------------------------------------------------------------------------
def command_confirm_keys
save_config
@keys_unsaved = false
command_exit
end
#--------------------------------------------------------------------------
# * Command: Default Keys
#--------------------------------------------------------------------------
def command_default_keys
# Keys do not need to be confirmed as changed and need saving.
@keys_unsaved = false
@keys = Default_Keys.dup
# Recreate the choices command window.
for i in 0...@keys.size
@choices.commands[i] = string_from_key(i)
@choices.draw_item(i)
end
end
#--------------------------------------------------------------------------
# * Command: Exit
#--------------------------------------------------------------------------
def command_exit
# If the keys aren't saved when we try to exit prompt the user.
if @keys_unsaved
focus_confirm
else
$scene = Scene_Title.new
end
end
#--------------------------------------------------------------------------
# * Termination Processing
#--------------------------------------------------------------------------
def terminate
@choices.dispose
@confirm_command.dispose
@confirm_text.dispose
@catching_key.dispose
end
#--------------------------------------------------------------------------
# * Confirm Change Key
#--------------------------------------------------------------------------
def confirm_change_key
# Catch the new key
@keys[@changing_index] = @caught_key
@choices.commands[@changing_index] = string_from_key(@changing_index)
@choices.draw_item(@changing_index)
for i in 0...@keys.size
next if i == @changing_index
if @keys[i] == @caught_key
@choices.commands[i] = empty_string(i)
@choices.draw_item(i)
end
end
# The key that was polled.
@caught_key = nil
focus_choices
end
#--------------------------------------------------------------------------
# * Determine Strings From Keys
#--------------------------------------------------------------------------
def strings_from_keys
total_options = []
for i in 0...Key_Strings.size
unless @keys[i].nil?
total_options << Key_Strings[i].ljust(15) + '---' +
string_from_key_number(@keys[i]).rjust(15)
else
total_options << Key_Strings[i]
end
end
return total_options
end
#--------------------------------------------------------------------------
# * Determine String From Key
#--------------------------------------------------------------------------
def string_from_key(index)
return Key_Strings[index].ljust(15) + '---' +
string_from_key_number(@keys[index]).rjust(15)
end
#--------------------------------------------------------------------------
# * Empty String
#--------------------------------------------------------------------------
def empty_string(index)
return Key_Strings[index].ljust(15) + '---' +
'...'.rjust(15)
end
#--------------------------------------------------------------------------
# * Hand Focus To Key Config
#--------------------------------------------------------------------------
def focus_choices
@choices.opacity = 255
@choices.active = true
unfocus_confirm
unfocus_polling
end
#--------------------------------------------------------------------------
# * Take Focus Away From Key Config
#--------------------------------------------------------------------------
def unfocus_choices
@choices.opacity = 100
@choices.active = false
end
#--------------------------------------------------------------------------
# * Hand Focus To Key Confirmation
#--------------------------------------------------------------------------
def focus_confirm
@confirm_text.visible = true
@confirm_command.visible = @confirm_command.active = true
@confirm_text.refresh
unfocus_choices
unfocus_polling
end
#--------------------------------------------------------------------------
# * Take Focus Away From Key Confirmation
#--------------------------------------------------------------------------
def unfocus_confirm
@confirm_text.visible = false
@confirm_command.visible = @confirm_command.active = false
end
#--------------------------------------------------------------------------
# * Hand Focus To Key Polling
#--------------------------------------------------------------------------
def focus_polling
@catching_key.visible = true
@catching_key.refresh
unfocus_choices
unfocus_confirm
end
#--------------------------------------------------------------------------
# * Take Focus Away From Key Polling
#--------------------------------------------------------------------------
def unfocus_polling
@catching_key.visible = false
end
#--------------------------------------------------------------------------
# * Save The Configuration
#--------------------------------------------------------------------------
def save_config
begin
config = File.open(Config_File, File::CREAT|File::WRONLY)
@keys.each { |key| config.write(key.to_s + "\n") }
rescue
print 'There was an error saving the key configuration.'
ensure
config.close
end
Key_Config.keys = @keys
end
#--------------------------------------------------------------------------
# * String From A Key Number
#--------------------------------------------------------------------------
def string_from_key_number(key_number)
case key_number
when 11
return 'A'
when 12
return 'B'
when 13
return 'C'
when 14
return 'X'
when 15
return 'Y'
when 16
return 'Z'
when 17
return 'L'
when 18
return 'R'
when 8
return 'Up'
when 2
return 'Down'
when 4
return 'Left'
when 6
return 'Right'
else
return 'nil'
end
end
#--------------------------------------------------------------------------
# * Key Number From A String
#--------------------------------------------------------------------------
def key_number_from_string(string)
return string.to_i
end
end