Advertisement
Holy87

Controller Mapper

Apr 2nd, 2017 (edited)
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 10.32 KB | None | 0 0
  1. #===============================================================================
  2. # Icone tasti 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 mostrare l'icona del pulsante del comando nel
  10. # gioco. La peculiarità principale di questo script è che sa riconoscere se
  11. # il giocatore sta usando un controller ed i comandi configurati, e mostrare
  12. # l'icona appropriata del tasto. Esempio:
  13. # Si vuole mostrare al giocatore il messaggio "Premi [Tasto corsa] per correre"
  14. # Quello che bisogna fare è scrivere nel box del messaggio
  15. # "Premi \K[0] per correre", dove \K è il nuovo comando del messaggio e 0 è
  16. # l'ID del comando (puoi controllare gli ID in basso nella configurazione,
  17. # dove è definito KEY_INDEXES)
  18. # Al giocatore verrà mostrato al posto di \K[0]
  19. # ● L'icona del tasto SHIFT sulla tastiera, se ha solo la tastiera;
  20. # ● L'icona del tasto Y del controller, se il controller è collegato;
  21. # ● Oppure l'icona del tasto che il giocatore ha scelto di configurare nelle
  22. #   opzioni di gioco come corsa.
  23. # PER SMANETTONI:
  24. # Gli scripter possono usare il metodo delle finestre e delle bitmap
  25. # draw_key_icon(comando, x, y)
  26. # per mostrare l'icona del comando nella finestra, dove nel parametro comando
  27. # inserirete il simbolo del comando (:C, :B, :LEFT, :RIGHT ecc...)
  28. #===============================================================================
  29. # Istruzioni: inserire lo script sotto Materials, prima del Main e sotto lo
  30. # script di configurazione del controller (se presente, ma non è necessario
  31. # averlo se non si vuole usarlo)
  32. #===============================================================================
  33.  
  34. #===============================================================================
  35. # ** Impostazioni dello script
  36. #===============================================================================
  37. module Controller_Mapper
  38.   module Settings
  39.     # File immagine delle icone dei tasti del controller
  40.     PAD_ICONS_GRAPHIC = 'GamepadKeys'
  41.     # File immagine delle icone dei tasti della tastiera
  42.     KEY_ICONS_GRAPHIC = 'KeyboardKeys'
  43.  
  44.     # Comandi del gamepad con le rispettive icone (dell'immagine
  45.     # PAD_ICONS_GRAPHIC)
  46.     PAD_ICONS = {
  47.         # comando         indice
  48.         :DPAD_DOWN      => 0,
  49.         :DPAD_LEFT      => 1,
  50.         :DPAD_UP        => 2,
  51.         :DPAD_RIGHT     => 3,
  52.         :PAD_A          => 4,
  53.         :PAD_B          => 5,
  54.         :PAD_X          => 6,
  55.         :PAD_Y          => 7,
  56.         :DIR_KEY        => 8,
  57.         :LEFT_SHOULDER  => 9,
  58.         :RIGHT_SHOULDER => 10,
  59.         :L_TRIG         => 11,
  60.         :R_TRIG         => 12,
  61.         :L_AXIS_X       => 13,
  62.         :L_AXIS_Y       => 13,
  63.         :R_AXIS_X       => 14,
  64.         :R_AXIS_Y       => 14,
  65.         :L_AXIS_LEFT        => 13,
  66.         :L_AXIS_RIGHT       => 13,
  67.         :L_AXIS_UP          => 13,
  68.         :L_AXIS_DOWN        => 13,
  69.         :R_AXIS_LEFT        => 14,
  70.         :R_AXIS_RIGHT       => 14,
  71.         :R_AXIS_UP          => 14,
  72.         :R_AXIS_DOWN        => 14,
  73.         :PAD_BACK       => 15,
  74.         :PAD_START      => 16
  75.     }
  76.  
  77.     # Tasti della tastiera con le rispettive icone (dell'immagine
  78.     # KEY_ICONS_GRAPHIC)
  79.     KEY_ICONS = {
  80.         :ESC => 0,
  81.         :ENTER => 1,
  82.         :PAG_UP => 2,
  83.         :PAG_DOWN => 3,
  84.         :KEY_A => 4,
  85.         :KEY_S => 5,
  86.         :KEY_D => 6,
  87.         :KEY_DOWN => 7,
  88.         :KEY_LEFT => 8,
  89.         :KEY_UP => 9,
  90.         :KEY_RIGHT => 10,
  91.         :SHIFT => 11,
  92.         :DIR_KEY => 12
  93.     }
  94.  
  95.     # Settaggi predefiniti della tastiera (ad un comando di gioco,
  96.     # corrisponde un tasto della tastiera).
  97.     DEFAULT_KEY_SET = {
  98.         #input => tasto
  99.         :A => :SHIFT,
  100.         :B => :ESC,
  101.         :C => :ENTER,
  102.         :X => :KEY_A,
  103.         :Y => :KEY_S,
  104.         :Z => :KEY_D,
  105.         :L => :PAG_UP,
  106.         :R => :PAG_DOWN,
  107.         :LEFT => :KEY_LEFT,
  108.         :RIGHT => :KEY_RIGHT,
  109.         :UP => :KEY_UP,
  110.         :DOWN => :KEY_DOWN
  111.     }
  112.  
  113.     # Questo array serve per mostrare il pulsante nella finestra di messaggio
  114.     # e di aiuto. Ad ogni indice corrisponde un pulsante. Ad esempio, se
  115.     # scrivi \K[2] nel messaggio, mostrerà l'icona del tasto invio della
  116.     # tastiera o del tasto A del gamepad (perché nell'array si riferisce a
  117.     # :C, dato che l'ordine parte da 0)
  118.     #               0   1   2   3   4   5   6   7   8      9       10   11     12
  119.     KEY_INDEXES = [:A, :B, :C, :X, :Y, :Z, :L, :R, :LEFT, :RIGHT, :UP, :DOWN, :DIR_KEY]
  120.   end
  121.  
  122. # ---------ATTENZIONE: MODIFICARE SOTTO QUESTO PARAGRAFO COMPORTA SERI RISCHI
  123. #           PER IL CORRETTO FUNZIONAMENTO DELLO SCRIPT! -------------
  124.  
  125.  
  126.   $imported = {} if $imported == nil
  127.   $imported['H87-ControllerMapper'] = 1.0
  128.  
  129.  
  130.   #--------------------------------------------------------------------------
  131.   # * Controller used condition
  132.   #--------------------------------------------------------------------------
  133.   def self.use_controller?
  134.     $imported['H87-XInput'] && Input.controller_connected?
  135.   end
  136.   #--------------------------------------------------------------------------
  137.   # * Gets the bitmap of the proper device (gamepad or keyboard)
  138.   # @return [Bitmap]
  139.   #--------------------------------------------------------------------------
  140.   def self.actual_device_keys
  141.     if use_controller?
  142.       Cache.system(Settings::PAD_ICONS_GRAPHIC)
  143.     else
  144.       Cache.system(Settings::KEY_ICONS_GRAPHIC)
  145.     end
  146.   end
  147.   #--------------------------------------------------------------------------
  148.   # * Gets the index of the key icon
  149.   # @param [Symbol] key_symbol
  150.   # @return [Integer]
  151.   #--------------------------------------------------------------------------
  152.   def self.key_icon_index(key_symbol)
  153.     if use_controller?
  154.       index = Settings::PAD_ICONS[$game_system.adjust_buttons(key_symbol)]
  155.     else
  156.       index = Settings::KEY_ICONS[$game_system.adjust_commands(key_symbol)]
  157.     end
  158.     index ? index : -1
  159.   end
  160. end
  161.  
  162. #===============================================================================
  163. # ** Game_System
  164. #===============================================================================
  165. class Game_System
  166.   #--------------------------------------------------------------------------
  167.   # * Adjust commands method (so someone can overwrite this method)
  168.   # @param [Symbol] original_key
  169.   # @return [Symbol]
  170.   #--------------------------------------------------------------------------
  171.   def adjust_commands(original_key)
  172.     key_set = Controller_Mapper::Settings::DEFAULT_KEY_SET
  173.     key_set[original_key] ? key_set[original_key] : original_key
  174.   end
  175. end
  176.  
  177. #===============================================================================
  178. # ** Bitmap
  179. #===============================================================================
  180. class Bitmap
  181.   #--------------------------------------------------------------------------
  182.   # * Draws the key on the bitmap.
  183.   # @param [Symbol] key_symbol
  184.   # @param [Integer] x
  185.   # @param [Integer] y
  186.   #--------------------------------------------------------------------------
  187.   def draw_key_icon(key_symbol, x, y, enabled = true)
  188.     bitmap = Controller_Mapper.actual_device_keys
  189.     index = Controller_Mapper.key_icon_index(key_symbol)
  190.     rect = Rect.new(index * 24, 0, 24, 24)
  191.     opacity = enabled ? 255 : 128
  192.     blt(x, y, bitmap, rect, opacity)
  193.   end
  194. end
  195.  
  196. #===============================================================================
  197. # ** Window_Base
  198. #===============================================================================
  199. class Window_Base < Window
  200.   alias h87_normal_process_escape process_escape_character unless $@
  201.   #--------------------------------------------------------------------------
  202.   # * Draws the proper key icon
  203.   # @param [Symbol] key
  204.   # @param [Integer] x
  205.   # @param [Integer] y
  206.   # @param [Boolean] enabled
  207.   #--------------------------------------------------------------------------
  208.   def draw_key_icon(key, x, y, enabled = true)
  209.     contents.draw_key_icon(key, x, y, enabled)
  210.   end
  211.   #--------------------------------------------------------------------------
  212.   # * Alias method: process_escape_character
  213.   # @param [String] code
  214.   # @param [String] text
  215.   # @param [Hash<Integer>] pos
  216.   #--------------------------------------------------------------------------
  217.   def process_escape_character(code, text, pos)
  218.     if code.upcase == 'K'
  219.       process_draw_key_icon(obtain_escape_param(text), pos)
  220.       return
  221.     end
  222.     h87_normal_process_escape(code, text, pos)
  223.   end
  224.   #--------------------------------------------------------------------------
  225.   # * Draws the key icon
  226.   # @param [Integer] index
  227.   # @param [Hash<Integer>] pos
  228.   #--------------------------------------------------------------------------
  229.   def process_draw_key_icon(index, pos)
  230.     symbol = Controller_Mapper::Settings::KEY_INDEXES[index]
  231.     puts "Key not found for index #{index}" if symbol.nil?
  232.     draw_key_icon(symbol, pos[:x], pos[:y])
  233.     pos[:x] += 24
  234.   end
  235. end
  236.  
  237. if $imported['H87-PadSettings'] # for showing the icons in the configuration
  238. #===============================================================================
  239. # ** Window_PadKeys
  240. #===============================================================================
  241. class Window_PadKeys < Window_Selectable
  242.   #--------------------------------------------------------------------------
  243.   # * Overwrite method
  244.   #--------------------------------------------------------------------------
  245.   def draw_key_command(key, index)
  246.     rect = item_rect(index)
  247.     rect.width -= 4
  248.     change_color(normal_color)
  249.     draw_text(rect.x, rect.y, rect.width/2, line_height, Vocab.command_name(key))
  250.     if $game_system.xinput_key_set[key]
  251.       if Controller_Mapper.key_icon_index(key) >= 0
  252.         x = rect.width/4 * 3
  253.         draw_key_icon(key, x, rect.y)
  254.       else
  255.         change_color(crisis_color)
  256.         draw_text(rect.width/2, rect.y, rect.width/2, line_height, Vocab.gamepad_key($game_system.xinput_key_set[key]), 1)
  257.       end
  258.     else
  259.       change_color(knockout_color)
  260.       draw_text(rect.width/2, rect.y, rect.width/2, line_height, ControllerSettings::NO_KEY_SET, 1)
  261.     end
  262.   end
  263. end
  264. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement