#===============================================================================
# 选择分辨率脚本                               updated: 11/15/2014
# 共同完成 by: Dekita 和 Venka
#===============================================================================
# Dekita's web site and available scripts:
# [url]www.dekyde.com[/url]
# [url]www.dekitarpg.wordpress.com[/url]
#===============================================================================
# Contact Venka by private message via the official RPG Maker forums here:
# [url]http://forums.rpgmakerweb.com/index.php?/user/2313-venka/[/url]
# The most up to date version of this script can be found here:
# [url]http://forums.rpgmakerweb.com/index.php?/topic/33783-resolution-selection-script/[/url]
#===============================================================================
# - 介绍 -
#-------------------------------------------------------------------------------
# 本脚本可以让玩家在游戏里选择分辨率。
# 在下面可以获取高分辨率的dll:
# [url]http://forums.rpgmakerweb.com/index.php?/topic/31953-high-res-rpg-maker-project/[/url]
#
# 该dll非必需,但如果你不使用它则无法将游戏分辨率扩大到 640x480 以上.
# 
#-------------------------------------------------------------------------------
# - 安装 -
#-------------------------------------------------------------------------------
# 本脚本还可以设定不用分辨率下使用的文件素材路径。
# 例如:在 宽屏3 (1024x640) 时,脚本会在 Graphics 文件夹里寻找一个叫 1024x640
# 的文件夹. 
# 则在该分辨率下,title1的文件会使用: "Graphics/1024x640/Title1/文件名" 
# 而不是原来的 "Graphics/Title1/文件名"
# 
# 如果脚本在分辨率文件夹中没有找到素材,则会在默认的文件路径中寻找(避免不受分
# 辨率影响的素材重复放置)
# 所以文件夹命名要准确 :)
# 
# 这是一个即插即用的脚本,请放在  "▼ 插件脚本" 之下
#
#===============================================================================
# ★☆★☆★☆★☆★☆★☆★☆★ TERMS AND CONDITIONS: ☆★☆★☆★☆★☆★☆★☆★☆★☆
#===============================================================================
# This script can be use in both commercial and non-commercial games.
# Credit must be given to Dekita and Venka.
#===============================================================================
module SelRes
  #-----------------------------------------------------------------------------
  # 设置分辨率
  # 列表里的分辨率类型可以任意增减,长款最好设定为32的倍数,因为1个图块=32像素.
  #-----------------------------------------------------------------------------
  Sizes = [
  # [ "名称", 宽度, 高度],
    [ "普通1"  ,   544,   416 ],
    [ "普通2"  ,   640,   480 ],
    [ "普通3"  ,   736,   576 ],
    [ "普通4"  ,   928,   704 ],
    [ "宽屏1"  ,   640,   416 ],
    [ "宽屏2"  ,   832,   544 ],
    [ "宽屏3"  ,  1024,   640 ],
    [ "宽屏4"  ,  1920,   960 ],
    [ "宽屏5"  ,  1920,   1200 ],
  ] # <- 别动
  #-----------------------------------------------------------------------------
  # 在上面的列表里选一个作为默认分辨率,游戏重新开始时会应用该设定.
  #-----------------------------------------------------------------------------
  Default_Size = ["宽屏1", 640, 416]
  #-----------------------------------------------------------------------------
  # 玩家在选择分辨率后显示的第一个场景。
  # 一般都是"Scene_Title"除非你使用自定义标题场景.
  #-----------------------------------------------------------------------------
  First_Scene  = Scene_Title
  Options_Text = "分辨率"
end
#===============================================================================
module SceneManager
#===============================================================================
  #----------------------------------------------------------------------------
  # ● overwrite method: first_scene_class
  #----------------------------------------------------------------------------
  def self.first_scene_class
    if Graphics.initialize_resolution[1]
      size = Graphics.initialize_resolution[0]
      Graphics.resize_screen(size[1],size[2])
      $BTEST ? Scene_Battle : SelRes::First_Scene
    else
      Scene_ResSel
    end
  end
end
#===============================================================================
module Cache
#===============================================================================
  #----------------------------------------------------------------------------
  # ● overwrite method: normal_bitmap
  #----------------------------------------------------------------------------
  def self.normal_bitmap(path)
    if path[0..8] == "Graphics/"
      begin
        reso_path = path.clone
        reso_path.insert(9, "#{Graphics.width}x#{Graphics.height}/")
        @cache[reso_path] = Bitmap.new(reso_path) unless include?(reso_path)
        @cache[reso_path]
      rescue
        @cache[path] = Bitmap.new(path) unless include?(path)
        @cache[path]
      end
    end
  end
end
#===============================================================================
class Window_ResSel < Window_Command
#===============================================================================
  #----------------------------------------------------------------------------
  # ● upgraded method: initialize
  #----------------------------------------------------------------------------
  def initialize(reso_size)
    super(0, 0)
    select(get_index(reso_size))
    @reso = reso_size
    hide
    move_window
  end
  #----------------------------------------------------------------------------
  # ○ new method: get_index
  #----------------------------------------------------------------------------
  def get_index(reso_size)
    item_max.times do |i|
      return i if @list[i][:name] == "#{reso_size[1]}x#{reso_size[2]}"
    end
  end
  #----------------------------------------------------------------------------
  # ○ new method: move_window
  #----------------------------------------------------------------------------
  def move_window
    self.x = (Graphics.width - window_width) * 0.5
    self.y = (Graphics.height - height) / 2
    show
  end
  #----------------------------------------------------------------------------
  # ● window settings
  #----------------------------------------------------------------------------
  def window_width; return 220; end
  #----------------------------------------------------------------------------
  # ● upgraded method: make_command_list
  #----------------------------------------------------------------------------
  def make_command_list
    SelRes::Sizes.compact.each do |data|
      add_command("#{data[1]}x#{data[2]}", :reso)
    end
  end
  #----------------------------------------------------------------------------
  # ● upgraded method: draw_item
  #----------------------------------------------------------------------------
  def draw_item(index)
    rect = item_rect_for_text(index)
    rect.width = item_rect_for_text(index).width * 0.5
    draw_text(rect, SelRes::Sizes[index][0], 1)
    rect.x += rect.width
    draw_text(rect, command_name(index), 1)
  end
end
#===============================================================================
class Scene_ResSel < Scene_Base
#===============================================================================
  #----------------------------------------------------------------------------
  # ● upgraded method: start
  #----------------------------------------------------------------------------
  def start
    size = Graphics.initialize_resolution[0]
    Graphics.resize_screen(size[1],size[2])
    super
    @command = Window_ResSel.new(size)
    @command.set_handler(:reso, method(:fix_reso))
  end
  #----------------------------------------------------------------------------
  # ○ new method: fix_reso
  #----------------------------------------------------------------------------
  def fix_reso
    @command.hide
    size = SelRes::Sizes.compact[@command.index]
    Graphics.resize_screen(size[1], size[2])
    Graphics.save_resolution(size)
    SceneManager.goto($BTEST ? Scene_Battle : SelRes::First_Scene)
  end
end
#===============================================================================
class << Graphics
#===============================================================================
  #----------------------------------------------------------------------------
  # ♦ Private Instance Variables
  #----------------------------------------------------------------------------
  ReadIni  = Win32API.new('kernel32', 'GetPrivateProfileString'  , 'ppppip', 'i')
  WriteIni = Win32API.new('kernel32', 'WritePrivateProfileString', 'pppp'  , 'i')
  #----------------------------------------------------------------------------
  # ○ new method: initialize_resolution
  #----------------------------------------------------------------------------
  def initialize_resolution
    @reso_setting = []
    load_resolution
    set_up = (@reso_setting[1] == 0 ? false : true)
    @reso_setting = SelRes::Default_Size if @reso_setting[1] == 0
    return [@reso_setting, set_up]
  end
  #----------------------------------------------------------------------------
  # ○ new method: load_resolution
  #----------------------------------------------------------------------------
  def load_resolution
    buffer = [].pack('x256')
    get_option = Proc.new do |key|
      l = ReadIni.call('Resolution', key, '0', buffer, buffer.size, './Game.ini')
      buffer[0, l]
    end
    @reso_setting[1] = get_option.call('Width', '0').to_i
    @reso_setting[2] = get_option.call('Height', '0').to_i
  end
  #----------------------------------------------------------------------------
  # ○ new method: save_resolution
  #----------------------------------------------------------------------------
  def save_resolution(setting)
    @reso_setting = setting
    set_option = Proc.new do |key, value|
      WriteIni.call('Resolution', key, value.to_s, './Game.ini')
    end
    set_option.call('Width', @reso_setting[1])
    set_option.call('Height', @reso_setting[2])
  end
end
#===============================================================================
class Window_TitleCommand < Window_Command
#===============================================================================
  #----------------------------------------------------------------------------
  # ● overwrite method: make_command_list
  #----------------------------------------------------------------------------
  def make_command_list
    add_command(Vocab::new_game,      :new_game)
    add_command(Vocab::continue,      :continue, continue_enabled)
    add_command(SelRes::Options_Text, :settings)
    add_command(Vocab::shutdown,      :shutdown)
  end
end
#===============================================================================
class Scene_Title < Scene_Base
#===============================================================================
  #----------------------------------------------------------------------------
  # ● alias method: create_command_window
  #----------------------------------------------------------------------------
  alias :reso_settings_ccw :create_command_window
  def create_command_window
    reso_settings_ccw
    @command_window.set_handler(:settings, method(:command_settings))
  end
  #----------------------------------------------------------------------------
  # ○ new method: command_settings
  #----------------------------------------------------------------------------
  def command_settings
    SceneManager.goto(Scene_ResSel)
  end
end