Search code examples
rubyruboto

Ruunning a method with paramters and give out a returned value


I have prepared that Ruby-Class:

require 'ruboto/widget'
require 'ruboto/util/toast'

ruboto_import_widgets :Button, :LinearLayout, :TextView, :EditText

# http://xkcd.com/378/

class MinecraftDistanceCalculatorActivity
  def onCreate(bundle)
    super
    set_title 'Minecraft Distance Calculator'

    self.content_view =
        linear_layout :orientation => :vertical do
          @startx    = text_view :text => 'Enter start x-axe', :id => 42,
                                 :layout => {:width => :match_parent},
                                 :gravity => :center, :text_size => 16.0
          @edit_text = edit_text :id => 44,
                                 :layout => {:width => :match_parent},
                                 :gravity => :center, :text_size => 16.0
          @startz    = text_view :text => 'Enter start z-axe', :id => 45,
                                 :layout => {:width => :match_parent},
                                 :gravity => :center, :text_size => 16.0
          @edit_text = edit_text :id => 46,
                                 :layout => {:width => :match_parent},
                                 :gravity => :center, :text_size => 16.0
          @targetx   = text_view :text => 'Enter target x-axe', :id => 47,
                                 :layout => {:width => :match_parent},
                                 :gravity => :center, :text_size => 16.0
          @edit_text = edit_text :id => 48,
                                 :layout => {:width => :match_parent},
                                 :gravity => :center, :text_size => 16.0
          @targetz   = text_view :text => 'Enter target z-axe', :id => 49,
                                 :layout => {:width => :match_parent},
                                 :gravity => :center, :text_size => 16.0
          @edit_text = edit_text :id => 50,
                                 :layout => {:width => :match_parent},
                                 :gravity => :center, :text_size => 16.0

          button :text => 'Calculate!',
                 :layout => {:width => :match_parent},
                 :id => 43, :on_click_listener => proc { distance }

          # @text_view = text_view :text => "The distance is #{distance} blocks", :id => 50,
          #                        :layout => {:width => :match_parent},
          #                        :gravity => :center, :text_size => 16.0
        end

  rescue Exception
    puts "Exception creating activity: #{$!}"
    puts $!.backtrace.join("\n")
  end

  private

  def self.distance(startx, startz, targetx, targetz)
    calcx = startx - targetx
    calcz = startz - targetz

    calcdist = calcx**2 + calcz**2
    distance = Math.sqrt(calcdist).round
    toast 'Calculated distance'
    return distance
  end

Now i would like to a access the button with using the specific parameters startx, startz, targetx and targetz. Then i plan to give out the returned variable "distance" as text_field. Maybe anyone knows, how to do that?


Solution

  • According to your comment, you can try this :

    :on_click_listener => proc { @distance = self.class.distance(@startx, @startz, @targetx, @targetz) }
    

    Then, just use your distance with @distance

    @text_view = text_view :text => "The distance is #{@distance} blocks", :id => 50,
                           :layout => {:width => :match_parent},
                           :gravity => :center, :text_size => 16.0