Advertisement
roninator2

Map Item Menu

Nov 17th, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 27.09 KB | Gaming | 0 0
  1. # ╔═══════════════════════════════════════════════╦════════════════════╗
  2. # ║ Title: Map Item Menu                          ║  Version: 1.00     ║
  3. # ║ Author: Roninator2                            ║                    ║
  4. # ╠═══════════════════════════════════════════════╬════════════════════╣
  5. # ║ Function:                                     ║   Date Created     ║
  6. # ║                                               ╠════════════════════╣
  7. # ║ Call a item menu on the map                   ║    14 Jan 2024     ║
  8. # ╚═══════════════════════════════════════════════╩════════════════════╝
  9. # ╔════════════════════════════════════════════════════════════════════╗
  10. # ║ Requires: nil                                                      ║
  11. # ║                                                                    ║
  12. # ╚════════════════════════════════════════════════════════════════════╝
  13. # ╔════════════════════════════════════════════════════════════════════╗
  14. # ║ Brief Description:                                                 ║
  15. # ║       Show Item Menu on map without main menu                      ║
  16. # ╚════════════════════════════════════════════════════════════════════╝
  17. # ╔════════════════════════════════════════════════════════════════════╗
  18. # ║ Instructions:                                                      ║
  19. # ║                                                                    ║
  20. # ║  Change the values below to the desired configuration              ║
  21. # ║                                                                    ║
  22. # ║  Call_Item_Button -> Button pressed to call the menu               ║
  23. # ║                                                                    ║
  24. # ║  POSITION -> values are 0, 1, 2, 3                                 ║
  25. # ║              0 = Help window left side                             ║
  26. # ║              1 = Help window right side                            ║
  27. # ║              2 = Help window top                                   ║
  28. # ║              3 = Help window bottom                                ║
  29. # ║    All other windows move based on the Help window                 ║
  30. # ║                                                                    ║
  31. # ║  USE_NOTE -> allows using note tags in the item notebox            ║
  32. # ║    If using the note tags they are to be used like this            ║
  33. # ║       <item text>                                                  ║
  34. # ║        write all your text here                                    ║
  35. # ║        and more text here                                          ║
  36. # ║        also supports icon codes \i[112]                            ║
  37. # ║       </item text>                                                 ║
  38. # ║    If not using item note tags then it will write the              ║
  39. # ║    default item description                                        ║
  40. # ║                                                                    ║
  41. # ║  HELP_X and HELP_Y -> move the window away from the                ║
  42. # ║                       screen edge                                  ║
  43. # ║  HELP_LINES -> How many lines to have for the help window          ║
  44. # ║                                                                    ║
  45. # ║  STATUS_X and STATUS_Y ->  move the window away from the           ║
  46. # ║                            screen edge                             ║
  47. # ║  GRAPHICS_SPRITE -> use the walking sprite, or face                ║
  48. # ║  STATUS_ROWS -> How many lines to use for the status               ║
  49. # ║    window. Positoin 0 & 1 -> recommend 4                           ║
  50. # ║            Positoin 2 & 3 -> recommend 3                           ║
  51. # ║                                                                    ║
  52. # ║  COMMAND_X & COMMAND_Y -> move the window away from the            ║
  53. # ║                           screen edge                              ║
  54. # ║  COMMAND_COLUMNS -> How many commands to show in window            ║
  55. # ║   More commands in small window means they get squished            ║
  56. # ║                                                                    ║
  57. # ║  ITEM_X & ITEM_Y -> move the window away from the                  ║
  58. # ║                       screen edge                                  ║
  59. # ║  FULL_WIDTH -> Ignore the X value and go full width                ║
  60. # ║  ITEM_COLUMNS -> How many item columns to display                  ║
  61. # ║                                                                    ║
  62. # ╚════════════════════════════════════════════════════════════════════╝
  63. # ╔════════════════════════════════════════════════════════════════════╗
  64. # ║ Note:                                                              ║
  65. # ║   This script does not adjust for new item categories.             ║
  66. # ║   The script is based on the default system.                       ║
  67. # ║   If you add in new item categories then this script               ║
  68. # ║   will need to be adjusted accordingly.                            ║
  69. # ║   Default is :item, :weapon, :armor, :key_item                     ║
  70. # ╚════════════════════════════════════════════════════════════════════╝
  71. # ╔════════════════════════════════════════════════════════════════════╗
  72. # ║ Updates:                                                           ║
  73. # ║ 1.00 - 22 Nov 2024 - Script finished                               ║
  74. # ║                                                                    ║
  75. # ╚════════════════════════════════════════════════════════════════════╝
  76. # ╔════════════════════════════════════════════════════════════════════╗
  77. # ║ Credits and Thanks:                                                ║
  78. # ║   Roninator2                                                       ║
  79. # ║   DP3 & Tsukihime for their code                                   ║
  80. # ║                                                                    ║
  81. # ╚════════════════════════════════════════════════════════════════════╝
  82. # ╔════════════════════════════════════════════════════════════════════╗
  83. # ║ Terms of use:                                                      ║
  84. # ║  Follow the original Authors terms of use where applicable         ║
  85. # ║    - When not made by me (Roninator2)                              ║
  86. # ║  Free for all uses in RPG Maker except nudity                      ║
  87. # ║  Anyone using this script in their project before these terms      ║
  88. # ║  were changed are allowed to use this script even if it conflicts  ║
  89. # ║  with these new terms. New terms effective 03 Apr 2024             ║
  90. # ║  No part of this code can be used with AI programs or tools        ║
  91. # ║  Credit must be given                                              ║
  92. # ╚════════════════════════════════════════════════════════════════════╝
  93.  
  94. module R2_MAP_ITEM_MENU
  95.   Call_Item_Button = :Z
  96.     # :C = Z, :B = X, :A = SHIFT, :X = A, :Y = S, :Z = D, :L = Q, :R = W
  97.  
  98.   # ======================================================
  99.   POSITION = 3
  100.   # 0 = left, 1 = Right, 2 = top, 3 = bottom
  101.   # help window goes to the indicated position
  102.  
  103.   # ======================================================
  104.   # Help Window
  105.   USE_NOTE = true
  106.   # configure to use Note Box for Item Description or
  107.   # use description from the item itself.
  108.   # true = use note box tag
  109.   # false = use built in description box
  110.   HELP_X = 20
  111.   # position of where the Help window is positioned on X axis
  112.   # This will position the window away from the edge of the screen
  113.   HELP_Y = 20
  114.   # position of where the Help window is positioned on Y axis
  115.   # This will position the window away from the edge of the screen
  116.   HELP_LINES = 4
  117.   # The number of lines the help window will show. Sets the height
  118.   # position 0 & 1 -> recommend 15, position 2 & 3 -> recommend 4
  119.  
  120.   # ======================================================
  121.   # Command Window
  122.   COMMAND_X = 20
  123.   # position of where the ommand window is positioned on X axis
  124.   COMMAND_Y = 20
  125.   # position of where the ommand window is positioned on Y axis
  126.   COMMAND_COLUMNS = 4
  127.   # position 0 & 1 -> recommend 1 or 2, position 2 & 3 -> recommend < 5
  128.  
  129.   # ======================================================
  130.   # Item Window
  131.   ITEM_X = 20
  132.   # position of where the Skill window is positioned on X axis
  133.   ITEM_Y = 20
  134.   # position of where the Skill window is positioned on Y axis
  135.   FULL_WIDTH = false
  136.   # wether to span across the window width
  137.   ITEM_COLUMNS = 2
  138.   # number of columns in window
  139.   # position 0 & 1 -> recommend 1, position 2 & 3 -> recommend 2 or 3
  140. end
  141.  
  142. # ╔════════════════════════════════════════════════════════════════════╗
  143. # ║                      End of editable region                        ║
  144. # ╚════════════════════════════════════════════════════════════════════╝
  145.  
  146. #==============================================================================
  147. # ** Window_Help
  148. #==============================================================================
  149.  
  150. class Window_ItemHelp < Window_Base
  151.   #--------------------------------------------------------------------------
  152.   # * Object Initialization
  153.   #--------------------------------------------------------------------------
  154.   def initialize(line_number = R2_MAP_ITEM_MENU::HELP_LINES)
  155.     @position = R2_MAP_ITEM_MENU::POSITION
  156.     super(R2_MAP_ITEM_MENU::HELP_X, R2_MAP_ITEM_MENU::HELP_Y, width, fitting_height(line_number))
  157.     adjust_help_position(@position)
  158.     @text_desc = []
  159.   end
  160.   #--------------------------------------------------------------------------
  161.   # * Set Text
  162.   #--------------------------------------------------------------------------
  163.   def set_text(text)
  164.     @text_desc = []
  165.     if text != @text
  166.       @text = text
  167.       @text_desc.push("#{@text}")
  168.       refresh
  169.     end
  170.   end
  171.   #--------------------------------------------------------------------------
  172.   # * Clear
  173.   #--------------------------------------------------------------------------
  174.   def clear
  175.    contents.clear
  176.     @text_desc = []
  177.     set_text("")
  178.   end
  179.   #--------------------------------------------------------------------------
  180.   # * Set Item
  181.   #     item : Skills and items etc.
  182.   #--------------------------------------------------------------------------
  183.   def set_item(item)
  184.     @text_desc = []
  185.     if R2_MAP_ITEM_MENU::USE_NOTE == true
  186.       @text_desc = []
  187.       return if item.nil?
  188.       results = item.note.scan(/<item[-_ ]*text>(.*?)<\/item[-_ ]*text>/imx)
  189.       results.each do |res|
  190.         res[0].strip.split("\r\n").each do |line|
  191.         @text_desc.push("#{line}")
  192.         end
  193.       end
  194.     else
  195.       set_text(item ? item.description : "")
  196.     end
  197.     refresh
  198.   end
  199.   #--------------------------------------------------------------------------
  200.   # * Refresh
  201.   #--------------------------------------------------------------------------
  202.   def refresh
  203.     contents.clear
  204.     @y = 0
  205.     @text_desc.each do |l|
  206.       draw_text_ex(0, @y, word_wrapping(l))
  207.       @y += line_height
  208.     end
  209.   end
  210.   #--------------------------------------------------------------------------
  211.   # * Calculate Width of Window Contents
  212.   #--------------------------------------------------------------------------
  213.   def width
  214.     case @position
  215.     when 0..1
  216.       Graphics.width / 2 - R2_MAP_ITEM_MENU::HELP_X
  217.     when 2..3
  218.       Graphics.width - R2_MAP_ITEM_MENU::HELP_X * 2
  219.     end
  220.   end
  221.   #--------------------------------------------------------------------------
  222.   # * Move Help Window
  223.   #--------------------------------------------------------------------------
  224.   def adjust_help_position(position)
  225.     case position
  226.     when 0
  227.       self.x = R2_MAP_ITEM_MENU::HELP_X
  228.       self.y = R2_MAP_ITEM_MENU::HELP_Y
  229.     when 1
  230.       self.x = Graphics.width / 2
  231.       self.y = R2_MAP_ITEM_MENU::HELP_Y
  232.     when 2
  233.       self.x = R2_MAP_ITEM_MENU::HELP_X
  234.       self.y = R2_MAP_ITEM_MENU::HELP_Y
  235.     when 3
  236.       self.x = R2_MAP_ITEM_MENU::HELP_X
  237.       self.y = Graphics.height - self.height - R2_MAP_ITEM_MENU::HELP_Y / 2
  238.     end
  239.   end
  240.   #--------------------------------------------------------------------------
  241.   # * Word Wrapping
  242.   #--------------------------------------------------------------------------
  243.   def word_wrapping(text, pos = 0)
  244.     # Current Text Position
  245.     current_text_position = 0    
  246.     for i in 0..(text.length - 1)
  247.       if text[i] == "\n"
  248.         current_text_position = 0
  249.         next
  250.       end
  251.       # Current Position += character width
  252.       current_text_position += contents.text_size(text[i]).width
  253.       # If Current Position > Window Width
  254.       if (pos + current_text_position) >= (contents.width)
  255.         # Then Format the Sentence to fit Line
  256.         current_element = i
  257.         while(text[current_element] != " ")
  258.           break if current_element == 0
  259.           current_element -= 1
  260.         end
  261.         temp_text = ""
  262.         for j in 0..(text.length - 1)
  263.           temp_text += text[j]
  264.           temp_text += "\n" if j == current_element
  265.           @y += line_height if j == current_element
  266.         end
  267.         text = temp_text
  268.         i = current_element
  269.         current_text_position = 0
  270.       end
  271.     end
  272.     return text
  273.   end
  274. end
  275.  
  276. #==============================================================================
  277. # ** Window_MapSkillCommand
  278. #==============================================================================
  279.  
  280. class Window_MapItemCommand < Window_HorzCommand
  281.   #--------------------------------------------------------------------------
  282.   # * Public Instance Variables
  283.   #--------------------------------------------------------------------------
  284.   attr_reader   :item_window
  285.   #--------------------------------------------------------------------------
  286.   # * Object Initialization
  287.   #--------------------------------------------------------------------------
  288.   def initialize(x, y)
  289.     @position = R2_MAP_ITEM_MENU::POSITION
  290.     super(x, y)
  291.     adjust_command_position(@position)
  292.     @actor = nil
  293.   end
  294.   #--------------------------------------------------------------------------
  295.   # * Get Window Width
  296.   #--------------------------------------------------------------------------
  297.   def window_width
  298.     case @position
  299.     when 0..1
  300.       Graphics.width / 2 - R2_MAP_ITEM_MENU::COMMAND_X
  301.     when 2..3
  302.       Graphics.width - R2_MAP_ITEM_MENU::COMMAND_X * 2
  303.     end
  304.   end
  305.   #--------------------------------------------------------------------------
  306.   # * Move Command Window
  307.   #--------------------------------------------------------------------------
  308.   def adjust_command_position(position)
  309.     case position
  310.     when 0
  311.       self.x = Graphics.width / 2
  312.       self.y = R2_MAP_ITEM_MENU::COMMAND_Y
  313.     when 1
  314.       self.x = R2_MAP_ITEM_MENU::COMMAND_X
  315.       self.y = R2_MAP_ITEM_MENU::COMMAND_Y
  316.     when 2
  317.       self.x = R2_MAP_ITEM_MENU::COMMAND_X
  318.       self.y = Graphics.height - R2_MAP_ITEM_MENU::COMMAND_Y - self.height
  319.     when 3
  320.       self.x = R2_MAP_ITEM_MENU::COMMAND_X
  321.       self.y = R2_MAP_ITEM_MENU::COMMAND_Y
  322.     end
  323.   end
  324.   #--------------------------------------------------------------------------
  325.   # * Get Digit Count
  326.   #--------------------------------------------------------------------------
  327.   def col_max
  328.     return R2_MAP_ITEM_MENU::COMMAND_COLUMNS
  329.   end
  330.   #--------------------------------------------------------------------------
  331.   # * Create Command List
  332.   #--------------------------------------------------------------------------
  333.   def make_command_list
  334.     add_command(Vocab::item,     :item)
  335.     add_command(Vocab::weapon,   :weapon)
  336.     add_command(Vocab::armor,    :armor)
  337.     add_command(Vocab::key_item, :key_item)
  338.   end
  339.   #--------------------------------------------------------------------------
  340.   # * Frame Update
  341.   #--------------------------------------------------------------------------
  342.   def update
  343.     super
  344.     @item_window.category = current_symbol if @item_window
  345.   end
  346.   #--------------------------------------------------------------------------
  347.   # * Set Skill Window
  348.   #--------------------------------------------------------------------------
  349.   def item_window=(item_window)
  350.     @item_window = item_window
  351.     update
  352.   end
  353.   #--------------------------------------------------------------------------
  354.   # * Set Leading Digits
  355.   #--------------------------------------------------------------------------
  356.   def top_col=(col)
  357.     col = 0 if col < 0
  358.     col = item_max if col > item_max
  359.     self.ox = col * (item_width + spacing)
  360.   end
  361.   #--------------------------------------------------------------------------
  362.   # * Move Cursor Right # col_max >= 2 &&
  363.   #--------------------------------------------------------------------------
  364.   def cursor_right(wrap = false)
  365.     if (index < item_max - 1 || (wrap && horizontal?))
  366.       select((index + 1) % item_max)
  367.     end
  368.   end
  369.   #--------------------------------------------------------------------------
  370.   # * Move Cursor Left
  371.   #--------------------------------------------------------------------------
  372.   def cursor_left(wrap = false)
  373.     if (index > 0 || (wrap && horizontal?))
  374.       select((index - 1 + item_max) % item_max)
  375.     end
  376.   end
  377. end # Window_MapSkillCommand
  378.  
  379. #==============================================================================
  380. # ** Window_MapSkillList
  381. #==============================================================================
  382.  
  383. class Window_MapItemList < Window_ItemList
  384.   #--------------------------------------------------------------------------
  385.   # * Object Initialization
  386.   #--------------------------------------------------------------------------
  387.   def initialize(x, y, width, height)
  388.     super
  389.     @category = :none
  390.     @data = []
  391.   end
  392.   #--------------------------------------------------------------------------
  393.   # * Get Digit Count
  394.   #--------------------------------------------------------------------------
  395.   def col_max
  396.     return R2_MAP_ITEM_MENU::ITEM_COLUMNS
  397.   end
  398.   #--------------------------------------------------------------------------
  399.   # * Draw Item
  400.   #--------------------------------------------------------------------------
  401.   def draw_item(index)
  402.     skill = @data[index]
  403.     if skill
  404.       rect = item_rect(index)
  405.       rect.width -= 4
  406.       draw_item_name(skill, rect.x, rect.y, enable?(skill))
  407.     end
  408.   end
  409.   #--------------------------------------------------------------------------
  410.   # * Update Help Text
  411.   #--------------------------------------------------------------------------
  412.   def update_help
  413.     @help_window.set_item(item)
  414.   end
  415.   #--------------------------------------------------------------------------
  416.   # * Refresh
  417.   #--------------------------------------------------------------------------
  418.   def refresh
  419.     make_item_list
  420.     create_contents
  421.     draw_all_items
  422.   end
  423. end # Window_MapItemList
  424.  
  425. #==============================================================================
  426. # ** Scene_Map
  427. #==============================================================================
  428.  
  429. class Scene_Map < Scene_Base
  430.   #--------------------------------------------------------------------------
  431.   # * Update Scene Transition
  432.   #--------------------------------------------------------------------------
  433.   alias r2_update_call_item_map_scene  update_scene
  434.   def update_scene
  435.     r2_update_call_item_map_scene
  436.     update_call_item_menu unless scene_changing?
  437.   end
  438.   #--------------------------------------------------------------------------
  439.   # * Determine if Menu is Called due to Cancel Button
  440.   #--------------------------------------------------------------------------
  441.   def update_call_item_menu
  442.     if !$game_system.menu_disabled || !$game_map.interpreter.running?
  443.       if Input.trigger?(R2_MAP_ITEM_MENU::Call_Item_Button) && !$game_player.moving?
  444.         call_item_menu
  445.       end
  446.     end
  447.   end
  448.   #--------------------------------------------------------------------------
  449.   # * Call Menu Screen
  450.   #--------------------------------------------------------------------------
  451.   def call_item_menu
  452.     Sound.play_ok
  453.     SceneManager.call(Scene_Item_Menu)
  454.   end
  455. end
  456.  
  457. #==============================================================================
  458. # ** Scene_Skill
  459. #==============================================================================
  460.  
  461. class Scene_Item_Menu < Scene_ItemBase
  462.   #--------------------------------------------------------------------------
  463.   # * Start Processing
  464.   #--------------------------------------------------------------------------
  465.   def start
  466.     super
  467.     create_help_window
  468.     create_command_window
  469.     create_item_window
  470.   end
  471.   #--------------------------------------------------------------------------
  472.   # * Create Help Window
  473.   #--------------------------------------------------------------------------
  474.   def create_help_window
  475.     @help_window = Window_ItemHelp.new
  476.     @help_window.viewport = @viewport
  477.   end
  478.   #--------------------------------------------------------------------------
  479.   # * Create Command Window
  480.   #--------------------------------------------------------------------------
  481.   def create_command_window
  482.     @command_window = Window_MapItemCommand.new(0, 0)
  483.     @command_window.viewport = @viewport
  484.     @command_window.help_window = @help_window
  485.     @command_window.set_handler(:ok,     method(:on_category_ok))
  486.     @command_window.set_handler(:cancel,   method(:return_scene))
  487.   end
  488.   #--------------------------------------------------------------------------
  489.   # * Create Item Window
  490.   #--------------------------------------------------------------------------
  491.   def create_item_window
  492.     wx = 0
  493.     ww = Graphics.width
  494.     case R2_MAP_ITEM_MENU::POSITION
  495.     when 0
  496.       wx = Graphics.width / 2
  497.       wy = @command_window.y + @command_window.height
  498.       ww = Graphics.width / 2 - R2_MAP_ITEM_MENU::ITEM_X unless R2_MAP_ITEM_MENU::FULL_WIDTH
  499.       wh = Graphics.height - wy - (R2_MAP_ITEM_MENU::ITEM_Y - 5)
  500.     when 1
  501.       wx = R2_MAP_ITEM_MENU::ITEM_X
  502.       wy = @command_window.y + @command_window.height
  503.       ww = Graphics.width / 2 - R2_MAP_ITEM_MENU::ITEM_X unless R2_MAP_ITEM_MENU::FULL_WIDTH
  504.       wh = Graphics.height - wy - (R2_MAP_ITEM_MENU::ITEM_Y - 5)
  505.     when 2
  506.       wx = R2_MAP_ITEM_MENU::ITEM_X unless R2_MAP_ITEM_MENU::FULL_WIDTH
  507.       wy = @help_window.y + @help_window.height
  508.       ww = Graphics.width - R2_MAP_ITEM_MENU::ITEM_X * 2 unless R2_MAP_ITEM_MENU::FULL_WIDTH
  509.       wh = Graphics.height - @help_window.height - @command_window.height - R2_MAP_ITEM_MENU::ITEM_Y * 2
  510.     when 3
  511.       wx = R2_MAP_ITEM_MENU::ITEM_X unless R2_MAP_ITEM_MENU::FULL_WIDTH
  512.       wy = @command_window.height + R2_MAP_ITEM_MENU::ITEM_Y
  513.       ww = Graphics.width - R2_MAP_ITEM_MENU::ITEM_X * 2 unless R2_MAP_ITEM_MENU::FULL_WIDTH
  514.       wh = Graphics.height - @help_window.height - @command_window.height -  + R2_MAP_ITEM_MENU::ITEM_Y
  515.       wh -= (R2_MAP_ITEM_MENU::ITEM_Y - 10)
  516.     end
  517.     @item_window = Window_MapItemList.new(wx, wy, ww, wh)
  518.     @item_window.viewport = @viewport
  519.     @item_window.help_window = @help_window
  520.     @item_window.set_handler(:ok,     method(:on_item_ok))
  521.     @item_window.set_handler(:cancel, method(:on_item_cancel))
  522.     @command_window.item_window = @item_window
  523.   end
  524.   #--------------------------------------------------------------------------
  525.   # * [Skill] Command
  526.   #--------------------------------------------------------------------------
  527.   def on_category_ok
  528.     @item_window.activate
  529.     @item_window.select_last
  530.   end
  531.   #--------------------------------------------------------------------------
  532.   # * Item [OK]
  533.   #--------------------------------------------------------------------------
  534.   def on_item_ok
  535.     determine_item
  536.   end
  537.   #--------------------------------------------------------------------------
  538.   # * Item [Cancel]
  539.   #--------------------------------------------------------------------------
  540.   def on_item_cancel
  541.     @help_window.clear
  542.     @item_window.unselect
  543.     @command_window.activate
  544.   end
  545.   #--------------------------------------------------------------------------
  546.   # * Play SE When Using Item
  547.   #--------------------------------------------------------------------------
  548.   def play_se_for_item
  549.     Sound.play_use_skill
  550.   end
  551.   #--------------------------------------------------------------------------
  552.   # * Use Item
  553.   #--------------------------------------------------------------------------
  554.   def use_item
  555.     super
  556.     @item_window.refresh
  557.   end
  558. end
  559.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement