summaryrefslogtreecommitdiff
path: root/Userland/Applications/Piano/KeysWidget.cpp
blob: 5d128b4523bfa6477a07b65ef10157a496121a97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2019-2020, William McPherson <willmcpherson2@gmail.com>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "KeysWidget.h"
#include "TrackManager.h"
#include <AK/Array.h>
#include <AK/StringView.h>
#include <LibGUI/Painter.h>

KeysWidget::KeysWidget(TrackManager& track_manager)
    : m_track_manager(track_manager)
{
    set_fill_with_background_color(true);
}

int KeysWidget::mouse_note() const
{
    if (m_mouse_down && m_mouse_note + m_track_manager.octave_base() < note_count)
        return m_mouse_note; // Can be -1.
    else
        return -1;
}

void KeysWidget::set_key(int key, Switch switch_key)
{
    if (key == -1 || key + m_track_manager.octave_base() >= note_count)
        return;

    if (switch_key == On) {
        ++m_key_on[key];
    } else {
        if (m_key_on[key] >= 1)
            --m_key_on[key];
    }
    VERIFY(m_key_on[key] <= 2);

    m_track_manager.set_keyboard_note(key + m_track_manager.octave_base(), switch_key);
}

bool KeysWidget::note_is_set(int note) const
{
    if (note < m_track_manager.octave_base())
        return false;

    if (note >= m_track_manager.octave_base() + note_count)
        return false;

    return m_key_on[note - m_track_manager.octave_base()] != 0;
}

int KeysWidget::key_code_to_key(int key_code) const
{
    switch (key_code) {
    case Key_A:
        return 0;
    case Key_W:
        return 1;
    case Key_S:
        return 2;
    case Key_E:
        return 3;
    case Key_D:
        return 4;
    case Key_F:
        return 5;
    case Key_T:
        return 6;
    case Key_G:
        return 7;
    case Key_Y:
        return 8;
    case Key_H:
        return 9;
    case Key_U:
        return 10;
    case Key_J:
        return 11;
    case Key_K:
        return 12;
    case Key_O:
        return 13;
    case Key_L:
        return 14;
    case Key_P:
        return 15;
    case Key_Semicolon:
        return 16;
    case Key_Apostrophe:
        return 17;
    case Key_RightBracket:
        return 18;
    case Key_Return:
        return 19;
    default:
        return -1;
    }
}

constexpr int white_key_width = 24;
constexpr int black_key_width = 16;
constexpr int black_key_x_offset = black_key_width / 2;
constexpr int black_key_height = 60;

constexpr int white_key_labels_count = 12;
constexpr Array<StringView, white_key_labels_count> white_key_labels = {
    "A",
    "S",
    "D",
    "F",
    "G",
    "H",
    "J",
    "K",
    "L",
    ";",
    "\'",
    "\u23CE", // Return key symbol
};

constexpr int black_key_labels_count = 8;
constexpr Array<StringView, black_key_labels_count> black_key_labels = {
    "W",
    "E",
    "T",
    "Y",
    "U",
    "O",
    "P",
    "]",
};

constexpr int black_key_offsets[] = {
    white_key_width,
    white_key_width * 2,
    white_key_width,
    white_key_width,
    white_key_width * 2,
};

constexpr int white_key_note_accumulator[] = {
    2,
    2,
    1,
    2,
    2,
    2,
    1,
};

constexpr int black_key_note_accumulator[] = {
    2,
    3,
    2,
    2,
    3,
};

void KeysWidget::paint_event(GUI::PaintEvent& event)
{
    GUI::Painter painter(*this);
    painter.translate(frame_thickness(), frame_thickness());

    int note = 0;
    int x = 0;
    int i = 0;
    for (;;) {
        Gfx::IntRect rect(x, 0, white_key_width, frame_inner_rect().height());
        painter.fill_rect(rect, m_key_on[note] ? note_pressed_color : Color::White);
        painter.draw_rect(rect, Color::Black);
        if (i < white_key_labels_count) {
            rect.set_height(rect.height() * 1.5);
            painter.draw_text(rect, white_key_labels[i], Gfx::TextAlignment::Center, Color::Black);
        }

        note += white_key_note_accumulator[i % white_keys_per_octave];
        x += white_key_width;
        ++i;

        if (note + m_track_manager.octave_base() >= note_count)
            break;
        if (x >= frame_inner_rect().width())
            break;
    }

    note = 1;
    x = white_key_width - black_key_x_offset;
    i = 0;
    for (;;) {
        Gfx::IntRect rect(x, 0, black_key_width, black_key_height);
        painter.fill_rect(rect, m_key_on[note] ? note_pressed_color : Color::Black);
        painter.draw_rect(rect, Color::Black);
        if (i < black_key_labels_count) {
            rect.set_height(rect.height() * 1.5);
            painter.draw_text(rect, black_key_labels[i], Gfx::TextAlignment::Center, Color::White);
        }

        note += black_key_note_accumulator[i % black_keys_per_octave];
        x += black_key_offsets[i % black_keys_per_octave];
        ++i;

        if (note + m_track_manager.octave_base() >= note_count)
            break;
        if (x >= frame_inner_rect().width())
            break;
    }

    GUI::Frame::paint_event(event);
}

constexpr int notes_per_white_key[] = {
    1,
    3,
    5,
    6,
    8,
    10,
    12,
};

// Keep in mind that in any of these functions a note value can be out of
// bounds. Bounds checking is done in set_key().

static inline int note_from_white_keys(int white_keys)
{
    int octaves = white_keys / white_keys_per_octave;
    int remainder = white_keys % white_keys_per_octave;
    int notes_from_octaves = octaves * notes_per_octave;
    int notes_from_remainder = notes_per_white_key[remainder];
    int note = (notes_from_octaves + notes_from_remainder) - 1;
    return note;
}

int KeysWidget::note_for_event_position(const Gfx::IntPoint& a_point) const
{
    if (!frame_inner_rect().contains(a_point))
        return -1;

    auto point = a_point;
    point.translate_by(-frame_thickness(), -frame_thickness());

    int white_keys = point.x() / white_key_width;
    int note = note_from_white_keys(white_keys);

    bool black_key_on_left = note != 0 && key_pattern[(note - 1) % notes_per_octave] == Black;
    if (black_key_on_left) {
        int black_key_x = (white_keys * white_key_width) - black_key_x_offset;
        Gfx::IntRect black_key(black_key_x, 0, black_key_width, black_key_height);
        if (black_key.contains(point))
            return note - 1;
    }

    bool black_key_on_right = key_pattern[(note + 1) % notes_per_octave] == Black;
    if (black_key_on_right) {
        int black_key_x = ((white_keys + 1) * white_key_width) - black_key_x_offset;
        Gfx::IntRect black_key(black_key_x, 0, black_key_width, black_key_height);
        if (black_key.contains(point))
            return note + 1;
    }

    return note;
}

void KeysWidget::mousedown_event(GUI::MouseEvent& event)
{
    if (event.button() != GUI::MouseButton::Primary)
        return;

    m_mouse_down = true;

    m_mouse_note = note_for_event_position(event.position());

    set_key(m_mouse_note, On);
    update();
}

void KeysWidget::mouseup_event(GUI::MouseEvent& event)
{
    if (event.button() != GUI::MouseButton::Primary)
        return;

    m_mouse_down = false;

    set_key(m_mouse_note, Off);
    update();
}

void KeysWidget::mousemove_event(GUI::MouseEvent& event)
{
    if (!m_mouse_down)
        return;

    int new_mouse_note = note_for_event_position(event.position());

    if (m_mouse_note == new_mouse_note)
        return;

    set_key(m_mouse_note, Off);
    set_key(new_mouse_note, On);
    update();

    m_mouse_note = new_mouse_note;
}