Change icon on a custom menu

Started by Dweller, April 11, 2011, 10:04:06 am

Previous topic - Next topic

Dweller

April 11, 2011, 10:04:06 am Last Edit: April 12, 2011, 12:49:32 am by Dweller
I´m using this script to add icons to my custom menu:

#=============================================================
# ** Redd's MenuIcons
#=================================================================

class Window_MenuIcons < Window_Command
 #--------------------------------------------------------------------------
 # * Refresh
 #--------------------------------------------------------------------------
 def refresh
   self.contents.clear
   for i in 0...@item_max
     draw_item(i, normal_color)
     # Draw the icon associated with the index.
     draw_icon(i)
   end
 end
 #--------------------------------------------------------------------------
 # * Draw Item
 #--------------------------------------------------------------------------
 def draw_item(index, color)
   self.contents.font.color = color
   # rect = Rect.new(x, y, width, height) Note the x is 32, not the default 4.
   rect = Rect.new(32, 32 * index, self.contents.width - 8, 32)
   self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
   self.contents.draw_text(rect, @commands[index])
 end
 #--------------------------------------------------------------------------
 # * Draw Icon
 #--------------------------------------------------------------------------
 def draw_icon(index)
   # Case index
   case index
   # Conditional branch
   when 0
     # Sets the icon name (Case Sensitive)
    icon = '1'      
   when 1
    icon = '2'
   when 2
    icon = '3'
   when 3
    icon = '4'
   when 4
    icon = '5'
   when 5
    icon = '6'
   
   else
     # Don't draw anything.
     icon = ''
   end
   # Create the bitmap image for the icon
   bitmap = RPG::Cache.icon(icon)
   # Make the rectangle for the image to be in
   src_rect = Rect.new(0, 0, bitmap.width, bitmap.height)
   # Draw the bitmap inside the rectangle, at 4 pixels across,
   # and down according to the index
   self.contents.blt(4, 32*index+5, bitmap, src_rect)
 end
 end


Also changed Scene_Menu script adding
@command_window = Window_MenuIcons.new(160, [s1, s2, s3, s4, s5, s6])
to display the icons.

Now I want to change icons if an option is active (not selected). I added a global variable to the Window_MenuIcons  script to select between the two different icons (one when the option is active and the other when is not):

def draw_icon(index)
   # Case index
   case index
   # Conditional branch
   when 0
     # Sets the icon name (Case Sensitive)
    icon = '1a' if $Icon_popup  = 0
    icon = '1b' if $Icon_popup  != 0        
   when 1
    icon = '2a' if $Icon_popup  = 1
    icon = '2b' if $Icon_popup  != 1
   when 2
    icon = '3a' if $Icon_popup  = 2
    icon = '3b' if $Icon_popup  != 2
   when 3
    icon = '4a' if $Icon_popup  = 3
    icon = '4b' if $Icon_popup  != 3
   when 4
    icon = '5a' if $Icon_popup  = 4
    icon = '5b' if $Icon_popup  != 4
   when 5
    icon = '6a' if $Icon_popup  = 5
    icon = '6b' if $Icon_popup  != 5
   
   else
     # Don't draw anything.
     icon = ''
   end
   # Create the bitmap image for the icon
   bitmap = RPG::Cache.icon(icon)
   # Make the rectangle for the image to be in
   src_rect = Rect.new(0, 0, bitmap.width, bitmap.height)
   # Draw the bitmap inside the rectangle, at 4 pixels across,
   # and down according to the index
   self.contents.blt(4, 32*index+5, bitmap, src_rect)
 end


The next step is to change the global variable if the menu option is active or not. I think I must use something like:
case @command_window.index
     when 0  # item
       $Icon_popup = 0
     when 1  # skill
      $Icon_popup = 1
     when 2  # equipment
       $Icon_popup = 2
     when 3  # status
       $Icon_popup = 3
     when 4  # save
       $Icon_popup = 4
     when 5  # end game
       $Icon_popup = 5
     end


But I`m a bit confused about where I must use this code. I tried inside  update_command and update (inside Scene_menu) but fail.

I´m on the right way doing this or I´m completly wrong from the begining?
Dwellercoc
Spoiler: ShowHide