2020-01-30 13:02:45 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
* Copyright (c) 2019-2020, William McPherson <willmcpherson2@gmail.com>
|
2022-02-10 16:28:48 -03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-30 13:02:45 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-30 13:02:45 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "MainWidget.h"
|
|
|
|
|
#include "KeysWidget.h"
|
|
|
|
|
#include "KnobsWidget.h"
|
2021-07-10 23:58:17 -03:00
|
|
|
#include "PlayerWidget.h"
|
2020-01-30 13:02:45 -03:00
|
|
|
#include "RollWidget.h"
|
2020-02-08 03:04:05 -03:00
|
|
|
#include "SamplerWidget.h"
|
2020-06-15 02:33:53 -03:00
|
|
|
#include "TrackManager.h"
|
2020-01-30 13:02:45 -03:00
|
|
|
#include "WaveWidget.h"
|
2020-06-17 06:34:25 -03:00
|
|
|
#include <LibGUI/Action.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
2020-06-17 06:34:25 -03:00
|
|
|
#include <LibGUI/Menu.h>
|
2020-02-08 03:04:05 -03:00
|
|
|
#include <LibGUI/TabWidget.h>
|
2020-01-30 13:02:45 -03:00
|
|
|
|
2021-07-10 23:58:17 -03:00
|
|
|
MainWidget::MainWidget(TrackManager& track_manager, AudioPlayerLoop& loop)
|
2020-06-15 02:33:53 -03:00
|
|
|
: m_track_manager(track_manager)
|
2021-07-10 23:58:17 -03:00
|
|
|
, m_audio_loop(loop)
|
2020-01-30 13:02:45 -03:00
|
|
|
{
|
2020-03-04 05:43:54 -03:00
|
|
|
set_layout<GUI::VerticalBoxLayout>();
|
2020-01-30 13:02:45 -03:00
|
|
|
layout()->set_spacing(2);
|
Userland+LibGUI: Add shorthand versions of the Margins constructor
This allows for typing [8] instead of [8, 8, 8, 8] to specify the same
margin on all edges, for example. The constructors follow CSS' style of
specifying margins. The added constructors are:
- Margins(int all): Sets the same margin on all edges.
- Margins(int vertical, int horizontal): Sets the first argument to top
and bottom margins, and the second argument to left and right margins.
- Margins(int top, int vertical, int bottom): Sets the first argument to
the top margin, the second argument to the left and right margins,
and the third argument to the bottom margin.
2021-08-16 21:11:38 -03:00
|
|
|
layout()->set_margins(2);
|
2020-01-30 13:02:45 -03:00
|
|
|
set_fill_with_background_color(true);
|
|
|
|
|
|
2020-06-15 02:33:53 -03:00
|
|
|
m_wave_widget = add<WaveWidget>(track_manager);
|
2020-12-29 21:23:32 -03:00
|
|
|
m_wave_widget->set_fixed_height(100);
|
2020-01-30 13:02:45 -03:00
|
|
|
|
2020-02-23 08:23:48 -03:00
|
|
|
m_tab_widget = add<GUI::TabWidget>();
|
2020-06-15 02:33:53 -03:00
|
|
|
m_roll_widget = m_tab_widget->add_tab<RollWidget>("Piano Roll", track_manager);
|
2020-02-23 08:23:48 -03:00
|
|
|
|
2020-12-29 21:23:32 -03:00
|
|
|
m_roll_widget->set_fixed_height(300);
|
2020-01-30 13:02:45 -03:00
|
|
|
|
2020-06-15 02:33:53 -03:00
|
|
|
m_tab_widget->add_tab<SamplerWidget>("Sampler", track_manager);
|
2020-02-08 03:04:05 -03:00
|
|
|
|
2021-07-10 23:58:17 -03:00
|
|
|
m_player_widget = add<PlayerWidget>(track_manager, loop);
|
|
|
|
|
|
2020-02-23 06:57:42 -03:00
|
|
|
m_keys_and_knobs_container = add<GUI::Widget>();
|
2020-03-04 05:43:54 -03:00
|
|
|
m_keys_and_knobs_container->set_layout<GUI::HorizontalBoxLayout>();
|
2020-01-30 13:02:45 -03:00
|
|
|
m_keys_and_knobs_container->layout()->set_spacing(2);
|
2021-08-27 11:20:09 -03:00
|
|
|
m_keys_and_knobs_container->set_fixed_height(130);
|
2020-01-30 13:02:45 -03:00
|
|
|
m_keys_and_knobs_container->set_fill_with_background_color(true);
|
|
|
|
|
|
2022-05-13 20:11:23 -03:00
|
|
|
m_keys_widget = m_keys_and_knobs_container->add<KeysWidget>(track_manager.keyboard());
|
2020-01-30 13:02:45 -03:00
|
|
|
|
2020-06-15 02:33:53 -03:00
|
|
|
m_knobs_widget = m_keys_and_knobs_container->add<KnobsWidget>(track_manager, *this);
|
2020-10-06 00:31:01 -03:00
|
|
|
|
|
|
|
|
m_roll_widget->set_keys_widget(m_keys_widget);
|
2020-01-30 13:02:45 -03:00
|
|
|
}
|
|
|
|
|
|
2021-12-05 01:42:35 -03:00
|
|
|
void MainWidget::add_track_actions(GUI::Menu& menu)
|
2020-06-17 06:34:25 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
menu.add_action(GUI::Action::create("&Add Track", { Mod_Ctrl, Key_T }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/plus.png"sv).release_value_but_fixme_should_propagate_errors(), [&](auto&) {
|
2021-12-05 01:42:35 -03:00
|
|
|
m_player_widget->add_track();
|
2020-06-17 06:34:25 -03:00
|
|
|
}));
|
|
|
|
|
|
2022-07-11 14:32:29 -03:00
|
|
|
menu.add_action(GUI::Action::create("&Next Track", { Mod_Ctrl, Key_N }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-last.png"sv).release_value_but_fixme_should_propagate_errors(), [&](auto&) {
|
2020-06-17 06:34:25 -03:00
|
|
|
turn_off_pressed_keys();
|
2021-12-05 01:42:35 -03:00
|
|
|
m_player_widget->next_track();
|
2020-06-17 06:34:25 -03:00
|
|
|
turn_on_pressed_keys();
|
|
|
|
|
|
|
|
|
|
m_knobs_widget->update_knobs();
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-30 13:02:45 -03:00
|
|
|
// FIXME: There are some unnecessary calls to update() throughout this program,
|
|
|
|
|
// which are an easy target for optimization.
|
|
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
void MainWidget::custom_event(Core::CustomEvent&)
|
2020-01-30 13:02:45 -03:00
|
|
|
{
|
|
|
|
|
m_wave_widget->update();
|
2020-02-26 11:01:17 -03:00
|
|
|
m_roll_widget->update();
|
2020-01-30 13:02:45 -03:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
void MainWidget::keydown_event(GUI::KeyEvent& event)
|
2020-01-30 13:02:45 -03:00
|
|
|
{
|
|
|
|
|
// This is to stop held-down keys from creating multiple events.
|
|
|
|
|
if (m_keys_pressed[event.key()])
|
|
|
|
|
return;
|
|
|
|
|
else
|
|
|
|
|
m_keys_pressed[event.key()] = true;
|
|
|
|
|
|
2022-07-17 06:35:31 -03:00
|
|
|
note_key_action(event.key(), DSP::Keyboard::Switch::On);
|
2020-01-30 13:02:45 -03:00
|
|
|
special_key_action(event.key());
|
|
|
|
|
m_keys_widget->update();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
void MainWidget::keyup_event(GUI::KeyEvent& event)
|
2020-01-30 13:02:45 -03:00
|
|
|
{
|
|
|
|
|
m_keys_pressed[event.key()] = false;
|
|
|
|
|
|
2022-07-17 06:35:31 -03:00
|
|
|
note_key_action(event.key(), DSP::Keyboard::Switch::Off);
|
2020-01-30 13:02:45 -03:00
|
|
|
m_keys_widget->update();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-17 06:35:31 -03:00
|
|
|
void MainWidget::note_key_action(int key_code, DSP::Keyboard::Switch switch_note)
|
2020-01-30 13:02:45 -03:00
|
|
|
{
|
2022-05-13 20:11:23 -03:00
|
|
|
auto key = m_keys_widget->key_code_to_key(key_code);
|
|
|
|
|
if (key == -1)
|
|
|
|
|
return;
|
|
|
|
|
m_track_manager.keyboard()->set_keyboard_note_in_active_octave(key, switch_note);
|
2020-01-30 13:02:45 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MainWidget::special_key_action(int key_code)
|
|
|
|
|
{
|
|
|
|
|
switch (key_code) {
|
|
|
|
|
case Key_Z:
|
2022-07-17 06:35:31 -03:00
|
|
|
set_octave_and_ensure_note_change(DSP::Keyboard::Direction::Down);
|
2020-01-30 13:02:45 -03:00
|
|
|
break;
|
|
|
|
|
case Key_X:
|
2022-07-17 06:35:31 -03:00
|
|
|
set_octave_and_ensure_note_change(DSP::Keyboard::Direction::Up);
|
2020-01-30 13:02:45 -03:00
|
|
|
break;
|
2021-12-20 14:26:20 -03:00
|
|
|
case Key_Space:
|
|
|
|
|
m_player_widget->toggle_paused();
|
|
|
|
|
break;
|
2020-01-30 13:02:45 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-17 06:34:25 -03:00
|
|
|
void MainWidget::turn_off_pressed_keys()
|
2020-01-30 13:02:45 -03:00
|
|
|
{
|
2022-05-13 20:11:23 -03:00
|
|
|
if (m_keys_widget->mouse_note() != -1)
|
2022-07-17 06:35:31 -03:00
|
|
|
m_track_manager.keyboard()->set_keyboard_note_in_active_octave(m_keys_widget->mouse_note(), DSP::Keyboard::Switch::Off);
|
2020-01-30 13:02:45 -03:00
|
|
|
for (int i = 0; i < key_code_count; ++i) {
|
|
|
|
|
if (m_keys_pressed[i])
|
2022-07-17 06:35:31 -03:00
|
|
|
note_key_action(i, DSP::Keyboard::Switch::Off);
|
2020-01-30 13:02:45 -03:00
|
|
|
}
|
2020-06-17 06:34:25 -03:00
|
|
|
}
|
2020-01-30 13:02:45 -03:00
|
|
|
|
2020-06-17 06:34:25 -03:00
|
|
|
void MainWidget::turn_on_pressed_keys()
|
|
|
|
|
{
|
2022-05-13 20:11:23 -03:00
|
|
|
if (m_keys_widget->mouse_note() != -1)
|
2022-07-17 06:35:31 -03:00
|
|
|
m_track_manager.keyboard()->set_keyboard_note_in_active_octave(m_keys_widget->mouse_note(), DSP::Keyboard::Switch::On);
|
2020-01-30 13:02:45 -03:00
|
|
|
for (int i = 0; i < key_code_count; ++i) {
|
|
|
|
|
if (m_keys_pressed[i])
|
2022-07-17 06:35:31 -03:00
|
|
|
note_key_action(i, DSP::Keyboard::Switch::On);
|
2020-01-30 13:02:45 -03:00
|
|
|
}
|
2020-06-17 06:34:25 -03:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 17:52:29 -03:00
|
|
|
void MainWidget::set_octave_and_ensure_note_change(int octave)
|
|
|
|
|
{
|
|
|
|
|
turn_off_pressed_keys();
|
2022-05-13 20:11:23 -03:00
|
|
|
MUST(m_track_manager.keyboard()->set_virtual_keyboard_octave(octave));
|
2020-07-06 17:52:29 -03:00
|
|
|
turn_on_pressed_keys();
|
|
|
|
|
|
|
|
|
|
m_knobs_widget->update_knobs();
|
|
|
|
|
m_keys_widget->update();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-17 06:35:31 -03:00
|
|
|
void MainWidget::set_octave_and_ensure_note_change(DSP::Keyboard::Direction direction)
|
2020-06-17 06:34:25 -03:00
|
|
|
{
|
|
|
|
|
turn_off_pressed_keys();
|
2022-05-13 20:11:23 -03:00
|
|
|
m_track_manager.keyboard()->change_virtual_keyboard_octave(direction);
|
2020-06-17 06:34:25 -03:00
|
|
|
turn_on_pressed_keys();
|
2020-01-30 13:02:45 -03:00
|
|
|
|
|
|
|
|
m_knobs_widget->update_knobs();
|
|
|
|
|
m_keys_widget->update();
|
|
|
|
|
}
|