Module: Osb::Commandable

Included in:
Animation, Sprite
Defined in:
lib/osb/commandable.rb

Instance Method Summary collapse

Instance Method Details

#additive_color_blending(start_time:, end_time:) ⇒ void

This method returns an undefined value.

Use additive-color blending instead of alpha-blending.

Parameters:



329
330
331
332
333
334
335
336
337
# File 'lib/osb/commandable.rb', line 329

def additive_color_blending(start_time:, end_time:)
  self.raise_if_trigger_called!
  Internal.raise_if_invalid_start_time!(start_time)
  Internal.raise_if_invalid_end_time!(end_time)

  tabs = " " * self.tab_level
  command = "#{tabs}P,#{start_time},#{end_time},A"
  @commands << command
end

#color(start_time:, end_time: start_time, easing: Easing::Linear, start_color:, end_color: start_color) ⇒ void

This method returns an undefined value.

The virtual light source colour on the object. The colours of the pixels on the object are determined subtractively.

Parameters:



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/osb/commandable.rb', line 262

def color(
  start_time:,
  end_time: start_time,
  easing: Easing::Linear,
  start_color:,
  end_color: start_color
)
  self.raise_if_trigger_called!
  Internal.raise_if_invalid_start_time!(start_time)
  Internal.raise_if_invalid_end_time!(end_time)
  Internal.raise_if_invalid_easing!(easing)
  Internal.assert_type!(
    start_color,
    [Osb::Color, Internal::T[Array][Integer]],
    "start_color"
  )
  Internal.assert_type!(
    end_color,
    [Osb::Color, Internal::T[Array][Integer]],
    "end_color"
  )

  start_color = Color.new(start_color) if start_color.is_a?(Array)
  end_color = Color.new(end_color) if end_color.is_a?(Array)

  end_time = "" if start_time == end_time
  tabs = " " * self.tab_level
  command =
    "#{tabs}C,#{start_time},#{end_time},#{start_color.r},#{start_color.g},#{start_color.b}"
  if end_color != start_color
    command += ",#{end_color.r},#{end_color.g},#{end_color.b}"
  end
  @commands << command
end

#fade(start_time:, end_time: start_time, easing: Easing::Linear, start_opacity:, end_opacity: start_opacity) ⇒ void

This method returns an undefined value.

Change the opacity of the object (how transparent it is).

Parameters:

  • start_time (Integer)
  • end_time (Integer) (defaults to: start_time)
  • easing (Integer) (defaults to: Easing::Linear)
  • start_opacity (Numeric)
  • end_opacity (Numeric) (defaults to: start_opacity)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/osb/commandable.rb', line 48

def fade(
  start_time:,
  end_time: start_time,
  easing: Easing::Linear,
  start_opacity:,
  end_opacity: start_opacity
)
  self.raise_if_trigger_called!
  Internal.raise_if_invalid_start_time!(start_time)
  Internal.raise_if_invalid_end_time!(end_time)
  Internal.raise_if_invalid_easing!(easing)
  Internal.assert_type!(start_opacity, Numeric, "start_opacity")
  Internal.assert_type!(end_opacity, Numeric, "end_opacity")
  Internal.assert_value!(start_opacity, 0..1, "start_opacity")
  Internal.assert_value!(end_opacity, 0..1, "end_opacity")

  end_time = "" if start_time == end_time
  tabs = " " * self.tab_level
  command = "#{tabs}F,#{start_time},#{end_time},#{start_opacity}"
  command += ",#{end_opacity}" if end_opacity != start_opacity
  @commands << command
end

#flip(start_time:, end_time:, horizontally: true, vertically: false) ⇒ void

This method returns an undefined value.

Flip the object horizontally or vertically.

Parameters:

  • start_time (Integer)
  • end_time (Integer)
  • horizontally (Boolean) (defaults to: true)
  • vertically (Boolean) (defaults to: false)


303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/osb/commandable.rb', line 303

def flip(start_time:, end_time:, horizontally: true, vertically: false)
  self.raise_if_trigger_called!
  Internal.raise_if_invalid_start_time!(start_time)
  Internal.raise_if_invalid_end_time!(end_time)
  Internal.assert_type!(horizontally, Internal::Boolean, "horizontally")
  Internal.assert_type!(vertically, Internal::Boolean, "vertically")

  if horizontally && vertically
    raise InvalidValueError,
          "Cannot flip an object both horizontally and vertically."
  end
  if !horizontally && !vertically
    raise InvalidValueError, "Specify a direction to flip."
  end

  direction = horizontally ? "H" : "V"
  end_time = "" if start_time == end_time
  tabs = " " * self.tab_level
  command = "#{tabs}P,#{start_time},#{end_time},#{direction}"
  @commands << command
end

#move(start_time:, end_time: start_time, easing: Easing::Linear, start_position:, end_position: start_position) ⇒ void

This method returns an undefined value.

Move the object to a new position in the play area.

Parameters:



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
105
106
107
108
109
110
111
# File 'lib/osb/commandable.rb', line 78

def move(
  start_time:,
  end_time: start_time,
  easing: Easing::Linear,
  start_position:,
  end_position: start_position
)
  self.raise_if_trigger_called!
  Internal.raise_if_invalid_start_time!(start_time)
  Internal.raise_if_invalid_end_time!(end_time)
  Internal.raise_if_invalid_easing!(easing)
  Internal.assert_type!(
    start_position,
    [Osb::Vector2, Internal::T[Array][Numeric]],
    "start_position"
  )
  Internal.assert_type!(
    end_position,
    [Osb::Vector2, Internal::T[Array][Numeric]],
    "end_position"
  )
  if start_position.is_a?(Array)
    start_position = Osb::Vector2.new(start_position)
  end
  end_position = Osb::Vector2.new(end_position) if end_position.is_a?(Array)
  end_time = "" if start_time == end_time
  tabs = " " * self.tab_level
  command =
    "#{tabs}M,#{start_time},#{end_time},#{start_position.x},#{start_position.y}"
  if end_position != start_position
    command += ",#{end_position.x},#{end_position.y}"
  end
  @commands << command
end

#move_x(start_time:, end_time: start_time, easing: Easing::Linear, start_x:, end_x: start_x) ⇒ void

This method returns an undefined value.

Move the object along the x axis.

Parameters:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/osb/commandable.rb', line 120

def move_x(
  start_time:,
  end_time: start_time,
  easing: Easing::Linear,
  start_x:,
  end_x: start_x
)
  self.raise_if_trigger_called!
  Internal.raise_if_invalid_start_time!(start_time)
  Internal.raise_if_invalid_end_time!(end_time)
  Internal.raise_if_invalid_easing!(easing)
  Internal.assert_type!(start_x, Numeric, "start_x")
  Internal.assert_type!(end_x, Numeric, "end_x")

  end_time = "" if start_time == end_time
  tabs = " " * self.tab_level
  command = "#{tabs}MX,#{start_time},#{end_time},#{start_x}"
  command += ",#{end_x}" if end_x
  @commands << command
end

#move_y(start_time:, end_time: start_time, easing: Easing::Linear, start_y:, end_y: start_y) ⇒ void

This method returns an undefined value.

Move the object along the y axis.

Parameters:



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/osb/commandable.rb', line 148

def move_y(
  start_time:,
  end_time: start_time,
  easing: Easing::Linear,
  start_y:,
  end_y: start_y
)
  self.raise_if_trigger_called!
  Internal.raise_if_invalid_start_time!(start_time)
  Internal.raise_if_invalid_end_time!(end_time) if end_time
  Internal.raise_if_invalid_easing!(easing)
  Internal.assert_type!(start_y, Numeric, "start_y")
  Internal.assert_type!(end_y, Numeric, "end_y")

  end_time = "" if start_time == end_time
  tabs = " " * self.tab_level
  command = "#{tabs}MY,#{start_time},#{end_time},#{start_y}"
  command += ",#{end_y}" if end_y != start_y
  @commands << command
end

#rotate(start_time:, end_time: start_time, easing: Easing::Linear, start_angle:, end_angle: start_angle) ⇒ void

This method returns an undefined value.

Rotate the object around its origin.

Parameters:

  • start_time (Integer)
  • end_time (Integer) (defaults to: start_time)
  • easing (Integer) (defaults to: Easing::Linear)
  • start_angle (Float)

    start angle in radians.

  • end_angle (Float) (defaults to: start_angle)

    end angle in radians.



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/osb/commandable.rb', line 234

def rotate(
  start_time:,
  end_time: start_time,
  easing: Easing::Linear,
  start_angle:,
  end_angle: start_angle
)
  self.raise_if_trigger_called!
  Internal.raise_if_invalid_start_time!(start_time)
  Internal.raise_if_invalid_end_time!(end_time)
  Internal.raise_if_invalid_easing!(easing)
  Internal.assert_type!(start_angle, Numeric, "start_angle")
  Internal.assert_type!(end_angle, Numeric, "end_angle")

  end_time = "" if start_time == end_time
  tabs = " " * self.tab_level
  command = "#{tabs}R,#{start_time},#{end_time},#{start_angle}"
  command += ",#{end_angle}" if end_angle != start_angle
  @commands << command
end

#scale(start_time:, end_time: start_time, easing: Easing::Linear, start_scale:, end_scale: start_scale) ⇒ void

This method returns an undefined value.

Change the size of the object relative to its original size. Will scale seperatedly if given Osb::Vector2s or Array<Numeric>s. The scaling is affected by the object’s origin

Parameters:



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/osb/commandable.rb', line 178

def scale(
  start_time:,
  end_time: start_time,
  easing: Easing::Linear,
  start_scale:,
  end_scale: start_scale
)
  self.raise_if_trigger_called!
  Internal.raise_if_invalid_start_time!(start_time)
  Internal.raise_if_invalid_end_time!(end_time) if end_time
  Internal.raise_if_invalid_easing!(easing)
  Internal.assert_type!(
    start_scale,
    [Numeric, Internal::T[Array][Numeric], Osb::Vector2],
    "start_scale"
  )
  Internal.assert_type!(
    end_scale,
    [Numeric, Internal::T[Array][Numeric], Osb::Vector2],
    "end_scale"
  )

  end_time = "" if start_time == end_time
  tabs = " " * self.tab_level

  if start_scale.is_a?(Numeric)
    unless end_scale.is_a?(Numeric)
      raise InvalidValueError,
            "start_scale and end_scale must be either both Numeric values or Vector2-like values."
    end
    command = "#{tabs}S,#{start_time},#{end_time},#{start_scale}"
    command += ",#{end_scale}" if end_scale != start_scale
    @commands << command
  else
    if end_scale.is_a?(Numeric)
      raise InvalidValueError,
            "start_scale and end_scale must be either both Numeric values or Vector2-like values."
    end

    start_scale = Osb::Vector2.new(start_scale) if start_scale.is_a?(Array)
    end_scale = Osb::Vector2.new(end_scale) if end_scale.is_a?(Array)

    command =
      "#{tabs}V,#{start_time},#{end_time},#{start_scale.x},#{start_scale.y}"
    command += ",#{end_scale.x},#{end_scale.y}" if end_scale
    @commands << command
  end
end

#trigger(on:, start_time:, end_time:) {|_self| ... } ⇒ void

This method returns an undefined value.

Add a group of commands on a specific condition. ‘#trigger` can only be called on an empty object declaration (no commands). Pass a block to this method call to specify which commands to run if the condition is met.

In addition to the “implicit” player feedback via the separate Pass/Fail layers, you can use one of several Trigger conditions to cause a series of events to happen whenever that condition is fulfilled within a certain time period. Note that ‘start_time` and `end_time` of any commands called inside the block become relative to the `start_time` and `end_time` of the `#trigger` command.

While osu! supports trigger on hitsounds playing, we decided to not include it in because it is unreliable/unpredictable.

Examples:

img.trigger(on: "Passing", start_time: 0, end_time: 1000) do
  img.fade(start_time: 0, start_opacity: 0.5)
end

Parameters:

  • on (String)

    indicates the trigger condition. It can be “Failing” or “Passing”.

  • start_time (Integer)

    the timestamp at which the trigger becomes valid.

  • end_time (Integer)

    the timestamp at which the trigger stops being valid.

Yields:

  • (_self)

Yield Parameters:



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/osb/commandable.rb', line 364

def trigger(on:, start_time:, end_time:, &blk)
  self.raise_if_trigger_called!
  Internal.raise_if_invalid_start_time!(start_time)
  Internal.raise_if_invalid_end_time!(end_time)
  Internal.assert_type!(on, String, "on")
  Internal.assert_value!(on, %w[Passing Failing], "on")

  unless block_given?
    raise InvalidValueError, "Do not use an empty trigger."
  end

  if @commands.size > 1
    raise RuntimeError, "Do not call #trigger after any other commands."
  end

  if @is_in_trigger
    raise RuntimeError,
          "Do not call #trigger inside another #trigger block."
  end

  command = " T,#{on},#{start_time},#{end_time}"
  @commands << command

  @is_in_trigger = true
  yield self
  unless @commands.size > 1
    raise InvalidValueError, "Do not use an empty trigger."
  end
  @is_in_trigger = false
  @trigger_called = true
end