Class: Stagecraft::ObservedVec3

Inherits:
Object
  • Object
show all
Defined in:
lib/stagecraft/core/observed_value.rb

Constant Summary collapse

COMPONENTS =
%i[x y z].freeze

Instance Method Summary collapse

Constructor Details

#initialize(owner, value = Larb::Vec3.new) ⇒ ObservedVec3

Returns a new instance of ObservedVec3.



7
8
9
10
# File 'lib/stagecraft/core/observed_value.rb', line 7

def initialize(owner, value = Larb::Vec3.new)
  @owner = owner
  @value = coerce(value)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments, **keywords, &block) ⇒ Object



61
62
63
64
65
# File 'lib/stagecraft/core/observed_value.rb', line 61

def method_missing(name, *arguments, **keywords, &block)
  return super unless @value.respond_to?(name)

  @value.public_send(name, *unwrap_arguments(*arguments), **keywords, &block)
end

Instance Method Details

#==(other) ⇒ Object



53
54
55
# File 'lib/stagecraft/core/observed_value.rb', line 53

def ==(other)
  other.respond_to?(:to_a) && to_a == other.to_a
end

#[](index) ⇒ Object



37
38
39
# File 'lib/stagecraft/core/observed_value.rb', line 37

def [](index)
  @value[index]
end

#[]=(index, value) ⇒ Object



32
33
34
35
# File 'lib/stagecraft/core/observed_value.rb', line 32

def []=(index, value)
  component = COMPONENTS.fetch(index)
  public_send(:"#{component}=", value)
end

#inspectObject



57
58
59
# File 'lib/stagecraft/core/observed_value.rb', line 57

def inspect
  "#{self.class.name}[#{to_a.join(", ")}]"
end

#normalize!Object



41
42
43
# File 'lib/stagecraft/core/observed_value.rb', line 41

def normalize!
  set(@value.normalize)
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/stagecraft/core/observed_value.rb', line 67

def respond_to_missing?(name, include_private = false)
  @value.respond_to?(name, include_private) || super
end

#set(x, y = nil, z = nil) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/stagecraft/core/observed_value.rb', line 23

def set(x, y = nil, z = nil)
  values = y.nil? && z.nil? ? vector_components(x) : [x, y, z]
  return self if to_a == values.map(&:to_f)

  @value = Larb::Vec3.new(*values)
  changed!
  self
end

#to_aObject



49
50
51
# File 'lib/stagecraft/core/observed_value.rb', line 49

def to_a
  @value.to_a
end

#to_larbObject



45
46
47
# File 'lib/stagecraft/core/observed_value.rb', line 45

def to_larb
  Larb::Vec3.new(*to_a)
end