#==============================================================================
# ** Scene_Name
#------------------------------------------------------------------------------
#  This class performs name input screen processing.
#==============================================================================

class Scene_Name
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    # Make windows
    @edit_window = Window_NameEdit.new
    @input_window = Language.create_input
    # Execute transition
    Graphics.transition
    # Main loop
    while true
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # only for legacy funcs
      Audio.update
      # Frame update
      update
      # Abort loop if screen is changed
      if $scene != self
        break
      end
    end
    # Prepare for transition
    Graphics.freeze
    # Dispose of windows
    @edit_window.dispose
    @input_window.dispose
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Update windows
    @edit_window.update
    @input_window.update
    # If B button was pressed
    if Input.repeat?(Input::CANCEL)
      # If cursor position is at 0
      if @edit_window.index == 0
        return
      end
      # Play cancel SE
      $game_system.se_play($data_system.cancel_se)
      # Delete text
      @edit_window.back
      return
    end
    # If C button was pressed
    if Input.trigger?(Input::ACTION)
      # If cursor position is at [OK]
      if @input_window.character == nil
        # Cursor is at OK or mode buttons
        slot = @input_window.mode_button_slot
        if slot == -1
          # Cursor is at OK
          # If name is empty
          if @edit_window.name == ""
            # Return to default name
            @edit_window.restore_default
            # If name is empty
            if @edit_window.name == ""
              # Play buzzer SE
              $game_system.se_play($data_system.buzzer_se)
              return
            end
            # Play decision SE
            $game_system.se_play($data_system.decision_se)
            return
          end
          # Change actor name
		  if $game_switches[91]
		    $game_temp.countdown_password = @edit_window.name
		  else
            $game_oneshot.player_name = @edit_window.name
		  end
          # Play decision SE
          $game_system.se_play($data_system.decision_se)
          # Switch to map screen
          $scene = Scene_Map.new
          return
        end
        # Cursor is on mode buttons
        $game_system.se_play($data_system.decision_se)
        @input_window.cycle_mode(slot)
        return
      end
      # If cursor position is at maximum
      if @edit_window.index == @edit_window.max_char
        # Play buzzer SE
        $game_system.se_play($data_system.buzzer_se)
        return
      end
      # If text character is empty
      if @input_window.character == ""
        # Play buzzer SE
        $game_system.se_play($data_system.buzzer_se)
        return
      end
      # Play decision SE
      $game_system.se_play($data_system.decision_se)
      # Add text character
      @edit_window.add(@input_window.character)
      return
    end
  end
end
