Class: Fidgit::Selection

Inherits:
Object show all
Defined in:
lib/fidgit/selection.rb

Constant Summary collapse

MIN_DRAG_DISTANCE =
2

Instance Method Summary collapse

Constructor Details

#initializeSelection

Returns a new instance of Selection.



20
21
22
23
24
# File 'lib/fidgit/selection.rb', line 20

def initialize
  @items = []
  @moved = false
  @dragging = false
end

Instance Method Details

#[](index) ⇒ Object



9
# File 'lib/fidgit/selection.rb', line 9

def [](index); @items[index]; end

#add(object) ⇒ Object



26
27
28
29
30
31
# File 'lib/fidgit/selection.rb', line 26

def add(object)
  object.selected = true
  @items.push(object)

  self
end

#begin_drag(x, y) ⇒ Object



49
50
51
52
53
54
55
56
# 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

#clearObject



41
42
43
44
45
46
47
# 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?Boolean

Current being dragged?

Returns:

  • (Boolean)


15
# File 'lib/fidgit/selection.rb', line 15

def dragging?; @dragging; end

#each(&block) ⇒ Object



10
# File 'lib/fidgit/selection.rb', line 10

def each(&block); @items.each(&block); end

#empty?Boolean

Returns:

  • (Boolean)


8
# File 'lib/fidgit/selection.rb', line 8

def empty?; @items.empty?; end

#end_dragObject



58
59
60
61
62
63
64
65
66
67
# 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) ⇒ Boolean

Returns:

  • (Boolean)


12
# File 'lib/fidgit/selection.rb', line 12

def include?(object); @items.include? object; end

#moved?Boolean

Actually moved during a dragging operation?

Returns:

  • (Boolean)


18
# File 'lib/fidgit/selection.rb', line 18

def moved?; @moved; end

#remove(object) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/fidgit/selection.rb', line 33

def remove(object)
  @items.delete(object)
  object.selected = false
  object.dragging = false

  self
end

#reset_dragObject

Move all dragged object back to original positions.



70
71
72
73
74
75
76
77
78
79
80
81
# 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

#sizeObject



7
# File 'lib/fidgit/selection.rb', line 7

def size; @items.size; end

#to_aObject



11
# File 'lib/fidgit/selection.rb', line 11

def to_a; @items.dup; end

#update_drag(x, y) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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