Class: Point_Logic

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

Overview

Copyright © 2019 James “Nornec” Ratliff

This file is part of Midinous

Midinous is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Midinous is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Midinous.  If not, see <https://www.gnu.org/licenses/>.

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

#initializePoint_Logic

Returns a new instance of Point_Logic.



21
22
23
24
25
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
# File 'lib/midinous/points.rb', line 21

def initialize
	@prop_names       = ["Note",
									  	 "Velocity",
									  	 "Duration (beats)",
									  	 "Channel",
									  	 "Repeat",
									  	 "X-coordinate",
									  	 "Y-coordinate",
									  	 "Color",
									  	 "Path Mode",
									  	 "Signal Start",
									  	 "Play Mode"]
	@prop_names_multi = ["Note",
	                     "Velocity",
											 "Duration (beats)",
											 "Channel",
											 "Repeat",
											 "Color",
											 "Signal Start",
											 "Play Mode"]
	@prop_names_adv = []
	@prop_names_adv_multi = []
	@curr_prop = nil
	@curr_prop_adv = nil
	@mempoints = []
	@mempointsbuff = []
	@copy_pos = []
	@copy_type = nil
	@paste_count = 0
end

Instance Method Details

#add_path(points) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/midinous/points.rb', line 59

def add_path(points)
	points.find_all { |n| n.pathable && !n.source}.each do |t| 
		points.find(&:source).path_to << t
		t.path_from << points.find(&:source)
	end
	return points
end

#add_point(r_origin, points) ⇒ Object

Point existence search



52
53
54
55
56
57
# File 'lib/midinous/points.rb', line 52

def add_point(r_origin,points) #Point existence search
	unless (collision_check(r_origin,points))
		points << NousPoint.new(r_origin,-1)
	end
	return points
end

#cancel_path(points) ⇒ Object



407
408
409
410
# File 'lib/midinous/points.rb', line 407

def cancel_path(points)
	points.find_all(&:pathable).each { |n| n.path_unset }
	return points, false
end

#cancel_selected(points) ⇒ Object



403
404
405
406
# File 'lib/midinous/points.rb', line 403

def cancel_selected(points)
	points.find_all(&:selected).each { |n| n.deselect }
	return points
end

#check_input(text) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
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
296
297
298
299
300
301
302
303
# File 'lib/midinous/points.rb', line 244

def check_input(text)
	play_modes = ["robin","split","portal","random"]
	path_modes = ["horz","vert"]
	signal_states = ["true","false"]
	case @curr_prop
		when "Note"
			if (text.to_i >= 1 && text.to_i <= 127) || (text.match(/^([+]|-)[0-9]{1,2}$/))
					 UI::prop_mod_button.sensitive = true
			else UI::prop_mod_button.sensitive = false
			end
		when "Velocity"
			if (text.to_i >= 1 && text.to_i <= 127)
					 UI::prop_mod_button.sensitive = true
			else UI::prop_mod_button.sensitive = false
			end
		when "Duration (beats)"
			if text.to_i >= 1 && text.to_i <= 1000
					 UI::prop_mod_button.sensitive = true
			else UI::prop_mod_button.sensitive = false
			end
		when "Channel"
			if text.to_i >= 1 && text.to_i <= 16
				   UI::prop_mod_button.sensitive = true
			else UI::prop_mod_button.sensitive = false
			end
		when "X-coordinate", "Y-coordinate"
			if text.to_i >= CC.grid_spacing && 
			   text.to_i <= (CANVAS_SIZE - CC.grid_spacing)
			then
					 UI::prop_mod_button.sensitive = true
			else UI::prop_mod_button.sensitive = false
			end
		when "Color"
			if text.match(/^[0-9A-Fa-f]{6}$/)
				   UI::prop_mod_button.sensitive = true
			else UI::prop_mod_button.sensitive = false
			end
		when "Path Mode"
			if path_modes.include? text
					 UI::prop_mod_button.sensitive = true
			else UI::prop_mod_button.sensitive = false
			end
		when "Signal Start"
			if signal_states.include? text
				   UI::prop_mod_button.sensitive = true
			else UI::prop_mod_button.sensitive = false
			end
		when "Play Mode"
			if play_modes.include? text
				   UI::prop_mod_button.sensitive = true
			else UI::prop_mod_button.sensitive = false
			end
		when "Repeat"
			if text.to_i >= 0 && text.to_i <= 128
					 UI::prop_mod_button.sensitive = true
			else UI::prop_mod_button.sensitive = false
			end
		else UI::prop_mod_button.sensitive = false
	end
end

#collision_check(r_origin, points) ⇒ Object



97
98
99
# File 'lib/midinous/points.rb', line 97

def collision_check(r_origin,points)
		return true if points.any? { |n| r_origin == n.origin }
end

#copy_points(type) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/midinous/points.rb', line 125

def copy_points (type)
	return if CC.nouspoints.empty?
	origins = []
	CC.nouspoints.find_all(&:selected).each {|n| origins << n.origin}
	@copy_pos = origins.min
	@copy_type = type
	@mempointsbuff = CC.nouspoints.find_all(&:selected)
	@paste_count = 0
end

#delete_paths_from(points) ⇒ Object



429
430
431
432
433
# File 'lib/midinous/points.rb', line 429

def delete_paths_from(points)
	points.find_all {|f| !f.path_from.length.zero? && f.selected == true}.each {|n| n.path_from.each {|b| b.path_to.reject! {|g| g == n }}}
	points.find_all {|f| !f.path_from.length.zero? && f.selected == true}.each {|n| n.path_from = []}
	UI::canvas.queue_draw
end

#delete_paths_to(points) ⇒ Object



421
422
423
424
425
426
427
428
# File 'lib/midinous/points.rb', line 421

def delete_paths_to(points)
	points.find_all {|f| !f.path_to.length.zero? && f.selected == true}.each {|n| n.path_to.each {|b| b.path_from.reject! {|g| g == n }}}
	points.find_all {|f| !f.path_to.length.zero? && f.selected == true}.each do |n| 
	  n.path_to = []
		play_mode_rotate(1)
  end
	UI::canvas.queue_draw
end

#delete_points(points) ⇒ Object



412
413
414
415
416
417
418
419
420
# File 'lib/midinous/points.rb', line 412

def delete_points(points)
	points.find_all {|f| !f.path_to.length.zero?}.each   {|n| n.path_to.reject!(&:selected)}
	points.find_all {|f| !f.path_from.length.zero?}.each {|n| n.path_from.reject!(&:selected)}
	points.reject!(&:selected)
	UI::prop_list_model.clear
	UI::prop_mod.text = ""
	UI::canvas.queue_draw
	return points
end

#inc_note(inc) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/midinous/points.rb', line 80

def inc_note(inc)
	CC.nouspoints.find_all(&:selected).each do |n|
		signed = n.note
		val = n.note.to_i
		val += inc
		if val >= 0
			n.note = "+#{val}"
		else n.note = val
		end
		if !(signed.to_s.include?("+") || signed.to_s.include?("-"))
			n.note = n.note.to_i
			n.note = n.note.clamp(0,127)
		end
	end
	populate_prop(CC.nouspoints)
end

#modify_properties(points) ⇒ Object



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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/midinous/points.rb', line 305

def modify_properties(points)
	case @curr_prop
		when "Note"
			points.find_all(&:selected).each do |p| 
				if UI::prop_mod.text.match(/^([+]|-)[0-9]{1,2}$/)
					unless p.traveler_start == true
						p.note = UI::prop_mod.text
						p.use_rel = true
					end
				elsif (UI::prop_mod.text.to_i >= 0 &&
				       UI::prop_mod.text.to_i <= 127 &&
							!(UI::prop_mod.text.include?("+") || UI::prop_mod.text.include?("-")))
				then
					p.note = UI::prop_mod.text.to_i
					p.use_rel = false
				end
			end
		when "Velocity"
			points.find_all(&:selected).each {|p| p.velocity = UI::prop_mod.text.to_i}
		when "Duration (beats)"
			points.find_all(&:selected).each {|p| p.duration = UI::prop_mod.text.to_i}
		when "Channel"
			points.find_all(&:selected).each {|p| p.channel = UI::prop_mod.text.to_i}
		when "X-coordinate"
			points.find(&:selected).x = round_num_to_grid(UI::prop_mod.text.to_i)
		when "Y-coordinate"
			points.find(&:selected).y = round_num_to_grid(UI::prop_mod.text.to_i)
		when "Color"
			points.find_all(&:selected).each {|p| p.set_default_color(hex_to_color("##{UI::prop_mod.text}"))}
		when "Path Mode"
			points.find(&:selected).path_mode = UI::prop_mod.text
		when "Signal Start"
			case UI::prop_mod.text
			when "true"
				points.find_all(&:selected).each {|p| p.traveler_start = true}
			when "false"
				points.find_all(&:selected).each {|p| p.traveler_start = false}
			end
		when "Play Mode"
			if UI::prop_mod.text == "robin" || UI::prop_mod.text == "portal"
				points.find_all(&:selected).each {|p| p.play_modes.rotate! until p.play_modes[0] == UI::prop_mod.text}
			else
				points.find_all {|p| p.selected == true && p.path_to.length > 1}.each {|p| p.play_modes.rotate! until p.play_modes[0] == UI::prop_mod.text}
			end
		when "Repeat"
			points.find_all(&:selected).each do |p| 
				p.repeat = UI::prop_mod.text.to_i
			end
	end
	return points
end

#move_check(diff, points) ⇒ Object



442
443
444
445
446
447
448
449
450
# File 'lib/midinous/points.rb', line 442

def move_check(diff,points)
	points.find_all(&:selected).each do |n|
		dest = n.origin.map
		dest = dest.to_a
		dest.map! {|g| g += diff[dest.find_index(g)]}
		return false if points.find_all(&:not_selected).any? { |g| g.origin == dest}
	end
	return true
end

#move_points(diff, points) ⇒ Object



435
436
437
438
439
440
441
# File 'lib/midinous/points.rb', line 435

def move_points(diff,points)
	if move_check(diff,points)
		points.find_all(&:selected).each {|n| n.set_destination(diff) }	
		populate_prop(points)			
	end
	return points
end

#paste_check(diff, memp) ⇒ Object



172
173
174
175
# File 'lib/midinous/points.rb', line 172

def paste_check(diff,memp)
	memp.each {|m| return false if CC.nouspoints.any? { |n| n.origin == m.origin}}
	return true
end

#paste_pointsObject



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
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/midinous/points.rb', line 134

def paste_points
	return if @mempointsbuff.empty?
	copy_lookup = {}
	
	# Clone the cut copy of pasted points if someone wants to cutpaste more than once
	# Then turn the cut operation into the copy operation
	copy_points("copy") if @paste_count == 1

	# Clone the points and track old point => new point
	@mempointsbuff.each do |n|
		n.selected = false if @copy_type == "copy"
		mem_point = n.clone
		@mempoints << mem_point
		copy_lookup[n.object_id] = mem_point
	end

	# Update the point relations
	@mempoints.each do |n|
		n.path_to   = n.path_to.map   { |pt| copy_lookup[pt.object_id] }.compact
		n.path_from = n.path_from.map { |pf| copy_lookup[pf.object_id] }.compact
	end
	
	@paste_count = 0 if @copy_type == "copy"
	delete_points(CC.nouspoints) if @copy_type == "cut" && @paste_count == 0
	@paste_count += 1
	
	paste_pos = CC.mouse_last_pos
	diff = round_to_grid([paste_pos[0] - @copy_pos[0],paste_pos[1] - @copy_pos[1]])	
	@mempoints.each do |m|
		m.set_destination(diff)
		m.selected = true
	end
	@mempoints.each {|m| CC.nouspoints << m} if paste_check(diff,@mempoints)
	@mempoints = []
	
	populate_prop(CC.nouspoints)
	UI::canvas.queue_draw
end

#path_rotate(dir) ⇒ Object



390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/midinous/points.rb', line 390

def path_rotate(dir)
	CC.nouspoints.find_all(&:selected).each do |n|
		case dir
		when "+"
			n.path_to.rotate!(1)
		when "-"
			n.path_to.rotate!(-1)
		end
	end

	UI::canvas.queue_draw
end

#play_mode_rotate(dir) ⇒ Object



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/midinous/points.rb', line 372

def play_mode_rotate(dir)
	CC.nouspoints.find_all(&:selected).each do |n|
		if n.path_to.length <= 1
			case n.play_modes[0]
			when "robin"
				n.play_modes.rotate!(dir)	until n.play_modes[0] == "portal"
			when "portal"
				n.play_modes.rotate!(dir)	until n.play_modes[0] == "robin"
			when "random","split"
			  n.play_modes.rotate!(dir) until n.play_modes[0] == "robin"
			end
		else
			n.play_modes.rotate!(dir)
		end
		populate_prop(CC.nouspoints.find_all)
		UI::canvas.queue_draw
	end
end

#populate_prop(points) ⇒ Object



177
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
226
227
228
229
230
231
232
# File 'lib/midinous/points.rb', line 177

def populate_prop (points)
	UI::prop_list_model.clear
	UI::prop_mod.text = ""
	point = nil
	point = points.find(&:selected) if points.find_all(&:selected).length == 1
	if point
	  prop_vals = [point.note,
		             point.velocity,
								 point.duration,
								 point.channel,
								 point.repeat,
								 point.x,
								 point.y,
								 color_to_hex(point.default_color),
								 point.path_mode,
								 point.traveler_start,
								 point.play_modes[0]]
		@prop_names.each	do |v|
			iter = UI::prop_list_model.append
			iter[0] = v
			iter[1] = prop_vals[@prop_names.find_index(v)].to_s
		end
	elsif points.find_all(&:selected).length > 1
		@prop_names_multi.each do |v|
			equalizer = []
			iter = UI::prop_list_model.append
			iter[0] = v
			case v
			when "Note"
				points.find_all(&:selected).each {|p| equalizer << p.note}
			when "Velocity"
				points.find_all(&:selected).each {|p|	equalizer << p.velocity}
			when "Duration (beats)"
				points.find_all(&:selected).each {|p| equalizer << p.duration}
			when "Channel"
				points.find_all(&:selected).each {|p| equalizer << p.channel}
			when "Color"
				points.find_all(&:selected).each {|p| equalizer << color_to_hex(p.default_color)}
			when "Signal Start"
				points.find_all(&:selected).each {|p| equalizer << p.traveler_start}
			when "Play Mode"
				points.find_all(&:selected).each {|p| equalizer << p.play_modes[0]}
			when "Repeat"
				points.find_all(&:selected).each {|p| equalizer << p.repeat}
			end
			if equalizer.uniq.count == 1 
				iter[1] = equalizer[0].to_s
			else iter[1] = "Multiple Values"
			end
		end
	else
		UI::prop_list_model.clear
		UI::prop_mod.text = ""
	end
	
end

#prop_list_select(selected) ⇒ Object



233
234
235
236
237
238
239
240
241
242
# File 'lib/midinous/points.rb', line 233

def prop_list_select(selected)
	return if selected == nil
	@curr_prop = selected[0]
	if   selected[1][0] == "#"
			 UI::prop_mod.text = selected[1][1..6]
	else UI::prop_mod.text = selected[1]
	end
	UI::prop_mod.position = 0
	UI::prop_mod.grab_focus
end

#select_allObject



120
121
122
123
124
# File 'lib/midinous/points.rb', line 120

def select_all
	CC.nouspoints.each {|n| n.selected = true} unless CC.nouspoints == []
	populate_prop(CC.nouspoints)
	UI::canvas.queue_draw
end

#select_path_point(origin, points, source_chosen) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/midinous/points.rb', line 357

def select_path_point(origin,points,source_chosen)
	points.find_all {|g| check_bounds(origin,g.bounds)}.each do |n|
		case !n.pathable
			when true #If clicking where a non-pathable point is
				source_chosen = n.path_set(source_chosen)
			when false
				if n.source
					points, source_chosen = cancel_path(points)
				end
				n.path_unset
		end
	end
	return points, source_chosen
end

#select_points(box, points) ⇒ Object

Select points with the select tool



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/midinous/points.rb', line 101

def select_points(box,points) #Select points with the select tool
	UI::prop_list_model.clear
	UI::prop_mod.text = ""
	box_origin = [box[0],box[1]] #box is an array with 4 values
	points.each do |n|
		if check_bounds(n.origin,box)
				 n.select
		elsif check_bounds(box_origin,n.bounds)
				 n.select
		else 
			n.deselect
			UI::prop_list_model.clear
			UI::prop_mod.text = ""
		end
	end
	populate_prop(points)
	return points
end

#set_note_via_devc(note) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/midinous/points.rb', line 71

def set_note_via_devc(note)
	CC.nouspoints.find_all(&:selected).each {|n| n.note = note}
	Pm.note_send(Pm.in_chan,note,100)
	GLib::Timeout.add(100) do 
		Pm.note_rlse(1,note)
		false
	end
	populate_prop(CC.nouspoints)
end

#set_path_mode(mode) ⇒ Object



67
68
69
70
# File 'lib/midinous/points.rb', line 67

def set_path_mode(mode)
	CC.nouspoints.find_all(&:selected).each {|n| n.path_mode = mode}
	UI::canvas.queue_draw
end

#set_startObject



452
453
454
455
456
457
458
459
460
461
462
# File 'lib/midinous/points.rb', line 452

def set_start
	CC.nouspoints.find_all(&:selected).each do |n| 
		if n.traveler_start == false
			n.traveler_start = true
		elsif n.traveler_start == true
			n.traveler_start = false
		end 
	end
	UI::canvas.queue_draw
	populate_prop(CC.nouspoints)
end