Advertisement
LiTTleDRAgo

[RGSS] Drago Save Layout

Mar 14th, 2017
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 39.45 KB | None | 0 0
  1. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
  2. # [XP] Drago Save Layout
  3. # Version: 1.05
  4. # Author : LiTTleDRAgo
  5. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
  6. ($imported ||= {})[:drg_save_layout] = 1.05
  7. #==============================================================================
  8. # ** Drago_DataModule
  9. #------------------------------------------------------------------------------
  10. #  
  11. #==============================================================================
  12. core  = "This script needs Drago - Core Engine ver 1.58 or above"
  13. ($imported[:drg_core_engine] || 0) >= 1.58 || raise(core)
  14.  
  15. module Drago_DataModule
  16.   #--------------------------------------------------------------------------
  17.   # * Constant
  18.   #--------------------------------------------------------------------------
  19.   FOLDERSAVE = "Saves/"        # Folder which save data is saved.
  20.   SAVENAME   = "Save%d.rxdata" # Filename of the save data.
  21.   MAXSAVES   = 20              # Max save data that can be saved and load.
  22.   FASTCURSOR = false           # Toggle the button to fast cursor selection.
  23.   # Edit if want to use background, else the script will use moving image.
  24.   #   Example: "Graphics/Pictures/Background.png"
  25.   # Type "MAP" if you want to use map screenshot as background.
  26.   BACKGROUND = nil
  27.   #--------------------------------------------------------------------------
  28.   # * Public Instance Variables
  29.   #--------------------------------------------------------------------------
  30.   attr_sec_accessor :scene_update,     'Array.new'
  31.   #--------------------------------------------------------------------------
  32.   # * Main Processing
  33.   #--------------------------------------------------------------------------
  34.   def main
  35.     start
  36.     post_start
  37.     Graphics.transition
  38.     scene_update.update while $scene == self
  39.     Graphics.freeze
  40.     all_variable_dispose
  41.   end
  42.   #--------------------------------------------------------------------------
  43.   # * Start
  44.   #--------------------------------------------------------------------------
  45.   def start
  46.     create_background
  47.     create_plane
  48.     create_font_sprite
  49.     create_screenshot_sprite
  50.     create_window_save
  51.   end
  52.   #--------------------------------------------------------------------------
  53.   # * Create Background
  54.   #--------------------------------------------------------------------------
  55.   def create_background
  56.     preview_scene = $game_temp.instance_variable_get(:@preview_bitmap)
  57.     @viewport = Viewport.new(0,0,Graphics.width,Graphics.height)
  58.     @background_sprite = Sprite.new(@viewport)
  59.     @background_sprite.bitmap =
  60.       if preview_scene.is_a?(Bitmap) && preview_scene.not.disposed?
  61.         preview_scene
  62.       else
  63.         Graphics.snap_to_bitmap
  64.       end
  65.     @background_sprite.color.set(16, 16, 16, 128)
  66.   end
  67.   #--------------------------------------------------------------------------
  68.   # * Create Plane
  69.   #--------------------------------------------------------------------------
  70.   def create_plane
  71.     @plane_sprite = Plane.new(@viewport)
  72.     if BACKGROUND
  73.       bitmap = Bitmap.new(BACKGROUND) rescue LiTTleDRAgo.cache.empty_bitmap
  74.       @plane_sprite.bitmap  = bitmap.clone
  75.       @plane_sprite.opacity = 255
  76.     else
  77.       bitmap = LiTTleDRAgo.cache.transitions('015-Diamond01') rescue
  78.                LiTTleDRAgo.cache.empty_bitmap
  79.       @plane_sprite.bitmap = Bitmap.new(bitmap.width,bitmap.height*2)
  80.       @plane_sprite.bitmap.blt(0,0,bitmap,bitmap.rect)
  81.       bitmap.flip_vertical!
  82.       @plane_sprite.bitmap.blt(0,bitmap.height,bitmap,bitmap.rect)
  83.       @plane_sprite.opacity = not_scene_title? ? 50 : 255
  84.     end
  85.     bitmap.dispose
  86.   end  
  87.   #--------------------------------------------------------------------------
  88.   # * Create Font Sprite
  89.   #--------------------------------------------------------------------------
  90.   def create_font_sprite
  91.     font = ['Georgia','Arial']
  92.     font += [Font.default_name]
  93.     @font_sprite = Sprite.new
  94.     @font_sprite.z = @plane_sprite.z + 1
  95.     @font_sprite.bitmap = Bitmap.new(Graphics.width, Graphics.height)
  96.     @font_sprite.bitmap.font.name = font.flatten
  97.     @font_sprite.bitmap.font.size = 42
  98.     @font_sprite.bitmap.font.italic = true
  99.     @font_sprite.bitmap.draw_edging_text(0,32,Graphics.width,36,"SaveData",2)
  100.   end  
  101.   #--------------------------------------------------------------------------
  102.   # * Create Screenshot Sprite
  103.   #--------------------------------------------------------------------------
  104.   def create_screenshot_sprite
  105.     @current_bitmap = @background_sprite.bitmap.stretch(
  106.                           Graphics.width / 2, Graphics.height / 2)
  107.                          
  108.     font = ['Monotype Corsiva','Arial','Times New Roman']
  109.     font += [Font.default_name]
  110.     @screenshot_sprite = Sprite.new
  111.     @screenshot_sprite.bitmap = Bitmap.new(Graphics.width/2, Graphics.height/2)
  112.     @screenshot_sprite.bitmap.font.name   = font.flatten
  113.     @screenshot_sprite.bitmap.font.size   = 16
  114.     @screenshot_sprite.bitmap.font.italic = true
  115.     @screenshot_sprite.bitmap.full_fill(Color.black)
  116.     @screenshot_sprite.x = (Graphics.width - @screenshot_sprite.width) - 32
  117.     @screenshot_sprite.y = (Graphics.height - @screenshot_sprite.height) - 32
  118.     @screenshot_sprite.z = @font_sprite.z
  119.   end
  120.   #--------------------------------------------------------------------------
  121.   # * Create Window Save
  122.   #--------------------------------------------------------------------------
  123.   def create_window_save
  124.     @window_save = Window_CStream.new(create_command(MAXSAVES),20,0)
  125.     @window_save.set_handler(:ok,     method(:command_ok) )
  126.     @window_save.set_handler(:cancel, method(:command_cancel) )
  127.   end
  128.   #--------------------------------------------------------------------------
  129.   # * Post Start
  130.   #--------------------------------------------------------------------------
  131.   def post_start
  132.     rect = Rect.new(0,26, Graphics.width, 5)
  133.     scene_update.replace([Graphics,Input,self])
  134.     @font_sprite.bitmap.fill_rect(rect,Color.white)
  135.     Dir.make_dir(FOLDERSAVE) unless FileTest.directory?(FOLDERSAVE)
  136.     @notesavedata = NoteSaveData.new
  137.   end
  138.   #--------------------------------------------------------------------------
  139.   # * Create Command
  140.   #--------------------------------------------------------------------------
  141.   def create_command(size)
  142.     command = []
  143.     size.times do |i|
  144.       if (exist = FileTest.exist?(filename = make_filename(i)))
  145.         content = read_content(filename)
  146.         text = content[:game_system].instance_variable_get(:@notesavedata)
  147.       else
  148.         text = "---------------No Data---------------"
  149.       end
  150.       savedata = {}  
  151.       savedata[:name]      = "\\}File #{i+1}: #{text}"
  152.       savedata[:condition] = exist if self.is_a?(Scene_Load)
  153.       command.push(savedata)
  154.     end
  155.     command
  156.   end
  157.   #--------------------------------------------------------------------------
  158.   # * Make File Name
  159.   #     file_index : save file index (0-3)
  160.   #--------------------------------------------------------------------------
  161.   def make_filename(file_index)
  162.     name = FOLDERSAVE + '/' + sprintf(SAVENAME,file_index)
  163.     name = name.gsub("\\","/").gsub("//","/")
  164.     return name
  165.   end
  166.   #--------------------------------------------------------------------------
  167.   # * Make Preview Bitmap Name
  168.   #     file_index : save file index (0-3)
  169.   #--------------------------------------------------------------------------
  170.   def preview_bitmap_name(file_index)
  171.     #filename = LiTTleDRAgo::APPPATHDRAGO + make_filename(file_index)
  172.     filename = make_filename(file_index)
  173.     dirname = File.dirname(filename)
  174.     Dir.make_dir(dirname) unless FileTest.directory?(dirname)
  175.     name = dirname + '/' + File.basename(filename) + '.png'
  176.     name = name.gsub("\\","/").gsub("//","/").sub('.rxdata','')
  177.     return name
  178.   end
  179.   #--------------------------------------------------------------------------
  180.   # * Read Save Data
  181.   #     file : file object for reading (opened)
  182.   #--------------------------------------------------------------------------
  183.   def read_content(filename)
  184.     content = (c = {})
  185.     file = File.open(filename, "r")
  186.     begin
  187.       content[:time_stamp]         = file.mtime
  188.       content[:characters]         = Marshal.load(file)
  189.       content[:frame_count]        = Marshal.load(file)
  190.       content[:game_system]        = Marshal.load(file)
  191.       content[:game_switches]      = Marshal.load(file)
  192.       content[:game_variables]     = Marshal.load(file)
  193.       content[:game_self_switches] = Marshal.load(file)
  194.       content[:game_screen]        = Marshal.load(file)
  195.       content[:game_actors]        = Marshal.load(file)
  196.       content[:game_party]         = Marshal.load(file)
  197.       content[:game_troop]         = Marshal.load(file)
  198.       content[:game_map]           = Marshal.load(file)
  199.       content[:game_player]        = Marshal.load(file)
  200.  
  201.       c[:total_sec]   = c[:frame_count] / Graphics.frame_rate
  202.       c[:hour]        = c[:total_sec] / 60 / 60
  203.       c[:min]         = c[:total_sec] / 60 % 60
  204.       c[:sec]         = c[:total_sec] % 60
  205.       c[:time_string] = sprintf("%02d:%02d:%02d", c[:hour], c[:min], c[:sec])
  206.       c[:date_string] = c[:time_stamp].strftime("%Y/%m/%d %H:%M")
  207.       file.close
  208.     rescue
  209.       file.close
  210.       Graphics.transition
  211.       dprint("File #{File.basename(filename)} is corrupt!")
  212.       FileTest.exist?(filename) && File.delete(filename)
  213.       content[:game_system].instance_variable_set(:@notesavedata,"File Deleted")
  214.     end
  215.     return content
  216.   end
  217.   #--------------------------------------------------------------------------
  218.   # * Frame Update
  219.   #--------------------------------------------------------------------------
  220.   def update
  221.     update_window_save
  222.     update_screenshot_sprite
  223.     update_plane
  224.   end
  225.   #--------------------------------------------------------------------------
  226.   # * Update Plane
  227.   #--------------------------------------------------------------------------
  228.   def update_plane
  229.     unless BACKGROUND
  230.       @plane_sprite.ox += 1
  231.       @plane_sprite.oy += 1
  232.     end
  233.   end
  234.   #--------------------------------------------------------------------------
  235.   # * Update Window Save
  236.   #--------------------------------------------------------------------------
  237.   def update_window_save  
  238.     @window_save.update
  239.   end
  240.   #--------------------------------------------------------------------------
  241.   # * Update Screenshot Sprite
  242.   #--------------------------------------------------------------------------
  243.   def update_screenshot_sprite
  244.     return if @old_index == @window_save.index
  245.     @old_index  = @window_save.index
  246.     filename    = make_filename(@old_index)
  247.     if FileTest.exist?(filename)
  248.       content = read_content(filename)
  249.       system  = content[:game_system]
  250.       if FileTest.exist?(bitmap = preview_bitmap_name(@old_index))
  251.         temp = (s = @screenshot_sprite.bitmap) && Bitmap.new(bitmap)
  252.         rect = Rect.new(4,4,s.width-8,s.height-8)
  253.         s.stretch_blt(rect,temp,temp.rect)
  254.         temp.dispose
  255.       else
  256.         s = @screenshot_sprite.bitmap
  257.         fontsize = s.font.size
  258.         if (preview = system.instance_variable_get(:@preview_save))
  259.           s.font.size = ((s.full_fill(Color.black) || 0) && 32)
  260.           s.draw_edging_text(0,s.height/2-16,s.width,32,'Loading...',1)
  261.           Graphics.wait(5)
  262.           temp = BitmapDump.read_bitmap_data(*preview)
  263.           temp.export(preview_bitmap_name(@old_index))
  264.           rect = Rect.new(4,4,s.width-8,s.height-8)
  265.           s.stretch_blt(rect,temp,temp.rect)
  266.           temp.dispose
  267.         else
  268.           s.font.size = ((s.full_fill(Color.black) || 0) && 32)
  269.           s.draw_edging_text(0,s.height/2-16,s.width,32,'No Preview',1)
  270.         end
  271.         s.font.size = fontsize
  272.       end
  273.       time = "#{content[:time_string]} #{content[:date_string]}"
  274.       if system.respond_to?(:chapter_name)
  275.         text  = system.chapter_name
  276.         s.draw_edging_text(0-4,s.height-32,s.width,32,"#{text}",2)
  277.         s.draw_edging_text(0-4,s.height-64,s.width,32,"#{time}",2)
  278.       else
  279.         s.draw_edging_text(0-4,s.height-32,s.width,32,"#{time}",2)
  280.       end      
  281.     else
  282.       delete_savedata(@old_index)
  283.       @screenshot_sprite.bitmap.full_fill(Color.black)
  284.     end
  285.   end
  286.   #--------------------------------------------------------------------------
  287.   # * Not in Scene_Title?
  288.   #--------------------------------------------------------------------------
  289.   def not_scene_title?
  290.     $game_system.is_a?(Game_System) && $game_system.magic_number != 0
  291.   end
  292.   #--------------------------------------------------------------------------
  293.   # * Decision Processing
  294.   #--------------------------------------------------------------------------
  295.   def command_ok
  296.     command = confirm_saveload(0)
  297.     execute_choice(command)
  298.   end
  299.   #--------------------------------------------------------------------------
  300.   # * Execute Choice
  301.   #--------------------------------------------------------------------------
  302.   def execute_choice(command)
  303.     index = @window_save.index
  304.     filename = make_filename(index)
  305.     if [:save,:overwrite].include?(command.result[:symbol])
  306.       if command.result[:symbol] == :overwrite
  307.         proceed = confirm_saveload(2)
  308.         if proceed.result[:symbol] == :cancel
  309.           Sound.play_cancel
  310.           return @window_save.activate
  311.         end
  312.       end
  313.       @notesavedata.show_window
  314.      
  315.       @current_bitmap.export(preview_bitmap_name(index))
  316.       preview_data = BitmapDump.bitmap_data(@current_bitmap)
  317.       $game_system.instance_variable_set(:@preview_save,preview_data)
  318.       Audio.se_stop
  319.       on_decision(filename)
  320.     elsif command.result[:symbol] == :load
  321.       Audio.se_stop
  322.       on_decision(filename) rescue Sound.play_buzzer
  323.       @window_save.activate unless $scene.is_a?(Scene_Map)
  324.     elsif command.result[:symbol] == :delete
  325.       proceed = confirm_saveload(2)
  326.       if proceed.result[:symbol] == :proceed
  327.         delete_savedata((@old_index = index + 100) - 100)
  328.         @window_save.dispose
  329.         create_window_save
  330.       elsif proceed.result[:symbol] == :cancel
  331.         Sound.play_cancel
  332.         @window_save.activate
  333.       end
  334.     elsif command.result[:symbol] == :cancel
  335.       Sound.play_cancel
  336.       @window_save.activate
  337.     end
  338.   end
  339.   #--------------------------------------------------------------------------
  340.   # * Confirm SaveLoad
  341.   #--------------------------------------------------------------------------
  342.   def confirm_saveload(type=0)
  343.     index = @window_save.index
  344.     exist = FileTest.exist?(filename = make_filename(index))
  345.     if type == 0
  346.       choice = exist ? '\\}Overwrite' : '\\}Save Game'
  347.       delete = '\\}Delete Save'
  348.       cancel = '\\}Cancel Save'
  349.       symbol = exist ? :overwrite     : :save
  350.     elsif type == 1
  351.       choice = '\\}Load Game'
  352.       delete = '\\}Delete Save'
  353.       cancel = '\\}Cancel Load'
  354.       symbol = :load
  355.     else
  356.       choice = '\\}Proceed'
  357.       cancel = '\\}Cancel'
  358.       symbol = :proceed
  359.     end
  360.     command = [{:name => choice, :symbol => symbol}]
  361.     if exist && [0,1].include?(type)
  362.       command << {:name => delete, :symbol => :delete}
  363.     end
  364.     command << {:name => cancel, :symbol => :cancel}
  365.     command = Window_CStream.new(command,
  366.               Graphics.width/2-30, Graphics.height/2-30)
  367.     command.set_handler(:ok,     carrot {})
  368.     command.set_handler(:cancel, carrot { command.result[:symbol] = :cancel })
  369.     command.x_off = 0
  370.     command.x     = Graphics.width/2 - command.width / 2
  371.     command.y     = Graphics.height/2 - command.height * command.item_max
  372.     while command.active
  373.       [command,Graphics,Input].update  
  374.       @window_save.process_cursor_rect
  375.       update_plane
  376.     end
  377.     command.dispose
  378.     command    
  379.   end
  380.   #--------------------------------------------------------------------------
  381.   # * Delete SaveData
  382.   #--------------------------------------------------------------------------
  383.   def delete_savedata(index)
  384.     filename = make_filename(index)
  385.     preview  = preview_bitmap_name(index)
  386.     FileTest.exist?(filename) && File.delete(filename) rescue nil
  387.     FileTest.exist?(preview)  && File.delete(preview)  rescue nil
  388.   end
  389.   #--------------------------------------------------------------------------
  390.   # * Cancel Processing
  391.   #--------------------------------------------------------------------------
  392.   def command_cancel
  393.     on_cancel
  394.   end
  395. end
  396.  
  397. Scene_Save.send(:include, Drago_DataModule)
  398. Scene_Load.send(:include, Drago_DataModule)
  399. #==============================================================================
  400. # ** Scene_Load
  401. #------------------------------------------------------------------------------
  402. #  This class performs load screen processing.
  403. #==============================================================================
  404. class Scene_Load
  405.   #--------------------------------------------------------------------------
  406.   # * Create Font Sprite
  407.   #--------------------------------------------------------------------------
  408.   define_post_alias(:create_font_sprite) do
  409.     @font_sprite.bitmap.clear
  410.     @font_sprite.bitmap.draw_edging_text(0,32,Graphics.width,36,"LoadData",2)
  411.   end  
  412.   #--------------------------------------------------------------------------
  413.   # * Decision Processing
  414.   #--------------------------------------------------------------------------
  415.   def command_ok
  416.     command = confirm_saveload(1)
  417.     execute_choice(command)
  418.   end
  419.   #--------------------------------------------------------------------------
  420.   # * Cancel Processing
  421.   #--------------------------------------------------------------------------
  422.   def command_cancel
  423.     if not_scene_title?
  424.       $scene = Scene_Map.new
  425.     else
  426.       $scene = Scene_Title.new
  427.     end
  428.   end
  429. end
  430.  
  431. #==============================================================================
  432. # ** Scene_Title
  433. #------------------------------------------------------------------------------
  434. #  This class performs title screen processing.
  435. #==============================================================================
  436. class Scene_Title
  437.   #--------------------------------------------------------------------------
  438.   # * Frame Update
  439.   #--------------------------------------------------------------------------
  440.   define_pre_alias(:update) do
  441.     if @command_window.not.nil? && !@drg_save_checker
  442.       @drg_save_checker = true
  443.       @continue_enabled = false
  444.       load = Scene_Load.new
  445.       Drago_DataModule::MAXSAVES.times do |i|
  446.         @continue_enabled = true if FileTest.exist?(load.make_filename(i))
  447.       end
  448.       if @continue_enabled
  449.         @command_window.index = 1
  450.         @command_window.draw_item(1, @command_window.normal_color)
  451.       else
  452.         @command_window.index = 0
  453.         @command_window.disable_item(1)
  454.       end
  455.     end
  456.   end  
  457. end
  458. #==============================================================================
  459. # ** Scene_Map
  460. #------------------------------------------------------------------------------
  461. #  This class performs map screen processing.
  462. #==============================================================================
  463.  
  464. class Scene_Map
  465.   #--------------------------------------------------------------------------
  466.   # * Main Processing
  467.   #--------------------------------------------------------------------------
  468.   define_pre_alias(:main)  { dispose_preview_bitmap }
  469.   define_post_alias(:main) do
  470.     unless $scene.is_a?(Scene_Title)
  471.       dispose_preview_bitmap
  472.       $game_temp.instance_variable_set(:@preview_bitmap,
  473.       LiTTleDRAgo.snap_to_bitmap)
  474.     end
  475.   end
  476.   #--------------------------------------------------------------------------
  477.   # * Dispose Preview Bitmap
  478.   #--------------------------------------------------------------------------
  479.   def dispose_preview_bitmap
  480.     bitmap = $game_temp.instance_variable_get(:@preview_bitmap)
  481.     bitmap.dispose if bitmap.is_a?(Bitmap) && bitmap.not.disposed?
  482.   end
  483. end
  484.  
  485. #==============================================================================
  486. # ** NoteSaveData
  487. #------------------------------------------------------------------------------
  488. #
  489. #==============================================================================
  490.  
  491. class NoteSaveData
  492.   #--------------------------------------------------------------------------
  493.   # * Object Initialization
  494.   #--------------------------------------------------------------------------
  495.   def initialize
  496.     @main = LiTTleDRAgo.hwnd
  497.     @create_window  = Win32API.new('user32','CreateWindowEx','lpplllllllll','l')
  498.     @show_window    = Win32API.new('user32','ShowWindow',%w(l l),'l')
  499.     @destroy_window = Win32API.new('user32','DestroyWindow','p','l')
  500.     @metrics        = LiTTleDRAgo.systemmetrix
  501.     @window_text    = Win32API.new('user32','GetWindowText',%w(n p n ),'l')
  502.     @setwindowpos   = Win32API.new('user32','SetWindowPos', 'LLIIIII', 'I')
  503.     @window_rect    = Win32API.new('user32','GetWindowRect',%w(l p),'i')
  504.     @update_window  = Win32API.new('user32','UpdateWindow','p','i')
  505.     @input          = Win32API.new('user32','GetAsyncKeyState','i','i')
  506.   end
  507.   #--------------------------------------------------------------------------
  508.   # * Destroy Window
  509.   #--------------------------------------------------------------------------
  510.   def destroy_window
  511.     @destroy_window.call(@note_window)
  512.     @note_window = nil
  513.   end
  514.   #--------------------------------------------------------------------------
  515.   # * Show Window
  516.   #--------------------------------------------------------------------------
  517.   def show_window
  518.     return if @note_window != nil    
  519.    
  520.     @sprite_help = s = Sprite.new
  521.     s.bitmap = Bitmap.new((g = Graphics).width,g.height)
  522.     s.bitmap.font.name = ['Georgia',Font.default_name].flatten
  523.     s.opacity = (s.z += 9999) * 0
  524.     s.bitmap.draw_edging_text(0,0-32,g.width,g.height,"Enter notes here.",1)
  525.     (s.opacity += 10) && Graphics.update until s.opacity >= 255
  526.     calculate_window_pos
  527.     @note_window = @create_window.call(768, 'Edit', '',
  528.        0x86000000, @x, @y, Graphics.width / 2, 22, @main, 0, 0, 0)
  529.     @show_window.call(@note_window, 1)
  530.     start_script_update
  531.   end  
  532.   #--------------------------------------------------------------------------
  533.   # * Calculate Window Pos
  534.   #--------------------------------------------------------------------------
  535.   def calculate_window_pos    
  536.     @window_rect.call(@main, (rect = '0'*16))
  537.     left, top, right, bottom = rect.unpack("LLLL")
  538.     @x = left + (me5 = @metrics.call(5)) + (me45 = @metrics.call(45))
  539.     @y = top  + (me6 = @metrics.call(6)) + (me46 = @metrics.call(46))
  540.     @x = (@x + (Graphics.width / 4))
  541.     @y = (@y + (Graphics.height / 2))
  542.   end
  543.   #--------------------------------------------------------------------------
  544.   # * Start Script Update
  545.   #--------------------------------------------------------------------------
  546.   def start_script_update
  547.     @count = 0
  548.     until @input.call(0x0D) & 0x01 == 1 && @count > 10 do
  549.       Graphics.update
  550.       @update_window.call(@note_window)
  551.       @count += 1
  552.       if @count % 5 == 0
  553.         calculate_window_pos
  554.         @setwindowpos.call(@note_window, 0, @x, @y, Graphics.width / 2, 22,0)
  555.       end
  556.     end
  557.     @window_text.call(@note_window, (text = "\0" * 256), 0x3E80)
  558.     $game_system.instance_variable_set(:@notesavedata, text.gsub("\0", ""))
  559.     until @sprite_help.opacity <= 0
  560.       Graphics.update
  561.       @sprite_help.opacity -= 10
  562.     end
  563.     @sprite_help.bitmap.dispose
  564.     @sprite_help.dispose
  565.     destroy_window
  566.   end
  567. end
  568.  
  569. #==============================================================================
  570. # ** Window_CStream
  571. #------------------------------------------------------------------------------
  572. #  
  573. #==============================================================================
  574. class Window_CStream
  575.  
  576.   #--------------------------------------------------------------------------
  577.   # * Public Instance Variables
  578.   #--------------------------------------------------------------------------
  579.   attr_sec_reader :commands, :window_commands, 'Array.new'
  580.   attr_sec_reader :handler, 'Hash.new'
  581.   attr_sec_reader :index, :width, :height, 0
  582.   attr_sec_reader :disabled_item, 'Array.new'
  583.   attr_sec_accessor :scroll_index,   0
  584.   attr_sec_accessor :x, :y,  0
  585.   attr_sec_accessor :x_off,  30
  586.   attr_accessor :active, :visible
  587.   #--------------------------------------------------------------------------
  588.   # *
  589.   #--------------------------------------------------------------------------
  590.   define_method(:item_max) { commands.size   }
  591.   define_method(:result)   { commands[index] }  
  592.   #--------------------------------------------------------------------------
  593.   # * Object Initialization
  594.   #--------------------------------------------------------------------------
  595.   def initialize(commands, x = 0,y = 0)
  596.     @x, @y = x, y
  597.     change_commands(commands)
  598.     activate
  599.     show
  600.   end
  601.   #--------------------------------------------------------------------------
  602.   # * Add Command
  603.   #--------------------------------------------------------------------------
  604.   def add_command(command)
  605.     commands.push(command)
  606.     change_commands(commands)
  607.   end
  608.   #--------------------------------------------------------------------------
  609.   # * Change Existing Commands
  610.   #--------------------------------------------------------------------------
  611.   def change_commands(commands)
  612.     @commands = commands
  613.     refresh(true)
  614.   end
  615.   #--------------------------------------------------------------------------
  616.   # * Activate Window
  617.   #--------------------------------------------------------------------------
  618.   def activate
  619.     window_commands.activate
  620.     self.active = true
  621.     self
  622.   end
  623.   #--------------------------------------------------------------------------
  624.   # * Deactivate Window
  625.   #--------------------------------------------------------------------------
  626.   def deactivate
  627.     window_commands.deactivate
  628.     self.active = false
  629.     self
  630.   end
  631.   #--------------------------------------------------------------------------
  632.   # * Show Window
  633.   #--------------------------------------------------------------------------
  634.   def show
  635.     window_commands.show
  636.     self.visible = true
  637.     self
  638.   end
  639.   #--------------------------------------------------------------------------
  640.   # * Hide Window
  641.   #--------------------------------------------------------------------------
  642.   def hide
  643.     window_commands.hide
  644.     self.visible = false
  645.     self
  646.   end
  647.   #--------------------------------------------------------------------------
  648.   # * Refresh
  649.   #--------------------------------------------------------------------------
  650.   def refresh(create = false)
  651.     clear_window
  652.     @width  = 0
  653.     @height = 0
  654.     @drawed = []
  655.     create && (@index = 0)
  656.     commands.each_with_index do |c,i|
  657.       @window_commands[i] = s = Window_Help.new
  658.       s.draw_text_ex(4,0,c[:name])
  659.       @width = @width.clamp(s.text_size(c[:name]).width+32, Graphics.width)
  660.       @height = s.contents.height
  661.     end
  662.     window_commands.each_with_index do |s,i|
  663.       if create
  664.         s.x, s.y = @x, @y
  665.       else
  666.         process_position_x(s,i,true)
  667.         process_position_y(s,i,true)
  668.       end
  669.       s.width = @width
  670.       s.contents.dispose
  671.       s.contents = Bitmap.new(s.width - 32, @height)
  672.       s.opacity = i == 0 ? 255 : 0
  673.       if i == 0
  674.         s.draw_text_ex(4,0,commands[i][:name])
  675.         s.z += 10
  676.       else
  677.         s.z = window_commands[i-1].z - 1
  678.       end
  679.      
  680.       condition = commands[i][:condition]
  681.       unless condition.nil?
  682.         condition_met = condition.is_a?(String) ? eval(condition) : condition
  683.         unless condition_met
  684.           disabled_item.push(i)
  685.         end
  686.       end
  687.     end
  688.   end
  689.   #--------------------------------------------------------------------------
  690.   # * Frame Dispose
  691.   #--------------------------------------------------------------------------
  692.   def dispose
  693.     clear_window
  694.   end  
  695.   #--------------------------------------------------------------------------
  696.   # * Clear All Window
  697.   #--------------------------------------------------------------------------
  698.   def clear_window
  699.     window_commands.dispose
  700.     window_commands.clear
  701.     disabled_item.clear
  702.   end
  703.   #--------------------------------------------------------------------------
  704.   # * Get Number of Rows Displayable on 1 Page
  705.   #--------------------------------------------------------------------------
  706.   def page_row_max
  707.     # Subtract a frame height of 32 from the window height, and divide it by
  708.     # 1 row height of 32
  709.     return 0 if item_max == 0
  710.     Graphics.height / window_commands[0].height
  711.   end
  712.   #--------------------------------------------------------------------------
  713.   # * Select Item
  714.   #--------------------------------------------------------------------------
  715.   def select(index)
  716.     return unless index
  717.     if (@index = index) < page_row_max
  718.       @scroll_index = 0
  719.     else
  720.       limit = [page_row_max/2,0].max
  721.       @scroll_index = [@scroll_index.clamp(index,index-limit),
  722.            item_max-page_row_max].min
  723.     end
  724.   end
  725.   #--------------------------------------------------------------------------
  726.   # * Move Cursor Down
  727.   #--------------------------------------------------------------------------
  728.   def cursor_down(wrap = false)
  729.     if index < (item_max - 1) || (wrap)
  730.       select((index + 1) % item_max)
  731.     end
  732.   end
  733.   #--------------------------------------------------------------------------
  734.   # * Move Cursor Up
  735.   #--------------------------------------------------------------------------
  736.   def cursor_up(wrap = false)
  737.     if index >= 1 || (wrap)
  738.       select((index - 1) % item_max)
  739.     end
  740.   end
  741.   #--------------------------------------------------------------------------
  742.   # * Scroll Cursor Down
  743.   #--------------------------------------------------------------------------
  744.   def scroll_down
  745.     @scroll_index = ((scroll_index + 1) % item_max)
  746.   end
  747.   #--------------------------------------------------------------------------
  748.   # * Scroll Cursor Up
  749.   #--------------------------------------------------------------------------
  750.   def scroll_up
  751.     @scroll_index = ((scroll_index - 1) % item_max)
  752.   end
  753.   #--------------------------------------------------------------------------
  754.   # * Frame Update
  755.   #--------------------------------------------------------------------------
  756.   def update
  757.     if mouse_in_position?
  758.       process_mouse_move
  759.       process_mouse_scroll
  760.       process_mouse_handling
  761.     else
  762.       process_cursor_move
  763.       process_cursor_rect
  764.       process_handling
  765.     end
  766.   end
  767.   #--------------------------------------------------------------------------
  768.   # * Cursor Movement Processing
  769.   #--------------------------------------------------------------------------
  770.   def process_cursor_move
  771.     return unless cursor_movable?
  772.     last_index = @index
  773.     if Drago_DataModule::FASTCURSOR
  774.       cursor_down( Input.trigger?(:DOWN))  if Input.repeat?(:DOWN)
  775.       cursor_up(   Input.trigger?(:UP  ))  if Input.repeat?(:UP  )
  776.     else
  777.       cursor_down( Input.trigger?(:DOWN))  if Input.trigger?(:DOWN)
  778.       cursor_up(   Input.trigger?(:UP  ))  if Input.trigger?(:UP  )
  779.     end
  780.     Sound.play_cursor if @index != last_index
  781.   end
  782.   #--------------------------------------------------------------------------
  783.   # * Mouse Movement Processing
  784.   #--------------------------------------------------------------------------
  785.   def process_mouse_move
  786.     return unless cursor_visible?
  787.     window_commands.each_with_index do |window,index|
  788.       process_position_mouse(window,index)
  789.     end
  790.   end
  791.   #--------------------------------------------------------------------------
  792.   # * Mouse Scroll Processing
  793.   #--------------------------------------------------------------------------
  794.   def process_mouse_scroll
  795.     cursor_down       if Mouse.scroll_down?
  796.     cursor_up         if Mouse.scroll_up?
  797.   end
  798.   #--------------------------------------------------------------------------
  799.   # * Cursor Position Processing
  800.   #--------------------------------------------------------------------------
  801.   def process_cursor_rect
  802.     return unless cursor_visible?
  803.     window_commands.each_with_index do |window,index|
  804.       process_position_x(window,index)
  805.       process_position_y(window,index)
  806.       process_opacity(window,index)
  807.     end
  808.   end
  809.   #--------------------------------------------------------------------------
  810.   # * Cursor Position X Processing
  811.   #--------------------------------------------------------------------------
  812.   def process_position_x(s,i,fixed = false)
  813.     pos = index == i
  814.     return s.x = @x + (pos ? x_off : 0) if fixed
  815.     s.x = pos ? [s.x + 1, @x + x_off].min : [s.x - 1, @x].max
  816.   end
  817.   #--------------------------------------------------------------------------
  818.   # * Cursor Position Y Processing
  819.   #--------------------------------------------------------------------------
  820.   def process_position_y(s,i,fixed = false)
  821.     pos = @y + (s.height * (i - scroll_index))
  822.     return s.y = pos if fixed
  823.     if s.y < pos
  824.       s.y = [s.y + 5, pos].min
  825.     elsif s.y > pos
  826.       s.y = [s.y - 5, pos].max
  827.     end
  828.   end
  829.   #--------------------------------------------------------------------------
  830.   # * Cursor Position X and Y (Mouse) Processing
  831.   #--------------------------------------------------------------------------
  832.   def process_position_mouse(s,i)
  833.     if Mouse.in_area?(@x,s.y,s.width+x_off,s.height)
  834.       s.x = [s.x + 1, @x + x_off].min
  835.       unless Mouse.scroll?
  836.         last_index = @index
  837.         @index = i  
  838.         Sound.play_cursor if last_index != index
  839.       end
  840.     else
  841.       s.x = [s.x - 1, @x].max
  842.     end
  843.     pos = @y + (s.height * (i - scroll_index))
  844.     if s.y < pos
  845.       s.y = [s.y + 5, pos].min
  846.     elsif s.y > pos
  847.       s.y = [s.y - 5, pos].max
  848.     end
  849.     process_opacity(s,i)
  850.   end
  851.   #--------------------------------------------------------------------------
  852.   # * Process Opacity
  853.   #--------------------------------------------------------------------------
  854.   def process_opacity(s,i)
  855.     return if s.y > Graphics.height
  856.     if i == 0 || s.y >= window_commands[i-1].y + (s.height / 4)
  857.       s.opacity = [s.opacity + 20, disabled_item.include?(i) ?
  858.         index == i ? 120 : 60 :
  859.         index == i ? 255 : 180].min
  860.       if s.opacity >= 100 && !(@drawed||=[]).include?(i)
  861.         s.draw_text_ex(4,0,commands[i][:name])
  862.         @drawed.push(i)
  863.       end
  864.     end
  865.   end
  866.   #--------------------------------------------------------------------------
  867.   # * Handling Processing for Mouse Clicking Etc.
  868.   #--------------------------------------------------------------------------
  869.   def process_mouse_handling
  870.     return unless active
  871.     return process_ok     if ok_enabled?     &&
  872.           (Mouse.trigger?(Mouse::PRIMARY)  or Input.trigger?(:C))
  873.     return process_cancel if cancel_enabled? &&
  874.           (Mouse.trigger?(Mouse::SECONDARY)or Input.trigger?(:B))
  875.   end
  876.   #--------------------------------------------------------------------------
  877.   # * Handling Processing for OK and Cancel Etc.
  878.   #--------------------------------------------------------------------------
  879.   def process_handling
  880.     return unless active
  881.     return process_ok     if ok_enabled?     && Input.trigger?(:C)
  882.     return process_cancel if cancel_enabled? && Input.trigger?(:B)
  883.   end
  884.   #--------------------------------------------------------------------------
  885.   # * Determine if Mouse is in range
  886.   #--------------------------------------------------------------------------
  887.   def mouse_in_position?
  888.     return false unless ($imported[:drg_custom_input] || 0) >= 2.00
  889.     return false unless cursor_movable?
  890.     x = [window_commands[0].x,0].max
  891.     y = [window_commands[0].y,0].max
  892.     w = [window_commands[0].width + x_off, Graphics.width].min
  893.     h = [window_commands[0].height * item_max, Graphics.height].min
  894.     return true if Mouse.in_area?(x,y,w,h)
  895.     return false
  896.   end
  897.   #--------------------------------------------------------------------------
  898.   # * Determine if Cursor is Moveable
  899.   #--------------------------------------------------------------------------
  900.   def cursor_movable?
  901.     return false unless item_max > 0
  902.     return false unless active
  903.     return true
  904.   end
  905.   #--------------------------------------------------------------------------
  906.   # * Determine if Cursor is Visible
  907.   #--------------------------------------------------------------------------
  908.   def cursor_visible?
  909.     return false unless item_max > 0
  910.     return false unless visible
  911.     return true
  912.   end
  913.   #--------------------------------------------------------------------------
  914.   # * Get Activation State of OK Processing
  915.   #--------------------------------------------------------------------------
  916.   def ok_enabled?
  917.     return false unless item_max > 0  
  918.     return false unless (handle?(:ok) || result[:handle])
  919.     return true
  920.   end
  921.   #--------------------------------------------------------------------------
  922.   # * Get Activation State of Cancel Processing
  923.   #--------------------------------------------------------------------------
  924.   def cancel_enabled?
  925.     return false unless handle?(:cancel)
  926.     return true
  927.   end
  928.   #--------------------------------------------------------------------------
  929.   # * Get Activation State of Selection Item
  930.   #--------------------------------------------------------------------------
  931.   def current_item_enabled?
  932.     return false unless index > -1 && result != nil
  933.     return false if disabled_item.include?(index)
  934.     return true
  935.   end
  936.   #--------------------------------------------------------------------------
  937.   # * Set Handler Corresponding to Operation
  938.   #     method : Method set as a handler (Method object)
  939.   #--------------------------------------------------------------------------
  940.   def set_handler(symbol, method)
  941.     handler[symbol] = method
  942.   end
  943.   #--------------------------------------------------------------------------
  944.   # * Check for Handler Existence
  945.   #--------------------------------------------------------------------------
  946.   def handle?(symbol)
  947.     handler.include?(symbol)
  948.   end
  949.   #--------------------------------------------------------------------------
  950.   # * Call Handler
  951.   #--------------------------------------------------------------------------
  952.   def call_handler(symbol)
  953.     handler[symbol].call if handle?(symbol)
  954.   end
  955.   #--------------------------------------------------------------------------
  956.   # * Processing When OK Button Is Pressed
  957.   #--------------------------------------------------------------------------
  958.   def process_ok
  959.     if current_item_enabled?
  960.       Sound.play_ok
  961.       Input.update
  962.       deactivate
  963.       call_ok_handler
  964.     else
  965.       Sound.play_buzzer
  966.     end
  967.   end
  968.   #--------------------------------------------------------------------------
  969.   # * Processing When Cancel Button Is Pressed
  970.   #--------------------------------------------------------------------------
  971.   def process_cancel
  972.     Sound.play_cancel
  973.     Input.update
  974.     deactivate
  975.     call_cancel_handler
  976.   end
  977.   #--------------------------------------------------------------------------
  978.   # * Call OK Handler
  979.   #--------------------------------------------------------------------------
  980.   def call_ok_handler
  981.     if result[:handle]
  982.       result[:handle].call
  983.     else
  984.       call_handler(:ok)
  985.     end
  986.   end
  987.   #--------------------------------------------------------------------------
  988.   # * Call Cancel Handler
  989.   #--------------------------------------------------------------------------
  990.   def call_cancel_handler
  991.     call_handler(:cancel)
  992.   end
  993. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement