TheoAllen

Theo - Database Manager

Nov 10th, 2013
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 12.11 KB | None | 0 0
  1. # =============================================================================
  2. # TheoAllen - Database Manager
  3. # Version : 1.0
  4. # Contact : www.rpgmakerid.com (or) http://theolized.blogspot.com
  5. # (This script documentation is written in informal indonesian language)
  6. # =============================================================================
  7. ($imported ||= {})[:Theo_DBManager] = true
  8. # =============================================================================
  9. # Change Logs:
  10. # -----------------------------------------------------------------------------
  11. # 2013.11.11 - Finished Script
  12. # =============================================================================
  13. =begin
  14.   ----------------------------------------------------------------------------
  15.   Perkenalan :
  16.   Merasa kurang bebas ngedit database dari editor yang hanya disedian copy,
  17.   paste, clear, dan multicopy?
  18.  
  19.   Sekarang, dengan script ini, kamu bisa menukar ID dari sebuah database,
  20.   atau bahkan menyisipkan database kosong dan menghapus database untuk alasan
  21.   kerapian. Coba aja
  22.  
  23.   ----------------------------------------------------------------------------
  24.   Cara penggunaan :
  25.   Pasang script ini dibawah material namun diatas main
  26.   Set Activate ke true jika kamu ingin mengaktifasi script ini. Lalu playtest.
  27.  
  28.   Setelah kamu selesai mengedit database dari game, tutup gamenya beserta
  29.   editornya. Lalu buka kembali editor RMVXA kamu. Dan lihat database yang
  30.   barusan kamu ubah
  31.  
  32.   ----------------------------------------------------------------------------
  33.   Terms of use :
  34.   Script ini diperuntukkan cuman buat alat bantu ngedit database. Kalo kamu
  35.   bisa dan berani mengedit script ini, wa bolehin. Kalo kamu share script
  36.   versi editan dari ini, jangan lupa, tetep kredit gw, TheoAllen
  37.  
  38. =end
  39. # =============================================================================
  40. Font.default_name = ["Calibri"]       # <-- Default font
  41. class Window_DB < Window_Selectable   # <-- Yang ini jangan diubah-ubah
  42. # =============================================================================
  43.   # --------------------------------------------------------------------------
  44.   # Activation flag. Set ke true jika kamu mau gunain script ini
  45.   # --------------------------------------------------------------------------
  46.     Activate = true
  47.   # --------------------------------------------------------------------------
  48.   # Database Object. Tulis antara pilihan berikut :
  49.   # --------------------------------------------------------------------------
  50.   # $data_actors          >> Untuk database Actor  
  51.   # $data_classes         >> Untuk database Class
  52.   # $data_skills          >> Untuk database Skill
  53.   # $data_items           >> Untuk database Item
  54.   # $data_weapons         >> Untuk database Weapon
  55.   # $data_armors          >> Untuk database Armor
  56.   # $data_enemies         >> Untuk database Enemy
  57.   # $data_troops          >> Untuk database Troop
  58.   # $data_states          >> Untuk database State
  59.   # $data_animations      >> Untuk database Animasi
  60.   # $data_tilesets        >> Untuk database Tileset
  61.   # $data_common_events   >> Untuk database common event
  62.   #
  63.   # Letakkan antara "def" dan "end"
  64.   # --------------------------------------------------------------------------
  65.   def database
  66.     $data_skills  # <-- disini
  67.   end
  68. # =============================================================================
  69. # Akhir dari konfigurasi. Setelah ini jangan sentuh apapun, atawa database
  70. # game lu bakal rusak :v
  71. # =============================================================================
  72.   attr_reader :pending_index
  73.   def initialize(*args)
  74.     super(*args)
  75.     @load_index = 0
  76.     reset_pending_index
  77.     init_handlers
  78.     refresh
  79.     activate
  80.     select(0)
  81.   end
  82.  
  83.   def window_progress=(window)
  84.     @progress = window
  85.   end
  86.  
  87.   def reset_pending_index
  88.     @pending_index = -1
  89.   end
  90.  
  91.   def init_handlers
  92.     set_handler(:ok, method(:on_okay))
  93.     set_handler(:cancel, method(:on_cancel))
  94.   end
  95.  
  96.   def on_okay
  97.     if pending?
  98.       change_id
  99.       clear_pending
  100.     elsif index == pending_index
  101.       clear_pending
  102.     else
  103.       @pending_index = index
  104.       draw_pending(@pending_index)
  105.     end
  106.     activate
  107.   end
  108.  
  109.   def on_cancel
  110.     if pending?
  111.       clear_pending
  112.     else
  113.       SceneManager.exit
  114.     end
  115.     activate
  116.   end
  117.  
  118.   def pending?
  119.     @pending_index > -1
  120.   end
  121.  
  122.   def item
  123.     @data[index]
  124.   end
  125.  
  126.   def pending_item
  127.     @data[@pending_index]
  128.   end
  129.  
  130.   def change_id
  131.     id1 = item.id
  132.     id2 = pending_item.id
  133.     temp_item = item
  134.     database[id1] = pending_item
  135.     database[id2] = temp_item
  136.     database[id1].id = id1
  137.     database[id2].id = id2
  138.     make_item_list
  139.     redraw_item(id1)
  140.     redraw_item(index)
  141.   end
  142.  
  143.   def draw_pending(index)
  144.     clear_item(index) if index >= 0
  145.     contents.fill_rect(item_rect(index), Color.new(255,255,255,128))
  146.     draw_item(index)  if index >= 0
  147.   end
  148.  
  149.   def clear_pending
  150.     index = @pending_index
  151.     redraw_item(index)
  152.     reset_pending_index
  153.   end
  154.  
  155.   def item_max
  156.     unless @data
  157.       make_item_list
  158.     end
  159.     @data.size
  160.   end
  161.  
  162.   def make_item_list
  163.     @data = database.compact
  164.   end
  165.  
  166.   def draw_item(index)
  167.     item = @data[index]
  168.     if item
  169.       rect = item_rect(index)
  170.       rect.width -= 4
  171.       draw_item_name(item, rect.x, rect.y, true)
  172.     end
  173.   end
  174.  
  175.   def draw_item_name(item, x, y, enabled = true, width = contents.width)
  176.     return unless item
  177.     if item.respond_to?("icon_index") && !item.icon_index.nil?
  178.       draw_icon(item.icon_index, x, y, enabled)
  179.     else
  180.       draw_icon(0, x, y, enabled)
  181.     end
  182.     change_color(normal_color, enabled)
  183.     contents.font.size = line_height
  184.     draw_text(x+24, y, width, line_height, sprintf("%03d:%s",item.id,item.name))
  185.   end
  186.  
  187.   def update_help
  188.     @help_window.set_item(item)
  189.   end
  190.  
  191.   def refresh
  192.     make_item_list
  193.     create_contents
  194.     draw_all_items
  195.   end
  196.  
  197.   def draw_all_items
  198.     @load_index = 0
  199.     @fiber = Fiber.new { fiber_load }
  200.   end
  201.  
  202.   def insert_new_db(index)
  203.     Sound.play_ok
  204.     index += 1
  205.     database.insert(index, db_class.new)
  206.     database[index].id = database.index(database[index])
  207.     refresh_id(index+1)
  208.     refresh_at(index-1)
  209.   end
  210.  
  211.   def delete_db(index)
  212.     Sound.play_cancel
  213.     index += 1
  214.     database.delete_at(index)
  215.     refresh_id(index)
  216.     refresh_at(index-1)
  217.   end
  218.  
  219.   def refresh_at(index)
  220.     make_item_list
  221.     @load_index = index
  222.     @fiber = Fiber.new { fiber_load }
  223.   end
  224.  
  225.   def refresh_id(start_id)
  226.     for id in (start_id)..database.size-1
  227.       database[id].id = id
  228.     end
  229.   end
  230.  
  231.   def save_database
  232.     save_data(database, db_name[db_class])
  233.   end
  234.  
  235.   def db_name
  236.     hash = {
  237.       RPG::Actor => "Data/Actors.rvdata2",
  238.       RPG::Class => "Data/Classes.rvdata2",
  239.       RPG::Skill => "Data/Skills.rvdata2",
  240.       RPG::Item => "Data/Items.rvdata2",
  241.       RPG::Weapon => "Data/Weapons.rvdata2",
  242.       RPG::Armor => "Data/Armors.rvdata2",
  243.       RPG::Enemy => "Data/Enemies.rvdata2",
  244.       RPG::Troop => "Data/Troops.rvdata2",
  245.       RPG::State => "Data/States.rvdata2",
  246.       RPG::Animation => "Data/Animations.rvdata2",
  247.       RPG::Tileset => "Data/Tilesets.rvdata2",
  248.       RPG::CommonEvent => "Data/CommonEvents.rvdata2",
  249.     }
  250.     return hash
  251.   end
  252.  
  253.   def db_class
  254.     database[1].class
  255.   end
  256.  
  257.   def process_handling
  258.     super
  259.     return unless open? && active
  260.     return insert_new_db(index) if Input.trigger?(:SHIFT)
  261.     return delete_db(index) if Input.trigger?(:CTRL)
  262.     if Input.trigger?(:ALT)
  263.       Sound.play_load
  264.       return refresh
  265.     end
  266.   end
  267.  
  268.   def update
  269.     super
  270.     if @fiber
  271.       @fiber.resume
  272.     end
  273.   end
  274.  
  275.   def fiber_load
  276.     refresh_count = 5
  277.     for index in @load_index..item_max
  278.       redraw_item(index)
  279.       refresh_count -= 1
  280.       # --------------------------------------------
  281.       # To avoid lag
  282.       # --------------------------------------------
  283.       if @progress && refresh_count == 0
  284.         @progress.set(index+1, item_max-1)
  285.         refresh_count = 5
  286.       end
  287.       Fiber.yield
  288.     end
  289.     @progress.set(1,1)
  290.     Graphics.frame_reset
  291.     @fiber = nil
  292.   end
  293.  
  294. end
  295.  
  296. class Manual < Window_Base
  297.  
  298.   def initialize(*args)
  299.     super(*args)
  300.     @line = 0
  301.     refresh
  302.   end
  303.  
  304.   def text(command,text)
  305.     change_color(system_color)
  306.     draw_text(0,@line,contents.width,line_height,command)
  307.     change_color(normal_color)
  308.     xpos = text_size(command).width
  309.     draw_text(xpos,@line,contents.width,line_height,text)
  310.   end
  311.  
  312.   def just_text(text,align = 1)
  313.     draw_text(0,@line,contents.width,line_height,text,align)
  314.   end
  315.  
  316.   def refresh
  317.     @line = 0
  318.     contents.clear
  319.     contents.font.size = line_height
  320.     just_text "------------------------------------------"
  321.     line_plus
  322.     just_text "Movement Controls"
  323.     line_plus
  324.     just_text "------------------------------------------"
  325.     line_plus
  326.     text "Up              :"," Scroll up"
  327.     line_plus
  328.     text "Down         :"," Scroll down"
  329.     line_plus
  330.     text "PageUP      :"," Prev page"
  331.     line_plus
  332.     text "PageDown :"," Next Page"
  333.     line_plus
  334.     just_text "------------------------------------------"
  335.     line_plus
  336.     just_text "Editor Controls"
  337.     line_plus
  338.     just_text "------------------------------------------"
  339.     line_plus
  340.     text "Z       :"," Swap database"
  341.     line_plus
  342.     text "X       :"," Cancel / Exit"
  343.     line_plus
  344.     text "Shift  :"," Insert empty database"
  345.     line_plus
  346.     text "CTRL :"," Delete database"
  347.     line_plus
  348.     text "ALT   :"," Refresh database list"
  349.     line_plus
  350.     text "S      :"," Save Database"
  351.   end
  352.  
  353.   def line_plus
  354.     @line += line_height
  355.   end
  356.  
  357. end
  358.  
  359. class Window_DBLoading < Window_Base
  360.  
  361.   def initialize(*args)
  362.     super(*args)
  363.   end
  364.  
  365.   def set(current, max)
  366.     contents.clear
  367.     rate = ((current/max.to_f) * 100).to_i
  368.     rect = contents.rect
  369.     txt = sprintf("%d%",rate)
  370.     txt2 = (rate < 100 ? "Loading . . ." : "Done")
  371.     draw_text(rect, txt, 2)
  372.     draw_text(rect, txt2)
  373.   end
  374.  
  375. end
  376.  
  377. class DBHelp < Window_Help
  378.  
  379.   def set_item(item)
  380.     if item.respond_to?("description")
  381.       super(item)
  382.     end
  383.     contents.font.size = 20
  384.     text = "#{item.class}"
  385.     h = text_size(text).height
  386.     rect = Rect.new(0,contents.height-h,contents.width-6,h)
  387.     draw_text(rect,text,2)
  388.   end
  389.  
  390. end
  391.  
  392. class DBManager < Scene_Base
  393.  
  394.   def start
  395.     super
  396.     Graphics.resize_screen(680,480)
  397.     create_help
  398.     create_list
  399.     create_manual
  400.     create_loading_progress
  401.     create_popup
  402.   end
  403.  
  404.   def create_help
  405.     @help = DBHelp.new
  406.   end
  407.  
  408.   def create_list
  409.     wy = @help.height
  410.     ww = Graphics.width / 2
  411.     wh = Graphics.height - wy - 48
  412.     @list = Window_DB.new(0,wy,ww,wh)
  413.     @list.help_window = @help
  414.   end
  415.  
  416.   def create_popup
  417.     @popup = Window_Base.new(0,0,200,48)
  418.     @popup.x = (Graphics.width - @popup.width)/2
  419.     @popup.y = (Graphics.height - @popup.height)/2
  420.     @popup.draw_text(@popup.contents.rect, "Database Saved!",1)
  421.     @popup.openness = 0
  422.   end
  423.  
  424.   def create_manual
  425.     wx = @list.width
  426.     wy = @list.y
  427.     ww = @list.width
  428.     wh = @list.height + 48
  429.     @manual = Manual.new(wx,wy,ww,wh)
  430.   end
  431.  
  432.   def create_loading_progress
  433.     wx = 0
  434.     wy = @list.height + @help.height
  435.     ww = @list.width
  436.     wh = 48
  437.     @loading = Window_DBLoading.new(wx,wy,ww,wh)
  438.     @list.window_progress = @loading
  439.   end
  440.  
  441.   def update
  442.     super
  443.     if Input.trigger?(:Y)
  444.       Sound.play_save
  445.       @list.deactivate
  446.       @list.save_database
  447.       @popup.open
  448.       wait(180)
  449.       @popup.close
  450.       @list.activate
  451.     end
  452.   end
  453.  
  454.   def return_scene
  455.     SceneManager.exit
  456.   end
  457.  
  458.   def wait(duration)
  459.     duration.times do
  460.       update_basic
  461.       if Input.trigger?(:C)
  462.         Sound.play_ok
  463.         return
  464.       end
  465.     end
  466.   end
  467.  
  468. end
  469.  
  470. class << SceneManager
  471.  
  472.   alias theo_dbmanager_first_scene first_scene_class
  473.   def first_scene_class
  474.     Window_DB::Activate ? DBManager : theo_dbmanager_first_scene
  475.   end
  476.  
  477. end
Advertisement
Add Comment
Please, Sign In to add comment