samples/06_save_load/01_save_load_game/app/main.rb
# Returns a boolean value.
# Finds the button that was clicked from the button list and determines what method to call.
# Adds a highlight to the highlights collection.
Returns a boolean value.
Finds the button that was clicked from the button list and determines what method to call.
Adds a highlight to the highlights collection.
Returns a boolean value . Finds the button that was clicked from the button list and determines what method to call . Adds a highlight to the highlights collection .
def button_clicked?
    return nil unless click_pos 
      button = @button_list.find do |k, v| 
        click_pos.inside_rect? v[:primitives].last.rect 
      end
      return unless button 
      method_to_call = "#{button[0]}_clicked".to_sym 
      if self.respond_to? method_to_call 
        border = button[1][:primitives].last 
        state.layout.highlights << state.new_entity(:highlight) do |h|
            h.x = border.x
            h.y = border.y
            h.w = border.w
            h.h = border.h
            h.max_lifetime = 10
            h.lifetime = h.max_lifetime
            h.color = [120, 120, 180] 
          end
          self.send method_to_call 
        else 
          border = button[1][:primitives].last 
          state.layout.highlights << state.new_entity(:highlight) do |h|
            h.x = border.x
            h.y = border.y
            h.w = border.w
            h.h = border.h
            h.max_lifetime = 4 
            h.lifetime = h.max_lifetime
            h.color = [120, 80, 80] 
          end
          puts "It looks like #{method_to_call} doesn't exists on TextedBasedGamePresenter. Please add this method:"
          puts "Just copy the code below and put it in the #{TextedBasedGamePresenter} class definition."
          puts ""
          puts "```"
          puts "class TextedBasedGamePresenter <--- find this class and put the method below in it"
          puts ""
          puts "  def #{method_to_call}"
          puts "    puts 'Yay that worked!'"
          puts "  end"
          puts ""
          puts "end <-- make sure to put the #{method_to_call} method in between the `class` word and the final `end` statement."
          puts "```"
          puts ""
      end
  end
def button_clicked? return nil unless click_pos button = @button_list . find do | k , v | click_pos . inside_rect? v [ :primitives ] . last . rect end return unless button method_to_call = "#{button[0]}_clicked" . to_sym if self . respond_to? method_to_call border = button [ 1 ] [ :primitives ] . last state . layout . highlights << state . new_entity ( :highlight ) do | h | h . x = border . x h . y = border . y h . w = border . w h . h = border . h h . max_lifetime = 10 h . lifetime = h . max_lifetime h . color = [ 120 , 120 , 180 ] end self . send method_to_call else border = button [ 1 ] [ :primitives ] . last state . layout . highlights << state . new_entity ( :highlight ) do | h | h . x = border . x h . y = border . y h . w = border . w h . h = border . h h . max_lifetime = 4 h . lifetime = h . max_lifetime h . color = [ 120 , 80 , 80 ] end puts "It looks like #{method_to_call} doesn't exists on TextedBasedGamePresenter. Please add this method:" puts "Just copy the code below and put it in the #{TextedBasedGamePresenter} class definition." puts "" puts "```" puts "class TextedBasedGamePresenter <--- find this class and put the method below in it" puts "" puts "  def #{method_to_call}" puts "    puts 'Yay that worked!'" puts "  end" puts "" puts "end <-- make sure to put the #{method_to_call} method in between the `class` word and the final `end` statement." puts "```" puts "" end end
Returns a boolean value.
Returns a boolean value .
# return nil unless click_pos holds coordinates of mouse click # goes through button_list to find button clicked # was the mouse clicked inside the rect of button? # return unless a button was clicked # sets method_to_call to symbol (like :save_game or :load_game) # returns true if self responds to the given method (method actually exists) # sets border definition using value of last key in button list hash # declares each highlight as a new entity, sets properties # sets color to shade of purple # invoke method identified by symbol # otherwise, if self doesn't respond to given method # sets border definition using value of last key in hash # declares each highlight as a new entity, sets properties # different max_lifetime than the one set if respond_to? had been true # sets color to dark color # instructions for users on how to add the missing method_to_call to the code