cooldoode325

Choice Options

Feb 24th, 2014
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.82 KB | None | 0 0
  1. =begin
  2. #==============================================================================
  3. Title: Choice Options
  4. Author: Tsukihime
  5. Date: Nov 17, 2013
  6. ------------------------------------------------------------------------------
  7. ** Change log
  8. Nov 17, 2013
  9. - choice formulas are now evaluated in the interpreter (rather than
  10. Game_Message)
  11. Oct 18, 2013
  12. - added "disable_color" option
  13. Jun 9, 2013
  14. - bug fix: last choice was not colored correctly
  15. Apr 10, 2013
  16. - new lines automatically removed for "text" option
  17. Apr 9, 2013
  18. - added "text" choice option
  19. Mar 8, 2013
  20. - fixed a copy-by-reference issue
  21. Mar 6, 2013
  22. - hidden choice fixed
  23. - new script interface added
  24. Dec 6, 2012
  25. - removed multiple choice implementation to be more flexible
  26. - implemented scrolling choices
  27. Dec 4, 2012
  28. - added support for built-in choice editor
  29. - initial release
  30. ------------------------------------------------------------------------------
  31. ** Terms of Use
  32. * Free to use in non-commercial projects
  33. * Contact me for commercial use
  34. * No real support. The script is provided as-is
  35. * Will do bug fixes, but no compatibility patches
  36. * Features may be requested but no guarantees, especially if it is non-trivial
  37. * Preserve this header
  38. ------------------------------------------------------------------------------
  39. ** Description
  40.  
  41. This script provides extended control over choices.
  42.  
  43. You can add "choice options" to each choice for further control over how
  44. they should appear, when they should appear, ...
  45. --------------------------------------------------------------------------------
  46. ** Installation
  47.  
  48. Place this script below Materials and above Main
  49. ------------------------------------------------------------------------------
  50. ** Usage
  51.  
  52. To add choice option, use one of the methods defined in the reference section.
  53. For example, if you want to hide a choice 1 if actor 1's level is less than 5,
  54. you would write
  55.  
  56. hide_choice(1, "$game_actors[1].level < 5")
  57.  
  58. ------------------------------------------------------------------------------
  59. ** Reference
  60.  
  61. The following options are available
  62.  
  63. Method: disable_choice
  64. Effect: disables choice if condition is not met
  65. Usage: Takes a string representing a boolean statement. For example,
  66. "$game_actors[1].level > 5" means that the condition will only be
  67. selectable if actor 1's level is greater than 5.
  68.  
  69. Method: hide_choice
  70. Effect: hides choice if condition is met
  71. Usage: Takes a string representing a boolean statement. For example,
  72. "$game_party.gold < 2000" means that the condition will not be shown
  73. if the party's gold is less than 2000
  74.  
  75. Method: color_choice
  76. Effect: Very simple text color changing based on system colors
  77. Usage: Takes an integer as the text color, based on the system colors.
  78. (eg: 2 is red by default). Check the "Window.png" file in your
  79. RTP folder to see the default colors
  80.  
  81. Method: text_choice
  82. Effect: Sets the text of the choice to the custom text.
  83. Usage: Takes a string that will replace whatever you place in the choice
  84. editor. This allows you to exceed the 50-char limit.
  85. ------------------------------------------------------------------------------
  86. ** Compatibility
  87.  
  88. This script must be placed below Large Choices
  89. ------------------------------------------------------------------------------
  90. ** Credits
  91. Enelvon, for scrolling choices implementation
  92. #==============================================================================
  93. =end
  94. $imported = {} if $imported.nil?
  95. $imported["TH_ChoiceOptions"] = true
  96. #==============================================================================
  97. # ** Configuration
  98. #==============================================================================
  99. module Tsuki
  100. module Choice_Options
  101.  
  102. Scroll_Choices = true # disable this if it is causing problems
  103. Default_Visible_Choices = 8 # Number of choices to show by default
  104. end
  105. end
  106. #==============================================================================
  107. # ** Rest of the script
  108. #==============================================================================
  109. class Game_Message
  110. attr_reader :choice_map
  111. attr_accessor :orig_choices
  112. attr_accessor :choice_options
  113. attr_accessor :num_visible_choices
  114.  
  115. alias :th_choice_options_clear :clear
  116. def clear
  117. th_choice_options_clear
  118. clear_choice_options
  119. @choice_map = []
  120. @orig_choices = []
  121. @num_visible_choices = Tsuki::Choice_Options::Default_Visible_Choices
  122. end
  123.  
  124. def clear_choice_map
  125.  
  126. end
  127.  
  128. # Hardcode...
  129. def clear_choice_options
  130. @choice_options = {}
  131. @choice_options[:condition] = {}
  132. @choice_options[:hidden] = {}
  133. @choice_options[:color] = {}
  134. @choice_options[:text] = {}
  135. @choice_options[:disable_color] = {}
  136. end
  137.  
  138. # Just hardcode. Refactor later.
  139. def set_choice_option(type, num, arg)
  140. case type
  141. when :condition
  142. @choice_options[type][num] = arg
  143. when :hidden
  144. @choice_options[type][num] = arg
  145. when :color
  146. @choice_options[type][num] = arg.to_i
  147. when :text
  148. @choice_options[type][num] = arg.gsub("\n", "")
  149. when :disable_color
  150. @choice_options[type][num] = arg.to_i
  151. else
  152. return
  153. end
  154. end
  155.  
  156. def get_choice_option(type, num)
  157. return @choice_options[type][num]
  158. end
  159.  
  160. def choice_hidden?(num)
  161. return @choice_options[:hidden][num]
  162. end
  163. end
  164.  
  165. class Game_Interpreter
  166.  
  167. alias :th_choice_options_setup_choices :setup_choices
  168. def setup_choices(params)
  169. # start with our original choices
  170. th_choice_options_setup_choices(params)
  171. replace_choice_texts
  172. setup_choice_map
  173. end
  174.  
  175. def replace_choice_texts
  176. $game_message.choices.size.times do |i|
  177. str = $game_message.get_choice_option(:text, i+1)
  178. $game_message.choices[i] = str if str
  179. end
  180. end
  181.  
  182. #-----------------------------------------------------------------------------
  183. # New. Go through hidden choices and map the indices appropriately.
  184. # The list of choices should only contain the list of visible choices
  185. # since other classes probably don't expect to have to check whether
  186. # a choice is hidden or not
  187. #-----------------------------------------------------------------------------
  188. def setup_choice_map
  189. $game_message.orig_choices = $game_message.choices.clone
  190. ($game_message.choices.size).times{|i|
  191. if choice_hidden?(i+1)
  192. $game_message.choices.delete_at(i)
  193. else
  194. $game_message.choice_map.push(i)
  195. end
  196. }
  197. # redefine the choice proc
  198. $game_message.choice_proc = Proc.new {|n| @branch[@indent] = $game_message.choice_map[n] }
  199. end
  200.  
  201. # Return true if the choice is hidden
  202. def choice_hidden?(n)
  203. $game_message.get_choice_option(:hidden, n)
  204. end
  205.  
  206. # add a choice option
  207. def choice_option(type, choice_num, arg)
  208. $game_message.set_choice_option(type.to_sym, choice_num, arg)
  209. end
  210.  
  211. def hide_choice(choice_num, condition)
  212. $game_message.set_choice_option(:hidden, choice_num, eval_choice_condition(condition))
  213. end
  214.  
  215. def disable_choice(choice_num, condition)
  216. $game_message.set_choice_option(:condition, choice_num, eval_choice_condition(condition))
  217. end
  218.  
  219. def color_choice(choice_num, value)
  220. $game_message.set_choice_option(:color, choice_num, value)
  221. end
  222.  
  223. def disable_color_choice(choice_num, value)
  224. $game_message.set_choice_option(:disable_color, choice_num, value)
  225. end
  226.  
  227. def text_choice(choice_num, text)
  228. $game_message.set_choice_option(:text, choice_num, text)
  229. end
  230.  
  231. def eval_choice_condition(condition, p=$game_party, t=$game_troop, s=$game_switches, v=$game_variables)
  232. eval(condition)
  233. end
  234. end
  235.  
  236. class Window_ChoiceList < Window_Command
  237.  
  238. #-----------------------------------------------------------------------------
  239. # Enelvon's scrolled choices
  240. #-----------------------------------------------------------------------------
  241. if Tsuki::Choice_Options::Scroll_Choices
  242. def update_placement
  243. self.width = [max_choice_width + 12, 96].max + padding * 2
  244. self.width = [width, Graphics.width].min
  245. self.height = fitting_height([$game_message.choices.size, $game_message.num_visible_choices].min)
  246. self.x = Graphics.width - width
  247. if @message_window.y >= Graphics.height / 2
  248. self.y = @message_window.y - height
  249. else
  250. self.y = @message_window.y + @message_window.height
  251. end
  252. end
  253. end
  254.  
  255. # Overwrite. Apply choice options when making text
  256. def make_command_list
  257. $game_message.orig_choices.each_with_index do |choice, i|
  258. next if $game_message.choice_hidden?(i+1)
  259. condition = $game_message.get_choice_option(:condition, i+1)
  260. condition_met = condition.nil? ? true : !condition
  261. add_command(choice, :choice, condition_met)
  262. end
  263. end
  264.  
  265. # Apply font-related choice options when drawing choices
  266. alias :th_multiple_choice_draw_item :draw_item
  267. def draw_item(index)
  268. set_choice_color(index)
  269. th_multiple_choice_draw_item(index)
  270. end
  271.  
  272. # I have my own font settings for each option so don't need the default
  273. def reset_font_settings
  274. end
  275.  
  276. # New. Apply font settings
  277. def set_choice_color(index)
  278. color = $game_message.get_choice_option(:color, $game_message.choice_map[index] + 1)
  279. disable_color = $game_message.get_choice_option(:disable_color, $game_message.choice_map[index] + 1)
  280. if color && command_enabled?(index)
  281. change_color(text_color(color), command_enabled?(index))
  282. elsif disable_color && !command_enabled?(index)
  283. change_color(text_color(disable_color), command_enabled?(index))
  284. else
  285. change_color(normal_color, command_enabled?(index))
  286. end
  287. end
  288. end
Advertisement
Add Comment
Please, Sign In to add comment