class Fidgit::Selection
Constants
- MIN_DRAG_DISTANCE
Public Class Methods
new()
click to toggle source
# File lib/fidgit/selection.rb, line 20 def initialize @items = [] @moved = false @dragging = false end
Public Instance Methods
[](index)
click to toggle source
# File lib/fidgit/selection.rb, line 9 def [](index); @items[index]; end
add(object)
click to toggle source
# File lib/fidgit/selection.rb, line 26 def add(object) object.selected = true @items.push(object) self end
begin_drag(x, y)
click to toggle source
# File lib/fidgit/selection.rb, line 49 def begin_drag(x, y) @initial_x, @initial_y = x, y @last_x, @last_y = x, y @dragging = true @moved = false self end
clear()
click to toggle source
# File lib/fidgit/selection.rb, line 41 def clear end_drag if dragging? @items.each { |o| o.selected = false; o.dragging = false } @items.clear self end
dragging?()
click to toggle source
Current being dragged?
# File lib/fidgit/selection.rb, line 15 def dragging?; @dragging; end
each(&block)
click to toggle source
# File lib/fidgit/selection.rb, line 10 def each(&block); @items.each(&block); end
empty?()
click to toggle source
# File lib/fidgit/selection.rb, line 8 def empty?; @items.empty?; end
end_drag()
click to toggle source
# File lib/fidgit/selection.rb, line 58 def end_drag @items.each do |object| object.x, object.y = object.x.round, object.y.round object.dragging = false end @dragging = false @moved = false self end
include?(object)
click to toggle source
# File lib/fidgit/selection.rb, line 12 def include?(object); @items.include? object; end
moved?()
click to toggle source
Actually moved during a dragging operation?
# File lib/fidgit/selection.rb, line 18 def moved?; @moved; end
remove(object)
click to toggle source
# File lib/fidgit/selection.rb, line 33 def remove(object) @items.delete(object) object.selected = false object.dragging = false self end
reset_drag()
click to toggle source
Move all dragged object back to original positions.
# File lib/fidgit/selection.rb, line 70 def reset_drag if moved? @items.each do |o| o.x += @initial_x - @last_x o.y += @initial_y - @last_y end end self.end_drag self end
size()
click to toggle source
# File lib/fidgit/selection.rb, line 7 def size; @items.size; end
to_a()
click to toggle source
# File lib/fidgit/selection.rb, line 11 def to_a; @items.dup; end
update_drag(x, y)
click to toggle source
# File lib/fidgit/selection.rb, line 83 def update_drag(x, y) x, y = x.round, y.round # If the mouse has been dragged far enough from the initial click position, then 'pick up' the objects and drag. unless moved? if distance(@initial_x, @initial_y, x, y) > MIN_DRAG_DISTANCE @items.each { |o| o.dragging = true } @moved = true end end if moved? @items.each do |o| o.x += x - @last_x o.y += y - @last_y end @last_x, @last_y = x, y end self end