Advertisement
Guest User

Text Input Script 1.1rc

a guest
Jun 19th, 2016
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 15.58 KB | None | 0 0
  1. #==============================================================================
  2. # ** Text Input script
  3. #------------------------------------------------------------------------------
  4. # author:  erpicci
  5. # version: 1.1rc
  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. # * Changelog and notes
  38. #------------------------------------------------------------------------------
  39. # -1.1rc: Added options to manipulate the text edit window (position, size,
  40. #         opacity); manipulation is obtained through the new Scene_Text_Config
  41. #         class. Backward compatibile.
  42. #
  43. #------------------------------------------------------------------------------
  44. # * Examples
  45. #------------------------------------------------------------------------------
  46. #   # Open a window whose title is "Talk to Bobby", text is centered, allows
  47. #   # to enter up to 20 characters, writing is right-to-left
  48. #   variable = Scene_Text.read("Talk to Bobby", "center", 20, false)
  49. #
  50. #   # Open a window whose title is "Tell me something", text is aligned to
  51. #   # right, allows to enter up to 88 characters, writing is left-to-right
  52. #   # (default)
  53. #   variable = Scene_Text.read("Talk to Bobby", "center", 20)
  54. #
  55. #   # Open a window whose title is "Insert text", text is centered (default),
  56. #   # accepts up to 104 characters (default), writing is left-to-right (default)
  57. #   variable = Scene_Text.read("Talk to Bobby")
  58. #
  59. #   # Open a window whose title is "Text:", text is centered, allows
  60. #   # to enter up to 20 characters, writing is right-to-left,
  61. #   # window is resized, centered in the screen and semi-transparent
  62. #   config = Scene_Text_Config.new
  63. #   config.width = 256
  64. #   config.height = 128
  65. #   config.verticalAlignment = "center"
  66. #   config.horizontalAlignment = "center"
  67. #   config.opacity = 64
  68. #   variable = Scene_Text.read("Text:", "center", 20, true, config)
  69. #
  70. #   # Open a window whose title is "Text:", text is centered, allows
  71. #   # to enter up to 30 characters, writing is right-to-left,
  72. #   # window is resized, aligned on the top-right corner and fully transparent
  73. #   config = Scene_Text_Config.new
  74. #   config.width = 128
  75. #   config.height = 256
  76. #   config.verticalAlignment = "top"
  77. #   config.horizontalAlignment = "right"
  78. #   config.opacity = 0
  79. #   variable = Scene_Text.read("Text:", "center", 30, true, config)
  80. #==============================================================================
  81.  
  82. module Text_Input
  83.   ACCEPTED_KEYS = {
  84.     :k0 => 48, :k1 => 49, :k2 => 50, :k3 => 51, :k4 => 52,
  85.     :k5 => 53, :k6 => 54, :k7 => 55, :k8 => 56, :k9 => 57,
  86.    
  87.     :kA => 65, :kB => 66, :kC => 67, :kD => 68, :kE => 69, :kF => 70,
  88.     :kG => 71, :kH => 72, :kI => 73, :kJ => 74, :kK => 75, :kL => 76,
  89.     :kM => 77, :kN => 78, :kO => 79, :kP => 80, :kQ => 81, :kR => 82,
  90.     :kS => 83, :kT => 84, :kU => 85, :kV => 86, :kW => 87, :kX => 88,
  91.     :kY => 89, :kZ => 90,
  92.    
  93.     :kCOLON     => 186, :kQUOTE     => 222, :kSPACE      => 32,
  94.     :kCOMMA     => 188, :kPERIOD    => 190, :kSLASH      => 191,
  95.     :kBACKSLASH => 220, :kLEFTBRACE => 219, :kRIGHTBRACE => 221,
  96.     :kMINUS     => 189, :kEQUAL     => 187, :kTILDE      => 192,          
  97.   }
  98. end
  99.  
  100.  
  101.  
  102. #==============================================================================
  103. # ** Scebe_Text_Config
  104. #------------------------------------------------------------------------------
  105. #  This object contains additional configuration for the text edit window
  106. #==============================================================================
  107.  
  108. class Scene_Text_Config
  109.   attr_accessor :lines
  110.   attr_accessor :x
  111.   attr_accessor :y
  112.   attr_accessor :width
  113.   attr_accessor :height
  114.   attr_accessor :opacity
  115.  
  116.   #--------------------------------------------------------------------------
  117.   # * Object Initialization: set default values
  118.   #--------------------------------------------------------------------------
  119.   def initialize
  120.     @lines   = 6
  121.     @x       = 0
  122.     @y       = Graphics.height - window.fitting_height(lines)
  123.     @width   = Graphics.width
  124.     @height  = window.fitting_height(lines)
  125.     @opacity = 256
  126.   end
  127.   #--------------------------------------------------------------------------
  128.   # * Access to Properties of Windows
  129.   #--------------------------------------------------------------------------
  130.   def window
  131.     Window_Base.new(0, 0, 0, 0)
  132.   end
  133.   #--------------------------------------------------------------------------
  134.   # * Sets Horizontal Alignment
  135.   #--------------------------------------------------------------------------
  136.   def horizontalAlignment=(position = "center")
  137.     if (position == "left")
  138.       @x = 0
  139.     elsif (position == "right")
  140.       @x = Graphics.width - @width
  141.     else
  142.       @x = (Graphics.width - @width) / 2
  143.     end
  144.   end
  145.   #--------------------------------------------------------------------------
  146.   # * Sets Vertical Alignment
  147.   #--------------------------------------------------------------------------
  148.   def verticalAlignment=(position = "bottom")
  149.     if (position == "top")
  150.       @y = 0
  151.     elsif (position == "center")
  152.       @y = (Graphics.height - @height) / 2
  153.     else
  154.       @y = Graphics.height - @height
  155.     end
  156.   end
  157. end
  158.  
  159.  
  160.  
  161. #==============================================================================
  162. # ** Scene_Text
  163. #------------------------------------------------------------------------------
  164. #  This class shows an input field for the player.
  165. #==============================================================================
  166.  
  167. class Scene_Text < Scene_MenuBase
  168.   ALIGN    = "center"
  169.   MAX_CHAR = 104
  170.   LTR      = true
  171.  
  172.   #--------------------------------------------------------------------------
  173.   # * Show a dialog window to the player
  174.   #--------------------------------------------------------------------------
  175.   def self.read(title, align = ALIGN, max_char = MAX_CHAR, ltr = LTR, opts = nil)
  176.     @@text     = ""
  177.     @@title    = title
  178.     @@align    = align
  179.     @@max_char = max_char
  180.     @@ltr      = ltr
  181.     @@opts     = opts.nil? ? Scene_Text_Config.new : opts
  182.     SceneManager.call(Scene_Text)
  183.     Fiber.yield while SceneManager.scene_is?(Scene_Text)
  184.     return @@text
  185.   end
  186.   #--------------------------------------------------------------------------
  187.   # * Start Processing
  188.   #--------------------------------------------------------------------------
  189.   def start
  190.     super
  191.     @edit_window  = Window_TextEdit.new(@@title, @@align, @@max_char, @@ltr,
  192.       @@opts)
  193.     @edit_window.set_handler(:ok, method(:on_input_ok))
  194.   end
  195.   #--------------------------------------------------------------------------
  196.   # * Set text when done
  197.   #--------------------------------------------------------------------------
  198.   def on_input_ok
  199.     @@text = @edit_window.text
  200.     return_scene
  201.   end
  202. end
  203.  
  204.  
  205.  
  206. #==============================================================================
  207. # ** Window_TextEdit
  208. #------------------------------------------------------------------------------
  209. #  This window allows to edit a text.
  210. #==============================================================================
  211.  
  212. class Window_TextEdit < Window_Selectable
  213.   include Text_Input
  214.   #--------------------------------------------------------------------------
  215.   # * Public Instance Variables
  216.   #--------------------------------------------------------------------------
  217.   attr_accessor :text
  218.  
  219.   #--------------------------------------------------------------------------
  220.   # * Object Initialization
  221.   #--------------------------------------------------------------------------
  222.   def initialize(title, align = "center", max_char = 104, ltr = true, opts = nil)
  223.     opts = opts.nil? ? Scene_Text_Config.new : opts
  224.     super(opts.x, opts.y, opts.width, opts.height)
  225.     self.opacity = opts.opacity
  226.    
  227.     @text     = ""
  228.     @title    = title
  229.     @align    = align
  230.     @max_char = max_char
  231.     @ltr      = ltr
  232.     @opts     = opts
  233.     @index    = @text.size
  234.     activate
  235.     refresh
  236.   end
  237.   #--------------------------------------------------------------------------
  238.   # * Revert to Default Text
  239.   #--------------------------------------------------------------------------
  240.   def restore_default
  241.     @text = ""
  242.     @index = @text.size
  243.     refresh
  244.     return !@text.empty?
  245.   end
  246.   #--------------------------------------------------------------------------
  247.   # * Get Character Width
  248.   #--------------------------------------------------------------------------
  249.   def char_width
  250.     text_size($game_system.japanese? ? "‚ " : "A").width
  251.   end
  252.   #--------------------------------------------------------------------------
  253.   # * Get Number of Columns
  254.   #--------------------------------------------------------------------------
  255.   def max_col
  256.     [(@opts.width - 32) / char_width, @max_char].min
  257.   end
  258.   #--------------------------------------------------------------------------
  259.   # * Get Left Padding
  260.   #--------------------------------------------------------------------------
  261.   def left(n)
  262.     return 10                                if @align == "left"
  263.     return (width - 32 - n * char_width)     if @align == "right"
  264.     return (width - 32 - n * char_width) / 2 # if align == "center"
  265.   end
  266.   #--------------------------------------------------------------------------
  267.   # * Get Rectangle for Displaying Item
  268.   #--------------------------------------------------------------------------
  269.   def item_rect(index)
  270.     index -= 1 if index == @max_char
  271.     x = index % max_col
  272.     y = index / max_col
  273.     n = [(@max_char - y * max_col), max_col].min
  274.    
  275.     x = left(n) + x * char_width
  276.     x = width   - x - 48 if not @ltr
  277.     y = 24      + y * (line_height + 4)
  278.     Rect.new(x, y, char_width, line_height)
  279.   end
  280.   #--------------------------------------------------------------------------
  281.   # * Get Underline Rectangle
  282.   #--------------------------------------------------------------------------
  283.   def underline_rect(index)
  284.     rect = item_rect(index)
  285.     rect.x += 1
  286.     rect.y += rect.height
  287.     rect.width -= 2
  288.     rect.height = 2
  289.     rect
  290.   end
  291.   #--------------------------------------------------------------------------
  292.   # * Get Underline Color
  293.   #--------------------------------------------------------------------------
  294.   def underline_color
  295.     color = normal_color
  296.     color.alpha = 48
  297.     color
  298.   end
  299.   #--------------------------------------------------------------------------
  300.   # * Draw Underline
  301.   #--------------------------------------------------------------------------
  302.   def draw_underline(index)
  303.     contents.fill_rect(underline_rect(index), underline_color)
  304.   end
  305.   #--------------------------------------------------------------------------
  306.   # * Draw Text
  307.   #--------------------------------------------------------------------------
  308.   def draw_char(index)
  309.     rect       = item_rect(index)
  310.     rect.x     += 4
  311.     rect.width += 4
  312.     change_color(normal_color)
  313.     draw_text(rect, @text[index] || "")
  314.   end
  315.   #--------------------------------------------------------------------------
  316.   # * Draw Title
  317.   #--------------------------------------------------------------------------
  318.   def draw_title
  319.     draw_text(0, 0, self.width, line_height, @title, 1)
  320.   end
  321.   #--------------------------------------------------------------------------
  322.   # * Refresh
  323.   #--------------------------------------------------------------------------
  324.   def refresh
  325.     contents.clear
  326.     draw_title
  327.     @text.size.times {|i| draw_char(i) }
  328.     @max_char.times  {|i| draw_underline(i) }
  329.     cursor_rect.set(item_rect(@index))
  330.   end
  331.   #--------------------------------------------------------------------------
  332.   # * Handle Process
  333.   #--------------------------------------------------------------------------
  334.   def process_handling
  335.     return unless open? && active
  336.     process_delete if Input.repeat?(:kBACKSPACE)
  337.     process_abort  if Input.trigger?(:kESC)
  338.     process_ok     if Input.trigger?(:kENTER)
  339.     process_keyboard
  340.   end
  341.   #--------------------------------------------------------------------------
  342.   # * Check Input From Keyboard
  343.   #--------------------------------------------------------------------------
  344.   def process_keyboard
  345.     ACCEPTED_KEYS.each {|key|
  346.       if Input.repeat?(key[0])
  347.         c = (key[0] != :kSPACE) ? Keyboard.add_char(Ascii::SYM[key[0]]) : " "
  348.         process_add(c)
  349.         Sound.play_ok
  350.       end
  351.     }
  352.   end
  353.   #--------------------------------------------------------------------------
  354.   # * Add One Character
  355.   #--------------------------------------------------------------------------
  356.   def process_add(c)
  357.     return if @index > @max_char
  358.     if @index == @max_char
  359.       @text[@index - 1] = c
  360.     else
  361.       @text  += c
  362.       @index += 1
  363.     end
  364.     refresh
  365.   end
  366.   #--------------------------------------------------------------------------
  367.   # * Delete One Character
  368.   #--------------------------------------------------------------------------
  369.   def process_delete
  370.     return if @index == 0
  371.     @index -= 1
  372.     @text  = @text[0, @index]
  373.     Sound.play_cancel
  374.     refresh
  375.   end
  376.   #--------------------------------------------------------------------------
  377.   # * Abort Text Input
  378.   #--------------------------------------------------------------------------
  379.   def process_abort
  380.     restore_default
  381.     Sound.play_cancel
  382.     call_ok_handler
  383.   end
  384.   #--------------------------------------------------------------------------
  385.   # * Complete Text Input
  386.   #--------------------------------------------------------------------------
  387.   def process_ok
  388.     Sound.play_ok
  389.     call_ok_handler
  390.   end
  391. end
  392.  
  393.  
  394.  
  395.  
  396.  
  397. #==============================================================================
  398. # ** Game_Interpreter
  399. #------------------------------------------------------------------------------
  400. #  An interpreter for executing event commands. This class is used within the
  401. # Game_Map, Game_Troop, and Game_Event classes.
  402. #==============================================================================
  403.  
  404. class Game_Interpreter
  405.   #--------------------------------------------------------------------------
  406.   # * Insert a text
  407.   #--------------------------------------------------------------------------
  408.   def text_input(title, align = "center", max_char = 103, ltr = true, opts = nil)
  409.     Scene_Text.read(title, align, max_char, ltr, opts)
  410.   end
  411. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement