Module: Logic_Controls

Included in:
Canvas_Control, NousPoint, Point_Logic, UI_Elements
Defined in:
lib/midinous/logic.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

Instance Method Details

#check_bounds(coord, bounds) ⇒ Object

returns true if coordinate is colliding with a point bounding box.



147
148
149
150
151
152
153
# File 'lib/midinous/logic.rb', line 147

def check_bounds(coord,bounds) # returns true if coordinate is colliding with a point bounding box.

  if coord[0].between?(bounds[0],bounds[2]) == true &&
     coord[1].between?(bounds[1],bounds[3]) == true
    return true
  else return false
  end
end

#color_to_hex(color) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/midinous/logic.rb', line 127

def color_to_hex(color)
  c_str = "#"
  color.each do |n|
    n = "%x" % (n*127)
    if n.length < 2
         c_str = "#{c_str}0#{n}"
    else c_str = "#{c_str}#{n}"
    end
  end
  return c_str
end

#draw_chevron(cr, offset, dir, p) ⇒ Object



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
112
113
114
115
116
117
118
# File 'lib/midinous/logic.rb', line 81

def draw_chevron(cr,offset,dir,p)
  x = p.x
  y = p.y
  case dir
  when "n"
    cr.move_to(x,  y+12+offset)
    cr.line_to(x+5,y+17+offset)
    cr.line_to(x+5,y+21+offset)
    cr.line_to(x,  y+16+offset)
    cr.line_to(x-5,y+21+offset)
    cr.line_to(x-5,y+17+offset)
    cr.line_to(x,  y+12+offset)
  when "s"
    cr.move_to(x,  y-12-offset)
    cr.line_to(x+5,y-17-offset)
    cr.line_to(x+5,y-21-offset)
    cr.line_to(x,  y-16-offset)
    cr.line_to(x-5,y-21-offset)
    cr.line_to(x-5,y-17-offset)
    cr.line_to(x,  y-12-offset)
  when "e"
    cr.move_to(x-12-offset,y)
    cr.line_to(x-17-offset,y+5)
    cr.line_to(x-21-offset,y+5)
    cr.line_to(x-16-offset,y)
    cr.line_to(x-21-offset,y-5)
    cr.line_to(x-17-offset,y-5)
    cr.line_to(x-12-offset,y)
  when "w"
    cr.move_to(x+12+offset,y)
    cr.line_to(x+17+offset,y+5)
    cr.line_to(x+21+offset,y+5)
    cr.line_to(x+16+offset,y)
    cr.line_to(x+21+offset,y-5)
    cr.line_to(x+17+offset,y-5)
    cr.line_to(x+12+offset,y)
  end
end

#hex_to_color(c_hex) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/midinous/logic.rb', line 139

def hex_to_color(c_hex)
  color = []
  color[0] = ((c_hex[1..2].hex).to_f/127)
  color[1] = ((c_hex[3..4].hex).to_f/127)
  color[2] = ((c_hex[5..6].hex).to_f/127) 
  return color
end

#pos_box(bounds) ⇒ Object

turn a coordinate-bounded box with unmatching coordinates into one with positive coordinates



155
156
157
158
159
160
161
162
163
# File 'lib/midinous/logic.rb', line 155

def pos_box(bounds) #turn a coordinate-bounded box with unmatching coordinates into one with positive coordinates

  if bounds[0] > bounds[2] #Flip the array positions if the box is drawn backwards in any direction.

     bounds[0], bounds[2] = bounds[2], bounds[0]
  end
  if bounds[1] > bounds[3]
    bounds[1], bounds[3] = bounds[3], bounds[1]
  end
  return bounds
end

#relative_pos(xd, yd) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/midinous/logic.rb', line 40

def relative_pos(xd,yd)
  x_sign = nil
  y_sign = nil
  case 
  when xd > 0
    x_sign = "+"
  when xd == 0
    x_sign = "0"
  when xd < 0
    x_sign = "-"
  end
  case
  when yd > 0
    y_sign = "+"
  when yd == 0
    y_sign = "0"
  when yd < 0
    y_sign = "-"
  end
  
  sign = [x_sign,y_sign]
  case sign
  when ["+","+"]
    return "se"
  when ["+","-"]
    return "ne"
  when ["-","-"]
    return "nw"
  when ["-","+"]
    return "sw"
  when ["+","0"]
    return "e"      
  when ["-","0"]
    return "w"
  when ["0","+"]
    return "s"
  when ["0","-"]
    return "n"
  end
end

#round_num_to_grid(num) ⇒ Object

2050



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

def round_num_to_grid(num) #2050

  temp = num % CC.grid_spacing
  num -= temp if temp < (CC.grid_spacing/2)
  num  = (num-temp)+CC.grid_spacing if temp >= (CC.grid_spacing/2)
  return num
end

#round_to_grid(coord) ⇒ Object

rounds a coordinate to the nearest snappable grid point



25
26
27
28
29
30
31
32
33
# File 'lib/midinous/logic.rb', line 25

def round_to_grid(coord) #rounds a coordinate to the nearest snappable grid point

  coord.map! do |n|
    temp = n % CC.grid_spacing
    n -= temp if temp < (CC.grid_spacing/2)
    n = n-temp+CC.grid_spacing if temp >= (CC.grid_spacing/2)
    n
  end
  return coord
end

#set_point_speed(tempo, beats, beat_note) ⇒ Object

various reusable functions useful for checks and math



20
21
22
23
# File 'lib/midinous/logic.rb', line 20

def set_point_speed(tempo,beats,beat_note) #Sets time between each grid point

  point_speed = (tempo/60)*CC.grid_spacing #Grid points that will be hit per second 

  return point_speed                       #(will be a fraction most of the time)

end

#sync_diff(stored_time) ⇒ Object



35
36
37
38
# File 'lib/midinous/logic.rb', line 35

def sync_diff(stored_time)
  new_time = Time.now.to_f*1000
  return (CC.ms_per_tick - (new_time - (stored_time + CC.ms_per_tick)))
end