TheoAllen

Theo - Database Manager (ENG)

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