Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2019-2020, William McPherson <willmcpherson2@gmail.com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "RollWidget.h"
29#include "AudioEngine.h"
30#include <LibGUI/Painter.h>
31#include <LibGUI/ScrollBar.h>
32#include <math.h>
33
34constexpr int note_height = 20;
35constexpr int max_note_width = note_height * 2;
36constexpr int roll_height = note_count * note_height;
37constexpr int horizontal_scroll_sensitivity = 20;
38constexpr int max_zoom = 1 << 8;
39
40RollWidget::RollWidget(AudioEngine& audio_engine)
41 : m_audio_engine(audio_engine)
42{
43 set_should_hide_unnecessary_scrollbars(true);
44 set_content_size({ 0, roll_height });
45 vertical_scrollbar().set_value(roll_height / 2);
46}
47
48RollWidget::~RollWidget()
49{
50}
51
52void RollWidget::paint_event(GUI::PaintEvent& event)
53{
54 m_roll_width = widget_inner_rect().width() * m_zoom_level;
55 set_content_size({ m_roll_width, roll_height });
56
57 // Divide the roll by the maximum note width. If we get fewer notes than
58 // our time signature requires, round up. Otherwise, round down to the
59 // nearest x*(2^y), where x is the base number of notes of our time
60 // signature. In other words, find a number that is a double of our time
61 // signature. For 4/4 that would be 16, 32, 64, 128 ...
62 m_num_notes = m_roll_width / max_note_width;
63 int time_signature_notes = beats_per_bar * notes_per_beat;
64 if (m_num_notes < time_signature_notes)
65 m_num_notes = time_signature_notes;
66 else
67 m_num_notes = time_signature_notes * pow(2, static_cast<int>(log2(m_num_notes / time_signature_notes)));
68 m_note_width = static_cast<double>(m_roll_width) / m_num_notes;
69
70 // This calculates the minimum number of rows needed. We account for a
71 // partial row at the top and/or bottom.
72 int y_offset = vertical_scrollbar().value();
73 int note_offset = y_offset / note_height;
74 int note_offset_remainder = y_offset % note_height;
75 int paint_area = widget_inner_rect().height() + note_offset_remainder;
76 if (paint_area % note_height != 0)
77 paint_area += note_height;
78 int notes_to_paint = paint_area / note_height;
79 int key_pattern_index = (notes_per_octave - 1) - (note_offset % notes_per_octave);
80
81 int x_offset = horizontal_scrollbar().value();
82 int horizontal_note_offset_remainder = fmod(x_offset, m_note_width);
83 int horizontal_paint_area = widget_inner_rect().width() + horizontal_note_offset_remainder;
84 if (fmod(horizontal_paint_area, m_note_width) != 0)
85 horizontal_paint_area += m_note_width;
86 int horizontal_notes_to_paint = horizontal_paint_area / m_note_width;
87
88 GUI::Painter painter(*this);
89 painter.translate(frame_thickness(), frame_thickness());
90 painter.translate(-horizontal_note_offset_remainder, -note_offset_remainder);
91 painter.add_clip_rect(event.rect());
92
93 for (int y = 0; y < notes_to_paint; ++y) {
94 int y_pos = y * note_height;
95 for (int x = 0; x < horizontal_notes_to_paint; ++x) {
96 // This is needed to avoid rounding errors. You can't just use
97 // m_note_width as the width.
98 int x_pos = x * m_note_width;
99 int next_x_pos = (x + 1) * m_note_width;
100 int distance_to_next_x = next_x_pos - x_pos;
101 Gfx::Rect rect(x_pos, y_pos, distance_to_next_x, note_height);
102
103 if (key_pattern[key_pattern_index] == Black)
104 painter.fill_rect(rect, Color::LightGray);
105 else
106 painter.fill_rect(rect, Color::White);
107
108 painter.draw_line(rect.top_right(), rect.bottom_right(), Color::Black);
109 painter.draw_line(rect.bottom_left(), rect.bottom_right(), Color::Black);
110 }
111
112 if (--key_pattern_index == -1)
113 key_pattern_index = notes_per_octave - 1;
114 }
115
116 painter.translate(-x_offset, -y_offset);
117 painter.translate(horizontal_note_offset_remainder, note_offset_remainder);
118
119 for (int note = note_count - (note_offset + notes_to_paint); note <= (note_count - 1) - note_offset; ++note) {
120 for (auto roll_note : m_audio_engine.roll_notes(note)) {
121 int x = m_roll_width * (static_cast<double>(roll_note.on_sample) / roll_length);
122 int width = m_roll_width * (static_cast<double>(roll_note.length()) / roll_length);
123 if (x + width < x_offset || x > x_offset + widget_inner_rect().width())
124 continue;
125 if (width < 2)
126 width = 2;
127
128 int y = ((note_count - 1) - note) * note_height;
129 int height = note_height;
130
131 Gfx::Rect rect(x, y, width, height);
132 painter.fill_rect(rect, note_pressed_color);
133 painter.draw_rect(rect, Color::Black);
134 }
135 }
136
137 int x = m_roll_width * (static_cast<double>(m_audio_engine.time()) / roll_length);
138 if (x > x_offset && x <= x_offset + widget_inner_rect().width())
139 painter.draw_line({ x, 0 }, { x, roll_height }, Gfx::Color::Black);
140
141 GUI::Frame::paint_event(event);
142}
143
144void RollWidget::mousedown_event(GUI::MouseEvent& event)
145{
146 if (!widget_inner_rect().contains(event.x(), event.y()))
147 return;
148
149 int y = (event.y() + vertical_scrollbar().value()) - frame_thickness();
150 y /= note_height;
151
152 // There's a case where we can't just use x / m_note_width. For example, if
153 // your m_note_width is 3.1 you will have a rect starting at 3. When that
154 // leftmost pixel of the rect is clicked you will do 3 / 3.1 which is 0
155 // and not 1. We can avoid that case by shifting x by 1 if m_note_width is
156 // fractional, being careful not to shift out of bounds.
157 int x = (event.x() + horizontal_scrollbar().value()) - frame_thickness();
158 bool note_width_is_fractional = m_note_width - static_cast<int>(m_note_width) != 0;
159 bool x_is_not_last = x != widget_inner_rect().width() - 1;
160 if (note_width_is_fractional && x_is_not_last)
161 ++x;
162 x /= m_note_width;
163
164 int note = (note_count - 1) - y;
165 u32 on_sample = roll_length * (static_cast<double>(x) / m_num_notes);
166 u32 off_sample = (roll_length * (static_cast<double>(x + 1) / m_num_notes)) - 1;
167 m_audio_engine.set_roll_note(note, on_sample, off_sample);
168
169 update();
170}
171
172// FIXME: Implement zoom and horizontal scroll events in LibGUI, not here.
173void RollWidget::mousewheel_event(GUI::MouseEvent& event)
174{
175 if (event.modifiers() & KeyModifier::Mod_Shift) {
176 horizontal_scrollbar().set_value(horizontal_scrollbar().value() + (event.wheel_delta() * horizontal_scroll_sensitivity));
177 return;
178 }
179
180 if (!(event.modifiers() & KeyModifier::Mod_Ctrl)) {
181 GUI::ScrollableWidget::mousewheel_event(event);
182 return;
183 }
184
185 double multiplier = event.wheel_delta() >= 0 ? 0.5 : 2;
186
187 if (m_zoom_level * multiplier > max_zoom)
188 return;
189
190 if (m_zoom_level * multiplier < 1) {
191 if (m_zoom_level == 1)
192 return;
193 m_zoom_level = 1;
194 } else {
195 m_zoom_level *= multiplier;
196 }
197
198 int absolute_x_of_pixel_at_cursor = horizontal_scrollbar().value() + event.position().x();
199 int absolute_x_of_pixel_at_cursor_after_resize = absolute_x_of_pixel_at_cursor * multiplier;
200 int new_scrollbar = absolute_x_of_pixel_at_cursor_after_resize - event.position().x();
201
202 m_roll_width = widget_inner_rect().width() * m_zoom_level;
203 set_content_size({ m_roll_width, roll_height });
204
205 horizontal_scrollbar().set_value(new_scrollbar);
206}