Advertisement
Guest User

String Input Scene v1.0

a guest
May 26th, 2018
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.38 KB | None | 0 0
  1. =begin ========================================================================
  2. # ** String Input Scene
  3. #------------------------------------------------------------------------------
  4. # Author: Rikifive
  5. # Contact me: https://www.rpgmakercentral.com/profile/57081-rikifive/
  6. # Discord: Rikifive#2820
  7. #
  8. # v 1.0 // 26.05.2018 (dd.mm.yyyy)
  9. #
  10. #---[ DESCRIPTION ]------------------------------------------------------------
  11. # In this scene you can input a string into a variable, similar to name input.
  12. #
  13. #---[ FEATURES ]---------------------------------------------------------------
  14. # Allow players to input a string and store it in a variable.
  15. # Can be used to say something 'custom' in the message box,
  16. # can be used to change names etc.,
  17. # can be used to ask for password, codes and so on...
  18. #
  19. #---[ TERMS OF USE ]-----------------------------------------------------------
  20. # - Can be used in commercial projects
  21. # - Can be modified to your needs
  22. # - Cannot be reposted without permission
  23. # - Credit me, { Rikifive } in your project
  24. #
  25. #---[ INSTRUCTIONS ]-----------------------------------------------------------
  26. # Paste below materials; above main.
  27. #
  28. # Assign [ 5 ] variables to settings in the configuration section below.
  29. #
  30. # To call a scene, use the [ script call ] from 3rd page.
  31. #
  32. #---[ EXAMPLE ]----------------------------------------------------------------
  33. > Event Commands / 3rd Page / Advanced / Script...
  34. - - - - - - - - - - - - - - - - - - - - - - - - - -
  35.  
  36. $game_variables[21] = "What's your name?" # desc
  37. $game_variables[22] = Color.new(255,125,60) # desc color
  38. $game_variables[23] = "Eric" # string suggested by default
  39. $game_variables[24] = 30 # length (max 30)
  40. SceneManager.call(Scene_Input);
  41.  
  42. - - - - - - - - - - - - - - - - - - - - - - - - - -
  43. Color is in [ RGB ] format; from [ 0 ] to [ 255 ] for each color.
  44. Maximum supported length is [ 30 ] characters. Putting more won't have effect.
  45.  
  46.  
  47. You can modify each settings as you wish, you DON'T have to include all
  48. of them each time you want to bring the input menu; for example:
  49.  
  50. $game_variables[21] = "What's your name?" # desc
  51. SceneManager.call(Scene_Input);
  52.  
  53. Will call a scene with the description specified above.
  54. The rest of settings will remain the same as they were set last time OR
  55. reset to defaults; see configuration below.
  56.  
  57. /!\ WARNING
  58. HOWEVER, you HAVE TO to configure ALL settings when calling the input menu
  59. for the FIRST TIME. Otherwise it will crash, due to lack of values, or rather,
  60. them not being formatted correctly.
  61.  
  62. =end #=========================================================================
  63. module RK5_VAREDIT
  64.  
  65. #----------------------------------------------------------------------------
  66. # QUICK CONFIGURATION
  67. #----------------------------------------------------------------------------
  68. # In which variable would you like to store...
  69. # - Please assign variable ID's for each setting. -
  70. #----------------------------------------------------------------------------
  71.  
  72. # This is a variable, where the input is stored, which can be used afterwards.
  73. STRING = 20 # USER INPUT (STRING)
  74.  
  75.  
  76. DESC = 21 # DESCRIPTION / QUESTION
  77. COLOR = 22 # DESCRIPTION COLOR
  78.  
  79. DEFAULT = 23 # STRING SUGGESTED BY DEFAULT
  80. LENGTH = 24 # MAXIMUM ALLOWED CHARACTERS
  81.  
  82. #----------------------------------------------------------------------------
  83. # Should settings be reset after use?
  84. # - Enabling that will restore defaults to specific settings after use -
  85. #----------------------------------------------------------------------------
  86.  
  87. CLEAN_DEFAULT = true # if true, it will leave default input blank after use.
  88. CLEAN_DESC = true # if true, it will leave description blank after use.
  89. RESET_COLOR = true # if true, it will reset font color to white after use.
  90. RESET_LENGTH = 12 # this is the default length that will be set after use.
  91. # put [ 0 ] to disable this and keep the last used value.
  92.  
  93. #----------------------------------------------------------------------------
  94. # For example, if all above would be enabled, after putting:
  95. #
  96. # SceneManager.call(Scene_Input);
  97. #
  98. # ~in the script call, would successfully call the input scene with default
  99. # settings.
  100. # The defaults being:
  101. # - no default string
  102. # - no description
  103. # - color set to white
  104. # - length set to specified default
  105. #
  106. # ... just input window.
  107. #
  108. # REMINDER: You can modify each settings as you wish,
  109. # you don't have to include all of them each time.
  110. # Skipping settings will either use defaults or last used values.
  111. #----------------------------------------------------------------------------
  112.  
  113. end
  114. #==============================================================================
  115. # ::: END OF CONFIGURATION :::
  116. #==============================================================================
  117. # ** DONUT EDIT THINGS BELOW ** - Do it at your own risk. -
  118. #==============================================================================
  119.  
  120. class Scene_Input < Scene_MenuBase
  121. #--------------------------------------------------------------------------
  122. # * Start Processing
  123. #--------------------------------------------------------------------------
  124. def start
  125. super
  126. $game_variables[RK5_VAREDIT::STRING] = $game_variables[RK5_VAREDIT::DEFAULT]
  127. @edit_window = Window_StringEdit.new
  128. @input_window = Window_NameInput.new(@edit_window)
  129. @input_window.set_handler(:ok, method(:on_input_ok))
  130. end
  131. #--------------------------------------------------------------------------
  132. # * Input [OK]
  133. #--------------------------------------------------------------------------
  134. def on_input_ok
  135. $game_variables[RK5_VAREDIT::STRING] = @edit_window.string
  136. reset_settings
  137. return_scene
  138. end
  139. #--------------------------------------------------------------------------
  140. # * Reset to Defaults
  141. #--------------------------------------------------------------------------
  142. def reset_settings
  143. $game_variables[RK5_VAREDIT::DEFAULT] = "" if RK5_VAREDIT::CLEAN_DEFAULT
  144. $game_variables[RK5_VAREDIT::DESC] = "" if RK5_VAREDIT::CLEAN_DESC
  145. $game_variables[RK5_VAREDIT::COLOR] = Color.new(255,255,255) if RK5_VAREDIT::RESET_COLOR
  146. if RK5_VAREDIT::RESET_LENGTH != 0
  147. $game_variables[RK5_VAREDIT::LENGTH] = RK5_VAREDIT::RESET_LENGTH
  148. end
  149. end
  150.  
  151. end
  152.  
  153.  
  154. #==============================================================================
  155. # ** Window_StringEdit
  156. #------------------------------------------------------------------------------
  157. # This window is used to edit the string on the input screen.
  158. # Based on NameEdit, duh!
  159. #==============================================================================
  160.  
  161. class Window_StringEdit < Window_Base
  162. #--------------------------------------------------------------------------
  163. # * Public Instance Variables
  164. #--------------------------------------------------------------------------
  165. attr_reader :string # input string
  166. attr_reader :index # cursor position
  167. attr_reader :max_char # maximum number of characters
  168. attr_reader :name # COMPATIBILITY PURPOSES
  169.  
  170. #--------------------------------------------------------------------------
  171. # * Object Initialization
  172. #--------------------------------------------------------------------------
  173. def initialize
  174. x = (Graphics.width - 360) / 2
  175. y = (Graphics.height - (fitting_height(4) + fitting_height(9) + 8)) / 2
  176. super(x, y, 360, fitting_height(4))
  177. @name = "compatibility"
  178. @max_char = [$game_variables[RK5_VAREDIT::LENGTH], 30].min
  179. @string = $game_variables[RK5_VAREDIT::DEFAULT][0, @max_char]
  180. @index = @string.size
  181. deactivate
  182. refresh
  183. end
  184. #--------------------------------------------------------------------------
  185. # * Add Text Character
  186. # ch : character to add
  187. #--------------------------------------------------------------------------
  188. def add(ch)
  189. return false if @index >= @max_char
  190. @string += ch
  191. @index += 1
  192. refresh
  193. return true
  194. end
  195. #--------------------------------------------------------------------------
  196. # * Go Back One Character
  197. #--------------------------------------------------------------------------
  198. def back
  199. return false if @index == 0
  200. @index -= 1
  201. @string = @string[0, @index]
  202. refresh
  203. return true
  204. end
  205. #--------------------------------------------------------------------------
  206. # * Get Character Width
  207. #--------------------------------------------------------------------------
  208. def char_width
  209. text_size($game_system.japanese? ? "あ" : "A").width
  210. end
  211. #--------------------------------------------------------------------------
  212. # * Get Coordinates of Left Side for Drawing Name
  213. #--------------------------------------------------------------------------
  214. def left
  215. input_center = contents_width / 2
  216. input_width = (@max_char + 1) * char_width
  217. return [input_center - input_width / 2, contents_width - input_width].min
  218. end
  219. #--------------------------------------------------------------------------
  220. # * Get Rectangle for Displaying Item
  221. #--------------------------------------------------------------------------
  222. def item_rect(index)
  223. Rect.new(left + index * char_width, 36+10, char_width, line_height)
  224. end
  225. #--------------------------------------------------------------------------
  226. # * Get Underline Rectangle
  227. #--------------------------------------------------------------------------
  228. def underline_rect(index)
  229. rect = item_rect(index)
  230. rect.x += 1
  231. rect.y += rect.height - 4
  232. rect.width -= 2
  233. rect.height = 2
  234. rect
  235. end
  236. #--------------------------------------------------------------------------
  237. # * Get Underline Color
  238. #--------------------------------------------------------------------------
  239. def underline_color
  240. color = normal_color
  241. color.alpha = 48
  242. color
  243. end
  244. #--------------------------------------------------------------------------
  245. # * Draw Underline
  246. #--------------------------------------------------------------------------
  247. def draw_underline(index)
  248. contents.fill_rect(underline_rect(index), underline_color)
  249. end
  250. #--------------------------------------------------------------------------
  251. # * Draw Text
  252. #--------------------------------------------------------------------------
  253. def draw_char(index)
  254. rect = item_rect(index)
  255. rect.x -= 1
  256. rect.width += 4
  257. change_color(normal_color)
  258. draw_text(rect, @string[index] || "")
  259. end
  260. #--------------------------------------------------------------------------
  261. # * Draw Description
  262. #--------------------------------------------------------------------------
  263. def draw_desc
  264. desc = $game_variables[RK5_VAREDIT::DESC]
  265. change_color($game_variables[RK5_VAREDIT::COLOR])
  266. draw_text(0,line_height/2,contents_width,line_height,desc,1)
  267. end
  268. #--------------------------------------------------------------------------
  269. # * Refresh
  270. #--------------------------------------------------------------------------
  271. def refresh
  272. contents.clear
  273. @max_char.times {|i| draw_underline(i) }
  274. @string.size.times {|i| draw_char(i) }
  275. draw_desc
  276. cursor_rect.set(item_rect(@index))
  277. end
  278. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement