Class: Reight::SoundEditor::Canvas

Inherits:
Object
  • Object
show all
Includes:
Hookable
Defined in:
lib/reight/app/sound/canvas.rb

Constant Summary collapse

SEQUENCE_LEN =
32
NOTE_HEIGHT =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hookable

#hook

Constructor Details

#initialize(app) ⇒ Canvas

Returns a new instance of Canvas.



11
12
13
14
15
16
# File 'lib/reight/app/sound/canvas.rb', line 11

def initialize(app)
  hook :sound_changed

  @app, @sound = app, app.project.sounds.first
  @scrolly     = NOTE_HEIGHT * Reight::Sound::Note::MAX / 3
end

Instance Attribute Details

#soundObject

Returns the value of attribute sound.



20
21
22
# File 'lib/reight/app/sound/canvas.rb', line 20

def sound
  @sound
end

#toneObject

Returns the value of attribute tone.



18
19
20
# File 'lib/reight/app/sound/canvas.rb', line 18

def tone
  @tone
end

#toolObject

Returns the value of attribute tool.



18
19
20
# File 'lib/reight/app/sound/canvas.rb', line 18

def tool
  @tool
end

Instance Method Details

#begin_editing(&block) ⇒ Object



31
32
33
34
35
36
# File 'lib/reight/app/sound/canvas.rb', line 31

def begin_editing(&block)
  @app.history.begin_grouping
  block.call if block
ensure
  end_editing if block
end

#delete(x, y) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/reight/app/sound/canvas.rb', line 63

def delete(x, y)
  time_i, note_i = note_pos_at x, y
  note           = @sound.at time_i, note_i
  return unless note

  remove_note time_i, note_i, note.tone

  @app.flash note_name y
end

#end_editingObject



38
39
40
41
# File 'lib/reight/app/sound/canvas.rb', line 38

def end_editing()
  @app.history.end_grouping
  save
end

#note_pos_at(x, y) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/reight/app/sound/canvas.rb', line 43

def note_pos_at(x, y)
  sp         = sprite
  notew      = sp.w / SEQUENCE_LEN
  max        = Reight::Sound::Note::MAX
  time_index = (x / notew).floor
  note_index = max - ((@scrolly + y) / NOTE_HEIGHT).floor
  return time_index, note_index.clamp(0, max)
end

#put(x, y) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/reight/app/sound/canvas.rb', line 52

def put(x, y)
  time_i, note_i = note_pos_at x, y

  @sound.each_note(time_index: time_i)
    .select {|note,| (note.index == note_i) != (note.tone == tone)}
    .each   {|note,| remove_note time_i, note.index, note.tone}
  add_note time_i, note_i, tone

  @app.flash note_name y
end

#saveObject



27
28
29
# File 'lib/reight/app/sound/canvas.rb', line 27

def save()
  @app.project.save
end

#spriteObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/reight/app/sound/canvas.rb', line 73

def sprite()
  @sprite ||= RubySketch::Sprite.new.tap do |sp|
    pos = -> {return sp.mouse_x, sp.mouse_y}
    sp.draw           {draw}
    sp.mouse_pressed  {mouse_pressed( *pos.call, sp.mouse_button)}
    sp.mouse_released {mouse_released(*pos.call, sp.mouse_button)}
    sp.mouse_moved    {mouse_moved(   *pos.call)}
    sp.mouse_dragged  {mouse_dragged( *pos.call, sp.mouse_button)}
    sp.mouse_clicked  {mouse_clicked( *pos.call, sp.mouse_button)}
  end
end