-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatternHistoryListBox.cpp
More file actions
589 lines (492 loc) · 18.5 KB
/
PatternHistoryListBox.cpp
File metadata and controls
589 lines (492 loc) · 18.5 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
==============================================================================
PatternHistoryListBox.cpp
Created: 30 Jul 2025
Author: BMad Master
Implementation of Pattern History Panel UI Component.
==============================================================================
*/
#include "PatternHistoryListBox.h"
#include <regex>
//==============================================================================
PatternHistoryListBox::PatternHistoryListBox(PatternManager& pm)
: patternManager(pm), searchComponent()
{
setupListBox();
// Setup search component
addAndMakeVisible(searchComponent);
searchComponent.onSearchChanged = [this](const PatternSearchComponent::FilterCriteria& criteria) {
setSearchCriteria(criteria);
};
// Register for pattern manager updates
patternManager.addChangeListener(this);
// Initial pattern list update
updatePatternList();
}
PatternHistoryListBox::~PatternHistoryListBox()
{
patternManager.removeChangeListener(this);
}
//==============================================================================
void PatternHistoryListBox::paint(juce::Graphics& g)
{
g.fillAll(backgroundColour);
// Draw header
auto headerArea = getLocalBounds().removeFromTop(30);
g.setColour(textColour);
g.setFont(juce::FontOptions(16.0f, juce::Font::bold));
g.drawText("Pattern History", headerArea, juce::Justification::centred);
// Draw border
g.setColour(juce::Colour(0xff404040));
g.drawRect(getLocalBounds(), 1);
}
void PatternHistoryListBox::resized()
{
auto bounds = getLocalBounds();
// Header space
auto headerArea = bounds.removeFromTop(30);
// Search component area (100px height)
auto searchArea = bounds.removeFromTop(100);
searchArea.reduce(2, 2);
searchComponent.setBounds(searchArea);
// Remaining space for list
bounds.reduce(2, 2); // Border padding
listBox.setBounds(bounds);
}
//==============================================================================
void PatternHistoryListBox::setupListBox()
{
listBox.setModel(this);
listBox.setColour(juce::ListBox::backgroundColourId, backgroundColour);
listBox.setColour(juce::ListBox::outlineColourId, juce::Colours::transparentBlack);
listBox.setRowHeight(60);
listBox.setMultipleSelectionEnabled(false);
addAndMakeVisible(listBox);
}
//==============================================================================
// ListBoxModel implementation
int PatternHistoryListBox::getNumRows()
{
return static_cast<int>(filteredPatterns.size());
}
void PatternHistoryListBox::paintListBoxItem(int rowNumber, juce::Graphics& g,
int width, int height, bool rowIsSelected)
{
// Background handled by PatternRowComponent
}
juce::Component* PatternHistoryListBox::refreshComponentForRow(int rowNumber, bool isRowSelected,
juce::Component* existingComponentToUpdate)
{
if (rowNumber >= 0 && rowNumber < static_cast<int>(filteredPatterns.size()))
{
auto* rowComponent = dynamic_cast<PatternRowComponent*>(existingComponentToUpdate);
if (rowComponent == nullptr)
{
rowComponent = new PatternRowComponent(*this, rowNumber);
}
rowComponent->updatePattern(filteredPatterns[rowNumber], rowNumber, isRowSelected || rowNumber == selectedPatternIndex);
return rowComponent;
}
delete existingComponentToUpdate;
return nullptr;
}
void PatternHistoryListBox::listBoxItemClicked(int row, const juce::MouseEvent& e)
{
if (row >= 0 && row < static_cast<int>(filteredPatterns.size()))
{
selectPattern(row);
}
}
//==============================================================================
void PatternHistoryListBox::changeListenerCallback(juce::ChangeBroadcaster* source)
{
if (source == &patternManager)
{
// Update on message thread
juce::MessageManager::callAsync([this]()
{
updatePatternList();
});
}
}
//==============================================================================
void PatternHistoryListBox::buttonClicked(juce::Button* button)
{
// Button handling is delegated to PatternRowComponent
}
//==============================================================================
void PatternHistoryListBox::updatePatternList()
{
// Get recent patterns from manager (last 20 for UI display)
currentPatterns = patternManager.getRecentPatterns(20);
// Reverse order so newest patterns appear at top
std::reverse(currentPatterns.begin(), currentPatterns.end());
// Update filtered patterns based on current search criteria
updateFilteredPatterns();
// Update list box
listBox.updateContent();
listBox.repaint();
// Maintain selection if valid
if (selectedPatternIndex >= static_cast<int>(filteredPatterns.size()))
{
selectedPatternIndex = -1;
}
}
void PatternHistoryListBox::selectPattern(int index)
{
if (index >= 0 && index < static_cast<int>(currentPatterns.size()))
{
selectedPatternIndex = index;
listBox.selectRow(index);
listBox.repaint();
if (onPatternSelect)
{
onPatternSelect(index);
}
}
}
void PatternHistoryListBox::handlePatternAction(int patternIndex, const juce::String& action)
{
if (patternIndex < 0 || patternIndex >= static_cast<int>(currentPatterns.size()))
return;
if (action == "preview" && onPatternPreview)
{
onPatternPreview(patternIndex);
}
else if (action == "delete" && onPatternDelete)
{
// Show confirmation dialog
auto options = juce::MessageBoxOptions()
.withIconType(juce::AlertWindow::QuestionIcon)
.withTitle("Delete Pattern")
.withMessage("Are you sure you want to delete this pattern from history?")
.withButton("Delete")
.withButton("Cancel");
juce::AlertWindow::showAsync(options, [this, patternIndex](int result)
{
if (result == 1) // Delete button
{
onPatternDelete(patternIndex);
}
});
}
}
//==============================================================================
// PatternRowComponent Implementation
PatternHistoryListBox::PatternRowComponent::PatternRowComponent(PatternHistoryListBox& parent, int rowIndex)
: parentList(parent), patternIndex(rowIndex)
{
// Setup buttons
previewButton.setButtonText("▶");
previewButton.setColour(juce::TextButton::buttonColourId, juce::Colour(0xff4a9eff));
previewButton.setColour(juce::TextButton::textColourOffId, juce::Colours::white);
previewButton.onClick = [this]() { parentList.handlePatternAction(patternIndex, "preview"); };
addAndMakeVisible(previewButton);
favoriteButton.setButtonText("♡");
favoriteButton.setColour(juce::TextButton::buttonColourId, juce::Colour(0xff555555));
favoriteButton.setColour(juce::TextButton::textColourOffId, juce::Colours::white);
favoriteButton.onClick = [this]()
{
isFavorited = !isFavorited;
favoriteButton.setButtonText(isFavorited ? "♥" : "♡");
favoriteButton.setColour(juce::TextButton::buttonColourId,
isFavorited ? juce::Colour(0xffff4444) : juce::Colour(0xff555555));
};
addAndMakeVisible(favoriteButton);
deleteButton.setButtonText("×");
deleteButton.setColour(juce::TextButton::buttonColourId, juce::Colour(0xff666666));
deleteButton.setColour(juce::TextButton::textColourOffId, juce::Colours::white);
deleteButton.onClick = [this]() { parentList.handlePatternAction(patternIndex, "delete"); };
addAndMakeVisible(deleteButton);
}
void PatternHistoryListBox::PatternRowComponent::paint(juce::Graphics& g)
{
auto bounds = getLocalBounds();
// Background
if (isSelectedRow)
{
g.setColour(parentList.selectedColour);
g.fillRect(bounds);
}
else if (isDragging)
{
// Semi-transparent when being dragged
g.setColour(parentList.backgroundColour.withAlpha(0.7f));
g.fillRect(bounds);
}
else
{
g.setColour(parentList.backgroundColour);
g.fillRect(bounds);
}
// Border
g.setColour(juce::Colour(0xff404040));
g.drawRect(bounds, 1);
// Draw drop indicator if this is a drop target
drawDropIndicator(g);
// Pattern info text
auto textArea = bounds.reduced(10, 5);
textArea.removeFromRight(150); // Space for buttons
g.setColour(parentList.textColour.withAlpha(isDragging ? 0.7f : 1.0f));
g.setFont(juce::FontOptions(12.0f));
g.drawText(formatPatternInfo(), textArea.removeFromTop(20), juce::Justification::topLeft);
// Mini piano roll visualization
if (textArea.getHeight() > 10)
{
auto pianoRollArea = textArea.reduced(0, 2);
drawMiniPianoRoll(g, pianoRollArea);
}
}
void PatternHistoryListBox::PatternRowComponent::resized()
{
auto bounds = getLocalBounds();
auto buttonArea = bounds.removeFromRight(140);
buttonArea = buttonArea.reduced(5);
// Arrange buttons horizontally
auto buttonWidth = (buttonArea.getWidth() - 10) / 3;
previewButton.setBounds(buttonArea.removeFromLeft(buttonWidth));
buttonArea.removeFromLeft(5);
favoriteButton.setBounds(buttonArea.removeFromLeft(buttonWidth));
buttonArea.removeFromLeft(5);
deleteButton.setBounds(buttonArea);
}
void PatternHistoryListBox::PatternRowComponent::updatePattern(const MIDIPattern& pattern, int index, bool isSelected)
{
currentPattern = pattern;
patternIndex = index;
isSelectedRow = isSelected;
repaint();
}
void PatternHistoryListBox::PatternRowComponent::drawMiniPianoRoll(juce::Graphics& g, juce::Rectangle<int> area)
{
if (area.getWidth() < 10 || area.getHeight() < 10)
return;
// Simple visualization - draw note blocks
g.setColour(juce::Colour(0xff666666));
g.drawRect(area, 1);
if (currentPattern.notes.empty())
return;
// Find note range for scaling
int minNote = 127, maxNote = 0;
double maxTime = 0.0;
for (const auto& note : currentPattern.notes)
{
minNote = juce::jmin(minNote, note.pitch);
maxNote = juce::jmax(maxNote, note.pitch);
maxTime = juce::jmax(maxTime, note.startTime + note.duration);
}
if (maxNote <= minNote || maxTime <= 0.0)
return;
// Draw notes
g.setColour(parentList.accentColour);
auto drawArea = area.reduced(2);
for (const auto& note : currentPattern.notes)
{
// Scale position
float x = static_cast<float>(note.startTime / maxTime) * drawArea.getWidth();
float width = juce::jmax(2.0f, static_cast<float>(note.duration / maxTime) * drawArea.getWidth());
float y = static_cast<float>(maxNote - note.pitch) / (maxNote - minNote) * drawArea.getHeight();
float height = juce::jmax(1.0f, drawArea.getHeight() / 12.0f); // Approximate note height
juce::Rectangle<float> noteRect(drawArea.getX() + x, drawArea.getY() + y, width, height);
g.fillRect(noteRect);
}
}
juce::String PatternHistoryListBox::PatternRowComponent::formatPatternInfo() const
{
auto timeStr = juce::Time::getCurrentTime().toString(true, true, false, true);
auto noteCount = static_cast<int>(currentPattern.notes.size());
return juce::String("Pattern #") + juce::String(patternIndex + 1) +
" - " + juce::String(noteCount) + " notes - " + timeStr;
}
//==============================================================================
// Epic 3 Story 3.3: Drag & Drop Implementation
void PatternHistoryListBox::reorderPattern(int fromIndex, int toIndex)
{
if (fromIndex == toIndex || fromIndex < 0 || toIndex < 0 ||
fromIndex >= static_cast<int>(currentPatterns.size()) ||
toIndex >= static_cast<int>(currentPatterns.size()))
return;
// Reorder patterns in our local list
auto patternToMove = currentPatterns[fromIndex];
currentPatterns.erase(currentPatterns.begin() + fromIndex);
currentPatterns.insert(currentPatterns.begin() + toIndex, patternToMove);
// Update the list box display
listBox.updateContent();
listBox.repaint();
// Update selection to follow the moved pattern
if (selectedPatternIndex == fromIndex)
{
selectedPatternIndex = toIndex;
listBox.selectRow(toIndex);
}
else if (selectedPatternIndex > fromIndex && selectedPatternIndex <= toIndex)
{
selectedPatternIndex--;
}
else if (selectedPatternIndex < fromIndex && selectedPatternIndex >= toIndex)
{
selectedPatternIndex++;
}
}
// PatternRowComponent Drag & Drop Implementation
void PatternHistoryListBox::PatternRowComponent::mouseDown(const juce::MouseEvent& e)
{
if (e.mods.isLeftButtonDown() && !e.mods.isRightButtonDown())
{
// Don't start drag if clicking on buttons
if (previewButton.getBounds().contains(e.getPosition()) ||
favoriteButton.getBounds().contains(e.getPosition()) ||
deleteButton.getBounds().contains(e.getPosition()))
{
return;
}
// Prepare for potential drag operation
isDragging = false;
}
}
void PatternHistoryListBox::PatternRowComponent::mouseDrag(const juce::MouseEvent& e)
{
if (e.mods.isLeftButtonDown() && !isDragging)
{
// Start drag operation if moved sufficient distance
if (e.getDistanceFromDragStart() > 10)
{
isDragging = true;
repaint();
// For now, just enable visual feedback
// TODO: Implement actual drag and drop in future iteration
}
}
}
bool PatternHistoryListBox::PatternRowComponent::isInterestedInDragSource(const juce::DragAndDropTarget::SourceDetails& dragSourceDetails)
{
// Only accept pattern row drags
return dragSourceDetails.description.toString().startsWith("PatternRow:");
}
void PatternHistoryListBox::PatternRowComponent::itemDragEnter(const juce::DragAndDropTarget::SourceDetails& dragSourceDetails)
{
auto draggedIndex = dragSourceDetails.description.toString().fromFirstOccurrenceOf(":", false, false).getIntValue();
// Don't accept drops on self
if (draggedIndex != patternIndex)
{
isDropTarget = true;
// Determine if drop indicator should be above or below
auto localMousePos = dragSourceDetails.localPosition;
showDropIndicatorAbove = localMousePos.getY() < getHeight() / 2;
repaint();
}
}
void PatternHistoryListBox::PatternRowComponent::itemDragExit(const juce::DragAndDropTarget::SourceDetails& dragSourceDetails)
{
isDropTarget = false;
repaint();
}
void PatternHistoryListBox::PatternRowComponent::itemDropped(const juce::DragAndDropTarget::SourceDetails& dragSourceDetails)
{
auto draggedIndex = dragSourceDetails.description.toString().fromFirstOccurrenceOf(":", false, false).getIntValue();
if (draggedIndex != patternIndex)
{
// Calculate target index based on drop position
int targetIndex = patternIndex;
if (!showDropIndicatorAbove)
{
targetIndex++;
}
// Adjust for source removal
if (draggedIndex < targetIndex)
{
targetIndex--;
}
// Perform the reorder
parentList.reorderPattern(draggedIndex, targetIndex);
}
isDropTarget = false;
isDragging = false;
repaint();
}
void PatternHistoryListBox::PatternRowComponent::drawDropIndicator(juce::Graphics& g)
{
if (!isDropTarget)
return;
// Draw drop indicator line
g.setColour(juce::Colour(0xff4a9eff));
auto bounds = getLocalBounds();
int y = showDropIndicatorAbove ? bounds.getY() : bounds.getBottom() - 1;
// Draw thick line with some glow effect
for (int i = 0; i < 3; ++i)
{
g.setOpacity(0.7f - i * 0.2f);
g.drawHorizontalLine(y + i - 1, static_cast<float>(bounds.getX() + 5), static_cast<float>(bounds.getRight() - 5));
}
}
//==============================================================================
// Epic 3 Story 3.3: Search and filtering implementation
void PatternHistoryListBox::setSearchCriteria(const PatternSearchComponent::FilterCriteria& criteria)
{
currentFilter = criteria;
updateFilteredPatterns();
}
void PatternHistoryListBox::refreshFilteredPatterns()
{
updateFilteredPatterns();
}
bool PatternHistoryListBox::matchesFilter(const MIDIPattern& pattern) const
{
// Search text filter - search in pattern ID or metadata
if (!currentFilter.searchText.isEmpty())
{
juce::String searchText = currentFilter.searchText.toLowerCase();
juce::String patternId = pattern.id.toString().toLowerCase();
if (currentFilter.useRegex)
{
try
{
std::regex searchRegex(searchText.toStdString(), std::regex_constants::icase);
if (!std::regex_search(patternId.toStdString(), searchRegex))
return false;
}
catch (const std::regex_error&)
{
// Fall back to simple text search if regex is invalid
if (!patternId.contains(searchText))
return false;
}
}
else
{
if (!patternId.contains(searchText))
return false;
}
}
// Note count range filter
int noteCount = static_cast<int>(pattern.notes.size());
if (!currentFilter.noteCountRange.contains(noteCount))
return false;
// Tempo range filter using metadata tempo
if (pattern.metadata.tempo != 0.0f)
{
int patternBpm = static_cast<int>(pattern.metadata.tempo);
if (!currentFilter.tempoRange.contains(patternBpm))
return false;
}
return true;
}
void PatternHistoryListBox::updateFilteredPatterns()
{
filteredPatterns.clear();
for (const auto& pattern : currentPatterns)
{
if (matchesFilter(pattern))
{
filteredPatterns.push_back(pattern);
}
}
// Update UI
listBox.updateContent();
listBox.repaint();
// Reset selection if it's no longer valid
if (selectedPatternIndex >= static_cast<int>(filteredPatterns.size()))
{
selectedPatternIndex = -1;
}
}