-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw_tools.lua
151 lines (135 loc) · 4.45 KB
/
draw_tools.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
80
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
local tools = {}
local livereload = require "livereload"
local constants = require "draw_constants"
function tools.drawStaffLines(x, flow_y, width, cleff, octave, play)
love.graphics.setLineWidth(1)
local staff_top = flow_y
for i = 0, 4, 1 do
love.graphics.setColor(0.65, 0.65, 0.65)
local bar_y = flow_y + (i * constants.BAR_HEIGHT * 2)
love.graphics.line(x, bar_y, x + width, bar_y)
end
flow_y = flow_y + constants.BAR_HEIGHT * 8
if cleff == "piano" then
flow_y = flow_y + constants.BAR_HEIGHT * 4
for i = 0, 4, 1 do
local bar_y = flow_y + (i * constants.BAR_HEIGHT * 2)
love.graphics.line(x, bar_y, x + width, bar_y)
end
flow_y = flow_y + constants.BAR_HEIGHT * 8
end
-- clef
love.graphics.setFont(constants.note_font)
if play then
love.graphics.setColor(0.8, 0.5, 0.5)
end
if cleff == "treble" or cleff == "piano" then
love.graphics.print(constants.G_CLEFF, x, staff_top + constants.NOTE_OFFSET + (6 * constants.BAR_HEIGHT))
elseif cleff == "bass" then
love.graphics.print(constants.F_CLEFF, x, staff_top + constants.NOTE_OFFSET + (2 * constants.BAR_HEIGHT))
end
if cleff == "piano" then
love.graphics.print(constants.F_CLEFF, x, staff_top + constants.NOTE_OFFSET + (14 * constants.BAR_HEIGHT))
end
-- octave marker
love.graphics.setFont(constants.text_font)
if octave > 0 then
local octave_x = x
local octave_y = staff_top - 14
if cleff ~= "bass" then
octave_y = math.min(octave_y, staff_top + (6 * constants.BAR_HEIGHT) - 45)
else
octave_x = octave_x - 5
octave_y = math.min(octave_y, staff_top + (2 * constants.BAR_HEIGHT) - 20)
end
love.graphics.printf(1 + (octave * 7), octave_x, octave_y, 25, "center")
end
if octave < 0 then
local octave_y = flow_y + 3
local octave_x = x - 2
if cleff ~= "treble" then
octave_y = math.max(octave_y, flow_y - (6 * constants.BAR_HEIGHT) + 18)
else
octave_y = math.max(octave_y, flow_y - (2 * constants.BAR_HEIGHT) + 20)
end
love.graphics.printf(1 + (-octave * 7), octave_x, octave_y, 25, "center")
end
return flow_y
end
function tools.noteStaffPos(note, cleff)
local note_staff_pos = 0
if note then
local octave, letter = unpack(note)
note_staff_pos = (octave - 5) * 7 + letter - 1
end
if (cleff == "bass") then
note_staff_pos = note_staff_pos + 12
end
return note_staff_pos
end
function tools.noteYFromPosition(staff_position, staff_top)
local note_y = staff_top - (staff_position * constants.BAR_HEIGHT) + constants.BAR_HEIGHT * 10
return note_y
end
function tools.drawAccidental(note, flow_x, note_y)
local _, _, accidental = unpack(note)
if accidental == 1 then
love.graphics.setFont(constants.small_note_font)
love.graphics.printf(constants.ACCIDENTAL_SHARP, flow_x - 10, note_y + constants.SMALL_NOTE_OFFSET, 10, "right")
flow_x = flow_x + 5
end
if accidental == -1 then
love.graphics.setFont(constants.small_note_font)
love.graphics.printf(constants.ACCIDENTAL_FLAT, flow_x - 8, note_y + constants.SMALL_NOTE_OFFSET, 10, "right")
flow_x = flow_x + 5
end
return flow_x
end
function tools.drawLine(line_x, pos, staff_top)
local bar_y = staff_top + (pos * constants.BAR_HEIGHT * 2)
love.graphics.line(line_x - 4, bar_y, line_x + 12, bar_y)
end
function tools.drawLinesForNote(line_x, pos, cleff, staff_top)
if pos >= 12 then
local bar_count = math.floor(pos / 2) - 5
for bar_i = 1, bar_count, 1 do
tools.drawLine(line_x, -bar_i, staff_top)
end
end
if cleff == "piano" then
if pos == 0 then
tools.drawLine(line_x, 5, staff_top, a)
end
if pos <= -10 then
local bar_count = math.floor(-pos / 2) - 5
for bar_i = 1, bar_count, 1 do
tools.drawLine(line_x, bar_i + 10, staff_top, a)
end
end
else
if pos <= 0 then
local bar_count = math.floor(-pos / 2) + 1
for bar_i = 1, bar_count, 1 do
tools.drawLine(line_x, bar_i + 4, staff_top, a)
end
end
end
end
function tools.drawNoteHead(x, y, up)
local symbol = constants.NOTE_QUARTER_UP
if up == false then
symbol = constants.NOTE_QUARTER_DOWN
end
love.graphics.setFont(constants.note_font)
love.graphics.print(symbol, x - 1, y + constants.NOTE_OFFSET)
end
function tools.isNoteUp(staff_pos, cleff)
note_up = true
if staff_pos >= 6 then
note_up = false
elseif cleff == "piano" and staff_pos > -6 and staff_pos < 0 then
note_up = true
end
return note_up
end
return tools