Holy87

Text Input

Sep 20th, 2020 (edited)
947
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 22.23 KB | None | 0 0
  1. #===============================================================================
  2. # Inserimento testo da Tastiera di Holy87
  3. # Difficoltà utente: ★★★★
  4. # Versione 1.2
  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. # changelog
  10. # v1.2 => • Possibilità di incollare il testo con CTRL+V
  11. #         • Possibilità di impostare un handler su aggiornamento
  12. #           del testo come evento (set_handler (:change, method(:nome_metodo)))
  13. # v1.1 => • Possibilità di inserire un placeholder
  14. #         • Possibilità di disattivare a capo automatico
  15. #         • Possibilità di impostare un testo (window.text=)
  16. #         • Modifica ai simboli
  17. #         • Quando si ottiene il testo, ora vengono rimossi autoamticamente
  18. #           spazi all'inizio ed alla fine
  19. #         • Miglioramenti e risoluzione di bug
  20. #===============================================================================
  21. # Questo script permette agli scripter di creare delle finestre dove è possibile
  22. # inserire il testo direttamente dalla tastiera in modo semplice e completamente
  23. # personalizzabile.
  24. #
  25. # FUNZIONI SPECIALI:
  26. # ● Puoi decidere quali caratteri sono permessi (ad es. solo lettere, numeri o
  27. #   anche simboli e spaziature)
  28. # ● Puoi forzare il testo in maiuscolo
  29. # ● Puoi creare un inserimento password, offuscando il testo con un pallino •
  30. # ● Puoi decidere il numero massimo di righe
  31. # ● Puoi decidere un numero massimo di caratteri (o lasciare decidere alla
  32. #   finestra di non permettere inserimenti quando lo spazio finisce)
  33. # ● Segue il layout della tastiera, nessuna configurazione!
  34. # ● Puoi impostare un placeholder (testo descrittivo) prima di scrivere
  35. # ● Puoi scegliere se poter andare a capo automaticamente o no
  36. # ● Puoi far incollare il testo copiato negli appunti con CTRL+V
  37. #
  38. #===============================================================================
  39. # ISTRUZIONI: inserire lo script sotto Materials, prima del Main. Richiede
  40. # lo script "Interfaccia Tastiera".
  41. #
  42. #  Puoi creare una nuova finestra della classe Window_TextInput (o creando una
  43. #  sottoclasse se vuoi personalizzarla meglio) o creare qualcosa di completamente
  44. #  nuovo includendo il modulo Text_Inputtable (se sei esperto!)
  45. #
  46. # ■ Utilizzo:
  47. # crea una finestra con Window_TextInput.new(x, y, width, height, options)
  48. # per quanto riguarda x, y, width ed height non ci sono particolari spiegazioni
  49. # da dare.
  50. # Il parametro options è un hash che include tutte le opzioni di personalizzazione
  51. # della tastiera:
  52. # - force_upcase: se true, tutti i caratteri sono scritti in maiuscolo (false se
  53. #   non metti l'opzione)
  54. # - max_characters: il numero massimo di caratteri (0 di default, infinito o fino
  55. #   a quando lo spazio nella finestra lo permette)
  56. # - max_lines: il numero di righe disponibile per l'inserimento del testo (1 di
  57. #   default). Se il numero di righe è 1, il tasto INVIO conferma il testo.
  58. #   Se invece sono permesse più righe, il tasto INVIO va a capo mentre per
  59. #   confermare devi premere CTRL+INVIO.
  60. # - password: Se true, tutto il testo visibile viene offuscato con un carattere
  61. #   speciale (false di default)
  62. # - placeholder: una stringa che viene mostrata quando il campo è vuoto
  63. # - text_prap: se true, il testo va a capo automaticamente (true di default)
  64. # - permit_paste: determina se permettere o no l'inserimento di testo copiato
  65. #   dagli appunti
  66. # - permitted: i tipi di caratteri permessi. Puoi assegnare questi valori:
  67. #   * Text_Inputable::WORDS: sono ammesse solo lettere
  68. #   * Text_Inputable::NUMERIC: sono ammessi solo numeri
  69. #   * Text_Inputable::SPACING: sono ammessi solo spazi
  70. #   * Text_Inputable::ALPHA_WITH_SPACING: sono ammesse lettere, numeri e spazi
  71. #   * Text_Inputable::BASE_SYMBOLS: sono ammessi solo i simboli -, _, ., e @
  72. #   * Text_Inputable::STANDARD_SYMBOLS: sono ammessi altri simboli
  73. #   * Text_Inputable::ALL_SYMBOLS: sono ammessi tutti i simboli
  74. #   * Text_Inputable::NO_RESTRICTIONS: tutto il testo è permesso.
  75. #   Se non specificato, il parametro di default è Text_Inputable::NO_RESTRICTIONS.
  76. #   Puoi anche inserire delle combinazioni, ad esempio se metti come paraemtro
  77. #   Text_Inputable::WORDS|Text_Inputable::NUMERIC (separati da |) puoi permettere
  78. #   l'inserimento di lettere e numeri.
  79. # - ok_callback: il metodo che viene chiamato quando l'utente conferma l'inserimento
  80. # - cancel_callback: il metodo che viene chiamato quando l'utente annulla
  81. # - change_callback: il metodo che viene chiamato quando il testo cambia
  82. #   l'inserimento con ESC. Se non è definito, l'inserimento non può essere
  83. #   annullato.
  84. #   * Nota: puoi assegnare i metodi ok e cancel anche chiamando le funzioni
  85. #     set_done_handler e set_cancel_handler della finestra.
  86. #
  87. # ■ Esempio d'uso:
  88. #   options = {
  89. #     max_characters: 100,
  90. #     max_lines: 2,
  91. #     permitted_values: Text_Inputable::WORDS|Text_Inputable::SPACING
  92. #   }
  93. #   @text_window = Window_TextInput.new(0, 0, 140, 69, options)
  94. #   @text_window.set_done_handler(method(:process_text))
  95. #   @text_window.set_cancel_handler(method(:return_scene))
  96. #   @text_window.activate
  97. #
  98. #
  99. #
  100. #===============================================================================
  101.  
  102. #===============================================================================
  103. # ** Impostazioni dello script
  104. #===============================================================================
  105. module Input_Keyboard
  106.   # questi sono i tasti abilitati per l'input da tastiera
  107.   LISTENED_KEYS = [
  108.       :NUM_0, :NUM_1, :NUM_2, :NUM_3, :NUM_4, :NUM_5, :NUM_6, :NUM_7, :NUM_8,
  109.       :NUM_9, :KEY_A, :KEY_B, :KEY_C, :KEY_D, :KEY_E, :KEY_F, :KEY_G, :KEY_H,
  110.       :KEY_I, :KEY_J, :KEY_K, :KEY_L, :KEY_M, :KEY_N, :KEY_O, :KEY_P, :KEY_Q,
  111.       :KEY_R, :KEY_S, :KEY_T, :KEY_U, :KEY_V, :KEY_W, :KEY_X, :KEY_Y, :KEY_Z,
  112.       :VK_NUMPAD0, :VK_NUMPAD1, :VK_NUMPAD2, :VK_NUMPAD3, :VK_NUMPAD4,
  113.       :VK_NUMPAD5, :VK_NUMPAD6, :VK_NUMPAD7, :VK_NUMPAD8, :VK_NUMPAD9,
  114.       :VK_MULTIPLY , :VK_ADD, :VK_SEPARATOR, :VK_SUBTRACT, :VK_DECIMAL,
  115.       :VK_DIVIDE, :VK_OEM_1, :VK_OEM_PLUS, :VK_OEM_COMMA , :VK_OEM_MINUS ,
  116.       :VK_OEM_PERIOD, :VK_OEM_2, :VK_OEM_3, :VK_OEM_4, :VK_OEM_5, :VK_OEM_6,
  117.       :VK_OEM_7, :VK_OEM_8, :VK_OEM_102, :VK_SPACE
  118.   ]
  119.  
  120.   # Configura i suoni dell'input
  121.   #               SE name      Volume  Pitch
  122.   INPUT_SE =      ['Cursor1',  100,    150]
  123.   BACKSPACE_SE =  ['Cursor1',  100,    100]
  124.   SPACE_SE =      ['Cursor1',  100,     50]
  125.   FORBIDDEN_SE =  ['Cursor2',  100,     50]
  126.   PASTE_SE =      ['Cursor2',  100,    150]
  127.  
  128.   # carattere mostrato quando si digita una password
  129.   PASSWORD_CHARACTER = '•'
  130. end
  131.  
  132.  
  133.  
  134. # --- NON MODIFICARE OLTRE SE NON SAI QUELLO CHE FAI! ---
  135.  
  136. #===============================================================================
  137. # ** Text_Inputable
  138. #-------------------------------------------------------------------------------
  139. # modulo per la gestione del testo da una tastiera.
  140. #===============================================================================
  141. module Text_Inputable
  142.   WORD_PATTERN = /[A--ú]/i
  143.   NUMERIC_PATTERN = /\d/
  144.   SPACING_PATTERN = /[\s\t]/
  145.   BASE_SYMBOL_PATTERN = /[\-_\.@,\']/
  146.  STANDARD_SYMBOL_PATTERN = /[!\?"\+\*\(\)<\>=\#&\%\^\\|\/°]/
  147.  
  148.  # SETTINGS CONSTANTS
  149.  WORDS               = 0x01 # only alphabetic characters
  150.  NUMERIC             = 0x02 # only numeric characters (0-9)
  151.  ALPHANUMERIC        = 0x03 # alphabetic and numeric
  152.  SPACING             = 0x04 # spaces and tabs
  153.  ALPHA_WITH_SPACING  = 0x0F # all above
  154.  BASE_SYMBOLS        = 0x10 # -, _, ., @, ,, '
  155.   STANDARD_SYMBOLS    = 0x30 # !, ?, (, ), ", +, *, <, >, =, #, &, %, ^, \, |, /, ° and above
  156.   ALL_SYMBOLS         = 0x70 # no symbol limit.
  157.   NO_RESTRICTIONS     = 0x7F # all alphanumeric, spacing an symbols
  158.  
  159.   SPACE_CHARACTER   = " "
  160.   NEWLINE_CHARACTER = "\n"
  161.  
  162.   # @return[String]
  163.   attr_reader :text
  164.   # @return[Integer] the max number of lines
  165.   attr_accessor :max_lines
  166.   # @return [Integer] the maximum number of characters
  167.   attr_accessor :max_characters
  168.   # @return [Integer]
  169.   attr_accessor :permitted_values
  170.   # @return [String]
  171.   attr_accessor :placeholder
  172.   attr_accessor :text_wrap
  173.  
  174.   # inizializza i parametri principali dell'input
  175.   # @param [Hash] options
  176.   def initialize_input_parameters(options)
  177.     @text = ''
  178.     @handlers = {}
  179.     @force_upcase = options[:upcase] || false
  180.     @permitted_values = options[:permitted] || NO_RESTRICTIONS
  181.     @max_characters = options[:max_characters] || 0
  182.     @max_lines = options[:max_lines] || 1
  183.     @handlers[:ok] = options[:ok_callback]
  184.     @handlers[:cancel] = options[:cancel_callback]
  185.     @handlers[:change] = options[:change_callback]
  186.     @password_hide = options[:password] || false
  187.     @placeholder = options[:placeholder]
  188.     @text_wrap = options[:text_wrap].nil? ? true : options[:text_wrap]
  189.     @permit_paste = options[:permit_paste] || false
  190.     @current_line = 0
  191.     @line_x = [0]
  192.   end
  193.  
  194.   # aggiorna l'inserimento da tastiera
  195.   def update_input
  196.     return unless active?
  197.     process_done
  198.     process_escape
  199.     return process_paste if Input.paste_command?
  200.     process_input_return
  201.     process_input_backspace
  202.     process_input_character
  203.   end
  204.  
  205.   # restituisce il numero di caratteri
  206.   def characters_count
  207.     @text.size
  208.   end
  209.  
  210.   # determina il numero di caratteri rimanenti
  211.   def remaining_characters
  212.     @max_characters - characters_count
  213.   end
  214.  
  215.   # restituisce il conteggio delle parole
  216.   def word_count
  217.     @text.split(/[\s\t\n\r]+/).size
  218.   end
  219.  
  220.   def add_character(character)
  221.     raise NotImplementedError
  222.   end
  223.  
  224.   # deactivates all
  225.   def deactivate
  226.     raise NotImplementedError
  227.   end
  228.  
  229.   def active?
  230.     raise NotImplementedError
  231.   end
  232.  
  233.   def process_input_character
  234.     return unless active?
  235.     key = Input.any_keyboard_input?
  236.     return unless key
  237.     begin
  238.       character = Keyboard.unicode_char(key)
  239.     rescue
  240.       puts sprintf('Error in processing character for %s', key)
  241.       return
  242.     end
  243.     return unless character_permitted?(character)
  244.     add_character(character)
  245.   end
  246.  
  247.   def process_paste
  248.     raise NotImplementedError
  249.   end
  250.  
  251.   def process_input_return
  252.     raise NotImplementedError
  253.   end
  254.  
  255.   def process_input_backspace
  256.     raise NotImplementedError
  257.   end
  258.  
  259.   def process_escape
  260.     return unless active?
  261.     return unless Input.escape_triggered?
  262.     return unless handle? :cancel
  263.     deactivate
  264.     call_handler :cancel
  265.   end
  266.  
  267.   def process_done
  268.     return unless active?
  269.     if @max_lines > 1
  270.       return unless Input.force_return?
  271.     else
  272.       return unless Input.return_pressed?
  273.     end
  274.     return unless handle? :ok
  275.     deactivate
  276.     call_handler :ok
  277.   end
  278.  
  279.   # set the function when the user press CTRL + RETURN
  280.   # @param [Method] handler
  281.   def set_done_handler(handler)
  282.     @handlers[:ok] = handler
  283.   end
  284.  
  285.   # set the function when the user press ESC
  286.   # @param [Method] handler
  287.   def set_cancel_handler(handler)
  288.     @handlers[:cancel] = handler
  289.   end
  290.  
  291.   # @param [Symbol] symbol
  292.   # @param [Method] method
  293.   def set_handler(symbol, method)
  294.     @handlers[symbol] = method
  295.   end
  296.  
  297.   def call_handler(symbol)
  298.     return unless handle? symbol
  299.     @handlers[symbol].call
  300.   end
  301.  
  302.   def handle?(symbol)
  303.     @handlers[symbol] != nil
  304.   end
  305.  
  306.   # @param [String] character
  307.   def character_permitted?(character)
  308.     return true if permitted?(ALL_SYMBOLS)
  309.     return true if character =~ WORD_PATTERN and permitted?(WORDS)
  310.     return true if character =~ SPACING_PATTERN and permitted?(SPACING)
  311.     return true if character =~ NUMERIC_PATTERN and permitted?(NUMERIC)
  312.     return true if character =~ BASE_SYMBOL_PATTERN and permitted?(BASE_SYMBOLS)
  313.     return true if character =~ STANDARD_SYMBOL_PATTERN and permitted?(STANDARD_SYMBOLS)
  314.     false
  315.   end
  316.  
  317.   def permitted?(const_setting)
  318.     (@permitted_values | const_setting) == @permitted_values
  319.   end
  320.  
  321.   # determina se tutto viene scritto in maiuscolo
  322.   def force_upcase?
  323.     @force_upcase
  324.   end
  325.  
  326.   def password?
  327.     @password_hide
  328.   end
  329.  
  330.   def space_remaining?(char)
  331.     raise NotImplementedError
  332.   end
  333.  
  334.   def play_input_se
  335.     set = Input_Keyboard::INPUT_SE
  336.     RPG::SE.new(set[0], set[1], set[2]).play
  337.   end
  338.  
  339.   def play_backspace_se
  340.     set = Input_Keyboard::BACKSPACE_SE
  341.     RPG::SE.new(set[0], set[1], set[2]).play
  342.   end
  343.  
  344.   def play_space_se
  345.     set = Input_Keyboard::SPACE_SE
  346.     RPG::SE.new(set[0], set[1], set[2]).play
  347.   end
  348.  
  349.   def play_forbidden_se
  350.     set = Input_Keyboard::FORBIDDEN_SE
  351.     RPG::SE.new(set[0], set[1], set[2]).play
  352.   end
  353.  
  354.   def play_paste_se
  355.     set = Input_Keyboard::PASTE_SE
  356.     RPG::SE.new(set[0], set[1], set[2]).play
  357.   end
  358.  
  359.   # @return [String]
  360.   def password_char
  361.     Input_Keyboard::PASSWORD_CHARACTER
  362.   end
  363. end
  364.  
  365.  
  366. #===============================================================================
  367. # ** Window_TextInput
  368. #-------------------------------------------------------------------------------
  369. # Finestra per immettere testo da una tastiera.
  370. #===============================================================================
  371. class Window_TextInput < Window_Base
  372.   include Text_Inputable
  373.   attr_accessor :active
  374.  
  375.   def initialize(x, y, width, height, options = {})
  376.     initialize_input_parameters(options)
  377.     super(x, y, width, height)
  378.     clear_text
  379.     create_cursor_sprite
  380.     deactivate
  381.   end
  382.  
  383.   # returns the text with spacing removed
  384.   # @return [String]
  385.   def text
  386.     @text.strip
  387.   end
  388.  
  389.   # @param [String] new_text
  390.   def text=(new_text)
  391.     return if @text == new_text
  392.     clear_text
  393.     add_text(new_text)
  394.   end
  395.  
  396.   # @param [String] new_text
  397.   def add_text(new_text)
  398.     new_text.each_char { |c| break unless add_character(c.chr, true) }
  399.     call_handler :change
  400.   end
  401.  
  402.   # restituisce il rettangolo di  input
  403.   # @return [Rect]
  404.   def text_input_rect
  405.     Rect.new(0, 0, contents_width, contents_height)
  406.   end
  407.  
  408.   def refresh
  409.     draw_placeholder if @text.empty?
  410.   end
  411.  
  412.   def refresh_cursor_position
  413.     return if @cursor.nil?
  414.     @cursor.x = padding + self.x + text_x + 1
  415.     @cursor.y = padding + self.y + text_y
  416.     @cursor.z = self.z + 10
  417.     @cursor.force_show if active? && open?
  418.   end
  419.  
  420.   def clear_text
  421.     contents.clear_rect(text_input_rect)
  422.     @text = ''
  423.     @line_x = [0]
  424.     @current_line = 0
  425.     refresh_cursor_position
  426.     call_handler :change
  427.     refresh
  428.   end
  429.  
  430.   def draw_placeholder
  431.     return unless @placeholder
  432.     change_color(normal_color, false)
  433.     rect = text_input_rect
  434.     rect.height = line_height
  435.     draw_text(rect, @placeholder)
  436.     change_color normal_color
  437.   end
  438.  
  439.   def create_cursor_sprite
  440.     @cursor = Sprite_InputCursor.new(normal_color, 1, line_height - 6, self.viewport)
  441.     @cursor.visible = false
  442.     @cursor.oy -= 3
  443.   end
  444.  
  445.   # determina se c'è abbastanza spazio per inserire il carattere
  446.   # @param [String] character
  447.   # @return [TrueClass, FalseClass]
  448.   def space_remaining?(character)
  449.     return false if character_limit? && remaining_characters <= 0
  450.     return true if @text_wrap and !max_line_reached?
  451.     return false if remaining_line_space - text_size(character).width < 0
  452.     true
  453.   end
  454.  
  455.   # aggiornamento
  456.   def update
  457.     super
  458.     update_input
  459.     update_cursor
  460.   end
  461.  
  462.   # aggiornamento del cursore
  463.   def update_cursor
  464.     return unless active?
  465.     return unless open?
  466.     @cursor.update
  467.   end
  468.  
  469.   # rende invisibile il cursore alla chiusura
  470.   def close
  471.     super
  472.     @cursor.visible = false
  473.   end
  474.  
  475.   # rende visibile il cursore all'apertura
  476.   def open
  477.     super
  478.     @cursor.visible = true
  479.   end
  480.  
  481.   def visible=(value)
  482.     super
  483.     @cursor.visible = value
  484.   end
  485.  
  486.   def activate
  487.     Input.update
  488.     @cursor.visible = true
  489.     @active = true
  490.   end
  491.  
  492.   def deactivate
  493.     @cursor.visible = false
  494.     @active = false
  495.     Input.update
  496.   end
  497.  
  498.   def active?
  499.     @active
  500.   end
  501.  
  502.   # @param [Fixnum] val
  503.   def x=(val)
  504.     super
  505.     refresh_cursor_position
  506.   end
  507.  
  508.   # @param [Fixnum] val
  509.   def y=(val)
  510.     super
  511.     refresh_cursor_position
  512.   end
  513.  
  514.   # @param [Fixnum] val
  515.   def z=(val)
  516.     super
  517.     refresh_cursor_position
  518.   end
  519.  
  520.   # @param [Viewport] viewport
  521.   def viewport=(viewport)
  522.     super
  523.     @cursor.viewport = viewport
  524.   end
  525.  
  526.   # aggiunge il carattere, va a capo se necessario ed aggiorna
  527.   # il cursore
  528.   # @param [String] character
  529.   # @param [Boolean] hidden
  530.   def add_character(character, hidden = false)
  531.     character.upcase! if force_upcase?
  532.     return true unless character_permitted?(character)
  533.     unless space_remaining?(character)
  534.       play_forbidden_se unless hidden
  535.       return false
  536.     end
  537.     contents.clear_rect(text_input_rect) if @placeholder != nil and @text.empty?
  538.     character =~ SPACING_PATTERN ? play_space_se : play_input_se unless hidden
  539.     add_new_line if remaining_line_space - char_width(character) < 0
  540.     write_character(character)
  541.     @line_x[@current_line] += char_width(character)
  542.     refresh_cursor_position
  543.     @text << character
  544.     call_handler(:change) unless hidden
  545.     true
  546.   end
  547.  
  548.   # rimuove l'ultimo carattere aggiunto ed aggiorna la grafica
  549.   def remove_character
  550.     return play_forbidden_se if @text.empty?
  551.     play_backspace_se
  552.     character = last_character
  553.     @line_x[@current_line] -= char_width(character)
  554.     refresh_cursor_position
  555.     @text.chop!
  556.     delete_character(character)
  557.     remove_line if text_x <= 0 && last_character != NEWLINE_CHARACTER && @current_line > 0
  558.     clear_text if @text.empty?
  559.     call_handler :change
  560.   end
  561.  
  562.   # aggiunge una nuova riga
  563.   def process_input_return
  564.     return unless Input.return_pressed?
  565.     return play_forbidden_se if max_line_reached?
  566.     play_input_se
  567.     add_new_line
  568.     @text << NEWLINE_CHARACTER
  569.   end
  570.  
  571.   # incolla il testo dagli appunti
  572.   def process_paste
  573.     return unless active?
  574.     return unless @permit_paste
  575.     play_paste_se
  576.     add_text Clipboard.read_text_safe
  577.   end
  578.  
  579.   # esecuzione del tasto BACKSPACE
  580.   def process_input_backspace
  581.     return unless Input.backspace_pressed?
  582.     remove_character
  583.   end
  584.  
  585.   # disegna il carattere sul contenuto
  586.   # @param [String] character
  587.   def write_character(character)
  588.     character = password_char if password?
  589.     width = text_size(character).width
  590.     draw_text(text_x, text_y, width + 1, line_height, character)
  591.   end
  592.  
  593.   # cancella il carattere dalla bitmap
  594.   def delete_character(character)
  595.     rect = Rect.new(text_x, text_y, char_width(character), line_height)
  596.     contents.clear_rect(rect)
  597.   end
  598.  
  599.   # determina se la finestra ha un limite di caratteri
  600.   def character_limit?
  601.     @max_characters > 0
  602.   end
  603.  
  604.   # determina se è stata raggiunta l'ultima riga disponibile
  605.   # per l'inserimento del testo
  606.   def max_line_reached?
  607.     @current_line == @max_lines - 1
  608.   end
  609.  
  610.   # restituisce lo spazio rimanente nella riga attuale
  611.   def remaining_line_space
  612.     text_input_rect.width - (text_x + text_input_rect.x)
  613.   end
  614.  
  615.   # aggiunge una nuova riga
  616.   def add_new_line
  617.     @current_line += 1
  618.     @line_x[@current_line] = 0
  619.     refresh_cursor_position
  620.   end
  621.  
  622.   # rimuove l'ultima riga e torna su
  623.   def remove_line
  624.     @current_line -= 1
  625.     refresh_cursor_position
  626.   end
  627.  
  628.   # @param [String] character
  629.   # @return [Integer]
  630.   def char_width(character)
  631.     return 0 if character == NEWLINE_CHARACTER
  632.     return text_size(password_char).width if password?
  633.     text_size(character).width
  634.   end
  635.  
  636.   # restituisce la coordinata X del testo rispetto al rettangolo di
  637.   # input nella riga attuale
  638.   def text_x
  639.     text_input_rect.x + @line_x[@current_line]
  640.   end
  641.  
  642.   # restituisce la coordinata Y del testo
  643.   def text_y
  644.     text_input_rect.y + line_height * @current_line
  645.   end
  646.  
  647.   # restituisce l'ultimo carattere del testo (per cancellazione)
  648.   # @return [String]
  649.   def last_character
  650.     return '' if @text.empty?
  651.     @text[-1].chr
  652.   end
  653.  
  654.   # eliminazione (anche per la grafica del cursore)
  655.   def dispose
  656.     @cursor.dispose
  657.     super
  658.   end
  659. end
  660.  
  661. #===============================================================================
  662. # ** Sprite_InputCursor
  663. #-------------------------------------------------------------------------------
  664. # Sprite speciale specifico per i cursori
  665. #===============================================================================
  666. class Sprite_InputCursor < Sprite
  667.   # @param [Color] color
  668.   # @param [Fixnum] width
  669.   # @param [Fixnum] height
  670.   # @param [Viewport] viewport
  671.   def initialize(color, width, height, viewport=nil)
  672.     super(viewport)
  673.     self.bitmap = Bitmap.new(width, height)
  674.     self.bitmap.fill_rect(0, 0, bitmap.width, bitmap.height, color)
  675.     @blink_timing = 0
  676.   end
  677.  
  678.   # aggiornamento
  679.   def update
  680.     super
  681.     update_blink
  682.   end
  683.  
  684.   # aggiorna la grafica (mostra/nascondi)
  685.   def update_blink
  686.     @blink_timing += 1
  687.     return if @blink_timing < 30
  688.     @blink_timing = 0
  689.     self.visible = !self.visible
  690.   end
  691.  
  692.   # the cursor should be visible while typing
  693.   def force_show
  694.     self.visible = true
  695.     @blink_timing = 0
  696.   end
  697. end
  698.  
  699. #===============================================================================
  700. # ** Input
  701. #-------------------------------------------------------------------------------
  702. # Aggiunta di metodi per l'inserimento da tastiera
  703. #===============================================================================
  704. module Input
  705.  
  706.   # determina (e viene restituito) se è stato premuto un tasto della tastiera
  707.   # che è assegnato ad una lettera, numero o simbolo.
  708.   def self.any_keyboard_input?
  709.     Input_Keyboard::LISTENED_KEYS.each { |key| return key if repeat?(key) }
  710.     false
  711.   end
  712.  
  713.   # determina se è stato premuto il tasto INVIO
  714.   def self.return_pressed?
  715.     repeat?(:VK_RETURN)
  716.   end
  717.  
  718.   # determina se è stato premuto il tasto BACKSPACE
  719.   def self.backspace_pressed?
  720.     repeat?(:VK_BACK)
  721.   end
  722.  
  723.   # determina se è stato premuto il tasto CANC
  724.   def self.canc_pressed?
  725.     repeat?(:VK_DELETE)
  726.   end
  727.  
  728.   # determina se è stato premuto il tasto ESC
  729.   def self.escape_triggered?
  730.     trigger?(:VK_ESCAPE)
  731.   end
  732.  
  733.   # determina se è stato premuto CTRL + INVIO.
  734.   def self.force_return?
  735.     press?(:VK_CONTROL) && trigger?(:VK_RETURN)
  736.   end
  737.  
  738.   def self.paste_command?
  739.     press?(:VK_CONTROL) && trigger?(:KEY_V)
  740.   end
  741. end
Add Comment
Please, Sign In to add comment