Advertisement
DrDhoom

[RGSS] More Choice

Dec 4th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.54 KB | None | 0 0
  1. module Dhoom
  2.   module MoreChoice
  3.     #How to use:
  4.     # Create an array and then call setup_choice(Choice Array)
  5.     # in event script call.
  6.     # Example:
  7.     #   array = ["Choice 1", "Choice 2"]
  8.     #   setup_choice(array)
  9.     #
  10.     # The choice window only be displayed if you call any message after
  11.     # the setup.
  12.    
  13.     #Variable ID to store choice index
  14.     ChoiceVariableID = 1
  15.   end
  16. end
  17.  
  18. class Window_Choice < Window_Selectable
  19.   def initialize(choices)
  20.     super(0,0,32,32)
  21.     self.contents = Bitmap.new(32, 32)
  22.     self.back_opacity = 160
  23.     @choices = choices
  24.     self.x = 80
  25.     self.y = 304-get_height
  26.     self.width = get_width
  27.     self.height = get_height
  28.     @item_max = @choices.size
  29.     @column_max = 1
  30.     @index = 0
  31.     self.contents = Bitmap.new(width - 32, @item_max * 32)
  32.     refresh
  33.   end
  34.    
  35.   def get_width
  36.     w = 72
  37.     @choices.each do |choice|
  38.       t = self.contents.text_size(choice)
  39.       w = t.width if w < t.width
  40.     end
  41.     return w+40
  42.   end
  43.  
  44.   def get_height
  45.     return @choices.size*32+32
  46.   end
  47.  
  48.   def refresh
  49.     self.contents.clear
  50.     for i in 0...@item_max
  51.       draw_item(i, normal_color)
  52.     end
  53.   end
  54.  
  55.   def draw_item(index, color)
  56.     self.contents.font.color = color
  57.     rect = Rect.new(4, 32 * index, self.contents.width-8, 32)
  58.     self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
  59.     self.contents.draw_text(rect, @choices[index])
  60.   end
  61. end
  62.  
  63. class Window_Message < Window_Selectable
  64.   def refresh
  65.     self.contents.clear
  66.     self.contents.font.color = normal_color
  67.     x = y = 0
  68.     @cursor_width = 0
  69.     # Indent if choice
  70.     if $game_temp.choice_start == 0
  71.       x = 8
  72.     end
  73.     # If waiting for a message to be displayed
  74.     if $game_temp.message_text != nil
  75.       text = $game_temp.message_text
  76.       # Control text processing
  77.       begin
  78.         last_text = text.clone
  79.         text.gsub!(/\\[Vv]\[([0-9]+)\]/) { $game_variables[$1.to_i] }
  80.       end until text == last_text
  81.       text.gsub!(/\\[Nn]\[([0-9]+)\]/) do
  82.         $game_actors[$1.to_i] != nil ? $game_actors[$1.to_i].name : ""
  83.       end
  84.       # Change "\\\\" to "\000" for convenience
  85.       text.gsub!(/\\\\/) { "\000" }
  86.       # Change "\\C" to "\001" and "\\G" to "\002"
  87.       text.gsub!(/\\[Cc]\[([0-9]+)\]/) { "\001[#{$1}]" }
  88.       text.gsub!(/\\[Gg]/) { "\002" }
  89.       # Get 1 text character in c (loop until unable to get text)
  90.       while ((c = text.slice!(/./m)) != nil)
  91.         # If \\
  92.         if c == "\000"
  93.           # Return to original text
  94.           c = "\\"
  95.         end
  96.         # If \C[n]
  97.         if c == "\001"
  98.           # Change text color
  99.           text.sub!(/\[([0-9]+)\]/, "")
  100.           color = $1.to_i
  101.           if color >= 0 and color <= 7
  102.             self.contents.font.color = text_color(color)
  103.           end
  104.           # go to next text
  105.           next
  106.         end
  107.         # If \G
  108.         if c == "\002"
  109.           # Make gold window
  110.           if @gold_window == nil
  111.             @gold_window = Window_Gold.new
  112.             @gold_window.x = 560 - @gold_window.width
  113.             if $game_temp.in_battle
  114.               @gold_window.y = 192
  115.             else
  116.               @gold_window.y = self.y >= 128 ? 32 : 384
  117.             end
  118.             @gold_window.opacity = self.opacity
  119.             @gold_window.back_opacity = self.back_opacity
  120.           end
  121.           # go to next text
  122.           next
  123.         end
  124.         # If new line text
  125.         if c == "\n"
  126.           # Update cursor width if choice
  127.           if y >= $game_temp.choice_start
  128.             @cursor_width = [@cursor_width, x].max
  129.           end
  130.           # Add 1 to y
  131.           y += 1
  132.           x = 0
  133.           # Indent if choice
  134.           if y >= $game_temp.choice_start
  135.             x = 8
  136.           end
  137.           # go to next text
  138.           next
  139.         end
  140.         # Draw text
  141.         self.contents.draw_text(4 + x, 32 * y, 40, 32, c)
  142.         # Add x to drawn text width
  143.         x += self.contents.text_size(c).width
  144.       end
  145.     end
  146.     # If choice
  147.     if $game_temp.choice_max > 0
  148.       @choice_window = Window_Choice.new($game_temp.message_choice)
  149.     end
  150.     # If number input
  151.     if $game_temp.num_input_variable_id > 0
  152.       digits_max = $game_temp.num_input_digits_max
  153.       number = $game_variables[$game_temp.num_input_variable_id]
  154.       @input_number_window = Window_InputNumber.new(digits_max)
  155.       @input_number_window.number = number
  156.       @input_number_window.x = self.x + 8
  157.       @input_number_window.y = self.y + $game_temp.num_input_start * 32
  158.     end
  159.   end
  160.  
  161.   alias dhoom_mrchoice_wndmsg_update update
  162.   def update    
  163.     if @choice_window != nil
  164.       @choice_window.update
  165.       if Input.trigger?(Input::C)
  166.         $game_system.se_play($data_system.decision_se)
  167.         $game_variables[Dhoom::MoreChoice::ChoiceVariableID] = @choice_window.index
  168.         terminate_message
  169.         return
  170.       end      
  171.     end  
  172.     dhoom_mrchoice_wndmsg_update
  173.   end
  174.  
  175.   alias dhoom_mrchoice_wndmsg_terminate_message terminate_message
  176.   def terminate_message
  177.     dhoom_mrchoice_wndmsg_terminate_message
  178.     if @choice_window != nil
  179.       @choice_window.dispose
  180.       @choice_window = nil
  181.     end
  182.   end
  183. end
  184.  
  185. class Game_Temp
  186.   attr_accessor :message_choice
  187.   alias dhoom_mrchoice_gmtemp_initialize initialize
  188.   def initialize
  189.     dhoom_mrchoice_gmtemp_initialize
  190.     @message_choice = []
  191.   end
  192. end
  193.  
  194. class Interpreter
  195.   def setup_choice(choices)
  196.     $game_temp.message_choice = choices
  197.     $game_temp.choice_max = 1
  198.     $game_temp.choice_cancel_type = 0
  199.   end
  200. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement