Sha256: c0ba20d0459c13da7fb0aaeff295cebb7d2b3fe6c09bf640769c2b0268b158dc

Contents?: true

Size: 1.37 KB

Versions: 9

Compression:

Stored size: 1.37 KB

Contents

require 'ruby-processing'

# Click on the box and drag it across the screen. 

class MouseFunctions < Processing::App

  def setup
    @block = {"x" => width/2.0, "y" => height/2.0}
    @block_diff = {"x" => 0.0, "y" => 0.0}
    @locked = false
    @over_block = false
    @block_width = 20
    
    rect_mode RADIUS
  end
  
  def draw
  	background 0
  	
  	fill 153
  	
  	if (mouse_x > @block["x"]-@block_width &&	# Test if the cursor is over the box 
	      mouse_x < @block["x"]+@block_width &&
	      mouse_y > @block["y"]-@block_width &&
	      mouse_y < @block["y"]+@block_width )
  		
  		@over_block = true
  		
  		stroke 255
  		
  		fill 255 if block_locked?
  	else
  		@over_block = false
  		stroke 153
  	end
  	
  	# Draw the box
  	rect @block["x"], @block["y"], @block_width, @block_width
  end
  
  def block_locked?
  	@block_locked
  end
  
  def over_block?
  	@over_block
  end
  
  def mouse_pressed
  	if over_block?
  		@block_locked = true
  		fill 255
  	else
  		@block_locked = false
  	end
  	@block_diff["x"] = mouse_x - @block["x"]
  	@block_diff["y"] = mouse_y - @block["y"]
  end
  
  def mouse_dragged
  	if block_locked?
  		@block["x"] = mouse_x - @block_diff["x"]
  		@block["y"] = mouse_y - @block_diff["y"]
  	end
  end
  
  def mouse_released
  	@block_locked = false
  end
  
end

MouseFunctions.new :title => "Mouse Functions", :width => 200, :height => 200

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ruby-processing-1.0.11 samples/processing_app/basics/input/mouse_functions.rb
ruby-processing-1.0.10.1 samples/processing_app/basics/input/mouse_functions.rb
ruby-processing-1.0.9 samples/processing_app/basics/input/mouse_functions.rb
ruby-processing-1.0.4 samples/processing_app/basics/input/mouse_functions.rb
ruby-processing-1.0.3 samples/processing_app/basics/input/mouse_functions.rb
ruby-processing-1.0.5 samples/processing_app/basics/input/mouse_functions.rb
ruby-processing-1.0.6 samples/processing_app/basics/input/mouse_functions.rb
ruby-processing-1.0.7 samples/processing_app/basics/input/mouse_functions.rb
ruby-processing-1.0.8 samples/processing_app/basics/input/mouse_functions.rb