MainWidget.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include "MainWidget.h"
  28. #include "AudioEngine.h"
  29. #include "KeysWidget.h"
  30. #include "KnobsWidget.h"
  31. #include "RollWidget.h"
  32. #include "WaveWidget.h"
  33. #include <LibGUI/GBoxLayout.h>
  34. MainWidget::MainWidget(AudioEngine& audio_engine)
  35. : m_audio_engine(audio_engine)
  36. {
  37. set_layout(make<GVBoxLayout>());
  38. layout()->set_spacing(2);
  39. layout()->set_margins({ 2, 2, 2, 2 });
  40. set_fill_with_background_color(true);
  41. m_wave_widget = WaveWidget::construct(this, audio_engine);
  42. m_wave_widget->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  43. m_wave_widget->set_preferred_size(0, 100);
  44. m_roll_widget = RollWidget::construct(this, audio_engine);
  45. m_roll_widget->set_size_policy(SizePolicy::Fill, SizePolicy::Fill);
  46. m_roll_widget->set_preferred_size(0, 300);
  47. m_keys_and_knobs_container = GWidget::construct(this);
  48. m_keys_and_knobs_container->set_layout(make<GHBoxLayout>());
  49. m_keys_and_knobs_container->layout()->set_spacing(2);
  50. m_keys_and_knobs_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  51. m_keys_and_knobs_container->set_preferred_size(0, 100);
  52. m_keys_and_knobs_container->set_fill_with_background_color(true);
  53. m_keys_widget = KeysWidget::construct(m_keys_and_knobs_container, audio_engine);
  54. m_knobs_widget = KnobsWidget::construct(m_keys_and_knobs_container, audio_engine, *this);
  55. m_knobs_widget->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  56. m_knobs_widget->set_preferred_size(200, 0);
  57. }
  58. MainWidget::~MainWidget()
  59. {
  60. }
  61. // FIXME: There are some unnecessary calls to update() throughout this program,
  62. // which are an easy target for optimization.
  63. void MainWidget::custom_event(Core::CustomEvent&)
  64. {
  65. m_wave_widget->update();
  66. if (m_audio_engine.time() == 0)
  67. m_roll_widget->update_roll();
  68. }
  69. void MainWidget::keydown_event(GKeyEvent& event)
  70. {
  71. // This is to stop held-down keys from creating multiple events.
  72. if (m_keys_pressed[event.key()])
  73. return;
  74. else
  75. m_keys_pressed[event.key()] = true;
  76. note_key_action(event.key(), On);
  77. special_key_action(event.key());
  78. m_keys_widget->update();
  79. }
  80. void MainWidget::keyup_event(GKeyEvent& event)
  81. {
  82. m_keys_pressed[event.key()] = false;
  83. note_key_action(event.key(), Off);
  84. m_keys_widget->update();
  85. }
  86. void MainWidget::note_key_action(int key_code, Switch switch_note)
  87. {
  88. int key = m_keys_widget->key_code_to_key(key_code);
  89. m_keys_widget->set_key(key, switch_note);
  90. }
  91. void MainWidget::special_key_action(int key_code)
  92. {
  93. switch (key_code) {
  94. case Key_Z:
  95. set_octave_and_ensure_note_change(Down);
  96. break;
  97. case Key_X:
  98. set_octave_and_ensure_note_change(Up);
  99. break;
  100. case Key_C:
  101. m_audio_engine.set_wave(Up);
  102. m_knobs_widget->update_knobs();
  103. break;
  104. }
  105. }
  106. void MainWidget::set_octave_and_ensure_note_change(Direction direction)
  107. {
  108. m_keys_widget->set_key(m_keys_widget->mouse_note(), Off);
  109. for (int i = 0; i < key_code_count; ++i) {
  110. if (m_keys_pressed[i])
  111. note_key_action(i, Off);
  112. }
  113. m_audio_engine.set_octave(direction);
  114. m_keys_widget->set_key(m_keys_widget->mouse_note(), On);
  115. for (int i = 0; i < key_code_count; ++i) {
  116. if (m_keys_pressed[i])
  117. note_key_action(i, On);
  118. }
  119. m_knobs_widget->update_knobs();
  120. m_keys_widget->update();
  121. }