Advertisement
Holy87

Controller Mapper V2

Sep 19th, 2020
2,832
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 16.14 KB | None | 0 0
  1. #===============================================================================
  2. # Icone tasti di Holy87
  3. # Difficoltà utente: ★
  4. # Versione 2.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. # Novità: supporto alla gestione completa della tastiera e genera automaticamente
  10. # le icone dei tasti A-Z, 0-9 e simboli.
  11. #===============================================================================
  12. # Questo script vi permette di mostrare l'icona del pulsante del comando nel
  13. # gioco. La peculiarità principale di questo script è che sa riconoscere se
  14. # il giocatore sta usando un controller ed i comandi configurati, e mostrare
  15. # l'icona appropriata del tasto. Esempio:
  16. # Si vuole mostrare al giocatore il messaggio "Premi [Tasto corsa] per correre"
  17. # Quello che bisogna fare è scrivere nel box del messaggio
  18. # "Premi \K[0] per correre", dove \K è il nuovo comando del messaggio e 0 è
  19. # l'ID del comando (puoi controllare gli ID in basso nella configurazione,
  20. # dove è definito KEY_INDEXES)
  21. # Al giocatore verrà mostrato al posto di \K[0]
  22. # ● L'icona del tasto SHIFT sulla tastiera, se ha solo la tastiera;
  23. # ● L'icona del tasto Y del controller, se il controller è collegato;
  24. # ● Oppure l'icona del tasto che il giocatore ha scelto di configurare nelle
  25. #   opzioni di gioco come corsa.
  26. #
  27. # Per quanto riguarda la tastiera, è possibile far generare molti dei tasti
  28. # senza dover creare una grafica per ognuno. Ad esempio, puoi creare una
  29. # grafica dedicata per Spazio, Invio ecc... ma non vuoi perdere tempo nel
  30. # creare tutti i tasti dalla A alla Z, i numeri ed i simboli. Nella grafica
  31. # dei pulsanti della tastiera, usa il primo quadrato per il colore del testo
  32. # ed il secondo per il tasto vuoto... Lo script farà il resto!
  33. #
  34. # PER SMANETTONI:
  35. # Gli scripter possono usare il metodo delle finestre e delle bitmap
  36. # draw_key_icon(comando, x, y) -> disegna l'icona del pulsante del pad o della
  37. # tastiera a seconda di quale sia quello attivo per mostrare l'icona del comando
  38. # nella finestra, dove nel parametro comando inserirete il simbolo del comando
  39. # (:C, :B, :LEFT, :RIGHT ecc...)
  40. # draw_keyboard_icon(comando, x, y) -> disegna l'icona della tastiera
  41. # draw_gamepad_icon(comando, x, y) -> disegna l'icona del controller anche se
  42. # non collegato
  43. #
  44. #
  45. #===============================================================================
  46. # Istruzioni: inserire lo script sotto Materials, prima del Main e sotto lo
  47. # script di configurazione del controller (se presente, ma non è necessario
  48. # averlo se non si vuole usarlo)
  49. #===============================================================================
  50.  
  51. #===============================================================================
  52. # ** Impostazioni dello script
  53. #===============================================================================
  54. module Controller_Mapper
  55.   module Settings
  56.     # Questo array serve per mostrare il pulsante nella finestra di messaggio
  57.     # e di aiuto. Ad ogni indice corrisponde un pulsante. Ad esempio, se
  58.     # scrivi \K[2] nel messaggio, mostrerà l'icona del tasto invio della
  59.     # tastiera o del tasto A del gamepad (perché nell'array si riferisce a
  60.     # :C, dato che l'ordine parte da 0)
  61.     #               0   1   2   3   4   5   6   7   8      9       10   11     12
  62.     KEY_INDEXES = [:A, :B, :C, :X, :Y, :Z, :L, :R, :LEFT, :RIGHT, :UP, :DOWN, :DIR_KEY]
  63.  
  64.     # File immagine delle icone dei tasti del controller
  65.     PAD_ICONS_GRAPHIC = 'GamepadKeys'
  66.     # File immagine delle icone dei tasti della tastiera
  67.     KEY_ICONS_GRAPHIC = 'KeyboardKeys'
  68.  
  69.     # Comandi del gamepad con le rispettive icone (dell'immagine
  70.     # PAD_ICONS_GRAPHIC)
  71.     PAD_ICONS = {
  72.         # comando         indice
  73.         :DPAD_DOWN => 0,
  74.         :DPAD_LEFT => 1,
  75.         :DPAD_UP => 2,
  76.         :DPAD_RIGHT => 3,
  77.         :PAD_A => 4,
  78.         :PAD_B => 5,
  79.         :PAD_X => 6,
  80.         :PAD_Y => 7,
  81.         :DIR_KEY => 8,
  82.         :LEFT_SHOULDER => 9,
  83.         :RIGHT_SHOULDER => 10,
  84.         :L_TRIG => 11,
  85.         :R_TRIG => 12,
  86.         :L_AXIS_X => 13,
  87.         :L_AXIS_Y => 13,
  88.         :R_AXIS_X => 14,
  89.         :R_AXIS_Y => 14,
  90.         :L_AXIS_LEFT => 13,
  91.         :L_AXIS_RIGHT => 13,
  92.         :L_AXIS_UP => 13,
  93.         :L_AXIS_DOWN => 13,
  94.         :R_AXIS_LEFT => 14,
  95.         :R_AXIS_RIGHT => 14,
  96.         :R_AXIS_UP => 14,
  97.         :R_AXIS_DOWN => 14,
  98.         :PAD_BACK => 15,
  99.         :PAD_START => 16,
  100.         :LEFT_THUMB => 17,
  101.         :RIGHT_THUMB => 18
  102.     }
  103.  
  104.     # Tasti della tastiera con le rispettive icone (dell'immagine
  105.     # KEY_ICONS_GRAPHIC)
  106.     KEY_ICONS = {
  107.         :VK_ESCAPE => 3,
  108.         :VK_RETURN => 4,
  109.         :VK_PRIOR => 5,
  110.         :VK_NEXT => 6,
  111.         :VK_DOWN => 7,
  112.         :VK_LEFT => 8,
  113.         :VK_UP => 9,
  114.         :VK_RIGHT => 10,
  115.         :VK_SHIFT => 11,
  116.         :DIR_KEY => 12
  117.     }
  118.  
  119.     # Indice del bitmap dei tasti della tastiera dove ottenere
  120.     # il colore della lettera
  121.     FOREGROUND_COLOR_KEY_ICON_INDEX = 0
  122.     # indice del bitmap dei tasti della tastiera dove ottenere
  123.     # il tasto vuoto da scrivere
  124.     EMPTY_KEY_ICON_INDEX = 1
  125.     # rettangolo delle coordinate su cui scrivere la lettera
  126.     #                  X  Y  LARG ALT
  127.     GENERATION_RECT = [3, 2, 19, 17]
  128.  
  129.     # Impostazioni del font del pulsante per l'auto-generazione
  130.     GENERATION_FONT_NAME = 'Arial'
  131.     GENERATION_FONT_SIZE = 15
  132.     GENERATION_FONT_BOLD = true
  133.     GENERATION_FONT_ITALIC = false
  134.     GENERATION_FONT_SHADOW = false
  135.     GENERATION_FONT_OUTLINE = false
  136.  
  137.  
  138.     # Inserisci qui i tasti che genereranno l'icona automaticamente. L'icona
  139.     # generata prenderà l'unica lettera o simbolo che costituisce il nome
  140.     # del tasto. Assicurati che il nome del tasto non sia composto da più
  141.     # di un carattere, in caso contrario potresti avere degli artefatti.
  142.     # Puoi personalizzare il nome del tasto nello script dell'interfaccia da
  143.     # tastiera aggiungendolo in CUSTOM_KEY_TRANSLATION e cambiando, ad esempio,
  144.     # il tasto :VK_BACKSPACE con il simbolo 🔙 per essere stampato.
  145.     GENERATED_KEY_ICONS = [
  146.         :NUM_0, :NUM_1, :NUM_2, :NUM_3, :NUM_4, :NUM_5, :NUM_6, :NUM_7, :NUM_8,
  147.         :NUM_9, :KEY_A, :KEY_B, :KEY_C, :KEY_D, :KEY_E, :KEY_F, :KEY_G, :KEY_H,
  148.         :KEY_I, :KEY_J, :KEY_K, :KEY_L, :KEY_M, :KEY_N, :KEY_O, :KEY_P, :KEY_Q,
  149.         :KEY_R, :KEY_S, :KEY_T, :KEY_U, :KEY_V, :KEY_W, :KEY_X, :KEY_Y, :KEY_Z,
  150.         :VK_OEM_1, :VK_OEM_PLUS, :VK_OEM_COMMA, :VK_OEM_MINUS,
  151.         :VK_OEM_PERIOD, :VK_OEM_2, :VK_OEM_3, :VK_OEM_4, :VK_OEM_5, :VK_OEM_6,
  152.         :VK_OEM_7, :VK_OEM_8, :VK_OEM_102
  153.     ]
  154.   end
  155.  
  156.   # ---------ATTENZIONE: MODIFICARE SOTTO QUESTO PARAGRAFO COMPORTA SERI RISCHI
  157.   #           PER IL CORRETTO FUNZIONAMENTO DELLO SCRIPT! -------------
  158.  
  159.  
  160.   $imported = {} if $imported == nil
  161.   $imported['H87-ControllerMapper'] = 2.0
  162.  
  163.  
  164.   # Controller used condition
  165.   def self.use_controller?
  166.     $imported['H87-XInput'] && Input.controller_connected?
  167.   end
  168.  
  169.   # determines if the key icon is defined in KEY_ICONS setting
  170.   # @param [Symbol] key
  171.   def self.key_has_custom_icon?(key)
  172.     Settings::KEY_ICONS.keys.include?(key)
  173.   end
  174.  
  175.   # determines if the key has an auto-generated or custom icon
  176.   # @param [Symbol] key
  177.   def self.key_has_icon?(key)
  178.     return true if key_has_custom_icon?(key)
  179.     return true if Settings::GENERATED_KEY_ICONS.include?(key)
  180.     false
  181.   end
  182. end
  183.  
  184. module Cache
  185.   include Controller_Mapper::Settings
  186.  
  187.   # @param [Symbol] key
  188.   # @return [Bitmap]
  189.   def self.generated_keyboard_icon(key)
  190.     @generated_keys ||= {}
  191.     @generated_keys[key] = generate_key(Vocab.keyboard_button(key)) if @generated_keys[key].nil?
  192.     @generated_keys[key]
  193.   end
  194.  
  195.   # @param [String] key_name
  196.   # @return [Bitmap]
  197.   def self.generate_key(key_name)
  198.     bitmap = Bitmap.new(24, 24)
  199.     background = system(KEY_ICONS_GRAPHIC)
  200.     rect = Rect.new(24 * EMPTY_KEY_ICON_INDEX, 0, 24, 24)
  201.     bitmap.blt(0, 0, background, rect)
  202.     color = background.get_pixel(24 * FOREGROUND_COLOR_KEY_ICON_INDEX, 0)
  203.     bitmap.font.color = color
  204.     bitmap.font.size = GENERATION_FONT_SIZE
  205.     bitmap.font.name = GENERATION_FONT_NAME
  206.     bitmap.font.bold = GENERATION_FONT_BOLD
  207.     bitmap.font.italic = GENERATION_FONT_ITALIC
  208.     bitmap.font.outline = GENERATION_FONT_OUTLINE
  209.     bitmap.font.shadow = GENERATION_FONT_SHADOW
  210.     params = GENERATION_RECT
  211.     rect = Rect.new(params[0], params[1], params[2], params[3])
  212.     bitmap.draw_text(rect, key_name, 1)
  213.     bitmap
  214.   end
  215. end
  216.  
  217. #===============================================================================
  218. # ** Game_System
  219. #===============================================================================
  220. class Game_System
  221.   # @param [Symbol] command
  222.   # @return [Symbol]
  223.   def best_key_representation(command)
  224.     keys = adjust_keyboard(command)
  225.     return command if keys.nil?
  226.     return keys if keys.is_a?(Symbol)
  227.     return nil if keys.size == 0
  228.     return keys.first if keys.size == 1
  229.     #noinspection RubyArgCount
  230.     icons = keys.select { |key| Controller_Mapper.key_has_icon?(key) }
  231.     return icons.first unless icons.empty?
  232.     keys.first
  233.   end
  234. end
  235.  
  236. #===============================================================================
  237. # ** Bitmap
  238. #===============================================================================
  239. class Bitmap
  240.  
  241.   # Draws the key on the bitmap.
  242.   # @param [Symbol] key_symbol
  243.   # @param [Integer] x
  244.   # @param [Integer] y
  245.   def draw_key_icon(key_symbol, x, y, enabled = true)
  246.     return draw_gamepad_icon(key_symbol, x, y, enabled) if Controller_Mapper.use_controller?
  247.     draw_keyboard_icon(key_symbol, x, y, enabled)
  248.   end
  249.  
  250.   # force drawing the controller key icon
  251.   # @param [Symbol] key_symbol
  252.   # @param [Integer] x
  253.   # @param [Integer] y
  254.   # @param [Boolean] enabled
  255.   def draw_gamepad_icon(key_symbol, x, y, enabled = true)
  256.     bitmap = Cache.system(Controller_Mapper::Settings::PAD_ICONS_GRAPHIC)
  257.     index = Controller_Mapper::Settings::PAD_ICONS[$game_system.adjust_buttons(key_symbol)]
  258.     return if index.nil?
  259.     return if index < 0
  260.     rect = Rect.new(index * 24, 0, 24, 24)
  261.     opacity = enabled ? 255 : 128
  262.     blt(x, y, bitmap, rect, opacity)
  263.   end
  264.  
  265.   # force drawing the keyboard key icon
  266.   # @param [Symbol] key_symbol
  267.   # @param [Integer] x
  268.   # @param [Integer] y
  269.   # @param [Boolean] enabled
  270.   def draw_keyboard_icon(key_symbol, x, y, enabled = true)
  271.     key = $game_system.best_key_representation(key_symbol)
  272.     return draw_custom_keyboard_key_icon(key, x, y, enabled) if Controller_Mapper.key_has_custom_icon?(key)
  273.     draw_generated_keyboard_icon(key, x, y, enabled)
  274.   end
  275.  
  276.   private
  277.  
  278.   # draws an auto-generated key icon (with letter)
  279.   # @param [Symbol] key
  280.   # @param [Integer] x
  281.   # @param [Integer] y
  282.   # @param [Boolean] enabled
  283.   def draw_generated_keyboard_icon(key, x, y, enabled = true)
  284.     key_bmp = Cache.generated_keyboard_icon(key)
  285.     return if key_bmp.nil?
  286.     rect = Rect.new(0, 0, 24, 24)
  287.     blt(x, y, key_bmp, rect, enabled ? 255 : 128)
  288.   end
  289.  
  290.   # draws the keyboard key icon graphic loaded in System folder
  291.   # @param [Symbol] key_symbol
  292.   # @param [Integer] x
  293.   # @param [Integer] y
  294.   # @param [Boolean] enabled
  295.   def draw_custom_keyboard_key_icon(key_symbol, x, y, enabled = true)
  296.     bitmap = Cache.system(Controller_Mapper::Settings::KEY_ICONS_GRAPHIC)
  297.     index = Controller_Mapper::Settings::KEY_ICONS[$game_system.adjust_keyboard(key_symbol)]
  298.     return if index < 0
  299.     rect = Rect.new(index * 24, 0, 24, 24)
  300.     opacity = enabled ? 255 : 128
  301.     blt(x, y, bitmap, rect, opacity)
  302.   end
  303. end
  304.  
  305. #===============================================================================
  306. # ** Window_Base
  307. #===============================================================================
  308. class Window_Base < Window
  309.   alias h87_normal_process_escape process_escape_character unless $@
  310.   # Draws the proper key icon using automatically keyboard or
  311.   # gamepad if connected
  312.   # @param [Symbol] key
  313.   # @param [Integer] x
  314.   # @param [Integer] y
  315.   # @param [Boolean] enabled
  316.   def draw_key_icon(key, x, y, enabled = true)
  317.     contents.draw_key_icon(key, x, y, enabled)
  318.   end
  319.  
  320.   # force drawing the keyboard icon
  321.   # @param [Symbol] key
  322.   # @param [Integer] x
  323.   # @param [Integer] y
  324.   # @param [Boolean] enabled
  325.   def draw_keyboard_icon(key, x, y, enabled = true)
  326.     contents.draw_keyboard_icon(key, x, y, enabled)
  327.   end
  328.  
  329.   # force drawing the gamepad icon
  330.   # @param [Symbol] key
  331.   # @param [Integer] x
  332.   # @param [Integer] y
  333.   # @param [Boolean] enabled
  334.   def draw_gamepad_icon(key, x, y, enabled = true)
  335.     contents.draw_gamepad_icon(key, x, y, enabled)
  336.   end
  337.  
  338.   # Alias method: process_escape_character
  339.   # @param [String] code
  340.   # @param [String] text
  341.   # @param [Hash<Integer>] pos
  342.   def process_escape_character(code, text, pos)
  343.     if code.upcase == 'K'
  344.       process_draw_key(obtain_escape_param(text), pos)
  345.       return
  346.     end
  347.     h87_normal_process_escape(code, text, pos)
  348.   end
  349.  
  350.   # draws the key - gamepad icon, keyboard icon or key name
  351.   # @param [Integer] index
  352.   # @param [Hash] pos
  353.   def process_draw_key(index, pos)
  354.     symbol = Controller_Mapper::Settings::KEY_INDEXES[index]
  355.     puts "Key not found for index #{index}" if symbol.nil?
  356.     return process_draw_key_icon(index, pos) if Controller_Mapper.use_controller?
  357.     key = $game_system.best_key_representation(symbol)
  358.     if Controller_Mapper.key_has_icon?(key)
  359.       process_draw_key_icon(index, pos)
  360.     else
  361.       process_draw_key_name(index, pos)
  362.     end
  363.  
  364.   end
  365.  
  366.   # Draws the key icon
  367.   # @param [Integer] index
  368.   # @param [Hash<Integer>] pos
  369.   def process_draw_key_icon(index, pos)
  370.     symbol = Controller_Mapper::Settings::KEY_INDEXES[index]
  371.     puts "Key not found for index #{index}" if symbol.nil?
  372.     draw_key_icon(symbol, pos[:x], pos[:y])
  373.     pos[:x] += 24
  374.   end
  375. end
  376.  
  377. if $imported['H87-PadSettings'] # for showing the icons in the configuration
  378.   #===============================================================================
  379.   # ** Window_PadKeys
  380.   #===============================================================================
  381.   class Window_PadKeys < Window_Selectable
  382.     # Overwrite method
  383.     def draw_key_command(key, index)
  384.       rect = item_rect(index)
  385.       rect.width -= 4
  386.       change_color(normal_color)
  387.       draw_text(rect.x, rect.y, rect.width / 2, line_height, Vocab.command_name(key))
  388.       if $game_system.xinput_key_set[key]
  389.         if Controller_Mapper::Settings::PAD_ICONS[$game_system.adjust_buttons(key)] >= 0
  390.           x = rect.width / 4 * 3
  391.           draw_gamepad_icon(key, x, rect.y)
  392.         else
  393.           change_color(crisis_color)
  394.           draw_text(rect.width / 2, rect.y, rect.width / 2, line_height, Vocab.gamepad_key($game_system.xinput_key_set[key]), 1)
  395.         end
  396.       else
  397.         change_color(knockout_color)
  398.         draw_text(rect.width / 2, rect.y, rect.width / 2, line_height, ControllerSettings::NO_KEY_SET, 1)
  399.       end
  400.     end
  401.   end
  402. end
  403.  
  404. if $imported['H87-Keyboard_Settings']
  405.   #===============================================================================
  406.   # ** Input_Setting_Wrapper
  407.   #===============================================================================
  408.   class Input_Setting_Wrapper
  409.     # determines if icon can be drawn
  410.     def has_icon?
  411.       return false if @key.nil?
  412.       Controller_Mapper.key_has_icon?(@key)
  413.     end
  414.   end
  415.  
  416.   #===============================================================================
  417.   # ** Window_KeyboardKeys
  418.   #===============================================================================
  419.   class Window_KeyboardKeys < Window_Selectable
  420.     alias draw_text_key_command draw_key_command unless $@
  421.  
  422.     # @param [Input_Setting_Wrapper] key_obj
  423.     # @param [Integer] index
  424.     def draw_key_command(key_obj, index)
  425.       return draw_text_key_command(key_obj, index) unless key_obj.has_icon?
  426.       rect = item_rect(index)
  427.       rect.width -= 4
  428.       enabled = enable?(key_obj)
  429.       return if key_obj.key.nil?
  430.       x = rect.x + rect.width / 2 - 12
  431.       draw_keyboard_icon(key_obj.key, x, rect.y, enabled)
  432.     end
  433.   end
  434. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement