#==============================================================================
# Emoticon on Face
#------------------------------------------------------------------------------
# Autor: Nechigawara Sanzenin
# Tłumaczenie i poprawki: Ayene
# www.ultimateam.pl
#
# ZASTOSOWANIE
# By wyświetlić emotikonkę nad twarzą w oknie wiadomości wpisz:
# "\E[Numer emotikonki]"
#
# By podejrzeć numery emotikonek otwórz plik Baloon.png, znajdujący się w folderze
# Graphics\System.
#
# Pozycja emotikonki może zostać zmieniona poprzez paramtery EMO_X i EMO_Y
# 
# Jeśli w oknie wiadomości nie będzie twarzy, emotikonka się nie wyświetli.

#==============================================================================
class Window_Message < Window_Selectable
  #--------------------------------------------------------------------------
  BALLOON_WAIT = 12
  EMO_X = 93
  EMO_Y = 15
  #--------------------------------------------------------------------------
  alias inc_initialize initialize
  def initialize
    inc_initialize
    @viewport = Viewport.new(0, 0, 544, 416)
    @viewport.z = z + 50
    @balloon_face = 0
    create_balloon
  end
  #--------------------------------------------------------------------------
  alias inc_dispose dispose
  def dispose
    inc_dispose
    dispose_balloon
    @viewport.dispose
  end
  #--------------------------------------------------------------------------
  def update
    super
    update_gold_window
    update_number_input_window
    update_back_sprite
    update_show_fast
    update_balloon
    unless @opening or @closing             
      if @wait_count > 0                    
        @wait_count -= 1
      elsif self.pause                      
        input_pause
      elsif self.active                     
        input_choice
      elsif @number_input_window.visible   
        input_number
      elsif @text != nil                     
        update_message                        
      elsif continue?                       
        start_message                         
        open                                  
        $game_message.visible = true
      else                                  
        close                                 
        $game_message.visible = @closing
      end
    end
  end
  #--------------------------------------------------------------------------
  def create_balloon
    dispose_balloon
    @balloon_duration = 8 * 8 + BALLOON_WAIT
    @balloon_sprite = ::Sprite.new(@viewport)
    @balloon_sprite.bitmap = Cache.system("Balloon")
    @balloon_sprite.ox = 16
    @balloon_sprite.oy = 32
    @balloon_sprite.visible = false
    update_balloon
  end
  #--------------------------------------------------------------------------
  def refresh_balloon
    if @balloon_face == 0 or @balloon_face > 10
      @balloon_sprite.visible = false
    else
      @balloon_sprite.visible = true
    end
    update_balloon
  end
  #--------------------------------------------------------------------------
  def update_balloon
    if @balloon_duration > 0
      @balloon_duration -= 1
      if @balloon_duration == 0
        @balloon_duration = 8 * 8 + BALLOON_WAIT
        @balloon_duration -= 1
      else
        @balloon_sprite.x = x + EMO_X
        @balloon_sprite.y = y + EMO_Y
        @balloon_sprite.z = z + 50
        if @balloon_duration < BALLOON_WAIT
          sx = 7 * 32
        else
          sx = (7 - (@balloon_duration - BALLOON_WAIT) / 8) * 32
        end
        sy = (@balloon_face - 1) * 32
        @balloon_sprite.src_rect.set(sx, sy, 32, 32)
      end
    end
  end
  #--------------------------------------------------------------------------
  def dispose_balloon
    if @balloon_sprite != nil
      @balloon_sprite.dispose
      @balloon_sprite = nil
    end
  end
  #--------------------------------------------------------------------------
  alias inc_terminate_message terminate_message
  def terminate_message
    inc_terminate_message
    @balloon_sprite.visible = false
  end
  #--------------------------------------------------------------------------
  alias inc_convert_special_characters convert_special_characters
  def convert_special_characters
    inc_convert_special_characters
    @text.gsub!(/\\E\[([0-9]+)\]/i) { "\x09[#{$1}]" }
  end
  #--------------------------------------------------------------------------
  def update_message
    loop do
      c = @text.slice!(/./m)            
      case c
      when nil                          
        finish_message                  
        break
      when "\x00"                       
        new_line
        if @line_count >= MAX_LINE      
          unless @text.empty?           
            self.pause = true          
            break
          end
        end
      when "\x01"                       
        @text.sub!(/\[([0-9]+)\]/, "")
        contents.font.color = text_color($1.to_i)
        next
      when "\x02"                      
        @gold_window.refresh
        @gold_window.open
      when "\x03"                       
        @wait_count = 15
        break
      when "\x04"                       
        @wait_count = 60
        break
      when "\x05"                       
        self.pause = true
        break
      when "\x06"                       
        @line_show_fast = true
      when "\x07"                       
        @line_show_fast = false
      when "\x08"                       
        @pause_skip = true
      when "\x09"
        @text.sub!(/\[([0-9]+)\]/, "")
        unless $game_message.face_name.empty?
          @balloon_face = $1.to_i
          refresh_balloon
        end
      else                              
        contents.draw_text(@contents_x, @contents_y, 40, WLH, c)
        c_width = contents.text_size(c).width
        @contents_x += c_width
      end
      break unless @show_fast or @line_show_fast
    end
  end
end