#==============================================================================
# Warning message
# By gerkrt/gerrtunk
# Version: 1.3
# License: MIT, credits
# Visit this for my other scripts and updates: [url]http://pastebin.com/u/gerkrt[/url]
#==============================================================================

=begin
 
Features:
 
I created this script long ago for one of my menus and any other project. Its very flexible
and can be used for a lot of things. It uses a modified window+sprite combination
because i think that it looks better, anyway you can modify it or do what you want.
 
-The window size and position is based on the text size.
-Autocenter in the screen if no position specified.
-All other sizes or positions are set automatically.
-You can define default index used and some other values.
-It have methods for automatically visible/invisible, dipose, hide, etc, all sprites
and windows.
-You can add a question to it. With this a modified version of a window command
is actived.
-You can changue the text, ask option, x and y in every call.
-You can use it for multiples purposes in a signle scene. For that you have to
changue the message_type value that specifies wich of the ways of being used 
is now. For example: 
@warning_message.refresh('Are you sure to exit?')
@warning_message.message_type = :asking_for exit.  

Note that you can also pass that ass a extra parameter for the refresh method.

=end

class Warning_Message
  @@default_x = 200
  @@default_y = 200
  @@default_z = 200
  @@selection_options = ["Si", "No"]
  @@default_option = 1
  @@default_message_type = :normal
  
  attr_accessor   :message_type  # Use this for differents uses in the same scene.
  attr_accessor   :active        # This specifies if all the window is active or not
  
  def initialize()
    @message_type = @@default_message_type # Default value for the basic option.
    @active = false

    # Default values when refreshing it
    @default_x = 200
    @default_y = 200
    # Back text window. This is used only as a back window to simulate rpgmaker ones.
    @text_window = Window_Base.new(120, 136, 400, 64)
    @text_window.z = 252 
    @text_window.visible = false
    # Text sprite. The text where is drawn the refreshed text.
    @text_sprite = Sprite.new
    @text_sprite.z = 254
    @text_sprite.x = 120
    @text_sprite.y = 136
    @text_sprite.bitmap = Bitmap.new(400,64)
    # Testing bitmap for size test
    @testing_bitmap =  Bitmap.new(1,1)
    # Question window. A modified version of window command its wich have the
    # yes/no options. 
    @question_window = Window_Selection.new(80, @@selection_options)
    @question_window.x = 280
    @question_window.y = 200
    @question_window.z = 253
    @question_window.index = @@default_option
    @question_window.back_opacity = 0
    @question_window.opacity = 0
    @question_window.active = false
    @question_window.visible = false
    # Back question window. Used to simulate the rpgmaker back window.
    @back_question_window = Window_Base.new(120, 136, 64, 64)
    @back_question_window.x = 280
    @back_question_window.y = 200
    @back_question_window.z = 254
    @back_question_window.visible = false
  end
 
  # Make all the sprites/windows visibles
  def visible
    @text_window.visible = true
    @text_sprite.visible = true
    # Question ones only if active
    if @question_window.active
      @question_window.visible = true
      @back_question_window.visible = true
    end
  end
 
  # Make all the sprites/windows invisibles
  def no_visible
    @text_window.visible = false
    @text_sprite.visible = false
    @question_window.visible = false
    @back_question_window.visible = false
  end
 
  # Is question window active?. Only works for the yes/no option.
  def question_active
    return @question_window.active
  end
 
  # Set question window active. Only works for the yes/no option.
  def question_active=(value)
    @question_window.active = value
  end
 
  # Update all the sprites/windows visibles
  def update
    @text_window.update
    @text_sprite.update
    @back_question_window.update
    @question_window.update
  end
  
  # Get if yes or no when using two options
  def is_Yes?
    if @question_window.index == 0
      true
    else
      false
    end
  end
  
  # Get question index
  def index 
    @question_window.index
  end
  
  # Set question index
  def index=(value)
    @question_window.index = value
  end
    
  
  # Draw the warning message. You have to use this method to show the window
  # message changuing each time. Note that you can changue default options and
  # also specifiy directly the message type here directly.
  # question: called to add a yes/no window.
  def refresh(message, question=true, message_type=@@default_message_type, x=@default_x, 
      y=@default_y)
    
    # Basic position settings
    @text_window.y = y
    @text_window.x = x
    @text_sprite.x = x
    @text_sprite.y = y
    rect = @testing_bitmap.text_size(message)
    @active = true
    
    # With question window or not. All the positions auto-setting are done here.
    if question
      @question_window.active = true
      @text_window.width = rect.width+26+40
      #@question_window.visible = true
      @question_window.active = true
      @question_window.index == @@default_option
      #@back_question_window.visible = true
      @question_window.x = @text_window.x+rect.width+4+6
      @question_window.y = @text_window.y - 16
      @back_question_window.x = @text_window.x+rect.width+4+16
      @back_question_window.y = @text_window.y
    else
      @text_window.width = rect.width+26
    end
    @message_type = message_type
    visible
    # First update the back window
    @text_window.update
    @text_sprite.bitmap.clear
    # Draw text
    @text_sprite.bitmap.draw_text(0+10,0,400,64,message, 0)
    @text_sprite.update
  end
 
  # Hide window and make inactive all. Use this to stop using the message each
  # time
  def hide
    no_visible
    @active = false
    @question_window.active = false
  end
  
  # Dispose all the sprites/windows visibles
  def dispose
    @text_window.dispose
    @text_sprite.dispose
    @question_window.dispose
    @back_question_window.dispose
  end
end
 
# Modified to shown horizontal.
class Window_Selection < Window_Command
  #--------------------------------------------------------------------------
  # * Update Cursor Rectangle
  #--------------------------------------------------------------------------
  def update_cursor_rect
    # If cursor position is less than 0
    if @index < 0
      self.cursor_rect.empty
      return
    end
    # Get current row
    row = @index / @column_max
    # If current row is before top row
    if row < self.top_row
      # Scroll so that current row becomes top row
      self.top_row = row
    end
    # If current row is more to back than back row
    if row > self.top_row + (self.page_row_max - 1)
      # Scroll so that current row becomes back row
      self.top_row = row - (self.page_row_max - 1)
    end
    # Calculate cursor width
    cursor_width = (self.width / @column_max - 32)+11
    # Calculate cursor coordinates
    x = @index % @column_max * (cursor_width + 32)
    y = @index / @column_max * 32 - self.oy
    # Update cursor rectangle
    self.cursor_rect.set(x-4, y, cursor_width, 32)
  end
 
end