#===============================================================================
# 
# Yanfly Engine Melody - Debug Menu Extension
# Last Date Updated: 2010.06.24
# Level: Normal, Hard, Lunatic
# 
# This script, in by no means, is meant for player use. This script offers an
# extended debug menu that is more accessible and cleaner to use than what the
# default debug menu has to offer. The goal of this script is to ensure that
# the users receive proper and detailed information regarding their games
# during test play.
# 
#===============================================================================
# Updates
# -----------------------------------------------------------------------------
# o 2010.06.24 - Item Management bugfix.
# o 2010.06.20 - Added Common Event debugger.
# o 2010.06.19 - Added Teleport, Battle, Items, Weapons, and Armours debuggers.
# o 2010.06.16 - Converted to Yanfly Engine Melody.
#===============================================================================
# Instructions
# -----------------------------------------------------------------------------
# To install this script, open up your script editor and copy/paste this script
# to an open slot below ▼ Materials but above ▼ Main. Remember to save.
# 
# -----------------------------------------------------------------------------
# Debug Shortcuts - Only during $TEST and $BTEST mode
# -----------------------------------------------------------------------------
# Alt   + F5-F9 - Common Event Debug Shortcut
# Ctrl  + F5-F9 - Common Event Debug Shortcut
# Shift + F5-F9 - Common Event Debug Shortcut
# F9 on the map - Open Debug Menu.
# 
# F10 anywhere - Opens up the Debug Entry Window.
#   This requires the Keyboard Input module to grant full access to the entire
# keyboard. Here, you may enter in a piece of code and the script itself will
# run it using the current scene as its host. So long as the code doesn't
# contain any syntax errors, it'll run right immediately. Idea by OriginalWij.
# 
#===============================================================================

$imported = {} if $imported == nil
$imported["DebugMenuExtension"] = true

module YEM
  module DEBUG
    
    #===========================================================================
    # Section I. Basic Settings
    # --------------------------------------------------------------------------
    # The following adjusts debug menu data. There's no real need to edit
    # anything here unless you feel like it. Here's what the commands do:
    #   :switches     Adjust switches like default.
    #   :variables    Adjust variables like default.
    #   :teleport     Teleport to different maps.
    #   :battle       Enter the selected battle.
    #   :items        Adjust item quantities.
    #   :weapons      Adjust weapon quantities.
    #   :armours      Adjust armour quantities.
    #===========================================================================
    
    # Command window layout for the debug menu. Determines order and which
    # commands will be shown in the command window.
    COMMANDS =[
      :switches,
      :variables,
      :teleport,
      :battle,
      :events,
      :items,
      :weapons,
      :armours,
    ] # Do not remove this.
    
    # This is the vocabulary used to display various debug menu text.
    VOCAB ={
      :switches  => "Switches",
      :variables => "Variables",
      :teleport  => "Teleport",
      :battle    => "Battle",
      :events    => "Events",
      :items     => "Items",
      :weapons   => "Weapons",
      :armours   => "Armours",
    } # Do not remove this.
    
    #===========================================================================
    # Section II. Shortcut Settings
    # --------------------------------------------------------------------------
    # Here, you can define common event shortcuts to launch during test play
    # mode and pressing the right shortcut combination.
    #===========================================================================
    
    # Common event shortcuts when holding down ALT and pressing an F5-F9 key.
    ALT ={ # Only works during test play mode.
      :f5 => 31,
      :f6 => 32,
      :f7 => 33,
      :f8 => 34,
      :f9 => 35,
    } # Do not remove this.
    
    # Common event shortcuts when holding down CTRL and pressing an F5-F9 key.
    CTRL ={ # Only works during test play mode.
      :f5 => 36,
      :f6 => 37,
      :f7 => 38,
      :f8 => 39,
      :f9 => 40,
    } # Do not remove this.
    
    # Common event shortcuts when holding down SHIFT and pressing an F5-F9 key.
    SHIFT ={ # Only works during test play mode.
      :f5 => 41,
      :f6 => 42,
      :f7 => 43,
      :f8 => 44,
      :f9 => 45,
    } # Do not remove this.
    
  end # DEBUG  
end # YEM

#===============================================================================
# Editting anything past this point may potentially result in causing computer
# damage, incontinence, explosion of user's head, coma, death, and/or halitosis.
# Therefore, edit at your own risk.
#===============================================================================

#===============================================================================
# Game_Temp
#===============================================================================

class Game_Temp
  
  #--------------------------------------------------------------------------
  # public instance variables
  #--------------------------------------------------------------------------
  attr_accessor :undisposed_windows
  
end # Game_Temp

#===============================================================================
# Sprite_TeleportCursor
#===============================================================================

class Sprite_TeleportCursor < Sprite_Base
  
  #--------------------------------------------------------------------------
  # initialize
  #--------------------------------------------------------------------------
  def initialize
    super
    create_bitmap
    self.ox = self.oy = 16
    self.x = 352
    self.y = 248
    self.z = 200
  end
  
  #--------------------------------------------------------------------------
  # dispose
  #--------------------------------------------------------------------------
  def dispose
    self.bitmap.dispose
    super
  end
  
  #--------------------------------------------------------------------------
  # update
  #--------------------------------------------------------------------------
  def update
    super
    self.bitmap.hue_change(30) if @active
  end
  
  #--------------------------------------------------------------------------
  # create_bitmap
  #--------------------------------------------------------------------------
  def create_bitmap
    bitmap = Bitmap.new(32, 32)
    bitmap.fill_rect(0, 0, 32, 32, Color.new(0, 0, 0))
    bitmap.fill_rect(1, 1, 30, 30, Color.new(0, 255, 0))
    bitmap.fill_rect(3, 3, 26, 26, Color.new(0, 0, 0))
    bitmap.clear_rect(4, 4, 24, 24)
    self.bitmap = bitmap
  end
  
end # Sprite_TeleportCursor

#===============================================================================
# Window_Base
#===============================================================================

class Window_Base < Window
  
  #--------------------------------------------------------------------------
  # alias method: initialize
  #--------------------------------------------------------------------------
  alias initialize_window_base_debug initialize unless $@
  def initialize(x, y, width, height)
    $game_temp.undisposed_windows = [] if $game_temp.undisposed_windows == nil
    $game_temp.undisposed_windows.push(self)
    initialize_window_base_debug(x, y, width, height)
  end
  
  #--------------------------------------------------------------------------
  # alias method: dispose
  #--------------------------------------------------------------------------
  alias dispose_window_base_debug dispose unless $@
  def dispose
    $game_temp.undisposed_windows = [] if $game_temp.undisposed_windows == nil
    $game_temp.undisposed_windows.delete(self)
    dispose_window_base_debug
  end
  
end # Window_Base

#===============================================================================
# Window_DebugSwitches
#===============================================================================

class Window_DebugSwitches < Window_Selectable
  
  #--------------------------------------------------------------------------
  # initialize
  #--------------------------------------------------------------------------
  def initialize(page, info_window)
    super(160, 0, Graphics.width - 160, 272)
    self.index = 0
    @info_window = info_window
    refresh(page)
  end
  
  #--------------------------------------------------------------------------
  # switch
  #--------------------------------------------------------------------------
  def switch; return @data[self.index]; end
  
  #--------------------------------------------------------------------------
  # refresh_info_window
  #--------------------------------------------------------------------------
  def refresh_info_window
    @info_window.contents.clear
    sw = @info_window.contents.width
    value1 = @page*10+1
    value2 = [@page*10+10, $data_system.variables.size-1].min
    text1 = sprintf("Switches %d to %d", value1, value2)
    text2 = "Press L or Left to page backward."
    text3 = "Press R or Right to page forward."
    text4 = "Press Z to toggle switch."
    @info_window.contents.draw_text(0, WLH*0, sw, WLH, text1, 1)
    @info_window.contents.draw_text(0, WLH*1+8, sw, WLH, text2, 1)
    @info_window.contents.draw_text(0, WLH*2+8, sw, WLH, text3, 1)
    @info_window.contents.draw_text(0, WLH*3+8, sw, WLH, text4, 1)
  end
    
  #--------------------------------------------------------------------------
  # refresh
  #--------------------------------------------------------------------------
  def refresh(page)
    @data = []; @page = page
    refresh_info_window
    for i in (@page*10+1)..(@page*10+10); @data.push(i); end
    @item_max = @data.size
    create_contents
    for i in 0...@item_max; draw_item(i); end
  end
  
  #--------------------------------------------------------------------------
  # draw_item
  #--------------------------------------------------------------------------
  def draw_item(index)
    rect = item_rect(index)
    self.contents.clear_rect(rect)
    self.contents.font.color = normal_color
    switch_id = @data[index]
    rect.x += 4
    rect.width -= 8
    if switch_id > $data_system.switches.size-1
      self.contents.font.color.alpha = 128
      self.contents.draw_text(rect.x, rect.y, rect.width, WLH, "N/A")
      return
    end
    name = $data_system.switches[switch_id]
    self.contents.font.color.alpha = name == "" ? 128 : 255
    if $game_switches[switch_id]
      if name == ""
        self.contents.font.color = knockout_color
        name = "ATTENTION"
      else
        self.contents.font.color = crisis_color
      end
    end
    name = "Undefined" if name == ""
    text = sprintf("S%04d:%s", switch_id, name)
    value = $game_switches[switch_id] ? "[ON]" : "[OFF]"
    self.contents.draw_text(rect.x, rect.y, rect.width, WLH, text, 0)
    self.contents.draw_text(rect.x, rect.y, rect.width, WLH, value, 2)
  end

end # Window_DebugSwitches

#===============================================================================
# Window_DebugVariables
#===============================================================================

class Window_DebugVariables < Window_Selectable
  
  #--------------------------------------------------------------------------
  # initialize
  #--------------------------------------------------------------------------
  def initialize(page, info_window)
    super(160, 0, Graphics.width - 160, 272)
    self.index = 0
    @info_window = info_window
    refresh(page)
  end
  
  #--------------------------------------------------------------------------
  # variable
  #--------------------------------------------------------------------------
  def variable; return @data[self.index]; end
  
  #--------------------------------------------------------------------------
  # refresh_info_window
  #--------------------------------------------------------------------------
  def refresh_info_window
    @info_window.contents.clear
    sw = @info_window.contents.width
    value1 = @page*10+1
    value2 = [@page*10+10, $data_system.switches.size-1].min
    text1 = sprintf("Variables %d to %d", value1, value2)
    text2 = "Press L or Left to page backward."
    text3 = "Press R or Right to page forward."
    text4 = "Press Z to modify variable."
    @info_window.contents.draw_text(0, WLH*0, sw, WLH, text1, 1)
    @info_window.contents.draw_text(0, WLH*1+8, sw, WLH, text2, 1)
    @info_window.contents.draw_text(0, WLH*2+8, sw, WLH, text3, 1)
    @info_window.contents.draw_text(0, WLH*3+8, sw, WLH, text4, 1)
  end
    
  #--------------------------------------------------------------------------
  # refresh
  #--------------------------------------------------------------------------
  def refresh(page)
    @data = []; @page = page
    refresh_info_window
    for i in (@page*10+1)..(@page*10+10); @data.push(i); end
    @item_max = @data.size
    create_contents
    for i in 0...@item_max; draw_item(i); end
  end
  
  #--------------------------------------------------------------------------
  # draw_item
  #--------------------------------------------------------------------------
  def draw_item(index)
    rect = item_rect(index)
    self.contents.clear_rect(rect)
    self.contents.font.color = normal_color
    variable_id = @data[index]
    rect.x += 4
    rect.width -= 8
    if variable_id > $data_system.variables.size-1
      self.contents.font.color.alpha = 128
      self.contents.draw_text(rect.x, rect.y, rect.width, WLH, "N/A")
      return
    end
    name = $data_system.variables[variable_id]
    self.contents.font.color.alpha = name == "" ? 128 : 255
    if $game_variables[variable_id] != 0
      if name == ""
        self.contents.font.color = knockout_color
        name = "ATTENTION"
      else
        self.contents.font.color = crisis_color
      end
    end
    name = "Undefined" if name == ""
    text = sprintf("V%04d:%s", variable_id, name)
    value = $game_variables[variable_id]
    self.contents.draw_text(rect.x, rect.y, rect.width, WLH, text, 0)
    self.contents.draw_text(rect.x, rect.y, rect.width, WLH, value, 2)
  end

end # Window_DebugVariables

#===============================================================================
# Window_DebugInput
#===============================================================================

class Window_DebugInput < Window_Selectable
  
  #--------------------------------------------------------------------------
  # public instance variables
  #--------------------------------------------------------------------------
  attr_accessor :value
  attr_accessor :host_window
  
  #--------------------------------------------------------------------------
  # initialize
  #--------------------------------------------------------------------------
  def initialize(host_window)
    dw = Graphics.width*3/4
    dx = (Graphics.width-dw)/2
    dh = WLH*2+32
    dy = (Graphics.height-dh)/2
    super(dx, dy, dw, dh)
    self.back_opacity = 255
    @spacing = 0
    @host_window = host_window
    if @host_window.is_a?(Window_DebugVariables)
      @column_max = @item_max = 9
      @text = $data_system.variables[@host_window.variable]
      @text = "Undefined" if @text == ""
      @text = sprintf("V%04d:%s", @host_window.variable, @text)
      @value = $game_variables[@host_window.variable]
      @max_value =  99999999
      @min_value = -99999999
      self.index = 8
    else; return
    end
    self.contents.clear
    draw_input_title
    refresh
  end
  
  #--------------------------------------------------------------------------
  # refresh
  #--------------------------------------------------------------------------
  def refresh
    if @host_window.is_a?(Window_DebugVariables)
      for i in 0...@item_max; draw_variable(i); end
    end
  end
  
  #--------------------------------------------------------------------------
  # value=
  #--------------------------------------------------------------------------
  def value=(value)
    @value = value
    @value = [[@value, @max_value].min, @min_value].max
  end
  
  #--------------------------------------------------------------------------
  # draw_input_title
  #--------------------------------------------------------------------------
  def draw_input_title
    self.contents.draw_text(0, 0, self.width-32, WLH, @text, 1)
  end
  
  #--------------------------------------------------------------------------
  # draw_variable
  #--------------------------------------------------------------------------
  def draw_variable(index)
    rect = item_rect(index)
    self.contents.clear_rect(rect)
    if index == 0
      text = @value >= 0 ? "+" : "-"
    else
      text = @value.abs % 10**(@column_max-index)/(10**(@column_max-1-index))
    end
    self.contents.draw_text(rect.x, rect.y, rect.width, WLH, text, 1)
  end
  
  #--------------------------------------------------------------------------
  # item_rect
  #--------------------------------------------------------------------------
  def item_rect(index)
    rect = Rect.new(0, 0, 24, WLH)
    rect.x = (self.contents.width-32-24*@column_max)/2+index*24+12
    rect.y = index / @column_max * WLH + WLH
    return rect
  end
  
  #--------------------------------------------------------------------------
  # update
  #--------------------------------------------------------------------------
  def update
    if Input.trigger?(Input::RIGHT) and @index == @column_max -1
      Sound.play_cursor
      @index = 0
    elsif Input.trigger?(Input::LEFT) and @index == 0
      Sound.play_cursor
      @index = @column_max -1
    else
      super
    end
  end

end # Window_DebugInput

#===============================================================================
# Window_DebugEntry
#===============================================================================

class Window_DebugEntry < Window_Base
  
  #--------------------------------------------------------------------------
  # public instance variables
  #--------------------------------------------------------------------------
  attr_accessor :text
  
  #--------------------------------------------------------------------------
  # initialize
  #--------------------------------------------------------------------------
  def initialize
    super(-16, Graphics.height-40, Graphics.width+32, 56)
    self.contents.font.name = ["UmePlus Gothic", "Courier New"]
    self.contents.font.bold = false
    self.contents.font.italic = false
    self.contents.font.shadow = false
    self.contents.font.size = 20
    self.contents.font.color = Color.new(192, 192, 192)
    self.opacity = 0
    @text = ""
    @rect = Rect.new(4, 0, 8192, 24)
    refresh
  end
  
  #--------------------------------------------------------------------------
  # refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    cw = contents.width
    self.contents.fill_rect(0, 0, cw, WLH, Color.new(192, 192, 192))
    self.contents.fill_rect(1, 1, contents.width-2, WLH-2, Color.new(0, 0, 0))
    if @blink
      self.contents.draw_text(@rect, @text + "▂", 0)
    else
      self.contents.draw_text(@rect, @text, 0)
    end
  end
  
  #--------------------------------------------------------------------------
  # update
  #--------------------------------------------------------------------------
  def update
    super
    @blink = !@blink if (Graphics.frame_count % 30 == 0)
    if Input.typing?
      @text += Input.key_type
    elsif Input.repeat?(Input::BACK) and @text.size > 0
      @text[@text.size - 1] = ""
    end
    @rect.x = @text.size < 52 ? 4 : 4 - (@text.size - 52) * 10
    refresh
  end
  
end # Window_DebugInput

#===============================================================================
# Window_DebugTeleportList
#===============================================================================

class Window_DebugTeleportList < Window_Selectable
  
  #--------------------------------------------------------------------------
  # initialize
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 160, Graphics.height)
    @map_info = load_data('Data/MapInfos.rvdata')
    refresh
    self.index = 0
  end
  
  #--------------------------------------------------------------------------
  # map_id
  #--------------------------------------------------------------------------
  def map_id; return @data[self.index]; end
  
  #--------------------------------------------------------------------------
  # refresh
  #--------------------------------------------------------------------------
  def refresh
    @data = []
    for i in 1..999
      filename = sprintf("Data/Map%03d.rvdata", i)
      next unless FileTest.exist?(filename)
      @data.push(i)
    end
    @item_max = @data.size
    create_contents
    for i in 0...@item_max; draw_item(i); end
  end
  
  #--------------------------------------------------------------------------
  # draw_item
  #--------------------------------------------------------------------------
  def draw_item(index)
    rect = item_rect(index)
    self.contents.clear_rect(rect)
    map_id = @data[index]
    return if map_id == nil
    text = @map_info[map_id].name
    rect.x += 4
    rect.width = 8192
    self.contents.font.size = 16
    self.contents.draw_text(rect, text, 0)
  end
  
end # Window_DebugTeleportList

#===============================================================================
# Window_DebugTeleportMap
#===============================================================================

class Window_DebugTeleportMap < Window_Base
  
  #--------------------------------------------------------------------------
  # public instance variables
  #--------------------------------------------------------------------------
  attr_accessor :tilex
  attr_accessor :tiley
  
  #--------------------------------------------------------------------------
  # initialize
  #--------------------------------------------------------------------------
  def initialize(main_window)
    super(160, 80, Graphics.width-160, Graphics.height-80)
    self.active = false
    @main_window = main_window
    create_tilemap
    refresh
  end
  
  #--------------------------------------------------------------------------
  # dispose
  #--------------------------------------------------------------------------
  def dispose
    @tile_viewport.dispose
    @tilemap.dispose
    super
  end
  
  #--------------------------------------------------------------------------
  # create_tilemap
  #--------------------------------------------------------------------------
  def create_tilemap
    c = self.contents
    @tile_viewport = Viewport.new(self.x+16, self.y+16, c.width, c.height)
    @tile_viewport.z = 200
    @tilemap = Tilemap.new(@tile_viewport)
    @tilemap.bitmaps[0] = Cache.system("TileA1")
    @tilemap.bitmaps[1] = Cache.system("TileA2")
    @tilemap.bitmaps[2] = Cache.system("TileA3")
    @tilemap.bitmaps[3] = Cache.system("TileA4")
    @tilemap.bitmaps[4] = Cache.system("TileA5")
    @tilemap.bitmaps[5] = Cache.system("TileB")
    @tilemap.bitmaps[6] = Cache.system("TileC")
    @tilemap.bitmaps[7] = Cache.system("TileD")
    @tilemap.bitmaps[8] = Cache.system("TileE")
  end
  
  #--------------------------------------------------------------------------
  # update
  #--------------------------------------------------------------------------
  def update
    super
    @tile_viewport.update
    @tilemap.update
    refresh if @map_id != @main_window.map_id
    calculate_coordinates
    return unless self.active
    if Input.repeat?(Input::UP)
      move_tilemap(8)
    elsif Input.repeat?(Input::DOWN)
      move_tilemap(2)
    elsif Input.repeat?(Input::LEFT)
      move_tilemap(4)
    elsif Input.repeat?(Input::RIGHT)
      move_tilemap(6)
    end
  end
  
  #--------------------------------------------------------------------------
  # refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    @map_id = @main_window.map_id
    @map = load_data(sprintf("Data/Map%03d.rvdata", @map_id))
    @tilemap.map_data = @map.data
    @tilemap.ox = 0
    @tilemap.oy = 24
  end
  
  #--------------------------------------------------------------------------
  # calculate_coordinates
  #--------------------------------------------------------------------------
  def calculate_coordinates
    @tilex = (@tilemap.ox/32)+5
    @tiley = ((@tilemap.oy+8)/32)+4
  end
  
  #--------------------------------------------------------------------------
  # move_tilemap
  #--------------------------------------------------------------------------
  def move_tilemap(direction)
    return if @map == nil
    calculate_coordinates
    case direction
    when 8 # Up
      if @tiley <= 0
        @tilemap.oy = (@map.height - 5) * 32 - 8
      else
        @tilemap.oy -= 32
      end
    when 2 # Down
      if @tiley >= @map.height - 1
        @tilemap.oy = -136
      else
        @tilemap.oy += 32
      end
    when 4 # Left
      if @tilex <= 0
        @tilemap.ox = (@map.width - 6) * 32
      else
        @tilemap.ox -= 32
      end
    when 6 # Right
      if @tilex >= @map.width - 1
        @tilemap.ox = -160
      else
        @tilemap.ox += 32
      end
    end
    Sound.play_cursor
    calculate_coordinates
  end
  
end # Window_DebugTeleportList

#===============================================================================
# Window_DebugTeleportHelp
#===============================================================================

class Window_DebugTeleportHelp < Window_Base
  
  #--------------------------------------------------------------------------
  # initialize
  #--------------------------------------------------------------------------
  def initialize(main_window, map_window)
    @main_window = main_window
    @map_window = map_window
    super(160, 0, Graphics.width-160, 80)
    @map_info = load_data('Data/MapInfos.rvdata')
    refresh
  end
  
  #--------------------------------------------------------------------------
  # update
  #--------------------------------------------------------------------------
  def update
    super
    if @map_id != @main_window.map_id
      refresh
    elsif @tilex != @map_window.tilex
      refresh
    elsif @tiley != @map_window.tiley
      refresh
    end
  end
  
  #--------------------------------------------------------------------------
  # refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    cw = contents.width
    @map_id = @main_window.map_id
    @tilex = @map_window.tilex
    @tiley = @map_window.tiley
    name = @map_info[@map_id].name
    self.contents.draw_text(4, 0, cw-8, WLH, name, 1)
    map = load_data(sprintf("Data/Map%03d.rvdata", @map_id))
    text = sprintf("Map%03d", @map_id)
    self.contents.draw_text(4, WLH, cw/4, WLH, text, 1)
    text = sprintf("%d×%d", map.width, map.height)
    self.contents.draw_text(cw*1/4, WLH, cw/4, WLH, text, 1)
    text = sprintf("X:%03d", @tilex)
    self.contents.draw_text(cw*2/4, WLH, cw/4, WLH, text, 1)
    text = sprintf("Y:%03d", @tiley)
    self.contents.draw_text(cw*3/4, WLH, cw/4, WLH, text, 1)
  end
  
end # Window_DebugTeleportHelp

#===============================================================================
# Window_DebugItems
#===============================================================================

class Window_DebugItems < Window_Selectable
  
  #--------------------------------------------------------------------------
  # initialize
  #--------------------------------------------------------------------------
  def initialize(info_window)
    super(160, 0, Graphics.width - 160, 272)
    self.index = 0
    @info_window = info_window
    refresh
  end
  
  #--------------------------------------------------------------------------
  # item
  #--------------------------------------------------------------------------
  def item; return @data[self.index]; end
  
  #--------------------------------------------------------------------------
  # refresh_info_window
  #--------------------------------------------------------------------------
  def refresh_info_window
    @info_window.contents.clear
    sw = @info_window.contents.width
    text1 = @title + " Adjustment Window"
    text2 = "Press Right to increase item by 1."
    text3 = "Press Left to decrease item by 1."
    text4 = "Hold Shift to adjust by 10."
    @info_window.contents.draw_text(0, WLH*0, sw, WLH, text1, 1)
    @info_window.contents.draw_text(0, WLH*1+8, sw, WLH, text2, 1)
    @info_window.contents.draw_text(0, WLH*2+8, sw, WLH, text3, 1)
    @info_window.contents.draw_text(0, WLH*3+8, sw, WLH, text4, 1)
  end
  
  #--------------------------------------------------------------------------
  # title
  #--------------------------------------------------------------------------
  def title; return @title; end
    
  #--------------------------------------------------------------------------
  # refresh
  #--------------------------------------------------------------------------
  def refresh
    @data = $data_items.clone
    @data.delete(nil)
    @title = "Item"
    refresh_info_window
    @item_max = @data.size
    create_contents
    for i in 0...@item_max; draw_item(i); end
  end
  
  #--------------------------------------------------------------------------
  # draw_item
  #--------------------------------------------------------------------------
  def draw_item(index)
    rect = item_rect(index)
    self.contents.clear_rect(rect)
    item = @data[index]
    return if item == nil
    enabled = $game_party.item_number(item) > 0
    draw_icon(item.icon_index, rect.x, rect.y, enabled)
    rect.x += 24
    rect.width -= 28
    self.contents.font.color.alpha = enabled ? 255 : 128
    self.contents.draw_text(rect, item.name, 0)
    text = sprintf("×%02d", 1)
    text = sprintf("×%02d", $game_party.item_number(item))
    self.contents.draw_text(rect, text, 2)
  end

end # Window_DebugItems

#===============================================================================
# Window_DebugWeapons
#===============================================================================

class Window_DebugWeapons < Window_DebugItems
  
  #--------------------------------------------------------------------------
  # refresh
  #--------------------------------------------------------------------------
  def refresh
    @data = $data_weapons.clone
    @data.delete(nil)
    @title = "Weapon"
    refresh_info_window
    @item_max = @data.size
    create_contents
    for i in 0...@item_max; draw_item(i); end
  end

end # Window_DebugWeapons

#===============================================================================
# Window_DebugArmours
#===============================================================================

class Window_DebugArmours < Window_DebugItems
  
  #--------------------------------------------------------------------------
  # refresh
  #--------------------------------------------------------------------------
  def refresh
    @data = $data_armors.clone
    @data.delete(nil)
    @title = "Armour"
    refresh_info_window
    @item_max = @data.size
    create_contents
    for i in 0...@item_max; draw_item(i); end
  end

end # Window_DebugArmours

#===============================================================================
# Window_DebugBattle
#===============================================================================

class Window_DebugBattle < Window_Selectable
  
  #--------------------------------------------------------------------------
  # initialize
  #--------------------------------------------------------------------------
  def initialize
    super(160, 0, Graphics.width-160, Graphics.height)
    self.index = 0
    refresh
  end
  
  #--------------------------------------------------------------------------
  # troop
  #--------------------------------------------------------------------------
  def troop; return @data[self.index]; end
  
  #--------------------------------------------------------------------------
  # refresh
  #--------------------------------------------------------------------------
  def refresh
    @data = $data_troops
    @data.delete(nil)
    @item_max = @data.size
    create_contents
    for i in 0...@item_max; draw_item(i); end
  end
  
  #--------------------------------------------------------------------------
  # draw_item
  #--------------------------------------------------------------------------
  def draw_item(index)
    rect = item_rect(index)
    self.contents.clear_rect(rect)
    troop = @data[index]
    return if troop == nil
    rect.x += 4
    rect.width -= 8
    text = sprintf("B%04d:%s", troop.id, troop.name)
    self.contents.draw_text(rect, text, 0)
  end
  
end # Window_DebugBattle

#===============================================================================
# Window_DebugEvents
#===============================================================================

class Window_DebugEvents < Window_Selectable
  
  #--------------------------------------------------------------------------
  # initialize
  #--------------------------------------------------------------------------
  def initialize
    super(160, 0, Graphics.width-160, Graphics.height)
    self.index = 0
    refresh
  end
  
  #--------------------------------------------------------------------------
  # id
  #--------------------------------------------------------------------------
  def id; return @data[self.index]; end
  
  #--------------------------------------------------------------------------
  # refresh
  #--------------------------------------------------------------------------
  def refresh
    @data = []
    for i in 1..$data_common_events.size
      next unless include?(i)
      @data.push(i)
    end
    @item_max = @data.size
    create_contents
    for i in 0...@item_max; draw_item(i); end
    end
    
  #--------------------------------------------------------------------------
  # include?
  #--------------------------------------------------------------------------
  def include?(i)
    return false if i == 0
    event = $data_common_events[i]
    return false if event == nil
    return false if event.list.size <= 1
    return true
  end
  
  #--------------------------------------------------------------------------
  # draw_item
  #--------------------------------------------------------------------------
  def draw_item(index)
    rect = item_rect(index)
    self.contents.clear_rect(rect)
    event = $data_common_events[@data[index]]
    return if event == nil
    rect.x += 4
    rect.width -= 8
    if event.name == ""
      self.contents.font.color = knockout_color
      name = "ATTENTION"
    else
      self.contents.font.color = normal_color
      name = event.name
    end
    text = sprintf("E%04d:%s", event.id, name)
    self.contents.draw_text(rect, text, 0)
  end
  
end # Window_DebugEvents

#===============================================================================
# Scene_Base
#===============================================================================

class Scene_Base
  
  #--------------------------------------------------------------------------
  # alias method: update
  #--------------------------------------------------------------------------
  alias update_scene_base update unless $@
  def update
    update_scene_base
    trigger_debug_window_entry
  end
  
  #--------------------------------------------------------------------------
  # new method: trigger_debug_window_entry
  #--------------------------------------------------------------------------
  def trigger_debug_window_entry
    return unless $TEST or $BTEST
    return unless $imported["KeyboardInput"]
    if Input.trigger?(Input::F10)
      Sound.play_decision
      process_debug_window_entry
    end
  end
  
  #--------------------------------------------------------------------------
  # new method: process_debug_window_entry
  #--------------------------------------------------------------------------
  def process_debug_window_entry
    Graphics.freeze
    viewport = Viewport.new(0, 0, Graphics.width, Graphics.height)
    viewport.z = 8000
    @debug_entry_window = Window_DebugEntry.new
    @debug_entry_window.viewport = viewport
    Graphics.transition(4)
    #---
    update_debug_window_entry
    #---
    Graphics.freeze
    @debug_entry_window.dispose
    @debug_entry_window = nil
    viewport.dispose
    Graphics.transition(4)
  end
  
  #--------------------------------------------------------------------------
  # new method: update_debug_window_entry
  #--------------------------------------------------------------------------
  def update_debug_window_entry
    loop do
      if $scene.is_a?(Scene_Map) or $scene.is_a?(Scene_Battle)
        update_basic
      else
        Graphics.update
        Input.update
      end
      @debug_entry_window.update
      if Input.trigger?(Input::ESC)
        Sound.play_cancel
        if @debug_entry_window.text.size > 0
          @debug_entry_window.text = ""
        else
          break
        end
      elsif Input.trigger?(Input::F10)
        Sound.play_cancel
        break
      elsif Input.trigger?(Input::ENTER)
        code = @debug_entry_window.text
        begin
          Sound.play_decision
          eval(code)
          break
        rescue
          Sound.play_buzzer
        end
      end
    end
  end
  
end # Scene_Base

#===============================================================================
# Scene_Map
#===============================================================================

class Scene_Map < Scene_Base
  
  #--------------------------------------------------------------------------
  # overwrite method: update_call_debug
  #--------------------------------------------------------------------------
  def update_call_debug
    return unless $TEST
    if Input.trigger?(Input::F5)
      if Input.press?(Input::ALT)
        $game_temp.common_event_id = YEM::DEBUG::ALT[:f5]
      elsif Input.press?(Input::CTRL)
        $game_temp.common_event_id = YEM::DEBUG::CTRL[:f5]
      elsif Input.press?(Input::SHIFT)
        $game_temp.common_event_id = YEM::DEBUG::SHIFT[:f5]
      end
    elsif Input.trigger?(Input::F6)
      if Input.press?(Input::ALT)
        $game_temp.common_event_id = YEM::DEBUG::ALT[:f6]
      elsif Input.press?(Input::CTRL)
        $game_temp.common_event_id = YEM::DEBUG::CTRL[:f6]
      elsif Input.press?(Input::SHIFT)
        $game_temp.common_event_id = YEM::DEBUG::SHIFT[:f6]
      end
    elsif Input.trigger?(Input::F7)
      if Input.press?(Input::ALT)
        $game_temp.common_event_id = YEM::DEBUG::ALT[:f7]
      elsif Input.press?(Input::CTRL)
        $game_temp.common_event_id = YEM::DEBUG::CTRL[:f7]
      elsif Input.press?(Input::SHIFT)
        $game_temp.common_event_id = YEM::DEBUG::SHIFT[:f7]
      end
    elsif Input.trigger?(Input::F8)
      if Input.press?(Input::ALT)
        $game_temp.common_event_id = YEM::DEBUG::ALT[:f8]
      elsif Input.press?(Input::CTRL)
        $game_temp.common_event_id = YEM::DEBUG::CTRL[:f8]
      elsif Input.press?(Input::SHIFT)
        $game_temp.common_event_id = YEM::DEBUG::SHIFT[:f8]
      end
    elsif Input.trigger?(Input::F9)
      return if @debug_entry_window != nil
      if Input.press?(Input::ALT)
        $game_temp.common_event_id = YEM::DEBUG::ALT[:f9]
      elsif Input.press?(Input::CTRL)
        $game_temp.common_event_id = YEM::DEBUG::CTRL[:f9]
      elsif Input.press?(Input::SHIFT)
        $game_temp.common_event_id = YEM::DEBUG::SHIFT[:f9]
      else
        Sound.play_decision
        $game_temp.next_scene = "debug"
      end
    end
  end
  
end # Scene_Map

#===============================================================================
# Scene_Debug
#===============================================================================

class Scene_Debug < Scene_Base

  #--------------------------------------------------------------------------
  # overwrite method: start
  #--------------------------------------------------------------------------
  def start
    super
    create_menu_background
    create_command_window
    @dummy_window = Window_Base.new(160, 0, Graphics.width - 
      @command_window.width, Graphics.height)      
  end
  
  #--------------------------------------------------------------------------
  # overwrite method: terminate
  #--------------------------------------------------------------------------
  def terminate
    super
    dispose_menu_background
    @command_window.dispose if @command_window != nil
    @dummy_window.dispose if @dummy_window != nil
    $game_map.refresh
  end
  
  #--------------------------------------------------------------------------
  # new method: create_command_window
  #--------------------------------------------------------------------------
  def create_command_window
    commands = []; @data = []
    for command in YEM::DEBUG::COMMANDS
      case command
      when :switches, :variables, :items, :weapons, :armours, :events
      when :teleport, :battle
        next if $game_temp.in_battle
      else; next
      end
      @data.push(command)
      commands.push(YEM::DEBUG::VOCAB[command])
    end
    @command_window = Window_Command.new(160, commands)
    @command_window.height = Graphics.height
    @var_page = @switch_page = 0
  end
  
  #--------------------------------------------------------------------------
  # new method: return_scene
  #--------------------------------------------------------------------------
  def return_scene
    if $game_temp.in_battle
      $scene = Scene_Battle.new
    else
      $scene = Scene_Map.new
    end
  end
  
  #--------------------------------------------------------------------------
  # overwrite method: update
  #--------------------------------------------------------------------------
  def update
    super
    update_menu_background
    if @command_window.active
      update_command_selection
    elsif @switch_window != nil and @switch_window.active
      update_switch_selection
    elsif @variable_window != nil and @variable_window.active
      update_variable_selection
    elsif @input_window != nil and @input_window.active
      update_input_window
    elsif @teleport_list != nil and @teleport_list.active
      update_teleport_list
    elsif @teleport_map != nil and @teleport_map.active
      update_teleport_map
    elsif @item_window != nil and @item_window.active
      update_item_selection
    elsif @battle_window != nil and @battle_window.active
      update_battle_selection
    elsif @event_window != nil and @event_window.active
      update_event_selection
    end
  end
  
  #--------------------------------------------------------------------------
  # new method: update_command_selection
  #--------------------------------------------------------------------------
  def update_command_selection
    @command_window.update
    if Input.trigger?(Input::B) or Input.trigger?(Input::F9)
      Sound.play_cancel
      return_scene
    elsif Input.trigger?(Input::C)
      Sound.play_decision
      case @data[@command_window.index]
      when :switches
        @command_window.active = false
        @dummy_window.visible = false
        @info_window = Window_Base.new(160, 272, Graphics.width-160,
          Graphics.height-272)
        @switch_window = Window_DebugSwitches.new(@switch_page, @info_window)
      when :variables
        @command_window.active = false
        @dummy_window.visible = false
        @info_window = Window_Base.new(160, 272, Graphics.width-160,
          Graphics.height-272)
        @variable_window = Window_DebugVariables.new(@var_page, @info_window)
      when :teleport
        @command_window.active = false
        @command_window.visible = false
        @dummy_window.visible = false
        @teleport_list = Window_DebugTeleportList.new
        @teleport_map = Window_DebugTeleportMap.new(@teleport_list)
        @teleport_help = Window_DebugTeleportHelp.new(@teleport_list, 
          @teleport_map)
        @teleport_cursor = Sprite_TeleportCursor.new
      when :items
        @command_window.active = false
        @dummy_window.visible = false
        @info_window = Window_Base.new(160, 272, Graphics.width-160,
          Graphics.height-272)
        @item_window = Window_DebugItems.new(@info_window)
      when :weapons
        @command_window.active = false
        @dummy_window.visible = false
        @info_window = Window_Base.new(160, 272, Graphics.width-160,
          Graphics.height-272)
        @item_window = Window_DebugWeapons.new(@info_window)
      when :armours
        @command_window.active = false
        @dummy_window.visible = false
        @info_window = Window_Base.new(160, 272, Graphics.width-160,
          Graphics.height-272)
        @item_window = Window_DebugArmours.new(@info_window)
      when :battle
        @command_window.active = false
        @dummy_window.visible = false
        @battle_window = Window_DebugBattle.new
      when :events
        @command_window.active = false
        @dummy_window.visible = false
        @event_window = Window_DebugEvents.new
      end
    end
  end
  
  #--------------------------------------------------------------------------
  # new method: update_switch_selection
  #--------------------------------------------------------------------------
  def update_switch_selection
    @switch_window.update
    if Input.trigger?(Input::B)
      Sound.play_cancel
      @command_window.active = true
      @dummy_window.visible = true
      @switch_window.dispose
      @switch_window = nil
      @info_window.dispose
      @info_window = nil
    elsif Input.repeat?(Input::R) or Input.repeat?(Input::RIGHT)
      Sound.play_cursor
      @switch_page += 1
      @switch_page = 0 if @switch_page+1 > ($data_system.switches.size-1+9)/10
      @switch_window.refresh(@switch_page)
    elsif Input.repeat?(Input::L) or Input.repeat?(Input::LEFT)
      Sound.play_cursor
      @switch_page -= 1
      @switch_page = ($data_system.switches.size-1+9)/10-1 if @switch_page < 0
      @switch_window.refresh(@switch_page)
    elsif Input.trigger?(Input::C)
      switch = @switch_window.switch
      if switch > $data_system.switches.size-1
        Sound.play_buzzer
        return
      end
      Sound.play_decision
      $game_switches[switch] = !$game_switches[switch]
      @switch_window.refresh(@switch_page)
    end
  end
  
  #--------------------------------------------------------------------------
  # new method: update_variable_selection
  #--------------------------------------------------------------------------
  def update_variable_selection
    @variable_window.update
    if Input.trigger?(Input::B)
      Sound.play_cancel
      @command_window.active = true
      @dummy_window.visible = true
      @variable_window.dispose
      @variable_window = nil
      @info_window.dispose
      @info_window = nil
    elsif Input.repeat?(Input::R) or Input.repeat?(Input::RIGHT)
      Sound.play_cursor
      @var_page += 1
      @var_page = 0 if @var_page+1 > ($data_system.variables.size-1+9)/10
      @variable_window.refresh(@var_page)
    elsif Input.repeat?(Input::L) or Input.repeat?(Input::LEFT)
      Sound.play_cursor
      @var_page -= 1
      @var_page = ($data_system.switches.size-1+9)/10-1 if @var_page < 0
      @variable_window.refresh(@var_page)
    elsif Input.trigger?(Input::C)
      variable = @variable_window.variable
      if variable > $data_system.variables.size-1
        Sound.play_buzzer
        return
      end
      Sound.play_decision
      @input_window = Window_DebugInput.new(@variable_window)
      @variable_window.active = false
    end
  end
  
  #--------------------------------------------------------------------------
  # new method: update_input_window
  #--------------------------------------------------------------------------
  def update_input_window
    @input_window.update
    if Input.trigger?(Input::B)
      Sound.play_cancel
      @input_window.host_window.active = true
      @input_window.dispose
      @input_window = nil
    elsif Input.repeat?(Input::UP)
      Sound.play_cursor
      if @input_window.index == 0
        @input_window.value *= -1
      else
        value = 10**(8-@input_window.index)
        @input_window.value += value
      end
      @input_window.refresh
    elsif Input.repeat?(Input::DOWN)
      Sound.play_cursor
      if @input_window.index == 0
        @input_window.value *= -1
      else
        value = 10**(8-@input_window.index)
        @input_window.value -= value
      end
      @input_window.refresh
    elsif Input.trigger?(Input::C)
      Sound.play_decision
      if @input_window.host_window.is_a?(Window_DebugVariables)
        $game_variables[@variable_window.variable] = @input_window.value
        @variable_window.draw_item(@variable_window.index)
      end
      @input_window.host_window.active = true
      @input_window.dispose
      @input_window = nil
    end
  end
  
  #--------------------------------------------------------------------------
  # new method: update_teleport_list
  #--------------------------------------------------------------------------
  def update_teleport_list
    @teleport_list.update
    @teleport_map.update
    @teleport_help.update
    @teleport_cursor.update
    if Input.trigger?(Input::B)
      Sound.play_cancel
      @command_window.active = true
      @command_window.visible = true
      @dummy_window.visible = true
      dispose_teleport_items
    elsif Input.trigger?(Input::C)
      Sound.play_decision
      @teleport_list.active = false
      @teleport_map.active = true
      @teleport_cursor.bitmap.hue_change(-60)
    end
  end
  
  #--------------------------------------------------------------------------
  # new method: update_teleport_map
  #--------------------------------------------------------------------------
  def update_teleport_map
    @teleport_list.update
    @teleport_map.update
    @teleport_help.update
    @teleport_cursor.update
    if Input.trigger?(Input::B)
      Sound.play_cancel
      @teleport_list.active = true
      @teleport_map.active = false
      @teleport_cursor.bitmap.hue_change(60)
    elsif Input.trigger?(Input::C)
      Sound.play_decision
      @teleport_map.calculate_coordinates
      map_id = @teleport_list.map_id
      map_x = @teleport_map.tilex
      map_y = @teleport_map.tiley
      dispose_teleport_items
      $scene = Scene_Map.new
      direction = $game_player.direction
      $game_player.reserve_transfer(map_id, map_x, map_y, direction)
    end
  end
  
  #--------------------------------------------------------------------------
  # new method: dispose_teleport_items
  #--------------------------------------------------------------------------
  def dispose_teleport_items
    @teleport_list.dispose
    @teleport_list = nil
    @teleport_map.dispose
    @teleport_map = nil
    @teleport_help.dispose
    @teleport_help = nil
    @teleport_cursor.dispose
    @teleport_cursor = nil
  end
  
  #--------------------------------------------------------------------------
  # new method: update_item_selection
  #--------------------------------------------------------------------------
  def update_item_selection
    @item_window.update
    if Input.trigger?(Input::B)
      Sound.play_cancel
      @command_window.active = true
      @dummy_window.visible = true
      @item_window.dispose
      @item_window = nil
      @info_window.dispose
      @info_window = nil
    elsif Input.repeat?(Input::LEFT)
      Sound.play_cursor
      item = @item_window.item
      $game_party.lose_item(item, Input.press?(Input::SHIFT) ? 10 : 1)
      @item_window.draw_item(@item_window.index)
    elsif Input.repeat?(Input::RIGHT)
      Sound.play_cursor
      item = @item_window.item
      $game_party.gain_item(item, Input.press?(Input::SHIFT) ? 10 : 1)
      @item_window.draw_item(@item_window.index)
    elsif Input.repeat?(Input::X)
      Sound.play_cursor
      case @item_window.title
      when "Item";   group = $data_items
      when "Weapon"; group = $data_weapons
      when "Armour"; group = $data_armors
      end
      for obj in group
        next if obj == nil
        $game_party.lose_item(obj, Input.press?(Input::SHIFT) ? 10 : 1)
      end
      @item_window.refresh
    elsif Input.repeat?(Input::Y)
      Sound.play_cursor
      case @item_window.title
      when "Item";   group = $data_items
      when "Weapon"; group = $data_weapons
      when "Armour"; group = $data_armors
      end
      for obj in group
        next if obj == nil
        $game_party.gain_item(obj, Input.press?(Input::SHIFT) ? 10 : 1)
      end
      @item_window.refresh
    end
  end
  
  #--------------------------------------------------------------------------
  # new method: update_battle_selection
  #--------------------------------------------------------------------------
  def update_battle_selection
    @battle_window.update
    if Input.trigger?(Input::B)
      Sound.play_cancel
      @command_window.active = true
      @dummy_window.visible = true
      @battle_window.dispose
      @battle_window = nil
    elsif Input.trigger?(Input::C)
      Sound.play_decision
      troop = @battle_window.troop
      $game_troop.setup(troop.id - 1)
      $game_troop.can_escape = true
      $game_temp.battle_proc = nil
      $game_temp.next_scene = "battle"
      @battle_window.dispose
      $scene = Scene_Map.new
      $scene.preemptive_or_surprise
    end
  end
  
  #--------------------------------------------------------------------------
  # new method: update_event_selection
  #--------------------------------------------------------------------------
  def update_event_selection
    @event_window.update
    if Input.trigger?(Input::B)
      Sound.play_cancel
      @command_window.active = true
      @dummy_window.visible = true
      @event_window.dispose
      @event_window = nil
    elsif Input.trigger?(Input::C)
      Sound.play_decision
      $game_temp.common_event_id = @event_window.id
      @event_window.dispose
      return_scene
      $scene.process_battle_event if $scene.is_a?(Scene_Battle)
    end
  end
  
end # Scene_Debug

#===============================================================================
# 
# END OF FILE
# 
#==============================================================================