Class: SparkleMotion::LaunchPad::Widget

Inherits:
Object
  • Object
show all
Defined in:
lib/sparkle_motion/launch_pad/widget.rb

Overview

Base class for Launchpad UI widgets.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(launchpad:, x: nil, y: nil, position: nil, width:, height:, on:, off:, down:, value:) ⇒ Widget

TODO: Use ‘Vector2` for position/size…



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sparkle_motion/launch_pad/widget.rb', line 9

def initialize(launchpad:, x: nil, y: nil, position: nil, width:, height:, on:, off:, down:,
               value:)
  @x          = x
  @y          = y
  @position   = position
  @width      = width
  @height     = height
  @launchpad  = launchpad
  @value      = value
  @pressed    = {}
  set_colors!(on, off, down)
  attach_handler!
end

Instance Attribute Details

#downObject

Returns the value of attribute down.



6
7
8
# File 'lib/sparkle_motion/launch_pad/widget.rb', line 6

def down
  @down
end

#heightObject (readonly)

Returns the value of attribute height.



5
6
7
# File 'lib/sparkle_motion/launch_pad/widget.rb', line 5

def height
  @height
end

#offObject

Returns the value of attribute off.



6
7
8
# File 'lib/sparkle_motion/launch_pad/widget.rb', line 6

def off
  @off
end

#onObject

Returns the value of attribute on.



6
7
8
# File 'lib/sparkle_motion/launch_pad/widget.rb', line 6

def on
  @on
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/sparkle_motion/launch_pad/widget.rb', line 5

def value
  @value
end

#widthObject (readonly)

Returns the value of attribute width.



5
6
7
# File 'lib/sparkle_motion/launch_pad/widget.rb', line 5

def width
  @width
end

#xObject (readonly)

Returns the value of attribute x.



5
6
7
# File 'lib/sparkle_motion/launch_pad/widget.rb', line 5

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



5
6
7
# File 'lib/sparkle_motion/launch_pad/widget.rb', line 5

def y
  @y
end

Instance Method Details

#blankObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sparkle_motion/launch_pad/widget.rb', line 41

def blank
  black = SparkleMotion::LaunchPad::Color::BLACK.to_h
  if @x
    (0..max_x).each do |xx|
      (0..max_y).each do |yy|
        change_grid(x: xx, y: yy, color: black)
      end
    end
  else
    change_command(position: @position, color: black)
  end
end

#max_vObject



54
55
56
57
58
59
60
# File 'lib/sparkle_motion/launch_pad/widget.rb', line 54

def max_v
  if @x
    @max_v ||= (height * width) - 1
  else
    1
  end
end

#renderObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sparkle_motion/launch_pad/widget.rb', line 29

def render
  @pressed.map do |idx, state|
    next unless state
    if @x
      xx, yy = coords_for(idx: idx)
      change_grid(x: xx, y: yy, color: down)
    else
      change_command(position: @position, color: down)
    end
  end
end

#update(value, render_now = true) ⇒ Object



23
24
25
26
27
# File 'lib/sparkle_motion/launch_pad/widget.rb', line 23

def update(value, render_now = true)
  @value = value
  @value = max_v if max_v && @value && @value > max_v
  render if render_now
end