SamplerWidget.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2020, William McPherson <willmcpherson2@gmail.com>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "SamplerWidget.h"
  8. #include "TrackManager.h"
  9. #include <LibGUI/BoxLayout.h>
  10. #include <LibGUI/Button.h>
  11. #include <LibGUI/FilePicker.h>
  12. #include <LibGUI/Label.h>
  13. #include <LibGUI/MessageBox.h>
  14. #include <LibGUI/Painter.h>
  15. WaveEditor::WaveEditor(TrackManager& track_manager)
  16. : m_track_manager(track_manager)
  17. {
  18. }
  19. int WaveEditor::sample_to_y(double percentage) const
  20. {
  21. double portion_of_half_height = percentage * ((frame_inner_rect().height() - 1) / 2.0);
  22. double y = (frame_inner_rect().height() / 2.0) + portion_of_half_height;
  23. return y;
  24. }
  25. void WaveEditor::paint_event(GUI::PaintEvent& event)
  26. {
  27. GUI::Frame::paint_event(event);
  28. GUI::Painter painter(*this);
  29. painter.fill_rect(frame_inner_rect(), Color::Black);
  30. // FIXME: This widget currently can't display anything.
  31. return;
  32. }
  33. SamplerWidget::SamplerWidget(TrackManager& track_manager)
  34. : m_track_manager(track_manager)
  35. {
  36. set_layout<GUI::VerticalBoxLayout>(10, 10);
  37. set_fill_with_background_color(true);
  38. m_open_button_and_recorded_sample_name_container = add<GUI::Widget>();
  39. m_open_button_and_recorded_sample_name_container->set_layout<GUI::HorizontalBoxLayout>(GUI::Margins {}, 10);
  40. m_open_button_and_recorded_sample_name_container->set_fixed_height(24);
  41. m_open_button = m_open_button_and_recorded_sample_name_container->add<GUI::Button>();
  42. m_open_button->set_fixed_size(24, 24);
  43. m_open_button->set_focus_policy(GUI::FocusPolicy::TabFocus);
  44. m_open_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"sv).release_value_but_fixme_should_propagate_errors());
  45. m_open_button->on_click = [this](auto) {
  46. Optional<DeprecatedString> open_path = GUI::FilePicker::get_open_filepath(window());
  47. if (!open_path.has_value())
  48. return;
  49. // TODO: We don't actually load the sample.
  50. m_recorded_sample_name->set_text(String::from_deprecated_string(open_path.value()).release_value_but_fixme_should_propagate_errors());
  51. m_wave_editor->update();
  52. };
  53. m_recorded_sample_name = m_open_button_and_recorded_sample_name_container->add<GUI::Label>("No sample loaded"_string.release_value_but_fixme_should_propagate_errors());
  54. m_recorded_sample_name->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  55. m_wave_editor = add<WaveEditor>(m_track_manager);
  56. m_wave_editor->set_fixed_height(100);
  57. }