Advertisement
Vlue

Basic Options Menu

Sep 26th, 2013
10,103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 18.14 KB | None | 0 0
  1. #Basic Options Menu v1.4
  2. #----------#
  3. #Features: Provides a simple option menu for the player to do optiony stuff...
  4. #           Like volume effects for all, bgm, and se's. Fancy!
  5. #          
  6. #          Also additional options if you have Basic Window Resizer,
  7. #           Basic Message SE, and Basic Autosave.
  8. #
  9. #Usage:    Plug and play unless you want a bit more. Customization below.
  10. #
  11. #----------#
  12. #-- Script by: V.M of D.T
  13. #
  14. #- Questions or comments can be:
  15. #    posted on the thread for the script
  16. #    given by email: sumptuaryspade@live.ca
  17. #    provided on facebook: http://www.facebook.com/DaimoniousTailsGames
  18. #   All my other scripts and projects can be found here: http://daimonioustails.weebly.com/
  19. #
  20. #- Free to use in any project with credit given, donations always welcome!
  21.  
  22. #This lets you set the order of the windows via an array
  23. WINDOW_ORDER = [:master,:bgm,:se,:resolution,:message_se,:switch]
  24. #Basic options are:
  25. #  :master - Master Volume               :bgm     - Background Music Volume
  26. #  :se     - Sound Effect Volume         :switch  - Allow the player to turn on/off a switch or global variable
  27. #
  28. #-- Additional Options are:
  29. #  :resolution     -   If you have Basic Window Resizer installed
  30. #  :message_se     -   If you have Basic Message SE installed
  31. #  :autosave       -   If you have Basic Autosave installed
  32.  
  33. #These are all the strings for the help window and headers:
  34. module OPT_STR
  35.   #When the command window is selected
  36.   COMMAND_HELP       = "Confirm and exit or simply exit"
  37.   #Details for :master
  38.   MASTER_VOLUME      = "Master Volume"
  39.   MASTER_VOLUME_HELP = "Set the volume of all sound"
  40.   #Details for :bgm
  41.   BGM_VOLUME         = "BGM Volume"
  42.   BGM_VOLUME_HELP    = "Set the volume of music"
  43.   #Details for :se
  44.   SE_VOLUME          = "SE Volume"
  45.   SE_VOLUME_HELP     = "Set the volume of sound effects"
  46.   #Details for :resolution
  47.   RESOLUTION         = "Resolution"
  48.   RESOLUTION_HELP    = "Change the resolution of the game window"
  49.   #Details for :autosave
  50.   AUTOSAVE           = "Autosave"
  51.   AUTOSAVE_HELP      = "Enable/Disable autosaving"
  52.   #Details for :message
  53.   MESSAGE_SE         = "Message SE"
  54.   MESSAGE_SE_HELP    = "Enable/Disable the message sound effect"
  55.   #Details for :switch
  56.   SWITCH_DETAIL      = "$game_switches[5]"
  57.   SWITCH             = "Header"
  58.   SWITCH_HELP        = "Help Message"
  59.   SWITCH_COMMAND     = ["On","Off"]
  60. end
  61.  
  62. class Scene_Options < Scene_MenuBase
  63.   def start
  64.     super
  65.     $game_options.memorize_options
  66.     create_base_windows
  67.     @current_index = 1
  68.     @window_index = []
  69.     @size = 0
  70.     WINDOW_ORDER.each do |symbol|
  71.       create_resolution_window if symbol == :resolution
  72.       create_autosave_window   if symbol == :autosave
  73.       create_master_window     if symbol == :master
  74.       create_bgm_window        if symbol == :bgm
  75.       create_se_window         if symbol == :se
  76.       create_message_se_window if symbol == :message_se
  77.       create_window_switch     if symbol == :switch
  78.     end
  79.     create_command_window
  80.     @index = 0
  81.     @old_index = @index
  82.     activate_window(0)
  83.   end
  84.   def create_base_windows
  85.     @help_window = Window_Help.new(1)
  86.     @help_window.set_text("Options")
  87.     @back_window = Window_Options.new
  88.     @back_window.height = (WINDOW_ORDER.size + 1)* 48 + 24
  89.     @back_window.create_contents
  90.   end
  91.   def create_command_window
  92.     @window_command = Window_OCommand.new(48+24 * @current_index)
  93.     @window_command.set_handler(:ok, method(:command_ok))
  94.     @window_index.push(:command)
  95.     @back_window.draw_line(24 * (@current_index - 1))
  96.   end
  97.   def create_resolution_window
  98.     if Module.const_defined?(:Window_Resize)
  99.       @window_resolution = Window_OR.new(48+24 * @current_index)
  100.       @window_resolution.set_handler(:ok, method(:resolution_ok))
  101.       @back_window.add_text(24 * (@current_index - 1),OPT_STR::RESOLUTION)
  102.       @current_index += 2
  103.       @size += 1
  104.       @window_index.push(:resolution)
  105.     end
  106.   end
  107.   def create_autosave_window
  108.     if Module.const_defined?(:AUTOSAVE_FILE_NAME)
  109.       @window_autosave = Window_OAS.new(48+24 * @current_index)
  110.       @window_autosave.set_handler(:ok, method(:autosave_ok))
  111.       @back_window.add_text(24 * (@current_index - 1),OPT_STR::AUTOSAVE)
  112.       @current_index += 2
  113.       @size += 1
  114.       @window_index.push(:autosave)
  115.     end
  116.   end
  117.   def create_master_window
  118.     @window_masterbar = Window_OBar.new(48+24 * @current_index)
  119.     @back_window.add_text(24 * (@current_index - 1),OPT_STR::MASTER_VOLUME)
  120.     @current_index += 2
  121.     @size += 1
  122.     @window_masterbar.refresh($game_options.master_volume)
  123.     @window_index.push(:master)
  124.   end
  125.   def create_bgm_window
  126.     @window_bgmbar = Window_OBar.new(48+24 * @current_index)
  127.     @back_window.add_text(24 * (@current_index - 1),OPT_STR::BGM_VOLUME)
  128.     @current_index += 2
  129.     @size += 1
  130.     @window_bgmbar.refresh($game_options.bgm_volume)
  131.     @window_index.push(:bgm)
  132.   end
  133.   def create_se_window
  134.     @window_sebar = Window_OBar.new(48+24*@current_index)
  135.     @back_window.add_text(24 * (@current_index - 1),OPT_STR::SE_VOLUME)
  136.     @current_index += 2
  137.     @size += 1
  138.     @window_sebar.refresh($game_options.se_volume)
  139.     @window_index.push(:se)
  140.   end
  141.   def create_message_se_window
  142.     if Window_Message.const_defined?(:DEFAULT_SE_FREQ)
  143.       @window_message = Window_OMSE.new(48+24 * @current_index)
  144.       @window_message.set_handler(:ok, method(:message_ok))
  145.       @back_window.add_text(24 * (@current_index - 1),OPT_STR::MESSAGE_SE)
  146.       @current_index += 2
  147.       @size += 1
  148.       @window_index.push(:message)
  149.     end
  150.   end
  151.   def create_window_switch
  152.     @window_switch = Window_OSW.new(48+24 * @current_index)
  153.     @window_switch.set_handler(:ok, method(:switch_ok))
  154.     @back_window.add_text(24 * (@current_index - 1),OPT_STR::SWITCH)
  155.     @current_index += 2
  156.     @size += 1
  157.     @window_index.push(:switch)
  158.   end
  159.   def activate_window(index)
  160.     case @window_index[index]
  161.     when :resolution
  162.       @window_resolution.select(0)
  163.       @window_resolution.activate
  164.       @help_window.set_text(OPT_STR::RESOLUTION_HELP)
  165.     when :autosave
  166.       @window_autosave.select(0)
  167.       @window_autosave.activate
  168.       @help_window.set_text(OPT_STR::AUTOSAVE_HELP)
  169.     when :master
  170.       @window_masterbar.select(0)
  171.       @window_masterbar.activate
  172.       @help_window.set_text(OPT_STR::MASTER_VOLUME_HELP)
  173.     when :bgm
  174.       @window_bgmbar.select(0)
  175.       @window_bgmbar.activate
  176.       @help_window.set_text(OPT_STR::BGM_VOLUME_HELP)
  177.     when :se
  178.       @window_sebar.select(0)
  179.       @window_sebar.activate
  180.       @help_window.set_text(OPT_STR::SE_VOLUME_HELP)
  181.     when :message
  182.       @window_message.select(0)
  183.       @window_message.activate
  184.       @help_window.set_text(OPT_STR::MESSAGE_SE_HELP)
  185.     when :command
  186.       @window_command.select(0)
  187.       @window_command.activate
  188.       @help_window.set_text(OPT_STR::COMMAND_HELP)
  189.     when :switch
  190.       @window_switch.select(0)
  191.       @window_switch.activate
  192.       @help_window.set_text(OPT_STR::SWITCH_HELP)
  193.     end
  194.   end
  195.   def deactivate_window(index)
  196.     case @window_index[index]
  197.     when :resolution
  198.       @window_resolution.select(-1)
  199.       @window_resolution.deactivate
  200.     when :autosave
  201.       @window_autosave.select(-1)
  202.       @window_autosave.deactivate
  203.     when :master
  204.       @window_masterbar.select(-1)
  205.       @window_masterbar.deactivate
  206.     when :bgm
  207.       @window_bgmbar.select(-1)
  208.       @window_bgmbar.deactivate
  209.     when :se
  210.       @window_sebar.select(-1)
  211.       @window_sebar.deactivate
  212.     when :message
  213.       @window_message.select(-1)
  214.       @window_message.deactivate
  215.     when :command
  216.       @window_command.select(-1)
  217.       @window_command.deactivate
  218.     when :switch
  219.       @window_switch.select(-1)
  220.       @window_switch.deactivate
  221.     end
  222.   end
  223.   def update
  224.     super
  225.     update_input
  226.     update_index
  227.   end
  228.   def update_input
  229.     if Input.trigger?(:UP)
  230.       @index -= 1
  231.       @index = @size if @index < 0
  232.     elsif Input.trigger?(:DOWN)
  233.       @index += 1
  234.       @index = 0 if @index > @size
  235.     elsif Input.repeat?(:LEFT)
  236.       change_volume(@index, -0.01)
  237.     elsif Input.repeat?(:RIGHT)
  238.       change_volume(@index, 0.01)
  239.     elsif Input.trigger?(:B)
  240.       @index = @size
  241.     end
  242.   end
  243.   def update_index
  244.     return if @index == @old_index
  245.     deactivate_window(@old_index)
  246.     activate_window(@index)
  247.     Sound.play_cursor
  248.     @old_index = @index
  249.   end
  250.   def change_volume(index, value)
  251.     return unless audio_index(index)
  252.     $game_options.set_volume(:master, value) if @window_index[index] == :master
  253.     $game_options.set_volume(:bgm, value) if @window_index[index] == :bgm
  254.     $game_options.set_volume(:se, value) if @window_index[index] == :se
  255.     @window_masterbar.refresh($game_options.master_volume)
  256.     @window_bgmbar.refresh($game_options.bgm_volume)
  257.     @window_sebar.refresh($game_options.se_volume)
  258.     Sound.play_cursor
  259.     $game_map.autoplay if $game_map && $game_map.map_id > 0
  260.   end
  261.   def audio_index(index)
  262.     return true if @window_index[index] == :master
  263.     return true if @window_index[index] == :bgm
  264.     return true if @window_index[index] == :se
  265.     return false
  266.   end
  267.   def resolution_ok
  268.     case @window_resolution.index
  269.     when 0
  270.       $game_options.set_resolution(Graphics.width,Graphics.height)
  271.     when 1
  272.       $game_options.set_resolution(Graphics.width*2,Graphics.height*2)
  273.     when 2
  274.       $game_options.set_fullscreen
  275.     end
  276.     @window_resolution.refresh
  277.     @window_resolution.activate
  278.   end
  279.   def autosave_ok
  280.     $game_options.auto_save = true if @window_autosave.index == 0
  281.     $game_options.auto_save = false if @window_autosave.index == 1
  282.     @window_autosave.refresh
  283.     @window_autosave.activate
  284.   end
  285.   def message_ok
  286.     $game_options.message_se = true if @window_message.index == 0
  287.     $game_options.message_se = false if @window_message.index == 1
  288.     @window_message.refresh
  289.     @window_message.activate
  290.   end
  291.   def switch_ok
  292.     eval(OPT_STR::SWITCH_DETAIL + " = true") if @window_switch.index == 0
  293.     eval(OPT_STR::SWITCH_DETAIL + " = false") if @window_switch.index == 1
  294.     @window_switch.refresh
  295.     @window_switch.activate
  296.   end
  297.   def command_ok
  298.     case @window_command.index
  299.     when 0
  300.       $game_options.save_options
  301.       $game_options.clear_memorize
  302.       return_scene
  303.     when 1
  304.       $game_options.copy_memorize
  305.       $game_options.clear_memorize
  306.       return_scene
  307.     end
  308.   end
  309. end
  310.  
  311. class Window_Options < Window_Base
  312.   def initialize
  313.     super(0,48,544,416-48)
  314.     self.contents.font.color = Color.new(150,100,255,200)
  315.   end
  316.   def add_text(y,string)
  317.     draw_text(15,y,544-24,24,string)
  318.   end
  319.   def draw_line(y)
  320.     contents.fill_rect(15,y+12,544-24,1,Color.new(75,75,75))
  321.   end
  322. end
  323.  
  324. class Window_OR < Window_Selectable
  325.   TEXT = ["Normal","Large","Fullscreen"]
  326.   def initialize(y)
  327.     super(24,y,544-48,48)
  328.     refresh
  329.     self.opacity = 0
  330.   end
  331.   def item_width; self.contents.width / 4; end
  332.   def item_max; return 3; end
  333.   def col_max; return item_max; end
  334.   def row_max; return 1; end
  335.   def draw_item(index)
  336.     rect = item_rect(index)
  337.     change_color(normal_color, index == $game_options.resolution_index)
  338.     draw_text(rect, TEXT[index],1)
  339.   end
  340. end
  341.  
  342. class Window_OAS < Window_Selectable
  343.   def initialize(y)
  344.     super(24,y,544-48,48)
  345.     refresh
  346.     self.opacity = 0
  347.   end
  348.   def item_width; return 118; end
  349.   def item_max; return 2; end
  350.   def col_max; return item_max; end
  351.   def row_max; return 1; end
  352.   def draw_item(index)
  353.     rect = item_rect(index)
  354.     $game_options.auto_save ? id = 0 : id = 1
  355.     change_color(normal_color, id == index)
  356.     draw_text(rect, ["Yes","No"][index],1)
  357.   end
  358. end
  359.  
  360. class Window_OSW < Window_Selectable
  361.   def initialize(y)
  362.     super(24,y,544-48,48)
  363.     refresh
  364.     self.opacity = 0
  365.   end
  366.   def item_width; return 118; end
  367.   def item_max; return 2; end
  368.   def col_max; return item_max; end
  369.   def row_max; return 1; end
  370.   def draw_item(index)
  371.     rect = item_rect(index)
  372.     eval(OPT_STR::SWITCH_DETAIL) ? id = 0 : id = 1
  373.     change_color(normal_color, id == index)
  374.     draw_text(rect, [OPT_STR::SWITCH_COMMAND[0],OPT_STR::SWITCH_COMMAND[1]][index],1)
  375.   end
  376. end
  377.  
  378. class Window_OCommand < Window_Selectable
  379.   def initialize(y)
  380.     super(24+96,y,544-48,48)
  381.     refresh
  382.     self.opacity = 0
  383.   end
  384.   def item_width; return 118; end
  385.   def item_max; return 2; end
  386.   def col_max; return item_max; end
  387.   def row_max; return 1; end
  388.   def draw_item(index)
  389.     rect = item_rect(index)
  390.     draw_text(rect, ["Ok","Cancel"][index],1)
  391.   end
  392. end
  393.  
  394. class Window_OMSE < Window_OAS
  395.   def initialize(y)
  396.     super
  397.     self.y = y
  398.     refresh
  399.     self.opacity = 0
  400.   end
  401.   def draw_item(index)
  402.     rect = item_rect(index)
  403.     $game_options.message_se ? id = 0 : id = 1
  404.     change_color(normal_color, id == index)
  405.     draw_text(rect, ["Yes","No"][index],1)
  406.   end
  407. end
  408.  
  409. class Window_OBar < Window_Selectable
  410.   def initialize(y)
  411.     super(24,y,544-48,48)
  412.     self.opacity = 0
  413.   end
  414.   def item_width; return contents.width; end
  415.   def item_max; return 1; end
  416.   def col_max; return item_max; end
  417.   def row_max; return 1; end
  418.   def refresh(temp_value)
  419.     contents.clear
  420.     temp_value *= 100
  421.     temp_value.to_i.times do |i|
  422.       rect = Rect.new(4+i*4,6,2,8)
  423.       contents.fill_rect(rect, Color.new(75,100,255))
  424.     end
  425.     (100-temp_value).to_i.times do |i|
  426.       rect = Rect.new(396-i*4,6,2,8)
  427.       contents.fill_rect(rect, Color.new(0,0,0))
  428.     end
  429.     draw_text(0,0,contents.width,24,temp_value.to_i.to_s + "%",2)
  430.   end
  431.   def update_mouse
  432.   end
  433. end
  434.  
  435. class Game_Options
  436.   attr_accessor  :fullscreen
  437.   attr_accessor  :resolution
  438.   attr_accessor  :master_volume
  439.   attr_accessor  :bgm_volume
  440.   attr_accessor  :se_volume
  441.   attr_accessor  :auto_save
  442.   attr_accessor  :message_se
  443.   def default_options
  444.     @fullscreen = false
  445.     @resolution = [544,416]
  446.     @master_volume = 1.0
  447.     @bgm_volume = 1.0
  448.     @se_volume = 1.0
  449.     @auto_save = true
  450.     @message_se = true
  451.     save_options
  452.   end
  453.   def load_options
  454.     if FileTest.exist?("System/Options.dt")
  455.       File.open("System/Options.dt", "rb") do |file|
  456.         $game_options = Marshal.load(file)
  457.       end
  458.     else
  459.       default_options
  460.     end
  461.     reset_screen
  462.   end
  463.   def reset_screen
  464.     if Module.const_defined?(:Window_Resize)
  465.       if $game_options.fullscreen
  466.         Window_Resize.f
  467.       else
  468.         Window_Resize.r($game_options.resolution[0],$game_options.resolution[1])
  469.       end
  470.     end
  471.   end
  472.   def save_options
  473.     File.open("System/Options.dt", "wb") do |file|
  474.       Marshal.dump($game_options, file)
  475.     end
  476.   end
  477.   def memorize_options
  478.     @old_options = Marshal.load(Marshal.dump($game_options))
  479.   end
  480.   def copy_memorize
  481.     $game_options = Marshal.load(Marshal.dump(@old_options))
  482.     reset_screen
  483.   end
  484.   def clear_memorize
  485.     @old_options = nil
  486.   end
  487.   def resolution_index
  488.     return 2 if @fullscreen
  489.     return 1 if @resolution[0] == 544*2
  490.     return 0
  491.   end
  492.   def set_resolution(w,h)
  493.     @resolution = [w,h]
  494.     @fullscreen = false
  495.     Window_Resize.window
  496.     15.times {|i| Graphics.update }
  497.     Window_Resize.r(@resolution[0],@resolution[1])
  498.   end
  499.   def set_fullscreen
  500.     @fullscreen = true
  501.     Window_Resize.f
  502.   end
  503.   def set_volume(symbol, value)
  504.     case symbol
  505.     when :master
  506.       @master_volume += value
  507.       @master_volume = [[@master_volume, 1].min, 0].max
  508.     when :bgm
  509.       @bgm_volume += value
  510.       @bgm_volume = [[@bgm_volume, 1].min, 0].max
  511.     when :se
  512.       @se_volume += value
  513.       @se_volume = [[@se_volume, 1].min, 0].max
  514.     end
  515.   end
  516.   def preset_volume(symbol, value)
  517.     if symbol == :master
  518.       @master_volume = value
  519.       @master_volume = [[@master_volume, 1].min, 0].max
  520.     end
  521.     if symbol == :bgm
  522.       @bgm_volume = value
  523.       @bgm_volume = [[@bgm_volume, 1].min, 0].max
  524.     end
  525.     if symbol == :se
  526.       @se_volume = value
  527.       @se_volume = [[@se_volume, 1].min, 0].max
  528.     end
  529.   end
  530. end
  531.  
  532. class RPG::BGM < RPG::AudioFile
  533.   def play(pos = 0)
  534.     if @name.empty?
  535.       Audio.bgm_stop
  536.       @@last = RPG::BGM.new
  537.     else
  538.       if $game_options
  539.         volume = @volume * $game_options.master_volume
  540.         volume *= $game_options.bgm_volume
  541.       else
  542.         volume = @volume
  543.       end
  544.       Audio.bgm_play('Audio/BGM/' + @name, volume.to_i, @pitch, pos)
  545.       @@last = self.clone
  546.     end
  547.   end
  548. end
  549.  
  550. class RPG::BGS < RPG::AudioFile
  551.   def play(pos = 0)
  552.     if @name.empty?
  553.       Audio.bgs_stop
  554.       @@last = RPG::BGS.new
  555.     else
  556.       if $game_options
  557.         volume = @volume * $game_options.master_volume
  558.         volume *= $game_options.bgm_volume
  559.       else
  560.         volume = @volume
  561.       end
  562.       Audio.bgs_play('Audio/BGS/' + @name, volume.to_i, @pitch, pos)
  563.       @@last = self.clone
  564.     end
  565.   end
  566. end
  567.  
  568. class RPG::ME < RPG::AudioFile
  569.   def play
  570.     if @name.empty?
  571.       Audio.me_stop
  572.     else
  573.       if $game_options
  574.         volume = @volume * $game_options.master_volume
  575.         volume *= $game_options.se_volume
  576.       else
  577.         volume = @volume
  578.       end
  579.       Audio.me_play('Audio/ME/' + @name, volume.to_i, @pitch)
  580.     end
  581.   end
  582. end
  583.  
  584. class RPG::SE < RPG::AudioFile
  585.   def play
  586.     unless @name.empty?
  587.       if $game_options
  588.         volume = @volume * $game_options.master_volume
  589.         volume *= $game_options.se_volume
  590.       else
  591.         volume = @volume
  592.       end
  593.       Audio.se_play('Audio/SE/' + @name, volume.to_i, @pitch)
  594.     end
  595.   end
  596. end
  597.  
  598. module SceneManager
  599.   def self.run
  600.     DataManager.init
  601.     Audio.setup_midi if use_midi?
  602.     $game_options = Game_Options.new
  603.     $game_options.load_options
  604.     @scene = first_scene_class.new
  605.     @scene.main while @scene
  606.   end
  607. end
  608.  
  609. class Window_MenuCommand < Window_Command
  610.   alias options_aoc add_original_commands
  611.   def add_original_commands
  612.     options_aoc
  613.     add_command("Options", :options)
  614.   end
  615. end
  616.  
  617. class Scene_Menu < Scene_MenuBase
  618.   alias options_create_command_window create_command_window
  619.   def create_command_window
  620.     options_create_command_window
  621.     @command_window.set_handler(:options,   method(:command_options))
  622.   end
  623.   def command_options
  624.     SceneManager.call(Scene_Options)
  625.   end
  626. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement