Advertisement
Holy87

Keyboard Options

Sep 19th, 2020
2,424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 14.37 KB | None | 0 0
  1. #===============================================================================
  2. # Personalizzazione tastiera 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 aggiungere nelle opzioni di gioco una nuova voce
  10. # che permetterà al giocatore di personalizzare i pulsanti della tastiera per
  11. # eseguire i vari comandi nel gioco. Puoi definire più tasti per ogni
  12. #===============================================================================
  13. # Istruzioni: inserire lo script sotto Materials, prima del Main e sotto gli
  14. # script Interfaccia Tastiera e Opzioni di gioco. I due script sono necessari
  15. # per il corretto funzionamento, altrimenti va in 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 Keyboard
  23.   module Settings
  24.     KEYBOARD_COMMAND = 'Tastiera'
  25.     KEYBOARD_HELP = 'Imposta i pulsanti della tastiera.'
  26.     HELP_INPUT = 'Premi un tasto per assegnarlo, ESC per annullare.'
  27.     HELP_KEY = 'Puoi cambiare i pulsanti della tastiera.
  28. Seleziona Ripristina o premi %s per resettare.'
  29.     RESET_KEY_COMMAND = 'Reimposta tutto'
  30.     ASSIGNED_ERROR = 'Il tasto %s è già assegnato a %s.'
  31.     RESERVED_ERROR = 'Non puoi usare %s.'
  32.  
  33.     # Tasti configurabili nella schermata. Nota che puoi aggiungere
  34.     # anche :X, :Y e :Z, nonché i nuovi :START e :SELECT.
  35.     # Sono configurabili solo se i comandi sono ARRAY
  36.     INPUTS = [:UP, :DOWN, :LEFT, :RIGHT, :C, :B, :A, :L, :R]
  37.  
  38.     # Descrizione dei comandi.
  39.     INPUT_DESCRIPTION = {
  40.         :UP => 'Su',
  41.         :DOWN => 'Giù',
  42.         :LEFT => 'Sinistra',
  43.         :RIGHT => 'Destra',
  44.         :C => 'Accetta/Interagisci',
  45.         :B => 'Menu/Indietro',
  46.         :A => 'Corri',
  47.         :L => 'Precedente',
  48.         :R => 'Successivo'
  49.     }
  50.  
  51.     # Comandi che sono in sola lettura così da non bloccare il giocatore
  52.     # per un'errata configurazione
  53.     BLOCKED_CONFIGURATIONS = [] #es. :UP, :SELECT, :START
  54.  
  55.     # Numero massimo di tasti configurabili della tastiera per ogni
  56.     # comando. Ad esempio per :L sono usabili :KEY_Q e :VK_PRIOR.
  57.     # Per :C invece ce ne sono 3, ma :VK_RETURN è riservato per il
  58.     # Reset Key (nulla vieta di fare come ti pare, comunque)
  59.     MAX_CONFIGURABLE = 2
  60.  
  61.     # Pulsanti della tastiera che possono essere utilizzati per il gioco
  62.     PERMITTED_KEYS = [:NUM_0, :NUM_1, :NUM_2, :NUM_3, :NUM_4, :NUM_5, :NUM_6, :NUM_7,
  63.                       :NUM_8, :NUM_9, :KEY_A, :KEY_B, :KEY_C, :KEY_D, :KEY_E, :KEY_F,
  64.                       :KEY_G, :KEY_H, :KEY_I, :KEY_J, :KEY_K, :KEY_L, :KEY_M, :KEY_N,
  65.                       :KEY_O, :KEY_P, :KEY_Q, :KEY_R, :KEY_S, :KEY_T, :KEY_U, :KEY_V,
  66.                       :KEY_W, :KEY_X, :KEY_Y, :KEY_Z, :VK_OEM_1, :VK_OEM_PLUS,
  67.                       :VK_OEM_COMMA, :VK_OEM_MINUS, :VK_OEM_PERIOD, :VK_OEM_2,
  68.                       :VK_OEM_3, :VK_OEM_4, :VK_OEM_5, :VK_OEM_6, :VK_OEM_7,
  69.                       :VK_OEM_8, :VK_OEM_102, :VK_TAB, :VK_BACK, :VK_MENU,
  70.                       :VK_SPACE, :VK_PRIOR, :VK_NEXT, :VK_END, :VK_HOME, :VK_SELECT,
  71.                       :VK_INSERT, :VK_SHIFT, :VK_UP, :VK_DOWN, :VK_RIGHT, :VK_LEFT,
  72.                       :VK_RETURN, :VK_CONTROL, :VK_NUMPAD0, :VK_NUMPAD1, :VK_NUMPAD2,
  73.                       :VK_NUMPAD3, :VK_NUMPAD4, :VK_NUMPAD5, :VK_NUMPAD6, :VK_NUMPAD7,
  74.                       :VK_NUMPAD8, :VK_NUMPAD9, :VK_MULTIPLY, :VK_ADD, :VK_SEPARATOR,
  75.                       :VK_SUBTRACT, :VK_DECIMAL, :VK_DIVIDE
  76.     ]
  77.  
  78.     # Pulsanti della tastiera che NON possono essere rimossi o riassegnati
  79.     FIXED_KEYS = [:VK_RETURN, :VK_CONTROL, :VK_ESCAPE, :VK_UP, :VK_DOWN, :VK_RIGHT, :VK_LEFT]
  80.  
  81.     # Pulsante della tastiera da usare per resettare tutti i tasti
  82.     RESET_KEY = :VK_CONTROL
  83.  
  84.     # Pulsante della tastiera da usare per annullare l'immissione
  85.     CANCEL_KEY = :VK_ESCAPE
  86.  
  87.     # Impedisci di selezionare un tasto già assegnato ad un altro comando
  88.     # se è l'unico assegnato a quel comando.
  89.     BLOCK_ALREADY_ASSIGNED = true
  90.   end
  91.  
  92.   def self.create_keys_command
  93.     command = {:type => :advanced,
  94.                :method => :call_keyboard_configuration,
  95.                :text => Settings::KEYBOARD_COMMAND,
  96.                :help => Settings::KEYBOARD_HELP}
  97.     command
  98.   end
  99. end
  100.  
  101. H87Options.push_keys_option(Keyboard.create_keys_command)
  102. $imported = {} if $imported == nil
  103. $imported['H87-Keyboard_Settings'] = 1.0
  104.  
  105. module Vocab
  106.   def self.keyboard_config_help
  107.     sprintf(Keyboard::Settings::HELP_KEY, Vocab.key_name(Keyboard::Settings::RESET_KEY))
  108.   end
  109.  
  110.   def self.command_name(command)
  111.     Keyboard::Settings::INPUT_DESCRIPTION[command]
  112.   end
  113.  
  114.   def self.keyboard_input_window_help_text
  115.     Keyboard::Settings::HELP_INPUT
  116.   end
  117. end
  118.  
  119. class Option
  120.   def call_keyboard_configuration
  121.     Sound.play_ok
  122.     SceneManager.call(Scene_KeyboardConfig)
  123.   end
  124. end
  125.  
  126. class Game_System
  127.   def configure_key(command, key, index = 0)
  128.     delete_key(key)
  129.     keyboard_set[command][index] = key
  130.   end
  131.  
  132.   def delete_key(key)
  133.     keyboard_set.keys.each do |command|
  134.       keys = keyboard_set[command]
  135.       if keys.is_a?(Symbol)
  136.         keyboard_set[command] = nil if keys == key
  137.       else
  138.         keys.each_with_index { |_key, index| keys[index] = nil if _key == key }
  139.       end
  140.     end
  141.   end
  142. end
  143.  
  144.  
  145. class Scene_KeyboardConfig < Scene_MenuBase
  146.   def start
  147.     super
  148.     create_help_window
  149.     create_key_list_window
  150.     create_input_window
  151.   end
  152.  
  153.   def create_help_window
  154.     super
  155.     @help_window.set_text Vocab::keyboard_config_help
  156.   end
  157.  
  158.   def create_key_list_window
  159.     y = @help_window.height
  160.     @keys_window = Window_KeyboardKeys.new(0, y, Graphics.width, Graphics.height - y)
  161.     @keys_window.set_handler(:ok, method(:check_command))
  162.     @keys_window.set_handler(:cancel, method(:return_scene))
  163.     @keys_window.set_handler(:reset, method(:reset_keys))
  164.     @keys_window.activate
  165.     @keys_window.index = 0
  166.   end
  167.  
  168.   def create_input_window
  169.     @input_window = Window_KeyInput.new(method(:check_selected_key))
  170.   end
  171.  
  172.   def check_command
  173.     if @keys_window.item == :reset
  174.       reset_keys
  175.     else
  176.       open_input_window
  177.     end
  178.   end
  179.  
  180.   def reset_keys
  181.     Sound.play_load
  182.     $game_system.create_default_keyboard_set
  183.     @keys_window.refresh
  184.     @keys_window.activate
  185.   end
  186.  
  187.   def open_input_window
  188.     @input_window.open(@keys_window.item.key)
  189.   end
  190.  
  191.   def check_selected_key
  192.     if @input_window.selected_key != nil
  193.       $game_system.configure_key(
  194.           @keys_window.item.command,
  195.           @input_window.selected_key,
  196.           @keys_window.item.index
  197.       )
  198.       @keys_window.refresh
  199.     end
  200.     @keys_window.activate
  201.   end
  202. end
  203.  
  204. #===============================================================================
  205. # ** Input_Setting_Wrapper
  206. #-------------------------------------------------------------------------------
  207. # This is a container for command -> key association
  208. #===============================================================================
  209. class Input_Setting_Wrapper
  210.   attr_accessor :command
  211.   attr_accessor :key
  212.   attr_accessor :index
  213.  
  214.   def initialize(command, key, index)
  215.     @command = command
  216.     @key = key
  217.     @index = index
  218.   end
  219.  
  220.   def editable?
  221.     return false if Keyboard::Settings::BLOCKED_CONFIGURATIONS.include?(@command)
  222.     return true if @key.nil?
  223.     return false if Keyboard::Settings::FIXED_KEYS.include?(@key)
  224.     true
  225.   end
  226. end
  227.  
  228. #===============================================================================
  229. # ** Window_KeyInput
  230. #-------------------------------------------------------------------------------
  231. # This window awaits the user input with the keyboard
  232. #===============================================================================
  233. class Window_KeyInput < Window_Base
  234.   attr_reader :selected_key
  235.  
  236.   def initialize(method)
  237.     width = calc_width
  238.     height = fitting_height(2)
  239.     @closing_method = method
  240.     @selected_key = nil
  241.     @old_key = nil
  242.     super(0, 0, width, height)
  243.     center_window
  244.     self.openness = 0
  245.     refresh
  246.   end
  247.  
  248.   #--------------------------------------------------------------------------
  249.   # * Refreshj
  250.   #--------------------------------------------------------------------------
  251.   def refresh
  252.     contents.clear
  253.     change_color normal_color
  254.     draw_text(0, 0, contents_width, line_height, Vocab.keyboard_input_window_help_text, 1)
  255.   end
  256.  
  257.   #--------------------------------------------------------------------------
  258.   # * Update process with key scanning
  259.   #--------------------------------------------------------------------------
  260.   def update
  261.     super
  262.     update_key_scan
  263.   end
  264.  
  265.   def open(key = nil)
  266.     refresh
  267.     @old_key = key
  268.     super()
  269.   end
  270.  
  271.   # * Detects the first user input
  272.   def update_key_scan
  273.     return unless open?
  274.     return if @closing
  275.     return selection_ok(nil) if canceled?
  276.     key_selection
  277.   end
  278.  
  279.   def canceled?
  280.     Input.trigger?(Keyboard::Settings::CANCEL_KEY)
  281.   end
  282.  
  283.   # Key selection process
  284.   def reserved_fail(selected)
  285.     text = sprintf(Keyboard::Settings::RESERVED_ERROR, selected)
  286.     show_failure(text)
  287.   end
  288.  
  289.   def key_selection
  290.     selected = triggered_key
  291.     return if selected.nil?
  292.     assigned = already_assigned?(selected)
  293.     return assigned_fail(assigned, selected) if assigned
  294.     return reserved_fail(selected) if reserved?(selected)
  295.     selection_ok selected
  296.   end
  297.  
  298.   # @param [Symbol] key
  299.   # @return [Symbol, nil]
  300.   def already_assigned?(key)
  301.     return unless Keyboard::Settings::BLOCK_ALREADY_ASSIGNED
  302.     return if @old_key == key
  303.     Keyboard::Settings::INPUTS.each do |command|
  304.       next if $game_system.keyboard_set[command].compact.size > 1
  305.       return command if $game_system.keyboard_set[command].compact.first == key
  306.     end
  307.     nil
  308.   end
  309.  
  310.   def reserved?(key)
  311.     Keyboard::Settings::FIXED_KEYS.include?(key)
  312.   end
  313.  
  314.   def triggered_key
  315.     Keyboard::Settings::PERMITTED_KEYS.each {|k| return k if Input.trigger?(k)}
  316.     nil
  317.   end
  318.  
  319.   # * Command selection success
  320.   def selection_ok(key)
  321.     @selected_key = key
  322.     key.nil? ? Sound.play_cancel : Sound.play_ok
  323.     close
  324.   end
  325.  
  326.   # * Command selection failed
  327.   def assigned_fail(command, key)
  328.     text = sprintf(Keyboard::Settings::ASSIGNED_ERROR, Vocab.key_name(key), Vocab.command_name(command))
  329.     show_failure text
  330.   end
  331.  
  332.   def show_failure(text)
  333.     Sound.play_buzzer
  334.     contents.clear_rect(0, line_height, contents_width, line_height)
  335.     change_color(knockout_color)
  336.     draw_text(0, line_height, contents_width, line_height, text, 1)
  337.     change_color(normal_color)
  338.   end
  339.  
  340.   # * Centra la finestra nel campo
  341.   def center_window
  342.     self.x = (Graphics.width - self.width) / 2
  343.     self.y = (Graphics.height - self.height) / 2
  344.   end
  345.  
  346.   # Closes the window calling the refresh method
  347.   def close
  348.     super
  349.     @closing_method.call
  350.   end
  351.  
  352.   # * Gets the window width
  353.   def calc_width
  354.     Graphics.width
  355.   end
  356. end
  357.  
  358. #===============================================================================
  359. # ** Window_KeyboardKeys
  360. #-------------------------------------------------------------------------------
  361. # This window show the configurable commands and assigned keys.
  362. #===============================================================================
  363. class Window_KeyboardKeys < Window_Selectable
  364.   def initialize(x, y, width, height)
  365.     super
  366.     refresh
  367.   end
  368.  
  369.   def make_item_list
  370.     @data = []
  371.     Keyboard::Settings::INPUTS.each do |command|
  372.       Keyboard::Settings::MAX_CONFIGURABLE.times do |index|
  373.         key = $game_system.keyboard_set[command][index]
  374.         @data.push(Input_Setting_Wrapper.new(command, key, index))
  375.       end
  376.     end
  377.     @data.push(:reset)
  378.   end
  379.  
  380.   def col_max
  381.     Keyboard::Settings::MAX_CONFIGURABLE
  382.   end
  383.  
  384.   def item_max
  385.     @data ? @data.size : 0
  386.   end
  387.  
  388.   # @param [Integer] index
  389.   # @return [Symbol, Input_Setting_Wrapper]
  390.   def item(index = @index)
  391.     @data[index]
  392.   end
  393.  
  394.   def command_description_width
  395.     Graphics.width / 3
  396.   end
  397.  
  398.   def draw_command_names
  399.     Keyboard::Settings::INPUT_DESCRIPTION.each_with_index do |obj, index|
  400.       draw_text(0, index * line_height, command_description_width, line_height, obj[1])
  401.     end
  402.   end
  403.  
  404.   def refresh
  405.     make_item_list
  406.     super
  407.     draw_command_names
  408.   end
  409.  
  410.   # @param [Integer] index
  411.   # @return [Rect]
  412.   def item_rect(index)
  413.     rect = super(index)
  414.     if item(index).is_a?(Symbol)
  415.       rect.x = 0
  416.       rect.width = contents_width
  417.     else
  418.       rect.x += command_description_width
  419.     end
  420.     rect
  421.   end
  422.  
  423.   def item_width
  424.     (width - command_description_width - standard_padding * 2 + spacing) / col_max - spacing
  425.   end
  426.  
  427.   def draw_item(index)
  428.     key = @data[index]
  429.     return unless key
  430.     return draw_reset_command(index) if key == :reset
  431.     draw_key_command(key, index)
  432.   end
  433.  
  434.   # @param [Symbol, Input_Setting_Wrapper] key
  435.   def enable?(key)
  436.     return true if key.is_a? Symbol
  437.     key.editable?
  438.   end
  439.  
  440.   def current_item_enabled?
  441.     enable?(@data[index])
  442.   end
  443.  
  444.   # @param [Input_Setting_Wrapper] key_obj
  445.   # @param [Integer] index
  446.   def draw_key_command(key_obj, index)
  447.     rect = item_rect(index)
  448.     rect.width -= 4
  449.     enabled = enable?(key_obj)
  450.     return if key_obj.key.nil?
  451.     change_color(normal_color, enabled)
  452.     draw_text(rect, Vocab.key_name(key_obj.key), 1)
  453.   end
  454.  
  455.   def draw_reset_command(index)
  456.     rect = item_rect(index)
  457.     rect.width = contents_width
  458.     rect.x = 0
  459.     rect.width -= 4
  460.     change_color(normal_color)
  461.     draw_text(rect, Keyboard::Settings::RESET_KEY_COMMAND, 1)
  462.   end
  463.  
  464.   # Handling Processing for OK and Cancel Etc.
  465.   def process_handling
  466.     return unless open? && active
  467.     process_reset if handle?(:reset) && Input.trigger?(:CTRL)
  468.     super
  469.   end
  470.  
  471.   # CTRL key called
  472.   def process_reset
  473.     call_reset_handler
  474.   end
  475.  
  476.   # Calls the reset process
  477.   def call_reset_handler
  478.     call_handler(:reset)
  479.   end
  480. end
  481.  
  482.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement