RollWidget.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2019-2020, William McPherson <willmcpherson2@gmail.com>
  4. * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
  5. * Copyright (c) 2022, the SerenityOS developers.
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "RollWidget.h"
  10. #include "TrackManager.h"
  11. #include <AK/IntegralMath.h>
  12. #include <LibDSP/Music.h>
  13. #include <LibGUI/Event.h>
  14. #include <LibGUI/Painter.h>
  15. #include <LibGUI/Scrollbar.h>
  16. #include <LibGfx/Font/Font.h>
  17. #include <LibGfx/Font/FontDatabase.h>
  18. constexpr int note_height = 20;
  19. constexpr int max_note_width = note_height * 2;
  20. constexpr int roll_height = note_count * note_height;
  21. constexpr int horizontal_scroll_sensitivity = 20;
  22. constexpr int max_zoom = 1 << 8;
  23. RollWidget::RollWidget(TrackManager& track_manager)
  24. : m_track_manager(track_manager)
  25. {
  26. set_should_hide_unnecessary_scrollbars(true);
  27. set_content_size({ 0, roll_height });
  28. vertical_scrollbar().set_value(roll_height / 2);
  29. }
  30. void RollWidget::paint_event(GUI::PaintEvent& event)
  31. {
  32. m_roll_width = widget_inner_rect().width() * m_zoom_level;
  33. set_content_size({ m_roll_width, roll_height });
  34. // Divide the roll by the maximum note width. If we get fewer notes than
  35. // our time signature requires, round up. Otherwise, round down to the
  36. // nearest x*(2^y), where x is the base number of notes of our time
  37. // signature. In other words, find a number that is a double of our time
  38. // signature. For 4/4 that would be 16, 32, 64, 128 ...
  39. m_num_notes = m_roll_width / max_note_width;
  40. int time_signature_notes = beats_per_bar * notes_per_beat;
  41. if (m_num_notes < time_signature_notes)
  42. m_num_notes = time_signature_notes;
  43. else
  44. m_num_notes = time_signature_notes * AK::exp2(AK::log2(m_num_notes / time_signature_notes));
  45. m_note_width = static_cast<double>(m_roll_width) / m_num_notes;
  46. // This calculates the minimum number of rows needed. We account for a
  47. // partial row at the top and/or bottom.
  48. int y_offset = vertical_scrollbar().value();
  49. int note_offset = y_offset / note_height;
  50. int note_offset_remainder = y_offset % note_height;
  51. int paint_area = widget_inner_rect().height() + note_offset_remainder;
  52. if (paint_area % note_height != 0)
  53. paint_area += note_height;
  54. int notes_to_paint = paint_area / note_height;
  55. int key_pattern_index = (notes_per_octave - 1) - (note_offset % notes_per_octave);
  56. int x_offset = horizontal_scrollbar().value();
  57. int horizontal_note_offset_remainder = static_cast<int>(AK::fmod((double)x_offset, m_note_width));
  58. int horizontal_paint_area = widget_inner_rect().width() + horizontal_note_offset_remainder;
  59. if (AK::fmod((double)horizontal_paint_area, m_note_width) != 0.)
  60. horizontal_paint_area += m_note_width;
  61. int horizontal_notes_to_paint = horizontal_paint_area / m_note_width;
  62. GUI::Painter painter(*this);
  63. // Draw the background, if necessary.
  64. if (viewport_changed() || paint_area != m_background->height()) {
  65. m_background = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, Gfx::IntSize(m_roll_width, paint_area)).release_value_but_fixme_should_propagate_errors();
  66. Gfx::Painter background_painter(*m_background);
  67. background_painter.translate(frame_thickness(), frame_thickness());
  68. background_painter.translate(-horizontal_note_offset_remainder, -note_offset_remainder);
  69. for (int y = 0; y < notes_to_paint; ++y) {
  70. int y_pos = y * note_height;
  71. for (int x = 0; x < horizontal_notes_to_paint; ++x) {
  72. // This is needed to avoid rounding errors. You can't just use
  73. // m_note_width as the width.
  74. int x_pos = x * m_note_width;
  75. int next_x_pos = (x + 1) * m_note_width;
  76. int distance_to_next_x = next_x_pos - x_pos;
  77. Gfx::IntRect rect(x_pos, y_pos, distance_to_next_x, note_height);
  78. if (key_pattern[key_pattern_index] == Black)
  79. background_painter.fill_rect(rect, Color::LightGray);
  80. else
  81. background_painter.fill_rect(rect, Color::White);
  82. rect.shrink(0, 1, 1, 0);
  83. background_painter.draw_line(rect.top_right(), rect.bottom_right(), Color::Black);
  84. background_painter.draw_line(rect.bottom_left(), rect.bottom_right(), Color::Black);
  85. }
  86. if (--key_pattern_index == -1)
  87. key_pattern_index = notes_per_octave - 1;
  88. }
  89. background_painter.translate(-x_offset, -y_offset);
  90. background_painter.translate(horizontal_note_offset_remainder, note_offset_remainder);
  91. m_prev_zoom_level = m_zoom_level;
  92. m_prev_scroll_x = horizontal_scrollbar().value();
  93. m_prev_scroll_y = vertical_scrollbar().value();
  94. }
  95. painter.blit(Gfx::IntPoint(0, 0), *m_background, m_background->rect());
  96. // Draw the notes, mouse interaction, and time position.
  97. painter.translate(frame_thickness(), frame_thickness());
  98. painter.add_clip_rect(event.rect());
  99. painter.translate(-horizontal_note_offset_remainder, -note_offset_remainder);
  100. for (int y = 0; y < notes_to_paint; ++y) {
  101. int y_pos = y * note_height;
  102. int note = (note_count - note_offset - 1) - y;
  103. for (int x = 0; x < horizontal_notes_to_paint; ++x) {
  104. // This is needed to avoid rounding errors. You can't just use
  105. // m_note_width as the width.
  106. int x_pos = x * m_note_width;
  107. int next_x_pos = (x + 1) * m_note_width;
  108. int distance_to_next_x = next_x_pos - x_pos;
  109. Gfx::IntRect rect(x_pos, y_pos, distance_to_next_x, note_height);
  110. if (static_cast<size_t>(note) < DSP::note_frequencies.size() && m_track_manager.keyboard()->is_pressed(note))
  111. painter.fill_rect(rect, note_pressed_color.with_alpha(128));
  112. }
  113. }
  114. painter.translate(-x_offset, -y_offset);
  115. painter.translate(horizontal_note_offset_remainder, note_offset_remainder);
  116. for (auto const& clip : m_track_manager.current_track()->notes()) {
  117. for (auto const& roll_note : clip->notes()) {
  118. int y = ((note_count - 1) - roll_note.pitch) * note_height;
  119. int x = m_roll_width * (static_cast<double>(roll_note.on_sample) / roll_length);
  120. int width = m_roll_width * (static_cast<double>(roll_note.length()) / roll_length);
  121. if (x + width < x_offset || x > x_offset + widget_inner_rect().width())
  122. continue;
  123. if (width < 2)
  124. width = 2;
  125. int height = note_height;
  126. Gfx::IntRect rect(x, y, width, height);
  127. painter.fill_rect(rect, note_pressed_color);
  128. painter.draw_rect(rect, Color::Black);
  129. }
  130. }
  131. for (int note = note_count - (note_offset + notes_to_paint); note <= (note_count - 1) - note_offset; ++note) {
  132. int y = ((note_count - 1) - note) * note_height;
  133. Gfx::IntRect note_name_rect(3, y, 1, note_height);
  134. auto note_name = note_names[note % notes_per_octave];
  135. painter.draw_text(note_name_rect, note_name, Gfx::TextAlignment::CenterLeft);
  136. note_name_rect.translate_by(Gfx::FontDatabase::default_font().width(note_name) + 2, 0);
  137. if (note % notes_per_octave == 0)
  138. painter.draw_text(note_name_rect, ByteString::formatted("{}", note / notes_per_octave + 1), Gfx::TextAlignment::CenterLeft);
  139. }
  140. int x = m_roll_width * (static_cast<double>(m_track_manager.transport()->time()) / roll_length);
  141. if (x > x_offset && x <= x_offset + widget_inner_rect().width())
  142. painter.draw_line({ x, 0 }, { x, roll_height }, Gfx::Color::Black);
  143. GUI::Frame::paint_event(event);
  144. }
  145. bool RollWidget::viewport_changed() const
  146. {
  147. // height is complicated to check, will be done in paint_event
  148. return m_background.is_null()
  149. || m_roll_width != m_background->width()
  150. || m_prev_scroll_x != horizontal_scrollbar().value() || m_prev_scroll_y != vertical_scrollbar().value()
  151. || m_prev_zoom_level != m_zoom_level;
  152. }
  153. void RollWidget::mousedown_event(GUI::MouseEvent& event)
  154. {
  155. if (!widget_inner_rect().contains(event.x(), event.y()))
  156. return;
  157. if (event.button() == GUI::MouseButton::Secondary) {
  158. auto const time = roll_length * (static_cast<double>(get_note_for_x(event.x())) / m_num_notes);
  159. auto const note = m_track_manager.current_track()->note_at(time, get_pitch_for_y(event.y()));
  160. if (note.has_value()) {
  161. m_track_manager.current_track()->remove_note(note.value());
  162. update();
  163. }
  164. return;
  165. }
  166. m_mousedown_event = event;
  167. }
  168. void RollWidget::mouseup_event(GUI::MouseEvent& event)
  169. {
  170. mousemove_event(event);
  171. m_mousedown_event = {};
  172. }
  173. u8 RollWidget::get_pitch_for_y(int y) const
  174. {
  175. return (note_count - 1) - ((y + vertical_scrollbar().value()) - frame_thickness()) / note_height;
  176. }
  177. int RollWidget::get_note_for_x(int x) const
  178. {
  179. // There's a case where we can't just use x / m_note_width. For example, if
  180. // your m_note_width is 3.1 you will have a rect starting at 3. When that
  181. // leftmost pixel of the rect is clicked you will do 3 / 3.1 which is 0
  182. // and not 1. We can avoid that case by shifting x by 1 if m_note_width is
  183. // fractional, being careful not to shift out of bounds.
  184. x = (x + horizontal_scrollbar().value()) - frame_thickness();
  185. bool const note_width_is_fractional = m_note_width - static_cast<int>(m_note_width) != 0;
  186. bool const x_is_not_last = x != widget_inner_rect().width() - 1;
  187. if (note_width_is_fractional && x_is_not_last)
  188. ++x;
  189. x /= m_note_width;
  190. return clamp(x, 0, m_num_notes - 1);
  191. }
  192. void RollWidget::mousemove_event(GUI::MouseEvent& event)
  193. {
  194. if (!m_mousedown_event.has_value())
  195. return;
  196. if (m_mousedown_event.value().button() == GUI::MouseButton::Primary) {
  197. int const x_start = get_note_for_x(m_mousedown_event.value().x());
  198. int const x_end = get_note_for_x(event.x());
  199. u32 const on_sample = round(roll_length * (static_cast<double>(min(x_start, x_end)) / m_num_notes));
  200. u32 const off_sample = round(roll_length * (static_cast<double>(max(x_start, x_end) + 1) / m_num_notes)) - 1;
  201. auto const note = RollNote { on_sample, off_sample, get_pitch_for_y(m_mousedown_event.value().y()), 127 };
  202. m_track_manager.current_track()->set_note(note);
  203. update();
  204. }
  205. }
  206. // FIXME: Implement zoom and horizontal scroll events in LibGUI, not here.
  207. void RollWidget::mousewheel_event(GUI::MouseEvent& event)
  208. {
  209. if (event.modifiers() & KeyModifier::Mod_Shift) {
  210. horizontal_scrollbar().increase_slider_by(event.wheel_delta_y() * horizontal_scroll_sensitivity);
  211. return;
  212. }
  213. if (event.wheel_delta_x() != 0) {
  214. horizontal_scrollbar().increase_slider_by(event.wheel_delta_x() * horizontal_scroll_sensitivity);
  215. return;
  216. }
  217. if (!(event.modifiers() & KeyModifier::Mod_Ctrl)) {
  218. GUI::AbstractScrollableWidget::mousewheel_event(event);
  219. return;
  220. }
  221. double multiplier = event.wheel_delta_y() >= 0 ? 0.5 : 2;
  222. if (m_zoom_level * multiplier > max_zoom)
  223. return;
  224. if (m_zoom_level * multiplier < 1) {
  225. if (m_zoom_level == 1)
  226. return;
  227. m_zoom_level = 1;
  228. } else {
  229. m_zoom_level *= multiplier;
  230. }
  231. int absolute_x_of_pixel_at_cursor = horizontal_scrollbar().value() + event.position().x();
  232. int absolute_x_of_pixel_at_cursor_after_resize = absolute_x_of_pixel_at_cursor * multiplier;
  233. int new_scrollbar = absolute_x_of_pixel_at_cursor_after_resize - event.position().x();
  234. m_roll_width = widget_inner_rect().width() * m_zoom_level;
  235. set_content_size({ m_roll_width, roll_height });
  236. horizontal_scrollbar().set_value(new_scrollbar);
  237. }