Class: Ruby2D::Image

Inherits:
Object
  • Object
show all
Includes:
Renderable
Defined in:
lib/ruby2d/image.rb

Overview

An image drawn in the window

Direct Known Subclasses

Sprite

Constant Summary

Constants included from Interactive

Ruby2D::Interactive::OBJECT_EVENTS, Ruby2D::Interactive::OBJECT_EVENT_FILTER_PREDICATES

Instance Attribute Summary collapse

Attributes included from Renderable

#color, #padding_bottom, #padding_left, #padding_right, #padding_top, #visible, #x, #x_align, #y, #y_align, #z

Instance Method Summary collapse

Methods included from Renderable

#_alignment_anchor_dx, #_alignment_anchor_dy, #_apply_padding, #_extract_alignment, #_point_in_polygon?, #_point_on_segment?, #_require_numeric_position, #_resolve_alignment, #_unrotate, #_validate_dimensions, #add, #colour=, #contains?, flatten_color, flatten_per_vertex, flatten_points, flatten_resolved_color, #hide, #opacity, #opacity=, #padding=, #remove, resolve_color_or_default, resolve_single_color, #show

Methods included from Interactive

#_fire_event, #interactive?, #off, #on

Constructor Details

#initialize(path, width: nil, height: nil, x: 0, y: 0, z: 0, rotate: 0, rx: nil, ry: nil, tint: nil, opacity: nil, add: true, visible: true, padding: nil, padding_top: nil, padding_right: nil, padding_bottom: nil, padding_left: nil, _share_from: nil) ⇒ Image

Create an image



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ruby2d/image.rb', line 52

def initialize(path,
               width: nil, height: nil, x: 0, y: 0, z: 0,
               rotate: 0, rx: nil, ry: nil, tint: nil,
               opacity: nil, add: true, visible: true,
               padding: nil, padding_top: nil, padding_right: nil,
               padding_bottom: nil, padding_left: nil,
               _share_from: nil)
  @width = width
  @height = height

  x, y = _extract_alignment(x, y)
  _apply_padding(padding, padding_top, padding_right, padding_bottom, padding_left)
  @x = x
  @y = y
  @z = z
  @rotate = rotate
  @_user_rx = rx
  @_user_ry = ry
  self.tint = tint || 'white'
  self.tint.opacity = opacity unless opacity.nil?

  if 
    # SpriteSheet uses this to give every Sprite the same backing texture
    # without re-decoding the file or re-uploading to the GPU.
    @path        = .path
    @ext_image   = .instance_variable_get(:@ext_image)
    @orig_width  = .instance_variable_get(:@orig_width)
    @orig_height = .instance_variable_get(:@orig_height)
    @width     ||= @orig_width
    @height    ||= @orig_height
    @clipped     = false
    @clip_x      = 0.0
    @clip_y      = 0.0
    @clip_width  = @orig_width
    @clip_height = @orig_height
  else
    @path = path.to_s
    raise Error, "Image file `#{@path}` not found" unless File.exist?(@path)

    # Preserve user-provided values before image_create may overwrite them
    provided_width = @width
    provided_height = @height
    provided_rotate = @rotate
    Ext.image_create(self)

    @width = provided_width || @width
    @height = provided_height || @height
    @rotate = provided_rotate
  end

  @visible = visible
  self.add if add
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



13
14
15
# File 'lib/ruby2d/image.rb', line 13

def height
  @height
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/ruby2d/image.rb', line 12

def path
  @path
end

#rotateObject

Returns the value of attribute rotate.



13
14
15
# File 'lib/ruby2d/image.rb', line 13

def rotate
  @rotate
end

#widthObject

Returns the value of attribute width.



13
14
15
# File 'lib/ruby2d/image.rb', line 13

def width
  @width
end

Instance Method Details

#_render_sceneObject

Scene-graph draw hook (see Renderable#_render_scene): render with no overrides, minus its keyword handling — a zero-arg call into the 7-keyword render still pays ~3.4µs of keyword setup on wasm mruby.



176
177
178
179
# File 'lib/ruby2d/image.rb', line 176

def _render_scene
  _resolve_alignment
  Ext.image_draw(self)
end

#render(x: nil, y: nil, width: nil, height: nil, rotate: nil, tint: nil, opacity: nil) ⇒ Object

Render the image. Called with overrides for one-shot rendering inside a render block; with no arguments it draws the same frame the scene graph does (delegating to _render_scene).



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ruby2d/image.rb', line 136

def render(x: nil, y: nil, width: nil, height: nil, rotate: nil,
           tint: nil, opacity: nil)
  if x.nil? && y.nil? && width.nil? && height.nil? && rotate.nil? &&
     tint.nil? && opacity.nil?
    return _render_scene
  end

  Window.render_ready_check

  saved_x, saved_y = @x, @y
  saved_width, saved_height = @width, @height
  saved_rotate = @rotate
  saved_color = @color

  @x = x if x
  @y = y if y
  @width = width if width
  @height = height if height
  @rotate = rotate if rotate

  if tint || opacity
    @color = tint ? Color.new(tint) : Color.new(saved_color)
    @color.opacity = opacity if opacity
  end

  begin
    Ext.image_draw(self)
  ensure
    @x, @y = saved_x, saved_y
    @width, @height = saved_width, saved_height
    @rotate = saved_rotate
    @color = saved_color
  end
end

#resize!(width = @width, height = @height) ⇒ Object

Re-rasterize the source at a new pixel size and update width/height. For SVGs this re-runs the vector rasterizer so the image stays crisp at the new size. For raster images it resamples the source to the new size — useful for trimming GPU memory when displaying a large source small. Called with no arguments, re-rasterizes at the current width/height — handy after assigning to width=/height= to commit a fresh raster.



112
113
114
115
116
117
118
119
120
121
# File 'lib/ruby2d/image.rb', line 112

def resize!(width = @width, height = @height)
  unless width.is_a?(Numeric) && height.is_a?(Numeric) && width.positive? && height.positive?
    raise Error, 'Image#resize! requires positive width and height'
  end

  Ext.image_resize(self, width, height)
  @width = width
  @height = height
  self
end

#rxObject

Get the rotation center x coordinate



32
33
34
# File 'lib/ruby2d/image.rb', line 32

def rx
  @_user_rx.nil? ? @x + @width / 2.0 : @_user_rx
end

#rx=(val) ⇒ Object

Set the rotation center x coordinate



42
43
44
# File 'lib/ruby2d/image.rb', line 42

def rx=(val)
  @_user_rx = val
end

#ryObject

Get the rotation center y coordinate



37
38
39
# File 'lib/ruby2d/image.rb', line 37

def ry
  @_user_ry.nil? ? @y + @height / 2.0 : @_user_ry
end

#ry=(val) ⇒ Object

Set the rotation center y coordinate



47
48
49
# File 'lib/ruby2d/image.rb', line 47

def ry=(val)
  @_user_ry = val
end

#tintObject

The image's tint color. The image's own texture colors are multiplied by this — tint: 'red' makes the image redder, not solid red.



125
126
127
# File 'lib/ruby2d/image.rb', line 125

def tint
  @color
end

#tint=(c) ⇒ Object



129
130
131
# File 'lib/ruby2d/image.rb', line 129

def tint=(c)
  @color = Color.new(c)
end

#x=(value) ⇒ Object

Set the x position. Pass a symbol (:left, :center, :right) to set alignment intent — resolved at draw time against the window.



17
18
19
20
21
# File 'lib/ruby2d/image.rb', line 17

def x=(value)
  return self.x_align = value if value.is_a?(Symbol)
  @x_align = nil unless @_resolving_alignment
  @x = value
end

#y=(value) ⇒ Object

Set the y position. Pass a symbol (:top, :center, :bottom) to set alignment intent — resolved at draw time against the window.



25
26
27
28
29
# File 'lib/ruby2d/image.rb', line 25

def y=(value)
  return self.y_align = value if value.is_a?(Symbol)
  @y_align = nil unless @_resolving_alignment
  @y = value
end