Holy87

controller-settings

Dec 20th, 2016
164
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #===============================================================================
  2. # Personalizzazione controller di Holy87
  3. # Difficoltà utente: ★
  4. # Versione 1.0
  5. # Licenza: CC. Chiunque può scaricare, modificare, distribuire e utilizzare
  6. # lo script nei propri progetti, sia amatoriali che commerciali. Vietata
  7. # l'attribuzione impropria.
  8. #===============================================================================
  9. # Questo script vi permette di utilizzare aggiungere nelle opzioni di gioco due
  10. # nuovi comandi che permetteranno al giocatore di configurare a piacimento i
  11. # tasti del proprio controller e la potenza della vibrazione.
  12. #===============================================================================
  13. # Istruzioni: inserire lo script sotto Materials, prima del Main e sotto gli
  14. # script XInput e Opzioni di gioco. I due script sono necessari per il corretto
  15. # funzionamento, pena crash all'avvio.
  16. # Sotto è possibile configurare i testi per le voci e i comandi personalizzabili.
  17. #===============================================================================
  18.  
  19. #===============================================================================
  20. # ** Impostazioni dello script
  21. #===============================================================================
  22. module ControllerSettings
  23.   #--------------------------------------------------------------------------
  24.   # * Testi per il menu Opzioni
  25.   #--------------------------------------------------------------------------
  26.   CONTROLLER_COMMAND = 'Configura Game Pad'
  27.   CONTROLLER_HELP = 'Imposta i comandi del controller PC'
  28.   VIBRATION_COMMAND = 'Vibrazione'
  29.   VIBRATION_HELP = 'Imposta la potenza della vibrazione del controller'
  30.   RESET_COMMAND = 'Ripristina comandi'
  31.   NO_KEY_SET  = 'Nessun pulsante assegnato'
  32.   HELP_INPUT = 'Premi un tasto sul pad per assegnarlo'
  33.   HELP_KEY  = 'Puoi cambiare i tasti del controller.
  34. Seleziona Ripristina o premi CTRL per resettare.'
  35.   #--------------------------------------------------------------------------
  36.   # * Tasti configurabili nella schermata.
  37.   #--------------------------------------------------------------------------
  38.   INPUTS = [:UP, :DOWN, :LEFT, :RIGHT, :C, :B, :A, :L, :R]
  39.   #--------------------------------------------------------------------------
  40.   # * Tasti da escludere nella selezione
  41.   #--------------------------------------------------------------------------
  42.   EXCLUDED = [:L_AXIS_X, :L_AXIS_Y, :R_AXIS_X, :R_AXIS_Y]
  43.   #--------------------------------------------------------------------------
  44.   # * Descrizione dei comandi
  45.   #--------------------------------------------------------------------------
  46.   INPUT_DESCRIPTION = {
  47.       :UP     => 'Su',
  48.       :DOWN   => 'Giù',
  49.       :LEFT   => 'Sinistra',
  50.       :RIGHT  => 'Destra',
  51.       :C      => 'Accetta/Interagisci',
  52.       :B      => 'Menu/Indietro',
  53.       :A      => 'Corri',
  54.       :L      => 'Precedente',
  55.       :R      => 'Successivo'
  56.   }
  57. end
  58.  
  59. #===============================================================================
  60. # ** ATTENZIONE: NON MODIFICARE SOTTO QUESTO SCRIPT SE NON SAI COSA TOCCARE! **
  61. #===============================================================================
  62.  
  63.  
  64.  
  65. $imported = {} if $imported == nil
  66. $imported['H87-PadSettings'] = 1.0
  67.  
  68. #===============================================================================
  69. # ** ControllerSettings
  70. #-------------------------------------------------------------------------------
  71. # Some core methods
  72. #===============================================================================
  73. module ControllerSettings
  74.   #--------------------------------------------------------------------------
  75.   # * Key scene command creation
  76.   #--------------------------------------------------------------------------
  77.   def self.create_keys_command
  78.     command = {:type => :advanced, :method => :call_keys,
  79.                :text => CONTROLLER_COMMAND, :help => CONTROLLER_HELP,
  80.                :condition => 'Input.controller_connected?'}
  81.     command
  82.   end
  83.   #--------------------------------------------------------------------------
  84.   # * Vibration command creation
  85.   #--------------------------------------------------------------------------
  86.   def self.create_vibration_command
  87.     command = {:type => :bar, :method => :update_vibration,
  88.                :text => VIBRATION_COMMAND, :help => VIBRATION_HELP,
  89.                :condition => 'Input.controller_connected?',
  90.                :var => 0, :max => 100, :val_mt => :get_vibration,
  91.                :distance => 10, :default => 100}
  92.     command
  93.   end
  94.   #--------------------------------------------------------------------------
  95.   # * Excluded keys from the configuration
  96.   #--------------------------------------------------------------------------
  97.   def self.excluded_commands; EXCLUDED; end
  98. end
  99.  
  100. #--------------------------------------------------------------------------
  101. # * Insert to Options list
  102. #--------------------------------------------------------------------------
  103. H87Options.push_keys_option(ControllerSettings.create_keys_command)
  104. H87Options.push_keys_option(ControllerSettings.create_vibration_command)
  105.  
  106. #===============================================================================
  107. # ** Vocab
  108. #-------------------------------------------------------------------------------
  109. # Scome vocabs
  110. #===============================================================================
  111. module Vocab
  112.   #--------------------------------------------------------------------------
  113.   # * Command Input friendly name
  114.   #--------------------------------------------------------------------------
  115.   def self.command_name(input)
  116.     ControllerSettings::INPUT_DESCRIPTION[input]
  117.   end
  118.   #--------------------------------------------------------------------------
  119.   # * Help text to input
  120.   #--------------------------------------------------------------------------
  121.   def self.input_window_text
  122.     ControllerSettings::HELP_INPUT
  123.   end
  124.   #--------------------------------------------------------------------------
  125.   # * Help text to key config
  126.   #--------------------------------------------------------------------------
  127.   def self.key_config_help
  128.     ControllerSettings::HELP_KEY
  129.   end
  130. end
  131.  
  132. #===============================================================================
  133. # ** Option
  134. #-------------------------------------------------------------------------------
  135. # Custom game pad methods
  136. #===============================================================================
  137. class Option
  138.   #--------------------------------------------------------------------------
  139.   # * Calls the key configuration scene
  140.   #--------------------------------------------------------------------------
  141.   def call_keys
  142.     SceneManager.call(Scene_KeyConfig)
  143.   end
  144.   #--------------------------------------------------------------------------
  145.   # * Updates the user vibration (and vibrate)
  146.   #--------------------------------------------------------------------------
  147.   def update_vibration(value)
  148.     $game_system.set_vibration_rate(value)
  149.     Input.vibrate(100, 100, 20) if SceneManager.scene_is?(Scene_Options)
  150.   end
  151.   #--------------------------------------------------------------------------
  152.   # * Gets the user configured vibration
  153.   #--------------------------------------------------------------------------
  154.   def get_vibration
  155.     $game_system.vibration_rate
  156.   end
  157. end
  158.  
  159. #===============================================================================
  160. # ** Scene_Options
  161. #-------------------------------------------------------------------------------
  162. # Controller check process
  163. #===============================================================================
  164. class Scene_Options < Scene_MenuBase
  165.   alias h87contr_settings_update update unless $@
  166.   alias h87contr_settings_start start unless $@
  167.   #--------------------------------------------------------------------------
  168.   # * Start
  169.   #--------------------------------------------------------------------------
  170.   def start
  171.     h87contr_settings_start
  172.     @connected = Input.controller_connected?
  173.   end
  174.   #--------------------------------------------------------------------------
  175.   # * Update process
  176.   #--------------------------------------------------------------------------
  177.   def update
  178.     h87contr_settings_update
  179.     update_controller_connection
  180.   end
  181.   #--------------------------------------------------------------------------
  182.   # * Checks if the controller is connected/disconnected during this
  183.   #   scene, and refresh the window
  184.   #--------------------------------------------------------------------------
  185.   def update_controller_connection
  186.     if @connected != Input.controller_connected?
  187.       @connected = Input.controller_connected?
  188.       @option_window.refresh
  189.     end
  190.   end
  191. end
  192.  
  193. #===============================================================================
  194. # ** Scene_KeyConfig
  195. #-------------------------------------------------------------------------------
  196. # Keys configuration scene
  197. #===============================================================================
  198. class Scene_KeyConfig < Scene_MenuBase
  199.   #--------------------------------------------------------------------------
  200.   # * Start
  201.   #--------------------------------------------------------------------------
  202.   def start
  203.     super
  204.     create_help_window
  205.     create_keys_window
  206.     create_input_window
  207.   end
  208.   #--------------------------------------------------------------------------
  209.   # * Help window creation
  210.   #--------------------------------------------------------------------------
  211.   def create_help_window
  212.     super
  213.     @help_window.set_text(Vocab.key_config_help)
  214.   end
  215.   #--------------------------------------------------------------------------
  216.   # * Keys list window creation
  217.   #--------------------------------------------------------------------------
  218.   def create_keys_window
  219.     y = @help_window.height
  220.     @keys_window = Window_PadKeys.new(0, y, Graphics.width, Graphics.height - y)
  221.     @keys_window.set_handler(:ok, method(:check_command))
  222.     @keys_window.set_handler(:cancel, method(:return_scene))
  223.     @keys_window.set_handler(:reset, method(:reset_keys))
  224.     @keys_window.activate
  225.     @keys_window.index = 0
  226.   end
  227.   #--------------------------------------------------------------------------
  228.   # * Input window creation
  229.   #--------------------------------------------------------------------------
  230.   def create_input_window
  231.     @input_window = Window_Input_Key.new(method(:check_selected_key))
  232.   end
  233.   #--------------------------------------------------------------------------
  234.   # * Checks the selected command
  235.   #--------------------------------------------------------------------------
  236.   def check_command
  237.     if @keys_window.item == :reset
  238.       reset_keys
  239.     else
  240.       open_input_window
  241.     end
  242.   end
  243.   #--------------------------------------------------------------------------
  244.   # * Reset all custom keymap
  245.   #--------------------------------------------------------------------------
  246.   def reset_keys
  247.     $game_system.create_default_key_set
  248.     @keys_window.refresh
  249.     @keys_window.activate
  250.   end
  251.   #--------------------------------------------------------------------------
  252.   # * Opens the key input window
  253.   #--------------------------------------------------------------------------
  254.   def open_input_window
  255.     @input_window.open
  256.   end
  257.   #--------------------------------------------------------------------------
  258.   # * Checks what the user chosen. If the controller is accidentally
  259.   #   disconnected, returns to the previous scene.
  260.   #--------------------------------------------------------------------------
  261.   def check_selected_key
  262.     key = @input_window.selected_key
  263.     if key == 0
  264.       return_scene
  265.     else
  266.       $game_system.xinput_key_set[@keys_window.item] = key
  267.       @keys_window.refresh
  268.       @keys_window.activate
  269.     end
  270.   end
  271. end
  272.  
  273. #===============================================================================
  274. # ** Window_PadKeys
  275. #-------------------------------------------------------------------------------
  276. # This window contains the editable commands
  277. #===============================================================================
  278. class Window_PadKeys < Window_Selectable
  279.   #--------------------------------------------------------------------------
  280.   # * Object initialization
  281.   #--------------------------------------------------------------------------
  282.   def initialize(x, y, width, height)
  283.     super
  284.     @data = ControllerSettings::INPUTS + [:reset]
  285.     refresh
  286.   end
  287.   #--------------------------------------------------------------------------
  288.   # * Item max
  289.   #--------------------------------------------------------------------------
  290.   def item_max; @data ? @data.size : 0; end
  291.   #--------------------------------------------------------------------------
  292.   # * Item (as Symbol)
  293.   #--------------------------------------------------------------------------
  294.   def item; @data[index]; end
  295.   #--------------------------------------------------------------------------
  296.   # * Draw item
  297.   #--------------------------------------------------------------------------
  298.   def draw_item(index)
  299.     key = @data[index]
  300.     if key
  301.       if key != :reset
  302.         draw_key_command(key, index)
  303.       else
  304.         draw_reset_command(index)
  305.       end
  306.     end
  307.   end
  308.   #--------------------------------------------------------------------------
  309.   # * Draws a command with friendly name and controller input
  310.   #--------------------------------------------------------------------------
  311.   def draw_key_command(key, index)
  312.     rect = item_rect(index)
  313.     rect.width -= 4
  314.     change_color(normal_color)
  315.     draw_text(rect.x, rect.y, rect.width/2, line_height, Vocab.command_name(key))
  316.     if $game_system.xinput_key_set[key]
  317.       change_color(crisis_color)
  318.       draw_text(rect.width/2, rect.y, rect.width/2, line_height, Vocab.gamepad_key($game_system.xinput_key_set[key]), 1)
  319.     else
  320.       change_color(knockout_color)
  321.       draw_text(rect.width/2, rect.y, rect.width/2, line_height, ControllerSettings::NO_KEY_SET, 1)
  322.     end
  323.   end
  324.   #--------------------------------------------------------------------------
  325.   # * Draws the reset command
  326.   #--------------------------------------------------------------------------
  327.   def draw_reset_command(index)
  328.     rect = item_rect(index)
  329.     rect.width -= 4
  330.     change_color(normal_color)
  331.     draw_text(rect, ControllerSettings::RESET_COMMAND, 1)
  332.   end
  333.   #--------------------------------------------------------------------------
  334.   # * Handling Processing for OK and Cancel Etc.
  335.   #--------------------------------------------------------------------------
  336.   def process_handling
  337.     return unless open? && active
  338.     process_reset    if handle?(:reset)   && Input.trigger?(:CTRL)
  339.     super
  340.   end
  341.   #--------------------------------------------------------------------------
  342.   # * CTRL key called
  343.   #--------------------------------------------------------------------------
  344.   def process_reset
  345.     call_reset_handler
  346.   end
  347.   #--------------------------------------------------------------------------
  348.   # * Calls the reset process
  349.   #--------------------------------------------------------------------------
  350.   def call_reset_handler
  351.     call_handler(:reset)
  352.   end
  353. end
  354.  
  355. #===============================================================================
  356. # ** Window_Input_Key
  357. #-------------------------------------------------------------------------------
  358. # This window awaits the user input with the game pad
  359. #===============================================================================
  360. class Window_Input_Key < Window_Base
  361.   attr_reader :selected_key
  362.   #--------------------------------------------------------------------------
  363.   # * Object initialization
  364.   # @param [Method] method
  365.   #--------------------------------------------------------------------------
  366.   def initialize(method)
  367.     width = calc_width
  368.     height = fitting_height(2)
  369.     @closing_method = method
  370.     @selected_key = nil
  371.     super(0, 0, width, height)
  372.     center_window
  373.     self.openness = 0
  374.     self.back_opacity = 255
  375.     refresh
  376.   end
  377.   #--------------------------------------------------------------------------
  378.   # * Refreshj
  379.   #--------------------------------------------------------------------------
  380.   def refresh
  381.     contents.clear
  382.     draw_text(0, 0, contents_width, contents_height, Vocab.input_window_text, 1)
  383.   end
  384.   #--------------------------------------------------------------------------
  385.   # * Window opening with input check
  386.   #--------------------------------------------------------------------------
  387.   def open
  388.     @old_packet = XInput.get_state.packet_number
  389.     super
  390.   end
  391.   #--------------------------------------------------------------------------
  392.   # * Update process with key scanning
  393.   #--------------------------------------------------------------------------
  394.   def update
  395.     super
  396.     update_key_scan
  397.   end
  398.   #--------------------------------------------------------------------------
  399.   # * Detects the first user input. If the controller is disconnected,
  400.   #   closes the window.
  401.   #--------------------------------------------------------------------------
  402.   def update_key_scan
  403.     return unless open?
  404.     keymap = XInput.get_key_state.keys - ControllerSettings.excluded_commands
  405.     unless Input.controller_connected?
  406.       @selected_key = 0
  407.       close
  408.       return
  409.     end
  410.     if keymap.any? && XInput.get_state.packet_number != @old_packet
  411.       @selected_key = keymap.first
  412.       close
  413.     end
  414.   end
  415.   #--------------------------------------------------------------------------
  416.   # * Centra la finestra nel campo
  417.   #--------------------------------------------------------------------------
  418.   def center_window
  419.     self.x = (Graphics.width - self.width)/2
  420.     self.y = (Graphics.height - self.height)/2
  421.   end
  422.   #--------------------------------------------------------------------------
  423.   # * Closes the window calling the refresh method
  424.   #--------------------------------------------------------------------------
  425.   def close
  426.     super
  427.     @closing_method.call
  428.   end
  429.   #--------------------------------------------------------------------------
  430.   # * Gets the window width
  431.   #--------------------------------------------------------------------------
  432.   def calc_width; Graphics.width; end
  433. end
RAW Paste Data