#============================================================================== # LonaCraft - 85_MenuCraft.rb (online B.0.10.8.7.5 menu compatibility) # The online build constructs a local `contents` list inside Scene_Menu#start. # It has NO Scene_Menu#init_contents. The menu invokes create_menu(labels) # and create_contents(pages) separately, so inject both in the same position. # These wrappers also compose with LonaPaletteStudio's menu injections. #============================================================================== module LonaCraft module MenuCraft STATION = :hand # Menu_Command row layout (170_menu_base.rb:284): y = ROW_TOP + i * ROW_STEP ROW_TOP = 11 ROW_STEP = 18 NORMAL_ROWS = 9 TIGHT_STEP = 17 def self.text(key) $game_text["LonaCraft:Menu:#{key}"] end # nil when crafting by hand may start now, else :threat, :overmap or :actor def self.block_reason return :overmap if $game_map && $game_map.isOverMap return :threat if $game_map && $game_map.threat return :actor if $game_player.nil? || LonaCraft.vanilla_only_actor?($game_player.actor) nil end def self.skill_level return 0 unless defined?(LonaCraft::CraftUI) LonaCraft::CraftUI.skill_level.to_i rescue => ex p "LonaCraft MenuCraft skill_level: #{ex.message}" 0 end # The online version builds command names and pages separately. # Both insertions must match or a row will open the wrong page. def self.insert_command_name(names) return names unless names.is_a?(Array) return names if $game_player.nil? || $game_player.cannot_control items_label = $game_text["menu:core/items"] craft_label = text("Craft/entry") return names if names.include?(craft_label) at = names.index(items_label) return names if at.nil? result = names.dup result.insert(at + 1, craft_label) result rescue => ex p "LonaCraft MenuCraft insert_command_name: #{ex.message}" names end def self.insert_content_page(pages) return pages unless pages.is_a?(Array) return pages if $game_player.nil? || $game_player.cannot_control return pages if pages.any?{|page| page.is_a?(Menu_LonaCraft) || page == Menu_LonaCraft } at = pages.index{|page| page.is_a?(Menu_Items) || page == Menu_Items } return pages if at.nil? result = pages.dup # Scene_Menu#start already instantiates each page with .new. result.insert(at + 1, pages[at].is_a?(Class) ? Menu_LonaCraft : Menu_LonaCraft.new) result rescue => ex p "LonaCraft MenuCraft insert_content_page: #{ex.message}" pages end # Called from the create_menu alias below: more than NORMAL_ROWS rows # in the game's layout (row 0 at ROW_TOP, ROW_STEP apart) are moved to # TIGHT_STEP apart so the last one stays above the stats block. Clears # the cached Menu_Command#bounding_rect (170_menu_base.rb:293-313) so # the caller can rebuild the mouse rectangle. Any other layout is left # alone. Returns true when the rows were moved. def self.fit_rows(menu) return false if menu.nil? || !menu.respond_to?(:menu_sprites) rows = menu.menu_sprites return false if rows.nil? || rows.size <= NORMAL_ROWS return false unless rows[0].y == ROW_TOP && rows[1].y == ROW_TOP + ROW_STEP rows.each_with_index{|spr, i| spr.y = ROW_TOP + i * TIGHT_STEP } menu.instance_variable_set(:@bounding_rect, nil) true rescue => ex p "LonaCraft MenuCraft fit_rows: #{ex.message}" false end # Opens the crafting screen for the hand station; back = :menu makes # leaving it return to the menu (Scene_LonaCraft#exit_scene, # 80_CraftScene.rb). def self.open_hand_craft scene = SceneManager.scene LonaCraft.trace{ "menu Craft row picked (#{scene.class}, menu_index #{(scene.menu_index rescue nil).inspect})" } SceneManager.prevOptChooseSet(scene.menu_index) if scene.is_a?(Scene_Menu) && scene.menu_index LonaCraft.open_craft_scene(STATION, :menu) end end end #============================================================================== # Menu_LonaCraft: the Craft page of the main menu #============================================================================== class Menu_LonaCraft < Menu_ContentBase FONT_COLOR = [20, 255, 20, 255] BLOCK_COLOR = [255, 64, 64, 255] LEFT = 170 TOP = 40 # Left column of the main menu (as Menu_Items, 191_menu_07items.rb:122) MENU_COLUMN_W = 156 def initialize super() @info = Sprite.new(@viewport) @info.bitmap = Bitmap.new([Graphics.width - LEFT - 10, 32].max, 140) @info.x = LEFT @info.y = TOP @info.z = System_Settings::SCENE_Menu_Contents_Z refresh hide end def refresh bmp = @info.bitmap bmp.clear LonaCraft.apply_ui_font(bmp) bmp.font.color.set(*FONT_COLOR) bmp.font.size = 22 bmp.draw_text(0, 0, bmp.width, 28, LonaCraft::MenuCraft.text("Craft/title")) bmp.font.size = 18 bmp.draw_text(0, 34, bmp.width, 24, LonaCraft::MenuCraft.text("Craft/hint")) bmp.draw_text(0, 58, bmp.width, 24, "#{LonaCraft::MenuCraft.text("Craft/level")} #{LonaCraft::MenuCraft.skill_level}") reason = LonaCraft::MenuCraft.block_reason return if reason.nil? bmp.font.color.set(*BLOCK_COLOR) bmp.draw_text(0, 90, bmp.width, 24, LonaCraft::MenuCraft.text("Craft/blocked_#{reason}")) end def show refresh super end # OK on the row (Menu_Contents#process_ok, 170_menu_base.rb:412-417, # deactivates the menu first) or a left click on the page area. def enter_page if LonaCraft::MenuCraft.block_reason SndLib.sys_buzzer refresh @menu.activate if @menu return end LonaCraft::MenuCraft.open_hand_craft end # Runs every frame for every page (170_menu_base.rb:428-430). def update return unless @viewport.visible scene = SceneManager.scene return unless scene.is_a?(Scene_Menu) # a pick this frame already left the menu return unless Mouse.enable? return unless Input.trigger?(:MZ_LINK) return if Mouse.within_XYWH?(0, 0, MENU_COLUMN_W, Graphics.height) return if scene.menu_rect && Mouse.within_rect?(scene.menu_rect) enter_page end # Keeps the main menu active on mouse clicks (170_menu_base.rb:199-200), # as Menu_MainStats does; this page has no inner cursor. def mouse_no_need_update true end def dispose @info.bitmap.dispose if @info.bitmap @info.dispose super end end #============================================================================== # Scene_Menu: the Craft row after Items #============================================================================== class Scene_Menu unless method_defined?(:lonacraft_menu_create_menu) alias_method :lonacraft_menu_create_menu, :create_menu end # The source game (170_menu_base.rb) creates its commands inside #start; # #init_contents is absent. Inject after Items, then let other mods add theirs. def create_menu(command_names) lonacraft_menu_create_menu(LonaCraft::MenuCraft.insert_command_name(command_names)) # Layout the extra row above the stats panel and rebuild the mouse hitbox. @menu_rect = @menu.bounding_rect if LonaCraft::MenuCraft.fit_rows(@menu) end unless method_defined?(:lonacraft_menu_create_contents) alias_method :lonacraft_menu_create_contents, :create_contents end # Keep actual Menu_Contents pages aligned with the command labels. def create_contents(pages, *args, **kwargs, &block) pages = LonaCraft::MenuCraft.insert_content_page(pages) if kwargs.empty? lonacraft_menu_create_contents(pages, *args, &block) else lonacraft_menu_create_contents(pages, *args, **kwargs, &block) end end end