Module: SFML::Graphics::Transformable

Included in:
CircleShape, ConvexShape, RectangleShape, Shape, Sprite, Text, TransformableObject
Defined in:
lib/sfml/graphics/transformable.rb

Overview

Shared transform interface for Sprite, CircleShape, RectangleShape (and Text once added). Including class must:

1. Define a constant CSFML_PREFIX (Symbol), e.g. :sfCircleShape
2. Hold the FFI handle in @handle

All wrappers go through C::Graphics.public_send so the same Ruby code picks up sfSprite_, sfCircleShape_, sfRectangleShape_* etc. without repetition. The cost (~1µs of method dispatch on a typical call) is negligible against any GPU work, but the wins on maintainability are large — adding a new shape becomes ~5 lines.

Instance Method Summary collapse

Instance Method Details

#move(offset) ⇒ Object

Translate by offset (relative move). Chainable — returns self.



56
57
58
59
# File 'lib/sfml/graphics/transformable.rb', line 56

def move(offset)
  _csfml(:move, @handle, _coerce_vec2(offset).to_native_f)
  self
end

#origin ⇒ Object

The local origin (pivot for rotation/scale). Default is [0, 0] — set it to the centre of the bounding box for "rotate in place".



46
47
48
# File 'lib/sfml/graphics/transformable.rb', line 46

def origin
  Vector2.from_native(_csfml(:getOrigin, @handle))
end

#origin=(value) ⇒ Object

Set the origin — accepts Vector2 or [x, y] Array.



51
52
53
# File 'lib/sfml/graphics/transformable.rb', line 51

def origin=(value)
  _csfml(:setOrigin, @handle, _coerce_vec2(value).to_native_f)
end

#position ⇒ Object

Current 2D position as a Vector2.



15
16
17
# File 'lib/sfml/graphics/transformable.rb', line 15

def position
  Vector2.from_native(_csfml(:getPosition, @handle))
end

#position=(value) ⇒ Object

Move to absolute [x, y] — accepts Vector2 or [x, y] Array.



20
21
22
# File 'lib/sfml/graphics/transformable.rb', line 20

def position=(value)
  _csfml(:setPosition, @handle, _coerce_vec2(value).to_native_f)
end

#rotate(degrees) ⇒ Object

Rotate by degrees (relative). Chainable.



62
63
64
65
# File 'lib/sfml/graphics/transformable.rb', line 62

def rotate(degrees)
  _csfml(:rotate, @handle, degrees.to_f)
  self
end

#rotation ⇒ Object

Current rotation in degrees (counter-clockwise positive).



25
26
27
# File 'lib/sfml/graphics/transformable.rb', line 25

def rotation
  _csfml(:getRotation, @handle)
end

#rotation=(degrees) ⇒ Object

Set absolute rotation in degrees.



30
31
32
# File 'lib/sfml/graphics/transformable.rb', line 30

def rotation=(degrees)
  _csfml(:setRotation, @handle, degrees.to_f)
end

#scale ⇒ Object

Current scale factors as a Vector2 ([1, 1] means original size).



35
36
37
# File 'lib/sfml/graphics/transformable.rb', line 35

def scale
  Vector2.from_native(_csfml(:getScale, @handle))
end

#scale=(value) ⇒ Object

Set the scale — accepts Vector2 or [sx, sy] Array.



40
41
42
# File 'lib/sfml/graphics/transformable.rb', line 40

def scale=(value)
  _csfml(:setScale, @handle, _coerce_vec2(value).to_native_f)
end

#scale_by(factors) ⇒ Object

Multiply current scale by factors (relative scale). Chainable.



68
69
70
71
# File 'lib/sfml/graphics/transformable.rb', line 68

def scale_by(factors)
  _csfml(:scale, @handle, _coerce_vec2(factors).to_native_f)
  self
end