Advertisement
Guest User

Keyboard Text Input

a guest
Mar 16th, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 12.12 KB | None | 0 0
  1. #==============================================================================
  2. # ** Text Input script
  3. #------------------------------------------------------------------------------
  4. # author:  erpicci
  5. # version: 1.0
  6. # require: CP Keyboard Input script  by Neon Black
  7. #
  8. #------------------------------------------------------------------------------
  9. # * Introduction
  10. #------------------------------------------------------------------------------
  11. # This script allows the player to insert a medium-sized text by typing
  12. # directly from the keyboard. Inserted text is returned as a string.
  13. #
  14. # Requires Neon Black's Keyboard Input Script to work.
  15. #
  16. # It is completely free, both for commercial and non-commercial use. All you
  17. # are asked to do is to give me credits for the script.
  18. #
  19. #------------------------------------------------------------------------------
  20. # * Installation
  21. #------------------------------------------------------------------------------
  22. # Be sure to have Neon Black's Keyboard Input Script installed.
  23. # Place this script in the "Materials" section of the scripts above main.
  24. #
  25. # To insert a text using an event, create a script event like:
  26. #   text_var = text_input("Window Title")
  27. #
  28. # To have a script call the text input, use:
  29. #   text_var = Scene_Text.read("Window Title")
  30. #
  31. # Only window title is mandatory. As optional arguments, you can set:
  32. #  * alignment: "center" (default), "left", "right"
  33. #  * maximum number of character: default is 104
  34. #  * left-to-right: default is true, use false to get a right-to-left input
  35. #
  36. #------------------------------------------------------------------------------
  37. # * Examples
  38. #------------------------------------------------------------------------------
  39. #   # Open a window whose title is "Talk to Bobby", text is centered, allows
  40. #   # to enter up to 20 characters, writing is right-to-left
  41. #   variable = Scene_Text.read("Talk to Bobby", "center", 20, false)
  42. #
  43. #   # Open a window whose title is "Tell me something", text is aligned to
  44. #   # right, allows to enter up to 88 characters, writing is left-to-right
  45. #   # (default)
  46. #   variable = Scene_Text.read("Talk to Bobby", "center", 20)
  47. #
  48. #   # Open a window whose title is "Insert text", text is centered (default),
  49. #   # accepts up to 104 characters (default), writing is left-to-right (default)
  50. #   variable = Scene_Text.read("Talk to Bobby")
  51. #==============================================================================
  52.  
  53. module Text_Input
  54.   ACCEPTED_KEYS = {
  55.     :k0 => 48, :k1 => 49, :k2 => 50, :k3 => 51, :k4 => 52,
  56.     :k5 => 53, :k6 => 54, :k7 => 55, :k8 => 56, :k9 => 57,
  57.    
  58.     :kA => 65, :kB => 66, :kC => 67, :kD => 68, :kE => 69, :kF => 70,
  59.     :kG => 71, :kH => 72, :kI => 73, :kJ => 74, :kK => 75, :kL => 76,
  60.     :kM => 77, :kN => 78, :kO => 79, :kP => 80, :kQ => 81, :kR => 82,
  61.     :kS => 83, :kT => 84, :kU => 85, :kV => 86, :kW => 87, :kX => 88,
  62.     :kY => 89, :kZ => 90,
  63.    
  64.     :kCOLON     => 186, :kQUOTE     => 222, :kSPACE      => 32,
  65.     :kCOMMA     => 188, :kPERIOD    => 190, :kSLASH      => 191,
  66.     :kBACKSLASH => 220, :kLEFTBRACE => 219, :kRIGHTBRACE => 221,
  67.     :kMINUS     => 189, :kEQUAL     => 187, :kTILDE      => 192,          
  68.   }
  69. end
  70.  
  71.  
  72.  
  73. #==============================================================================
  74. # ** Scene_Text
  75. #------------------------------------------------------------------------------
  76. #  This class shows an input field for the player.
  77. #==============================================================================
  78.  
  79. class Scene_Text < Scene_MenuBase
  80.   ALIGN    = "center"
  81.   MAX_CHAR = 104
  82.   LTR      = true
  83.  
  84.   #--------------------------------------------------------------------------
  85.   # * Show a dialog window to the player
  86.   #--------------------------------------------------------------------------
  87.   def self.read(title, align = ALIGN, max_char = MAX_CHAR, ltr = LTR)
  88.     @@text     = ""
  89.     @@title    = title
  90.     @@align    = align
  91.     @@max_char = max_char
  92.     @@ltr      = ltr
  93.     SceneManager.call(Scene_Text)
  94.     Fiber.yield while SceneManager.scene_is?(Scene_Text)
  95.     return @@text
  96.   end
  97.   #--------------------------------------------------------------------------
  98.   # * Start Processing
  99.   #--------------------------------------------------------------------------
  100.   def start
  101.     super
  102.     @edit_window  = Window_TextEdit.new(@@title, @@align, @@max_char, @@ltr)
  103.     @edit_window.set_handler(:ok, method(:on_input_ok))
  104.   end
  105.   #--------------------------------------------------------------------------
  106.   # * Set text when done
  107.   #--------------------------------------------------------------------------
  108.   def on_input_ok
  109.     @@text = @edit_window.text
  110.     return_scene
  111.   end
  112. end
  113.  
  114.  
  115.  
  116. #==============================================================================
  117. # ** Window_TextEdit
  118. #------------------------------------------------------------------------------
  119. #  This window allows to edit a text.
  120. #==============================================================================
  121.  
  122. class Window_TextEdit < Window_Selectable
  123.   include Text_Input
  124.   #--------------------------------------------------------------------------
  125.   # * Public Instance Variables
  126.   #--------------------------------------------------------------------------
  127.   attr_reader   :text
  128.  
  129.   #--------------------------------------------------------------------------
  130.   # * Object Initialization
  131.   #--------------------------------------------------------------------------
  132.   def initialize(title, align = "center", max_char = 104, ltr = true)
  133.     x = 0
  134.     y = Graphics.height - fitting_height(6)
  135.     super(x, y, Graphics.width, fitting_height(6))
  136.     @text     = ""
  137.     @title    = title
  138.     @align    = align
  139.     @max_char = max_char
  140.     @ltr      = ltr
  141.     @index    = @text.size
  142.     activate
  143.     refresh
  144.   end
  145.   #--------------------------------------------------------------------------
  146.   # * Revert to Default Text
  147.   #--------------------------------------------------------------------------
  148.   def restore_default
  149.     @text = ""
  150.     @index = @text.size
  151.     refresh
  152.     return !@text.empty?
  153.   end
  154.   #--------------------------------------------------------------------------
  155.   # * Get Character Width
  156.   #--------------------------------------------------------------------------
  157.   def char_width
  158.     text_size($game_system.japanese? ? "‚ " : "A").width
  159.   end
  160.   #--------------------------------------------------------------------------
  161.   # * Get Number of Columns
  162.   #--------------------------------------------------------------------------
  163.   def max_col
  164.     [(Graphics.width - 32) / char_width, @max_char].min
  165.   end
  166.   #--------------------------------------------------------------------------
  167.   # * Get Left Padding
  168.   #--------------------------------------------------------------------------
  169.   def left(n)
  170.     return 10                                if @align == "left"
  171.     return (width - 32 - n * char_width)     if @align == "right"
  172.     return (width - 32 - n * char_width) / 2 # if align == "center"
  173.   end
  174.   #--------------------------------------------------------------------------
  175.   # * Get Rectangle for Displaying Item
  176.   #--------------------------------------------------------------------------
  177.   def item_rect(index)
  178.     index -= 1 if index == @max_char
  179.     x = index % max_col
  180.     y = index / max_col
  181.     n = [(@max_char - y * max_col), max_col].min
  182.    
  183.     x = left(n) + x * char_width
  184.     x = width - x - 48 if not @ltr
  185.     y = 24      + y * (line_height + 4)
  186.     Rect.new(x, y, char_width, line_height)
  187.   end
  188.   #--------------------------------------------------------------------------
  189.   # * Get Underline Rectangle
  190.   #--------------------------------------------------------------------------
  191.   def underline_rect(index)
  192.     rect = item_rect(index)
  193.     rect.x += 1
  194.     rect.y += rect.height
  195.     rect.width -= 2
  196.     rect.height = 2
  197.     rect
  198.   end
  199.   #--------------------------------------------------------------------------
  200.   # * Get Underline Color
  201.   #--------------------------------------------------------------------------
  202.   def underline_color
  203.     color = normal_color
  204.     color.alpha = 48
  205.     color
  206.   end
  207.   #--------------------------------------------------------------------------
  208.   # * Draw Underline
  209.   #--------------------------------------------------------------------------
  210.   def draw_underline(index)
  211.     contents.fill_rect(underline_rect(index), underline_color)
  212.   end
  213.   #--------------------------------------------------------------------------
  214.   # * Draw Text
  215.   #--------------------------------------------------------------------------
  216.   def draw_char(index)
  217.     rect       = item_rect(index)
  218.     rect.x     += 4
  219.     rect.width += 4
  220.     change_color(normal_color)
  221.     draw_text(rect, @text[index] || "")
  222.   end
  223.   #--------------------------------------------------------------------------
  224.   # * Draw Title
  225.   #--------------------------------------------------------------------------
  226.   def draw_title
  227.     draw_text(0, 0, self.width, line_height, @title, 1)
  228.   end
  229.   #--------------------------------------------------------------------------
  230.   # * Refresh
  231.   #--------------------------------------------------------------------------
  232.   def refresh
  233.     contents.clear
  234.     draw_title
  235.     @text.size.times {|i| draw_char(i) }
  236.     @max_char.times  {|i| draw_underline(i) }
  237.     cursor_rect.set(item_rect(@index))
  238.   end
  239.   #--------------------------------------------------------------------------
  240.   # * Handle Process
  241.   #--------------------------------------------------------------------------
  242.   def process_handling
  243.     return unless open? && active
  244.     process_delete if Input.repeat?(:kBACKSPACE)
  245.     process_abort  if Input.trigger?(:kESC)
  246.     process_ok     if Input.trigger?(:kENTER)
  247.     process_keyboard
  248.   end
  249.   #--------------------------------------------------------------------------
  250.   # * Check Input From Keyboard
  251.   #--------------------------------------------------------------------------
  252.   def process_keyboard
  253.     ACCEPTED_KEYS.each {|key|
  254.       if Input.repeat?(key[0])
  255.         c = (key[0] != :kSPACE) ? Keyboard.add_char(Ascii::SYM[key[0]]) : " "
  256.         process_add(c)
  257.         Sound.play_ok
  258.       end
  259.     }
  260.   end
  261.   #--------------------------------------------------------------------------
  262.   # * Add One Character
  263.   #--------------------------------------------------------------------------
  264.   def process_add(c)
  265.     return if @index > @max_char
  266.     if @index == @max_char
  267.       @text[@index - 1] = c
  268.     else
  269.       @text  += c
  270.       @index += 1
  271.     end
  272.     refresh
  273.   end
  274.   #--------------------------------------------------------------------------
  275.   # * Delete One Character
  276.   #--------------------------------------------------------------------------
  277.   def process_delete
  278.     return if @index == 0
  279.     @index -= 1
  280.     @text  = @text[0, @index]
  281.     Sound.play_cancel
  282.     refresh
  283.   end
  284.   #--------------------------------------------------------------------------
  285.   # * Abort Text Input
  286.   #--------------------------------------------------------------------------
  287.   def process_abort
  288.     restore_default
  289.     Sound.play_cancel
  290.     call_ok_handler
  291.   end
  292.   #--------------------------------------------------------------------------
  293.   # * Complete Text Input
  294.   #--------------------------------------------------------------------------
  295.   def process_ok
  296.     Sound.play_ok
  297.     call_ok_handler
  298.   end
  299. end
  300.  
  301.  
  302.  
  303.  
  304.  
  305. #==============================================================================
  306. # ** Game_Interpreter
  307. #------------------------------------------------------------------------------
  308. #  An interpreter for executing event commands. This class is used within the
  309. # Game_Map, Game_Troop, and Game_Event classes.
  310. #==============================================================================
  311.  
  312. class Game_Interpreter
  313.   #--------------------------------------------------------------------------
  314.   # * Insert a text
  315.   #--------------------------------------------------------------------------
  316.   def text_input(title, align = "center", max_char = 103, ltr = true)
  317.     Scene_Text.read(title, align, max_char, ltr)
  318.   end
  319. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement