Class: Canvas_Control

Inherits:
Object
  • Object
show all
Includes:
Logic_Controls
Defined in:
lib/midinous/canvas.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logic_Controls

#check_bounds, #color_to_hex, #draw_chevron, #hex_to_color, #pos_box, #relative_pos, #round_num_to_grid, #round_to_grid, #set_point_speed, #sync_diff

Constructor Details

#initializeCanvas_Control

Returns a new instance of Canvas_Control.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/midinous/canvas.rb', line 26

def initialize
  @mouse_last_pos = nil
  @sel_box      = nil
  @selecting    = false
  @sel_white    = [0.8,0.8,0.8,0.1]  #selection box colors

  @sel_blue     = [0,0.5,1,0.5]      #selection box colors

  @point_origin = nil
  @path_origin  = nil
  @point_move   = nil
  @dragging     = false
  @diff         = [0,0]
  @nouspoints   = []
  @travelers    = []
  @starters     = []
  @repeaters    = []
  @scale        = "Chromatic"
  @root_note    = 60
  @scale_notes  = []
  @scale_posns  = []
  set_scale(@scale,@root_note)
  @tempo        = 120.000
  @midi_sync    = 0.000
  @path_sourced = false
  @attempt_path = false
  @ms_per_tick  = 125.000 #default tempo of 120bpm

  @beats        = 4 #number of beats in a whole note -- should be reasonably between 1 and 16

  @beat_note    = 4 #as a fraction of a whole note   -- should be between 2 and 16 via powers of 2

  @grid_spacing = 35
  @queued_note_plays = []
  @queued_note_stops = []
end

Instance Attribute Details

#beat_noteObject

Returns the value of attribute beat_note.



22
23
24
# File 'lib/midinous/canvas.rb', line 22

def beat_note
  @beat_note
end

#beatsObject

Returns the value of attribute beats.



22
23
24
# File 'lib/midinous/canvas.rb', line 22

def beats
  @beats
end

#draggingObject (readonly)

Returns the value of attribute dragging.



24
25
26
# File 'lib/midinous/canvas.rb', line 24

def dragging
  @dragging
end

#grid_spacingObject (readonly)

Returns the value of attribute grid_spacing.



24
25
26
# File 'lib/midinous/canvas.rb', line 24

def grid_spacing
  @grid_spacing
end

#midi_syncObject (readonly)

Returns the value of attribute midi_sync.



24
25
26
# File 'lib/midinous/canvas.rb', line 24

def midi_sync
  @midi_sync
end

#mouse_last_posObject (readonly)

Returns the value of attribute mouse_last_pos.



24
25
26
# File 'lib/midinous/canvas.rb', line 24

def mouse_last_pos
  @mouse_last_pos
end

#ms_per_tickObject (readonly)

Returns the value of attribute ms_per_tick.



24
25
26
# File 'lib/midinous/canvas.rb', line 24

def ms_per_tick
  @ms_per_tick
end

#nouspointsObject

Returns the value of attribute nouspoints.



22
23
24
# File 'lib/midinous/canvas.rb', line 22

def nouspoints
  @nouspoints
end

#queued_note_playsObject

Returns the value of attribute queued_note_plays.



22
23
24
# File 'lib/midinous/canvas.rb', line 22

def queued_note_plays
  @queued_note_plays
end

#queued_note_stopsObject

Returns the value of attribute queued_note_stops.



22
23
24
# File 'lib/midinous/canvas.rb', line 22

def queued_note_stops
  @queued_note_stops
end

#repeatersObject

Returns the value of attribute repeaters.



22
23
24
# File 'lib/midinous/canvas.rb', line 22

def repeaters
  @repeaters
end

#root_noteObject

Returns the value of attribute root_note.



22
23
24
# File 'lib/midinous/canvas.rb', line 22

def root_note
  @root_note
end

#scaleObject

Returns the value of attribute scale.



22
23
24
# File 'lib/midinous/canvas.rb', line 22

def scale
  @scale
end

#scale_posnsObject (readonly)

Returns the value of attribute scale_posns.



24
25
26
# File 'lib/midinous/canvas.rb', line 24

def scale_posns
  @scale_posns
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



24
25
26
# File 'lib/midinous/canvas.rb', line 24

def start_time
  @start_time
end

#tempoObject

Returns the value of attribute tempo.



22
23
24
# File 'lib/midinous/canvas.rb', line 22

def tempo
  @tempo
end

#travelersObject

Returns the value of attribute travelers.



22
23
24
# File 'lib/midinous/canvas.rb', line 22

def travelers
  @travelers
end

Instance Method Details

#canvas_bg_draw(cr) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/midinous/canvas.rb', line 298

def canvas_bg_draw(cr)
  # fill background with black

  cr.set_source_rgb(BLACK)
  cr.paint
  cr.set_source_rgb(BLUGR)
  x = @grid_spacing
  while x < CANVAS_SIZE
    y = @grid_spacing
    while y < CANVAS_SIZE
      cr.circle(x,y,1)
      cr.fill
      y += @grid_spacing
    end
    x += @grid_spacing
  end
  #Handle measure drawing via notches on paths instead of this, maybe

  cr.set_source_rgba(DGREY)
  x = @grid_spacing
  y = CANVAS_SIZE - @grid_spacing
  while x < CANVAS_SIZE
    cr.move_to(x,@grid_spacing)
    cr.line_to(x,CANVAS_SIZE-@grid_spacing)
    cr.set_line_width(1)
    cr.stroke
    x += @grid_spacing*@beats
  end
  y = @grid_spacing
  x = CANVAS_SIZE - @grid_spacing
  while y < CANVAS_SIZE
    cr.move_to(@grid_spacing,y)
    cr.line_to(CANVAS_SIZE-@grid_spacing,y)
    cr.set_line_width(1)
    cr.stroke
    y += @grid_spacing*@beats
  end
end

#canvas_drag(obj, event) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/midinous/canvas.rb', line 251

def canvas_drag(obj,event)
  @dragging = false
  @mouse_last_pos = [event.x,event.y]
  case
    when (@selecting && @sel_box)
      @dragging = true
      @sel_box[2] = event.x
      @sel_box[3] = event.y
      obj.queue_draw
    when (@point_origin)
      @dragging = true
      @point_origin[0] = event.x
      @point_origin[1] = event.y
    when (@point_move)
      @dragging = true
      # difference in movement of the point, cumulative until mouse released

      @diff = round_to_grid([(event.x - @point_move[0]) , (event.y - @point_move[1])])
      @point_move[2] = event.x
      @point_move[3] = event.y
      obj.queue_draw
  end
end

#canvas_draw(cr) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/midinous/canvas.rb', line 335

def canvas_draw(cr)
  canvas_bg_draw(cr)
  case #These draw events are for in-progress/temporary activities

  when(@sel_box)  
    width  = @sel_box[2] - @sel_box[0]
    height = @sel_box[3] - @sel_box[1]
    
    cr.set_source_rgba(@sel_blue)
    cr.rectangle(@sel_box[0],@sel_box[1],width,height)
    cr.set_line_width(2)
    cr.stroke
  when(@point_move)
    start_coord = [@point_move[0],@point_move[1]]
    end_coord   = [@point_move[2],@point_move[3]]
    start_coord = round_to_grid(start_coord)
    end_coord   = round_to_grid(end_coord)
    
    cr.move_to(start_coord[0],start_coord[1])
    cr.set_source_rgba(RED)
    cr.line_to(end_coord[0],end_coord[1])
    cr.set_line_width(2)
    cr.stroke
    cr.rounded_rectangle(end_coord[0]-10,end_coord[1]-10,20,20,2,2)
    cr.set_line_width(2)
    cr.stroke
  end
  
  #Set the scene if the current tool does not permit a style

  case Active_Tool.tool_id
  when 1
    @nouspoints, @path_sourced = Pl.cancel_path(@nouspoints) 
  when 2
    @nouspoints, @path_sourced = Pl.cancel_path(@nouspoints)
  when 3
    @nouspoints, @path_sourced = Pl.cancel_path(@nouspoints)
  when 4
    @nouspoints = Pl.cancel_selected(@nouspoints)
  end

  #Draw all the points and paths last

  @nouspoints.each        { |n| n.set_path_color }
  @nouspoints.each        { |n| n.path_draw(cr) }
  @nouspoints.each        { |n| n.caret_draw(cr) }
  @nouspoints.each        { |n| n.draw(cr) }
  
end

#canvas_generic(string) ⇒ Object

Used as a pseudo-handler between classes



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/midinous/canvas.rb', line 93

def canvas_generic(string) #Used as a pseudo-handler between classes

  case string
  when "path" 
    if !@nouspoints.empty? && @nouspoints.find_all(&:pathable).any?
      @nouspoints = Pl.add_path(@nouspoints)
      @nouspoints, @path_sourced = Pl.cancel_path(@nouspoints)
      UI::canvas.queue_draw
    end
  when "prop"
    @nouspoints = Pl.modify_properties(@nouspoints)
    Pl.populate_prop(@nouspoints)
    UI::canvas.queue_draw
  end
end

#canvas_grid_change(dir) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/midinous/canvas.rb', line 208

def canvas_grid_change(dir)
  prev_beat_note = @beat_note
  case dir
  when "+"
    @beats += 1
    @beats = 16 if @beats > 16
  when "-"
    @beats -= 1
    @beats = 1 if @beats < 1
  when "++"
    @beat_note *= 2
    @beat_note = 16 if @beat_note > 16
  when "--"
    @beat_note /= 2
    @beat_note = 2      if @beat_note < 2
  end
  if @beat_note != prev_beat_note
    case dir
    when "++"
      @ms_per_tick /= 2
    when "--"
      @ms_per_tick *= 2
    end
  end
  UI::t_sig.text = "#{@beats}/#{@beat_note}"
  UI::canvas.queue_draw
end

#canvas_playObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/midinous/canvas.rb', line 108

def canvas_play
  
  if @nouspoints.find(&:traveler_start)
    @playing = true
    @nouspoints.find_all {|n| n.path_to.length > 1}.each {|p| p.path_to.each {|e| p.path_to_memory << e}}
    UI::play.sensitive = false
    UI::stop.sensitive = true
  end

  @nouspoints.find_all(&:traveler_start).each do |n|
    @starters << Starter.new(nil,n,nil)
    UI::canvas.queue_draw
    @queued_note_plays.each {|o| o.play}
    signal_chain(n,n.note)
  end
  @stored_time = Time.now.to_f*1000
  canvas_timeout(@ms_per_tick) #Start sequence

end

#canvas_press(obj, event) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/midinous/canvas.rb', line 236

def canvas_press(obj,event)
  UI::logic_controls.focus = true
  case Active_Tool.tool_id
    when 1
      @sel_box   = [event.x,event.y,event.x,event.y]
      @selecting   = true
    when 2
      @point_origin = [event.x,event.y]
    when 3
      @point_move = [event.x,event.y,event.x,event.y]
    when 4
      @path_origin = [event.x,event.y]
  end
  obj.queue_draw
end

#canvas_release(obj, event) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/midinous/canvas.rb', line 273

def canvas_release(obj,event)
  @dragging = false
  case Active_Tool.tool_id
    when 1
      unless !@sel_box
        @sel_box = pos_box(@sel_box)
        @nouspoints = Pl.select_points(@sel_box,@nouspoints)
        @sel_box = nil
        @selecting = false
      end
    when 2 #Add a point where/when the tool is released

      unless !@point_origin
        @nouspoints = Pl.add_point(round_to_grid(@point_origin),@nouspoints)
        @point_origin = nil
      end
    when 3 #move point(s) designated by the move stencil

      @nouspoints = Pl.move_points(@diff,@nouspoints)
      @point_move = nil
      @diff = [0,0]
    when 4 #select singular point

      @nouspoints, @path_sourced = Pl.select_path_point(@path_origin,@nouspoints,@path_sourced)
  end
  obj.queue_draw
end

#canvas_stopObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/midinous/canvas.rb', line 161

def canvas_stop
  @playing = false
  @starters  = []
  @repeaters = []
  @queued_note_plays = []
  @queued_note_stops = []
  @nouspoints.find_all {|n| n.path_to.length > 1}.each {|p| p.reset_path_to}
  UI::canvas.queue_draw
  @nouspoints.each do |n|
    n.playing = false
    n.repeating = false
    Pm.note_rlse(n.channel,n.note) unless n.note.to_s.include?("+") || n.note.to_s.include?("-")
  end
  @travelers.each do |t|
    Pm.note_rlse(t.dest.channel,t.played_note) unless t.played_note == nil
  end
  @travelers = []
  UI::stop.sensitive = false
  UI::play.sensitive = true
end

#canvas_timeout(ms) ⇒ Object



127
128
129
130
131
132
# File 'lib/midinous/canvas.rb', line 127

def canvas_timeout(ms)
  GLib::Timeout.add(ms) do
    UI::canvas.signal_emit('travel-event') unless !@playing
    false
  end
end

#canvas_travelObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/midinous/canvas.rb', line 134

def canvas_travel
  @queued_note_plays = []
  canvas_stop if !@playing               || 
                 (@travelers.length == 0 && 
                  @starters.length  == 0 && 
                  @repeaters.length == 0)
  
  @starters.each  {|s| s.travel}
  @travelers.each {|t| t.travel}
  @travelers.find_all(&:reached).each do |t|
    signal_chain(t.dest,t.played_note) #Pass the last played note here. Gather the played note from the first traveler creation

    t.reached = false
  end
  @repeaters.each {|r| r.repeat}
  
  @queued_note_stops.each {|n| n.stop}
  @queued_note_plays.each {|n| n.play}
  @starters.reject!(&:remove)
  @travelers.reject!(&:remove)
  @repeaters.reject!(&:remove)
  @queued_note_stops = []

  canvas_timeout(sync_diff(@stored_time))
  @stored_time += @ms_per_tick
  UI::canvas.queue_draw
end

#set_scale(scale_text, root) ⇒ Object



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
# File 'lib/midinous/canvas.rb', line 62

def set_scale(scale_text,root)
  @scale = scale_text
  @root_note = root
  scale = SCALES[scale_text]
  slen = scale.length
  @scale_notes = []
  @scale_notes << root
  
  c = 0
  note = root
  while note < 127
    note += scale[c]
    @scale_notes << note unless note > 127
    c = (c + 1) % slen
  end

  c = 0
  note = root
  while note > 0
    note -= scale.reverse[c]
    @scale_notes << note unless note < 0
    c = (c + 1) % slen
  end
  @scale_notes.sort!
  
  (0..127).each do |num|
    @scale_posns[num] = false
    @scale_posns[num] = true if @scale_notes.find {|f| f == num} != nil
  end
end

#set_tempo(tempo) ⇒ Object



58
59
60
61
# File 'lib/midinous/canvas.rb', line 58

def set_tempo(tempo)
  @tempo = tempo
  @ms_per_tick = (1000 * (15 / @tempo)) / (@beat_note / 4)
end

#signal_chain(point, pn) ⇒ Object

pn = played note



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
# File 'lib/midinous/canvas.rb', line 182

def signal_chain(point,pn) #pn = played note

  case point.play_modes[0]
  when "robin"
    p = point.path_to.first
    if p
      @travelers << Traveler.new(point,p,pn)
      point.path_to.rotate!
    end
  when "split"
    point.path_to.each {|p| @travelers << Traveler.new(point,p,pn)}
  when "portal"
    point.path_to.each do |p|
      @starters << Starter.new(point,p,pn)
      UI::canvas.queue_draw
      #@queued_note_plays.each {|o| o.play}

      signal_chain(p,pn)
    end
  when "random"
    p = point.path_to.sample
    if p
      @travelers << Traveler.new(point,p,pn)
      point.path_to.rotate!(rand(point.path_to.length))
    end
  end
end