Class: SFML::RectangleShape

Inherits:
Object
  • Object
show all
Includes:
Graphics::ShapeInspectable, Graphics::Transformable
Defined in:
lib/sfml/graphics/rectangle_shape.rb

Overview

An axis-aligned filled rectangle.

wall = SFML::RectangleShape.new(
size: [200, 40],
position: [100, 500],
fill_color: SFML::Color["#444"],
)
window.draw(wall)

Constant Summary collapse

CSFML_PREFIX =
:sfRectangleShape

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Graphics::ShapeInspectable

#dup, #geometric_center, #global_bounds, #inverse_transform, #local_bounds, #point, #set_texture, #texture, #texture=, #texture_rect, #texture_rect=, #transform

Methods included from Graphics::Transformable

#move, #origin, #origin=, #position, #position=, #rotate, #rotation, #rotation=, #scale, #scale=, #scale_by

Constructor Details

#initialize(size:, **opts) ⇒ RectangleShape

Build a RectangleShape. Required: size: (Vector2 or [w, h]). All other styling/transform kwargs match CircleShape.

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sfml/graphics/rectangle_shape.rb', line 17

def initialize(size:, **opts)
  ptr = C::Graphics.sfRectangleShape_create
  raise GraphicsError, "sfRectangleShape_create returned NULL" if ptr.null?
  @handle = FFI::AutoPointer.new(ptr, C::Graphics.method(:sfRectangleShape_destroy))

  self.size = size
  self.fill_color         = opts[:fill_color]         if opts.key?(:fill_color)
  self.outline_color      = opts[:outline_color]      if opts.key?(:outline_color)
  self.outline_thickness  = opts[:outline_thickness]  if opts.key?(:outline_thickness)
  self.texture            = opts[:texture]            if opts.key?(:texture)
  self.texture_rect       = opts[:texture_rect]       if opts.key?(:texture_rect)
  self.position           = opts[:position]           if opts.key?(:position)
  self.origin             = opts[:origin]             if opts.key?(:origin)
  self.rotation           = opts[:rotation]           if opts.key?(:rotation)
  self.scale              = opts[:scale]              if opts.key?(:scale)
end

Instance Attribute Details

#handle ⇒ Object (readonly)

:nodoc:



77
78
79
# File 'lib/sfml/graphics/rectangle_shape.rb', line 77

def handle
  @handle
end

Instance Method Details

#draw_on(target, states_ptr = nil) ⇒ Object

Returns the draw on.



73
74
75
# File 'lib/sfml/graphics/rectangle_shape.rb', line 73

def draw_on(target, states_ptr = nil) # :nodoc:
  target._draw_native(:RectangleShape, @handle, states_ptr)
end

#fill_color ⇒ Object

Interior fill color.



49
# File 'lib/sfml/graphics/rectangle_shape.rb', line 49

def fill_color = Color.from_native(C::Graphics.sfRectangleShape_getFillColor(@handle))

#fill_color=(c) ⇒ Object

Set the fill color.



52
53
54
# File 'lib/sfml/graphics/rectangle_shape.rb', line 52

def fill_color=(c)
  C::Graphics.sfRectangleShape_setFillColor(@handle, c.to_native)
end

#outline_color ⇒ Object

Outline color (only visible when #outline_thickness > 0).



57
# File 'lib/sfml/graphics/rectangle_shape.rb', line 57

def outline_color = Color.from_native(C::Graphics.sfRectangleShape_getOutlineColor(@handle))

#outline_color=(c) ⇒ Object

Set the outline color.



60
61
62
# File 'lib/sfml/graphics/rectangle_shape.rb', line 60

def outline_color=(c)
  C::Graphics.sfRectangleShape_setOutlineColor(@handle, c.to_native)
end

#outline_thickness ⇒ Object

Outline thickness in pixels — negative draws inward.



65
# File 'lib/sfml/graphics/rectangle_shape.rb', line 65

def outline_thickness = C::Graphics.sfRectangleShape_getOutlineThickness(@handle)

#outline_thickness=(t) ⇒ Object

Set the outline thickness.



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

def outline_thickness=(t)
  C::Graphics.sfRectangleShape_setOutlineThickness(@handle, t.to_f)
end

#point_count ⇒ Object

Always 4 — the four corners.



35
# File 'lib/sfml/graphics/rectangle_shape.rb', line 35

def point_count = C::Graphics.sfRectangleShape_getPointCount(@handle)

#size ⇒ Object

Current size as a Vector2.



38
39
40
# File 'lib/sfml/graphics/rectangle_shape.rb', line 38

def size
  Vector2.from_native(C::Graphics.sfRectangleShape_getSize(@handle))
end

#size=(value) ⇒ Object

Set the size — accepts Vector2 or [w, h].



43
44
45
46
# File 'lib/sfml/graphics/rectangle_shape.rb', line 43

def size=(value)
  vec = value.is_a?(Vector2) ? value : Vector2.new(*value)
  C::Graphics.sfRectangleShape_setSize(@handle, vec.to_native_f)
end